mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-10 14:03:39 +00:00
compiler, runtime: make runtime panics recoverable
Emit fault checkpoints around compiler-generated runtime assertions so runtimePanicAt can unwind through the existing defer/recover machinery instead of aborting. This lets panics from bounds checks, type checks, and other compiler-inserted runtime checks be recovered by deferred functions. Mark functions that call recover as noinline. Inlining such a function into a deferred closure can make recover observe the wrong call context and report success when it should return nil. Addresses tinygo-org/tinygo issues 2759 and 3510.
This commit is contained in:
@@ -1895,6 +1895,12 @@ func (b *builder) createBuiltin(argTypes []types.Type, argValues []llvm.Value, c
|
||||
// not of the current function.
|
||||
useParentFrame = 1
|
||||
}
|
||||
// Prevent inlining of functions that call recover(), matching the
|
||||
// Go compiler's behavior. If this function were inlined into a
|
||||
// deferred function, recover() would incorrectly succeed because
|
||||
// the inlined code runs in the deferred function's context.
|
||||
noinline := b.ctx.CreateEnumAttribute(llvm.AttributeKindID("noinline"), 0)
|
||||
b.llvmFn.AddFunctionAttr(noinline)
|
||||
return b.createRuntimeCall("_recover", []llvm.Value{llvm.ConstInt(b.ctx.Int1Type(), useParentFrame, false)}, ""), nil
|
||||
case "ssa:wrapnilchk":
|
||||
// TODO: do an actual nil check?
|
||||
|
||||
Reference in New Issue
Block a user