Skip to content

Commit

Permalink
Unset the current state, enforce Machine is not reset while in a VM call
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Oct 22, 2024
1 parent e790d9b commit 44b94f9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/sandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ bool Sandbox::load(const PackedByteArray *buffer, const std::vector<std::string>
this->reset_machine();
return false;
}
// Check if a call is being made from the VM already,
// which could spell trouble when we now reset the machine.
if (this->m_current_state != nullptr) {
ERR_PRINT("Cannot load a new program while a VM call is in progress.");
return false;
}
const std::string_view binary_view = std::string_view{ (const char *)buffer->ptr(), static_cast<size_t>(buffer->size()) };

// Get t0 for the startup time
Expand All @@ -293,6 +299,7 @@ bool Sandbox::load(const PackedByteArray *buffer, const std::vector<std::string>
.default_exit_function = "fast_exit",
#ifdef RISCV_BINARY_TRANSLATION
.translate_enabled = false,
.translate_enable_embedded = true,
.translate_future_segments = false,
.translate_invoke_compiler = false,
//.translate_trace = true,
Expand Down Expand Up @@ -352,6 +359,8 @@ bool Sandbox::load(const PackedByteArray *buffer, const std::vector<std::string>
this->handle_exception(machine().cpu.pc());
}

this->m_current_state = nullptr;

// Read the program's custom properties, if any
this->read_program_properties(true);

Expand Down
7 changes: 6 additions & 1 deletion src/sandbox_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ bool Sandbox::resume(uint64_t max_instructions) {
ERR_PRINT("Sandbox: Cannot resume after initialization.");
return false;
}
if (this->m_current_state != &this->m_states[0]) {
if (this->m_current_state != nullptr) {
ERR_PRINT("Sandbox: Cannot resume while in a call.");
this->m_resumable_mode = false; // Disable resumable mode
return false;
}

this->m_current_state = &this->m_states[0];

const gaddr_t address = m_machine->cpu.pc();
try {
const bool stopped = m_machine->resume(max_instructions);
Expand All @@ -67,11 +69,14 @@ bool Sandbox::resume(uint64_t max_instructions) {
// It's not available for VM calls, only during startup
this->m_resumable_mode = false;
}

this->m_current_state = nullptr;
return stopped;

} catch (const std::exception &e) {
this->m_resumable_mode = false;
this->handle_exception(address);
this->m_current_state = nullptr;
return true; // Can't (shouldn't) be resumed anymore
}
}

0 comments on commit 44b94f9

Please sign in to comment.