Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/10_destructor_runtime_error.bloch
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions examples/10_destructor_runtime_error.expected-error
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Runtime error at Ln 6, Col 19: modulo by zero
21 changes: 20 additions & 1 deletion scripts/run_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=$?
Expand Down
33 changes: 32 additions & 1 deletion src/bloch/runtime/runtime_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -556,6 +557,7 @@ namespace bloch::runtime {
m_gcThread.join();
}
runCycleCollector();
throwDeferredRuntimeError();
// Ensure warnings appear before any normal echo output
if (m_warnOnExit)
warnUnmeasured();
Expand Down Expand Up @@ -1355,6 +1357,28 @@ namespace bloch::runtime {
obj->fields.clear();
}

void RuntimeEvaluator::deferRuntimeError(std::exception_ptr error) noexcept {
try {
std::lock_guard<std::mutex> 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<std::mutex> lock(m_deferredRuntimeErrorMutex);
error = std::move(m_deferredRuntimeError);
}
if (error)
std::rethrow_exception(error);
}

void RuntimeEvaluator::runFieldInitialisers(RuntimeClass* cls,
const std::shared_ptr<Object>& obj) {
if (!cls)
Expand Down Expand Up @@ -1944,6 +1968,7 @@ namespace bloch::runtime {
Value val = eval(assignStmt->value.get());
assign(assignStmt->name, val);
}
throwDeferredRuntimeError();
}

Value RuntimeEvaluator::eval(Expression* e) {
Expand Down Expand Up @@ -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<Object>(new Object{}, deleter);
Expand Down
7 changes: 7 additions & 0 deletions src/bloch/runtime/runtime_evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <exception>
#include <iostream>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -321,6 +326,8 @@ namespace bloch::runtime {
void markValue(const Value& v);
void markObject(const std::shared_ptr<Object>& 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<Object>& receiver, const std::vector<Value>& args);
void runConstructorChain(RuntimeClass* cls, const std::shared_ptr<Object>& obj,
Expand Down
Loading