Skip to content

Commit 7fb8a71

Browse files
committed
Squashed commit of the following:
commit 63a08b600f4a5cab740ba4a291bc7957493c3460 Author: Gab-San <santandreagabriele+github@gmail.com> Date: Wed Dec 24 18:58:50 2025 +0100 divided checkVerification commit ff943bc85c703d7e26862b27f6f6d065479f7f3b Author: Gab-San <santandreagabriele+github@gmail.com> Date: Wed Dec 24 17:24:29 2025 +0100 moving int values to uint32 and uint64 accordingly commit 006761882863a1d9229505dbf0cb44362caa7d3f Author: Gab-San <santandreagabriele+github@gmail.com> Date: Wed Dec 24 17:08:56 2025 +0100 standardizing code
1 parent e1192b8 commit 7fb8a71

File tree

2 files changed

+128
-106
lines changed

2 files changed

+128
-106
lines changed

passes/ASPIS.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ class RACFED : public PassInfoMixin<RACFED> {
160160
private:
161161
std::map<Value *, StringRef> FuncAnnotations;
162162
std::map<BasicBlock *, BasicBlock *> NewBBs;
163-
std::unordered_map<BasicBlock *, int> compileTimeSig;
164-
std::unordered_map<BasicBlock *, int> subRanPrevVals;
163+
std::unordered_map<BasicBlock *, uint32_t> compileTimeSig;
164+
std::unordered_map<BasicBlock *, uint32_t> subRanPrevVals;
165165
std::unordered_map<BasicBlock *, uint64_t> sumIntraInstruction;
166166

167167

@@ -170,22 +170,25 @@ class RACFED : public PassInfoMixin<RACFED> {
170170
#endif
171171

172172
// -- INITIALIZE BLOCKS --
173-
void initializeBlocksSignatures(Module &Md);
173+
void initializeBlocksSignatures(Module &Md, Function &Fn);
174+
174175
// -- UPDATE COMPILE SIG RANDOM --
175-
void updateCompileSigRandom(Function &F, Module &Md);
176+
void updateCompileSigRandom(Module &Md, Function &Fn);
176177

177178
// -- CREATE CFG VERIFICATION --
179+
void checkCompileTimeSigAtJump(Module &Md, Function &Fn);
178180

179181
void splitBBsAtCalls(Module &Md);
180-
int countOriginalInstructions(BasicBlock &BB);
181182
CallBase *isCallBB(BasicBlock &BB);
182183
void initializeEntryBlocksMap(Module &Md);
183184
Value *getCondition(Instruction &I);
184-
void createCFGVerificationBB(BasicBlock &BB,
185-
std::unordered_map<BasicBlock *, int> &RandomNumberBBs,
186-
std::unordered_map<BasicBlock *, int> &SubRanPrevVals,
187-
Value &RuntimeSig, Value &RetSig,
188-
BasicBlock &ErrBB);
185+
void createCFGVerificationBB(
186+
BasicBlock &BB,
187+
std::unordered_map<BasicBlock *, int> &RandomNumberBBs,
188+
std::unordered_map<BasicBlock *, int> &SubRanPrevVals,
189+
Value &RuntimeSig, Value &RetSig,
190+
BasicBlock &ErrBB
191+
);
189192

190193
public:
191194
PreservedAnalyses run(Module &Md, ModuleAnalysisManager &);

passes/RACFED.cpp

Lines changed: 115 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ using namespace llvm;
2929
8: for all original instructions insert after
3030
9: signature ← signature + random number
3131
* create CFG
32-
* checkBeginning
32+
* checkCompileTimeSigAtJump
3333
10:for all BB in CFG insert at beginning
3434
11: signature ← signature − subRanPrevVal
3535
12: if signature != compileTimeSig error()
@@ -38,7 +38,7 @@ using namespace llvm;
3838
14: if Last Instr. is return instr. and NrIntrBB > 1 then
3939
15: Calculate needed variables
4040
16: return Val ← random number
41-
17: adjust Value ← (compileTimeSigBB + Sum{instrMonUpdates}) -
41+
17: adjust Value ← (compileTimeSigBB + Sum{in%2 = load i32, ptr @signature, align 4
4242
18: return Val
4343
19: Insert signature update before return instr.
4444
20: signature ← signature + adjustValue
@@ -56,62 +56,57 @@ using namespace llvm;
5656
// --- INITIALIZE BLOCKS RANDOM ---
5757

5858
bool isNotUniqueCompileTimeSig(
59-
const int bb_num,
60-
const std::unordered_map<BasicBlock*, int> &compileTimeSig
59+
const uint32_t bb_num,
60+
const std::unordered_map<BasicBlock*, uint32_t> &compileTimeSig
6161
) {
62-
for (const auto &pair : compileTimeSig) {
63-
if (pair.second == bb_num) return true;
62+
for (const auto &[_, other_bb_id] : compileTimeSig) {
63+
if ( other_bb_id == bb_num ) return true;
6464
}
6565
return false;
6666
}
6767

6868
bool isNotUnique(
69-
const int bb_num, const int subrun_num,
70-
const std::unordered_map<BasicBlock*, int> &RandomNumBBs,
71-
const std::unordered_map<BasicBlock*, int> &SubRanPrevVals
69+
const uint64_t current_id,
70+
const std::unordered_map<BasicBlock*, uint32_t> &RandomNumBBs,
71+
const std::unordered_map<BasicBlock*, uint32_t> &SubRanPrevVals
7272
) {
7373

74-
for (const auto &pair : RandomNumBBs) {
75-
if (pair.second + SubRanPrevVals.at(pair.first) == bb_num + subrun_num) {
74+
for (const auto &[other_bb, other_bb_num] : RandomNumBBs) {
75+
uint64_t other_id = static_cast<uint64_t>(other_bb_num) + SubRanPrevVals.at(other_bb);
76+
if ( other_id == current_id ) {
7677
return true;
7778
}
7879
}
7980

8081
return false;
8182
}
8283

83-
void RACFED::initializeBlocksSignatures(Module &Md) {
84-
int i = 0;
85-
srand(static_cast<unsigned>(time(nullptr))); // compileTimeSig weak random generator
86-
int randomBB;
87-
int randomSub;
88-
89-
for (Function &Fn : Md) {
90-
if (shouldCompile(Fn, FuncAnnotations)) {
91-
for (BasicBlock &BB : Fn) {
92-
93-
do {
94-
randomBB = rand();
95-
} while (isNotUniqueCompileTimeSig(randomBB, compileTimeSig));
96-
97-
do {
98-
randomSub = rand();
99-
} while (isNotUnique(i, randomSub, compileTimeSig, subRanPrevVals));
100-
101-
compileTimeSig.insert(std::pair(&BB, randomBB));//not random, guarantees unicity
102-
subRanPrevVals.insert(std::pair(&BB, randomSub)); //In this way the sub value is random
103-
sumIntraInstruction.insert(std::pair(&BB, 0));//assign value to the sum of the instr
104-
i++;
105-
106-
}
107-
}
84+
void RACFED::initializeBlocksSignatures(Module &Md, Function &Fn) {
85+
std::mt19937 rng(0xB00BA5); // seed fisso per riproducibilità
86+
std::uniform_int_distribution<uint32_t> dist(1, 0x7fffffff);
87+
uint32_t randomBB;
88+
uint32_t randomSub;
89+
90+
for (BasicBlock &BB : Fn) {
91+
do {
92+
randomBB = dist(rng);
93+
} while ( isNotUniqueCompileTimeSig(randomBB, compileTimeSig) );
94+
95+
do {
96+
randomSub = dist(rng);
97+
} while ( isNotUnique(
98+
static_cast<uint64_t>(randomBB) + randomSub,
99+
compileTimeSig,
100+
subRanPrevVals) );
101+
102+
compileTimeSig.insert(std::pair(&BB, randomBB));
103+
subRanPrevVals.insert(std::pair(&BB, randomSub));
108104
}
109105
}
110106

111107

112108
// --- UPDATE SIGNATURE RANDOM ---
113109
void originalInstruction(BasicBlock &BB, std::vector<Instruction*> &OrigInstructions) {
114-
115110
for (Instruction &I : BB) {
116111
if (isa<PHINode>(&I)) continue; // NON è originale
117112
if (I.isTerminator()) continue; // NON è originale
@@ -120,28 +115,27 @@ void originalInstruction(BasicBlock &BB, std::vector<Instruction*> &OrigInstruc
120115
}
121116
}
122117

123-
void RACFED::updateCompileSigRandom(Function &F, Module &Md) {
118+
void RACFED::updateCompileSigRandom(Module &Md, Function &Fn) {
124119
LLVMContext &Ctx = Md.getContext();
125120
GlobalVariable *SigGV = Md.getNamedGlobal("signature");
126121
std::mt19937 rng(0xC0FFEE); // seed fisso per riproducibilità
127122
std::uniform_int_distribution<uint32_t> dist(1, 0x7fffffff);
128-
auto *I32 = Type::getInt32Ty(Ctx);
123+
auto *I64 = Type::getInt64Ty(Ctx);
129124
if (!SigGV) {
130125
SigGV = new GlobalVariable(
131126
Md,
132-
I32,
127+
I64,
133128
/*isConstant=*/false,
134129
GlobalValue::ExternalLinkage,
135-
ConstantInt::get(I32, 0),
130+
ConstantInt::get(I64, 0),
136131
"signature");
137132
}
138133

139-
for (auto &BB: F){
134+
for (auto &BB: Fn){
140135
std::vector<Instruction*> OrigInstructions;
141136
originalInstruction(BB, OrigInstructions);
142137

143-
if (OrigInstructions.size() <= 2)
144-
continue;
138+
if (OrigInstructions.size() <= 2) continue;
145139

146140
uint64_t partial_sum;
147141

@@ -168,15 +162,76 @@ void RACFED::updateCompileSigRandom(Function &F, Module &Md) {
168162

169163
sumIntraInstruction.insert(std::pair(&BB, partial_sum));
170164

171-
Value *OldSig = B.CreateLoad(I32, SigGV);
172-
Value *Add = B.CreateAdd(OldSig, ConstantInt::get(I32, K), "sig_add");
165+
Value *OldSig = B.CreateLoad(I64, SigGV);
166+
Value *Add = B.CreateAdd(OldSig, ConstantInt::get(I64, K), "sig_add");
173167
B.CreateStore(Add, SigGV);
174168
}
175169
}
170+
176171
}
177172

178173
// --- CREATE CFG VERIFICATION ---
179174

175+
void RACFED::checkCompileTimeSigAtJump(Module &Md, Function &Fn) {
176+
auto *IntType = llvm::Type::getInt64Ty(Md.getContext());
177+
178+
BasicBlock *ErrBB = BasicBlock::Create(Fn.getContext(), "ErrBB", &Fn);
179+
IRBuilder<> ErrB(ErrBB);
180+
181+
IRBuilder<> B(&*(Fn.front().getFirstInsertionPt()));
182+
183+
Value *RuntimeSig = B.CreateAlloca(IntType);
184+
185+
for(BasicBlock &BB: Fn) {
186+
int randomNumberBB = compileTimeSig.find(&BB)->second;
187+
int subRanPrevVal = subRanPrevVals.find(&BB)->second;
188+
189+
// in this case BB is not the first Basic Block of the function, so it has to
190+
// update RuntimeSig and check it
191+
if (!BB.isEntryBlock()) {
192+
if (isa<LandingPadInst>(BB.getFirstNonPHI()) ||
193+
BB.getName().contains_insensitive("verification")) {
194+
IRBuilder<> BChecker(&*BB.getFirstInsertionPt());
195+
BChecker.CreateStore(llvm::ConstantInt::get(IntType, randomNumberBB),
196+
RuntimeSig, true);
197+
} else if (!BB.getName().contains_insensitive("errbb")) {
198+
BasicBlock *NewBB = BasicBlock::Create(
199+
BB.getContext(), "RACFED_Verification_BB", BB.getParent(), &BB);
200+
IRBuilder<> BChecker(NewBB);
201+
202+
// add instructions for the first runtime signature update
203+
Value *InstrRuntimeSig = BChecker.CreateLoad(IntType, RuntimeSig, true);
204+
205+
Value *RuntimeSignatureVal = BChecker.CreateSub(
206+
InstrRuntimeSig, llvm::ConstantInt::get(IntType, subRanPrevVal));
207+
BChecker.CreateStore(RuntimeSignatureVal, RuntimeSig, true);
208+
209+
// update phi placing them in the new block
210+
while (isa<PHINode>(&BB.front())) {
211+
Instruction *PhiInst = &BB.front();
212+
PhiInst->removeFromParent();
213+
PhiInst->insertBefore(&NewBB->front());
214+
}
215+
216+
// replace the uses of BB with NewBB
217+
for (BasicBlock &BB_ : *BB.getParent()) {
218+
if (&BB_ != NewBB) {
219+
BB_.getTerminator()->replaceSuccessorWith(&BB, NewBB);
220+
}
221+
}
222+
223+
// add instructions for checking the runtime signature
224+
Value *CmpVal =
225+
BChecker.CreateCmp(llvm::CmpInst::ICMP_EQ, RuntimeSignatureVal,
226+
llvm::ConstantInt::get(IntType, randomNumberBB));
227+
BChecker.CreateCondBr(CmpVal, &BB, ErrBB);
228+
229+
// add NewBB and BB into the NewBBs map
230+
NewBBs.insert(std::pair<BasicBlock *, BasicBlock *>(NewBB, &BB));
231+
}
232+
}
233+
}
234+
}
180235

181236

182237
void RACFED::createCFGVerificationBB(
@@ -185,59 +240,15 @@ void RACFED::createCFGVerificationBB(
185240
Value &RetSig, BasicBlock &ErrBB
186241
) {
187242

188-
// checkBeginning() NON ENTRY BLOCK
243+
// checkCompileSigAtJump() NON ENTRY BLOCK
189244
// compileTimeSig - subRunPrevVal
190245
// if compileTimeSig != runtime_sig error()
191246

192-
auto *IntType = llvm::Type::getInt32Ty(BB.getContext());
247+
auto *IntType = llvm::Type::getInt64Ty(BB.getContext());
193248

194249
int randomNumberBB = RandomNumberBBs.find(&BB)->second;
195250
int subRanPrevVal = SubRanPrevVals.find(&BB)->second;
196251

197-
// in this case BB is not the first Basic Block of the function, so it has to
198-
// update RuntimeSig and check it
199-
if (!BB.isEntryBlock()) {
200-
if (isa<LandingPadInst>(BB.getFirstNonPHI()) ||
201-
BB.getName().contains_insensitive("verification")) {
202-
IRBuilder<> BChecker(&*BB.getFirstInsertionPt());
203-
BChecker.CreateStore(llvm::ConstantInt::get(IntType, randomNumberBB),
204-
&RuntimeSig, true);
205-
} else if (!BB.getName().contains_insensitive("errbb")) {
206-
BasicBlock *NewBB = BasicBlock::Create(
207-
BB.getContext(), "RACFED_Verification_BB", BB.getParent(), &BB);
208-
IRBuilder<> BChecker(NewBB);
209-
210-
// add instructions for the first runtime signature update
211-
Value *InstrRuntimeSig = BChecker.CreateLoad(IntType, &RuntimeSig, true);
212-
213-
Value *RuntimeSignatureVal = BChecker.CreateSub(
214-
InstrRuntimeSig, llvm::ConstantInt::get(IntType, subRanPrevVal));
215-
BChecker.CreateStore(RuntimeSignatureVal, &RuntimeSig, true);
216-
217-
// update phi placing them in the new block
218-
while (isa<PHINode>(&BB.front())) {
219-
Instruction *PhiInst = &BB.front();
220-
PhiInst->removeFromParent();
221-
PhiInst->insertBefore(&NewBB->front());
222-
}
223-
224-
// replace the uses of BB with NewBB
225-
for (BasicBlock &BB_ : *BB.getParent()) {
226-
if (&BB_ != NewBB) {
227-
BB_.getTerminator()->replaceSuccessorWith(&BB, NewBB);
228-
}
229-
}
230-
231-
// add instructions for checking the runtime signature
232-
Value *CmpVal =
233-
BChecker.CreateCmp(llvm::CmpInst::ICMP_EQ, RuntimeSignatureVal,
234-
llvm::ConstantInt::get(IntType, randomNumberBB));
235-
BChecker.CreateCondBr(CmpVal, &BB, &ErrBB);
236-
237-
// add NewBB and BB into the NewBBs map
238-
NewBBs.insert(std::pair<BasicBlock *, BasicBlock *>(NewBB, &BB));
239-
}
240-
}
241252

242253
// checkReturnVal()
243254
// compute returnValue
@@ -313,6 +324,7 @@ void RACFED::createCFGVerificationBB(
313324
// }
314325
}
315326

327+
316328
PreservedAnalyses RACFED::run(Module &Md, ModuleAnalysisManager &AM) {
317329
// mappa: istruzione originale -> random r usato per S = S + r
318330
std::unordered_map<llvm::Instruction*, int> InstrUpdates;
@@ -323,10 +335,17 @@ PreservedAnalyses RACFED::run(Module &Md, ModuleAnalysisManager &AM) {
323335
getFuncAnnotations(Md, FuncAnnotations);
324336
LinkageMap linkageMap = mapFunctionLinkageNames((Md));
325337

326-
initializeBlocksSignatures(Md);
338+
for (Function &Fn : Md) {
339+
if (shouldCompile(Fn, FuncAnnotations))
340+
initializeBlocksSignatures(Md, Fn);
341+
}
327342

328-
for (Function &F: Md){
329-
updateCompileSigRandom(F, Md);
343+
for (Function &Fn: Md) {
344+
updateCompileSigRandom(Md, Fn);
345+
}
346+
347+
for(Function &Fn: Md) {
348+
checkCompileTimeSigAtJump(Md, Fn);
330349
}
331350

332351
// createCFGVerificationBB();
@@ -408,4 +427,4 @@ llvmGetPassPluginInfo() {
408427
// }
409428
// }
410429
// return nullptr;
411-
// }
430+
// }

0 commit comments

Comments
 (0)