Skip to content

Commit cfc1a66

Browse files
aykevldeadprogram
authored andcommitted
interp: use correct initialization order on panic() calls
Whenever interp hits an unreachable instruction, it bails out at that point. However, it used to insert new instructions at the bottom with the old init calls still at the top. So when a panic() happened in a non-main package, the last packages to init would actually be called first. This commit fixes this by setting the insert point at the top of runtime.initAll before starting interpretation, so the initialization order is still correct when a panic() happens during init.
1 parent 4ad9bd8 commit cfc1a66

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

interp/interp.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@ func Run(mod llvm.Module, targetData llvm.TargetData, debug bool) error {
4242

4343
initAll := mod.NamedFunction(name)
4444
bb := initAll.EntryBasicBlock()
45-
e.builder.SetInsertPointBefore(bb.LastInstruction())
45+
// Create a dummy alloca in the entry block that we can set the insert point
46+
// to. This is necessary because otherwise we might be removing the
47+
// instruction (init call) that we are removing after successful
48+
// interpretation.
49+
e.builder.SetInsertPointBefore(bb.FirstInstruction())
50+
dummy := e.builder.CreateAlloca(e.Mod.Context().Int8Type(), "dummy")
51+
e.builder.SetInsertPointBefore(dummy)
4652
e.builder.SetInstDebugLocation(bb.FirstInstruction())
4753
var initCalls []llvm.Value
4854
for inst := bb.FirstInstruction(); !inst.IsNil(); inst = llvm.NextInstruction(inst) {
55+
if inst == dummy {
56+
continue
57+
}
4958
if !inst.IsAReturnInst().IsNil() {
5059
break // ret void
5160
}

0 commit comments

Comments
 (0)