From af77e5e4c08eac78d9395568167fdca2fa1bf914 Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Wed, 10 Aug 2022 20:53:02 -0700 Subject: [PATCH] [msan] Extract SetAllocaOrigin --- compiler-rt/lib/msan/msan.cpp | 56 ++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/compiler-rt/lib/msan/msan.cpp b/compiler-rt/lib/msan/msan.cpp index c1c6805..380753b 100644 --- a/compiler-rt/lib/msan/msan.cpp +++ b/compiler-rt/lib/msan/msan.cpp @@ -303,6 +303,31 @@ u32 ChainOrigin(u32 id, StackTrace *stack) { return chained.raw_id(); } +// 'descr' is created at compile time and contains '----' in the beginning. +// When we see descr for the first time we replace '----' with a uniq id +// and set the origin to (id | (31-th bit)). +static inline void SetAllocaOrigin(void *a, uptr size, char *descr, uptr pc) { + static const u32 dash = '-'; + static const u32 first_timer = + dash + (dash << 8) + (dash << 16) + (dash << 24); + u32 *id_ptr = (u32 *)descr; + bool print = false; // internal_strstr(descr + 4, "AllocaTOTest") != 0; + u32 id = *id_ptr; + if (id == first_timer) { + u32 idx = atomic_fetch_add(&NumStackOriginDescrs, 1, memory_order_relaxed); + CHECK_LT(idx, kNumStackOriginDescrs); + StackOriginDescr[idx] = descr + 4; + StackOriginPC[idx] = pc; + id = Origin::CreateStackOrigin(idx).raw_id(); + *id_ptr = id; + if (print) + Printf("First time: idx=%d id=%d %s 0x%zx \n", idx, id, descr + 4, pc); + } + if (print) + Printf("__msan_set_alloca_origin: descr=%s id=%x\n", descr + 4, id); + __msan_set_origin(a, size, id); +} + } // namespace __msan void __sanitizer::BufferedStackTrace::UnwindImpl( @@ -581,34 +606,17 @@ void __msan_set_origin(const void *a, uptr size, u32 origin) { if (__msan_get_track_origins()) SetOrigin(a, size, origin); } -// 'descr' is created at compile time and contains '----' in the beginning. -// When we see descr for the first time we replace '----' with a uniq id -// and set the origin to (id | (31-th bit)). void __msan_set_alloca_origin(void *a, uptr size, char *descr) { - __msan_set_alloca_origin4( - a, size, descr, StackTrace::GetPreviousInstructionPc(GET_CALLER_PC())); + SetAllocaOrigin(a, size, descr, + StackTrace::GetPreviousInstructionPc(GET_CALLER_PC())); } void __msan_set_alloca_origin4(void *a, uptr size, char *descr, uptr pc) { - static const u32 dash = '-'; - static const u32 first_timer = - dash + (dash << 8) + (dash << 16) + (dash << 24); - u32 *id_ptr = (u32*)descr; - bool print = false; // internal_strstr(descr + 4, "AllocaTOTest") != 0; - u32 id = *id_ptr; - if (id == first_timer) { - u32 idx = atomic_fetch_add(&NumStackOriginDescrs, 1, memory_order_relaxed); - CHECK_LT(idx, kNumStackOriginDescrs); - StackOriginDescr[idx] = descr + 4; - StackOriginPC[idx] = pc; - id = Origin::CreateStackOrigin(idx).raw_id(); - *id_ptr = id; - if (print) - Printf("First time: idx=%d id=%d %s 0x%zx \n", idx, id, descr + 4, pc); - } - if (print) - Printf("__msan_set_alloca_origin: descr=%s id=%x\n", descr + 4, id); - __msan_set_origin(a, size, id); + // Intentionally ignore pc and use return address. This function is here for + // compatibility, in case program is linked with library instrumented by + // older clang. + SetAllocaOrigin(a, size, descr, + StackTrace::GetPreviousInstructionPc(GET_CALLER_PC())); } u32 __msan_chain_origin(u32 id) { -- 2.7.4