Align command size to fix undefined behavior#67
Open
gaultier wants to merge 1 commit intorxi:masterfrom
Open
Conversation
namandixit
pushed a commit
to namandixit/microui
that referenced
this pull request
Oct 29, 2024
Merged rxi#67 Merge branch 'master' of github.com:gaultier/microui
|
I'm using microui as part of a project written in Zig and as Zig defaults to using -fsanitize by default, I hit this too. In my zig project I turned of the UBSan with the below: Zig doesn't even give an error message, just hits an invalid instruction when the unaligned case is detected. |
|
I also ran into the exact same issue while using zig |
|
there has been a new community fork of this repository. the community is looking to add some new simple features. please feel free to try your PR over there. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Compiling with
-fsanitize=address,memory(e.g.:clang -fsanitize=address,undefined src/microui.c demo/main.c demo/renderer.c -I src/ -lSDL2 -lGL) shows many instances of the same issue (excerpt):That's on x86_64 but other architectures will very likely show something similar.
The issue is that some commands such as
TextCommanduses a variable size which is not in general a multiple of the pointer size (8 on 64 bits, or 4 on 32 bits platforms).However this is required and not doing so leads to undefined behavior when dereferencing pointers to commands coming from the command list.
The easiest fix is to align the size to the next multiple of 8. If 32 bits platforms matter it could be tweaked with a ifdef to align to the next multiple of 4 for these.
There is open PR for 3 years that's similar, but I'd like to revive the topic once more since it matters to applications using sanitizers (which should be the majority or totality of modern applications), and it's a small non-intrusive fix.
See https://nullprogram.com/blog/2023/09/27/ and https://en.cppreference.com/w/c/language/object , 'Alignment' section, for a lengthier explanation.