Skip to content

Commit

Permalink
feat: add Mod3 bitrep
Browse files Browse the repository at this point in the history
  • Loading branch information
dtcxzyw committed Oct 12, 2023
1 parent c916abe commit 7f42d30
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
44 changes: 44 additions & 0 deletions BitRep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,48 @@ struct InvInt1BitRep final : public BitRepBase {
}
};

struct Mod3BitRep final : public BitRepBase {
explicit Mod3BitRep(IRBuilder<> &Builder) : BitRepBase(Builder) {}

Type *getBitTy() override { return Builder.getInt32Ty(); }
Constant *getBit0() override { return Builder.getInt32(1); }
Constant *getBit1() override { return Builder.getInt32(2); }

// handle vector of i1
Value *convertToBit(Value *V) override {
auto *VT = V->getType()->getWithNewType(getBitTy());
return Builder.CreateSelect(V, ConstantInt::get(VT, 2),
ConstantInt::get(VT, 1));
}
// handle vector of bitTy
Value *convertFromBit(Value *V) override {
return Builder.CreateICmpSGT(V, ConstantInt::get(V->getType(), 1));
}

Value *bitNot(Value *V) override {
return Builder.CreateURem(Builder.CreateShl(V, 1),
ConstantInt::get(V->getType(), 3));
}
Value *bitXor(Value *V1, Value *V2) override {
return Builder.CreateURem(Builder.CreateMul(V1, V2),
ConstantInt::get(V1->getType(), 3));
}
Value *bitOr(Value *V1, Value *V2) override {
auto *One = ConstantInt::get(V1->getType(), 1);
return Builder.CreateSub(
ConstantInt::get(V1->getType(), 2),
Builder.CreateURem(Builder.CreateMul(Builder.CreateAdd(V1, One),
Builder.CreateAdd(V2, One)),
ConstantInt::get(V1->getType(), 3)));
}
Value *bitAnd(Value *V1, Value *V2) override {
auto *One = ConstantInt::get(V1->getType(), 1);
return Builder.CreateAdd(Builder.CreateMul(Builder.CreateSub(V1, One),
Builder.CreateSub(V2, One)),
One);
}
};

std::unique_ptr<BitRepBase> BitRepBase::createBitRep(IRBuilder<> &Builder,
BitRepMethod Method) {
switch (Method) {
Expand All @@ -115,6 +157,8 @@ std::unique_ptr<BitRepBase> BitRepBase::createBitRep(IRBuilder<> &Builder,
return std::make_unique<Int1BitRep>(Builder);
case InvInt1:
return std::make_unique<InvInt1BitRep>(Builder);
case Mod3:
return std::make_unique<Mod3BitRep>(Builder);
default:
llvm_unreachable("Unexpected bit representation method");
}
Expand Down
1 change: 1 addition & 0 deletions BitRep.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum BitRepMethod {
FSub,
Int1,
InvInt1,
Mod3,

DefaultBitRep = FSub,
};
Expand Down
2 changes: 2 additions & 0 deletions BitRepTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ TEST_F(BinRepTest, MethodInvInt1) {

TEST_F(BinRepTest, MethodFSub) { testBitRep(Builder, BitRepMethod::FSub); }

TEST_F(BinRepTest, MethodMod3) { testBitRep(Builder, BitRepMethod::Mod3); }

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
InitLLVM Init{argc, argv};
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ if (${FSUBFUSCATOR_ENABLE_TESTS})

add_test(NAME test_standalone_int1 COMMAND sh -c "${FSUBFUSCATOR_EXECUTABLE} -S -bitrep=Int1 test.ll -o ${CMAKE_BINARY_DIR}/test_int1.ll && ${ALIVE2} test.ll ${CMAKE_BINARY_DIR}/test_int1.ll" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_test(NAME test_standalone_invint1 COMMAND sh -c "${FSUBFUSCATOR_EXECUTABLE} -S -bitrep=InvInt1 test.ll -o ${CMAKE_BINARY_DIR}/test_invint1.ll && ${ALIVE2} test.ll ${CMAKE_BINARY_DIR}/test_invint1.ll" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_test(NAME test_standalone_mod3 COMMAND sh -c "${FSUBFUSCATOR_EXECUTABLE} -S -bitrep=Mod3 test.ll -o ${CMAKE_BINARY_DIR}/test_mod3.ll" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_test(NAME test_standalone_fsub COMMAND ${FSUBFUSCATOR_EXECUTABLE} -S -bitrep=FSub test.ll -o ${CMAKE_BINARY_DIR}/test_fsub.ll WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_test(NAME test_opt COMMAND ${OPT} -O3 -S -load-pass-plugin ${FSUBFUSCATOR_PLUGIN} test.ll -o ${CMAKE_BINARY_DIR}/test_opt.ll WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_test(NAME test_clang COMMAND ${CLANG} -O3 -c -emit-llvm -S -fpass-plugin=${FSUBFUSCATOR_PLUGIN} test.c -o ${CMAKE_BINARY_DIR}/test_clang.ll WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down
4 changes: 3 additions & 1 deletion FSubFuscatorPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ static cl::opt<BitRepMethod> RepMethod(
"bitrep", cl::desc("Bit representation to use"),
cl::values(clEnumVal(FSub, "Default: Use fsub and f32. (T=0.0, F=-0.0)"),
clEnumVal(Int1, "Use bitwise and i1. (T=true, F=false)"),
clEnumVal(InvInt1, "Use bitwise and i1. (T=false, F=true)")),
clEnumVal(InvInt1, "Use bitwise and i1. (T=false, F=true)"),
clEnumVal(Mod3, "Use mod and i32. (T=2, F=1)")),
cl::init(DefaultBitRep), cl::cat(FsubFuscatorCategory));

class BitFuscatorImpl final : public InstVisitor<BitFuscatorImpl, Value *> {
Expand Down Expand Up @@ -765,6 +766,7 @@ class BitFuscatorImpl final : public InstVisitor<BitFuscatorImpl, Value *> {
.Case("Int1", BitRepMethod::Int1)
.Case("InvInt1", BitRepMethod::InvInt1)
.Case("FSub", BitRepMethod::FSub)
.Case("Mod3", BitRepMethod::Mod3)
.Default(BitRepMethod::DefaultBitRep);
}
return RepMethod;
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ configure && make ...
# Use wrapper in your project with CMake
cmake -DCMAKE_C_COMPILER=<path to build>/fsubcc -DCMAKE_CXX_COMPILER=<path to build>/fsub++ ...
# To use other bit representation instead of fsub
./fsubfuscator -bitrep=<FSub|Int1|InvInt1> test.ll -S -o out.ll
./fsubfuscator -bitrep=<FSub|Int1|InvInt1|Mod3> test.ll -S -o out.ll
# or use the environment variable (highest priority) to pass the parameter to clang
export FSUBFUSCATOR_BITREP_OVERRIDE=<FSub|Int1|InvInt1>
export FSUBFUSCATOR_BITREP_OVERRIDE=<FSub|Int1|InvInt1|Mod3>
./fsubcc ...
```

Expand Down

0 comments on commit 7f42d30

Please sign in to comment.