Skip to content

Make backtraces work with #[global_allocator] #1975

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

Merged
merged 1 commit into from
Mar 20, 2022

Conversation

beepster4096
Copy link
Contributor

@beepster4096 beepster4096 commented Feb 13, 2022

Currently, backtraces break when the global allocator is overridden because the allocator will attempt to deallocate memory allocated directly by Miri.

This PR fixes that by using a new memory kind and providing a function to deallocate it. We can't call the custom allocator to allocate because it's not possible to call a function in the middle of a shim.

This PR fixes that by adding a new version of the backtrace API accessible by setting flags to 1. Existing code still functions.

backtrace-rs PR: rust-lang/backtrace-rs#462

Fixes #1996

@oli-obk
Copy link
Contributor

oli-obk commented Feb 17, 2022

So, afaict in order to land this you'll need to remove the backtrace memorykind again, then land it here, then update backtrace-rs, then bump everything in rustc, then add the memory kind again.

Does that sound about right?

@beepster4096
Copy link
Contributor Author

Either that or special-casing this new memory kind so that it can be deallocated by the default allocator and removing that later, I think.

@RalfJung
Copy link
Member

We can't call the custom allocator to allocate because it's not possible to call a function in the middle of a shim.

Well, it is possible in principle, but it is really rather annoying...

Is there any way that we could change the backtrace API so that allocation is done by the user? Like, the user has to first call miri_get_backtrace_size, then create an allocation of that size, and then hand that to miri_get_backtrace?

@beepster4096
Copy link
Contributor Author

Is there any way that we could change the backtrace API so that allocation is done by the user? Like, the user has to first call miri_get_backtrace_size, then create an allocation of that size, and then hand that to miri_get_backtrace?

Yeah, that's a much better idea actually. Since this would involve completely rewriting the PR, should I open a new PR or just force push over this one?

@oli-obk
Copy link
Contributor

oli-obk commented Feb 24, 2022

Just force pushing is fine

@RalfJung
Copy link
Member

RalfJung commented Feb 24, 2022

In terms of migrating to the new API, we do have a flags argument -- so we could define our first flag, which indicates a pre-allocated buffer, and then only expect a 2nd argument if the flag is set appropriately. That way Miri can support both old and new clients of this API.

(open does something similar in POSIX, so such "optional arguments" actually seem possible in practice. And anyway we fully control the 'calling convention' here.)

@beepster4096
Copy link
Contributor Author

When updating the readme, should I keep documentation for the old api, or replace it completely with the new api?

@RalfJung
Copy link
Member

RalfJung commented Mar 3, 2022

When updating the readme, should I keep documentation for the old api, or replace it completely with the new api?

We want people to use the new one so probably it makes sense to only document that.

@RalfJung RalfJung added the S-waiting-on-author Status: Waiting for the PR author to address review comments label Mar 7, 2022
@beepster4096
Copy link
Contributor Author

Sorry that I took so long. The changes are done.

@RalfJung
Copy link
Member

Sorry that I took so long. The changes are done.

No worries. :)
I left a few more comments, just some nits. This is almost ready to go!

@RalfJung
Copy link
Member

Awesome, looks good. :) Can you squash the commits together?

@beepster4096
Copy link
Contributor Author

Whoops, forgot about the CI failure

@RalfJung
Copy link
Member

Thanks a lot for this PR. :-)
@bors r+

@bors
Copy link
Contributor

bors commented Mar 20, 2022

📌 Commit 2c670b1 has been approved by RalfJung

@bors
Copy link
Contributor

bors commented Mar 20, 2022

⌛ Testing commit 2c670b1 with merge 5778667...

@bors
Copy link
Contributor

bors commented Mar 20, 2022

☀️ Test successful - checks-actions
Approved by: RalfJung
Pushing 5778667 to master...

@bors bors merged commit 5778667 into rust-lang:master Mar 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: Waiting for the PR author to address review comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allocator confusion when a crate replaces the global allocator and also captures a backtrace
4 participants