Skip to content

Commit 4e4c85b

Browse files
committed
Tested racfed on c tests
Removed some redundant tests Updated testing/README
1 parent bbf651a commit 4e4c85b

File tree

10 files changed

+173
-106
lines changed

10 files changed

+173
-106
lines changed

passes/RACFED.cpp

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
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

1616
using namespace llvm;
1717

@@ -106,9 +106,9 @@ void RACFED::initializeBlocksSignatures(Function &Fn) {
106106
// --- UPDATE SIGNATURE RANDOM ---
107107
void 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,
155155
void 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

237237
Value *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
}

testing/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ This directory contains utilities and scripts for testing ASPIS.
44

55
## Local Testing
66

7-
For local testing, use the `test.py` script. Configure your tests using `test_config.toml`. Configure the llvm_bin flag in `llvm_bin.toml`.
7+
For local testing, use the `test.py` script. Configure your tests using `tests.toml`. Configure the llvm_bin flag in `llvm_bin.toml`.
88
Then run:
99
```bash
1010
pytest test.py
1111
```
1212

13+
> To run pytest the modules listed in requirements.txt must be installed.
14+
> To install the modules:
15+
> - directly install them globally with `pip install -r requirements.txt`
16+
> - use a tool like conda
17+
> - setup a python environment `python -m venv env`
18+
1319
### Writing a configuration file
1420

1521
Test config files must be `.toml` files with the following structure for each test:
@@ -22,7 +28,7 @@ expected_output = <output_expected>
2228
aspis_options = <compilation_flags>
2329
```
2430

25-
> `<relative_path_to_src_file>` will look into `./tests` folder
31+
> `<relative_path_to_src_file>` is a relative path from `./tests/` folder
2632
2733
### Flags
2834

testing/config/racfed-tests.toml

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,104 @@
1+
# ml := multiline
2+
13
[[tests]]
2-
test_name = "multiline_add"
4+
test_name = "racfed_ml_add"
35
source_file = "c/multi_instruction/add.c"
46
expected_output = "30"
57
aspis_options = "--no-dup --racfed"
68

79
[[tests]]
8-
test_name = "multiline_func_ret"
9-
source_file = "c/multi_instruction/func_ret.c"
10+
test_name = "racfed_ml_function_call"
11+
source_file = "c/multi_instruction/function.c"
1012
expected_output = "foo() 25"
1113
aspis_options = "--no-dup --racfed"
14+
15+
[[tests]]
16+
test_name = "racfed_preserve_runtime_sig"
17+
source_file = "c/multi_instruction/call_less_two.c"
18+
expected_output = "0"
19+
aspis_options = "--no-dup --racfed"
20+
21+
[[tests]]
22+
test_name = "racfed_ml_if_then_else"
23+
source_file = "c/multi_instruction/if_then_else.c"
24+
expected_output = "1001"
25+
aspis_options = "--no-dup --racfed"
26+
27+
[[tests]]
28+
test_name = "racfed_ml_phi_instruction"
29+
source_file = "c/multi_instruction/phi.c"
30+
expected_output = "1"
31+
aspis_options = "--no-dup --racfed"
32+
33+
[[tests]]
34+
test_name = "racfed_function_pointer"
35+
source_file = "c/control_flow/function_pointer.c"
36+
expected_output = "42"
37+
aspis_options = "--no-dup --racfed"
38+
39+
[[tests]]
40+
test_name = "racfed_loop_exit"
41+
source_file = "c/control_flow/loop_exit.c"
42+
expected_output = "2"
43+
aspis_options = "--no-dup --racfed"
44+
45+
[[tests]]
46+
test_name = "racfed_nested-branch"
47+
source_file = "c/control_flow/nested-branch.c"
48+
expected_output = "6"
49+
aspis_options = "--no-dup --racfed"
50+
51+
[[tests]]
52+
test_name = "racfed_simple-branch"
53+
source_file = "c/control_flow/simple-branch.c"
54+
expected_output = "OK"
55+
aspis_options = "--no-dup --racfed"
56+
57+
[[tests]]
58+
test_name = "racfed_switch-case"
59+
source_file = "c/control_flow/switch-case.c"
60+
expected_output = "300"
61+
aspis_options = "--no-dup --racfed"
62+
63+
[[tests]]
64+
test_name = "racfed_data_dep_branches"
65+
source_file = "c/data_duplication_integrity/data_dep_branches.c"
66+
expected_output = "7"
67+
aspis_options = "--no-dup --racfed"
68+
69+
[[tests]]
70+
test_name = "racfed_global_var_across_functions"
71+
source_file = "c/data_duplication_integrity/global_var_across_functions.c"
72+
expected_output = "2"
73+
aspis_options = "--no-dup --racfed"
74+
75+
[[tests]]
76+
test_name = "racfed_misc_data_dup"
77+
source_file = "c/data_duplication_integrity/misc_data_dup.c"
78+
expected_output = "OK"
79+
aspis_options = "--no-dup --racfed"
80+
81+
[[tests]]
82+
test_name = "racfed_volatile_io"
83+
source_file = "c/data_duplication_integrity/volatile_io.c"
84+
expected_output = "42"
85+
aspis_options = "--no-dup --racfed"
86+
87+
[[tests]]
88+
test_name = "racfed_arit_pipeline"
89+
source_file = "c/misc_math/arit_pipeline.c"
90+
expected_output = "3"
91+
aspis_options = "--no-dup --racfed"
92+
93+
[[tests]]
94+
test_name = "racfed_mixed_ops"
95+
source_file = "c/misc_math/mixed_ops.c"
96+
expected_output = "14.5"
97+
aspis_options = "--no-dup --racfed"
98+
99+
[[tests]]
100+
test_name = "racfed_xor_cypher"
101+
source_file = "c/misc_math/xor_cypher.c"
102+
expected_output = "SUCCESS"
103+
aspis_options = "--no-dup --racfed"
104+

testing/tests/c/multi_instruction/add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Created by Martina Starone on 24/12/25.
2+
// Created by Gabriele Santandrea on 24/12/25.
33
//
44
#include <stdio.h>
55

testing/tests/c/multi_instruction/func_ret.c

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)