Skip to content

Memory leak when calling var_set on a pointer that is dereferenced with array syntax #1882

@alexmanning9904

Description

@alexmanning9904

Summary

I have a trick sim with a variable var along with a pointer to var, varptr. I can use python bindings to set var by dereferencing varptr using trick.var_set("varptr[0]"). This functions correctly, but seems to cause a memory leak within the Trick MemoryManager.

Reproduction

Environment

  • Trick (my commit hash is 0482202, I'll take a TODO to reproduce this off of the head of master)
  • Valgrind. I installed via my package manager which gave v3.24.0

Sim Files

My sim folder is contained in minimal_leak_demo.zip. This includes:

  • An S_define file with a static variable and statically-defined pointer to it.
  • An input.py file that forces the memory leak
  • An S_overrides.mk
  • A demo.sh script to force the memory leak

Steps

  1. Extract minimal_leak_demo.zip
  2. cd into the extracted directory
  3. Run demo.sh
  4. Wait. This demo took ~2 hours to give me data on my machine. You can get faster results by reducing the number of trick.add_read calls in input.py, this just makes the leak a bit less obvious when browsing the massif outputs.

Outputs

The most important output is the results.txt file, which is produced by massif/ms_print. It contains a memory usage vs instructions executed graph alongside a few memory utilization snapshots.

Analysis

From performing the steps listed above, I got this results.txt. I copied out the memory allocation trace from snapshot 49 and snapshot 97. Snapshot 49 was the first snapshot taken when the leak was large enough for massif to print it, and snapshot 97 is the final and largest snapshot. The size difference between these two snapshots is ~170MB. The allocation within:

->11.03% (207,756,640B) 0xA1459C: Trick::MemoryManager::make_reference_attr(ALLOC_INFO*) (in /home/amanning/trick-memory-leak-hell/SIM_minimal_leak_demo/S_main_Linux_11.5_x86_64.exe)
| ->11.03% (207,756,640B) 0xA19C3C: Trick::MemoryManager::ref_var(REF2*, char*) (in /home/amanning/trick-memory-leak-hell/SIM_minimal_leak_demo/S_main_Linux_11.5_x86_64.exe)
|   ->11.03% (207,756,640B) 0x9FFEB0: REF_parse(RefParseContext*) (in /home/amanning/trick-memory-leak-hell/SIM_minimal_leak_demo/S_main_Linux_11.5_x86_64.exe)
|     ->11.03% (207,756,640B) 0xA18D23: Trick::MemoryManager::ref_attributes(char const*) (in /home/amanning/trick-memory-leak-hell/SIM_minimal_leak_demo/S_main_Linux_11.5_x86_64.exe)
|       ->11.03% (207,756,640B) 0xA092CD: ref_attributes (in /home/amanning/trick-memory-leak-hell/SIM_minimal_leak_demo/S_main_Linux_11.5_x86_64.exe)
|         ->11.03% (207,756,640B) 0x720977: int var_set_base<long long>(char const*, long long, char const*) (in /home/amanning/trick-memory-leak-hell/SIM_minimal_leak_demo/S_main_Linux_11.5_x86_64.exe)
|           ->11.03% (207,756,640B) 0x72069B: var_set(char const*, long long, char const*) (in /home/amanning/trick-memory-leak-hell/SIM_minimal_leak_demo/S_main_Linux_11.5_x86_64.exe)
|             ->11.03% (207,756,640B) 0x971C15: _wrap_var_set__SWIG_3 (in /home/amanning/trick-memory-leak-hell/SIM_minimal_leak_demo/S_main_Linux_11.5_x86_64.exe)
|               ->11.03% (207,756,640B) 0x972EC1: _wrap_var_set (in /home/amanning/trick-memory-leak-hell/SIM_minimal_leak_demo/S_main_Linux_11.5_x86_64.exe)

Accounts for ~130MB of this. Snapshot 97 also includes 2 additional traces that include var_set that look like they can explain the extra 40MB.


I've started to dig into the memory manager with the hope of fixing this, but I don't think that I'm anywhere close to a solution yet. I would appreciate any insight on corrective actions or workarounds. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions