[compiler-rt] [CopyProf] Implement minimal CopyProf runtime. (PR #223458)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 15 12:07:26 PDT 2026


================
@@ -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 &&
----------------
newgre wrote:

Good point, removed the comparison w/ `SmfContext ::None`

https://github.com/llvm/llvm-project/pull/223458


More information about the llvm-commits mailing list