From 6430a73c22fa5f8009b1b0ab22abcf9c4d0419cf Mon Sep 17 00:00:00 2001 From: yuesiyuan Date: Thu, 16 Jan 2025 21:07:19 +0800 Subject: [PATCH] [tsan] after ReportDestroyLocked, don't get slotlocker in some intercept or release function to avoid deadlock Signed-off-by: yuesiyuan Change-Id: If09604948bc1230af8121db3a45b4f3db1fb31e9 add comment OHOS_LOCAL Change-Id: I5cf93bd7b650b7c3df098d73487f05a6dff0868e add thr->slot_locked = true; in ReportDestroyLocked Change-Id: I6e631edd877838e00fe0d6aec8514175143fb3fe --- compiler-rt/lib/tsan/rtl/tsan_fd.cpp | 2 +- compiler-rt/lib/tsan/rtl/tsan_mman.cpp | 2 +- compiler-rt/lib/tsan/rtl/tsan_rtl.cpp | 3 ++- compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp | 8 +++++--- compiler-rt/lib/tsan/rtl/tsan_rtl_report.cpp | 2 ++ 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/compiler-rt/lib/tsan/rtl/tsan_fd.cpp b/compiler-rt/lib/tsan/rtl/tsan_fd.cpp index cf8f491fdbf0..f69314e03762 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_fd.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_fd.cpp @@ -212,7 +212,7 @@ void FdClose(ThreadState *thr, uptr pc, int fd, bool write) { { // Need to lock the slot to make MemoryAccess and MemoryResetRange atomic // with respect to global reset. See the comment in MemoryRangeFreed. - SlotLocker locker(thr); + SlotLocker locker(thr, thr->ignore_interceptors > 0); // OHOS_LOCAL if (!MustIgnoreInterceptor(thr)) { if (write) { // To catch races between fd usage and close. diff --git a/compiler-rt/lib/tsan/rtl/tsan_mman.cpp b/compiler-rt/lib/tsan/rtl/tsan_mman.cpp index 0937e521193f..ca3458c4555e 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_mman.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_mman.cpp @@ -269,7 +269,7 @@ void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write) { DPrintf("#%d: free(0x%zx, %zu) (no slot)\n", thr->tid, p, sz); return; } - SlotLocker locker(thr); + SlotLocker locker(thr, thr->ignore_interceptors > 0); // OHOS_LOCAL uptr sz = ctx->metamap.FreeBlock(thr->proc(), p, true); DPrintf("#%d: free(0x%zx, %zu)\n", thr->tid, p, sz); if (write && thr->ignore_reads_and_writes == 0) diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp b/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp index 646dec64864b..c21c5b6ab05a 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp @@ -801,6 +801,7 @@ int Finalize(ThreadState *thr) { ThreadFinalize(thr); + thr->ignore_interceptors++; // OHOS_LOCAL if (ctx->nreported) { failed = true; #if !SANITIZER_GO @@ -815,7 +816,7 @@ int Finalize(ThreadState *thr) { failed = OnFinalize(failed); Report("End Tsan report (Finalize)\n"); // OHOS_LOCAL - + thr->ignore_interceptors--; // OHOS_LOCAL return failed ? common_flags()->exitcode : 0; } diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp b/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp index 2e978852ea7d..1d02b81b886f 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp @@ -464,7 +464,7 @@ void Release(ThreadState *thr, uptr pc, uptr addr) { DPrintf("#%d: Release %zx\n", thr->tid, addr); if (thr->ignore_sync) return; - SlotLocker locker(thr); + SlotLocker locker(thr, thr->ignore_interceptors > 0); // OHOS_LOCAL { auto s = ctx->metamap.GetSyncOrCreate(thr, pc, addr, false); Lock lock(&s->mtx); @@ -477,7 +477,7 @@ void ReleaseStore(ThreadState *thr, uptr pc, uptr addr) { DPrintf("#%d: ReleaseStore %zx\n", thr->tid, addr); if (thr->ignore_sync) return; - SlotLocker locker(thr); + SlotLocker locker(thr, thr->ignore_interceptors > 0); // OHOS_LOCAL { auto s = ctx->metamap.GetSyncOrCreate(thr, pc, addr, false); Lock lock(&s->mtx); @@ -490,7 +490,7 @@ void ReleaseStoreAcquire(ThreadState *thr, uptr pc, uptr addr) { DPrintf("#%d: ReleaseStoreAcquire %zx\n", thr->tid, addr); if (thr->ignore_sync) return; - SlotLocker locker(thr); + SlotLocker locker(thr, thr->ignore_interceptors > 0); // OHOS_LOCAL { auto s = ctx->metamap.GetSyncOrCreate(thr, pc, addr, false); Lock lock(&s->mtx); @@ -558,6 +558,7 @@ void ReportDestroyLocked(ThreadState *thr, uptr pc, uptr addr, Lock slot_lock(&ctx->slots[static_cast(last_lock.sid())].mtx); ThreadRegistryLock l0(&ctx->thread_registry); Lock slots_lock(&ctx->slot_mtx); + thr->slot_locked = true; // OHOS_LOCAL ScopedReport rep(ReportTypeMutexDestroyLocked); rep.AddMutex(addr, creation_stack_id); VarSizeStackTrace trace; @@ -573,6 +574,7 @@ void ReportDestroyLocked(ThreadState *thr, uptr pc, uptr addr, rep.AddStack(trace, true); rep.AddLocation(addr, 1); OutputReport(thr, rep); + thr->slot_locked = false; // OHOS_LOCAL } } // namespace __tsan diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cpp b/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cpp index 706b12a841e1..4973b3b08059 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cpp @@ -656,6 +656,7 @@ bool OutputReport(ThreadState *thr, const ScopedReport &srep) { VPrintf(2, "[Suppression] Suppression hit type:%s file:%s pc:0x%zx.\n", supp->type, supp->templ, pc_or_addr); } + thr->ignore_interceptors++; // OHOS_LOCAL { bool suppressed = OnReport(rep, pc_or_addr != 0); if (suppressed) { @@ -664,6 +665,7 @@ bool OutputReport(ThreadState *thr, const ScopedReport &srep) { } } PrintReport(rep); + thr->ignore_interceptors--; // OHOS_LOCAL __tsan_on_report(rep); ctx->nreported++; if (flags()->halt_on_error) -- Gitee