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

Change: [Win32] don't allocate 192KiB of memory on the stack on crash #11240

Merged
merged 1 commit into from
Aug 28, 2023

Conversation

TrueBrain
Copy link
Member

Motivation / Problem

While looking at the Windows crashlog dialog, I noticed it was allocating a lot of memory on the stack. So I looked at JGRPP, and got some inspiration how to solve it properly.

Description

Heap is out of the question, as it might be corrupted. Allocating this much on stack is silly.

So instead, allocate virtual pages to write the information in.

While at it, also added the savegame filename to the dialog, and as they are (now) always set, remove any conditional around them. This simplifies the code a lot, as now it can be printed by a single snprint.

Limitations

Where JGRPP took the approach of: better allocate too much memory than run out of a buffer, I went for: let's do the math correctly, and make sure all buffers fit, while making them nice and snugly. This means the buffers are on the byte of the correct size.
JGRPP's approach might be more robust; although I am pretty sure my math adds up, and should be safe. I hope.

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR touches english.txt or translations? Check the guidelines
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, game_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

@TrueBrain TrueBrain changed the title Change: [Win32] don't allocate 256KiB of memory on the stack on crash Change: [Win32] don't allocate 192KiB of memory on the stack on crash Aug 27, 2023
@TrueBrain TrueBrain enabled auto-merge (squash) August 27, 2023 22:10
char32_t c;
while ((c = Utf8Consume(&unix_nl)) && p < lastof(dos_nl) - 4) { // 4 is max number of bytes per character
while ((c = Utf8Consume(&crashlog_unix_nl))) {
Copy link
Member Author

@TrueBrain TrueBrain Aug 27, 2023

Choose a reason for hiding this comment

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

The removal of the p < check is safe, as crashlog_dos_nl is calculated to be at least as wide as crashlog_unix_nl. So it is not possible for the latter to return a character while the first can't fit it.

(please confirm my claim, ofc :D )

Copy link
Contributor

Choose a reason for hiding this comment

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

And \n are counted twice to make room for \r in crashlog_dos_nl. I think I can confirm your claim :).

Heap is out of the question, as it might be corrupted.
Allocating this much on stack is silly.

So instead, allocate virtual pages to write the information in.
@TrueBrain TrueBrain merged commit b0d7cfa into OpenTTD:master Aug 28, 2023
20 checks passed
@TrueBrain TrueBrain deleted the crashlog-windows-stack branch January 18, 2024 18:46
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.

None yet

2 participants