~maelkum/viuavm

90ff10cee6fa4f174476a3aaf67842744078a9fa — Marek Marecki 11 months ago 53ac001
Allow calling functions from REPL

The patch in execute(CALL) function is required because otherwise the
return addresses are all wrong. The REPL allocates memory for the code
to be evaluated dynamically, and deallocates after that code has been
executed. If the return address points to that now-deallocated memory
BAD things happen (ie, the IP becomes invalid).
2 files changed, 12 insertions(+), 2 deletions(-)

M new/src/tools/exec/repl.cpp
M new/src/vm/ins.cpp
M new/src/tools/exec/repl.cpp => new/src/tools/exec/repl.cpp +11 -1
@@ 369,9 369,19 @@ auto evaluate_asm_expression(std::string const source_text) -> void
        return;
    }

    auto fn_offsets = std::map<std::string, size_t>{};
    {
        for (auto const& [mod_name, mod] : REPL_STATE->core.modules) {
            for (auto const& [fn_off, fn] : mod.elf.function_table()) {
                auto const fn_id = (mod_name.empty() ? "" : (mod_name + "::"))
                                   + std::get<0>(fn);
                fn_offsets[fn_id] = fn_off;
            }
        }
    }

    auto strings_table = std::vector<uint8_t>{};
    auto var_offsets   = std::map<std::string, size_t>{};
    auto fn_offsets    = std::map<std::string, size_t>{};
    auto cooked        = std::vector<viua::libs::parser::ast::Instruction>{};
    try {
        cooked = viua::libs::stage::cook_long_immediates(

M new/src/vm/ins.cpp => new/src/vm/ins.cpp +1 -1
@@ 1272,7 1272,7 @@ auto execute(CALL const op, Stack& stack, ip_type const ip) -> ip_type
        throw abort_execution{ip, "invalid IP after call"};
    }

    auto const fr_return = (ip + 1);
    auto const fr_return = (stack.ip + 1);
    auto const fr_entry  = (stack.proc.module.ip_base
                           + (fn_addr / sizeof(viua::arch::instruction_type)));