Skip to content

Commit

Permalink
Support global string initializer as part of constant expressions
Browse files Browse the repository at this point in the history
When encoding strings with `StringEncOptGlobal` option, take into
account those appearing as part of global initializers of constant
expressions, used by global variables.
  • Loading branch information
antoniofrighetto committed Jun 10, 2024
1 parent 4a2b42e commit daf92b1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/passes/strings-encoding/StringEncoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,29 @@ bool StringEncoding::runOnBasicBlock(Module& M, Function& F, BasicBlock& BB,
G = extractGlobalVariable(CE);
}

if (!G)
continue;
auto IsInitializerConstantExpr = [](const GlobalVariable &G) {
return (!G.isExternallyInitialized() && G.hasInitializer()) &&
isa<ConstantExpr>(G.getInitializer());
};

Use *ActualOp = &Op;
bool MaybeStringInCEInitializer = false;
if (G && IsInitializerConstantExpr(*G)) {
// Is the global initializer part of a constant expression?
G = extractGlobalVariable(cast<ConstantExpr>(G->getInitializer()));
ActualOp = G->getSingleUndroppableUse();
MaybeStringInCEInitializer = true;
}

if (!isEligible(*G)) {
if (!G || !ActualOp)
continue;
}

if (!isEligible(*G))
continue;

auto* data = dyn_cast<ConstantDataSequential>(G->getInitializer());
if (data == nullptr) {
if (data == nullptr)
continue;
}

// Create a default option which skips the encoding
auto encInfo = std::make_unique<StringEncodingOpt>(StringEncOptSkip());
Expand All @@ -150,11 +161,12 @@ bool StringEncoding::runOnBasicBlock(Module& M, Function& F, BasicBlock& BB,
encInfo = std::make_unique<StringEncodingOpt>(userConfig.obfuscate_string(&M, &F, data->getAsCString().str()));
}

if (isSkip(*encInfo)) {
if (isSkip(*encInfo) ||
(MaybeStringInCEInitializer &&
std::get_if<StringEncOptGlobal>(encInfo.get()) == nullptr))
continue;
}

Changed |= process(BB, I, Op, *G, *data, *encInfo);
Changed |= process(BB, I, *ActualOp, *G, *data, *encInfo);
}
}
return Changed;
Expand All @@ -171,12 +183,16 @@ PreservedAnalyses StringEncoding::run(Module &M, ModuleAnalysisManager &MAM) {
RNG_ = M.createRNG(name());
SDEBUG("[{}] Module: {}", name(), M.getSourceFileName());

for (Function &F : M) {
demotePHINode(F);
StringRef name = F.getName();
std::vector<Function *> FuncsToVisit;
for (Function &F : M)
FuncsToVisit.emplace_back(&F);

for (auto *F : FuncsToVisit) {
demotePHINode(*F);
StringRef name = F->getName();
std::string demangled = demangle(name.str());
for (BasicBlock &BB : F) {
Changed |= runOnBasicBlock(M, F, BB, *userConfig);
for (BasicBlock &BB : *F) {
Changed |= runOnBasicBlock(M, *F, BB, *userConfig);
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/passes/strings-encoding/basic-aarch64-ce-init.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; REQUIRES: aarch64-registered-target

; RUN: env OMVLL_CONFIG=%S/config_replace.py clang++ -fpass-plugin=%libOMVLL \
; RUN: -target arm64-apple-ios -fno-legacy-pass-manager -O1 -c %s -o - | strings | FileCheck %s
;
; RUN: env OMVLL_CONFIG=%S/config_replace.py clang++ -fpass-plugin=%libOMVLL \
; RUN: -target aarch64-linux-android -fno-legacy-pass-manager -O1 -c %s -o - | strings | FileCheck %s
;
; CHECK-NOT: {{.*Hello.*}}

@.hello = private constant [6 x i8] c"Hello\00"
@str = global i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.hello, i32 0, i32 0)

define i32 @main() #0 {
%load = load i8*, i8** @str, align 8
%call = call i32 @puts(i8* %load)
ret i32 0
}

declare i32 @puts(i8*)
2 changes: 2 additions & 0 deletions src/test/passes/strings-encoding/config_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class MyConfig(omvll.ObfuscationConfig):
def __init__(self):
super().__init__()
def obfuscate_string(self, _, __, string: bytes):
if string.endswith(b"Hello"):
return omvll.StringEncOptGlobal()
if string.endswith(b".cpp"):
return omvll.StringEncOptGlobal()
if string.endswith(b"Swift"):
Expand Down

0 comments on commit daf92b1

Please sign in to comment.