Disable malloc instrumentation for VM (#1)

This patch changes the VM makefile to specify that the magic pass is
to skip memory function instrumentation, and to transfer the data
variables of the malloc code (thus overriding the exception we made
for all other system services).  We add two magic pass flags to
achieve this.  Since the magic pass is a big bowl of spaghetti code,
ignoring whitespace changes while viewing this patch is recommended.

Change-Id: I5ab83b23d8437b37c44dea99537bc202469c9df6
This commit is contained in:
David van Moolenbroek 2015-09-01 06:12:47 +02:00
parent b7725c8552
commit 76b68f9f99
3 changed files with 487 additions and 458 deletions

View file

@ -153,7 +153,7 @@ do
fi
clean_module $n $m
( ${TOOLDIR}/nbmake-${ARCH} -C $m all install MKBITCODE=yes OPTFLAGS.$n="${OPTFLAGS}" \
( ${TOOLDIR}/nbmake-${ARCH} -C $m all install MKBITCODE=yes OPTFLAGS="${OPTFLAGS}" \
&& echo "INFO: $m successfully instrumented."
)
echo

View file

@ -68,7 +68,7 @@ MMAPCtlFunction("magic-mmap-ctlfunc",
static cl::opt<std::string>
MagicDataSections("magic-data-sections",
cl::desc("Specify all the colon-separated magic data section regexes not to instrument"),
cl::init("^" MAGIC_STATIC_VARS_SECTION_PREFIX ".*$:^" UNBL_SECTION_PREFIX ".*$:^" MAGIC_MALLOC_VARS_SECTION_PREFIX ".*$"), cl::NotHidden, cl::ValueRequired);
cl::init("^" MAGIC_STATIC_VARS_SECTION_PREFIX ".*$:^" UNBL_SECTION_PREFIX ".*$"), cl::NotHidden, cl::ValueRequired);
static cl::opt<std::string>
MagicFunctionSections("magic-function-sections",
@ -90,6 +90,16 @@ EnableShadowing("magic-enable-shadowing",
cl::desc("Enable state shadowing"),
cl::init(false), cl::NotHidden);
static cl::opt<bool>
DisableMemFunctions("magic-disable-mem-functions",
cl::desc("Disable hooking of memory functions"),
cl::init(false), cl::NotHidden);
static cl::opt<bool>
DisableMallocSkip("magic-disable-malloc-skip",
cl::desc("Disable ignoring malloc data variables"),
cl::init(false), cl::NotHidden);
static cl::opt<bool>
SkipAll("magic-skip-all",
cl::desc("Exit immediately"),
@ -355,7 +365,10 @@ bool MagicPass::runOnModule(Module &M) {
PassUtil::parseStringListOpt(mmapCtlFunctions, MMAPCtlFunction);
//determine magic data section regexes
PassUtil::parseRegexListOpt(magicDataSectionRegexes, MagicDataSections);
std::string DataSections = MagicDataSections;
if (!DisableMallocSkip)
DataSections += ":^" MAGIC_MALLOC_VARS_SECTION_PREFIX ".*$";
PassUtil::parseRegexListOpt(magicDataSectionRegexes, DataSections);
//determine magic function section regexes
PassUtil::parseRegexListOpt(magicFunctionSectionRegexes, MagicFunctionSections);
@ -415,6 +428,11 @@ bool MagicPass::runOnModule(Module &M) {
TypeInfo::setBitCastTypes(bitCastMap);
#if MAGIC_INSTRUMENT_MEM_FUNCS
std::vector<MagicMemFunction> magicMemFunctions;
std::set<Function*> originalMagicMemFunctions;
std::vector<MagicDebugFunction> magicDebugFunctions;
std::vector<MagicMmapCtlFunction> magicMmapCtlFunctions;
if (!DisableMemFunctions) {
//look up magic memory functions and corresponding wrappers
#define __X(P) #P
std::string magicMemFuncNames[] = { MAGIC_MEM_FUNC_NAMES };
@ -429,8 +447,6 @@ bool MagicPass::runOnModule(Module &M) {
}
llvmCallPrefixes.push_back("");
llvmCallPrefixes.push_back("\01"); //llvm uses odd prefixes for some functions, sometimes (e.g. mmap64)
std::vector<MagicMemFunction> magicMemFunctions;
std::set<Function*> originalMagicMemFunctions;
for(i=0;magicMemFuncNames[i].compare("");i++) {
int allocFlags = magicMemFuncAllocFlags[i];
for(unsigned j=0;j<llvmCallPrefixes.size();j++) {
@ -575,7 +591,6 @@ bool MagicPass::runOnModule(Module &M) {
}
}
std::vector<MagicDebugFunction> magicDebugFunctions;
if (mmPoolFunctions.size()) {
assert(mmPoolFunctions.size() >= 3 && mmPoolFunctions.size() <= 5 &&
"Specify at least 3 and at most 5 of the pool management types of functions: block alloc,pool create,pool destroy,pool management functions,pool reset functions.");
@ -665,7 +680,6 @@ bool MagicPass::runOnModule(Module &M) {
}
//lookup mmap ctl functions whose call arguments need to be fixed
std::vector<MagicMmapCtlFunction> magicMmapCtlFunctions;
for (std::vector<std::string>::iterator it = mmapCtlFunctions.begin(); it != mmapCtlFunctions.end(); ++it) {
std::vector<std::string> tokens;
tokens.clear();
@ -681,7 +695,8 @@ bool MagicPass::runOnModule(Module &M) {
MagicMmapCtlFunction magicMmapCtlFunction(function, PointerType::get(IntegerType::get(M.getContext(), 8), 0), ptrArgName, lenArgName);
magicMmapCtlFunctions.push_back(magicMmapCtlFunction);
}
#endif
}
#endif /*MAGIC_INSTRUMENT_MEM_FUNCS*/
//everything as expected, set magic enabled variable to TRUE
magicEnabled->setInitializer(ConstantInt::get(M.getContext(), APInt(32, 1)));
@ -943,8 +958,9 @@ bool MagicPass::runOnModule(Module &M) {
std::vector<int> magicDsindexFlagsList;
#if MAGIC_INSTRUMENT_MEM_FUNCS
//gather magic memory function calls to replace and figure out the type (adding more (local) types if needed)
std::vector<MagicMemFunction> magicMemFunctionCalls;
if (!DisableMemFunctions) {
//gather magic memory function calls to replace and figure out the type (adding more (local) types if needed)
std::map< std::pair<std::string,std::string>, int> namesMap;
int allocFlags;
std::set<Function*> extendedMagicMemFunctions;
@ -1052,7 +1068,7 @@ bool MagicPass::runOnModule(Module &M) {
allocName = MAGIC_ALLOC_EXT_NAME;
allocName = MAGIC_ALLOC_EXT_PARENT_NAME;
}
#endif
#endif
//avoid duplicates
namesMapIt = namesMap.find(std::pair<std::string, std::string>(allocParentName, allocName));
@ -1109,7 +1125,6 @@ bool MagicPass::runOnModule(Module &M) {
//if we have a parent, add a dependency
magicMemParent->addInstructionDep(magicMemFunction);
assert(magicMemParent->getAllocFlags());
}
else {
//if there is no parent, add it to the call queue
@ -1127,7 +1142,8 @@ bool MagicPass::runOnModule(Module &M) {
}
}
}
#endif
}
#endif /*MAGIC_INSTRUMENT_MEM_FUNCS*/
#if MAGIC_INSTRUMENT_STACK
std::vector<std::map<AllocaInst*, std::pair<TypeInfo*, std::string> > > localTypeInfoMaps;
@ -1601,6 +1617,7 @@ bool MagicPass::runOnModule(Module &M) {
}
#if MAGIC_INSTRUMENT_MEM_FUNCS
if (!DisableMemFunctions) {
//replace magic memory function calls with their wrappers
for(i=0;i<magicMemFunctionCalls.size();i++) {
MagicMemFunction *magicMemFunctionCall = &magicMemFunctionCalls[i];
@ -1618,7 +1635,8 @@ bool MagicPass::runOnModule(Module &M) {
MagicMmapCtlFunction *magicMmapCtlFunction = &magicMmapCtlFunctions[i];
magicMmapCtlFunction->fixCalls(M, magicGetPageSizeFunc);
}
#endif
}
#endif /*MAGIC_INSTRUMENT_MEM_FUNCS*/
#if MAGIC_INSTRUMENT_STACK
//instrument the stack for the relevant set of functions and add dsindex entries

View file

@ -18,5 +18,16 @@ LDADD+= -lsys -lexec
CPPFLAGS+= -I${.CURDIR} -I${.CURDIR}/arch/${MACHINE_ARCH}
CPPFLAGS+= -I${NETBSDSRCDIR}/minix
# For all other services, magic instrumentation involves instrumenting the
# libc malloc code, hooking its nested mmap/munmap calls, and ignoring its
# data. For VM, we need to do the exact opposite, since for VM, the malloc
# state is transferred as is. Thus, if the magic pass is enabled, tell it
# to skip the regular malloc instrumentation features.
.if !empty(OPTFLAGS:M*-magic*)
OPTFLAGS.vm?= ${OPTFLAGS} \
-magic-disable-mem-functions \
-magic-disable-malloc-skip
.endif
.include "arch/${MACHINE_ARCH}/Makefile.inc"
.include <minix.service.mk>