|
| 1 | +// Fizzy: A fast WebAssembly interpreter |
| 2 | +// Copyright 2019-2020 The Fizzy Authors. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#include <fizzy/fizzy.h> |
| 6 | + |
| 7 | +#include <test/utils/adler32.hpp> |
| 8 | +#include <test/utils/wasm_engine.hpp> |
| 9 | +#include <cassert> |
| 10 | +#include <cstring> |
| 11 | + |
| 12 | +namespace fizzy::test |
| 13 | +{ |
| 14 | +class FizzyCEngine final : public WasmEngine |
| 15 | +{ |
| 16 | + FizzyInstance* m_instance = nullptr; |
| 17 | + |
| 18 | +public: |
| 19 | + ~FizzyCEngine(); |
| 20 | + |
| 21 | + bool parse(bytes_view input) const final; |
| 22 | + std::optional<FuncRef> find_function( |
| 23 | + std::string_view name, std::string_view signature) const final; |
| 24 | + bool instantiate(bytes_view wasm_binary) final; |
| 25 | + bool init_memory(bytes_view memory) final; |
| 26 | + bytes_view get_memory() const final; |
| 27 | + Result execute(FuncRef func_ref, const std::vector<uint64_t>& args) final; |
| 28 | +}; |
| 29 | + |
| 30 | +namespace |
| 31 | +{ |
| 32 | +FizzyExecutionResult env_adler32( |
| 33 | + void*, FizzyInstance* /*instance*/, const FizzyValue* /*args*/, size_t, int) |
| 34 | +{ |
| 35 | + // assert(instance.memory != nullptr); |
| 36 | + // const auto ret = fizzy::test::adler32( |
| 37 | + // bytes_view{*instance.memory}.substr(args[0].as<uint32_t>(), args[1].as<uint32_t>())); |
| 38 | + // return Value{ret}; |
| 39 | + return {false, true, {0}}; |
| 40 | +} |
| 41 | +} // namespace |
| 42 | + |
| 43 | +std::unique_ptr<WasmEngine> create_fizzy_c_engine() |
| 44 | +{ |
| 45 | + return std::make_unique<FizzyCEngine>(); |
| 46 | +} |
| 47 | + |
| 48 | +FizzyCEngine::~FizzyCEngine() |
| 49 | +{ |
| 50 | + if (m_instance) |
| 51 | + fizzy_free_instance(m_instance); |
| 52 | +} |
| 53 | + |
| 54 | +bool FizzyCEngine::parse(bytes_view input) const |
| 55 | +{ |
| 56 | + const auto module = fizzy_parse(input.data(), input.size()); |
| 57 | + if (!module) |
| 58 | + return false; |
| 59 | + |
| 60 | + fizzy_free_module(module); |
| 61 | + return true; |
| 62 | +} |
| 63 | + |
| 64 | +bool FizzyCEngine::instantiate(bytes_view wasm_binary) |
| 65 | +{ |
| 66 | + const auto module = fizzy_parse(wasm_binary.data(), wasm_binary.size()); |
| 67 | + if (!module) |
| 68 | + return false; |
| 69 | + |
| 70 | + // TODO resolve_imported_functions |
| 71 | + FizzyExternalFunction imports[] = {{env_adler32, nullptr}}; |
| 72 | + m_instance = fizzy_instantiate(module, imports, 1); |
| 73 | + |
| 74 | + assert(m_instance != nullptr); |
| 75 | + return true; |
| 76 | +} |
| 77 | + |
| 78 | +bool FizzyCEngine::init_memory(bytes_view /*memory*/) |
| 79 | +{ |
| 80 | +// if (m_instance->memory == nullptr || m_instance->memory->size() < memory.size()) |
| 81 | +// return false; |
| 82 | +// |
| 83 | +// std::memcpy(m_instance->memory->data(), memory.data(), memory.size()); |
| 84 | +// return true; |
| 85 | + return false; |
| 86 | +} |
| 87 | + |
| 88 | +bytes_view FizzyCEngine::get_memory() const |
| 89 | +{ |
| 90 | +// if (!m_instance->memory) |
| 91 | +// return {}; |
| 92 | +// |
| 93 | +// return {m_instance->memory->data(), m_instance->memory->size()}; |
| 94 | + return {}; |
| 95 | +} |
| 96 | + |
| 97 | +std::optional<WasmEngine::FuncRef> FizzyCEngine::find_function( |
| 98 | + std::string_view /*name*/, std::string_view /*signature*/) const |
| 99 | +{ |
| 100 | +// const auto func_idx = fizzy::find_exported_function(m_instance->module, name); |
| 101 | +// if (func_idx.has_value()) |
| 102 | +// { |
| 103 | +// const auto func_type = m_instance->module.get_function_type(*func_idx); |
| 104 | +// const auto sig_type = translate_signature(signature); |
| 105 | +// if (sig_type != func_type) |
| 106 | +// return std::nullopt; |
| 107 | +// } |
| 108 | +// return func_idx; |
| 109 | + return std::nullopt; |
| 110 | +} |
| 111 | + |
| 112 | +WasmEngine::Result FizzyCEngine::execute( |
| 113 | + WasmEngine::FuncRef func_ref, const std::vector<uint64_t>& args) |
| 114 | +{ |
| 115 | + static_assert(sizeof(uint64_t) == sizeof(FizzyValue)); |
| 116 | + const auto first_arg = reinterpret_cast<const FizzyValue*>(args.data()); |
| 117 | + const auto status = fizzy_execute( |
| 118 | + m_instance, static_cast<uint32_t>(func_ref), first_arg, args.size(), 0); |
| 119 | + if (status.trapped) |
| 120 | + return {true, std::nullopt}; |
| 121 | + else if (status.has_value) |
| 122 | + return {false, status.value.i64}; |
| 123 | + else |
| 124 | + return {false, std::nullopt}; |
| 125 | +} |
| 126 | +} // namespace fizzy::test |
0 commit comments