diff --git a/compiler-rt/lib/msan/CMakeLists.txt b/compiler-rt/lib/msan/CMakeLists.txt --- a/compiler-rt/lib/msan/CMakeLists.txt +++ b/compiler-rt/lib/msan/CMakeLists.txt @@ -4,7 +4,6 @@ set(MSAN_RTL_SOURCES msan.cpp msan_allocator.cpp - msan_chained_origin_depot.cpp msan_interceptors.cpp msan_linux.cpp msan_report.cpp @@ -19,7 +18,6 @@ set(MSAN_RTL_HEADERS msan.h msan_allocator.h - msan_chained_origin_depot.h msan_flags.h msan_flags.inc msan_interface_internal.h diff --git a/compiler-rt/lib/msan/msan.cpp b/compiler-rt/lib/msan/msan.cpp --- a/compiler-rt/lib/msan/msan.cpp +++ b/compiler-rt/lib/msan/msan.cpp @@ -12,20 +12,21 @@ //===----------------------------------------------------------------------===// #include "msan.h" -#include "msan_chained_origin_depot.h" + #include "msan_origin.h" +#include "msan_poisoning.h" #include "msan_report.h" #include "msan_thread.h" -#include "msan_poisoning.h" #include "sanitizer_common/sanitizer_atomic.h" +#include "sanitizer_common/sanitizer_chained_origin_depot.h" #include "sanitizer_common/sanitizer_common.h" -#include "sanitizer_common/sanitizer_flags.h" #include "sanitizer_common/sanitizer_flag_parser.h" +#include "sanitizer_common/sanitizer_flags.h" #include "sanitizer_common/sanitizer_libc.h" #include "sanitizer_common/sanitizer_procmaps.h" +#include "sanitizer_common/sanitizer_stackdepot.h" #include "sanitizer_common/sanitizer_stacktrace.h" #include "sanitizer_common/sanitizer_symbolizer.h" -#include "sanitizer_common/sanitizer_stackdepot.h" #include "ubsan/ubsan_flags.h" #include "ubsan/ubsan_init.h" diff --git a/compiler-rt/lib/msan/msan_interceptors.cpp b/compiler-rt/lib/msan/msan_interceptors.cpp --- a/compiler-rt/lib/msan/msan_interceptors.cpp +++ b/compiler-rt/lib/msan/msan_interceptors.cpp @@ -16,22 +16,22 @@ #include "interception/interception.h" #include "msan.h" -#include "msan_chained_origin_depot.h" #include "msan_origin.h" +#include "msan_poisoning.h" #include "msan_report.h" #include "msan_thread.h" -#include "msan_poisoning.h" -#include "sanitizer_common/sanitizer_platform_limits_posix.h" -#include "sanitizer_common/sanitizer_platform_limits_netbsd.h" #include "sanitizer_common/sanitizer_allocator.h" #include "sanitizer_common/sanitizer_allocator_interface.h" #include "sanitizer_common/sanitizer_allocator_internal.h" #include "sanitizer_common/sanitizer_atomic.h" +#include "sanitizer_common/sanitizer_chained_origin_depot.h" #include "sanitizer_common/sanitizer_common.h" #include "sanitizer_common/sanitizer_errno.h" -#include "sanitizer_common/sanitizer_stackdepot.h" #include "sanitizer_common/sanitizer_libc.h" #include "sanitizer_common/sanitizer_linux.h" +#include "sanitizer_common/sanitizer_platform_limits_netbsd.h" +#include "sanitizer_common/sanitizer_platform_limits_posix.h" +#include "sanitizer_common/sanitizer_stackdepot.h" #include "sanitizer_common/sanitizer_tls_get_addr.h" #include "sanitizer_common/sanitizer_vector.h" diff --git a/compiler-rt/lib/msan/msan_origin.h b/compiler-rt/lib/msan/msan_origin.h --- a/compiler-rt/lib/msan/msan_origin.h +++ b/compiler-rt/lib/msan/msan_origin.h @@ -11,8 +11,8 @@ #ifndef MSAN_ORIGIN_H #define MSAN_ORIGIN_H +#include "sanitizer_common/sanitizer_chained_origin_depot.h" #include "sanitizer_common/sanitizer_stackdepot.h" -#include "msan_chained_origin_depot.h" namespace __msan { diff --git a/compiler-rt/lib/msan/msan_report.cpp b/compiler-rt/lib/msan/msan_report.cpp --- a/compiler-rt/lib/msan/msan_report.cpp +++ b/compiler-rt/lib/msan/msan_report.cpp @@ -11,11 +11,12 @@ // Error reporting. //===----------------------------------------------------------------------===// +#include "msan_report.h" + #include "msan.h" -#include "msan_chained_origin_depot.h" #include "msan_origin.h" -#include "msan_report.h" #include "sanitizer_common/sanitizer_allocator_internal.h" +#include "sanitizer_common/sanitizer_chained_origin_depot.h" #include "sanitizer_common/sanitizer_common.h" #include "sanitizer_common/sanitizer_flags.h" #include "sanitizer_common/sanitizer_mutex.h" diff --git a/compiler-rt/lib/sanitizer_common/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/CMakeLists.txt --- a/compiler-rt/lib/sanitizer_common/CMakeLists.txt +++ b/compiler-rt/lib/sanitizer_common/CMakeLists.txt @@ -73,6 +73,7 @@ set(SANITIZER_SYMBOLIZER_SOURCES sanitizer_allocator_report.cpp + sanitizer_chained_origin_depot.cpp sanitizer_stackdepot.cpp sanitizer_stacktrace.cpp sanitizer_stacktrace_libcdep.cpp @@ -119,6 +120,7 @@ sanitizer_atomic_msvc.h sanitizer_bitvector.h sanitizer_bvgraph.h + sanitizer_chained_origin_depot.h sanitizer_common.h sanitizer_common_interceptors.inc sanitizer_common_interceptors_format.inc diff --git a/compiler-rt/lib/msan/msan_chained_origin_depot.h b/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.h rename from compiler-rt/lib/msan/msan_chained_origin_depot.h rename to compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.h --- a/compiler-rt/lib/msan/msan_chained_origin_depot.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.h @@ -1,4 +1,4 @@ -//===-- msan_chained_origin_depot.h --------------------------*- C++ -*-===// +//===-- sanitizer_chained_origin_depot.h ------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -8,21 +8,27 @@ // // A storage for chained origins. //===----------------------------------------------------------------------===// -#ifndef MSAN_CHAINED_ORIGIN_DEPOT_H -#define MSAN_CHAINED_ORIGIN_DEPOT_H -#include "sanitizer_common/sanitizer_common.h" +#ifndef SANITIZER_CHAINED_ORIGIN_DEPOT_H +#define SANITIZER_CHAINED_ORIGIN_DEPOT_H -namespace __msan { +#include "sanitizer_common.h" +namespace __sanitizer { + +// Gets the statistic of the origin chain storage. StackDepotStats *ChainedOriginDepotGetStats(); + +// Stores a chain with StackDepot ID here_id and previous chain ID prev_id. +// If successful, returns true and the new chain id new_id. bool ChainedOriginDepotPut(u32 here_id, u32 prev_id, u32 *new_id); -// Retrieves a stored stack trace by the id. + +// Retrieves the stored StackDepot ID for the given origin id. u32 ChainedOriginDepotGet(u32 id, u32 *other); void ChainedOriginDepotLockAll(); void ChainedOriginDepotUnlockAll(); -} // namespace __msan +} // namespace __sanitizer -#endif // MSAN_CHAINED_ORIGIN_DEPOT_H +#endif // SANITIZER_CHAINED_ORIGIN_DEPOT_H diff --git a/compiler-rt/lib/msan/msan_chained_origin_depot.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.cpp rename from compiler-rt/lib/msan/msan_chained_origin_depot.cpp rename to compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.cpp --- a/compiler-rt/lib/msan/msan_chained_origin_depot.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_chained_origin_depot.cpp @@ -1,4 +1,5 @@ -//===-- msan_chained_origin_depot.cpp ----------------------------------===// +//===-- sanitizer_chained_origin_depot.cpp +//----------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -9,11 +10,11 @@ // A storage for chained origins. //===----------------------------------------------------------------------===// -#include "msan_chained_origin_depot.h" +#include "sanitizer_chained_origin_depot.h" -#include "sanitizer_common/sanitizer_stackdepotbase.h" +#include "sanitizer_stackdepotbase.h" -namespace __msan { +namespace __sanitizer { struct ChainedOriginDepotDesc { u32 here_id; @@ -120,12 +121,8 @@ return desc.here_id; } -void ChainedOriginDepotLockAll() { - chainedOriginDepot.LockAll(); -} +void ChainedOriginDepotLockAll() { chainedOriginDepot.LockAll(); } -void ChainedOriginDepotUnlockAll() { - chainedOriginDepot.UnlockAll(); -} +void ChainedOriginDepotUnlockAll() { chainedOriginDepot.UnlockAll(); } -} // namespace __msan +} // namespace __sanitizer diff --git a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt --- a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt +++ b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt @@ -13,6 +13,7 @@ sanitizer_atomic_test.cpp sanitizer_bitvector_test.cpp sanitizer_bvgraph_test.cpp + sanitizer_chained_origin_depot_test.cpp sanitizer_common_test.cpp sanitizer_deadlock_detector_test.cpp sanitizer_flags_test.cpp diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_chained_origin_depot_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_chained_origin_depot_test.cpp new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_chained_origin_depot_test.cpp @@ -0,0 +1,82 @@ +//===-- sanitizer_chained_origin_depot_test.cpp ---------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file is a part of Sanitizer runtime. +// Tests for sanitizer_chained_origin_depot.h. +// +//===----------------------------------------------------------------------===// + +#include "sanitizer_common/sanitizer_chained_origin_depot.h" + +#include "gtest/gtest.h" +#include "sanitizer_common/sanitizer_internal_defs.h" +#include "sanitizer_common/sanitizer_libc.h" + +namespace __sanitizer { + +TEST(SanitizerCommon, ChainedOriginDepotBasic) { + u32 new_id; + EXPECT_TRUE(ChainedOriginDepotPut(1, 2, &new_id)); + u32 prev_id; + EXPECT_EQ(ChainedOriginDepotGet(new_id, &prev_id), 1U); + EXPECT_EQ(prev_id, 2U); +} + +TEST(SanitizerCommon, ChainedOriginDepotAbsent) { + u32 prev_id; + EXPECT_EQ(0U, ChainedOriginDepotGet(99, &prev_id)); + EXPECT_EQ(0U, prev_id); +} + +TEST(SanitizerCommon, ChainedOriginDepotZeroId) { + u32 prev_id; + EXPECT_EQ(0U, ChainedOriginDepotGet(0, &prev_id)); + EXPECT_EQ(0U, prev_id); +} + +TEST(SanitizerCommon, ChainedOriginDepotSame) { + u32 new_id1; + EXPECT_TRUE(ChainedOriginDepotPut(1, 2, &new_id1)); + u32 new_id2; + EXPECT_FALSE(ChainedOriginDepotPut(1, 2, &new_id2)); + EXPECT_EQ(new_id1, new_id2); + + u32 prev_id; + EXPECT_EQ(ChainedOriginDepotGet(new_id1, &prev_id), 1U); + EXPECT_EQ(prev_id, 2U); +} + +TEST(SanitizerCommon, ChainedOriginDepotDifferent) { + u32 new_id1; + EXPECT_TRUE(ChainedOriginDepotPut(1, 2, &new_id1)); + u32 new_id2; + EXPECT_TRUE(ChainedOriginDepotPut(1, 3, &new_id2)); + EXPECT_NE(new_id1, new_id2); +} + +TEST(SanitizerCommon, ChainedOriginDepotStats) { + StackDepotStats stats0 = *ChainedOriginDepotGetStats(); + + u32 new_id; + EXPECT_TRUE(ChainedOriginDepotPut(3, 4, &new_id)); + StackDepotStats stats1 = *ChainedOriginDepotGetStats(); + EXPECT_EQ(stats1.n_uniq_ids, stats0.n_uniq_ids + 1); + EXPECT_GT(stats1.allocated, stats0.allocated); + + EXPECT_FALSE(ChainedOriginDepotPut(3, 4, &new_id)); + StackDepotStats stats2 = *ChainedOriginDepotGetStats(); + EXPECT_EQ(stats2.n_uniq_ids, stats1.n_uniq_ids); + EXPECT_EQ(stats2.allocated, stats1.allocated); + + EXPECT_TRUE(ChainedOriginDepotPut(5, 6, &new_id)); + StackDepotStats stats3 = *ChainedOriginDepotGetStats(); + EXPECT_EQ(stats3.n_uniq_ids, stats2.n_uniq_ids + 1); + EXPECT_GT(stats3.allocated, stats2.allocated); +} + +} // namespace __sanitizer