Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SplitBlockAndInsertIfThenElse caused label changes to entry instructions in predecessor #123388

Open
Casternum opened this issue Jan 17, 2025 · 1 comment

Comments

@Casternum
Copy link

Writing a pass. Using LLVM 20.0.0git, with opt command as follows:

/usr/local/llvm/bin/opt --relocation-model=pic -load-pass-plugin ./libLLVMObfuscator.so -passes="XXX" tr2.bc -o tr2_obf.bc

There are two basic blocks, each branching to a third BB that will handle the split. Each of the two predecessors begin with the same instruction (they were clones of each other to begin with). Their first instructions in IR are %39 trunc i64 %38 to i32, and %128 trunc i64 %38 to i32. I ran an errs() on both predecessor before and after the SplitBlockAndInsertIfThenElse instruction. After the split the entry instructions remained the same, but BOTH labels increased to %40 and %129, respectively! But, the split does not seem to involve the predecessors, and I get an error:

Basic Block in function 'get_next' does not have terminator!
label %39
LLVM ERROR: Broken module found, compilation aborted!

The crash backtrace:
Stack dump:
0. Program arguments: /usr/local/llvm/bin/opt --relocation-model=pic -load-pass-plugin ./libLLVMObfuscator.so -passes=dopseq tr2.bc -o tr2_obf.bc

  1. Running pass "verify" on module "tr2.bc"
    #0 0x000079f643a1ce30 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/local/llvm/bin/../lib/libLLVMSupport.so.20.0git+0x21ce30)
    Fixing Rust build #1 0x000079f643a1a3ee SignalHandler(int) Signals.cpp:0:0
    Fix a typo #2 0x000079f643445320 (/lib/x86_64-linux-gnu/libc.so.6+0x45320)
    Fix a compilation error on gcc8 #3 0x000079f64349eb1c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
    Fix a compilation error on gcc8 #4 0x000079f64349eb1c __pthread_kill_internal ./nptl/pthread_kill.c:78:10
    Seedarg/member/wip #5 0x000079f64349eb1c pthread_kill ./nptl/pthread_kill.c:89:10
    Add check for matching HeaderFilter before emitting Diagnostic #6 0x000079f64344526e raise ./signal/../sysdeps/posix/raise.c:27:6
    Random engine swap #7 0x000079f6434288ff abort ./stdlib/abort.c:81:7
    Update rwlock init #8 0x000079f6439325a3 (/usr/local/llvm/bin/../lib/libLLVMSupport.so.20.0git+0x1325a3)
    Next80 merge test #9 0x000079f6439327ae (/usr/local/llvm/bin/../lib/libLLVMSupport.so.20.0git+0x1327ae)
    Fix typo: 'soruce' -> 'source' #10 0x000079f63c05102e (/usr/local/llvm/bin/../lib/../lib/libLLVMCore.so.20.0git+0x45102e)
    fix for gcc 8 #11 0x000079f643bdd856 llvm::detail::PassModel<llvm::Module, llvm::VerifierPass, llvm::AnalysisManagerllvm::Module>::run(llvm::Module&, llvm::AnalysisManagerllvm::Module&) NewPMDriver.cpp:0:0
    Fix "Allman" style example #12 0x000079f63c002ca8 llvm::PassManager<llvm::Module, llvm::AnalysisManagerllvm::Module>::run(llvm::Module&, llvm::AnalysisManagerllvm::Module&) (/usr/local/llvm/bin/../lib/../lib/libLLVMCore.so.20.0git+0x402ca8)
    Defer diag #13 0x000079f643beb96a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRefllvm::PassPlugin, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (/usr/local/llvm/bin/../lib/libLLVMOptDriver.so.20.0git+0x2596a)
    WIP lifetime categories #14 0x000079f643bfaa9c optMain (/usr/local/llvm/bin/../lib/libLLVMOptDriver.so.20.0git+0x34a9c)
    Fix ClangOpenCLBuiltinsImpl target not found #15 0x000079f64342a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
    __unique_stable_name implementation. #16 0x000079f64342a28b call_init ./csu/../csu/libc-start.c:128:20
    Lockdown test #17 0x000079f64342a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
    Lockdown test #18 0x0000618c0ec62845 _start (/usr/local/llvm/bin/opt+0x1845)
    Aborted (core dumped)

I'm stumped as to why this happened, and need a fix. Any help would be greatly appreciated. Thanks in advance
ps. I am a novice coder, but the logic seems solid, just this final implementation detail.

@Casternum
Copy link
Author

The instruction that seems to be causing the problem is:

SplitBlockAndInsertIfThenElse(cond, itr_end_prev, &ptr_then, &ptr_else); which seems to work fine based on the resulting contents of XXX_BB, XXX_Then, and XXX_Else.

Here, cond is defined by Value * cond = new ICmpInst(XXXBB, CmpInst::ICMP_EQ , d1_rv, d2_rv, *var31);, where XXX_BB is the BB to be split, d1_rv and d2_rv are ConstantInt, and var3 is a twine.
itr_end_prev is an iterator that points to the only instruction in XXX_BB, which is actually cond and is not needed after the split. ptr_then and ptr_else are pointers to basic blocks XXX_Then and XXX_Else, each of which has conditional branches as terminators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants