Skip to content

Commit

Permalink
Fix CFF, add Test and improve TestAll
Browse files Browse the repository at this point in the history
  • Loading branch information
Slattz committed Apr 11, 2023
1 parent 46dd82f commit 24fe099
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ execute_process(
OUTPUT_STRIP_TRAILING_WHITESPACE
)

add_definitions(-DOBFUSC_VERSION_MAJOR=0 -DOBFUSC_VERSION_MINOR=1 -DOBFUSC_VERSION_MICRO=0 -DOBFUSC_GIT_REV=${GIT_COMMIT_HASH})
add_definitions(-DOBFUSC_VERSION_MAJOR=0 -DOBFUSC_VERSION_MINOR=2 -DOBFUSC_VERSION_MICRO=0 -DOBFUSC_GIT_REV=${GIT_COMMIT_HASH})


# Add Target and source dir
Expand Down
9 changes: 4 additions & 5 deletions source/CFF/CffPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ namespace obfusc {
CffPass::~CffPass() {}

bool CffPass::obfuscate(llvm::Module& mod, llvm::Function& func) {
// Lower switch
llvm::createLowerSwitchPass()->runOnFunction(func);

// Copy original blocks
std::vector<llvm::BasicBlock *> origBB;
for (auto& block : func) {
if (isa<llvm::InvokeInst>(block.getTerminator())) {
Expand Down Expand Up @@ -56,7 +54,7 @@ namespace obfusc {

// Create switch variable and set as it
llvm::AllocaInst* switchVar = new llvm::AllocaInst(llvm::Type::getInt32Ty(func.getContext()), 0, "switchVar", insert);
new llvm::StoreInst(llvm::ConstantInt::get(llvm::Type::getInt32Ty(func.getContext()), m_randGen64()), switchVar, insert);
new llvm::StoreInst(llvm::ConstantInt::get(llvm::Type::getInt32Ty(func.getContext()), 0), switchVar, insert);

// Create main loop
llvm::BasicBlock* loopEntry = llvm::BasicBlock::Create(func.getContext(), "loopEntry", &func, insert);
Expand Down Expand Up @@ -147,6 +145,7 @@ namespace obfusc {
}

fixStack(func);
//func.viewCFG();

return true;
}
Expand Down Expand Up @@ -195,7 +194,7 @@ namespace obfusc {
llvm::DemotePHIToStack(phi, func.begin()->getTerminator());
}

if (tmpReg.size() != 0 || tmpPhi.size() != 0) {
if (tmpReg.size() == 0 || tmpPhi.size() == 0) {
break;
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/TestAll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@ namespace MbaTest {
void MbaTestAll();
}

namespace CffTest {
void CffTestAll();
}

namespace iSubTest {
void iSubTestAll();
}

namespace BcfTest {
void BcfTestAll();
}

#ifdef OBFUSC_TEST_BUILD_ALL

int main(int argc, char *argv[]) {
MbaTest::MbaTestAll();
CffTest::CffTestAll();
iSubTest::iSubTestAll();
BcfTest::BcfTestAll();
return 0;
}

Expand Down
30 changes: 30 additions & 0 deletions tests/cffTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <cstdio>
#include <cstdint>
#include <cassert>

namespace CffTest {

[[obfusc::cff]] int CffTestVal(int numLoops) {
int ret = 0;
for (int i = 0; i < numLoops; i++) {
ret++;
}

return ret;
}


void CffTestAll() {
int ret = CffTestVal(100);
printf("Cff Ret: %d\n", ret);
}
}

#ifndef OBFUSC_TEST_BUILD_ALL

int main(int argc, char *argv[]) {
CffTest::CffTestAll();
return 0;
}

#endif

0 comments on commit 24fe099

Please sign in to comment.