Skip to content

Commit 8784dce

Browse files
authored
[msan] Detect dereferencing zero-alloc as use-of-uninitialized-value (#155944)
When a zero-byte allocation is requested, MSan actually allocates 1-byte for compatibility. This change poisons that byte, to detect dereferences. Also updates the test from #155934
1 parent b824f7c commit 8784dce

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

compiler-rt/lib/msan/msan_allocator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ static void *MsanAllocate(BufferedStackTrace *stack, uptr size, uptr alignment,
230230
__msan_set_origin(allocated, size, o.raw_id());
231231
}
232232
}
233+
234+
uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(allocated);
235+
// For compatibility, the allocator converted 0-sized allocations into 1 byte
236+
if (size == 0 && actually_allocated_size > 0 && flags()->poison_in_malloc)
237+
__msan_poison(allocated, 1);
238+
233239
UnpoisonParam(2);
234240
RunMallocHooks(allocated, size);
235241
return allocated;

compiler-rt/test/msan/zero_alloc.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// RUN: %clang_msan -Wno-alloc-size -fsanitize-recover=memory %s -o %t && not %run %t 2>&1 | FileCheck %s
22

3-
// MSan doesn't catch this because internally it translates 0-byte allocations
4-
// into 1-byte
5-
// XFAIL: *
6-
73
#include <malloc.h>
84
#include <stdio.h>
95

0 commit comments

Comments
 (0)