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

Fix TLS dtor + memory leaks on macOS #443

Closed
RalfJung opened this issue Aug 30, 2018 · 6 comments · Fixed by #1267
Closed

Fix TLS dtor + memory leaks on macOS #443

RalfJung opened this issue Aug 30, 2018 · 6 comments · Fixed by #1267
Labels
A-leaks Area: affects the memory leak checker A-mac Area: Affects only macOS targets A-shims Area: This affects the external function shims C-bug Category: This is a bug.

Comments

@RalfJung
Copy link
Member

We currently ignore memory leaks on macOS. Likely, this is due to not properly executing TLS destructors in _tlv_atexit. Debugging this requires a libstd with full MIR for macOS, which currently we do not know how to do cross-platform -- so, someone who's actually using macOS should take this.

@RalfJung RalfJung added C-bug Category: This is a bug. A-shims Area: This affects the external function shims labels Nov 17, 2018
@mtak-
Copy link

mtak- commented Mar 12, 2019

IIUC this was fixed here: rust-lang/rust#57534 rust-lang/rust#57655

@RalfJung
Copy link
Member Author

The issue here is about Miri entirely ignoring _tlv_atexit. This is a Miri issue, not a rustc issue, and I don't think these PRs fix it.

@RalfJung RalfJung added the A-mac Area: Affects only macOS targets label Apr 8, 2019
@kungfukennyg
Copy link
Contributor

I'd like to take a crack at this issue. I have a macOS laptop, just need some guidance on where to get started. 😄

@RalfJung
Copy link
Member Author

RalfJung commented Nov 2, 2019

@kennethbgoodin great! Not knowing the macOS APIs in detail, I can only guess here. The first step is to make sure that you can locally build and run Miri; please let me know if you need any help with that.

Then remove macOS from this line to enable leak checking on macOS. ./miri test should fail now. Note which test(s) failed so that you can run them directly to check your changes!

My guess for what you will need to implement is this stub. Unfortunately I found no docs for _tlv_atexit, but it is instructive to see how Rust calls that function, which happens here. You will have to add a new field to Miri's Machine to remember the fn ptr that is to be called on exit, and then call that function around here.

These are high-level instructions; I can go into more detail once you have concrete questions. :)

@kungfukennyg
Copy link
Contributor

Okay I've done the first steps and have which tests are failing (168!).

From looking at what rustc does it sounds like our _tlv_atexit needs to accept a thread local list of destructors for each thread's globals. I'm not sure how I would go about constructing that list though.

Adding the field to Machine and calling that function ptr sounds straightforward enough.

@RalfJung
Copy link
Member Author

RalfJung commented Nov 3, 2019

Okay I've done the first steps and have which tests are failing (168!).

That's likely the same cause for all of them, just pick one (let's say ./miri run tests/run-pass/bools.rs) and work on that for now.

From looking at what rustc does it sounds like our _tlv_atexit needs to accept a thread local list of destructors for each thread's globals. I'm not sure how I would go about constructing that list though.

Why a list? All I can see is a single function pointer, and a pointer that is passed to that function as an argument.

@bors bors closed this as completed in f4308a0 Mar 28, 2020
@RalfJung RalfJung added the A-leaks Area: affects the memory leak checker label Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-leaks Area: affects the memory leak checker A-mac Area: Affects only macOS targets A-shims Area: This affects the external function shims C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants