diff --git a/examples/10_destructor_runtime_error.bloch b/examples/10_destructor_runtime_error.bloch new file mode 100644 index 0000000..8cfdccc --- /dev/null +++ b/examples/10_destructor_runtime_error.bloch @@ -0,0 +1,13 @@ +// Acceptance regression: errors in user destructor code must be reported normally. +class FailingCleanup { + public constructor() -> FailingCleanup { } + + destructor() -> void { + int x = 1 % 0; + } +} + +function main() -> void { + FailingCleanup resource = new FailingCleanup(); + destroy resource; +} diff --git a/examples/10_destructor_runtime_error.expected-error b/examples/10_destructor_runtime_error.expected-error new file mode 100644 index 0000000..4c774bf --- /dev/null +++ b/examples/10_destructor_runtime_error.expected-error @@ -0,0 +1 @@ +Runtime error at Ln 6, Col 19: modulo by zero diff --git a/scripts/run_examples.sh b/scripts/run_examples.sh index dc6a397..359928e 100755 --- a/scripts/run_examples.sh +++ b/scripts/run_examples.sh @@ -84,9 +84,28 @@ failures=0 for example in "${EXAMPLES[@]}"; do relative_path="${example#${TEMP_DIR}/}" output_file="${TEMP_DIR}/example-output.txt" + expected_error_file="${example%.bloch}.expected-error" printf ' RUN %s\n' "${relative_path}" - if run_example "${example}" "${output_file}"; then + if [[ -f "${expected_error_file}" ]]; then + expected_error="$(<"${expected_error_file}")" + if run_example "${example}" "${output_file}"; then + failures=$((failures + 1)) + printf ' %bFAIL%b %s (expected a runtime failure)\n' \ + "${RED}" "${RESET}" "${relative_path}" >&2 + else + status=$? + if [[ ${status} -ne 1 ]] || ! grep -Fq -- "${expected_error}" "${output_file}"; then + failures=$((failures + 1)) + printf ' %bFAIL%b %s (exit %d)\n' \ + "${RED}" "${RESET}" "${relative_path}" "${status}" >&2 + sed 's/^/ /' "${output_file}" >&2 + else + printf ' %bPASS%b %s (expected runtime diagnostic)\n' \ + "${GREEN}" "${RESET}" "${relative_path}" + fi + fi + elif run_example "${example}" "${output_file}"; then printf ' %bPASS%b %s\n' "${GREEN}" "${RESET}" "${relative_path}" else status=$? diff --git a/src/bloch/runtime/runtime_evaluator.cpp b/src/bloch/runtime/runtime_evaluator.cpp index baf5aec..cf6f844 100644 --- a/src/bloch/runtime/runtime_evaluator.cpp +++ b/src/bloch/runtime/runtime_evaluator.cpp @@ -534,6 +534,7 @@ namespace bloch::runtime { m_gcRequested = false; m_gcThreadStarted = false; m_allocSinceGc = 0; + m_deferredRuntimeError = nullptr; m_sim = QasmSimulator{m_collectQasmLog}; bool hasClasses = !program.classes.empty(); if (hasClasses) { @@ -556,6 +557,7 @@ namespace bloch::runtime { m_gcThread.join(); } runCycleCollector(); + throwDeferredRuntimeError(); // Ensure warnings appear before any normal echo output if (m_warnOnExit) warnUnmeasured(); @@ -1355,6 +1357,28 @@ namespace bloch::runtime { obj->fields.clear(); } + void RuntimeEvaluator::deferRuntimeError(std::exception_ptr error) noexcept { + try { + std::lock_guard lock(m_deferredRuntimeErrorMutex); + // Preserve the first failure: it is the one that caused teardown to begin. + if (!m_deferredRuntimeError) + m_deferredRuntimeError = std::move(error); + } catch (...) { + // A deleter cannot propagate any exception. Failure to retain a diagnostic is + // preferable to terminating the host process. + } + } + + void RuntimeEvaluator::throwDeferredRuntimeError() { + std::exception_ptr error; + { + std::lock_guard lock(m_deferredRuntimeErrorMutex); + error = std::move(m_deferredRuntimeError); + } + if (error) + std::rethrow_exception(error); + } + void RuntimeEvaluator::runFieldInitialisers(RuntimeClass* cls, const std::shared_ptr& obj) { if (!cls) @@ -1944,6 +1968,7 @@ namespace bloch::runtime { Value val = eval(assignStmt->value.get()); assign(assignStmt->name, val); } + throwDeferredRuntimeError(); } Value RuntimeEvaluator::eval(Expression* e) { @@ -2253,7 +2278,13 @@ namespace bloch::runtime { "cannot instantiate static or abstract class '" + cls->name + "'"); } auto deleter = [this](Object* obj) { - destroyObject(obj, !obj->skipDestructor); + // This try/catch means that user error in custom destructors is handled internally + // and does not crash the application + try { + destroyObject(obj, !obj->skipDestructor); + } catch (...) { + deferRuntimeError(std::current_exception()); + } delete obj; }; auto obj = std::shared_ptr(new Object{}, deleter); diff --git a/src/bloch/runtime/runtime_evaluator.hpp b/src/bloch/runtime/runtime_evaluator.hpp index 77bbeee..c19f9f2 100644 --- a/src/bloch/runtime/runtime_evaluator.hpp +++ b/src/bloch/runtime/runtime_evaluator.hpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -260,6 +261,10 @@ namespace bloch::runtime { std::condition_variable m_gcCv; std::mutex m_gcMutex; std::mutex m_heapMutex; + // A shared_ptr destruction path must not allow a user-program exception to escape: + // shared_ptr destruction is noexcept, so escaping would call std::terminate. + std::exception_ptr m_deferredRuntimeError; + std::mutex m_deferredRuntimeErrorMutex; size_t m_allocSinceGc = 0; // Buffer for echo outputs so logs (INFO/WARNING/ERROR) // can be displayed first before normal program output. @@ -321,6 +326,8 @@ namespace bloch::runtime { void markValue(const Value& v); void markObject(const std::shared_ptr& obj); void destroyObject(Object* obj, bool runUserDestructor); + void deferRuntimeError(std::exception_ptr error) noexcept; + void throwDeferredRuntimeError(); Value callMethod(RuntimeMethod* method, RuntimeClass* staticDispatchClass, const std::shared_ptr& receiver, const std::vector& args); void runConstructorChain(RuntimeClass* cls, const std::shared_ptr& obj,