diff --git a/compiler-rt/lib/dfsan/dfsan_interceptors.cpp b/compiler-rt/lib/dfsan/dfsan_interceptors.cpp --- a/compiler-rt/lib/dfsan/dfsan_interceptors.cpp +++ b/compiler-rt/lib/dfsan/dfsan_interceptors.cpp @@ -22,6 +22,18 @@ static bool interceptors_initialized; +namespace { + +void ReleaseShadowMemoryPagesToOS(void *addr, SIZE_T length) { + uptr beg_shadow_addr = (uptr)__dfsan::shadow_for(addr); + void *end_addr = + (void *)((uptr)addr + RoundUpTo(length, GetPageSizeCached())); + uptr end_shadow_addr = (uptr)__dfsan::shadow_for(end_addr); + ReleaseMemoryPagesToOS(beg_shadow_addr, end_shadow_addr); +} + +} // namespace + INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags, int fd, OFF_T offset) { void *res; @@ -34,7 +46,7 @@ res = REAL(mmap)(addr, length, prot, flags, fd, offset); if (res != (void *)-1) - dfsan_set_label(0, res, RoundUpTo(length, GetPageSize())); + ReleaseShadowMemoryPagesToOS(res, length); return res; } @@ -42,18 +54,14 @@ int fd, OFF64_T offset) { void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset); if (res != (void *)-1) - dfsan_set_label(0, res, RoundUpTo(length, GetPageSize())); + ReleaseShadowMemoryPagesToOS(res, length); return res; } INTERCEPTOR(int, munmap, void *addr, SIZE_T length) { int res = REAL(munmap)(addr, length); if (res != -1) { - uptr beg_shadow_addr = (uptr)__dfsan::shadow_for(addr); - void *end_addr = - (void *)((uptr)addr + RoundUpTo(length, GetPageSizeCached())); - uptr end_shadow_addr = (uptr)__dfsan::shadow_for(end_addr); - ReleaseMemoryPagesToOS(beg_shadow_addr, end_shadow_addr); + ReleaseShadowMemoryPagesToOS(addr, length); } return res; } diff --git a/compiler-rt/test/dfsan/munmap_release_shadow.c b/compiler-rt/test/dfsan/release_shadow_space.c rename from compiler-rt/test/dfsan/munmap_release_shadow.c rename to compiler-rt/test/dfsan/release_shadow_space.c --- a/compiler-rt/test/dfsan/munmap_release_shadow.c +++ b/compiler-rt/test/dfsan/release_shadow_space.c @@ -23,32 +23,41 @@ char *p = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + memset(p, 0xff, map_size); + size_t after_mmap = get_rss_kb(); + const dfsan_label label = dfsan_create_label("l", 0); char val = 0xff; dfsan_set_label(label, &val, sizeof(val)); memset(p, val, map_size); - size_t after_mmap = get_rss_kb(); + size_t after_mmap_and_set_label = get_rss_kb(); munmap(p, map_size); size_t after_munmap = get_rss_kb(); p = mmap(NULL, map_size, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); dfsan_set_label(label, &val, sizeof(val)); memset(p, val, map_size); - size_t after_mmap2 = get_rss_kb(); + size_t after_mmap_and_set_label2 = get_rss_kb(); - fprintf(stderr, "RSS at start: %td, after mmap: %td, after mumap: %td, after mmap2: %td\n", - before, after_mmap, after_munmap, after_mmap2); + fprintf( + stderr, + "RSS at start: %td, after mmap: %td, after mmap+set label: %td, after " + "mumap: %td, after another mmap+set label: %td\n", + before, after_mmap, after_mmap_and_set_label, after_munmap, + after_mmap_and_set_label2); // The memory after mmap increases 3 times of map_size because the overhead of // shadow memory is 2x. - const size_t mmap_cost_kb = 3 * (map_size >> 10); + const size_t mmap_cost_kb = map_size >> 10; + const size_t mmap_shadow_cost_kb = 2 * mmap_cost_kb; assert(after_mmap >= before + mmap_cost_kb); + assert(after_mmap_and_set_label >= after_mmap + mmap_shadow_cost_kb); // OS does not release memory to the same level as the start of the program. // The assert checks the memory after munmap up to a delta. const size_t delta = 50000; - assert(after_mmap2 <= after_mmap + delta); + assert(after_mmap_and_set_label2 <= after_mmap_and_set_label + delta); return 0; }