diff --git a/compiler-rt/lib/hwasan/hwasan_interceptors.cpp b/compiler-rt/lib/hwasan/hwasan_interceptors.cpp --- a/compiler-rt/lib/hwasan/hwasan_interceptors.cpp +++ b/compiler-rt/lib/hwasan/hwasan_interceptors.cpp @@ -211,6 +211,12 @@ return res; } +template +static int munmap_interceptor(Munmap real_munmap, void *addr, SIZE_T length) { + __hwasan::TagMemoryAligned(reinterpret_cast(addr), length, 0); + return real_munmap(addr, length); +} + # define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, length, prot, flags, \ fd, offset) \ do { \ @@ -218,6 +224,12 @@ return mmap_interceptor(REAL(mmap), addr, sz, prot, flags, fd, off); \ } while (false) +# define COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, mmap, addr, length) \ + do { \ + (void)(ctx); \ + return munmap_interceptor(REAL(mmap), addr, sz); \ + } while (false) + # include "sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc" # include "sanitizer_common/sanitizer_common_interceptors.inc" diff --git a/compiler-rt/test/hwasan/TestCases/munmap.c b/compiler-rt/test/hwasan/TestCases/munmap.c new file mode 100644 --- /dev/null +++ b/compiler-rt/test/hwasan/TestCases/munmap.c @@ -0,0 +1,30 @@ +// RUN: %clang_hwasan %s -o %t +// RUN: %run %t 2>&1 + +// REQUIRES: aarch64-target-arch + +#include +#include +#include +#include + +int main(int argc, char **argv) { + int size = 4096; + int tag = 74; + int val = 42; + void *r = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, + -1, 0); + int *p1 = __hwasan_tag_pointer(r, tag); + if (r == MAP_FAILED) { + fprintf(stderr, "Failed to mmap\n"); + abort(); + } + __hwasan_tag_memory(r, tag, size); + *p1 = 42; + munmap(r, size); + // Remap the same address and make sure we can access it with a tagged pointer. + int *p2 = (int*) mmap(r, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, + -1, 0); + *p2 = 42; + return 0; +} \ No newline at end of file