[compiler-rt] [sanitizer_common] Add GNU/Linux main wrapper support (PR #196365)

Kunqiu Chen via llvm-commits llvm-commits at lists.llvm.org
Thu May 7 10:52:32 PDT 2026


================
@@ -0,0 +1,47 @@
+//===-- sanitizer_common_interceptors_main.inc -----------------*- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Common main wrapper interceptor for tools that define MAIN_WRAPPER.
+//
+//===----------------------------------------------------------------------===//
+
+#include "interception/interception.h"
+#include "sanitizer_common.h"
+#include "sanitizer_platform.h"
+
+// __libc_start_main is a dynamic symbol in glibc.
+#if SANITIZER_LINUX && SANITIZER_GLIBC && defined(MAIN_WRAPPER)
+namespace __sanitizer {
+namespace {
+
+MainFnTy real_main;
+
+int WrappedMain(int argc, char **argv, char **envp) {
+  return MAIN_WRAPPER(real_main, argc, argv, envp);
+}
+
+}  // namespace
+}  // namespace __sanitizer
+
+INTERCEPTOR(int, __libc_start_main, __sanitizer::MainFnTy main, int argc,
+            char **argv, void (*init)(), void (*fini)(), void (*rtld_fini)(),
+            void *stack_end) {
+  // This provider is for dynamic glibc startup only. Other libc/startup
+  // schemes need separate platform-specific providers.
+  __interception::DoesNotSupportStaticLinking();
+  if (!REAL(__libc_start_main))
+    CHECK(INTERCEPT_FUNCTION(__libc_start_main));
+  __sanitizer::real_main = main;
+  return REAL(__libc_start_main)(__sanitizer::WrappedMain, argc, argv, init,
+                                 fini, rtld_fini, stack_end);
+}
+
+#  define INIT_MAIN_WRAPPER COMMON_INTERCEPT_FUNCTION(__libc_start_main)
+#else
+#  define INIT_MAIN_WRAPPER
----------------
Camsyn wrote:

As it is implemented by `INTERCEPTOR(xxx)`, I expect it would be treated like interceptors at `sanitizer_common_interceptors.inc`.

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


More information about the llvm-commits mailing list