Skip to content

Commit

Permalink
Do not try opaque constants when handling infinite loops
Browse files Browse the repository at this point in the history
We should not break the correctness of the program, even when the
compiler may be able to remove infinite loops with no side-effects.
As constants get reconstructed with a stack-allocated variable, do
not conceal loop-variant constants when possibly in presence of
infinite loops; this otherwise could lead to stack-overflow issues.

Fixes: open-obfuscator#39.
  • Loading branch information
antoniofrighetto committed Apr 15, 2024
1 parent c45fe12 commit 6030d75
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/passes/opaque-constants/OpaqueConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ PreservedAnalyses OpaqueConstants::run(Module &M,
}

for (BasicBlock& BB : F) {
// Don't try opaque constants when potentially handling infinite loops.
if (is_contained(successors(&BB), &BB))
continue;
Changed |= runOnBasicBlock(BB, inserted);
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/passes/opaque-constants/config_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import omvll
from functools import lru_cache

class MyConfig(omvll.ObfuscationConfig):
def __init__(self):
super().__init__()
def obfuscate_constants(self, mod: omvll.Module, func: omvll.Function):
return True

@lru_cache(maxsize=1)
def omvll_get_config() -> omvll.ObfuscationConfig:
return MyConfig()
25 changes: 25 additions & 0 deletions src/test/passes/opaque-constants/opaque-constants-infinite-loop.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
; RUN: env OMVLL_CONFIG=%S/config_all.py clang -target aarch64-linux-android -fno-legacy-pass-manager -fpass-plugin=%libOMVLL -O0 -S -emit-llvm %s -o - | FileCheck %s --check-prefix=O0
; RUN: env OMVLL_CONFIG=%S/config_all.py clang -target aarch64-linux-android -fno-legacy-pass-manager -fpass-plugin=%libOMVLL -O1 -S -emit-llvm %s -o - | FileCheck %s --check-prefix=O1
; RUN: env OMVLL_CONFIG=%S/config_all.py clang -target arm64-apple-ios -fno-legacy-pass-manager -fpass-plugin=%libOMVLL -O0 -S -emit-llvm %s -o - | FileCheck %s --check-prefix=O0
; RUN: env OMVLL_CONFIG=%S/config_all.py clang -target arm64-apple-ios -fno-legacy-pass-manager -fpass-plugin=%libOMVLL -O1 -S -emit-llvm %s -o - | FileCheck %s --check-prefix=O1

define void @opaque_infinite_loop() {
; O0-LABEL: @opaque_infinite_loop
; O0: loop
; O0: %loaded_i = load i32, i32* %i, align 4
; O0-NEXT: %add = add nsw i32 %loaded_i, 1
; O0-NEXT: store i32 %add, i32* %i, align 4
; O0-NEXT: br label %loop
; O1-LABEL: @opaque_infinite_loop
; O1: loop
; O1: br label %loop
entry:
%i = alloca i32, align 4
store i32 0, i32* %i, align 4
br label %loop
loop: ; preds = %loop, %entry
%loaded_i = load i32, i32* %i, align 4
%add = add nsw i32 %loaded_i, 1
store i32 %add, i32* %i, align 4
br label %loop
}

0 comments on commit 6030d75

Please sign in to comment.