-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[UBSan] Fix incorrect alignment reported when global new returns an o… #152532
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
gbMattN
merged 5 commits into
llvm:main
from
gbMattN:users/nagym/ubsan-incorrect-alignment
Nov 3, 2025
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4714264
[UBSan] Report more detailed alignment report when overloaded global …
gbMattN eb9ea2c
Tidy
gbMattN 4cac6e2
Remove strict check and generalise error message
gbMattN 2b103a8
Finished code removal
gbMattN 32c44c0
Rename error type and change comment to reflect changes in checks
gbMattN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
36 changes: 36 additions & 0 deletions
36
compiler-rt/test/ubsan/TestCases/TypeCheck/minimum-alignment.cpp
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // RUN: %clangxx %gmlt -fsanitize=alignment %s -o %t | ||
| // RUN: %run %t 2>&1 | FileCheck %s | ||
|
|
||
| // UNSUPPORTED: i386 | ||
| // UNSUPPORTED: armv7l | ||
|
|
||
| // These sanitizers already overload the new operator so won't compile this test | ||
| // UNSUPPORTED: ubsan-msan | ||
| // UNSUPPORTED: ubsan-tsan | ||
|
|
||
| #include <cassert> | ||
| #include <cstdlib> | ||
|
|
||
| void *operator new(std::size_t count) { | ||
| constexpr const size_t offset = 8; | ||
|
|
||
| // allocate a bit more so we can safely offset it | ||
| void *ptr = std::malloc(count + offset); | ||
|
|
||
| // verify malloc returned 16 bytes aligned mem | ||
| static_assert(__STDCPP_DEFAULT_NEW_ALIGNMENT__ == 16); | ||
| assert(((std::ptrdiff_t)ptr & (__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1)) == 0); | ||
|
|
||
| return (char *)ptr + offset; | ||
| } | ||
|
|
||
| struct Foo { | ||
| void *_cookie1, *_cookie2; | ||
| }; | ||
|
|
||
| static_assert(alignof(Foo) == 8); | ||
| int main() { | ||
| // CHECK: runtime error: constructor call with pointer from overloaded operator new on misaligned address 0x{{.*}} for type 'Foo', which requires target minimum assumed alignment of 16 | ||
| Foo *f = new Foo; | ||
| return 0; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need to check whether the new operator was defined by the user? If new returns bad alignment, that's bad no matter who wrote the implementation.
Also, you can override the global new operator while using the declaration from
<new>.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If new somehow returns bad alignment without it being a user overload, it will still be caught with the regular constructor-on error. The new error adds information specific to cases when a user has erroneously returned a smaller than allowed alignment in their overload, so I thought it best to only emit the new error in those cases. I can certainly remove some of these checks if its too overcomplicated though.
And do you mind explaining your second point some more? I don't understand it yet!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, "override" isn't the formally correct term. Certain overloads of operator new/delete are replaceable; see https://en.cppreference.com/w/cpp/language/replacement_function.html.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, thats handled by the !SM.isInSystemHeader check, seeing if this is a user replaced call. Thats tested in the new minimum-alignment test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the declaration of the operator is still in a system header; just the definition is in user code.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, I think I understand now. If you'd prefer then, I can remove this check, and use the new check whenever the alignment of the type is less than the system default alignment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@efriedma-quic Made the changes we talked about at the conference