Skip to content

Commit 6cd9f4d

Browse files
committed
feat(string): allow limiting the length of obfuscated string via an environment variable
1 parent 0d2e488 commit 6cd9f4d

4 files changed

Lines changed: 13 additions & 3 deletions

File tree

Plugin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ bool addPassWithName(FunctionPassManager &FPM, StringRef &passName) {
4545

4646
bool addPassWithName(ModulePassManager &MPM, StringRef &passName) {
4747
if (passName == "string-encryption") {
48-
MPM.addPass(StringObfuscatorPass());
48+
size_t maxSize = 0;
49+
getEnvVar(EnvVarPrefix + "MAX_STRING_LENGTH").getAsInteger(10, maxSize);
50+
MPM.addPass(StringObfuscatorPass{maxSize});
4951
} else {
5052
return false;
5153
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ Or you can run the string encryption pass with:
5555

5656
Refer to the llvm::PassBuilder documentation for more information on each insertion point.
5757

58+
You can limit the length of strings that are obfuscated with the `LLVM_OBF_MAX_STRING_LENGTH` environment variable. A
59+
value of 0 (the default) means no limit.
60+
5861
### With opt
5962

6063
[`opt`](https://llvm.org/docs/CommandGuide/opt.html) can be used to apply specific passes from LLRM-IR you

string/StringObfuscation.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ ConstantDataArray *StringObfuscatorPass::encodeStringDataArray(LLVMContext &ctx,
2828
const char *str,
2929
size_t size,
3030
uint8_t key) {
31+
if (this->maxSize != 0 && size > this->maxSize)
32+
return nullptr;
33+
3134
// Encode the data
3235
char *encodedStr = (char *)malloc(size);
3336
for (unsigned int i = 0; i < size; i++) {
@@ -78,7 +81,7 @@ void StringObfuscatorPass::encodeStructString(LLVMContext &ctx,
7881
}
7982
}
8083

81-
StringObfuscatorPass::StringObfuscatorPass() {}
84+
StringObfuscatorPass::StringObfuscatorPass(size_t maxSize) : maxSize{maxSize} {}
8285

8386
bool StringObfuscatorPass::encodeAllStrings(Module &M) {
8487
auto &ctx = M.getContext();

string/StringObfuscation.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ struct GlobalStringVariable {
3030
}
3131
};
3232

33+
3334
namespace llvm {
3435
struct StringObfuscatorPass : public PassInfoMixin<StringObfuscatorPass> {
3536
std::vector<GlobalStringVariable> globalStrings;
37+
size_t maxSize;
3638

37-
StringObfuscatorPass();
39+
StringObfuscatorPass(size_t maxSize);
3840
ConstantDataArray *encodeStringDataArray(LLVMContext &ctx, const char *str,
3941
size_t size, uint8_t key);
4042
void encodeStructString(LLVMContext &ctx, GlobalVariable *gv,

0 commit comments

Comments
 (0)