2010-10-08 04:17:24 +08:00
|
|
|
//===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2010-10-08 04:17:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the common initialization infrastructure for the
|
|
|
|
// Instrumentation library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-08-15 00:45:42 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2010-10-08 04:17:24 +08:00
|
|
|
#include "llvm-c/Initialization.h"
|
2018-11-08 08:57:33 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2015-08-15 00:45:42 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2018-10-12 07:03:27 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2015-08-15 00:45:42 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/PassRegistry.h"
|
2010-10-08 04:17:24 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-08-15 00:45:42 +08:00
|
|
|
/// Moves I before IP. Returns new insert point.
|
|
|
|
static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP) {
|
|
|
|
// If I is IP, move the insert point down.
|
2019-04-26 04:09:00 +08:00
|
|
|
if (I == IP) {
|
|
|
|
++IP;
|
|
|
|
} else {
|
|
|
|
// Otherwise, move I before IP and return IP.
|
|
|
|
I->moveBefore(&*IP);
|
|
|
|
}
|
2015-08-15 00:45:42 +08:00
|
|
|
return IP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Instrumentation passes often insert conditional checks into entry blocks.
|
|
|
|
/// Call this function before splitting the entry block to move instructions
|
|
|
|
/// that must remain in the entry block up before the split point. Static
|
|
|
|
/// allocas and llvm.localescape calls, for example, must remain in the entry
|
|
|
|
/// block.
|
|
|
|
BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB,
|
|
|
|
BasicBlock::iterator IP) {
|
|
|
|
assert(&BB.getParent()->getEntryBlock() == &BB);
|
|
|
|
for (auto I = IP, E = BB.end(); I != E; ++I) {
|
|
|
|
bool KeepInEntry = false;
|
|
|
|
if (auto *AI = dyn_cast<AllocaInst>(I)) {
|
|
|
|
if (AI->isStaticAlloca())
|
|
|
|
KeepInEntry = true;
|
|
|
|
} else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
|
|
|
|
if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
|
|
|
|
KeepInEntry = true;
|
|
|
|
}
|
|
|
|
if (KeepInEntry)
|
|
|
|
IP = moveBeforeInsertPoint(I, IP);
|
|
|
|
}
|
|
|
|
return IP;
|
|
|
|
}
|
|
|
|
|
2018-10-12 07:03:27 +08:00
|
|
|
// Create a constant for Str so that we can pass it to the run-time lib.
|
|
|
|
GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str,
|
|
|
|
bool AllowMerging,
|
|
|
|
const char *NamePrefix) {
|
|
|
|
Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
|
|
|
|
// We use private linkage for module-local strings. If they can be merged
|
|
|
|
// with another one, we set the unnamed_addr attribute.
|
|
|
|
GlobalVariable *GV =
|
|
|
|
new GlobalVariable(M, StrConst->getType(), true,
|
|
|
|
GlobalValue::PrivateLinkage, StrConst, NamePrefix);
|
|
|
|
if (AllowMerging)
|
|
|
|
GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
|
2019-10-15 19:24:36 +08:00
|
|
|
GV->setAlignment(Align::None()); // Strings may not be merged w/o setting
|
|
|
|
// alignment explicitly.
|
2018-10-12 07:03:27 +08:00
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2018-11-08 08:57:33 +08:00
|
|
|
Comdat *llvm::GetOrCreateFunctionComdat(Function &F, Triple &T,
|
2018-10-13 07:21:48 +08:00
|
|
|
const std::string &ModuleId) {
|
|
|
|
if (auto Comdat = F.getComdat()) return Comdat;
|
|
|
|
assert(F.hasName());
|
|
|
|
Module *M = F.getParent();
|
|
|
|
std::string Name = F.getName();
|
2018-11-08 08:57:33 +08:00
|
|
|
|
|
|
|
// Make a unique comdat name for internal linkage things on ELF. On COFF, the
|
|
|
|
// name of the comdat group identifies the leader symbol of the comdat group.
|
|
|
|
// The linkage of the leader symbol is considered during comdat resolution,
|
|
|
|
// and internal symbols with the same name from different objects will not be
|
|
|
|
// merged.
|
|
|
|
if (T.isOSBinFormatELF() && F.hasLocalLinkage()) {
|
2018-10-13 07:21:48 +08:00
|
|
|
if (ModuleId.empty())
|
|
|
|
return nullptr;
|
|
|
|
Name += ModuleId;
|
|
|
|
}
|
2018-11-08 08:57:33 +08:00
|
|
|
|
|
|
|
// Make a new comdat for the function. Use the "no duplicates" selection kind
|
|
|
|
// for non-weak symbols if the object file format supports it.
|
|
|
|
Comdat *C = M->getOrInsertComdat(Name);
|
|
|
|
if (T.isOSBinFormatCOFF() && !F.isWeakForLinker())
|
|
|
|
C->setSelectionKind(Comdat::NoDuplicates);
|
|
|
|
F.setComdat(C);
|
|
|
|
return C;
|
2018-10-13 07:21:48 +08:00
|
|
|
}
|
|
|
|
|
2010-10-08 04:17:24 +08:00
|
|
|
/// initializeInstrumentation - Initialize all passes in the TransformUtils
|
|
|
|
/// library.
|
|
|
|
void llvm::initializeInstrumentation(PassRegistry &Registry) {
|
2019-02-14 06:22:48 +08:00
|
|
|
initializeAddressSanitizerLegacyPassPass(Registry);
|
|
|
|
initializeModuleAddressSanitizerLegacyPassPass(Registry);
|
2017-11-14 09:30:04 +08:00
|
|
|
initializeBoundsCheckingLegacyPassPass(Registry);
|
2018-09-05 01:19:13 +08:00
|
|
|
initializeControlHeightReductionLegacyPassPass(Registry);
|
2016-06-05 11:40:03 +08:00
|
|
|
initializeGCOVProfilerLegacyPassPass(Registry);
|
2016-05-06 13:49:19 +08:00
|
|
|
initializePGOInstrumentationGenLegacyPassPass(Registry);
|
2016-05-07 13:39:12 +08:00
|
|
|
initializePGOInstrumentationUseLegacyPassPass(Registry);
|
2016-05-15 09:04:24 +08:00
|
|
|
initializePGOIndirectCallPromotionLegacyPassPass(Registry);
|
2017-04-05 00:42:20 +08:00
|
|
|
initializePGOMemOPSizeOptLegacyPassPass(Registry);
|
2019-03-01 04:13:38 +08:00
|
|
|
initializeInstrOrderFileLegacyPassPass(Registry);
|
2016-04-19 01:47:38 +08:00
|
|
|
initializeInstrProfilingLegacyPassPass(Registry);
|
[NewPM] Port Msan
Summary:
Keeping msan a function pass requires replacing the module level initialization:
That means, don't define a ctor function which calls __msan_init, instead just
declare the init function at the first access, and add that to the global ctors
list.
Changes:
- Pull the actual sanitizer and the wrapper pass apart.
- Add a newpm msan pass. The function pass inserts calls to runtime
library functions, for which it inserts declarations as necessary.
- Update tests.
Caveats:
- There is one test that I dropped, because it specifically tested the
definition of the ctor.
Reviewers: chandlerc, fedor.sergeev, leonardchan, vitalybuka
Subscribers: sdardis, nemanjai, javed.absar, hiraditya, kbarton, bollu, atanasyan, jsji
Differential Revision: https://reviews.llvm.org/D55647
llvm-svn: 350305
2019-01-03 21:42:44 +08:00
|
|
|
initializeMemorySanitizerLegacyPassPass(Registry);
|
2019-05-15 05:17:21 +08:00
|
|
|
initializeHWAddressSanitizerLegacyPassPass(Registry);
|
2019-01-16 17:28:01 +08:00
|
|
|
initializeThreadSanitizerLegacyPassPass(Registry);
|
2019-09-05 04:30:29 +08:00
|
|
|
initializeModuleSanitizerCoverageLegacyPassPass(Registry);
|
2013-08-08 06:47:18 +08:00
|
|
|
initializeDataFlowSanitizerPass(Registry);
|
2010-10-08 04:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LLVMInitializeInstrumentation - C binding for
|
|
|
|
/// initializeInstrumentation.
|
|
|
|
void LLVMInitializeInstrumentation(LLVMPassRegistryRef R) {
|
|
|
|
initializeInstrumentation(*unwrap(R));
|
|
|
|
}
|