Skip to content

Commit

Permalink
Make sure tree_base is retained on reloads, update libriscv with bugfix
Browse files Browse the repository at this point in the history
Fixes a nasty bug when binary translation is enabled and programs get reloaded
  • Loading branch information
fwsGonzo committed Oct 23, 2024
1 parent 2a53785 commit 78a41cf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ext/libriscv
17 changes: 9 additions & 8 deletions src/elf/script_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ void ELFScriptInstance::reload(ELFScript *p_script) {
auto old_script = std::move(this->script);
this->script = Ref<ELFScript>(p_script);
this->update_methods();
bool updated_program = false;
bool update_program = false;

auto it = sandbox_instances.find(old_script.ptr());
if (it != sandbox_instances.end()) {
Expand All @@ -513,26 +513,27 @@ void ELFScriptInstance::reload(ELFScript *p_script) {
// Remove the old script instance and insert the new one
sandbox_instances.erase(it);
sandbox_instances.insert_or_assign(p_script, sandbox_ptr);
// Set the new program
sandbox_ptr->set_program(this->script);
updated_program = true;
update_program = true;
}

if (!updated_program) {
if (!update_program) {
Sandbox *sandbox_ptr = Object::cast_to<Sandbox>(this->owner);
if (sandbox_ptr != nullptr) {
this->current_sandbox = sandbox_ptr;
sandbox_ptr->set_program(this->script);
updated_program = true;
update_program = true;
}
}

if (!updated_program) {
if (!update_program) {
ERR_PRINT("ELFScriptInstance: Did not reload a Sandbox instance");
if constexpr (VERBOSE_LOGGING) {
fprintf(stderr, "ELFScriptInstance: owner is instead a '%s'!\n", this->owner->get_class().utf8().get_data());
}
} else {
// Set the new program, with potentially new parent Node
current_sandbox->set_tree_base(godot::Object::cast_to<godot::Node>(this->owner));
current_sandbox->set_program(this->script);

if constexpr (VERBOSE_LOGGING) {
ERR_PRINT("ELFScriptInstance: reloaded " + Object::cast_to<Node>(owner)->get_name());
}
Expand Down
16 changes: 6 additions & 10 deletions src/sandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ std::vector<PropertyInfo> Sandbox::create_sandbox_property_list() const {

void Sandbox::constructor_initialize() {
this->m_current_state = &this->m_states[0];
this->m_tree_base = this;
this->m_use_unboxed_arguments = SandboxProjectSettings::use_native_types();
// For each call state, reset the state
for (size_t i = 0; i < this->m_states.size(); i++) {
Expand Down Expand Up @@ -191,20 +190,17 @@ Sandbox::Sandbox() {
dummy_machine = new machine_t{};
}
this->constructor_initialize();
this->m_tree_base = this;
this->m_global_instance_count += 1;
// In order to reduce checks we guarantee that this
// class is well-formed at all times.
this->reset_machine();
}
Sandbox::Sandbox(const PackedByteArray &buffer) {
this->constructor_initialize();
this->m_global_instance_count += 1;
Sandbox::Sandbox(const PackedByteArray &buffer) : Sandbox() {
this->load_buffer(buffer);
}
Sandbox::Sandbox(Ref<ELFScript> program) {
this->constructor_initialize();
this->m_global_instance_count += 1;
this->set_program(std::move(program));
Sandbox::Sandbox(Ref<ELFScript> program) : Sandbox() {
this->set_program(program);
}

Sandbox::~Sandbox() {
Expand Down Expand Up @@ -247,7 +243,7 @@ void Sandbox::set_program(Ref<ELFScript> program) {
property_values.push_back(value);
}

this->m_program_data = std::move(program);
this->m_program_data = program;
this->m_program_bytes = {};

// Unload program and reset the machine
Expand Down Expand Up @@ -595,7 +591,7 @@ GuestVariant *Sandbox::setup_arguments(gaddr_t &sp, const Variant **args, int ar
}
Variant Sandbox::vmcall_internal(gaddr_t address, const Variant **args, int argc) {
this->m_current_state += 1;
if (this->m_current_state >= this->m_states.end()) {
if (UNLIKELY(this->m_current_state >= this->m_states.end())) {
ERR_PRINT("Too many VM calls in progress");
this->m_exceptions ++;
this->m_global_exceptions ++;
Expand Down
2 changes: 1 addition & 1 deletion src/sandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class Sandbox : public Node {
// State stack, with the permanent (initial) state at index 0.
// That means eg. static Variant values are held stored in the state at index 0,
// so that they can be accessed by future VM calls, and not lost when a call ends.
std::array<CurrentState, MAX_LEVEL + 1> m_states;
std::array<CurrentState, MAX_LEVEL> m_states;

// Properties
mutable std::vector<SandboxProperty> m_properties;
Expand Down

0 comments on commit 78a41cf

Please sign in to comment.