8000 Hunk cherry-pick from 8a2198773e: fix InterceptorEnabled visibility. by MorthimerMcMare · Pull Request #50 · focs-lab/llvm-project · GitHub
[go: up one dir, main page]

Skip to content

Hunk cherry-pick from 8a2198773e: fix InterceptorEnabled visibility. #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
f7fe4ca
Migration to getUnderlyingMayEscObjectsNew
apaznikov Nov 8, 2024
2f95b0b
Added inttoptr/ptrtoint tests
apaznikov Nov 11, 2024
348eeb0
Minor refactoring (mostly compOutEscapeState)
apaznikov Nov 12, 2024
1945512
Added GlobalEscapeAnalysis pass infrastructure
apaznikov Nov 13, 2024
9ac70ee
Updated escape/no-escape with passing to calls or aliasing to ptr arg…
apaznikov Nov 15, 2024
4c86254
Starting work on propagation of argument escape info: add argument to…
apaznikov Nov 26, 2024
cefcd9e
Finished IPA EA, refactoring, tests
apaznikov Nov 27, 2024
020af62
Added recursive call conservative algorithm
apaznikov Nov 28, 2024
b155fe5
Fix std::optional<std::reference_wrapper<ArgumentEscapesMap>> ArgsEsc…
apaznikov Nov 29, 2024
bc3186e
Added recursive call conservative algorithm
apaznikov Nov 28, 2024
fbbe19e
Fix std::optional<std::reference_wrapper<ArgumentEscapesMap>> ArgsEsc…
apaznikov Nov 29, 2024
b342f09
Efforts on TSan and IPA EA integration
apaznikov Nov 29, 2024
178fd58
Add Global EA usage to TSan
apaznikov Dec 3, 2024
d0c40d7
Finished support of IPA EA & TSan, fixed bugs
apaznikov Dec 3, 2024
f862a25
Add support of function returning escaped value -- draft
apaznikov Dec 4, 2024
56855b2
Minor fixes for argument escape logic (DBG)
apaznikov Dec 5, 2024
2efb371
Add support of function returning escaped value -- draft 2
apaznikov Dec 5, 2024
2aa25f7
Add EscapeReasonTy
apaznikov Dec 5, 2024
6fbd3bd
Fix addEscapingObject logic for EscReason support
apaznikov Dec 5, 2024
dbc01a3
Fix logic with EscReasons, isEscapeForFunc, etc
apaznikov Dec 6, 2024
24ded0c
Fixed merging escape states, fixed printing, minor fixes, add tests w…
apaznikov Dec 9, 2024
de71362
Support of SCC in IPA - in progress
apaznikov Dec 9, 2024
a03d550
Many fixes for recursive functions, argument, aliasing processing and…
apaznikov Dec 11, 2024
460e60e
Refactoring and debug
apaznikov Dec 12, 2024
ce24c31
Fixed logic mainly with EscapeReason/Argument checks. Add support for…
apaznikov Dec 13, 2024
142270d
Merge branch 'tsan-with-ea-IPA-DBG-rec-calls-TMP' into tsan-with-ea-IPA
apaznikov Dec 13, 2024
781082f
Refactor to simplify and unify logic
apaznikov Dec 13, 2024
2772c1e
Refactor EscapeAnalysis API to improve readability and maintainability
apaznikov Dec 15, 2024
7962dcd
Add support of calls returning pointers (should lead escape)
apaznikov Dec 16, 2024
ed91d09
Use CaptureTracker first, and then EscapeAnalysis
apaznikov Dec 17, 2024
83ca510
Added EscapeReason statistics to TSan. Refactor escape analysis to tr…
apaznikov Dec 18, 2024
01b846c
Add support IPA return info
apaznikov Dec 19, 2024
3f11e06
Try traverse -- DEBUG
apaznikov Dec 19, 2024
87eef69
Argument escape tracking - DEBUG.
apaznikov Dec 20, 2024
408ec0d
Add top-down argument escape analysis (DEBUG)
apaznikov Dec 23, 2024
a69e19e
Add support for detecting functions passed to ObjC selectors
apaznikov Dec 26, 2024
3c7af95
Remove debug logs and enhance escape analysis logic, add test for top…
apaznikov Dec 26, 2024
1d44daa
Fix incorrect assertion in escape analysis.
apaznikov Dec 29, 2024
675f201
Efforts for new pointer escape logic: add UnderlObjInfo, updates in a…
apaznikov Jan 3, 2025
d2329e4
Refactor logic for treating escaping pointers: now pointers (if they …
apaznikov Jan 10, 2025
8eaf60e
Make isEscapedForBBInFuncTSan more optimistic
apaznikov Jan 10, 2025
e6ff26f
Use IPA version `isEscapedForBBIPA` in isEscapedForBBInFuncTSan
apaznikov Jan 10, 2025
b3a7a9a
Fixed IPA escape analysis logic
apaznikov Jan 13, 2025
d9a48f9
Added support of SCCs for TopDownIPA, fixed evalTopDownArgEscStatus (…
apaznikov Jan 14, 2025
006c2d3
Large update for memcpy/memset/memmove
apaznikov Jan 17, 2025
2690475
Enable interceptor toggling via `InterceptorEnabled` global variable …
apaznikov Jan 27, 2025
c946299
Introduce global InterceptorEnabled for interceptor control
apaznikov Feb 4, 2025
f7cfec5
Add intercepted call handling (strlen, strcmp, memchr)
apaznikov Feb 5, 2025
22a26b0
Fixed bug and cleanup debug output
apaznikov Feb 5, 2025
d76f5b0
Enable/disable in sanitizer_common_interceptors.inc
apaznikov Feb 6, 2025
ef66b29
Add __tsan_enable/__tsan_disalbe intrinsics
apaznikov Feb 24, 2025
16216da
Minor fixes
apaznikov Mar 24, 2025
10d6ed8
hunk cherry-pick from 8a2198773e: fix InterceptorEnabled visibility.
MorthimerMcMare Apr 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Introduce global InterceptorEnabled for interceptor control
This adds a globally accessible `InterceptorEnabled` variable to control interceptor behavior dynamically. The changes ensure interceptors can be selectively disabled during specific instructions, with proper restoration of the state afterward. This improves flexibility in handling memory intrinsics and thread sanitization instrumentation.
  • Loading branch information
apaznikov committed Feb 4, 2025
commit c94629915ebd75b3a6e7063bcfa01329b494b717
2 changes: 2 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "sanitizer_libc.h"
#include "sanitizer_placement_new.h"

extern "C" bool InterceptorEnabled = true;

namespace __sanitizer {

const char *SanitizerToolName = "SanitizerTool";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "interception/interception.h"
#include "sanitizer_platform_interceptors.h"

extern "C" bool InterceptorEnabled;

// Platform-specific options.
#if SANITIZER_APPLE
#define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE 0
Expand All @@ -40,13 +42,10 @@
#define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE 1
#endif // SANITIZER_APPLE

bool InterceptorEnabled = 1;

#ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
#define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
{ \
if (InterceptorEnabled) { \
Printf("INTERCEPTOR_ENABLED\n"); \
if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
return internal_memset(dst, v, size); \
COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size); \
Expand All @@ -58,31 +57,35 @@ bool InterceptorEnabled = 1;
#endif

#ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
#define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
{ \
if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
return internal_memmove(dst, src, size); \
COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size); \
if (common_flags()->intercept_intrin) { \
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
} \
return REAL(memmove)(dst, src, size); \
#define COMMON_ 8000 INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
{ \
if (InterceptorEnabled) { \
if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
return internal_memmove(dst, src, size); \
COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size); \
if (common_flags()->intercept_intrin) { \
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
} \
} \
return REAL(memmove)(dst, src, size); \
}
#endif

#ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
#define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
{ \
if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) { \
return internal_memmove(dst, src, size); \
} \
COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size); \
if (common_flags()->intercept_intrin) { \
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
} \
return REAL(memcpy)(dst, src, size); \
#define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
{ \
if (InterceptorEnabled) { \
if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) { \
return internal_memmove(dst, src, size); \
} \
COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size); \
if (common_flags()->intercept_intrin) { \
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
} \
} \
return REAL(memcpy)(dst, src, size); \
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/tsan/rtl/tsan_rtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ bool OutputReport(ThreadState *thr, const ScopedReport &srep);
bool IsFiredSuppression(Context *ctx, ReportType type, StackTrace trace);
bool IsExpectedReport(uptr addr, uptr size);

#define TSAN_DEBUG_OUTPUT 2
//#define TSAN_DEBUG_OUTPUT 2

#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
# define DPrintf Printf
Expand Down
35 changes: 23 additions & 12 deletions llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ const char kTsanInitName[] = "__tsan_init";

namespace {

GlobalVariable *InterceptorEnabled;

/// ThreadSanitizer: instrument the code in module to find races.
///
/// Instantiating ThreadSanitizer inserts the tsan runtime library API function
Expand Down Expand Up @@ -167,6 +169,7 @@ struct ThreadSanitizer {
void initialize(Module &M, const TargetLibraryInfo &TLI);
bool instrumentLoadOrStore(const InstructionInfo &II, const DataLayout &DL);
bool instrumentAtomic(Instruction *I, const DataLayout &DL);
void disableInterceptorForInstr(Instruction *I, InstrumentationIRBuilder &IRB);
bool
instrumentMemIntrinsic(Instruction *I,
std::optional<EscapeAnalysisGlobalInfo *> EAIGlobal);
Expand Down Expand Up @@ -206,8 +209,6 @@ struct ThreadSanitizer {
FunctionCallee TsanVptrUpdate;
FunctionCallee TsanVptrLoad;
FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;

GlobalVariable *InterceptorEnabled;
};

void insertModuleCtor(Module &M) {
Expand Down Expand Up @@ -256,6 +257,12 @@ PreservedAnalyses ModuleThreadSanitizerPass::run(Module &M,
MAM.getResult<EscapeAnalysisGlobal>(M);

insertModuleCtor(M);

// Declare an external global variable InterceptorEnabled in the module
InterceptorEnabled = new GlobalVariable(
M, Type::getInt1Ty(M.getContext()), /*isConstant=*/false,
GlobalValue::ExternalLinkage, nullptr, "InterceptorEnabled");

return PreservedAnalyses::none();
}

Expand Down Expand Up @@ -405,12 +412,6 @@ void ThreadSanitizer::initialize(Module &M, const TargetLibraryInfo &TLI) {
"__tsan_memset",
TLI.getAttrList(&Ctx, {1}, /*Signed=*/true, /*Ret=*/false, Attr),
IRB.getPtrTy(), IRB.getPtrTy(), IRB.getInt32Ty(), IntptrTy);

// Declare a global variable InterceptorEnabled in the LLVM module
InterceptorEnabled =
new GlobalVariable(M, Type::getInt1Ty(Ctx), /*isConstant=*/false,
GlobalValue::ExternalLinkage,
ConstantInt::getTrue(Ctx), "InterceptorEnabled");
}

static bool isVtableAccess(Instruction *I) {
Expand Down Expand Up @@ -809,9 +810,15 @@ static ConstantInt *createOrdering(IRBuilder<> *IRB, AtomicOrdering ord) {
return IRB->getInt32(v);
}

/**
*
*/
void ThreadSanitizer::disableInterceptorForInstr(
Instruction *I, InstrumentationIRBuilder &IRB) {
// Disable interceptors for this call
IRB.CreateStore(IRB.getInt1(false), InterceptorEnabled);
// After MemsetFn, set the InterceptorEnabled back to true
IRB.SetInsertPoint(++BasicBlock::iterator(I));
IRB.CreateStore(IRB.getInt1(true), InterceptorEnabled);
}

// So, we either need to ensure the intrinsic is not inlined, or instrument it.
// We do not instrument memset/memmove/memcpy intrinsics (too complicated),
// instead we simply replace them with regular function calls, which are then
Expand Down Expand Up @@ -839,6 +846,7 @@ bool ThreadSanitizer::instrumentMemIntrinsic(Instruction *I,

// Check if pointer is not escape
if (!isPointerEscaped(M->getArgOperand(0))) {
disableInterceptorForInstr(I, IRB);
return false;
}

Expand All @@ -850,8 +858,11 @@ bool ThreadSanitizer::instrumentMemIntrinsic(Instruction *I,
I->eraseFromParent();
} else if (MemTransferInst *M = dyn_cast<MemTransferInst>(I)) {
// Check if pointers are not escape
if (!isPointerEscaped(M->getArgOperand(0)) && !isPointerEscaped(M->getArgOperand(1)))
if (!isPointerEscaped(M->getArgOperand(0)) &&
!isPointerEscaped(M->getArgOperand(1))) {
disableInterceptorForInstr(I, IRB);
return false;
}

IRB.CreateCall(
isa<MemCpyInst>(M) ? MemcpyFn : MemmoveFn,
Expand Down
0