<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>I'm a newbie to LLVM and I've really been struggling to write a pass which changes the behavior of the following function:<div><br></div><div>#include<stdlib.h></div><div>#include<stdio.h></div><div>#include<string.h></div><div>void foo(char* bar)</div><div>{</div><div>const char* path;</div><div>path=getenv(<SOME_ENV_VAR>);</div><div><br></div><div>if (path!=NULL)</div><div>{strcpy(bar,path);}</div><div>else</div><div>{printf("env_var not set!");}</div><div>}</div><div><br></div><div>In the pass, I hope to invoke <span style="font-size: 12pt;">strncpy(dest,src,n) instead of </span><span style="font-size: 12pt;">strcpy(dest,src).</span></div><div><span style="font-size: 12pt;"><br></span></div><div><span style="font-size: 12pt;">After looking at the IR, this is what I've got so far:</span></div><div><span style="font-size: 12pt;"><br></span></div><div><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">#include <stdlib.h></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">#include <stdio.h></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">#include "llvm/Pass.h"</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">#include "llvm/IR/Function.h"</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">#include "llvm/Support/raw_ostream.h"</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">#include "llvm/IR/Module.h"</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">#include "llvm/PassManager.h"</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">#include "llvm/Analysis/Verifier.h"</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">#include "llvm/Assembly/PrintModulePass.h"</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">#include "llvm/IR/IRBuilder.h"</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"> </p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">using namespace llvm;</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><span style="font-size: 12pt; white-space: pre;"><br></span></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><span style="font-size: 12pt; white-space: pre;">Module* makeLLVMModule() {</span></p></div><div><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">
   Module* mod = new Module(getGlobalContext());</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">  <span style="white-space: pre;">
    Function* strncpy_func = cast<Function>(mod->getOrInsertFunction(new StringRef("strncpy"), Type::getInt32Ty(getGlobalContext()), Type::getInt32Ty(getGlobalContext()), Type::getInt32Ty(getGlobalContext()), NULL));</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">  <span style="white-space: pre;">
    Function* printf_func = cast<Function>(mod->getOrInsertFunction(new StringRef("printf"), Type::getInt32Ty(getGlobalContext()), NULL));</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">        <span style="white-space: pre;">
</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">  C<span style="white-space: pre;">onstant* c = mod->getOrInsertFunction(new StringRef("foo"),Type::getInt32Ty(getGlobalContext()),NULL); </span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">  <span style="white-space: pre;">
    Function* foo = cast<Function>(c);</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">    Function::arg_iterator args =foo->arg_begin();</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">    Value* bar = args++;</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"> <span style="font-size: 12pt;"> </span></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">    BasicBlock* Entry = BasicBlock::Create(getGlobalContext(),new Twine("Entry"), foo);</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">    BasicBlock* False = BasicBlock::Create(getGlobalContext(),new Twine("False"), foo);</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">    BasicBlock* True = BasicBlock::Create(getGlobalContext(),new Twine("True"), foo);</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"> <span style="font-size: 12pt;"> </span><span style="font-size: 12pt;"> </span></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">    const char* pPath;</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">  <span style="white-space: pre;">  pPath = getenv("<SOME_ENV_VAR>");</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">    </font><span style="font-size: 12pt;">Twine temp=new Twine("tmp");</span></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">
    IRBuilder<> builder(Entry);</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">    Value* envVarDoesntExist = builder.CreateICmpEQ(new StringRef(pPath),Constant::getNullValue(StringRef),temp);</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">    builder.CreateCondBr(</span><span style="white-space: pre;">envVarDoesntExist</span><span style="white-space: pre;">, False, True);</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">    </span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">    builder.SetInsertPoint(True);</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">    builder.CreateCall3(strncpy_func,buf,new StringRef(pPath),29,temp);</span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;"><br></span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">    builder.SetInsertPoint(False);</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">    builder.CreateCall(printf_func,"Set <SOME_ENV_VAR>\n",temp);</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;"><span style="white-space: pre;">
    return mod;        </span></font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">                                               }</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">}</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"> </p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">char funcP::ID = 0;</font></p><p style="font-family: 'Times New Roman'; font-size: medium; margin-top: 0in; margin-bottom: 0in;"><font style="font-size: 12pt;">static RegisterPass<funcP> X("funcp", "funcP", false, false);</font></p></div><div><span style="font-size: 12pt;"><br></span></div><div><br></div><div>Any help would be appreciated.</div><div><br></div><div>Thanks,</div><div>Rohit</div><div><br></div>                                       </div></body>
</html>