From a74958f01fe2fa5e043d33ff9c53e5149b4442e1 Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 7 Aug 2018 05:53:13 +0000 Subject: [PATCH] ksupport: raise RuntimeError on reraise with no inflight exception. Fixes #1123. --- artiq/compiler/builtins.py | 3 +++ artiq/coredevice/comm_kernel.py | 2 +- artiq/coredevice/exceptions.py | 1 + artiq/firmware/ksupport/eh.rs | 18 ++++++++++++++++-- artiq/firmware/ksupport/lib.rs | 8 ++++---- 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/artiq/compiler/builtins.py b/artiq/compiler/builtins.py index 77e102d3b..d60db0840 100644 --- a/artiq/compiler/builtins.py +++ b/artiq/compiler/builtins.py @@ -169,6 +169,9 @@ def fn_ValueError(): def fn_ZeroDivisionError(): return types.TExceptionConstructor(TException("ZeroDivisionError")) +def fn_RuntimeError(): + return types.TExceptionConstructor(TException("RuntimeError")) + def fn_range(): return types.TBuiltinFunction("range") diff --git a/artiq/coredevice/comm_kernel.py b/artiq/coredevice/comm_kernel.py index 7bec59580..2cadd0d60 100644 --- a/artiq/coredevice/comm_kernel.py +++ b/artiq/coredevice/comm_kernel.py @@ -447,7 +447,7 @@ class CommKernel: self._write_string(function) else: exn_type = type(exn) - if exn_type in (ZeroDivisionError, ValueError, IndexError) or \ + if exn_type in (ZeroDivisionError, ValueError, IndexError, RuntimeError) or \ hasattr(exn, "artiq_builtin"): self._write_string("0:{}".format(exn_type.__name__)) else: diff --git a/artiq/coredevice/exceptions.py b/artiq/coredevice/exceptions.py index 8264c7a55..017266729 100644 --- a/artiq/coredevice/exceptions.py +++ b/artiq/coredevice/exceptions.py @@ -10,6 +10,7 @@ from artiq.coredevice.runtime import source_loader ZeroDivisionError = builtins.ZeroDivisionError ValueError = builtins.ValueError IndexError = builtins.IndexError +RuntimeError = builtins.RuntimeError class CoreException: diff --git a/artiq/firmware/ksupport/eh.rs b/artiq/firmware/ksupport/eh.rs index 5cd220bbc..73ba51ae2 100644 --- a/artiq/firmware/ksupport/eh.rs +++ b/artiq/firmware/ksupport/eh.rs @@ -392,7 +392,7 @@ static mut INFLIGHT: ExceptionInfo = ExceptionInfo { private: [0; uw::unwinder_private_data_size], }, exception: None, - handled: false, + handled: true, backtrace: [0; MAX_BACKTRACE_SIZE], backtrace_size: 0 }; @@ -418,8 +418,22 @@ pub unsafe extern fn raise(exception: *const Exception) -> ! { #[export_name="__artiq_reraise"] #[unwind(allowed)] pub unsafe extern fn reraise() -> ! { + use cslice::AsCSlice; + if INFLIGHT.handled { - raise(&INFLIGHT.exception.unwrap()) + match INFLIGHT.exception { + Some(ref exception) => raise(exception), + None => raise(&Exception { + name: "0:artiq.coredevice.exceptions.RuntimeError".as_c_slice(), + file: file!().as_c_slice(), + line: line!(), + column: column!(), + // https://github.com/rust-lang/rfcs/pull/1719 + function: "__artiq_reraise".as_c_slice(), + message: "No active exception to reraise".as_c_slice(), + param: [0, 0, 0] + }) + } } else { uw::_Unwind_Resume(&mut INFLIGHT.uw_exception) } diff --git a/artiq/firmware/ksupport/lib.rs b/artiq/firmware/ksupport/lib.rs index 73360ce05..b3a18bf1a 100644 --- a/artiq/firmware/ksupport/lib.rs +++ b/artiq/firmware/ksupport/lib.rs @@ -68,13 +68,13 @@ macro_rules! raise { ($name:expr, $message:expr, $param0:expr, $param1:expr, $param2:expr) => ({ use cslice::AsCSlice; let exn = $crate::eh::Exception { - name: concat!("0:artiq.coredevice.exceptions.", $name).as_bytes().as_c_slice(), - file: file!().as_bytes().as_c_slice(), + name: concat!("0:artiq.coredevice.exceptions.", $name).as_c_slice(), + file: file!().as_c_slice(), line: line!(), column: column!(), // https://github.com/rust-lang/rfcs/pull/1719 - function: "(Rust function)".as_bytes().as_c_slice(), - message: $message.as_bytes().as_c_slice(), + function: "(Rust function)".as_c_slice(), + message: $message.as_c_slice(), param: [$param0, $param1, $param2] }; #[allow(unused_unsafe)]