diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index fd2dbe4e4..e729e7f62 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -17,8 +17,6 @@ jobs: run: sudo apt install ninja-build -y - name: Install z3 run: sudo apt install libz3-dev -y - - name: Install bitcoin dependencies - run: sudo apt install build-essential libtool autotools-dev automake pkg-config bsdmainutils python3 libevent-dev libboost-dev -y - name: git submodule update --init run: git submodule update --init - name: make install @@ -27,10 +25,10 @@ jobs: run: ./tests/test-aes.sh - name: Test jsoncpp run: ./tests/test-jsoncpp.sh + - name: Test json + run: ./tests/test-json.sh - name: Test openssl run: ./tests/test-openssl.sh - - name: Test bitcoin - run: ./tests/test-bitcoin.sh # Windows: # runs-on: windows-latest diff --git a/.gitmodules b/.gitmodules index 742c521ec..8c3f4b41d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,9 +4,9 @@ [submodule "tests/openssl"] path = tests/openssl url = https://github.com/openssl/openssl.git -[submodule "tests/bitcoin"] - path = tests/bitcoin - url = https://github.com/bitcoin/bitcoin.git [submodule "tests/jsoncpp"] path = tests/jsoncpp url = https://github.com/open-source-parsers/jsoncpp.git +[submodule "tests/json"] + path = tests/json + url = https://github.com/nlohmann/json.git diff --git a/llvm/lib/Transforms/Obfuscation/MBAObfuscation.cpp b/llvm/lib/Transforms/Obfuscation/MBAObfuscation.cpp index 9a164cebf..b650c4854 100644 --- a/llvm/lib/Transforms/Obfuscation/MBAObfuscation.cpp +++ b/llvm/lib/Transforms/Obfuscation/MBAObfuscation.cpp @@ -10,7 +10,7 @@ using namespace std; using namespace llvm; using namespace MBAUtils; -#define NUM_TERMS 10 +#define NUM_COEFFS 10 PreservedAnalyses Pluto::MbaObfuscation::run(Function &F, FunctionAnalysisManager &AM) { for (BasicBlock &BB : F) { @@ -47,9 +47,9 @@ PreservedAnalyses Pluto::MbaObfuscation::run(Function &F, FunctionAnalysisManage void Pluto::MbaObfuscation::substituteConstant(Instruction *I, int i) { ConstantInt *val = dyn_cast(I->getOperand(i)); if (val && val->getBitWidth() <= 64) { - int64_t *terms = generateLinearMBA(NUM_TERMS); - terms[14] -= val->getValue().getZExtValue(); - Value *mbaExpr = insertLinearMBA(terms, I); + int64_t *coeffs = generateLinearMBA(NUM_COEFFS); + coeffs[14] -= val->getValue().getZExtValue(); + Value *mbaExpr = insertLinearMBA(coeffs, I); if (val->getBitWidth() <= 32) { mbaExpr = insertPolynomialMBA(mbaExpr, I); } @@ -87,33 +87,33 @@ void Pluto::MbaObfuscation::substitute(BinaryOperator *BI) { } Value *Pluto::MbaObfuscation::substituteAdd(BinaryOperator *BI) { - int64_t *terms = generateLinearMBA(NUM_TERMS); - terms[2] += 1; - terms[4] += 1; - return insertLinearMBA(terms, BI); + int64_t *coeffs = generateLinearMBA(NUM_COEFFS); + coeffs[2] += 1; + coeffs[4] += 1; + return insertLinearMBA(coeffs, BI); } Value *Pluto::MbaObfuscation::substituteSub(BinaryOperator *BI) { - int64_t *terms = generateLinearMBA(NUM_TERMS); - terms[2] += 1; - terms[4] -= 1; - return insertLinearMBA(terms, BI); + int64_t *coeffs = generateLinearMBA(NUM_COEFFS); + coeffs[2] += 1; + coeffs[4] -= 1; + return insertLinearMBA(coeffs, BI); } Value *Pluto::MbaObfuscation::substituteXor(BinaryOperator *BI) { - int64_t *terms = generateLinearMBA(NUM_TERMS); - terms[5] += 1; - return insertLinearMBA(terms, BI); + int64_t *coeffs = generateLinearMBA(NUM_COEFFS); + coeffs[5] += 1; + return insertLinearMBA(coeffs, BI); } Value *Pluto::MbaObfuscation::substituteAnd(BinaryOperator *BI) { - int64_t *terms = generateLinearMBA(NUM_TERMS); - terms[0] += 1; - return insertLinearMBA(terms, BI); + int64_t *coeffs = generateLinearMBA(NUM_COEFFS); + coeffs[0] += 1; + return insertLinearMBA(coeffs, BI); } Value *Pluto::MbaObfuscation::substituteOr(BinaryOperator *BI) { - int64_t *terms = generateLinearMBA(NUM_TERMS); - terms[6] += 1; - return insertLinearMBA(terms, BI); + int64_t *coeffs = generateLinearMBA(NUM_COEFFS); + coeffs[6] += 1; + return insertLinearMBA(coeffs, BI); } \ No newline at end of file diff --git a/llvm/lib/Transforms/Obfuscation/MBAUtils.cpp b/llvm/lib/Transforms/Obfuscation/MBAUtils.cpp index 36aa5c557..ffd835cfd 100644 --- a/llvm/lib/Transforms/Obfuscation/MBAUtils.cpp +++ b/llvm/lib/Transforms/Obfuscation/MBAUtils.cpp @@ -5,9 +5,12 @@ #include "llvm/Transforms/Obfuscation/CryptoUtils.h" #include #include +#include #include #include +#define USE_CACHE + using namespace z3; using namespace llvm; @@ -30,6 +33,19 @@ static int8_t truthTables[15][4] = { }; int64_t *MBAUtils::generateLinearMBA(int numExprs) { +#ifdef USE_CACHE + static std::queue cached_coeffs; + if (cached_coeffs.size() && cryptoutils->get_range(10) < 8) { + int64_t *coeffs = cached_coeffs.front(); + outs() << "[DEBUG] Use cached coefficients:"; + for (int i = 0; i < 15; i++) { + outs() << " " << coeffs[i]; + } + outs() << "\n"; + cached_coeffs.pop(); + return coeffs; + } +#endif int *exprs = new int[numExprs]; int64_t *coeffs = new int64_t[15]; while (true) { @@ -42,7 +58,7 @@ int64_t *MBAUtils::generateLinearMBA(int numExprs) { X.push_back(c.int_const(name.c_str())); } for (int i = 0; i < numExprs; i++) { - exprs[i] = rand() % 15; + exprs[i] = cryptoutils->get_range(15); } for (int i = 0; i < 4; i++) { expr equ = c.int_val(0); @@ -65,6 +81,13 @@ int64_t *MBAUtils::generateLinearMBA(int numExprs) { coeffs[exprs[i]] += m.eval(X[i]).as_int64(); } delete[] exprs; +#ifdef USE_CACHE + if (cached_coeffs.size() < 10) { + int64_t *coeffs_copy = new int64_t[15]; + std::copy(coeffs, coeffs + 15, coeffs_copy); + cached_coeffs.push(coeffs_copy); + } +#endif return coeffs; } } diff --git a/tests/bitcoin b/tests/bitcoin deleted file mode 160000 index 4b1196a98..000000000 --- a/tests/bitcoin +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4b1196a9855dcd188a24f393aa2fa21e2d61f061 diff --git a/tests/json b/tests/json new file mode 160000 index 000000000..a259ecc51 --- /dev/null +++ b/tests/json @@ -0,0 +1 @@ +Subproject commit a259ecc51e1951e12f757ce17db958e9881e9c6c diff --git a/tests/test-bitcoin.sh b/tests/test-bitcoin.sh deleted file mode 100755 index f1f480f8c..000000000 --- a/tests/test-bitcoin.sh +++ /dev/null @@ -1,9 +0,0 @@ -CC=`pwd`/install/bin/clang -CXX=`pwd`/install/bin/clang++ -FLAGS="-mllvm -passes=hlw,fla,mba" -cd tests/bitcoin -./autogen.sh -./configure CC=$CC CXX=$CXX CXXFLAGS=$FLAGS -make clean -make -j`nproc` -make check \ No newline at end of file diff --git a/tests/test-json.sh b/tests/test-json.sh new file mode 100755 index 000000000..1f49d24de --- /dev/null +++ b/tests/test-json.sh @@ -0,0 +1,12 @@ +CXX=`pwd`/install/bin/clang++ +CXX_FLAGS="-mllvm -passes=hlw,fla,mba" + +cd tests/json +rm -rf build +mkdir -p build +cmake -B build \ + -G "Ninja" \ + -DCMAKE_CXX_COMPILER=$CXX \ + -DCMAKE_CXX_FLAGS="$CXX_FLAGS" +ninja -j`nproc` -C build +ninja test \ No newline at end of file