Skip to content

Commit 0d2e488

Browse files
committed
string: obfuscate all byte arrays with alignment of 1
- Remove null-terminated check in `encodeStringDataArray`. - Filter globals to only obfuscate those with an alignment of 1. Not doing that lead to crashes of the compiler. - Use `isString()` instead of `isCString()` to include more byte arrays.
1 parent ddc0fec commit 0d2e488

1 file changed

Lines changed: 7 additions & 15 deletions

File tree

string/StringObfuscation.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ ConstantDataArray *StringObfuscatorPass::encodeStringDataArray(LLVMContext &ctx,
2828
const char *str,
2929
size_t size,
3030
uint8_t key) {
31-
// Check this is a valid string (not containing zeros)
32-
if (str[size - 1] == '\0') {
33-
if (strnlen(str, size) != size - 1)
34-
return nullptr;
35-
} else {
36-
if (strnlen(str, size) != size)
37-
return nullptr;
38-
}
39-
4031
// Encode the data
4132
char *encodedStr = (char *)malloc(size);
4233
for (unsigned int i = 0; i < size; i++) {
@@ -94,10 +85,11 @@ bool StringObfuscatorPass::encodeAllStrings(Module &M) {
9485

9586
// For each global variable
9687
for (GlobalVariable &gv : M.globals()) {
97-
if (!gv.isConstant() // constant
98-
|| !gv.hasInitializer() // unitialized
99-
|| gv.hasExternalLinkage() // external
100-
|| gv.getSection() == "llvm.metadata") { // Intrinsic Global Variables
88+
if (!gv.isConstant() // constant
89+
|| !gv.hasInitializer() // unitialized
90+
|| gv.hasExternalLinkage() // external
91+
|| gv.getAlign().valueOrOne().value() != 1 // align == 1
92+
|| gv.getSection() == "llvm.metadata") { // Intrinsic Global Variables
10193
//|| gv.getSection().find("__objc_methname") != string::npos) { // TODO :
10294
// is this necessary ?
10395
continue;
@@ -109,7 +101,7 @@ bool StringObfuscatorPass::encodeAllStrings(Module &M) {
109101
// Encode the value and update the variable
110102
if (isa<ConstantDataArray>(initializer)) { // Global variable
111103
auto array = cast<ConstantDataArray>(initializer);
112-
if (array->isCString()) {
104+
if (array->isString()) {
113105
encodeGlobalString(ctx, &gv, array);
114106
}
115107
} else if (isa<ConstantStruct>(initializer)) { // Variable in a struct
@@ -118,7 +110,7 @@ bool StringObfuscatorPass::encodeAllStrings(Module &M) {
118110
auto operand = cs->getOperand(i);
119111
if (isa<ConstantDataArray>(operand)) {
120112
auto array = cast<ConstantDataArray>(operand);
121-
if (array->isCString()) {
113+
if (array->isString()) {
122114
encodeStructString(ctx, &gv, cs, array, i);
123115
}
124116
}

0 commit comments

Comments
 (0)