diff --git a/compiler-rt/lib/msan/msan_allocator.cpp b/compiler-rt/lib/msan/msan_allocator.cpp --- a/compiler-rt/lib/msan/msan_allocator.cpp +++ b/compiler-rt/lib/msan/msan_allocator.cpp @@ -192,7 +192,10 @@ reinterpret_cast(allocator.GetMetaData(allocated)); meta->requested_size = size; if (zeroise) { - __msan_clear_and_unpoison(allocated, size); + if (allocator.FromPrimary(allocated)) + __msan_clear_and_unpoison(allocated, size); + else + __msan_unpoison(allocated, size); // Mem is already zeroed. } else if (flags()->poison_in_malloc) { __msan_poison(allocated, size); if (__msan_get_track_origins()) { @@ -215,8 +218,9 @@ uptr size = meta->requested_size; meta->requested_size = 0; // This memory will not be reused by anyone else, so we are free to keep it - // poisoned. - if (flags()->poison_in_free) { + // poisoned. The secondary allocator will unmap and unpoison by + // MsanMapUnmapCallback, no need to poison it here. + if (flags()->poison_in_free && allocator.FromPrimary(p)) { __msan_poison(p, size); if (__msan_get_track_origins()) { stack->tag = StackTrace::TAG_DEALLOC; diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/huge_malloc.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/huge_malloc.c --- a/compiler-rt/test/sanitizer_common/TestCases/Posix/huge_malloc.c +++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/huge_malloc.c @@ -9,9 +9,6 @@ // FIXME: Hangs. // UNSUPPORTED: tsan -// FIXME: Make it work. Don't xfail to avoid excessive memory usage. -// UNSUPPORTED: msan - // Hwasan requires tagging of new allocations, so needs RSS for shadow. // UNSUPPORTED: hwasan @@ -19,8 +16,9 @@ int main(int argc, char **argv) { for (int i = 0; i < sizeof(void *) * 8; ++i) { - p = malloc(1ull << i); - fprintf(stderr, "%llu: %p\n", (1ull << i), p); + // Calloc avoids MSAN shadow poisoning. + p = calloc(1ull << i, 1); + fprintf(stderr, "%d %llu: %p\n", i, (1ull << i), p); free(p); } return 0;