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

Write stacks correctly during stack overflows #78

Merged
merged 1 commit into from
May 26, 2023

Conversation

gabrielesvelto
Copy link
Contributor

When encountering a stack overflow we often crash accessing the guard page. The logic assumed that wherever the stack pointer was so was the stack, but this lead the writer to dump the guard page in these cases. This patch changes the logic to inspect the properties of the mapping that appears to correspond to the stack and - if it looks like a guard page - look for the actual stack instead.

This fixes #24

@gabrielesvelto gabrielesvelto marked this pull request as draft April 28, 2023 20:20
@gabrielesvelto
Copy link
Contributor Author

This is a WIP, I need to test it in real world scenarios and add tests too.

@gabrielesvelto gabrielesvelto force-pushed the stack-overflow branch 3 times, most recently from a604403 to d8179ce Compare May 2, 2023 08:36
@gabrielesvelto
Copy link
Contributor Author

gabrielesvelto commented May 2, 2023

This is better, it works in Firefox so I will be able to add tests soon. There's still a couple of things that need to be hashed out:

  • Breakpad's original approach was to grab a page-aligned chunk of the stack, this often included a bit more data below the stack pointer. We probably want that too, but this patch only grabs data up to the stack pointer and no more.
  • Breakpad had two different limitations on how much stack data was grabbed: one within the code that found the stack and one in the outer layer that wrote it to the minidump. I've removed the inner limit which aligns us to what windbg.dll does on Windows when grabbing a minidump, but maybe that's not the right decision given a stack overflow can produce a lot of content (e.g. 8 MiB of stack data in some of Firefox' threads)

@gabrielesvelto gabrielesvelto force-pushed the stack-overflow branch 5 times, most recently from f92b998 to cc90c1e Compare May 2, 2023 09:07
@gabrielesvelto gabrielesvelto force-pushed the stack-overflow branch 2 times, most recently from a2c68a5 to d1c35f4 Compare May 19, 2023 10:30
When encountering a stack overflow we often crash accessing the guard
page. The logic assumed that wherever the stack pointer was so was the
stack, but this lead the writer to dump the guard page in these cases.
This patch changes the logic to inspect the properties of the mapping
that appears to correspond to the stack and - if it looks like a guard
page - look for the actual stack instead.

This change also removes the double limitation we had when retrieving
stacks on Linux: previously the logic would only grab the first 32 KiB
of each stack before checking for user-specified limits. Now only the
user-specified limits are enforced and - if not present - the full
stack is stored in the minidump. This brings the behavior in line with
minidumps generated on Windows by windbg.dll.

This fixes rust-minidump#24
@gabrielesvelto gabrielesvelto force-pushed the stack-overflow branch 2 times, most recently from d14dfe9 to 04bfdb7 Compare May 19, 2023 12:57
@gabrielesvelto gabrielesvelto marked this pull request as ready for review May 19, 2023 13:18
@gabrielesvelto
Copy link
Contributor Author

This is as good as it gets, I tried to match the original intent, removing the redundant limit on the amount of stack we captured while ensuring we always grab a stack, provided the overflow is within 1MiB of where it should be as that's the size of guard pages in Linux ATM.

@gabrielesvelto
Copy link
Contributor Author

@Jake-Shadle would you have time to take a look?

@Jake-Shadle
Copy link
Collaborator

Oh sorry, this completely fell off my radar, will review tomorrow!

@gabrielesvelto
Copy link
Contributor Author

Thanks!

Copy link
Collaborator

@Jake-Shadle Jake-Shadle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, can cut a release on Monday unless there is a rush.

Comment on lines +340 to 352

pub fn is_executable(&self) -> bool {
self.permissions.contains(MMPermissions::EXECUTE)
}

pub fn is_readable(&self) -> bool {
self.permissions.contains(MMPermissions::READ)
}

pub fn is_writable(&self) -> bool {
self.permissions.contains(MMPermissions::WRITE)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could be inline, but that's just a nit.

@Jake-Shadle Jake-Shadle merged commit e706902 into rust-minidump:main May 26, 2023
@gabrielesvelto
Copy link
Contributor Author

LGTM, can cut a release on Monday unless there is a rush.

No rush at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect guard pages and dump the real stack instead when encountering stack overflows on Linux
2 participants