[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -1520,10 +1520,14 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, } bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_shared)) -return false; - - if (TC.getXRayArgs().needsXRayRt()) { + if (Args.hasArg(options::OPT_shared)) { +if (TC.getXRayArgs().needsXRayDSORt()) { + CmdArgs.push_back("-whole-archive"); sebastiankreutzer wrote: This is a typo, I will add the missing dashes. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -346,8 +392,8 @@ else() # not Apple DEFS ${XRAY_COMMON_DEFINITIONS} OBJECT_LIBS RTXrayBASIC PARENT_TARGET xray) - # Profiler Mode runtime - add_compiler_rt_runtime(clang_rt.xray-profiling +# Profiler Mode runtime +add_compiler_rt_runtime(clang_rt.xray-profiling sebastiankreutzer wrote: The indentation was inconsistent here so I've added the whitespaces to match the previous blocks. Should I revert this change? https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -0,0 +1,62 @@ +//===-- xray_init.cpp ---*- 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 is a part of XRay, a dynamic runtime instrumentation system. +// +// XRay initialisation logic for DSOs. +//===--===// + +#include "sanitizer_common/sanitizer_atomic.h" +#include "xray_defs.h" +#include "xray_flags.h" +#include "xray_interface_internal.h" + +using namespace __sanitizer; + +extern "C" { +extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRayFunctionSledIndex __stop_xray_fn_idx[] __attribute__((weak)) +__attribute__((visibility("hidden"))); + +#if SANITIZER_MAC sebastiankreutzer wrote: This check was adopted from `compiler-rt/lib/xray/xray_init.cpp` (although I just noticed `SANITIZER_MAC` was recently changed to `SANITIZER_APPLE` throughout the project and needs updating). Should I still update it as suggested? https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -50,14 +52,72 @@ atomic_uint8_t XRayInitialized{0}; // This should always be updated before XRayInitialized is updated. SpinMutex XRayInstrMapMutex; -XRaySledMap XRayInstrMap; +// XRaySledMap XRayInstrMap; +// Contains maps for the main executable as well as DSOs. +// std::vector XRayInstrMaps; sebastiankreutzer wrote: Will do https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -111,6 +156,71 @@ void __xray_init() XRAY_NEVER_INSTRUMENT { #endif } +// Default visibility is hidden, so we have to explicitly make it visible to +// DSO. +SANITIZER_INTERFACE_ATTRIBUTE int32_t __xray_register_dso( +const XRaySledEntry *SledsBegin, const XRaySledEntry *SledsEnd, +const XRayFunctionSledIndex *FnIndexBegin, +const XRayFunctionSledIndex *FnIndexEnd, +XRayTrampolines Trampolines) XRAY_NEVER_INSTRUMENT { + // Make sure XRay has been initialized in the main executable. + __xray_init(); + + if (__xray_num_objects() == 0) { +if (Verbosity()) + Report("No XRay instrumentation map in main executable. Not initializing " + "XRay for DSO.\n"); +return -1; + } + + // Register sleds in global map. + int ObjId = __xray_register_sleds(SledsBegin, SledsEnd, FnIndexBegin, + FnIndexEnd, true, Trampolines); + +#ifndef XRAY_NO_PREINIT + if (ObjId >= 0 && flags()->patch_premain) +__xray_patch_object(ObjId); +#endif + + return ObjId; +} + +SANITIZER_INTERFACE_ATTRIBUTE bool +__xray_deregister_dso(int32_t ObjId) XRAY_NEVER_INSTRUMENT { + // Make sure XRay has been initialized in the main executable. + __xray_init(); + + if (ObjId <= 0 || ObjId >= __xray_num_objects()) { +if (Verbosity()) + Report("Can't deregister object with ID %d: ID is invalid.\n", ObjId); +return false; + } + + { +SpinMutexLock Guard(&XRayInstrMapMutex); +auto &Entry = XRayInstrMaps[ObjId]; +if (!Entry.FromDSO) { + if (Verbosity()) +Report("Can't deregister object with ID %d: object does not correspond " + "to a shared library.\n", + ObjId); + return false; +} +if (!Entry.Loaded) { + if (Verbosity()) +Report("Can't deregister object with ID %d: object is not loaded.\n", + ObjId); sebastiankreutzer wrote: I see your point. I will adjust it to only report the debug message and return true. I will also add some documentation for the return values. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -471,42 +518,123 @@ uint16_t __xray_register_event_type( } XRayPatchingStatus __xray_patch() XRAY_NEVER_INSTRUMENT { - return controlPatching(true); + XRayPatchingStatus CombinedStatus{SUCCESS}; + for (size_t I = 0; I < __xray_num_objects(); ++I) { +if (!isObjectLoaded(I)) + continue; +auto LastStatus = controlPatching(true, I); +switch (LastStatus) { +case FAILED: + CombinedStatus = FAILED; + break; +case NOT_INITIALIZED: + if (CombinedStatus != FAILED) +CombinedStatus = NOT_INITIALIZED; + break; +case ONGOING: + if (CombinedStatus != FAILED && CombinedStatus != NOT_INITIALIZED) +CombinedStatus = ONGOING; + break; +default: + break; sebastiankreutzer wrote: Will do https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -471,42 +518,123 @@ uint16_t __xray_register_event_type( } XRayPatchingStatus __xray_patch() XRAY_NEVER_INSTRUMENT { - return controlPatching(true); + XRayPatchingStatus CombinedStatus{SUCCESS}; sebastiankreutzer wrote: I would argue yes, since in this case doing nothing would be the expected behavior. However, in practice this case should never occur because the executable itself is always "loaded". We could add an assertion to make this assumption explicit. What do you think? https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -63,6 +63,10 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) { << XRayInstrument->getSpelling() << Triple.str(); } + if (Args.hasFlag(options::OPT_fxray_enable_shared, + options::OPT_fno_xray_enable_shared, false)) +XRayEnableShared = true; + sebastiankreutzer wrote: Good idea. I will do that. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -0,0 +1,62 @@ +//===-- xray_init.cpp ---*- 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 is a part of XRay, a dynamic runtime instrumentation system. +// +// XRay initialisation logic for DSOs. +//===--===// + +#include "sanitizer_common/sanitizer_atomic.h" +#include "xray_defs.h" +#include "xray_flags.h" +#include "xray_interface_internal.h" + +using namespace __sanitizer; + +extern "C" { +extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRayFunctionSledIndex __stop_xray_fn_idx[] __attribute__((weak)) +__attribute__((visibility("hidden"))); sebastiankreutzer wrote: The `{start,stop}_xray_instr_map` symbols are resolved to the begin and end addresses of the `xray_instr_map` ELF section. Same for the `xray_fn_idx` section. They are using weak linkage so that if, for whatever reason, these sections don't exist, the linker will not fail. In this case, the XRay runtime will simply not register any patchable sleds. I adopted this behavior from the existing `xray_init.cpp`. Because they are using hidden visibility and each DSO has their own `xray_dso_init.cpp` statically linked, these symbols should resolve correctly regardless of `RTLD_GLOBAL`/`RTLD_GLOBAL`. I therefore think that the situation you describe should be handled correctly. I will add tests to verify this. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -0,0 +1,62 @@ +//===-- xray_init.cpp ---*- 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 is a part of XRay, a dynamic runtime instrumentation system. +// +// XRay initialisation logic for DSOs. +//===--===// + +#include "sanitizer_common/sanitizer_atomic.h" +#include "xray_defs.h" +#include "xray_flags.h" +#include "xray_interface_internal.h" + +using namespace __sanitizer; + +extern "C" { +extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRayFunctionSledIndex __stop_xray_fn_idx[] __attribute__((weak)) +__attribute__((visibility("hidden"))); sebastiankreutzer wrote: To your second point: With `-fxray-enable`, are you refering to the general `-fxray-instrument` flag or `-fxray-enable-shared`? If the main executable was built entirely without `-fxray-instrument`, linking the DSO will fail, as the `__xray_register_dso` and `__xray_deregister_dso` functions could not be resolved. Otherwise, the XRay runtime will currently always work with instrumented DSOs, regardless of whether the executable was compiled with `-fxray-enable-shared` or not. I don't see a reason to change this, since IMO there is no benefit (performance or otherwise) from disabling this feature statically. I can see practical use cases where one might want to explicitly disable patching DSOs (all or a specific subset). We could handle this by adding a runtime option, e.g. `xray_patch_dsos=true/false/list of names`. However, this would probably be best as a separate PR. Let me know what you think. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer edited https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: Ping https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer updated https://github.com/llvm/llvm-project/pull/90959 >From 86e252cb84803bfaa2ec096b671ef366fd3ac5cb Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 26 Oct 2023 15:13:05 +0200 Subject: [PATCH] [XRay] Add DSO support for XRay instrumentation on X86_64 --- clang/include/clang/Basic/CodeGenOptions.def | 2 + clang/include/clang/Driver/Options.td | 5 + clang/include/clang/Driver/XRayArgs.h | 4 + clang/lib/Driver/ToolChains/CommonArgs.cpp| 12 +- clang/lib/Driver/XRayArgs.cpp | 21 ++ clang/test/Driver/XRay/xray-enable-shared.cpp | 17 + .../cmake/Modules/AllSupportedArchDefs.cmake | 1 + compiler-rt/cmake/config-ix.cmake | 4 + compiler-rt/include/xray/xray_interface.h | 23 ++ compiler-rt/lib/xray/CMakeLists.txt | 86 +- compiler-rt/lib/xray/xray_dso_init.cpp| 62 compiler-rt/lib/xray/xray_init.cpp| 183 +-- compiler-rt/lib/xray/xray_interface.cpp | 292 ++ .../lib/xray/xray_interface_internal.h| 83 - compiler-rt/lib/xray/xray_trampoline_x86_64.S | 24 +- compiler-rt/lib/xray/xray_x86_64.cpp | 23 +- .../xray/TestCases/Posix/basic-mode-dso.cpp | 47 +++ .../TestCases/Posix/clang-enable-shared.cpp | 14 + .../test/xray/TestCases/Posix/dlopen.cpp | 107 +++ .../xray/TestCases/Posix/dso-dep-chains.cpp | 197 .../TestCases/Posix/patch-premain-dso.cpp | 45 +++ .../Posix/patching-unpatching-dso.cpp | 75 + 22 files changed, 1189 insertions(+), 138 deletions(-) create mode 100644 clang/test/Driver/XRay/xray-enable-shared.cpp create mode 100644 compiler-rt/lib/xray/xray_dso_init.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/basic-mode-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/clang-enable-shared.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dlopen.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dso-dep-chains.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patch-premain-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def index b600198998d85b..84006a884274f7 100644 --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -133,6 +133,8 @@ CODEGENOPT(XRayIgnoreLoops , 1, 0) ///< Emit the XRay function index section. CODEGENOPT(XRayFunctionIndex , 1, 1) +///< Set when -fxray-enable-shared is enabled +CODEGENOPT(XRayEnableShared , 1, 0) ///< Set the minimum number of instructions in a function to determine selective ///< XRay instrumentation. diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f78032255f036f..a22891e1ca6710 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2922,6 +2922,11 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +defm xray_enable_shared : BoolFOption<"xray-enable-shared", + CodeGenOpts<"XRayEnableShared">, DefaultFalse, + PosFlag, + NegFlag>; defm fine_grained_bitfield_accesses : BoolOption<"f", "fine-grained-bitfield-accesses", CodeGenOpts<"FineGrainedBitfieldAccesses">, DefaultFalse, diff --git a/clang/include/clang/Driver/XRayArgs.h b/clang/include/clang/Driver/XRayArgs.h index bdd3d979547eed..90a21e69586033 100644 --- a/clang/include/clang/Driver/XRayArgs.h +++ b/clang/include/clang/Driver/XRayArgs.h @@ -27,6 +27,7 @@ class XRayArgs { XRayInstrSet InstrumentationBundle; llvm::opt::Arg *XRayInstrument = nullptr; bool XRayRT = true; + bool XRayEnableShared = false; public: /// Parses the XRay arguments from an argument list. @@ -35,6 +36,9 @@ class XRayArgs { llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const; bool needsXRayRt() const { return XRayInstrument && XRayRT; } + bool needsXRayDSORt() const { +return XRayInstrument && XRayRT && XRayEnableShared; + } llvm::ArrayRef modeList() const { return Modes; } XRayInstrSet instrumentationBundle() const { return InstrumentationBundle; } }; diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 2ce6779f4b43e3..52a8cf90836033 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1629,10 +1629,14 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, } bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_shared)) -return false; - - if (TC.getXRayArgs().needs
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: I rebased on upstream `main` again and tested locally. @MaskRay Let me know if you have further comments or concerns. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer updated https://github.com/llvm/llvm-project/pull/90959 >From 1f0484b73ad0bb9b40e3cd86d8abad4af14e32dc Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 26 Oct 2023 15:13:05 +0200 Subject: [PATCH 1/7] [XRay] Add DSO support for XRay instrumentation on X86_64 --- clang/include/clang/Driver/Options.td | 4 + clang/include/clang/Driver/XRayArgs.h | 4 + clang/lib/Driver/ToolChains/CommonArgs.cpp| 12 +- clang/lib/Driver/XRayArgs.cpp | 7 + .../cmake/Modules/AllSupportedArchDefs.cmake | 1 + compiler-rt/cmake/config-ix.cmake | 4 + compiler-rt/include/xray/xray_interface.h | 23 ++ compiler-rt/lib/xray/CMakeLists.txt | 73 - compiler-rt/lib/xray/xray_dso_init.cpp| 62 + compiler-rt/lib/xray/xray_init.cpp| 158 +-- compiler-rt/lib/xray/xray_interface.cpp | 261 ++ .../lib/xray/xray_interface_internal.h| 83 +- compiler-rt/lib/xray/xray_trampoline_x86_64.S | 24 +- compiler-rt/lib/xray/xray_x86_64.cpp | 23 +- .../xray/TestCases/Posix/basic-mode-dso.cpp | 47 .../TestCases/Posix/clang-enable-shared.cpp | 14 + .../test/xray/TestCases/Posix/dlopen.cpp | 110 .../TestCases/Posix/patch-premain-dso.cpp | 45 +++ .../Posix/patching-unpatching-dso.cpp | 75 + 19 files changed, 912 insertions(+), 118 deletions(-) create mode 100644 compiler-rt/lib/xray/xray_dso_init.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/basic-mode-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/clang-enable-shared.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dlopen.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patch-premain-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 953f6fc649e62..3e3be5475c0c4 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2850,6 +2850,10 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +def fxray_enable_shared : Flag<["-"], "fxray-enable-shared">, Group, Visibility<[ClangOption, CC1Option]>, + HelpText<"Enable shared library instrumentation.">; +def fno_xray_enable_shared : Flag<["-"], "fno-xray-enable-shared">, Group, + Visibility<[ClangOption, CC1Option]>; defm fine_grained_bitfield_accesses : BoolOption<"f", "fine-grained-bitfield-accesses", CodeGenOpts<"FineGrainedBitfieldAccesses">, DefaultFalse, diff --git a/clang/include/clang/Driver/XRayArgs.h b/clang/include/clang/Driver/XRayArgs.h index bdd3d979547ee..90a21e6958603 100644 --- a/clang/include/clang/Driver/XRayArgs.h +++ b/clang/include/clang/Driver/XRayArgs.h @@ -27,6 +27,7 @@ class XRayArgs { XRayInstrSet InstrumentationBundle; llvm::opt::Arg *XRayInstrument = nullptr; bool XRayRT = true; + bool XRayEnableShared = false; public: /// Parses the XRay arguments from an argument list. @@ -35,6 +36,9 @@ class XRayArgs { llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const; bool needsXRayRt() const { return XRayInstrument && XRayRT; } + bool needsXRayDSORt() const { +return XRayInstrument && XRayRT && XRayEnableShared; + } llvm::ArrayRef modeList() const { return Modes; } XRayInstrSet instrumentationBundle() const { return InstrumentationBundle; } }; diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 6796b43a15502..399bf795ce394 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1520,10 +1520,14 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, } bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_shared)) -return false; - - if (TC.getXRayArgs().needsXRayRt()) { + if (Args.hasArg(options::OPT_shared)) { +if (TC.getXRayArgs().needsXRayDSORt()) { + CmdArgs.push_back("-whole-archive"); + CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray-dso")); + CmdArgs.push_back("-no-whole-archive"); + return true; +} + } else if (TC.getXRayArgs().needsXRayRt()) { CmdArgs.push_back("--whole-archive"); CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray")); for (const auto &Mode : TC.getXRayArgs().modeList()) diff --git a/clang/lib/Driver/XRayArgs.cpp b/clang/lib/Driver/XRayArgs.cpp index 8c5134e250135..7809cd7ef7c75 100644 --- a/clang/lib/Driver/XRayArgs.cpp +++ b/clang/lib/Driver/XRayArgs.cpp @@ -63,6 +63,10 @@ XRayArgs::XRayArgs(const ToolChain &TC, const Arg
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -0,0 +1,62 @@ +//===-- xray_init.cpp ---*- 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 is a part of XRay, a dynamic runtime instrumentation system. +// +// XRay initialisation logic for DSOs. +//===--===// + +#include "sanitizer_common/sanitizer_atomic.h" +#include "xray_defs.h" +#include "xray_flags.h" +#include "xray_interface_internal.h" + +using namespace __sanitizer; + +extern "C" { +extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak)) +__attribute__((visibility("hidden"))); +extern const XRayFunctionSledIndex __stop_xray_fn_idx[] __attribute__((weak)) +__attribute__((visibility("hidden"))); sebastiankreutzer wrote: I have added a test for the described scenario in [compiler-rt/test/xray/TestCases/Posix/dso-dep-chains.cpp](https://github.com/llvm/llvm-project/pull/90959/files#diff-01ed9b08b391513366a28d78cadb47b46f7b76f8b03163cd1e196e46b9892304). https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -63,6 +63,10 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) { << XRayInstrument->getSpelling() << Triple.str(); } + if (Args.hasFlag(options::OPT_fxray_enable_shared, + options::OPT_fno_xray_enable_shared, false)) +XRayEnableShared = true; + sebastiankreutzer wrote: PIC is now required and tested in [clang/test/Driver/XRay/xray-enable-shared.cpp](https://github.com/llvm/llvm-project/pull/90959/files#diff-f1d4e7dfcc8404c912868e2259709fcdd7d86cff83531591a1254c2a69412066) https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer updated https://github.com/llvm/llvm-project/pull/90959 >From 5143ef41ac88d88e53ebbf7d404f5a86ccd0a5c1 Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 26 Oct 2023 15:13:05 +0200 Subject: [PATCH 1/7] [XRay] Add DSO support for XRay instrumentation on X86_64 --- clang/include/clang/Driver/Options.td | 4 + clang/include/clang/Driver/XRayArgs.h | 4 + clang/lib/Driver/ToolChains/CommonArgs.cpp| 12 +- clang/lib/Driver/XRayArgs.cpp | 7 + .../cmake/Modules/AllSupportedArchDefs.cmake | 1 + compiler-rt/cmake/config-ix.cmake | 4 + compiler-rt/include/xray/xray_interface.h | 23 ++ compiler-rt/lib/xray/CMakeLists.txt | 73 - compiler-rt/lib/xray/xray_dso_init.cpp| 62 + compiler-rt/lib/xray/xray_init.cpp| 158 +-- compiler-rt/lib/xray/xray_interface.cpp | 261 ++ .../lib/xray/xray_interface_internal.h| 83 +- compiler-rt/lib/xray/xray_trampoline_x86_64.S | 24 +- compiler-rt/lib/xray/xray_x86_64.cpp | 23 +- .../xray/TestCases/Posix/basic-mode-dso.cpp | 47 .../TestCases/Posix/clang-enable-shared.cpp | 14 + .../test/xray/TestCases/Posix/dlopen.cpp | 110 .../TestCases/Posix/patch-premain-dso.cpp | 45 +++ .../Posix/patching-unpatching-dso.cpp | 75 + 19 files changed, 912 insertions(+), 118 deletions(-) create mode 100644 compiler-rt/lib/xray/xray_dso_init.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/basic-mode-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/clang-enable-shared.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dlopen.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patch-premain-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index fae865571001c..01b6e16262e9c 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2887,6 +2887,10 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +def fxray_enable_shared : Flag<["-"], "fxray-enable-shared">, Group, Visibility<[ClangOption, CC1Option]>, + HelpText<"Enable shared library instrumentation.">; +def fno_xray_enable_shared : Flag<["-"], "fno-xray-enable-shared">, Group, + Visibility<[ClangOption, CC1Option]>; defm fine_grained_bitfield_accesses : BoolOption<"f", "fine-grained-bitfield-accesses", CodeGenOpts<"FineGrainedBitfieldAccesses">, DefaultFalse, diff --git a/clang/include/clang/Driver/XRayArgs.h b/clang/include/clang/Driver/XRayArgs.h index bdd3d979547ee..90a21e6958603 100644 --- a/clang/include/clang/Driver/XRayArgs.h +++ b/clang/include/clang/Driver/XRayArgs.h @@ -27,6 +27,7 @@ class XRayArgs { XRayInstrSet InstrumentationBundle; llvm::opt::Arg *XRayInstrument = nullptr; bool XRayRT = true; + bool XRayEnableShared = false; public: /// Parses the XRay arguments from an argument list. @@ -35,6 +36,9 @@ class XRayArgs { llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const; bool needsXRayRt() const { return XRayInstrument && XRayRT; } + bool needsXRayDSORt() const { +return XRayInstrument && XRayRT && XRayEnableShared; + } llvm::ArrayRef modeList() const { return Modes; } XRayInstrSet instrumentationBundle() const { return InstrumentationBundle; } }; diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index ab1590104b790..b67e09145db2a 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1593,10 +1593,14 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, } bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_shared)) -return false; - - if (TC.getXRayArgs().needsXRayRt()) { + if (Args.hasArg(options::OPT_shared)) { +if (TC.getXRayArgs().needsXRayDSORt()) { + CmdArgs.push_back("-whole-archive"); + CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray-dso")); + CmdArgs.push_back("-no-whole-archive"); + return true; +} + } else if (TC.getXRayArgs().needsXRayRt()) { CmdArgs.push_back("--whole-archive"); CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray")); for (const auto &Mode : TC.getXRayArgs().modeList()) diff --git a/clang/lib/Driver/XRayArgs.cpp b/clang/lib/Driver/XRayArgs.cpp index 8c5134e250135..7809cd7ef7c75 100644 --- a/clang/lib/Driver/XRayArgs.cpp +++ b/clang/lib/Driver/XRayArgs.cpp @@ -63,6 +63,10 @@ XRayArgs::XRayArgs(const ToolChain &TC, const Arg
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -471,42 +518,123 @@ uint16_t __xray_register_event_type( } XRayPatchingStatus __xray_patch() XRAY_NEVER_INSTRUMENT { - return controlPatching(true); + XRayPatchingStatus CombinedStatus{SUCCESS}; sebastiankreutzer wrote: After looking at it again, I have refactored the affected functions a bit to reduce code duplication and clarify the returned status. I no objects are loaded, the returned status is `NOT_INITIALIZED`. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: Needed to rebase because buildkite was complaining about archived pipelines. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: > You might want to get approval from someone else - I'm not even remotely > close to a maintainer here. Will do - thanks for taking the time to review! https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: @petrhosek @MaskRay Could one of you have a look to give final approval? https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer created https://github.com/llvm/llvm-project/pull/90959 This PR introduces shared library (DSO) support for XRay based on a revised version of the implementation outlined in [this RFC](https://discourse.llvm.org/t/rfc-upstreaming-dso-instrumentation-support-for-xray/73000). The feature enables the patching and handling of events from DSOs, supporting both libraries linked at startup or explicitly loaded, e.g. via `dlopen`. This patch adds the following: - The `-fxray-enable-shared` flag to enable the feature (turned off by default) - A small runtime library that is linked into every instrumented DSO, providing position-independent trampolines and code to register with the main XRay runtime - Changes to the XRay runtime to support management and patching of multiple objects These changes are fully backward compatible, i.e. running without instrumented DSOs will produce identical traces (in terms of recorded function IDs) to the previous implementation. Due to my limited ability to test on other architectures, this feature is only implemented and tested with x86_64. Extending support to other architectures is fairly straightforward, requiring only a position-independent implementation of the architecture-specific trampoline implementation (see `compiler-rt/lib/xray/xray_trampoline_x86_64.S` for reference). This patch does not include any functionality to resolve function IDs from DSOs for the provided logging/tracing modes. These modes still work and will record calls from DSOs, but symbol resolution for these functions in not available. Getting this to work properly requires recording information about the loaded DSOs and should IMO be discussed in a separate RFC, as there are mulitple feasible approaches. @petrhosek @jplehr >From 1f0484b73ad0bb9b40e3cd86d8abad4af14e32dc Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 26 Oct 2023 15:13:05 +0200 Subject: [PATCH] [XRay] Add DSO support for XRay instrumentation on X86_64 --- clang/include/clang/Driver/Options.td | 4 + clang/include/clang/Driver/XRayArgs.h | 4 + clang/lib/Driver/ToolChains/CommonArgs.cpp| 12 +- clang/lib/Driver/XRayArgs.cpp | 7 + .../cmake/Modules/AllSupportedArchDefs.cmake | 1 + compiler-rt/cmake/config-ix.cmake | 4 + compiler-rt/include/xray/xray_interface.h | 23 ++ compiler-rt/lib/xray/CMakeLists.txt | 73 - compiler-rt/lib/xray/xray_dso_init.cpp| 62 + compiler-rt/lib/xray/xray_init.cpp| 158 +-- compiler-rt/lib/xray/xray_interface.cpp | 261 ++ .../lib/xray/xray_interface_internal.h| 83 +- compiler-rt/lib/xray/xray_trampoline_x86_64.S | 24 +- compiler-rt/lib/xray/xray_x86_64.cpp | 23 +- .../xray/TestCases/Posix/basic-mode-dso.cpp | 47 .../TestCases/Posix/clang-enable-shared.cpp | 14 + .../test/xray/TestCases/Posix/dlopen.cpp | 110 .../TestCases/Posix/patch-premain-dso.cpp | 45 +++ .../Posix/patching-unpatching-dso.cpp | 75 + 19 files changed, 912 insertions(+), 118 deletions(-) create mode 100644 compiler-rt/lib/xray/xray_dso_init.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/basic-mode-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/clang-enable-shared.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dlopen.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patch-premain-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 953f6fc649e621..3e3be5475c0c4d 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2850,6 +2850,10 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +def fxray_enable_shared : Flag<["-"], "fxray-enable-shared">, Group, Visibility<[ClangOption, CC1Option]>, + HelpText<"Enable shared library instrumentation.">; +def fno_xray_enable_shared : Flag<["-"], "fno-xray-enable-shared">, Group, + Visibility<[ClangOption, CC1Option]>; defm fine_grained_bitfield_accesses : BoolOption<"f", "fine-grained-bitfield-accesses", CodeGenOpts<"FineGrainedBitfieldAccesses">, DefaultFalse, diff --git a/clang/include/clang/Driver/XRayArgs.h b/clang/include/clang/Driver/XRayArgs.h index bdd3d979547eed..90a21e69586033 100644 --- a/clang/include/clang/Driver/XRayArgs.h +++ b/clang/include/clang/Driver/XRayArgs.h @@ -27,6 +27,7 @@ class XRayArgs { XRayInstrSet InstrumentationBundle; llvm::opt::Arg *XRayInstrument = nullptr; bool XRayRT = true; + bool XRayEnableShared = false; public: /// Parses the XRay arguments from an ar
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: Can't reproduce the buildbot failures, will try to find the cause. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer updated https://github.com/llvm/llvm-project/pull/90959 >From 1f0484b73ad0bb9b40e3cd86d8abad4af14e32dc Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 26 Oct 2023 15:13:05 +0200 Subject: [PATCH 1/4] [XRay] Add DSO support for XRay instrumentation on X86_64 --- clang/include/clang/Driver/Options.td | 4 + clang/include/clang/Driver/XRayArgs.h | 4 + clang/lib/Driver/ToolChains/CommonArgs.cpp| 12 +- clang/lib/Driver/XRayArgs.cpp | 7 + .../cmake/Modules/AllSupportedArchDefs.cmake | 1 + compiler-rt/cmake/config-ix.cmake | 4 + compiler-rt/include/xray/xray_interface.h | 23 ++ compiler-rt/lib/xray/CMakeLists.txt | 73 - compiler-rt/lib/xray/xray_dso_init.cpp| 62 + compiler-rt/lib/xray/xray_init.cpp| 158 +-- compiler-rt/lib/xray/xray_interface.cpp | 261 ++ .../lib/xray/xray_interface_internal.h| 83 +- compiler-rt/lib/xray/xray_trampoline_x86_64.S | 24 +- compiler-rt/lib/xray/xray_x86_64.cpp | 23 +- .../xray/TestCases/Posix/basic-mode-dso.cpp | 47 .../TestCases/Posix/clang-enable-shared.cpp | 14 + .../test/xray/TestCases/Posix/dlopen.cpp | 110 .../TestCases/Posix/patch-premain-dso.cpp | 45 +++ .../Posix/patching-unpatching-dso.cpp | 75 + 19 files changed, 912 insertions(+), 118 deletions(-) create mode 100644 compiler-rt/lib/xray/xray_dso_init.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/basic-mode-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/clang-enable-shared.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dlopen.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patch-premain-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 953f6fc649e621..3e3be5475c0c4d 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2850,6 +2850,10 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +def fxray_enable_shared : Flag<["-"], "fxray-enable-shared">, Group, Visibility<[ClangOption, CC1Option]>, + HelpText<"Enable shared library instrumentation.">; +def fno_xray_enable_shared : Flag<["-"], "fno-xray-enable-shared">, Group, + Visibility<[ClangOption, CC1Option]>; defm fine_grained_bitfield_accesses : BoolOption<"f", "fine-grained-bitfield-accesses", CodeGenOpts<"FineGrainedBitfieldAccesses">, DefaultFalse, diff --git a/clang/include/clang/Driver/XRayArgs.h b/clang/include/clang/Driver/XRayArgs.h index bdd3d979547eed..90a21e69586033 100644 --- a/clang/include/clang/Driver/XRayArgs.h +++ b/clang/include/clang/Driver/XRayArgs.h @@ -27,6 +27,7 @@ class XRayArgs { XRayInstrSet InstrumentationBundle; llvm::opt::Arg *XRayInstrument = nullptr; bool XRayRT = true; + bool XRayEnableShared = false; public: /// Parses the XRay arguments from an argument list. @@ -35,6 +36,9 @@ class XRayArgs { llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const; bool needsXRayRt() const { return XRayInstrument && XRayRT; } + bool needsXRayDSORt() const { +return XRayInstrument && XRayRT && XRayEnableShared; + } llvm::ArrayRef modeList() const { return Modes; } XRayInstrSet instrumentationBundle() const { return InstrumentationBundle; } }; diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 6796b43a155020..399bf795ce394e 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1520,10 +1520,14 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, } bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_shared)) -return false; - - if (TC.getXRayArgs().needsXRayRt()) { + if (Args.hasArg(options::OPT_shared)) { +if (TC.getXRayArgs().needsXRayDSORt()) { + CmdArgs.push_back("-whole-archive"); + CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray-dso")); + CmdArgs.push_back("-no-whole-archive"); + return true; +} + } else if (TC.getXRayArgs().needsXRayRt()) { CmdArgs.push_back("--whole-archive"); CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray")); for (const auto &Mode : TC.getXRayArgs().modeList()) diff --git a/clang/lib/Driver/XRayArgs.cpp b/clang/lib/Driver/XRayArgs.cpp index 8c5134e2501358..7809cd7ef7c759 100644 --- a/clang/lib/Driver/XRayArgs.cpp +++ b/clang/lib/Driver/XRayArgs.cpp @@ -63,6 +63,10 @@ XRayArgs::XRayArgs(const ToolChain &TC, c
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer updated https://github.com/llvm/llvm-project/pull/90959 >From 1f0484b73ad0bb9b40e3cd86d8abad4af14e32dc Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 26 Oct 2023 15:13:05 +0200 Subject: [PATCH 1/5] [XRay] Add DSO support for XRay instrumentation on X86_64 --- clang/include/clang/Driver/Options.td | 4 + clang/include/clang/Driver/XRayArgs.h | 4 + clang/lib/Driver/ToolChains/CommonArgs.cpp| 12 +- clang/lib/Driver/XRayArgs.cpp | 7 + .../cmake/Modules/AllSupportedArchDefs.cmake | 1 + compiler-rt/cmake/config-ix.cmake | 4 + compiler-rt/include/xray/xray_interface.h | 23 ++ compiler-rt/lib/xray/CMakeLists.txt | 73 - compiler-rt/lib/xray/xray_dso_init.cpp| 62 + compiler-rt/lib/xray/xray_init.cpp| 158 +-- compiler-rt/lib/xray/xray_interface.cpp | 261 ++ .../lib/xray/xray_interface_internal.h| 83 +- compiler-rt/lib/xray/xray_trampoline_x86_64.S | 24 +- compiler-rt/lib/xray/xray_x86_64.cpp | 23 +- .../xray/TestCases/Posix/basic-mode-dso.cpp | 47 .../TestCases/Posix/clang-enable-shared.cpp | 14 + .../test/xray/TestCases/Posix/dlopen.cpp | 110 .../TestCases/Posix/patch-premain-dso.cpp | 45 +++ .../Posix/patching-unpatching-dso.cpp | 75 + 19 files changed, 912 insertions(+), 118 deletions(-) create mode 100644 compiler-rt/lib/xray/xray_dso_init.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/basic-mode-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/clang-enable-shared.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dlopen.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patch-premain-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 953f6fc649e621..3e3be5475c0c4d 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2850,6 +2850,10 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +def fxray_enable_shared : Flag<["-"], "fxray-enable-shared">, Group, Visibility<[ClangOption, CC1Option]>, + HelpText<"Enable shared library instrumentation.">; +def fno_xray_enable_shared : Flag<["-"], "fno-xray-enable-shared">, Group, + Visibility<[ClangOption, CC1Option]>; defm fine_grained_bitfield_accesses : BoolOption<"f", "fine-grained-bitfield-accesses", CodeGenOpts<"FineGrainedBitfieldAccesses">, DefaultFalse, diff --git a/clang/include/clang/Driver/XRayArgs.h b/clang/include/clang/Driver/XRayArgs.h index bdd3d979547eed..90a21e69586033 100644 --- a/clang/include/clang/Driver/XRayArgs.h +++ b/clang/include/clang/Driver/XRayArgs.h @@ -27,6 +27,7 @@ class XRayArgs { XRayInstrSet InstrumentationBundle; llvm::opt::Arg *XRayInstrument = nullptr; bool XRayRT = true; + bool XRayEnableShared = false; public: /// Parses the XRay arguments from an argument list. @@ -35,6 +36,9 @@ class XRayArgs { llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const; bool needsXRayRt() const { return XRayInstrument && XRayRT; } + bool needsXRayDSORt() const { +return XRayInstrument && XRayRT && XRayEnableShared; + } llvm::ArrayRef modeList() const { return Modes; } XRayInstrSet instrumentationBundle() const { return InstrumentationBundle; } }; diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 6796b43a155020..399bf795ce394e 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1520,10 +1520,14 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, } bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_shared)) -return false; - - if (TC.getXRayArgs().needsXRayRt()) { + if (Args.hasArg(options::OPT_shared)) { +if (TC.getXRayArgs().needsXRayDSORt()) { + CmdArgs.push_back("-whole-archive"); + CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray-dso")); + CmdArgs.push_back("-no-whole-archive"); + return true; +} + } else if (TC.getXRayArgs().needsXRayRt()) { CmdArgs.push_back("--whole-archive"); CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray")); for (const auto &Mode : TC.getXRayArgs().modeList()) diff --git a/clang/lib/Driver/XRayArgs.cpp b/clang/lib/Driver/XRayArgs.cpp index 8c5134e2501358..7809cd7ef7c759 100644 --- a/clang/lib/Driver/XRayArgs.cpp +++ b/clang/lib/Driver/XRayArgs.cpp @@ -63,6 +63,10 @@ XRayArgs::XRayArgs(const ToolChain &TC, c
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer updated https://github.com/llvm/llvm-project/pull/90959 >From 1f0484b73ad0bb9b40e3cd86d8abad4af14e32dc Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 26 Oct 2023 15:13:05 +0200 Subject: [PATCH 1/6] [XRay] Add DSO support for XRay instrumentation on X86_64 --- clang/include/clang/Driver/Options.td | 4 + clang/include/clang/Driver/XRayArgs.h | 4 + clang/lib/Driver/ToolChains/CommonArgs.cpp| 12 +- clang/lib/Driver/XRayArgs.cpp | 7 + .../cmake/Modules/AllSupportedArchDefs.cmake | 1 + compiler-rt/cmake/config-ix.cmake | 4 + compiler-rt/include/xray/xray_interface.h | 23 ++ compiler-rt/lib/xray/CMakeLists.txt | 73 - compiler-rt/lib/xray/xray_dso_init.cpp| 62 + compiler-rt/lib/xray/xray_init.cpp| 158 +-- compiler-rt/lib/xray/xray_interface.cpp | 261 ++ .../lib/xray/xray_interface_internal.h| 83 +- compiler-rt/lib/xray/xray_trampoline_x86_64.S | 24 +- compiler-rt/lib/xray/xray_x86_64.cpp | 23 +- .../xray/TestCases/Posix/basic-mode-dso.cpp | 47 .../TestCases/Posix/clang-enable-shared.cpp | 14 + .../test/xray/TestCases/Posix/dlopen.cpp | 110 .../TestCases/Posix/patch-premain-dso.cpp | 45 +++ .../Posix/patching-unpatching-dso.cpp | 75 + 19 files changed, 912 insertions(+), 118 deletions(-) create mode 100644 compiler-rt/lib/xray/xray_dso_init.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/basic-mode-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/clang-enable-shared.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dlopen.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patch-premain-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 953f6fc649e621..3e3be5475c0c4d 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2850,6 +2850,10 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +def fxray_enable_shared : Flag<["-"], "fxray-enable-shared">, Group, Visibility<[ClangOption, CC1Option]>, + HelpText<"Enable shared library instrumentation.">; +def fno_xray_enable_shared : Flag<["-"], "fno-xray-enable-shared">, Group, + Visibility<[ClangOption, CC1Option]>; defm fine_grained_bitfield_accesses : BoolOption<"f", "fine-grained-bitfield-accesses", CodeGenOpts<"FineGrainedBitfieldAccesses">, DefaultFalse, diff --git a/clang/include/clang/Driver/XRayArgs.h b/clang/include/clang/Driver/XRayArgs.h index bdd3d979547eed..90a21e69586033 100644 --- a/clang/include/clang/Driver/XRayArgs.h +++ b/clang/include/clang/Driver/XRayArgs.h @@ -27,6 +27,7 @@ class XRayArgs { XRayInstrSet InstrumentationBundle; llvm::opt::Arg *XRayInstrument = nullptr; bool XRayRT = true; + bool XRayEnableShared = false; public: /// Parses the XRay arguments from an argument list. @@ -35,6 +36,9 @@ class XRayArgs { llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const; bool needsXRayRt() const { return XRayInstrument && XRayRT; } + bool needsXRayDSORt() const { +return XRayInstrument && XRayRT && XRayEnableShared; + } llvm::ArrayRef modeList() const { return Modes; } XRayInstrSet instrumentationBundle() const { return InstrumentationBundle; } }; diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 6796b43a155020..399bf795ce394e 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1520,10 +1520,14 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, } bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_shared)) -return false; - - if (TC.getXRayArgs().needsXRayRt()) { + if (Args.hasArg(options::OPT_shared)) { +if (TC.getXRayArgs().needsXRayDSORt()) { + CmdArgs.push_back("-whole-archive"); + CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray-dso")); + CmdArgs.push_back("-no-whole-archive"); + return true; +} + } else if (TC.getXRayArgs().needsXRayRt()) { CmdArgs.push_back("--whole-archive"); CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray")); for (const auto &Mode : TC.getXRayArgs().modeList()) diff --git a/clang/lib/Driver/XRayArgs.cpp b/clang/lib/Driver/XRayArgs.cpp index 8c5134e2501358..7809cd7ef7c759 100644 --- a/clang/lib/Driver/XRayArgs.cpp +++ b/clang/lib/Driver/XRayArgs.cpp @@ -63,6 +63,10 @@ XRayArgs::XRayArgs(const ToolChain &TC, c
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: Ping https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: > @sebastiankreutzer hey, were you able to reach the maintainers of llvm-xray > and probe them about its dev status? If yes, can you pls share how to reach > them and what did they say? Unfortunately no, I have no direct contact. It was my impression that none of the maintainers are active anymore. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer updated https://github.com/llvm/llvm-project/pull/90959 >From 5143ef41ac88d88e53ebbf7d404f5a86ccd0a5c1 Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 26 Oct 2023 15:13:05 +0200 Subject: [PATCH 1/8] [XRay] Add DSO support for XRay instrumentation on X86_64 --- clang/include/clang/Driver/Options.td | 4 + clang/include/clang/Driver/XRayArgs.h | 4 + clang/lib/Driver/ToolChains/CommonArgs.cpp| 12 +- clang/lib/Driver/XRayArgs.cpp | 7 + .../cmake/Modules/AllSupportedArchDefs.cmake | 1 + compiler-rt/cmake/config-ix.cmake | 4 + compiler-rt/include/xray/xray_interface.h | 23 ++ compiler-rt/lib/xray/CMakeLists.txt | 73 - compiler-rt/lib/xray/xray_dso_init.cpp| 62 + compiler-rt/lib/xray/xray_init.cpp| 158 +-- compiler-rt/lib/xray/xray_interface.cpp | 261 ++ .../lib/xray/xray_interface_internal.h| 83 +- compiler-rt/lib/xray/xray_trampoline_x86_64.S | 24 +- compiler-rt/lib/xray/xray_x86_64.cpp | 23 +- .../xray/TestCases/Posix/basic-mode-dso.cpp | 47 .../TestCases/Posix/clang-enable-shared.cpp | 14 + .../test/xray/TestCases/Posix/dlopen.cpp | 110 .../TestCases/Posix/patch-premain-dso.cpp | 45 +++ .../Posix/patching-unpatching-dso.cpp | 75 + 19 files changed, 912 insertions(+), 118 deletions(-) create mode 100644 compiler-rt/lib/xray/xray_dso_init.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/basic-mode-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/clang-enable-shared.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dlopen.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patch-premain-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index fae865571001ce..01b6e16262e9c9 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2887,6 +2887,10 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +def fxray_enable_shared : Flag<["-"], "fxray-enable-shared">, Group, Visibility<[ClangOption, CC1Option]>, + HelpText<"Enable shared library instrumentation.">; +def fno_xray_enable_shared : Flag<["-"], "fno-xray-enable-shared">, Group, + Visibility<[ClangOption, CC1Option]>; defm fine_grained_bitfield_accesses : BoolOption<"f", "fine-grained-bitfield-accesses", CodeGenOpts<"FineGrainedBitfieldAccesses">, DefaultFalse, diff --git a/clang/include/clang/Driver/XRayArgs.h b/clang/include/clang/Driver/XRayArgs.h index bdd3d979547eed..90a21e69586033 100644 --- a/clang/include/clang/Driver/XRayArgs.h +++ b/clang/include/clang/Driver/XRayArgs.h @@ -27,6 +27,7 @@ class XRayArgs { XRayInstrSet InstrumentationBundle; llvm::opt::Arg *XRayInstrument = nullptr; bool XRayRT = true; + bool XRayEnableShared = false; public: /// Parses the XRay arguments from an argument list. @@ -35,6 +36,9 @@ class XRayArgs { llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const; bool needsXRayRt() const { return XRayInstrument && XRayRT; } + bool needsXRayDSORt() const { +return XRayInstrument && XRayRT && XRayEnableShared; + } llvm::ArrayRef modeList() const { return Modes; } XRayInstrSet instrumentationBundle() const { return InstrumentationBundle; } }; diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index ab1590104b7903..b67e09145db2af 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1593,10 +1593,14 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, } bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_shared)) -return false; - - if (TC.getXRayArgs().needsXRayRt()) { + if (Args.hasArg(options::OPT_shared)) { +if (TC.getXRayArgs().needsXRayDSORt()) { + CmdArgs.push_back("-whole-archive"); + CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray-dso")); + CmdArgs.push_back("-no-whole-archive"); + return true; +} + } else if (TC.getXRayArgs().needsXRayRt()) { CmdArgs.push_back("--whole-archive"); CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray")); for (const auto &Mode : TC.getXRayArgs().modeList()) diff --git a/clang/lib/Driver/XRayArgs.cpp b/clang/lib/Driver/XRayArgs.cpp index 8c5134e2501358..7809cd7ef7c759 100644 --- a/clang/lib/Driver/XRayArgs.cpp +++ b/clang/lib/Driver/XRayArgs.cpp @@ -63,6 +63,10 @@ XRayArgs::XRayArgs(const ToolChain &TC, c
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -2887,6 +2887,10 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +def fxray_enable_shared : Flag<["-"], "fxray-enable-shared">, Group, Visibility<[ClangOption, CC1Option]>, sebastiankreutzer wrote: Thanks, I changed the option as requested https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: @androm3da @MaskRay I'm tagging you because I'm having trouble to get feedback to this PR, and you seem to be the most recent contributors to XRay. Would one of you be willing to review it? Any other pointers on who to get in touch with are also much appreciated. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: > > @androm3da @MaskRay I'm tagging you because I'm having trouble to get > > feedback to this PR, and you seem to be the most recent contributors to > > XRay. Would one of you be willing to review it? Any other pointers on who > > to get in touch with are also much appreciated. > > I'm happy to take a look - but I'm traveling this week and won't be able to > until this weekend. That'd be great! There is no rush. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer updated https://github.com/llvm/llvm-project/pull/90959 >From 86e252cb84803bfaa2ec096b671ef366fd3ac5cb Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 26 Oct 2023 15:13:05 +0200 Subject: [PATCH 1/2] [XRay] Add DSO support for XRay instrumentation on X86_64 --- clang/include/clang/Basic/CodeGenOptions.def | 2 + clang/include/clang/Driver/Options.td | 5 + clang/include/clang/Driver/XRayArgs.h | 4 + clang/lib/Driver/ToolChains/CommonArgs.cpp| 12 +- clang/lib/Driver/XRayArgs.cpp | 21 ++ clang/test/Driver/XRay/xray-enable-shared.cpp | 17 + .../cmake/Modules/AllSupportedArchDefs.cmake | 1 + compiler-rt/cmake/config-ix.cmake | 4 + compiler-rt/include/xray/xray_interface.h | 23 ++ compiler-rt/lib/xray/CMakeLists.txt | 86 +- compiler-rt/lib/xray/xray_dso_init.cpp| 62 compiler-rt/lib/xray/xray_init.cpp| 183 +-- compiler-rt/lib/xray/xray_interface.cpp | 292 ++ .../lib/xray/xray_interface_internal.h| 83 - compiler-rt/lib/xray/xray_trampoline_x86_64.S | 24 +- compiler-rt/lib/xray/xray_x86_64.cpp | 23 +- .../xray/TestCases/Posix/basic-mode-dso.cpp | 47 +++ .../TestCases/Posix/clang-enable-shared.cpp | 14 + .../test/xray/TestCases/Posix/dlopen.cpp | 107 +++ .../xray/TestCases/Posix/dso-dep-chains.cpp | 197 .../TestCases/Posix/patch-premain-dso.cpp | 45 +++ .../Posix/patching-unpatching-dso.cpp | 75 + 22 files changed, 1189 insertions(+), 138 deletions(-) create mode 100644 clang/test/Driver/XRay/xray-enable-shared.cpp create mode 100644 compiler-rt/lib/xray/xray_dso_init.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/basic-mode-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/clang-enable-shared.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dlopen.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dso-dep-chains.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patch-premain-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def index b600198998d85b..84006a884274f7 100644 --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -133,6 +133,8 @@ CODEGENOPT(XRayIgnoreLoops , 1, 0) ///< Emit the XRay function index section. CODEGENOPT(XRayFunctionIndex , 1, 1) +///< Set when -fxray-enable-shared is enabled +CODEGENOPT(XRayEnableShared , 1, 0) ///< Set the minimum number of instructions in a function to determine selective ///< XRay instrumentation. diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f78032255f036f..a22891e1ca6710 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2922,6 +2922,11 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +defm xray_enable_shared : BoolFOption<"xray-enable-shared", + CodeGenOpts<"XRayEnableShared">, DefaultFalse, + PosFlag, + NegFlag>; defm fine_grained_bitfield_accesses : BoolOption<"f", "fine-grained-bitfield-accesses", CodeGenOpts<"FineGrainedBitfieldAccesses">, DefaultFalse, diff --git a/clang/include/clang/Driver/XRayArgs.h b/clang/include/clang/Driver/XRayArgs.h index bdd3d979547eed..90a21e69586033 100644 --- a/clang/include/clang/Driver/XRayArgs.h +++ b/clang/include/clang/Driver/XRayArgs.h @@ -27,6 +27,7 @@ class XRayArgs { XRayInstrSet InstrumentationBundle; llvm::opt::Arg *XRayInstrument = nullptr; bool XRayRT = true; + bool XRayEnableShared = false; public: /// Parses the XRay arguments from an argument list. @@ -35,6 +36,9 @@ class XRayArgs { llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const; bool needsXRayRt() const { return XRayInstrument && XRayRT; } + bool needsXRayDSORt() const { +return XRayInstrument && XRayRT && XRayEnableShared; + } llvm::ArrayRef modeList() const { return Modes; } XRayInstrSet instrumentationBundle() const { return InstrumentationBundle; } }; diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 2ce6779f4b43e3..52a8cf90836033 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1629,10 +1629,14 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, } bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_shared)) -return false; - - if (TC.getXRayArgs().n
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -2922,6 +2922,11 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +defm xray_enable_shared : BoolFOption<"xray-enable-shared", sebastiankreutzer wrote: Good point. I adjusted the option name as suggested. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -27,6 +27,7 @@ class XRayArgs { XRayInstrSet InstrumentationBundle; llvm::opt::Arg *XRayInstrument = nullptr; bool XRayRT = true; + bool XRayEnableShared = false; sebastiankreutzer wrote: Done https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer edited https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Fix LLVM include in xray_interface.cpp (PR #111978)
https://github.com/sebastiankreutzer created https://github.com/llvm/llvm-project/pull/111978 Removes a dependency on LLVM in `xray_interface.cpp` by replacing `llvm_unreachable` with compiler-rt's `UNREACHABLE`. Applies clang-format to some unformatted changes. Original PR: #90959 >From e59e87cd218a9d8711452608f5200df813c43328 Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Fri, 11 Oct 2024 12:24:35 +0200 Subject: [PATCH] [XRay] Fix LLVM include in xray_interface.cpp --- clang/include/clang/Driver/XRayArgs.h | 4 +-- clang/lib/Driver/XRayArgs.cpp | 8 ++--- compiler-rt/include/xray/xray_interface.h | 40 +-- compiler-rt/lib/xray/xray_interface.cpp | 5 ++- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/clang/include/clang/Driver/XRayArgs.h b/clang/include/clang/Driver/XRayArgs.h index 8fbcf469e5bad1..1b5c4a4c42f12a 100644 --- a/clang/include/clang/Driver/XRayArgs.h +++ b/clang/include/clang/Driver/XRayArgs.h @@ -36,9 +36,7 @@ class XRayArgs { llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const; bool needsXRayRt() const { return XRayInstrument && XRayRT; } - bool needsXRayDSORt() const { -return XRayInstrument && XRayRT && XRayShared; - } + bool needsXRayDSORt() const { return XRayInstrument && XRayRT && XRayShared; } llvm::ArrayRef modeList() const { return Modes; } XRayInstrSet instrumentationBundle() const { return InstrumentationBundle; } }; diff --git a/clang/lib/Driver/XRayArgs.cpp b/clang/lib/Driver/XRayArgs.cpp index 411054e067cb42..d0bb5d4887c184 100644 --- a/clang/lib/Driver/XRayArgs.cpp +++ b/clang/lib/Driver/XRayArgs.cpp @@ -63,8 +63,8 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) { << XRayInstrument->getSpelling() << Triple.str(); } - if (Args.hasFlag(options::OPT_fxray_shared, - options::OPT_fno_xray_shared, false)) { + if (Args.hasFlag(options::OPT_fxray_shared, options::OPT_fno_xray_shared, + false)) { XRayShared = true; // DSO instrumentation is currently limited to x86_64 @@ -75,8 +75,8 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) { unsigned PICLvl = std::get<1>(tools::ParsePICArgs(TC, Args)); if (!PICLvl) { - D.Diag(diag::err_opt_not_valid_without_opt) - << "-fxray-shared" << "-fPIC"; + D.Diag(diag::err_opt_not_valid_without_opt) << "-fxray-shared" + << "-fPIC"; } } diff --git a/compiler-rt/include/xray/xray_interface.h b/compiler-rt/include/xray/xray_interface.h index 717cfe292ce416..675ea0cbc48c83 100644 --- a/compiler-rt/include/xray/xray_interface.h +++ b/compiler-rt/include/xray/xray_interface.h @@ -93,8 +93,8 @@ enum XRayPatchingStatus { FAILED = 3, }; -/// This tells XRay to patch the instrumentation points in all currently loaded objects. See XRayPatchingStatus -/// for possible result values. +/// This tells XRay to patch the instrumentation points in all currently loaded +/// objects. See XRayPatchingStatus for possible result values. extern XRayPatchingStatus __xray_patch(); /// This tells XRay to patch the instrumentation points in the given object. @@ -105,8 +105,8 @@ extern XRayPatchingStatus __xray_patch_object(int32_t ObjId); /// result values. extern XRayPatchingStatus __xray_unpatch(); -/// Reverses the effect of __xray_patch_object. See XRayPatchingStatus for possible -/// result values. +/// Reverses the effect of __xray_patch_object. See XRayPatchingStatus for +/// possible result values. extern XRayPatchingStatus __xray_unpatch_object(int32_t ObjId); /// This unpacks the given (packed) function id and patches @@ -114,8 +114,8 @@ extern XRayPatchingStatus __xray_unpatch_object(int32_t ObjId); /// result values. extern XRayPatchingStatus __xray_patch_function(int32_t FuncId); -/// This patches a specific function in the given object. See XRayPatchingStatus for possible -/// result values. +/// This patches a specific function in the given object. See XRayPatchingStatus +/// for possible result values. extern XRayPatchingStatus __xray_patch_function_in_object(int32_t FuncId, int32_t ObjId); @@ -129,26 +129,29 @@ extern XRayPatchingStatus __xray_unpatch_function(int32_t FuncId); extern XRayPatchingStatus __xray_unpatch_function_in_object(int32_t FuncId, int32_t ObjId); -/// This function unpacks the given (packed) function id and returns the address of the corresponding function. We return 0 if we encounter any error, even if 0 may be a valid function -/// address. +/// This function unpacks the given (packed) function id and returns the address +/// of the corresponding function. We return 0 if we encounter any error, even +/// if 0 may be a valid function address. extern uintptr_t __xray_function_addr
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: Buildbot failures are addressed in the follow-up PR #112930 https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] Reapply " [XRay] Add support for instrumentation of DSOs on x86_64 (#90959)" (PR #112930)
sebastiankreutzer wrote: @jplehr https://github.com/llvm/llvm-project/pull/112930 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
https://github.com/sebastiankreutzer updated https://github.com/llvm/llvm-project/pull/90959 >From 86e252cb84803bfaa2ec096b671ef366fd3ac5cb Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 26 Oct 2023 15:13:05 +0200 Subject: [PATCH 1/3] [XRay] Add DSO support for XRay instrumentation on X86_64 --- clang/include/clang/Basic/CodeGenOptions.def | 2 + clang/include/clang/Driver/Options.td | 5 + clang/include/clang/Driver/XRayArgs.h | 4 + clang/lib/Driver/ToolChains/CommonArgs.cpp| 12 +- clang/lib/Driver/XRayArgs.cpp | 21 ++ clang/test/Driver/XRay/xray-enable-shared.cpp | 17 + .../cmake/Modules/AllSupportedArchDefs.cmake | 1 + compiler-rt/cmake/config-ix.cmake | 4 + compiler-rt/include/xray/xray_interface.h | 23 ++ compiler-rt/lib/xray/CMakeLists.txt | 86 +- compiler-rt/lib/xray/xray_dso_init.cpp| 62 compiler-rt/lib/xray/xray_init.cpp| 183 +-- compiler-rt/lib/xray/xray_interface.cpp | 292 ++ .../lib/xray/xray_interface_internal.h| 83 - compiler-rt/lib/xray/xray_trampoline_x86_64.S | 24 +- compiler-rt/lib/xray/xray_x86_64.cpp | 23 +- .../xray/TestCases/Posix/basic-mode-dso.cpp | 47 +++ .../TestCases/Posix/clang-enable-shared.cpp | 14 + .../test/xray/TestCases/Posix/dlopen.cpp | 107 +++ .../xray/TestCases/Posix/dso-dep-chains.cpp | 197 .../TestCases/Posix/patch-premain-dso.cpp | 45 +++ .../Posix/patching-unpatching-dso.cpp | 75 + 22 files changed, 1189 insertions(+), 138 deletions(-) create mode 100644 clang/test/Driver/XRay/xray-enable-shared.cpp create mode 100644 compiler-rt/lib/xray/xray_dso_init.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/basic-mode-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/clang-enable-shared.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dlopen.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/dso-dep-chains.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patch-premain-dso.cpp create mode 100644 compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def index b600198998d85b..84006a884274f7 100644 --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -133,6 +133,8 @@ CODEGENOPT(XRayIgnoreLoops , 1, 0) ///< Emit the XRay function index section. CODEGENOPT(XRayFunctionIndex , 1, 1) +///< Set when -fxray-enable-shared is enabled +CODEGENOPT(XRayEnableShared , 1, 0) ///< Set the minimum number of instructions in a function to determine selective ///< XRay instrumentation. diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f78032255f036f..a22891e1ca6710 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2922,6 +2922,11 @@ def fxray_selected_function_group : HelpText<"When using -fxray-function-groups, select which group of functions to instrument. Valid range is 0 to fxray-function-groups - 1">, MarshallingInfoInt, "0">; +defm xray_enable_shared : BoolFOption<"xray-enable-shared", + CodeGenOpts<"XRayEnableShared">, DefaultFalse, + PosFlag, + NegFlag>; defm fine_grained_bitfield_accesses : BoolOption<"f", "fine-grained-bitfield-accesses", CodeGenOpts<"FineGrainedBitfieldAccesses">, DefaultFalse, diff --git a/clang/include/clang/Driver/XRayArgs.h b/clang/include/clang/Driver/XRayArgs.h index bdd3d979547eed..90a21e69586033 100644 --- a/clang/include/clang/Driver/XRayArgs.h +++ b/clang/include/clang/Driver/XRayArgs.h @@ -27,6 +27,7 @@ class XRayArgs { XRayInstrSet InstrumentationBundle; llvm::opt::Arg *XRayInstrument = nullptr; bool XRayRT = true; + bool XRayEnableShared = false; public: /// Parses the XRay arguments from an argument list. @@ -35,6 +36,9 @@ class XRayArgs { llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const; bool needsXRayRt() const { return XRayInstrument && XRayRT; } + bool needsXRayDSORt() const { +return XRayInstrument && XRayRT && XRayEnableShared; + } llvm::ArrayRef modeList() const { return Modes; } XRayInstrSet instrumentationBundle() const { return InstrumentationBundle; } }; diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 2ce6779f4b43e3..52a8cf90836033 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1629,10 +1629,14 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, } bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_shared)) -return false; - - if (TC.getXRayArgs().n
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
@@ -97,27 +97,50 @@ enum XRayPatchingStatus { /// for possible result values. extern XRayPatchingStatus __xray_patch(); +extern XRayPatchingStatus __xray_patch_object(int32_t ObjId); + /// Reverses the effect of __xray_patch(). See XRayPatchingStatus for possible /// result values. extern XRayPatchingStatus __xray_unpatch(); +extern XRayPatchingStatus __xray_unpatch_object(int32_t ObjId); + /// This patches a specific function id. See XRayPatchingStatus for possible /// result values. extern XRayPatchingStatus __xray_patch_function(int32_t FuncId); +extern XRayPatchingStatus __xray_patch_function_in_object(int32_t FuncId, + int32_t ObjId); + /// This unpatches a specific function id. See XRayPatchingStatus for possible /// result values. extern XRayPatchingStatus __xray_unpatch_function(int32_t FuncId); +extern XRayPatchingStatus __xray_unpatch_function_in_object(int32_t FuncId, +int32_t ObjId); + /// This function returns the address of the function provided a valid function /// id. We return 0 if we encounter any error, even if 0 may be a valid function /// address. extern uintptr_t __xray_function_address(int32_t FuncId); +extern uintptr_t __xray_function_address_in_object(int32_t FuncId, + int32_t ObjId); + /// This function returns the maximum valid function id. Returns 0 if we /// encounter errors (when there are no instrumented functions, etc.). extern size_t __xray_max_function_id(); +extern size_t __xray_max_function_id_in_object(int32_t ObjId); + +extern size_t __xray_num_objects(); + +extern int32_t __xray_unpack_function_id(int32_t PackedId); + +extern int32_t __xray_unpack_object_id(int32_t PackedId); + +extern int32_t __xray_pack_id(int32_t FuncId, int32_t ObjId); sebastiankreutzer wrote: Thanks for the feedback! I added the missing documentation. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: Buildbot failures are due to including an LLVM header in the XRay runtime. Will fix ASAP. https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Remove reliance on default PIC behavior in DSO tests (PR #113892)
sebastiankreutzer wrote: @rupprecht If this looks good to you, could you commit it? I don't have write access. https://github.com/llvm/llvm-project/pull/113892 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] Reapply " [XRay] Add support for instrumentation of DSOs on x86_64 (#90959)" (PR #113548)
sebastiankreutzer wrote: Tagging reviewers of previous PR: @jplehr @MaskRay @androm3da @deanberris https://github.com/llvm/llvm-project/pull/113548 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] Reapply " [XRay] Add support for instrumentation of DSOs on x86_64 (#90959)" (PR #113548)
sebastiankreutzer wrote: Buildkite failure is due to unrelated errors in `libcxx/test/libcxx/headers_in_modulemap.sh.py` https://github.com/llvm/llvm-project/pull/113548 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Remove reliance on default PIC behavior in DSO tests (PR #113892)
https://github.com/sebastiankreutzer edited https://github.com/llvm/llvm-project/pull/113892 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] Reapply " [XRay] Add support for instrumentation of DSOs on x86_64 (#90959)" (PR #113548)
@@ -0,0 +1,17 @@ +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fpic -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s sebastiankreutzer wrote: Hi, thanks for the comment. I agree with you that this check should be removed. The issue was pointed out to me by another person and I prepared a patch here: #113892 (forgot to link it here before). https://github.com/llvm/llvm-project/pull/113548 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay][AArch64] Support -fxray-shared (PR #114431)
https://github.com/sebastiankreutzer updated https://github.com/llvm/llvm-project/pull/114431 >From 63760cffc53815f3c8398bd3351486b658d0a1a8 Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 31 Oct 2024 16:01:29 +0100 Subject: [PATCH 1/2] [XRay][AArch64] Support -fxray-shared --- clang/lib/Driver/XRayArgs.cpp | 8 +++-- clang/test/Driver/XRay/xray-shared.cpp| 16 +++--- .../cmake/Modules/AllSupportedArchDefs.cmake | 2 +- compiler-rt/lib/xray/CMakeLists.txt | 4 +++ .../lib/xray/xray_trampoline_AArch64.S| 32 +++ .../xray/TestCases/Posix/basic-mode-dso.cpp | 3 +- .../TestCases/Posix/clang-xray-shared.cpp | 2 +- .../test/xray/TestCases/Posix/dlopen.cpp | 2 +- .../xray/TestCases/Posix/dso-dep-chains.cpp | 2 +- .../TestCases/Posix/patch-premain-dso.cpp | 2 +- .../Posix/patching-unpatching-dso.cpp | 2 +- 11 files changed, 47 insertions(+), 28 deletions(-) diff --git a/clang/lib/Driver/XRayArgs.cpp b/clang/lib/Driver/XRayArgs.cpp index d0bb5d4887c184..18bd8640f01308 100644 --- a/clang/lib/Driver/XRayArgs.cpp +++ b/clang/lib/Driver/XRayArgs.cpp @@ -67,8 +67,12 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) { false)) { XRayShared = true; -// DSO instrumentation is currently limited to x86_64 -if (Triple.getArch() != llvm::Triple::x86_64) { +// DSO instrumentation is currently limited to x86_64 and aarch64 +switch (Triple.getArch()) { +case llvm::Triple::x86_64: +case llvm::Triple::aarch64: + break; +default: D.Diag(diag::err_drv_unsupported_opt_for_target) << "-fxray-shared" << Triple.str(); } diff --git a/clang/test/Driver/XRay/xray-shared.cpp b/clang/test/Driver/XRay/xray-shared.cpp index e331fefed1e0c9..820c5b363d2c62 100644 --- a/clang/test/Driver/XRay/xray-shared.cpp +++ b/clang/test/Driver/XRay/xray-shared.cpp @@ -1,15 +1,21 @@ +// Check supported targets // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s +// RUN: %clang -### --target=aarch64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s + +// Check unsupported targets +// RUN: not %clang -### --target=arm-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=mips-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=loongarch64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=hexagon-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=powerpc64le-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET + +// Check PIC requirement // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fpic -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s // RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fno-PIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-PIC // RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fno-pic -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-PIC - // On 64 bit darwin, PIC is always enabled // RUN: %clang -### --target=x86_64-apple-darwin -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s -// Check unsupported targets -// RUN: not %clang -### --target=aarch64-pc-freebsd -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET -// RUN: not %clang -### --target=arm64-apple-macos -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET - // CHECK: "-cc1" {{.*}}"-fxray-instrument" {{.*}}"-fxray-shared" // ERR-TARGET: error: unsupported option '-fxray-shared' for target // ERR-PIC: error: option '-fxray-shared' cannot be specified without '-fPIC' diff --git a/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake b/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake index fb4dfa7bd09dfe..b29ae179c2b4f4 100644 --- a/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake +++ b/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake @@ -104,7 +104,7 @@ else() set(ALL_XRAY_SUPPORTED_ARCH ${X86_64} ${ARM32} ${ARM64} ${MIPS32} ${MIPS64} powerpc64le ${HEXAGON} ${LOONGARCH64}) endif() -set(ALL_XRAY_DSO_SUPPORTED_ARCH ${X86_64}) +set(ALL_XRAY_DSO_SUPPORTED_ARCH ${X86_64} ${ARM64}) set(ALL_SHADOWCALLSTACK_SUPPORTED_ARCH ${ARM64}) if (UNIX) diff --git a/compiler-rt/lib/xray
[clang] [compiler-rt] [XRay][AArch64] Support -fxray-shared (PR #114431)
sebastiankreutzer wrote: @felipepiovezan @juliannagele Sorry for the delayed response. This is quite strange, since the issues seem to be on the x86 side... https://github.com/llvm/llvm-project/pull/114431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay][AArch64] Support -fxray-shared (PR #114431)
sebastiankreutzer wrote: @MaskRay Thanks! Would you mind pushing the commit? I don't have write access. https://github.com/llvm/llvm-project/pull/114431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Remove reliance on default PIC behavior in DSO tests (PR #113892)
https://github.com/sebastiankreutzer created https://github.com/llvm/llvm-project/pull/113892 Compiling with `-fxray-shared` requires position-independent code. Some tests do not explicitly specify this, thus falling back to the compiler default. If, for example, Clang is compiled with `-DCLANG_DEFAULT_PIE_ON_LINUX=OFF`, these checks fail. This patch addresses this issue in two tests: - Removing a check in `xray-shared.cpp` that only tests default PIC behavior - Adding `-fPIC` explicitly in `clang-xray-shared.cpp` >From c7e2d0a4a92d2676c1d79efadae5d800fc8fc5b1 Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Mon, 28 Oct 2024 10:50:47 +0100 Subject: [PATCH] [XRay] Remove reliance on default PIC behavior in DSO tests --- clang/test/Driver/XRay/xray-shared.cpp | 1 - compiler-rt/test/xray/TestCases/Posix/clang-xray-shared.cpp | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/test/Driver/XRay/xray-shared.cpp b/clang/test/Driver/XRay/xray-shared.cpp index 215854e1fc7cef..e331fefed1e0c9 100644 --- a/clang/test/Driver/XRay/xray-shared.cpp +++ b/clang/test/Driver/XRay/xray-shared.cpp @@ -1,6 +1,5 @@ // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fpic -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s -// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s // RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fno-PIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-PIC // RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fno-pic -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-PIC diff --git a/compiler-rt/test/xray/TestCases/Posix/clang-xray-shared.cpp b/compiler-rt/test/xray/TestCases/Posix/clang-xray-shared.cpp index 92f3c29e970d42..0dd721571de9b8 100644 --- a/compiler-rt/test/xray/TestCases/Posix/clang-xray-shared.cpp +++ b/compiler-rt/test/xray/TestCases/Posix/clang-xray-shared.cpp @@ -1,6 +1,6 @@ // Test that the DSO-local runtime library has been linked if -fxray-shared is passed. // -// RUN: %clangxx -fxray-instrument -fxray-shared %s -shared -o %t.so +// RUN: %clangxx -fxray-instrument -fxray-shared -fPIC %s -shared -o %t.so // RUN: llvm-nm %t.so | FileCheck %s --check-prefix ENABLED // RUN: %clangxx -fxray-instrument %s -shared -o %t.so ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Remove reliance on default PIC behavior in DSO tests (PR #113892)
sebastiankreutzer wrote: @MatzeB https://github.com/llvm/llvm-project/pull/113892 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay][AArch64] Support -fxray-shared (PR #114431)
https://github.com/sebastiankreutzer created https://github.com/llvm/llvm-project/pull/114431 This patch adds support for `-fxray-shared` in AArch64. This feature, introduced in #113548 for x86_64, enables the instrumentation of shared libraries with XRay. Changes: - Adds AArch64 to the list of targets supporting `-fxray-shared` - Introduces PIC versions of the AArch64 XRay trampolines - Adjusts relevant XRay tests >From 63760cffc53815f3c8398bd3351486b658d0a1a8 Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 31 Oct 2024 16:01:29 +0100 Subject: [PATCH] [XRay][AArch64] Support -fxray-shared --- clang/lib/Driver/XRayArgs.cpp | 8 +++-- clang/test/Driver/XRay/xray-shared.cpp| 16 +++--- .../cmake/Modules/AllSupportedArchDefs.cmake | 2 +- compiler-rt/lib/xray/CMakeLists.txt | 4 +++ .../lib/xray/xray_trampoline_AArch64.S| 32 +++ .../xray/TestCases/Posix/basic-mode-dso.cpp | 3 +- .../TestCases/Posix/clang-xray-shared.cpp | 2 +- .../test/xray/TestCases/Posix/dlopen.cpp | 2 +- .../xray/TestCases/Posix/dso-dep-chains.cpp | 2 +- .../TestCases/Posix/patch-premain-dso.cpp | 2 +- .../Posix/patching-unpatching-dso.cpp | 2 +- 11 files changed, 47 insertions(+), 28 deletions(-) diff --git a/clang/lib/Driver/XRayArgs.cpp b/clang/lib/Driver/XRayArgs.cpp index d0bb5d4887c184..18bd8640f01308 100644 --- a/clang/lib/Driver/XRayArgs.cpp +++ b/clang/lib/Driver/XRayArgs.cpp @@ -67,8 +67,12 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) { false)) { XRayShared = true; -// DSO instrumentation is currently limited to x86_64 -if (Triple.getArch() != llvm::Triple::x86_64) { +// DSO instrumentation is currently limited to x86_64 and aarch64 +switch (Triple.getArch()) { +case llvm::Triple::x86_64: +case llvm::Triple::aarch64: + break; +default: D.Diag(diag::err_drv_unsupported_opt_for_target) << "-fxray-shared" << Triple.str(); } diff --git a/clang/test/Driver/XRay/xray-shared.cpp b/clang/test/Driver/XRay/xray-shared.cpp index e331fefed1e0c9..820c5b363d2c62 100644 --- a/clang/test/Driver/XRay/xray-shared.cpp +++ b/clang/test/Driver/XRay/xray-shared.cpp @@ -1,15 +1,21 @@ +// Check supported targets // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s +// RUN: %clang -### --target=aarch64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s + +// Check unsupported targets +// RUN: not %clang -### --target=arm-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=mips-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=loongarch64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=hexagon-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=powerpc64le-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET + +// Check PIC requirement // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fpic -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s // RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fno-PIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-PIC // RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fno-pic -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-PIC - // On 64 bit darwin, PIC is always enabled // RUN: %clang -### --target=x86_64-apple-darwin -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s -// Check unsupported targets -// RUN: not %clang -### --target=aarch64-pc-freebsd -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET -// RUN: not %clang -### --target=arm64-apple-macos -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET - // CHECK: "-cc1" {{.*}}"-fxray-instrument" {{.*}}"-fxray-shared" // ERR-TARGET: error: unsupported option '-fxray-shared' for target // ERR-PIC: error: option '-fxray-shared' cannot be specified without '-fPIC' diff --git a/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake b/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake index fb4dfa7bd09dfe..b29ae179c2b4f4 100644 --- a/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake +++ b/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake @@ -104,7 +104,7 @@ else() set(AL
[clang] [compiler-rt] [XRay][AArch64] Support -fxray-shared (PR #114431)
sebastiankreutzer wrote: Tagging potential reviewers: @jplehr @MaskRay @petrhosek https://github.com/llvm/llvm-project/pull/114431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay][AArch64] Support -fxray-shared (PR #114431)
https://github.com/sebastiankreutzer edited https://github.com/llvm/llvm-project/pull/114431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] [XRay] Add support for instrumentation of DSOs on x86_64 (PR #90959)
sebastiankreutzer wrote: Any further comments, @petrhosek? https://github.com/llvm/llvm-project/pull/90959 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [compiler-rt] Reapply "[XRay][AArch64] Support -fxray-shared (#114431)" (PR #115300)
https://github.com/sebastiankreutzer created https://github.com/llvm/llvm-project/pull/115300 This patch implements support for `-fxray-shared` on AArch64 and fixes a remaining issue in the previous PR #114431. A bug in the XRay `CMakeLists.txt` caused the XRay assembly sources to be built for every architecture in `XRAY_DSO_SUPPORTED_ARCH` on Apple. This led to the compiler trying to compile AArch64 assembly for X86 targets and vice versa. This is addressed here by ensuring that assembly sources are only built for the matching architecture (see fixup commit). **Original PR description:** This patch adds support for `-fxray-shared` on AArch64. This feature, introduced in #113548 for x86_64, enables the instrumentation of shared libraries with XRay. Changes: - Adds AArch64 to the list of targets supporting `-fxray-shared` - Introduces PIC versions of the AArch64 XRay trampolines - Adjusts relevant XRay tests >From 77993065f4870074dfa766a4ee38c247e4f800f4 Mon Sep 17 00:00:00 2001 From: Sebastian Kreutzer Date: Thu, 31 Oct 2024 16:01:29 +0100 Subject: [PATCH 1/2] [XRay][AArch64] Support -fxray-shared --- clang/lib/Driver/XRayArgs.cpp | 8 +++-- clang/test/Driver/XRay/xray-shared.cpp| 16 +++--- .../cmake/Modules/AllSupportedArchDefs.cmake | 2 +- compiler-rt/lib/xray/CMakeLists.txt | 5 ++- .../lib/xray/xray_trampoline_AArch64.S| 32 +++ .../xray/TestCases/Posix/basic-mode-dso.cpp | 3 +- .../TestCases/Posix/clang-xray-shared.cpp | 2 +- .../test/xray/TestCases/Posix/dlopen.cpp | 2 +- .../xray/TestCases/Posix/dso-dep-chains.cpp | 2 +- .../TestCases/Posix/patch-premain-dso.cpp | 2 +- .../Posix/patching-unpatching-dso.cpp | 2 +- 11 files changed, 47 insertions(+), 29 deletions(-) diff --git a/clang/lib/Driver/XRayArgs.cpp b/clang/lib/Driver/XRayArgs.cpp index 1cf31d10530a59..adb61544c0f924 100644 --- a/clang/lib/Driver/XRayArgs.cpp +++ b/clang/lib/Driver/XRayArgs.cpp @@ -68,8 +68,12 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) { false)) { XRayShared = true; -// DSO instrumentation is currently limited to x86_64 -if (Triple.getArch() != llvm::Triple::x86_64) { +// Certain targets support DSO instrumentation +switch (Triple.getArch()) { +case llvm::Triple::aarch64: +case llvm::Triple::x86_64: + break; +default: D.Diag(diag::err_drv_unsupported_opt_for_target) << "-fxray-shared" << Triple.str(); } diff --git a/clang/test/Driver/XRay/xray-shared.cpp b/clang/test/Driver/XRay/xray-shared.cpp index e331fefed1e0c9..820c5b363d2c62 100644 --- a/clang/test/Driver/XRay/xray-shared.cpp +++ b/clang/test/Driver/XRay/xray-shared.cpp @@ -1,15 +1,21 @@ +// Check supported targets // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s +// RUN: %clang -### --target=aarch64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s + +// Check unsupported targets +// RUN: not %clang -### --target=arm-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=mips-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=loongarch64-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=hexagon-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET +// RUN: not %clang -### --target=powerpc64le-unknown-linux-gnu -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET + +// Check PIC requirement // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fpic -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s // RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fno-PIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-PIC // RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fno-pic -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-PIC - // On 64 bit darwin, PIC is always enabled // RUN: %clang -### --target=x86_64-apple-darwin -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s -// Check unsupported targets -// RUN: not %clang -### --target=aarch64-pc-freebsd -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET -// RUN: not %clang -### --target=arm64-apple-macos -fPIC -fxray-instrument -fxray-shared -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR-TARGET - // CHECK: "-cc1" {{.*}}"-fxray-instrument
[clang] [compiler-rt] Reapply "[XRay][AArch64] Support -fxray-shared (#114431)" (PR #115300)
sebastiankreutzer wrote: @felipepiovezan @juliannagele @MaskRay Could one of you merge this, if it looks good to you? Buildkite seems to be broken at the moment, but it works locally for me (cross-compiled with Apple toolchain). https://github.com/llvm/llvm-project/pull/115300 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits