Skip to content

Commit f01ff23

Browse files
committed
[SanCov] Fix sancov global symbol collision due to cap_merged_table in CHERI
When multiple private sancov global variables across object files share the same name, even though they won't collide when they are linked together, their corresponding __cap_merged_table will. This commit appends a unique module identifier to the sancov global variable names to prevent collision. In the event that the unique module identifier obtained through getUniqueModuleId() is empty, we use the clang PID and time.
1 parent c2df8b7 commit f01ff23

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
1616
#include "llvm/ADT/ArrayRef.h"
1717
#include "llvm/ADT/SmallVector.h"
18+
#include "llvm/ADT/StringExtras.h"
1819
#include "llvm/Analysis/GlobalsModRef.h"
1920
#include "llvm/Analysis/PostDominators.h"
2021
#include "llvm/IR/Constant.h"
@@ -30,6 +31,7 @@
3031
#include "llvm/IR/Module.h"
3132
#include "llvm/IR/Type.h"
3233
#include "llvm/Support/CommandLine.h"
34+
#include "llvm/Support/Process.h"
3335
#include "llvm/Support/SpecialCaseList.h"
3436
#include "llvm/Support/VirtualFileSystem.h"
3537
#include "llvm/TargetParser/Triple.h"
@@ -390,6 +392,10 @@ bool ModuleSanitizerCoverage::instrumentModule(
390392
DL = &M.getDataLayout();
391393
CurModule = &M;
392394
CurModuleUniqueId = getUniqueModuleId(CurModule);
395+
if (CurModuleUniqueId.empty()) {
396+
CurModuleUniqueId = "clangPidTime_" + llvm::itostr(sys::Process::getProcessId()) +
397+
"_" + llvm::itostr(time(nullptr));
398+
}
393399
TargetTriple = Triple(M.getTargetTriple());
394400
FunctionGuardArray = nullptr;
395401
Function8bitCounterArray = nullptr;
@@ -724,7 +730,7 @@ GlobalVariable *ModuleSanitizerCoverage::CreateFunctionLocalArrayInSection(
724730
ArrayType *ArrayTy = ArrayType::get(Ty, NumElements);
725731
auto Array = new GlobalVariable(
726732
*CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
727-
Constant::getNullValue(ArrayTy), "__sancov_gen_");
733+
Constant::getNullValue(ArrayTy), Twine("__sancov_gen_") + Twine(CurModuleUniqueId));
728734

729735
if (TargetTriple.supportsCOMDAT() &&
730736
(TargetTriple.isOSBinFormatELF() || !F.isInterposable()))
@@ -862,7 +868,7 @@ void ModuleSanitizerCoverage::InjectTraceForSwitch(
862868
GlobalVariable *GV = new GlobalVariable(
863869
*CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage,
864870
ConstantArray::get(ArrayOfInt64Ty, Initializers),
865-
"__sancov_gen_cov_switch_values");
871+
Twine("__sancov_gen_cov_switch_values_") + Twine(CurModuleUniqueId));
866872
IRB.CreateCall(SanCovTraceSwitchFunction,
867873
{Cond, IRB.CreatePointerCast(GV, GlobalsInt64PtrTy)});
868874
}

0 commit comments

Comments
 (0)