1111 -0xDEAD // The same value has to be used as initializer for the signatures in
1212 // the code
1313
14- #define MARTI_DEBUG true
14+ #define MARTI_DEBUG false
1515
1616using namespace llvm ;
1717
@@ -106,9 +106,9 @@ void RACFED::initializeBlocksSignatures(Function &Fn) {
106106// --- UPDATE SIGNATURE RANDOM ---
107107void originalInstruction (BasicBlock &BB, std::vector<Instruction*> &OrigInstructions) {
108108 for (Instruction &I : BB) {
109- if (isa<PHINode>(&I)) continue ; // NON è originale
110- if (I.isTerminator ()) continue ; // NON è originale
111- if (isa<DbgInfoIntrinsic>(&I)) continue ; // debug, ignora OrigInstructions.push_back(&I);
109+ if ( isa<PHINode>(&I) ) continue ; // NON è originale
110+ if ( I.isTerminator () ) continue ; // NON è originale
111+ if ( isa<DbgInfoIntrinsic>(&I) ) continue ; // debug, ignora OrigInstructions.push_back(&I);
112112 OrigInstructions.push_back (&I);
113113 }
114114}
@@ -122,15 +122,15 @@ void RACFED::updateCompileSigRandom(Function &Fn,
122122 std::vector<Instruction*> OrigInstructions;
123123 originalInstruction (BB, OrigInstructions);
124124
125- if (OrigInstructions.size () <= 2 ) continue ;
125+ if ( OrigInstructions.size () <= 2 ) continue ;
126126
127127 uint64_t partial_sum = 0 ;
128128
129129 for (Instruction *I : OrigInstructions) {
130130 Instruction *InsertPt = nullptr ;
131131
132132 // Non puoi inserire "dopo" un terminator: inserisci prima del terminator stesso
133- if (I->isTerminator ()) {
133+ if ( I->isTerminator () ) {
134134 InsertPt = I; // insert BEFORE terminator
135135 } else {
136136 InsertPt = I->getNextNode (); // insert BEFORE next instruction (equivale a "dopo I")
@@ -155,20 +155,20 @@ void RACFED::updateCompileSigRandom(Function &Fn,
155155void RACFED::checkJumpSignature (BasicBlock &BB,
156156 GlobalVariable *RuntimeSigGV, Type *IntType,
157157 BasicBlock &ErrBB) {
158- if ( BB.isEntryBlock () ) return ;
158+ if ( BB.isEntryBlock () ) return ;
159159
160160 // in this case BB is not the first Basic Block of the function, so it has
161161 // to update RuntimeSig and check it
162162 Instruction *FirstNonPHI = BB.getFirstNonPHI ();
163163 if ( (FirstNonPHI && isa<LandingPadInst>(FirstNonPHI)) ||
164164 BB.getName ().contains_insensitive (" verification" ) ) {
165165
166- if (BB.getFirstInsertionPt () == BB.end ()) return ; // Skip empty/invalid blocks
166+ if ( BB.getFirstInsertionPt () == BB.end () ) return ; // Skip empty/invalid blocks
167167
168168 int randomNumberBB = compileTimeSig.find (&BB)->second ;
169169 IRBuilder<> BChecker (&*BB.getFirstInsertionPt ());
170170 BChecker.CreateStore (llvm::ConstantInt::get (IntType, randomNumberBB), RuntimeSigGV, true );
171- } else if (!BB.getName ().contains_insensitive (" errbb" )) {
171+ } else if ( !BB.getName ().contains_insensitive (" errbb" ) ) {
172172 int randomNumberBB = compileTimeSig.find (&BB)->second ;
173173 int subRanPrevVal = subRanPrevVals.find (&BB)->second ;
174174 BasicBlock *NewBB = BasicBlock::Create (
@@ -200,7 +200,7 @@ void RACFED::checkJumpSignature(BasicBlock &BB,
200200 for (BasicBlock *Succ : successors (&BB)) {
201201 for (PHINode &Phi : Succ->phis ()) {
202202 for (unsigned i = 0 ; i < Phi.getNumIncomingValues (); ++i) {
203- if (Phi.getIncomingBlock (i) == NewBB) {
203+ if ( Phi.getIncomingBlock (i) == NewBB ) {
204204 Phi.setIncomingBlock (i, &BB);
205205 }
206206 }
@@ -235,13 +235,13 @@ Constant* expectedSignature(
235235}
236236
237237Value *RACFED::getCondition (Instruction &I) {
238- if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional ()) {
239- if (!cast<BranchInst>(I).isConditional ()) {
238+ if ( isa<BranchInst>(I) && cast<BranchInst>(I).isConditional () ) {
239+ if ( !cast<BranchInst>(I).isConditional () ) {
240240 return nullptr ;
241241 } else {
242242 return cast<BranchInst>(I).getCondition ();
243243 }
244- } else if (isa<SwitchInst>(I)) {
244+ } else if ( isa<SwitchInst>(I) ) {
245245 errs () << " There is a switch!\n " ;
246246 abort ();
247247 return cast<SwitchInst>(I).getCondition ();
@@ -263,10 +263,10 @@ static void printSig(Module &Md, IRBuilder<> &B, Value *SigVal, const char *Msg)
263263
264264 // Crea stringa globale "Msg: %ld\n"
265265 std::string Fmt = std::string (Msg) + " : %ld\n " ;
266- Value *FmtStr = B.CreateGlobalStringPtr (Fmt);
266+ Value *FmtStr = B.CreateGlobalString (Fmt);
267267
268268
269- if (SigVal->getType ()->isIntegerTy (32 )) {
269+ if ( SigVal->getType ()->isIntegerTy (32 ) ) {
270270 SigVal = B.CreateZExt (SigVal, Type::getInt64Ty (Ctx));
271271 }
272272
@@ -287,7 +287,7 @@ void RACFED::checkBranches(Module &Md, BasicBlock &BB, GlobalVariable *RuntimeS
287287 IRBuilder<> B (&BB);
288288 B.SetInsertPoint (Term);
289289 auto *BI = dyn_cast<BranchInst>(Term);
290- if (!BI) return ;
290+ if ( !BI ) return ;
291291
292292 // TODO: check this
293293
@@ -296,25 +296,29 @@ void RACFED::checkBranches(Module &Md, BasicBlock &BB, GlobalVariable *RuntimeS
296296 static_cast <uint64_t >(compileTimeSig[&BB]) + sumIntraInstruction[&BB];
297297
298298 Value *Current = B.CreateLoad (IntType, RuntimeSigGV, " current" );
299+ #if MARTI_DEBUG
299300 printSig (Md, B, Current, " current" );
301+ #endif
300302
301303 // TODO: until here
302304
303305 // define if conditional or unconditional branch
304306 // Conditional: expected= CT_succ+subRan_succ
305307 // adj = CTB-exp--> new signature = RT -adj
306308 if ( BI->isUnconditional () ) { // only one successor
307- BasicBlock *Succ = BI->getSuccessor (0 );
308- uint64_t Expected =
309- static_cast <uint64_t >(compileTimeSig[Succ] + subRanPrevVals[Succ]);
310- // adj = expected - current
311- uint64_t AdjValue = Expected - SourceStatic;
312- Value *Adj = ConstantInt::get (IntType, AdjValue);
313- Value *NewSig = B.CreateAdd (Current, Adj, " racfed_newsig" );
314- B.CreateStore (NewSig, RuntimeSigGV);
315- printSig (Md,B, NewSig, " newsig" );
309+ BasicBlock *Succ = BI->getSuccessor (0 );
310+ uint64_t SuccExpected =
311+ static_cast <uint64_t >(compileTimeSig[Succ] + subRanPrevVals[Succ]);
312+ // adj = expected - current
313+ uint64_t AdjValue = SuccExpected - SourceStatic;
314+ Value *Adj = ConstantInt::get (IntType, AdjValue);
315+ Value *NewSig = B.CreateAdd (Current, Adj, " racfed_newsig" );
316+ B.CreateStore (NewSig, RuntimeSigGV);
317+ #if MARTI_DEBUG
318+ printSig (Md,B, NewSig, " newsig" );
319+ #endif
316320
317- return ;
321+ return ;
318322 }
319323
320324 if ( BI-> isConditional ()) {
@@ -337,7 +341,9 @@ void RACFED::checkBranches(Module &Md, BasicBlock &BB, GlobalVariable *RuntimeS
337341 Value *NewSig = B.CreateAdd (Current, Adj, " racfed_newsig" );
338342 B.CreateStore (NewSig, RuntimeSigGV);
339343
344+ #if MARTI_DEBUG
340345 printSig (Md, B, NewSig, " SIG after cond" );
346+ #endif
341347 }
342348}
343349
@@ -360,11 +366,11 @@ Instruction *RACFED::checkReturnValue(BasicBlock &BB,
360366 Value *BckupRunSig) {
361367 Instruction *Term = BB.getTerminator ();
362368
363- if ( !isa<ReturnInst>(Term) ) return nullptr ;
369+ if ( !isa<ReturnInst>(Term) ) return nullptr ;
364370
365371 std::vector<Instruction*> org_instr;
366372 originalInstruction (BB, org_instr);
367- if ( org_instr.size () > 2 ) {
373+ if ( org_instr.size () > 2 ) {
368374
369375 // Splits the BB that contains the return instruction into
370376 // two basic blocks:
@@ -443,7 +449,7 @@ PreservedAnalyses RACFED::run(Module &Md, ModuleAnalysisManager &AM) {
443449 }
444450
445451 for (Function &Fn: Md) {
446- if (!shouldCompile (Fn, FuncAnnotations)) continue ;
452+ if (!shouldCompile (Fn, FuncAnnotations)) continue ;
447453
448454 #if MARTI_DEBUG
449455 errs () << " Analysing func " << Fn.getName () << " \n " ;
@@ -474,23 +480,23 @@ PreservedAnalyses RACFED::run(Module &Md, ModuleAnalysisManager &AM) {
474480
475481 Value * runtime_sign_bkup = nullptr ;
476482 for (BasicBlock &BB : Fn) {
477- // TODO: Should the error basic block that is inserted be checked?
478-
479483 // Backup of compile time sign when entering a function
480- if ( BB.isEntryBlock () ) {
484+ if ( BB.isEntryBlock () ) {
481485 IRBuilder<> InstrIR (&*BB.getFirstInsertionPt ());
482- runtime_sign_bkup =
483- InstrIR.CreateLoad (I64, RuntimeSig, true , " backup_run_sig" );
484- InstrIR.CreateStore (llvm::ConstantInt::get (I64, compileTimeSig[&BB]),
485- RuntimeSig);
486+ if ( Fn.getName () != " main" ) {
487+ runtime_sign_bkup =
488+ InstrIR.CreateLoad (I64, RuntimeSig, true , " backup_run_sig" );
489+ }
490+ InstrIR.CreateStore (llvm::ConstantInt::get (I64, compileTimeSig[&BB]),
491+ RuntimeSig);
486492 }
487493
488494 checkJumpSignature (BB, RuntimeSig, I64, *ErrBB);
489495 RetInst = checkReturnValue (BB, RuntimeSig, I64, *ErrBB, runtime_sign_bkup);
490496 checkBranches (Md, BB, RuntimeSig, I64);
491497
492498 // Restore signature on return
493- if ( RetInst != nullptr ) {
499+ if ( RetInst != nullptr && Fn. getName () != " main " ) {
494500 IRBuilder<> RetInstIR (RetInst);
495501 RetInstIR.CreateStore (runtime_sign_bkup, RuntimeSig);
496502 }
0 commit comments