-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[compiler-rt][asan] _aligned_malloc/_aligned_free interception. #82049
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
base: main
Are you sure you want to change the base?
Conversation
|
@llvm/pr-subscribers-compiler-rt-sanitizer Author: David CARLIER (devnexen) ChangesFull diff: https://github.com/llvm/llvm-project/pull/82049.diff 2 Files Affected:
diff --git a/compiler-rt/lib/asan/asan_malloc_win.cpp b/compiler-rt/lib/asan/asan_malloc_win.cpp
index 7e1d04c36dd580..01428a7c582842 100644
--- a/compiler-rt/lib/asan/asan_malloc_win.cpp
+++ b/compiler-rt/lib/asan/asan_malloc_win.cpp
@@ -171,6 +171,17 @@ void *_recalloc_base(void *p, size_t n, size_t elem_size) {
return _recalloc(p, n, elem_size);
}
+ALLOCATION_FUNCTION_ATTRIBUTE
+void *_aligned_malloc(size_t alignment, size_t size) {
+ GET_STACK_TRACE_MALLOC;
+ return asan_aligned_alloc(alignment, size, &stack);
+}
+
+ALLOCATION_FUNCTION_ATTRIBUTE
+void *_aligned_free(void *p) {
+ free(p);
+}
+
ALLOCATION_FUNCTION_ATTRIBUTE
void *_expand(void *memblock, size_t size) {
// _expand is used in realloc-like functions to resize the buffer if possible.
diff --git a/compiler-rt/lib/asan/asan_win_dll_thunk.cpp b/compiler-rt/lib/asan/asan_win_dll_thunk.cpp
index 0fa636bec0d001..fecf63e53d66e9 100644
--- a/compiler-rt/lib/asan/asan_win_dll_thunk.cpp
+++ b/compiler-rt/lib/asan/asan_win_dll_thunk.cpp
@@ -30,10 +30,12 @@
INTERCEPT_WRAP_V_W(free)
INTERCEPT_WRAP_V_W(_free_base)
INTERCEPT_WRAP_V_WW(_free_dbg)
+INTERCEPT_WRAP_V_W(_aligned_free)
INTERCEPT_WRAP_W_W(malloc)
INTERCEPT_WRAP_W_W(_malloc_base)
INTERCEPT_WRAP_W_WWWW(_malloc_dbg)
+INTERCEPT_WRAP_W_WW(_aligned_malloc)
INTERCEPT_WRAP_W_WW(calloc)
INTERCEPT_WRAP_W_WW(_calloc_base)
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
|
This needs some tests |
99fb735 to
b44305b
Compare
zmodem
left a comment
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.
Thanks for working on this!
| return _recalloc(p, n, elem_size); | ||
| } | ||
|
|
||
| __declspec(noinline) void *_aligned_malloc(size_t alignment, size_t size) { |
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.
Shouldn't the first parameter be size and the second 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.
Also, we need to actually intercept the system functions by adding these to ReplaceSystemMalloc().
We should also remove the
// TODO(timurrrr): Might want to add support for _aligned_* allocation
// functions to detect a bit more bugs. Those functions seem to wrap malloc().
comment now :-)
| } | ||
|
|
||
| __declspec(noinline) void *_aligned_realloc(void *p, size_t alignment, | ||
| size_t size) { |
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.
Here also, I think the parameters should be p, size, alignment.
| } | ||
|
|
||
| __declspec(noinline) void *_aligned_free(void *p) { free(p); } | ||
|
|
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.
Might as well do _aligned_msize while we're here?
| @@ -0,0 +1,20 @@ | |||
| // RUN: %clang_cl_asan %Od %s %Fe%t | |||
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.
This basically does the same as compiler-rt/test/asan/TestCases/Windows/aligned_mallocs.cpp
But like the test you're adding, that only checks that we can call the functions and get aligned memory back. It doesn't check that we're intercepting the system's implementations.
We want a test that fails if we fail to intercept the functions, and passes otherwise. How about something like using _aligned_malloc for a 128-byte chunk of memory, trying to write to one byte before the address and checking that we get a proper asan error?
91e27d7 to
a735dda
Compare
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.
There needs to be a FileCheck invocation on the RUN line for the CHECKs to get checked.
Also, can we update compiler-rt/test/asan/TestCases/Windows/aligned_mallocs.cpp instead of adding a new one?
When using ASAN, PartitionAlloc-Everywhere is disabled because ASAN hooks the allocation functions instead, and needs to hear about the allocations in order to protect them. But ASAN does not hook the HeapAlloc() function used by the Rust stdlib so it does not hear about heap allocations made by Rust. When ASAN is enabled on Windows, we can redirect allocations to _aligned_malloc and friends. ASAN is being taught to hook those functions unconditionally and will do so in a clang roll in the near future: llvm/llvm-project#82049 Note that there is a runtime option to make ASAN hook HeapAlloc() but enabling it breaks Win32 APIs like CreateProcess: https://crbug.com/368070343#comment29 [email protected] Bug: 368070343 Change-Id: I4155d4b063980d7849a836dd88f74396a28adef1 Cq-Include-Trybots: luci.chromium.try:linux-rust-x64-dbg,android-rust-arm64-dbg,android-rust-arm32-rel,android-rust-arm64-rel,mac-rust-x64-dbg,linux-rust-x64-rel,win-rust-x64-rel,win-rust-x64-dbg Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5998676 Reviewed-by: Hans Wennborg <[email protected]> Commit-Queue: danakj <[email protected]> Cr-Commit-Position: refs/heads/main@{#1379904}
169f58e to
cf92c39
Compare
zmodem
left a comment
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.
Thanks! I think we're almost there.
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.
Since we expect the program execution to fail with an asan error, we need the "not" prefix. (Also since the asan diagnostic will print to stderr, we should probably redirect it):
// RUN: not %run %t 2>&1 | FileCheck %s
Additionally, the test does not currently work when statically linking against the crt. We need to update asan_malloc_windows_thunk.cpp with the new functions as well.
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.
This code doesn't actually guarantee the returned pointer will be aligned though.
I think the way to do this is to dllexport an _asan_aligned{malloc,realloc} in asan_malloc_win.cpp, and dllimport and call them here -- look at __asan_malloc for an example.
2241d52 to
4bff3cc
Compare
zmodem
left a comment
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.
Thanks! The test just needs a tweak, then this seems all good to me.
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.
The ASan errors get printed to stderr. We need to redirect the output so FileCheck sees it. In other words, this line should be
// RUN: not %run %t 2>&1 | FileCheck %s
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.
nit: Indentation looks off.
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.
purposely waited your ok before formatting :)
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.
nit: Formatting looks off.
4bff3cc to
3c0f86a
Compare
|
gonna wait @barcharcraz review for good measure. |
3c0f86a to
00aaeee
Compare
zmodem
left a comment
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.
lgtm
|
Note that I can no longer speak for the asan team at Microsoft, I've moved to a different team. Still, I'm across the hall from them and I'll review things if I have time. However, I think things will probably fall apart in programs that use the In hindsight it might have been a bad idea on our end to intercept these functions at all without handling all of them. We ended up breaking basically all applications that used This looks pretty good. We have an implementation of this internally. There should probably be tests of the following: _aligned_free. Verify behavior is similar between real aligned_free and intercepted w.r.t. stuff allocated with normal malloc |
Not sure about this ; |
|
Even if there isn't a problem (I think the _aligned_offset_malloc / _aligned_free combo should work), it would be good to expand the test to verify that mixing the intercepted/non-intercepted allocation/freeing functions works. |
a9e3833 to
a8591e2
Compare
|
The latest version of the test fails for me: |
|
Ah you re right building this target I can reproduce, I ll have a look later.. |
|
quick question how do you build LLVM ? might be best to align ... I m more used to unixes :) thx |
I build with cmake and ninja in a Visual Studio (2019) command prompt. The CMake invocation was probably something like: (You can probably add And then I run the compiler-rt tests with |
2503312 to
712ae4c
Compare
Locally now I have this output (did check-compiler-rt but check-asan is faster) |
|
ping :) |
|
The test fails for me today as well, see below.
First, are you sure that check-asan also runs TestCases/Windows/aligned_mallocs.cpp? If not, that could explain why you're not seeing the test failure. Second, you seem to be hitting a bunch of other test failures, that I'm not seeing. What are the error messages for those? It doesn't seem like a great use of time to review the patch if it's not even passing its own test. |
Sorry for this. I ll have a look soon-ish. Cheers. |
712ae4c to
9f4f319
Compare
Are there any hints in the test output as to why these are failing? Are all the failures for i386, or are there x86_64 failures too? |
|
Can you paste the output as text in a pastebin somewhere? It's hard to see what's going on from the screenshots.
It's supposed to test both automatically. From the screenshots it's not clear if they all passed on x86_64 or didn't run at all. |
|
Thanks! It looks like clang-cl is configured to target i386 by default. Maybe that's why it's having problems.
In what kind of environment did you run cmake? A Visual Studio Command Prompt window? After running VsDevCmd.bat or similar? Can you try configuring the build in an x86_64 environment, either in an "x64 Native Tools Command Prompt", or after running |
|
Well now 64 bits tests are launched I hit the same issues as you. |
117f2f1 to
65846ad
Compare
|
Here my tests result on this branch and main |
65846ad to
198a56f
Compare







No description provided.