Skip to content

Commit 6dfd8d0

Browse files
authored
[asan] Rewrite Windows/heaprealloc_alloc_zero check to avoid dereference (#156211)
The test currently checks that 1-byte is allocated when malloc(0) is called, by dereferencing the pointer. #155943 changed ASan to consider the dereference to be a heap buffer overflow. This patch changes the test to check the allocated size is still 1-byte, but not dereference the pointer. This aims to fix the breakage reported in #155943 (comment) It also enables the test for 64-bit Windows.
1 parent 2824b3c commit 6dfd8d0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

compiler-rt/test/asan/TestCases/Windows/heaprealloc_alloc_zero.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
// RUN: %clang_cl_asan %Od %MT -o %t %s
22
// RUN: %env_asan_opts=windows_hook_rtl_allocators=true %run %t 2>&1 | FileCheck %s
3-
// UNSUPPORTED: asan-64-bits
43
#include <cassert>
54
#include <iostream>
5+
#include <sanitizer/allocator_interface.h>
66
#include <windows.h>
77

88
int main() {
99
void *ptr = malloc(0);
1010
if (ptr)
1111
std::cerr << "allocated!\n";
12-
((char *)ptr)[0] = '\xff'; //check this 'allocate 1 instead of 0' hack hasn't changed
12+
13+
// Check the 'allocate 1 instead of 0' hack hasn't changed
14+
// Note that as of b3452d90b043a398639e62b0ab01aa339cc649de, dereferencing
15+
// the pointer will be detected as a heap-buffer-overflow.
16+
if (__sanitizer_get_allocated_size(ptr) != 1)
17+
return 1;
1318

1419
free(ptr);
1520

0 commit comments

Comments
 (0)