Skip to content

Commit 0f8a626

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

3 files changed

Lines changed: 10 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
}

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)