From 85f0a7e64426b0a97e0111c8b23a3143215401a2 Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Mon, 25 Feb 2013 08:43:10 +0000 Subject: [PATCH] [TSan] Add interceptor for malloc_usable_size() llvm-svn: 176013 --- compiler-rt/lib/tsan/rtl/tsan_interceptors.cc | 5 +++++ compiler-rt/lib/tsan/rtl/tsan_mman.cc | 8 ++++++++ compiler-rt/lib/tsan/rtl/tsan_mman.h | 1 + compiler-rt/lib/tsan/rtl/tsan_stat.h | 1 + compiler-rt/lib/tsan/tests/unit/tsan_mman_test.cc | 13 +++++++++++++ 5 files changed, 28 insertions(+) diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc index 81b859510fc1..25dfe9c01e1e 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc @@ -403,6 +403,11 @@ TSAN_INTERCEPTOR(void, cfree, void *p) { user_free(thr, pc, p); } +TSAN_INTERCEPTOR(uptr, malloc_usable_size, void *p) { + SCOPED_INTERCEPTOR_RAW(malloc_usable_size, p); + return user_alloc_usable_size(thr, pc, p); +} + #define OPERATOR_NEW_BODY(mangled_name) \ if (cur_thread()->in_symbolizer) \ return __libc_malloc(size); \ diff --git a/compiler-rt/lib/tsan/rtl/tsan_mman.cc b/compiler-rt/lib/tsan/rtl/tsan_mman.cc index 7ddcee2a9bf0..fb324834e72e 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_mman.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_mman.cc @@ -127,6 +127,14 @@ void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) { return p2; } +uptr user_alloc_usable_size(ThreadState *thr, uptr pc, void *p) { + CHECK_GT(thr->in_rtl, 0); + if (p == 0) + return 0; + MBlock *b = (MBlock*)allocator()->GetMetaData(p); + return (b) ? b->size : 0; +} + MBlock *user_mblock(ThreadState *thr, void *p) { CHECK_NE(p, (void*)0); Allocator *a = allocator(); diff --git a/compiler-rt/lib/tsan/rtl/tsan_mman.h b/compiler-rt/lib/tsan/rtl/tsan_mman.h index dc31acd75194..4a9240f660f1 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_mman.h +++ b/compiler-rt/lib/tsan/rtl/tsan_mman.h @@ -31,6 +31,7 @@ void *user_alloc(ThreadState *thr, uptr pc, uptr sz, void user_free(ThreadState *thr, uptr pc, void *p); void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz); void *user_alloc_aligned(ThreadState *thr, uptr pc, uptr sz, uptr align); +uptr user_alloc_usable_size(ThreadState *thr, uptr pc, void *p); // Given the pointer p into a valid allocated block, // returns the descriptor of the block. MBlock *user_mblock(ThreadState *thr, void *p); diff --git a/compiler-rt/lib/tsan/rtl/tsan_stat.h b/compiler-rt/lib/tsan/rtl/tsan_stat.h index 0b4ef832df56..e4362b034b10 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_stat.h +++ b/compiler-rt/lib/tsan/rtl/tsan_stat.h @@ -102,6 +102,7 @@ enum StatType { StatInt_realloc, StatInt_free, StatInt_cfree, + StatInt_malloc_usable_size, StatInt_mmap, StatInt_mmap64, StatInt_munmap, diff --git a/compiler-rt/lib/tsan/tests/unit/tsan_mman_test.cc b/compiler-rt/lib/tsan/tests/unit/tsan_mman_test.cc index af813a2ca5fc..ecbe874a0e76 100644 --- a/compiler-rt/lib/tsan/tests/unit/tsan_mman_test.cc +++ b/compiler-rt/lib/tsan/tests/unit/tsan_mman_test.cc @@ -117,6 +117,19 @@ TEST(Mman, UserRealloc) { } } +TEST(Mman, UsableSize) { + ScopedInRtl in_rtl; + ThreadState *thr = cur_thread(); + uptr pc = 0; + char *p = (char*)user_alloc(thr, pc, 10); + char *p2 = (char*)user_alloc(thr, pc, 20); + EXPECT_EQ(0U, user_alloc_usable_size(thr, pc, NULL)); + EXPECT_EQ(10U, user_alloc_usable_size(thr, pc, p)); + EXPECT_EQ(20U, user_alloc_usable_size(thr, pc, p2)); + user_free(thr, pc, p); + user_free(thr, pc, p2); +} + TEST(Mman, Stats) { ScopedInRtl in_rtl; ThreadState *thr = cur_thread();