microsoft / llvm-mctoll

llvm-mctoll
Other
816 stars 125 forks source link

[X86-64] Use `getRaisedFunction()` to insert a new BasicBlock #130

Closed martin-fink closed 3 years ago

martin-fink commented 3 years ago

If a function's return type gets changed, but there are still BB's left to be inserted, CurFunction will not point to the modified function. This commit fixes this by always querying for the current function.

The added test case compiles to the following:

assert_func:                            # @assert_func
        push    rax
        cmp     dword ptr [rdi], 0
        je      .LBB0_1
        cmp     qword ptr [rdi + 8], 0
        je      .LBB0_4
        pop     rax
        ret
.LBB0_1:
        mov     edi, offset .Lstr
        jmp     .LBB0_2
.LBB0_4:
        mov     edi, offset .Lstr.4
.LBB0_2:
        call    puts@PLT
        mov     edi, 1
        call    exit

Initially, llvm-mctoll assumes that this is a function returning i64, as rax is defined before the ret instruction. However, when raising the function, mctoll adjusts the return type to void, triggering incorrect behavior, as LBB0_1 and onwards still need to be raised.