#include "msan_allocator.h"
#include "msan_origin.h"
#include "msan_thread.h"
+#include "msan_poisoning.h"
namespace __msan {
if (new_size <= actually_allocated_size) {
// We are not reallocating here.
meta->requested_size = new_size;
- if (new_size > old_size)
- __msan_poison((char*)old_p + old_size, new_size - old_size);
+ if (new_size > old_size) {
+ if (zeroise) {
+ __msan_clear_and_unpoison((char *)old_p + old_size,
+ new_size - old_size);
+ } else if (flags()->poison_in_malloc) {
+ PoisonMemory((char *)old_p + old_size, new_size - old_size, stack);
+ }
+ }
return old_p;
}
uptr memcpy_size = Min(new_size, old_size);
void *new_p = MsanAllocate(stack, new_size, alignment, zeroise);
// Printf("realloc: old_size %zd new_size %zd\n", old_size, new_size);
if (new_p) {
- __msan_memcpy(new_p, old_p, memcpy_size);
+ CopyMemory(new_p, old_p, memcpy_size, stack);
MsanDeallocate(stack, old_p);
}
return new_p;
--- /dev/null
+// RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -m64 -O0 %s -o %t && not %run %t >%t.out 2>&1
+// RUN: FileCheck %s < %t.out
+// RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -m64 -O2 %s -o %t && not %run %t >%t.out 2>&1
+// RUN: FileCheck %s < %t.out
+
+// This is a regression test: there used to be broken "stored to memory at"
+// stacks with
+// in __msan_memcpy
+// in __msan::MsanReallocate
+// and nothing below that.
+
+#include <stdlib.h>
+int main(int argc, char **argv) {
+ char *p = (char *)malloc(100);
+ p = (char *)realloc(p, 10000);
+ char x = p[50];
+ free(p);
+ return x;
+
+// CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
+// CHECK: {{#0 0x.* in main .*realloc-large-origin.cc:}}[[@LINE-3]]
+
+// CHECK: Uninitialized value was stored to memory at
+// CHECK: {{#0 0x.* in realloc}}
+// CHECK: {{#1 0x.* in main .*realloc-large-origin.cc:}}[[@LINE-10]]
+
+// CHECK: Uninitialized value was created by a heap allocation
+// CHECK: {{#0 0x.* in malloc}}
+// CHECK: {{#1 0x.* in main .*realloc-large-origin.cc:}}[[@LINE-15]]
+}
--- /dev/null
+// RUN: %clangxx_msan -fsanitize-memory-track-origins -m64 -O0 %s -o %t && not %run %t >%t.out 2>&1
+// RUN: FileCheck %s < %t.out
+// RUN: %clangxx_msan -fsanitize-memory-track-origins -m64 -O2 %s -o %t && not %run %t >%t.out 2>&1
+// RUN: FileCheck %s < %t.out
+
+// This test relies on realloc from 100 to 101 being done in-place.
+
+#include <stdlib.h>
+int main(int argc, char **argv) {
+ char *p = (char *)malloc(100);
+ p = (char *)realloc(p, 101);
+ char x = p[100];
+ free(p);
+ return x;
+ // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
+ // CHECK: {{#0 0x.* in main .*realloc-origin.cc:}}[[@LINE-2]]
+
+ // CHECK: Uninitialized value was created by a heap allocation
+ // CHECK: {{#0 0x.* in realloc}}
+ // CHECK: {{#1 0x.* in main .*realloc-origin.cc:}}[[@LINE-9]]
+}