Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-927] Integrate leaks infrastructure into stdlib unittests #43539

Open
gottesmm opened this issue Mar 12, 2016 · 4 comments
Open

[SR-927] Integrate leaks infrastructure into stdlib unittests #43539

gottesmm opened this issue Mar 12, 2016 · 4 comments
Labels
good first issue Good for newcomers improvement standard library Area: Standard library umbrella

Comments

@gottesmm
Copy link
Contributor

Previous ID SR-927
Radar None
Original Reporter @gottesmm
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Improvement, StarterBug
Assignee None
Priority Medium

md5: af9683a46d2c1fd2060e2a6887b581f4

Issue Description:

Currently we have special infrastructure for precise leaks checking that we use to check for leaks in our benchmarks.

The way this works is that when the pass -DSWIFT_RUNTIME_ENABLE_LEAK_CHECKER is set to yes, the runtime provides the two entry points:

swift_leaks_startTrackingObjects(const char *name)
swift_leaks_stopTrackingObjects(const char *name)

where name is a string with the name of the test being run. When startTrackingObjects is called, a global set is initialized. This is our "live object set". From that point forward, whenever we allocate a swift or objective-c object, we add it to the set. When ever we deallocate such an object, we remove the object from that set.

When stopTrackingObjects is called, the runtime emits via simple json the current state of the live object set.

The nice thing about this approach is that it enables fine-grained leaks testing of specific tests.

As mentioned at the top of the description, currently the leak checker has been integrated only into the benchmarks. This bug is to track the work of making changes to stdlib unit tests to enable the leaks checker to work on the stdlib unit tests.

This is a non-trivial amount of work, but would be a good starter bug that would improve the quality of the compiler.

@gottesmm
Copy link
Contributor Author

Some notes:

1. Stdlib unit tests runs tests in parallel processes but within each process runs the tests sequentially. We take advantage of this setup to ease the amount of concurrency work that is necessary. If at some point this changes, we will need to be much more careful about creating the global set and in fact will need to do the work to create multiple live object sets and only modify the one associated with the given test thread. That would be unfortunate = (... but luckily is not the case currently!

2. Currently the stdlib unit tests does do some leaks checking, but this checking:
a. IIRC is only for specific tests. It does not work across all tests.
b. Only works for classes (i.e. things with deinits). Any heap memory that is not a class and is allocated by allocObject/deallocObject can not be found by such leak detection.

@gottesmm
Copy link
Contributor Author

The code used by the benchmark is in DriverUtils.swift in the sections ifdefed by

SWIFT_RUNTIME_ENABLE_LEAK_CHECKER

@gparker42
Copy link
Mannequin

gparker42 mannequin commented Mar 30, 2016

libobjc's test machinery has a similar mechanic. Some notes from experience there:

  • libobjc's mechanism uses the malloc heap enumeration API to add up all heap allocations in the process. Good: this catches everything. Bad: this can be sensitive to non-leaks. For example, preheating caches can be difficult. Some tests use workarounds such as "run the test 10000 times, then pass if memory growth is not more than 4096 bytes".

  • It is useful to be able to set the leak mark once and then check it multiple times.

  • You may need to cycle a test many times to catch a leak. For example, if some operation incorrectly adds one entry to a global cache every time, you won't notice the leak until you repeat the operation enough times that the cache reallocates. For Swift's use this probably should be controllable externally: perform a small number of leak checking cycles for most testing, and perform a large number of leak checking cycles on some infrequently-run test bot.

  • Diagnosing the leaks is easier if there are environment variables or something to control the detector machinery. For example, you sometimes need to dump the set of tracked allocations at the start and end of tracking, or make the test process hang after leaks are found so other debugging tools can be applied.

@belkadan
Copy link
Contributor

Resetting assignee on all Starter Bugs not modified since 2018.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers improvement standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants