[compiler-rt] [CopyProf] Implement minimal CopyProf runtime. (PR #223458)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 14 09:57:15 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-compiler-rt-sanitizer
Author: newgre
<details>
<summary>Changes</summary>
The runtime tracks object copies using shadow memory (with a 1:8 compression scale).
Each thread uses TLS to keep track of whether execution is currently inside a special member function. Copy function mark corresponding shadow memory as a copy. Upon destruction, if an object was marked as a copy and never modified, it is classified as an unnecessary copy, and a report with a stack trace is generated.
---
Patch is 50.42 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/223458.diff
22 Files Affected:
- (modified) compiler-rt/CMakeLists.txt (+2)
- (modified) compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake (+1)
- (modified) compiler-rt/cmake/config-ix.cmake (+9)
- (modified) compiler-rt/lib/CMakeLists.txt (+6-2)
- (added) compiler-rt/lib/copyprof/.clang-format (+3)
- (added) compiler-rt/lib/copyprof/CMakeLists.txt (+64)
- (added) compiler-rt/lib/copyprof/copyprof.cpp (+156)
- (added) compiler-rt/lib/copyprof/copyprof.syms.extra (+1)
- (added) compiler-rt/lib/copyprof/copyprof_interface_internal.h (+50)
- (added) compiler-rt/lib/copyprof/copyprof_internal.h (+33)
- (added) compiler-rt/lib/copyprof/copyprof_reporting.cpp (+42)
- (added) compiler-rt/lib/copyprof/copyprof_reporting.h (+28)
- (added) compiler-rt/lib/copyprof/copyprof_shadow.cpp (+112)
- (added) compiler-rt/lib/copyprof/copyprof_shadow.h (+63)
- (added) compiler-rt/lib/copyprof/copyprof_stack.cpp (+24)
- (added) compiler-rt/lib/copyprof/copyprof_state.cpp (+20)
- (added) compiler-rt/lib/copyprof/copyprof_state.h (+70)
- (added) compiler-rt/lib/copyprof/tests/CMakeLists.txt (+73)
- (added) compiler-rt/lib/copyprof/tests/copyprof_shadow_test.cpp (+164)
- (added) compiler-rt/lib/copyprof/tests/copyprof_state_test.cpp (+178)
- (added) compiler-rt/lib/copyprof/tests/driver.cpp (+19)
- (modified) compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h (+3)
``````````diff
diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index 9e45a43b34287..0458b17ba38c2 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -92,6 +92,8 @@ option(COMPILER_RT_BUILD_CTX_PROFILE "Build ctx profile runtime" ON)
mark_as_advanced(COMPILER_RT_BUILD_CTX_PROFILE)
option(COMPILER_RT_BUILD_MEMPROF "Build memory profiling runtime" ON)
mark_as_advanced(COMPILER_RT_BUILD_MEMPROF)
+option(COMPILER_RT_BUILD_COPYPROF "Build copy profiling runtime" ON)
+mark_as_advanced(COMPILER_RT_BUILD_COPYPROF)
option(COMPILER_RT_BUILD_XRAY_NO_PREINIT "Build xray with no preinit patching" OFF)
mark_as_advanced(COMPILER_RT_BUILD_XRAY_NO_PREINIT)
option(COMPILER_RT_BUILD_ORC "Build ORC runtime" ON)
diff --git a/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake b/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake
index 9c9874d94a1f2..8956ed2d305de 100644
--- a/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake
+++ b/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake
@@ -118,6 +118,7 @@ set(ALL_CFI_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${MIPS64}
${HEXAGON} ${LOONGARCH64})
set(ALL_SCUDO_STANDALONE_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64}
${MIPS32} ${MIPS64} ${PPC64} ${HEXAGON} ${LOONGARCH64} ${RISCV64} ${S390X})
+set(ALL_COPYPROF_SUPPORTED_ARCH ${X86_64})
if(APPLE)
set(ALL_XRAY_SUPPORTED_ARCH ${X86_64} ${ARM64})
else()
diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake
index 34a00a13f5dc0..7773c3b6bbd71 100644
--- a/compiler-rt/cmake/config-ix.cmake
+++ b/compiler-rt/cmake/config-ix.cmake
@@ -740,6 +740,8 @@ else()
filter_available_targets(GWP_ASAN_SUPPORTED_ARCH ${ALL_GWP_ASAN_SUPPORTED_ARCH})
filter_available_targets(NSAN_SUPPORTED_ARCH ${ALL_NSAN_SUPPORTED_ARCH})
filter_available_targets(ORC_SUPPORTED_ARCH ${ALL_ORC_SUPPORTED_ARCH})
+ filter_available_targets(COPYPROF_SUPPORTED_ARCH ${ALL_COPYPROF_SUPPORTED_ARCH})
+
endif()
if (MSVC)
@@ -915,6 +917,13 @@ else()
set(COMPILER_RT_HAS_CFI FALSE)
endif()
+if (COMPILER_RT_HAS_SANITIZER_COMMON AND COPYPROF_SUPPORTED_ARCH AND
+ OS_NAME MATCHES "Linux")
+ set(COMPILER_RT_HAS_COPYPROF TRUE)
+else()
+ set(COMPILER_RT_HAS_COPYPROF FALSE)
+endif()
+
#TODO(kostyak): add back Android & Fuchsia when the code settles a bit.
if (SCUDO_STANDALONE_SUPPORTED_ARCH AND
COMPILER_RT_BUILD_SANITIZERS AND
diff --git a/compiler-rt/lib/CMakeLists.txt b/compiler-rt/lib/CMakeLists.txt
index a5b2fbb38762c..90efc541cf15c 100644
--- a/compiler-rt/lib/CMakeLists.txt
+++ b/compiler-rt/lib/CMakeLists.txt
@@ -9,7 +9,7 @@ include(SanitizerUtils)
#
#TODO: Refactor sanitizer_common into smaller pieces (e.g. flag parsing, utils).
if (COMPILER_RT_HAS_SANITIZER_COMMON AND
- (COMPILER_RT_BUILD_SANITIZERS OR COMPILER_RT_BUILD_XRAY OR COMPILER_RT_BUILD_MEMPROF OR COMPILER_RT_BUILD_CTX_PROFILE))
+ (COMPILER_RT_BUILD_SANITIZERS OR COMPILER_RT_BUILD_XRAY OR COMPILER_RT_BUILD_MEMPROF OR COMPILER_RT_BUILD_COPYPROF OR COMPILER_RT_BUILD_CTX_PROFILE))
add_subdirectory(sanitizer_common)
endif()
@@ -31,7 +31,7 @@ function(compiler_rt_build_runtime runtime)
endif()
endfunction()
-if(COMPILER_RT_BUILD_SANITIZERS OR COMPILER_RT_BUILD_MEMPROF)
+if(COMPILER_RT_BUILD_SANITIZERS OR COMPILER_RT_BUILD_MEMPROF OR COMPILER_RT_BUILD_COPYPROF)
compiler_rt_build_runtime(interception)
endif()
@@ -74,6 +74,10 @@ if(COMPILER_RT_BUILD_MEMPROF AND COMPILER_RT_HAS_SANITIZER_COMMON)
compiler_rt_build_runtime(memprof)
endif()
+if(COMPILER_RT_BUILD_COPYPROF AND COMPILER_RT_HAS_SANITIZER_COMMON)
+ compiler_rt_build_runtime(copyprof)
+endif()
+
if(COMPILER_RT_BUILD_ORC)
compiler_rt_build_runtime(orc)
endif()
diff --git a/compiler-rt/lib/copyprof/.clang-format b/compiler-rt/lib/copyprof/.clang-format
new file mode 100644
index 0000000000000..1f2a97030379d
--- /dev/null
+++ b/compiler-rt/lib/copyprof/.clang-format
@@ -0,0 +1,3 @@
+BasedOnStyle: Google
+AllowShortIfStatementsOnASingleLine: false
+IndentPPDirectives: AfterHash
diff --git a/compiler-rt/lib/copyprof/CMakeLists.txt b/compiler-rt/lib/copyprof/CMakeLists.txt
new file mode 100644
index 0000000000000..1de2d5f3682b2
--- /dev/null
+++ b/compiler-rt/lib/copyprof/CMakeLists.txt
@@ -0,0 +1,64 @@
+# Build for the CopyProf runtime support library.
+
+set(COPYPROF_SOURCES
+ copyprof.cpp
+ copyprof_reporting.cpp
+ copyprof_shadow.cpp
+ copyprof_stack.cpp
+ copyprof_state.cpp
+)
+
+set(COPYPROF_HEADERS
+ copyprof_interface_internal.h
+ copyprof_internal.h
+ copyprof_reporting.h
+ copyprof_shadow.h
+ copyprof_state.h
+)
+
+include_directories(..)
+include_directories(../../include)
+
+set(COPYPROF_CFLAGS ${SANITIZER_COMMON_CFLAGS})
+set(COPYPROF_COMMON_DEFINITIONS "")
+append_rtti_flag(OFF COPYPROF_CFLAGS)
+
+add_compiler_rt_object_libraries(RTCopyProf
+ ARCHS ${COPYPROF_SUPPORTED_ARCH}
+ SOURCES ${COPYPROF_SOURCES}
+ ADDITIONAL_HEADERS ${COPYPROF_HEADERS}
+ CFLAGS ${COPYPROF_CFLAGS}
+ DEFS ${COPYPROF_COMMON_DEFINITIONS})
+
+# Build CopyProf runtimes shipped with Clang.
+add_compiler_rt_component(copyprof)
+
+set(COPYPROF_COMMON_RUNTIME_OBJECT_LIBS
+ RTInterception
+ RTSanitizerCommon
+ RTSanitizerCommonLibc
+ RTSanitizerCommonSymbolizer
+)
+
+add_compiler_rt_runtime(clang_rt.copyprof
+ STATIC
+ ARCHS ${COPYPROF_SUPPORTED_ARCH}
+ OBJECT_LIBS
+ RTCopyProf
+ ${COPYPROF_COMMON_RUNTIME_OBJECT_LIBS}
+ CFLAGS ${COPYPROF_CFLAGS}
+ DEFS ${COPYPROF_COMMON_DEFINITIONS}
+ PARENT_TARGET copyprof)
+
+if(SANITIZER_USE_SYMBOLS)
+ foreach(arch ${COPYPROF_SUPPORTED_ARCH})
+ add_sanitizer_rt_symbols(clang_rt.copyprof
+ ARCHS ${arch}
+ EXTRA copyprof.syms.extra)
+ add_dependencies(copyprof clang_rt.copyprof-${arch}-symbols)
+ endforeach()
+endif()
+
+if(COMPILER_RT_INCLUDE_TESTS)
+ add_subdirectory(tests)
+endif()
diff --git a/compiler-rt/lib/copyprof/copyprof.cpp b/compiler-rt/lib/copyprof/copyprof.cpp
new file mode 100644
index 0000000000000..854f9936847ce
--- /dev/null
+++ b/compiler-rt/lib/copyprof/copyprof.cpp
@@ -0,0 +1,156 @@
+//===-- copyprof.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 implements the core CopyProf runtime initialization and callback
+/// functions inserted by the instrumentation passes.
+///
+//===----------------------------------------------------------------------===//
+
+#include "copyprof_interface_internal.h"
+#include "copyprof_reporting.h"
+#include "copyprof_shadow.h"
+#include "copyprof_state.h"
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_flags.h"
+#include "sanitizer_common/sanitizer_internal_defs.h"
+
+using namespace __copyprof;
+
+namespace __copyprof {
+
+// Whether CopyProf has been initialized.
+bool copyprof_is_initialized;
+// Whether CopyProf is currently initializing.
+bool copyprof_init_is_running;
+
+static void CheckUnwind() {
+ UNINITIALIZED BufferedStackTrace trace;
+ trace.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(),
+ /*context=*/nullptr, common_flags()->fast_unwind_on_check);
+ trace.Print();
+}
+
+static void Initialize() {
+ if (LIKELY(copyprof_is_initialized))
+ return;
+ CHECK(!copyprof_init_is_running &&
+ "BUG: CopyProf Initialize() must not call itself.");
+ copyprof_init_is_running = true;
+ CacheBinaryName();
+ SetCheckUnwindCallback(&CheckUnwind);
+ InitializePlatformEarly();
+ SetCommonFlagsDefaults();
+ InitializeCommonFlags();
+ InitializeShadowMemory();
+ copyprof_init_is_running = false;
+ copyprof_is_initialized = true;
+}
+
+static void MaybeUpdateSmfContext(SmfContext context) {
+ // Only entering a top level special member function changes the current
+ // context. The context logically remains the same until control flow leaves
+ // the top level function.
+ if (__copyprof_state.smf_context == SmfContext::NONE ||
+ (__copyprof_state.construct_nesting_level == 0 &&
+ __copyprof_state.copy_nesting_level == 0 &&
+ __copyprof_state.destruct_nesting_level == 0)) {
+ __copyprof_state.smf_context = context;
+ }
+}
+
+// Updates shadow memory for `[addr, addr + size)` according to the current SMF
+// context: no update in `DTOR` context (objects may mutate their memory during
+// destruction, but that must not invalidate its classification as an
+// unnecessary copy), marked as copy in `COPY` context, and marked as non-copy
+// otherwise.
+static void UpdateShadow(const void* addr, uptr size) {
+ if (UNLIKELY(__copyprof_state.smf_context == SmfContext::DTOR))
+ return;
+ MarkApplicationMemory(addr, size,
+ __copyprof_state.smf_context == SmfContext::COPY);
+}
+
+static void CopyMemberFunctionEnter(const void* this_ptr, uptr obj_size) {
+ MaybeUpdateSmfContext(SmfContext::COPY);
+ if (__copyprof_state.copy_nesting_level++ == 0) {
+ __copyprof_state.current_this_ptr = this_ptr;
+ }
+ UpdateShadow(this_ptr, obj_size);
+}
+
+static void CopyMemberFunctionExit(const void* this_ptr, uptr obj_size) {
+ --__copyprof_state.copy_nesting_level;
+ MaybeUpdateSmfContext(SmfContext::NONE);
+}
+
+} // namespace __copyprof
+
+void __copyprof_init() { Initialize(); }
+
+void __copyprof_ctor_enter_callback(const void* this_ptr, uptr obj_size) {
+ MaybeUpdateSmfContext(SmfContext::CTOR);
+ ++__copyprof_state.construct_nesting_level;
+ UpdateShadow(this_ptr, obj_size);
+}
+
+void __copyprof_ctor_exit_callback(const void* this_ptr, uptr obj_size) {
+ --__copyprof_state.construct_nesting_level;
+ MaybeUpdateSmfContext(SmfContext::NONE);
+}
+
+void __copyprof_copy_ctor_enter_callback(const void* this_ptr,
+ const void* other_ptr, uptr obj_size) {
+ CopyMemberFunctionEnter(this_ptr, obj_size);
+}
+
+void __copyprof_copy_ctor_exit_callback(const void* this_ptr,
+ const void* other_ptr, uptr obj_size) {
+ CopyMemberFunctionExit(this_ptr, obj_size);
+}
+
+void __copyprof_copy_assign_op_enter_callback(const void* this_ptr,
+ const void* other_ptr,
+ uptr obj_size) {
+ CopyMemberFunctionEnter(this_ptr, obj_size);
+}
+
+void __copyprof_copy_assign_op_exit_callback(const void* this_ptr,
+ const void* other_ptr,
+ uptr obj_size) {
+ CopyMemberFunctionExit(this_ptr, obj_size);
+}
+
+void __copyprof_dtor_enter_callback(const void* this_ptr, uptr obj_size) {
+ MaybeUpdateSmfContext(SmfContext::DTOR);
+ if (__copyprof_state.destruct_nesting_level++ == 0) {
+ // Control flow just entered the top-level d'tor. Optimistically mark
+ // `is_transitive_copy` as `true`. If any subsequent d'tor observes object
+ // memory marked as not copy, then the flag will be set to `false`.
+ __copyprof_state.is_transitive_copy = true;
+ }
+}
+
+void __copyprof_dtor_exit_callback(const void* this_ptr, uptr obj_size) {
+ --__copyprof_state.destruct_nesting_level;
+ MaybeUpdateSmfContext(SmfContext::NONE);
+ // If this is the top level-dtor and `this` is transitively marked as copy,
+ // then an object has been found whose transitively owned memory is marked as
+ // copy, so a report is logged.
+ __copyprof_state.is_transitive_copy &= IsMarkedAsCopy(this_ptr, obj_size);
+ if (__copyprof_state.destruct_nesting_level == 0 &&
+ __copyprof_state.is_transitive_copy) {
+ // TODO: Dynamic allocation tracking via malloc/new interception will be
+ // added in a subsequent patch. For now, did_allocate is set to true.
+ LogCopyProfReport(GET_CALLER_PC(), GET_CURRENT_FRAME(), obj_size,
+ /*did_allocate=*/true);
+ }
+}
+
+void __copyprof_store_callback(const void* addr, uptr size) {
+ UpdateShadow(addr, size);
+}
diff --git a/compiler-rt/lib/copyprof/copyprof.syms.extra b/compiler-rt/lib/copyprof/copyprof.syms.extra
new file mode 100644
index 0000000000000..6d640e1fa2384
--- /dev/null
+++ b/compiler-rt/lib/copyprof/copyprof.syms.extra
@@ -0,0 +1 @@
+__copyprof_*
diff --git a/compiler-rt/lib/copyprof/copyprof_interface_internal.h b/compiler-rt/lib/copyprof/copyprof_interface_internal.h
new file mode 100644
index 0000000000000..58e6f84e442bb
--- /dev/null
+++ b/compiler-rt/lib/copyprof/copyprof_interface_internal.h
@@ -0,0 +1,50 @@
+//===-- copyprof_interface_internal.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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// This file declares the internal runtime interface for CopyProf, including
+/// initialization and special member function callback declarations.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef COPYPROF_INTERFACE_INTERNAL_H
+#define COPYPROF_INTERFACE_INTERNAL_H
+
+#include "copyprof_internal.h"
+#include "sanitizer_common/sanitizer_internal_defs.h"
+
+extern "C" {
+
+// Should be called at the very beginning of the process before any instrumented
+// code executes.
+SANITIZER_INTERFACE_ATTRIBUTE void __copyprof_init();
+
+// Runtime callbacks that update the CopyProf state machine and shadow memory
+// when entering or leaving special member functions.
+SANITIZER_INTERFACE_ATTRIBUTE void __copyprof_ctor_enter_callback(
+ const void* this_ptr, uptr obj_size);
+SANITIZER_INTERFACE_ATTRIBUTE void __copyprof_ctor_exit_callback(
+ const void* this_ptr, uptr obj_size);
+SANITIZER_INTERFACE_ATTRIBUTE void __copyprof_copy_ctor_enter_callback(
+ const void* this_ptr, const void* other_ptr, uptr obj_size);
+SANITIZER_INTERFACE_ATTRIBUTE void __copyprof_copy_ctor_exit_callback(
+ const void* this_ptr, const void* other_ptr, uptr obj_size);
+SANITIZER_INTERFACE_ATTRIBUTE void __copyprof_copy_assign_op_enter_callback(
+ const void* this_ptr, const void* other_ptr, uptr obj_size);
+SANITIZER_INTERFACE_ATTRIBUTE void __copyprof_copy_assign_op_exit_callback(
+ const void* this_ptr, const void* other_ptr, uptr obj_size);
+SANITIZER_INTERFACE_ATTRIBUTE void __copyprof_dtor_enter_callback(
+ const void* this_ptr, uptr obj_size);
+SANITIZER_INTERFACE_ATTRIBUTE void __copyprof_dtor_exit_callback(
+ const void* this_ptr, uptr obj_size);
+// Store instructions callback that marks memory as not copy.
+SANITIZER_INTERFACE_ATTRIBUTE void __copyprof_store_callback(const void* addr,
+ uptr size);
+
+} // extern "C"
+
+#endif // COPYPROF_INTERFACE_INTERNAL_H
diff --git a/compiler-rt/lib/copyprof/copyprof_internal.h b/compiler-rt/lib/copyprof/copyprof_internal.h
new file mode 100644
index 0000000000000..ccd9c95fe4dd4
--- /dev/null
+++ b/compiler-rt/lib/copyprof/copyprof_internal.h
@@ -0,0 +1,33 @@
+//===-- copyprof_internal.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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// This file declares internal runtime data structures, flags, and helper
+/// functions shared within the CopyProf runtime library.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef COPYPROF_INTERNAL_H
+#define COPYPROF_INTERNAL_H
+
+#include "sanitizer_common/sanitizer_internal_defs.h"
+#include "sanitizer_common/sanitizer_stacktrace.h"
+
+using __sanitizer::u32;
+using __sanitizer::u64;
+using __sanitizer::uptr;
+using __sanitizer::usize;
+
+namespace __copyprof {
+
+using __sanitizer::BufferedStackTrace;
+extern bool copyprof_is_initialized;
+extern bool copyprof_init_is_running;
+
+} // namespace __copyprof
+
+#endif // COPYPROF_INTERNAL_H
diff --git a/compiler-rt/lib/copyprof/copyprof_reporting.cpp b/compiler-rt/lib/copyprof/copyprof_reporting.cpp
new file mode 100644
index 0000000000000..439438bc12c84
--- /dev/null
+++ b/compiler-rt/lib/copyprof/copyprof_reporting.cpp
@@ -0,0 +1,42 @@
+//===-- copyprof_reporting.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 implements the reporting mechanisms for CopyProf, generating
+/// reports with stack traces when unnecessary object copies are destroyed.
+///
+//===----------------------------------------------------------------------===//
+
+#include "copyprof_reporting.h"
+
+#include "copyprof_internal.h"
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_stacktrace.h"
+
+namespace __copyprof {
+namespace {
+
+// TODO: Make configurable via flags.
+constexpr int kMaxNumStackFrames = 30;
+
+} // namespace
+
+void LogCopyProfReport(uptr pc, uptr bp, uptr obj_size, bool did_allocate) {
+ // FIXME: replace hard coded object size with flag.
+ if (obj_size <= 16 || !did_allocate)
+ return;
+ UNINITIALIZED BufferedStackTrace stack_trace;
+ stack_trace.Unwind(pc, bp, /*context=*/nullptr,
+ common_flags()->fast_unwind_on_fatal, kMaxNumStackFrames);
+ InternalScopedString output;
+ output.AppendF(
+ "[copyprof] Destroyed unnecessary copy amounting to %zu bytes:\n",
+ (usize)obj_size);
+ stack_trace.PrintTo(&output);
+ Printf("%s", output.data());
+}
+} // namespace __copyprof
diff --git a/compiler-rt/lib/copyprof/copyprof_reporting.h b/compiler-rt/lib/copyprof/copyprof_reporting.h
new file mode 100644
index 0000000000000..17b5be426346f
--- /dev/null
+++ b/compiler-rt/lib/copyprof/copyprof_reporting.h
@@ -0,0 +1,28 @@
+//===-- copyprof_reporting.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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// This file declares the reporting interface that is used to log unnecessary
+// copies identified during object destruction.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef COPYPROF_REPORTING_H_
+#define COPYPROF_REPORTING_H_
+
+#include "sanitizer_common/sanitizer_common.h"
+
+namespace __copyprof {
+
+// Prints a CopyProf report to stderr. `pc` and `bp` are the program counter
+// and frame pointer where the copy was destroyed, `obj_size` its flat size in
+// bytes, and `did_allocate` whether the copy allocated memory.
+void LogCopyProfReport(uptr pc, uptr bp, uptr obj_size, bool did_allocate);
+
+} // namespace __copyprof
+
+#endif // COPYPROF_REPORTING_H_
diff --git a/compiler-rt/lib/copyprof/copyprof_shadow.cpp b/compiler-rt/lib/copyprof/copyprof_shadow.cpp
new file mode 100644
index 0000000000000..e5b52baa8acd8
--- /dev/null
+++ b/compiler-rt/lib/copyprof/copyprof_shadow.cpp
@@ -0,0 +1,112 @@
+//===-- copyprof_shadow.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 implements the shadow memory management for CopyProf, tracking
+/// the copy status and modification state of application memory.
+///
+//===----------------------------------------------------------------------===//
+
+#include "copyprof_shadow.h"
+
+#include "sanitizer_common/sanitizer_internal_defs.h"
+
...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/223458
More information about the llvm-commits
mailing list