Emma Luciano
Emma Luciano

Reputation: 81

How can I update global variable value in LLVM IR using IRBuilder?

I want to update value of global variable in LLVM IR. I created new global variable in ModulePass:

bool runOnModule(llvm::Module &M) {
    IRBuilder<> Builder(M.getContext());
    Instruction *I = &*inst_begin(M.getFunction("main"));
    Builder.SetInsertPoint(I);
    M.getOrInsertGlobal("globalKey", Builder.getInt64Ty());
    GlobalVariable* gVar = M.getNamedGlobal("globalKey");
    gVar->setLinkage(GlobalValue::InternalLinkage);
    gVar->setAlignment(Align(8));
    gVar->setInitializer(Builder.getInt64(0));
    gVar->setConstant(false);

    for (Function &F : M.functions()) {
        InstructionVisitor visitor(DL, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F));
        for (Instruction &I : instructions(F)) {
            visitor.visit(I);
        }
    }
    return true;
}

Later in InstructionVisitor I try to increment globalKey on each allocation and print it using printf function:

Instruction* InstructionVisitor::print(Instruction* I, const char* text, Value* arg1, Value* arg2, Value* arg3, Value* arg4) {
    Function* printfFn = I->getModule()->getFunction("printf");
    if (printfFn) {
        IRBuilder<> Builder(I->getContext());
        Builder.SetInsertPoint(I->getNextNode());
        Value* convertedText = Builder.CreateGlobalStringPtr(text);
        std::vector <Value *> params;
        params.push_back(convertedText);
        if (arg1)
            params.push_back(arg1);
        if (arg2)
            params.push_back(arg2);
        if (arg3)
            params.push_back(arg3);
        if (arg4)
            params.push_back(arg4);
        return Builder.CreateCall(printfFn, params);
    }
    return I;
}

Instruction* InstructionVisitor::incrementGlobalKey(Instruction* I) {
    IRBuilder<> Builder(I->getContext());
    Builder.SetInsertPoint(I->getNextNode());
    GlobalVariable* key = I->getModule()->getNamedGlobal("globalKey");
    if (key) {
        LoadInst* load = Builder.CreateLoad(key);
        Value* inc = Builder.CreateAdd(load, Builder.getInt64(1));
        StoreInst* store = Builder.CreateStore(inc, key);
        return store;
    }
    return I;
}

void InstructionVisitor::visitCallInst(CallInst &CI) {
    if (isAllocationFn(&CI, &TLI)) {
        Value* allocatedAddress = &CI;
        Instruction* I = &CI;
        Value* allocatedSize = I->getOperand(0);
        Instruction* next = incrementGlobalKey(I);
        GlobalVariable* key = I->getModule()->getNamedGlobal("globalKey");
        const char* message = "Allocated address: 0x%p, size: %d, key: 0x%x\n";
        print(next, message, allocatedAddress, allocatedSize, key->getOperand(0));

    }
}

I print that global variable during execution of instrumented code (using injected printf call). I access it's value by key->getOperand(0) (as shown above), but it's unchanged. I'm using ORC JIT based on this tutorial: https://llvm.org/docs/tutorial/BuildingAJIT2.html and I run ModulePass from optimizeModule function from this tutorial.

IR, souce code that I'm instrumenting and program output can be found here: https://pastebin.com/JbDR2Wug

Does anyone know how to make it work? I will be grateful for help!

Upvotes: 5

Views: 2646

Answers (1)

Emma Luciano
Emma Luciano

Reputation: 81

After @droptop helpful comment I changed my code to actually load the global variable's value using load instruction. It works fine now. Updated code is shown below if anyone need it:

Instruction* InstructionVisitor::getGlobalValue(Instruction* I, StringRef Name) {
    IRBuilder<> Builder(I->getContext());
    Builder.SetInsertPoint(I->getNextNode());
    GlobalVariable* key = I->getModule()->getNamedGlobal(Name);
    if (key) {
        LoadInst* load = Builder.CreateLoad(key);
        return load;
    }
    return nullptr;
}

void InstructionVisitor::visitCallInst(CallInst &CI) {
    if (isAllocationFn(&CI, &TLI)) {
        Value* allocatedAddress = &CI;
        Instruction* I = &CI;
        Value* allocatedSize = I->getOperand(0);
        Instruction* next = incrementGlobalKey(I, allocatedAddress, allocatedSize);
        Instruction* loadKey = getGlobalValue(next, "globalKey"); //here

        const char* message = "Allocated address: 0x%p, size: %d, key: %lld\n";
        next = print(loadKey, message, allocatedAddress, allocatedSize, loadKey);
    }
}

Upvotes: 3

Related Questions