-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[asan] Add test for deferencing zero-sized malloc/calloc #155933
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
Conversation
ASan fails to catch this, because 0-byte allocations are converted into 1-byte allocations. Bug originally reported by dvyukov
|
@llvm/pr-subscribers-compiler-rt-sanitizer Author: Thurston Dang (thurstond) ChangesASan fails to catch this, because 0-byte allocations are converted into 1-byte allocations. Bug originally reported by dvyukov Full diff: https://github.com/llvm/llvm-project/pull/155933.diff 1 Files Affected:
diff --git a/compiler-rt/test/asan/TestCases/zero_alloc.cpp b/compiler-rt/test/asan/TestCases/zero_alloc.cpp
new file mode 100644
index 0000000000000..3decd5acb7bae
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/zero_alloc.cpp
@@ -0,0 +1,37 @@
+// RUN: %clang_asan -Wno-alloc-size -fsanitize-recover=address %s -o %t && %env_asan_opts=halt_on_error=0 %run %t 2>&1 | FileCheck %s
+
+// XFAIL: *
+
+#include <malloc.h>
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+ {
+ char* p1 = (char*)calloc(1, 0);
+ printf ("p1 is %p\n", p1);
+ printf ("Content of p1 is: %d\n", *p1);
+ // CHECK: ERROR: AddressSanitizer: heap-buffer-overflow
+ // CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]
+ free(p1);
+ }
+
+ {
+ char* p2 = (char*)calloc(0, 1);
+ printf ("p2 is %p\n", p2);
+ printf ("Content of p2 is: %d\n", *p2);
+ // CHECK: ERROR: AddressSanitizer: heap-buffer-overflow
+ // CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]
+ free(p2);
+ }
+
+ {
+ char* p3 = (char*)malloc(0);
+ printf ("p3 is %p\n", p3);
+ printf ("Content of p2 is: %d\n", *p3);
+ // CHECK: ERROR: AddressSanitizer: heap-buffer-overflow
+ // CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]
+ free(p3);
+ }
+
+ return 0;
+}
|
| @@ -0,0 +1,37 @@ | |||
| // RUN: %clang_asan -Wno-alloc-size -fsanitize-recover=address %s -o %t && %env_asan_opts=halt_on_error=0 %run %t 2>&1 | FileCheck %s | |||
|
|
|||
| // XFAIL: * | |||
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.
say why this fails
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.
Added note on bad compiler is bad
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
When a zero-byte allocation is requested, ASan actually allocates 1-byte for compatibility. This change poisons that byte, to detect dereferences. Also updates the test from llvm#155933
When a zero-byte allocation is requested, ASan actually allocates 1-byte for compatibility. This change poisons that byte, to detect dereferences. Also updates the test from #155933
ASan fails to catch this, because 0-byte allocations are converted into 1-byte allocations.
Bug originally reported by dvyukov