-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[ASan] Honor allocator_may_return_null
when set through user-function and fix large alloc edge case
#117929
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
zmodem
merged 9 commits into
llvm:main
from
davidmrdavid:dev/dajusto/honor-allocator-may-return-null-under-large-mallocs
Dec 11, 2024
Merged
[ASan] Honor allocator_may_return_null
when set through user-function and fix large alloc edge case
#117929
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
873cc21
honor allocator_may_return_null when set through user-function in win…
davidmrdavid 7e7dbfb
simplify comment in weak function callback
davidmrdavid 71918b1
incorporate clang-format feedback
davidmrdavid 9de9922
add cstdio for printf
davidmrdavid ebc3d0d
incorporate PR feedback: merge tests
davidmrdavid 7f0e5c0
revert changes to sanitizer_allocator
davidmrdavid caac8b5
add missing cstdio
davidmrdavid 4f86077
move allocator_may_return_null_limits to windows test folder
davidmrdavid 1f66929
incorporate PR feedback
davidmrdavid 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,7 +164,24 @@ void UnmapOrDie(void *addr, uptr size, bool raw_report) { | |
static void *ReturnNullptrOnOOMOrDie(uptr size, const char *mem_type, | ||
const char *mmap_type) { | ||
error_t last_error = GetLastError(); | ||
if (last_error == ERROR_NOT_ENOUGH_MEMORY) | ||
|
||
// Assumption: VirtualAlloc is the last system call that was invoked before | ||
// this method. | ||
// VirtualAlloc emits one of 2 error codes when running out of memory | ||
// 1. ERROR_NOT_ENOUGH_MEMORY: | ||
// There's not enough memory to execute the command | ||
// 2. ERROR_INVALID_PARAMETER: | ||
// VirtualAlloc will return this if the request would allocate memory at an | ||
// address exceeding or being very close to the maximum application address | ||
// (the `lpMaximumApplicationAddress` field within the `SystemInfo` struct). | ||
// This does not seem to be officially documented, but is corroborated here: | ||
// https://stackoverflow.com/questions/45833674/why-does-virtualalloc-fail-for-lpaddress-greater-than-0x6ffffffffff | ||
|
||
// Note - It's possible that 'ERROR_COMMITMENT_LIMIT' needs to be handled here | ||
// as well. It is currently not handled due to the lack of a reproducer that | ||
// induces the error code. | ||
if (last_error == ERROR_NOT_ENOUGH_MEMORY || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems fine, CC @barcharcraz @zmodem There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems fine to me to. |
||
last_error == ERROR_INVALID_PARAMETER) | ||
return nullptr; | ||
ReportMmapFailureAndDie(size, mem_type, mmap_type, last_error); | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
compiler-rt/test/asan/TestCases/Windows/allocator_may_return_null_limits.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,34 @@ | ||
// RUN: %clangxx_asan -O0 %s -o %t | ||
// RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-ABORT | ||
// RUN: %env_asan_opts=allocator_may_return_null=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-RETURN-NULL | ||
|
||
// RUN: %clangxx_asan -O0 %s -o %t -DUSER_FUNCTION | ||
// RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-RETURN-NULL | ||
|
||
#if USER_FUNCTION | ||
// On Windows, flags configured through the user-defined function `__asan_default_options` | ||
// are suspected to not always be honored according to GitHub bug: | ||
// https://github.com/llvm/llvm-project/issues/117925 | ||
// This test ensures we do not regress on `allocator_may_return_null` specifically. | ||
extern "C" __declspec(dllexport) extern const char *__asan_default_options() { | ||
return "allocator_may_return_null=1"; | ||
} | ||
#endif | ||
|
||
#include <cstdint> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <limits> | ||
|
||
int main() { | ||
// Attempt to allocate an excessive amount of memory, which should | ||
// terminate the program unless `allocator_may_return_null` is set. | ||
size_t max = std::numeric_limits<size_t>::max(); | ||
|
||
// CHECK-ABORT: exceeds maximum supported size | ||
// CHECK-ABORT: ABORT | ||
free(malloc(max)); | ||
|
||
printf("Success"); // CHECK-RETURN-NULL: Success | ||
return 0; | ||
} |
Oops, something went wrong.
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.
Funnily enough, we're applying a patch to add ERROR_COMMITMENT_LIMIT downstream. It was submitted as https://reviews.llvm.org/D130781 but never ended up being merged.
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.
That looks reasonable to me. Want to send it again as a PR? I think we could merge that without a test case.
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.
nice. Please tag me on that PR if you end up opening it, @glandium. Can also assist in opening it if you don't find the cycles, I think the phabricator bug report suffices to me as well to just add it the check without a repro.