Skip to content

Commit 725a4dd

Browse files
[MSan] factor userspace-specific declarations into createUserspaceApi(). NFC
This patch introduces createUserspaceApi() that creates function/global declarations for symbols used by MSan in the userspace. This is a step towards the upcoming KMSAN implementation patch. Reviewed at https://reviews.llvm.org/D49292 llvm-svn: 337155
1 parent 72da47d commit 725a4dd

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ class MemorySanitizer : public FunctionPass {
422422
friend struct VarArgPowerPC64Helper;
423423

424424
void initializeCallbacks(Module &M);
425+
void createUserspaceApi(Module &M);
425426

426427
/// Track origins (allocation points) of uninitialized values.
427428
int TrackOrigins;
@@ -455,8 +456,11 @@ class MemorySanitizer : public FunctionPass {
455456
/// function.
456457
GlobalVariable *OriginTLS;
457458

459+
/// Are the instrumentation callbacks set up?
460+
bool CallbacksInitialized = false;
461+
458462
/// The run-time callback to print a warning.
459-
Value *WarningFn = nullptr;
463+
Value *WarningFn;
460464

461465
// These arrays are indexed by log2(AccessSize).
462466
Value *MaybeWarningFn[kNumberOfAccessSizes];
@@ -522,12 +526,8 @@ static GlobalVariable *createPrivateNonConstGlobalForString(Module &M,
522526
GlobalValue::PrivateLinkage, StrConst, "");
523527
}
524528

525-
/// Insert extern declaration of runtime-provided functions and globals.
526-
void MemorySanitizer::initializeCallbacks(Module &M) {
527-
// Only do this once.
528-
if (WarningFn)
529-
return;
530-
529+
/// Insert declarations for userspace-specific functions and globals.
530+
void MemorySanitizer::createUserspaceApi(Module &M) {
531531
IRBuilder<> IRB(*C);
532532
// Create the callback.
533533
// FIXME: this function should have "Cold" calling conv,
@@ -536,6 +536,38 @@ void MemorySanitizer::initializeCallbacks(Module &M) {
536536
: "__msan_warning_noreturn";
537537
WarningFn = M.getOrInsertFunction(WarningFnName, IRB.getVoidTy());
538538

539+
// Create the global TLS variables.
540+
RetvalTLS = new GlobalVariable(
541+
M, ArrayType::get(IRB.getInt64Ty(), kRetvalTLSSize / 8), false,
542+
GlobalVariable::ExternalLinkage, nullptr, "__msan_retval_tls", nullptr,
543+
GlobalVariable::InitialExecTLSModel);
544+
545+
RetvalOriginTLS = new GlobalVariable(
546+
M, OriginTy, false, GlobalVariable::ExternalLinkage, nullptr,
547+
"__msan_retval_origin_tls", nullptr, GlobalVariable::InitialExecTLSModel);
548+
549+
ParamTLS = new GlobalVariable(
550+
M, ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), false,
551+
GlobalVariable::ExternalLinkage, nullptr, "__msan_param_tls", nullptr,
552+
GlobalVariable::InitialExecTLSModel);
553+
554+
ParamOriginTLS = new GlobalVariable(
555+
M, ArrayType::get(OriginTy, kParamTLSSize / 4), false,
556+
GlobalVariable::ExternalLinkage, nullptr, "__msan_param_origin_tls",
557+
nullptr, GlobalVariable::InitialExecTLSModel);
558+
559+
VAArgTLS = new GlobalVariable(
560+
M, ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), false,
561+
GlobalVariable::ExternalLinkage, nullptr, "__msan_va_arg_tls", nullptr,
562+
GlobalVariable::InitialExecTLSModel);
563+
VAArgOverflowSizeTLS = new GlobalVariable(
564+
M, IRB.getInt64Ty(), false, GlobalVariable::ExternalLinkage, nullptr,
565+
"__msan_va_arg_overflow_size_tls", nullptr,
566+
GlobalVariable::InitialExecTLSModel);
567+
OriginTLS = new GlobalVariable(
568+
M, IRB.getInt32Ty(), false, GlobalVariable::ExternalLinkage, nullptr,
569+
"__msan_origin_tls", nullptr, GlobalVariable::InitialExecTLSModel);
570+
539571
for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
540572
AccessSizeIndex++) {
541573
unsigned AccessSize = 1 << AccessSizeIndex;
@@ -556,6 +588,17 @@ void MemorySanitizer::initializeCallbacks(Module &M) {
556588
MsanPoisonStackFn =
557589
M.getOrInsertFunction("__msan_poison_stack", IRB.getVoidTy(),
558590
IRB.getInt8PtrTy(), IntptrTy);
591+
}
592+
593+
/// Insert extern declaration of runtime-provided functions and globals.
594+
void MemorySanitizer::initializeCallbacks(Module &M) {
595+
// Only do this once.
596+
if (CallbacksInitialized)
597+
return;
598+
599+
IRBuilder<> IRB(*C);
600+
// Initialize callbacks that are common for kernel and userspace
601+
// instrumentation.
559602
MsanChainOriginFn = M.getOrInsertFunction(
560603
"__msan_chain_origin", IRB.getInt32Ty(), IRB.getInt32Ty());
561604
MemmoveFn = M.getOrInsertFunction(
@@ -567,41 +610,13 @@ void MemorySanitizer::initializeCallbacks(Module &M) {
567610
MemsetFn = M.getOrInsertFunction(
568611
"__msan_memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(),
569612
IntptrTy);
570-
571-
// Create globals.
572-
RetvalTLS = new GlobalVariable(
573-
M, ArrayType::get(IRB.getInt64Ty(), kRetvalTLSSize / 8), false,
574-
GlobalVariable::ExternalLinkage, nullptr, "__msan_retval_tls", nullptr,
575-
GlobalVariable::InitialExecTLSModel);
576-
RetvalOriginTLS = new GlobalVariable(
577-
M, OriginTy, false, GlobalVariable::ExternalLinkage, nullptr,
578-
"__msan_retval_origin_tls", nullptr, GlobalVariable::InitialExecTLSModel);
579-
580-
ParamTLS = new GlobalVariable(
581-
M, ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), false,
582-
GlobalVariable::ExternalLinkage, nullptr, "__msan_param_tls", nullptr,
583-
GlobalVariable::InitialExecTLSModel);
584-
ParamOriginTLS = new GlobalVariable(
585-
M, ArrayType::get(OriginTy, kParamTLSSize / 4), false,
586-
GlobalVariable::ExternalLinkage, nullptr, "__msan_param_origin_tls",
587-
nullptr, GlobalVariable::InitialExecTLSModel);
588-
589-
VAArgTLS = new GlobalVariable(
590-
M, ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), false,
591-
GlobalVariable::ExternalLinkage, nullptr, "__msan_va_arg_tls", nullptr,
592-
GlobalVariable::InitialExecTLSModel);
593-
VAArgOverflowSizeTLS = new GlobalVariable(
594-
M, IRB.getInt64Ty(), false, GlobalVariable::ExternalLinkage, nullptr,
595-
"__msan_va_arg_overflow_size_tls", nullptr,
596-
GlobalVariable::InitialExecTLSModel);
597-
OriginTLS = new GlobalVariable(
598-
M, IRB.getInt32Ty(), false, GlobalVariable::ExternalLinkage, nullptr,
599-
"__msan_origin_tls", nullptr, GlobalVariable::InitialExecTLSModel);
600-
601613
// We insert an empty inline asm after __msan_report* to avoid callback merge.
602614
EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
603615
StringRef(""), StringRef(""),
604616
/*hasSideEffects=*/true);
617+
618+
createUserspaceApi(M);
619+
CallbacksInitialized = true;
605620
}
606621

607622
/// Module-level initialization.

0 commit comments

Comments
 (0)