mirror of
https://github.com/XRPLF/rippled.git
synced 2026-07-24 23:50:22 +00:00
Disable reusing wasm module (#6364)
* Remove ability to re-use wasm module * Check that HFS object is always new * Fix clang format * Remove perf tests * temp build fix * Fix merge
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -37,7 +37,8 @@ runFinishFunction(std::string const& code)
|
||||
{
|
||||
auto& engine = WasmEngine::instance();
|
||||
auto const wasm = hexToBytes(code);
|
||||
auto const re = engine.run(wasm, "finish");
|
||||
HostFunctions hfs;
|
||||
auto const re = engine.run(wasm, hfs, "finish");
|
||||
if (re.has_value())
|
||||
{
|
||||
return std::optional<int32_t>(re->result);
|
||||
@@ -107,10 +108,11 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
// clang-format on
|
||||
auto& vm = WasmEngine::instance();
|
||||
|
||||
std::shared_ptr<ImportVec> imports(std::make_shared<ImportVec>());
|
||||
WasmImpFunc<Add_proto>(*imports, "func-add", reinterpret_cast<void*>(&Add));
|
||||
HostFunctions hfs;
|
||||
ImportVec imports;
|
||||
WasmImpFunc<Add_proto>(imports, "func-add", reinterpret_cast<void*>(&Add), &hfs);
|
||||
|
||||
auto re = vm.run(wasm, "addTwo", wasmParams(1234, 5678), imports);
|
||||
auto re = vm.run(wasm, hfs, "addTwo", wasmParams(1234, 5678), imports);
|
||||
|
||||
// if (res) printf("invokeAdd get the result: %d\n", res.value());
|
||||
|
||||
@@ -125,7 +127,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
using namespace test::jtx;
|
||||
|
||||
Env env{*this};
|
||||
std::shared_ptr<HostFunctions> hfs(new HostFunctions(env.journal));
|
||||
HostFunctions hfs(env.journal);
|
||||
|
||||
{
|
||||
auto wasm = hexToBytes("00000000");
|
||||
@@ -175,22 +177,22 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
using namespace test::jtx;
|
||||
|
||||
Env env{*this};
|
||||
std::shared_ptr<HostFunctions> hfs(new TestLedgerDataProvider(env));
|
||||
auto imports = std::make_shared<ImportVec>();
|
||||
WASM_IMPORT_FUNC2(*imports, getLedgerSqn, "get_ledger_sqn", hfs.get(), 33);
|
||||
TestLedgerDataProvider hfs(env);
|
||||
ImportVec imports;
|
||||
WASM_IMPORT_FUNC2(imports, getLedgerSqn, "get_ledger_sqn", &hfs, 33);
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
auto re = engine.run(ledgerSqnWasm, ESCROW_FUNCTION_NAME, {}, imports, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(ledgerSqnWasm, hfs, ESCROW_FUNCTION_NAME, {}, imports, 1'000'000, env.journal);
|
||||
|
||||
checkResult(re, 0, 440);
|
||||
|
||||
env.close();
|
||||
env.close();
|
||||
|
||||
// empty module - run the same instance
|
||||
re = engine.run({}, ESCROW_FUNCTION_NAME, {}, imports, hfs, 1'000'000, env.journal);
|
||||
|
||||
checkResult(re, 5, 488);
|
||||
// empty module, throwing exception
|
||||
re = engine.run({}, hfs, ESCROW_FUNCTION_NAME, {}, imports, 1'000'000, env.journal);
|
||||
BEAST_EXPECT(!re);
|
||||
env.close();
|
||||
}
|
||||
|
||||
void
|
||||
@@ -200,43 +202,13 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
auto const fibWasm = hexToBytes(fibWasmHex);
|
||||
auto& engine = WasmEngine::instance();
|
||||
HostFunctions hfs;
|
||||
|
||||
auto const re = engine.run(fibWasm, "fib", wasmParams(10));
|
||||
auto const re = engine.run(fibWasm, hfs, "fib", wasmParams(10));
|
||||
|
||||
checkResult(re, 55, 1'137);
|
||||
}
|
||||
|
||||
void
|
||||
testWasmSha()
|
||||
{
|
||||
testcase("Wasm sha");
|
||||
|
||||
auto const sha512Wasm = hexToBytes(sha512PureWasmHex);
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
auto const re = engine.run(sha512Wasm, "sha512_process", wasmParams(sha512PureWasmHex));
|
||||
|
||||
checkResult(re, 34'432, 151'155);
|
||||
}
|
||||
|
||||
void
|
||||
testWasmB58()
|
||||
{
|
||||
testcase("Wasm base58");
|
||||
auto const b58Wasm = hexToBytes(b58WasmHex);
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
Bytes outb;
|
||||
outb.resize(1024);
|
||||
|
||||
auto const minSize = std::min(static_cast<std::uint32_t>(512), static_cast<std::uint32_t>(b58WasmHex.size()));
|
||||
auto const s = std::string_view(b58WasmHex.c_str(), minSize);
|
||||
|
||||
auto const re = engine.run(b58Wasm, "b58enco", wasmParams(outb, s));
|
||||
|
||||
checkResult(re, 700, 2'886'069);
|
||||
}
|
||||
|
||||
void
|
||||
testHFCost()
|
||||
{
|
||||
@@ -250,12 +222,12 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
auto imp = createWasmImport(*hfs);
|
||||
for (auto& i : *imp)
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto imp = createWasmImport(hfs);
|
||||
for (auto& i : imp)
|
||||
i.second.gas = 0;
|
||||
|
||||
auto re = engine.run(allHostFuncWasm, ESCROW_FUNCTION_NAME, {}, imp, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(allHostFuncWasm, hfs, ESCROW_FUNCTION_NAME, {}, imp, 1'000'000, env.journal);
|
||||
|
||||
checkResult(re, 1, 27'080);
|
||||
|
||||
@@ -273,10 +245,10 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
auto const imp = createWasmImport(*hfs);
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto const imp = createWasmImport(hfs);
|
||||
|
||||
auto re = engine.run(allHostFuncWasm, ESCROW_FUNCTION_NAME, {}, imp, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(allHostFuncWasm, hfs, ESCROW_FUNCTION_NAME, {}, imp, 1'000'000, env.journal);
|
||||
|
||||
checkResult(re, 1, 66'340);
|
||||
|
||||
@@ -289,10 +261,10 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
auto const imp = createWasmImport(*hfs);
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto const imp = createWasmImport(hfs);
|
||||
|
||||
auto re = engine.run(allHostFuncWasm, ESCROW_FUNCTION_NAME, {}, imp, hfs, 200, env.journal);
|
||||
auto re = engine.run(allHostFuncWasm, hfs, ESCROW_FUNCTION_NAME, {}, imp, 200, env.journal);
|
||||
|
||||
if (BEAST_EXPECT(!re))
|
||||
{
|
||||
@@ -313,14 +285,14 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
using namespace test::jtx;
|
||||
Env env{*this};
|
||||
{
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, ESCROW_FUNCTION_NAME, {}, 100'000);
|
||||
checkResult(re, 1, 66'340);
|
||||
}
|
||||
|
||||
{
|
||||
// max<int64_t>() gas
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, ESCROW_FUNCTION_NAME, {}, -1);
|
||||
checkResult(re, 1, 66'340);
|
||||
}
|
||||
@@ -332,13 +304,13 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
}
|
||||
Expected<Bytes, HostFunctionError>
|
||||
getTxField(SField const& fname) override
|
||||
getTxField(SField const& fname) const override
|
||||
{
|
||||
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
|
||||
}
|
||||
};
|
||||
|
||||
std::shared_ptr<HostFunctions> hfs(new FieldNotFoundHostFunctions(env));
|
||||
FieldNotFoundHostFunctions hfs(env);
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, ESCROW_FUNCTION_NAME, {}, 100'000);
|
||||
checkResult(re, -201, 28'965);
|
||||
}
|
||||
@@ -350,13 +322,13 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
}
|
||||
Expected<Bytes, HostFunctionError>
|
||||
getTxField(SField const& fname) override
|
||||
getTxField(SField const& fname) const override
|
||||
{
|
||||
return Bytes((128 + 1) * 64 * 1024, 1);
|
||||
}
|
||||
};
|
||||
|
||||
std::shared_ptr<HostFunctions> hfs(new OversizedFieldHostFunctions(env));
|
||||
OversizedFieldHostFunctions hfs(env);
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, ESCROW_FUNCTION_NAME, {}, 100'000);
|
||||
checkResult(re, -201, 28'965);
|
||||
}
|
||||
@@ -365,14 +337,14 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
auto const deepWasm = hexToBytes(deepRecursionHex);
|
||||
|
||||
std::shared_ptr<TestHostFunctionsSink> hfs(new TestHostFunctionsSink(env));
|
||||
TestHostFunctionsSink hfs(env);
|
||||
std::string funcName("finish");
|
||||
auto re = runEscrowWasm(deepWasm, hfs, funcName, {}, 1'000'000'000);
|
||||
BEAST_EXPECT(!re && re.error());
|
||||
// std::cout << "bad case (deep recursion) result " << re.error()
|
||||
// << std::endl;
|
||||
|
||||
auto const& sink = hfs->getSink();
|
||||
auto const& sink = hfs.getSink();
|
||||
auto countSubstr = [](std::string const& str, std::string const& substr) {
|
||||
std::size_t pos = 0;
|
||||
int occurrences = 0;
|
||||
@@ -392,7 +364,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{ // infinite loop
|
||||
auto const infiniteLoopWasm = hexToBytes(infiniteLoopWasmHex);
|
||||
std::string const funcName("loop");
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
TestHostFunctions hfs(env, 0);
|
||||
|
||||
// infinite loop should be caught and fail
|
||||
auto const re = runEscrowWasm(infiniteLoopWasm, hfs, funcName, {}, 1'000'000);
|
||||
@@ -405,13 +377,13 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
// expected import not provided
|
||||
auto const lgrSqnWasm = hexToBytes(ledgerSqnWasmHex);
|
||||
std::shared_ptr<HostFunctions> hfs(new TestLedgerDataProvider(env));
|
||||
std::shared_ptr<ImportVec> imports(std::make_shared<ImportVec>());
|
||||
WASM_IMPORT_FUNC2(*imports, getLedgerSqn, "get_ledger_sqn2", hfs.get());
|
||||
TestLedgerDataProvider hfs(env);
|
||||
ImportVec imports;
|
||||
WASM_IMPORT_FUNC2(imports, getLedgerSqn, "get_ledger_sqn2", &hfs);
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
auto re = engine.run(lgrSqnWasm, ESCROW_FUNCTION_NAME, {}, imports, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(lgrSqnWasm, hfs, ESCROW_FUNCTION_NAME, {}, imports, 1'000'000, env.journal);
|
||||
|
||||
BEAST_EXPECT(!re);
|
||||
}
|
||||
@@ -419,14 +391,14 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
// bad import format
|
||||
auto const lgrSqnWasm = hexToBytes(ledgerSqnWasmHex);
|
||||
std::shared_ptr<HostFunctions> hfs(new TestLedgerDataProvider(env));
|
||||
std::shared_ptr<ImportVec> imports(std::make_shared<ImportVec>());
|
||||
WASM_IMPORT_FUNC2(*imports, getLedgerSqn, "get_ledger_sqn", hfs.get());
|
||||
(*imports)[0].first = nullptr;
|
||||
TestLedgerDataProvider hfs(env);
|
||||
ImportVec imports;
|
||||
WASM_IMPORT_FUNC2(imports, getLedgerSqn, "get_ledger_sqn", &hfs);
|
||||
imports[0].first = nullptr;
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
auto re = engine.run(lgrSqnWasm, ESCROW_FUNCTION_NAME, {}, imports, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(lgrSqnWasm, hfs, ESCROW_FUNCTION_NAME, {}, imports, 1'000'000, env.journal);
|
||||
|
||||
BEAST_EXPECT(!re);
|
||||
}
|
||||
@@ -434,12 +406,12 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
// bad function name
|
||||
auto const lgrSqnWasm = hexToBytes(ledgerSqnWasmHex);
|
||||
std::shared_ptr<HostFunctions> hfs(new TestLedgerDataProvider(env));
|
||||
std::shared_ptr<ImportVec> imports(std::make_shared<ImportVec>());
|
||||
WASM_IMPORT_FUNC2(*imports, getLedgerSqn, "get_ledger_sqn", hfs.get());
|
||||
TestLedgerDataProvider hfs(env);
|
||||
ImportVec imports;
|
||||
WASM_IMPORT_FUNC2(imports, getLedgerSqn, "get_ledger_sqn", &hfs);
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
auto re = engine.run(lgrSqnWasm, "func1", {}, imports, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(lgrSqnWasm, hfs, "func1", {}, imports, 1'000'000, env.journal);
|
||||
|
||||
BEAST_EXPECT(!re);
|
||||
}
|
||||
@@ -458,7 +430,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
auto const floatTestWasm = hexToBytes(floatTestsWasmHex);
|
||||
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto re = runEscrowWasm(floatTestWasm, hfs, funcName, {}, 200'000);
|
||||
checkResult(re, 1, 110'699);
|
||||
env.close();
|
||||
@@ -467,102 +439,13 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
auto const float0Wasm = hexToBytes(float0Hex);
|
||||
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto re = runEscrowWasm(float0Wasm, hfs, funcName, {}, 100'000);
|
||||
checkResult(re, 1, 4'259);
|
||||
env.close();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
perfTest()
|
||||
{
|
||||
testcase("Perf test host functions");
|
||||
|
||||
using namespace jtx;
|
||||
using namespace std::chrono;
|
||||
|
||||
// std::string const funcName("test");
|
||||
auto const perfWasm = hexToBytes(hfPerfTest);
|
||||
|
||||
// std::string const credType = "abcde";
|
||||
// std::string const credType2 = "fghijk";
|
||||
// std::string const credType3 = "0123456";
|
||||
// char const uri[] = "uri";
|
||||
|
||||
Account const alan{"alan"};
|
||||
Account const bob{"bob"};
|
||||
Account const issuer{"issuer"};
|
||||
|
||||
{
|
||||
Env env(*this);
|
||||
// Env env(*this, envconfig(), {}, nullptr,
|
||||
// beast::severities::kTrace);
|
||||
env.fund(XRP(5000), alan, bob, issuer);
|
||||
env.close();
|
||||
|
||||
// // create escrow
|
||||
// auto const seq = env.seq(alan);
|
||||
// auto const k = keylet::escrow(alan, seq);
|
||||
// // auto const allowance = 3'600;
|
||||
// auto escrowCreate = escrow::create(alan, bob, XRP(1000));
|
||||
// XRPAmount txnFees = env.current()->fees().base + 1000;
|
||||
// env(escrowCreate,
|
||||
// escrow::finish_function(wasmHex),
|
||||
// escrow::finish_time(env.now() + 11s),
|
||||
// escrow::cancel_time(env.now() + 100s),
|
||||
// escrow::data("1000000000"), // 1000 XRP in drops
|
||||
// memodata("memo1234567"),
|
||||
// memodata("2memo1234567"),
|
||||
// fee(txnFees));
|
||||
|
||||
// // create depositPreauth
|
||||
// auto const k = keylet::depositPreauth(
|
||||
// bob,
|
||||
// {{issuer.id(), makeSlice(credType)},
|
||||
// {issuer.id(), makeSlice(credType2)},
|
||||
// {issuer.id(), makeSlice(credType3)}});
|
||||
// env(deposit::authCredentials(
|
||||
// bob,
|
||||
// {{issuer, credType},
|
||||
// {issuer, credType2},
|
||||
// {issuer, credType3}}));
|
||||
|
||||
// create nft
|
||||
[[maybe_unused]] uint256 const nft0{token::getNextID(env, alan, 0u)};
|
||||
env(token::mint(alan, 0u));
|
||||
auto const k = keylet::nftoffer(alan, 0);
|
||||
[[maybe_unused]] uint256 const nft1{token::getNextID(env, alan, 0u)};
|
||||
|
||||
env(token::mint(alan, 0u),
|
||||
token::uri(
|
||||
"https://github.com/XRPLF/XRPL-Standards/discussions/"
|
||||
"279?id=github.com/XRPLF/XRPL-Standards/discussions/"
|
||||
"279&ut=github.com/XRPLF/XRPL-Standards/discussions/"
|
||||
"279&sid=github.com/XRPLF/XRPL-Standards/discussions/"
|
||||
"279&aot=github.com/XRPLF/XRPL-Standards/disc"));
|
||||
[[maybe_unused]] uint256 const nft2{token::getNextID(env, alan, 0u)};
|
||||
env(token::mint(alan, 0u));
|
||||
env.close();
|
||||
|
||||
std::shared_ptr<HostFunctions> hfs(new PerfHostFunctions(env, k, env.tx()));
|
||||
|
||||
auto re = runEscrowWasm(perfWasm, hfs, ESCROW_FUNCTION_NAME);
|
||||
if (BEAST_EXPECT(re.has_value()))
|
||||
{
|
||||
BEAST_EXPECT(re->result);
|
||||
std::cout << "Res: " << re->result << " cost: " << re->cost << std::endl;
|
||||
}
|
||||
|
||||
// env(escrow::finish(alan, alan, seq),
|
||||
// escrow::comp_allowance(allowance),
|
||||
// fee(txnFees),
|
||||
// ter(tesSUCCESS));
|
||||
|
||||
env.close();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testCodecovWasm()
|
||||
{
|
||||
@@ -573,7 +456,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
Env env{*this};
|
||||
|
||||
auto const codecovWasm = hexToBytes(codecovTestsWasmHex);
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
TestHostFunctions hfs(env, 0);
|
||||
|
||||
auto const allowance = 202'724;
|
||||
auto re = runEscrowWasm(codecovWasm, hfs, ESCROW_FUNCTION_NAME, {}, allowance);
|
||||
@@ -591,7 +474,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
auto disabledFloatWasm = hexToBytes(disabledFloatHex);
|
||||
std::string const funcName("finish");
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
TestHostFunctions hfs(env, 0);
|
||||
|
||||
{
|
||||
// f32 set constant, opcode disabled exception
|
||||
@@ -704,91 +587,17 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
Env env(*this);
|
||||
|
||||
auto const startLoopWasm = hexToBytes(startLoopHex);
|
||||
std::shared_ptr<HostFunctions> hfs(new TestLedgerDataProvider(env));
|
||||
std::shared_ptr<ImportVec> imports(std::make_shared<ImportVec>());
|
||||
TestLedgerDataProvider hfs(env);
|
||||
ImportVec imports;
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
auto checkRes = engine.check(startLoopWasm, "finish", {}, imports, hfs, env.journal);
|
||||
auto checkRes = engine.check(startLoopWasm, hfs, "finish", {}, imports, env.journal);
|
||||
BEAST_EXPECTS(checkRes == tesSUCCESS, std::to_string(TERtoInt(checkRes)));
|
||||
|
||||
auto re = engine.run(startLoopWasm, ESCROW_FUNCTION_NAME, {}, imports, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(startLoopWasm, hfs, ESCROW_FUNCTION_NAME, {}, imports, 1'000'000, env.journal);
|
||||
BEAST_EXPECTS(re.error() == tecFAILED_PROCESSING, std::to_string(TERtoInt(re.error())));
|
||||
}
|
||||
|
||||
void
|
||||
testBadAlloc()
|
||||
{
|
||||
testcase("Wasm Bad Alloc");
|
||||
|
||||
// bad_alloc.c
|
||||
auto const badAllocWasm = hexToBytes(badAllocHex);
|
||||
|
||||
using namespace test::jtx;
|
||||
|
||||
Env env{*this};
|
||||
std::shared_ptr<HostFunctions> hfs(new TestLedgerDataProvider(env));
|
||||
|
||||
// std::shared_ptr<ImportVec> imports(std::make_shared<ImportVec>());
|
||||
uint8_t buf1[8] = {7, 8, 9, 10, 11, 12, 13, 14};
|
||||
{ // forged "allocate" return valid address
|
||||
std::vector<WasmParam> params = {{.type = WT_U8V, .of = {.u8v = {.d = buf1, .sz = sizeof(buf1)}}}};
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
auto re = engine.run(badAllocWasm, "test", params, {}, hfs, 1'000'000, env.journal);
|
||||
if (BEAST_EXPECT(re))
|
||||
{
|
||||
BEAST_EXPECTS(re->result == 7, std::to_string(re->result));
|
||||
BEAST_EXPECTS(re->cost == 430, std::to_string(re->cost));
|
||||
}
|
||||
}
|
||||
|
||||
{ // return 0 whithout calling wasm
|
||||
std::vector<WasmParam> params = {{.type = WT_U8V, .of = {.u8v = {.d = buf1, .sz = 0}}}};
|
||||
auto& engine = WasmEngine::instance();
|
||||
auto re = engine.run(badAllocWasm, "test", params, {}, hfs, 1'000'000, env.journal);
|
||||
BEAST_EXPECT(!re) && BEAST_EXPECT(re.error() == tecFAILED_PROCESSING);
|
||||
}
|
||||
|
||||
{ // forged "allocate" return 8Mb (which is more than memory limit)
|
||||
std::vector<WasmParam> params = {{.type = WT_U8V, .of = {.u8v = {.d = buf1, .sz = 1}}}};
|
||||
auto& engine = WasmEngine::instance();
|
||||
auto re = engine.run(badAllocWasm, "test", params, {}, hfs, 1'000'000, env.journal);
|
||||
BEAST_EXPECT(!re) && BEAST_EXPECT(re.error() == tecFAILED_PROCESSING);
|
||||
}
|
||||
|
||||
{ // forged "allocate" return 0
|
||||
std::vector<WasmParam> params = {{.type = WT_U8V, .of = {.u8v = {.d = buf1, .sz = 2}}}};
|
||||
auto& engine = WasmEngine::instance();
|
||||
auto re = engine.run(badAllocWasm, "test", params, {}, hfs, 1'000'000, env.journal);
|
||||
BEAST_EXPECT(!re) && BEAST_EXPECT(re.error() == tecFAILED_PROCESSING);
|
||||
}
|
||||
|
||||
{ // forged "allocate" return -1
|
||||
std::vector<WasmParam> params = {{.type = WT_U8V, .of = {.u8v = {.d = buf1, .sz = 3}}}};
|
||||
auto& engine = WasmEngine::instance();
|
||||
auto re = engine.run(badAllocWasm, "test", params, {}, hfs, 1'000'000, env.journal);
|
||||
|
||||
BEAST_EXPECT(!re) && BEAST_EXPECT(re.error() == tecFAILED_PROCESSING);
|
||||
}
|
||||
|
||||
{
|
||||
std::string what;
|
||||
try
|
||||
{
|
||||
char const test[] = "test";
|
||||
std::size_t sz = std::numeric_limits<int32_t>::max() + 1ull;
|
||||
auto p = wasmParams(std::string_view(test, sz));
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
what = e.what();
|
||||
}
|
||||
BEAST_EXPECT(what.find("can't allocate memory, size: 2147483648") != std::string::npos);
|
||||
}
|
||||
|
||||
env.close();
|
||||
}
|
||||
|
||||
void
|
||||
testBadAlign()
|
||||
{
|
||||
@@ -800,14 +609,14 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
using namespace test::jtx;
|
||||
|
||||
Env env{*this};
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
auto imports = createWasmImport(*hfs);
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto imports = createWasmImport(hfs);
|
||||
|
||||
{ // Calls float_from_uint with bad alignment.
|
||||
// Can be checked through codecov
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
auto re = engine.run(badAlignWasm, "test", {}, imports, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(badAlignWasm, hfs, "test", {}, imports, 1'000'000, env.journal);
|
||||
if (BEAST_EXPECTS(re, transToken(re.error())))
|
||||
{
|
||||
BEAST_EXPECTS(re->result == 0x684f7941, std::to_string(re->result));
|
||||
@@ -822,7 +631,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
using namespace test::jtx;
|
||||
Env env(*this);
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
TestHostFunctions hfs(env, 0);
|
||||
|
||||
testcase("Wasm invalid return type");
|
||||
|
||||
@@ -877,7 +686,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
using namespace test::jtx;
|
||||
Env env(*this);
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
TestHostFunctions hfs(env, 0);
|
||||
|
||||
testcase("Wasm invalid params");
|
||||
|
||||
@@ -988,8 +797,8 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
using namespace test::jtx;
|
||||
|
||||
Env env{*this};
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
auto imports = createWasmImport(*hfs);
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto imports = createWasmImport(hfs);
|
||||
|
||||
// add 1k parameter (max that wasmi support)
|
||||
std::vector<WasmParam> params;
|
||||
@@ -998,28 +807,28 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
{
|
||||
auto re = engine.run(params1k, "test", params, imports, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(params1k, hfs, "test", params, imports, 1'000'000, env.journal);
|
||||
BEAST_EXPECT(re && re->result == 999000);
|
||||
}
|
||||
|
||||
// add 1 more parameter, module can't be created now
|
||||
params.push_back({.type = WT_I32, .of = {.i32 = 2 * 1000}});
|
||||
{
|
||||
auto re = engine.run(params1k1, "test", params, imports, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(params1k1, hfs, "test", params, imports, 1'000'000, env.journal);
|
||||
BEAST_EXPECT(!re);
|
||||
}
|
||||
|
||||
// function that create 10k local variables
|
||||
auto const locals10k = hexToBytes(locals10kHex);
|
||||
{
|
||||
auto re = engine.run(locals10k, "test", wasmParams(0, 1), imports, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(locals10k, hfs, "test", wasmParams(0, 1), imports, 1'000'000, env.journal);
|
||||
BEAST_EXPECT(re && re->result == 890'489'442);
|
||||
}
|
||||
|
||||
// module has 5k functions
|
||||
auto const functions5k = hexToBytes(functions5kHex);
|
||||
{
|
||||
auto re = engine.run(functions5k, "test0001", wasmParams(2, 3), imports, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(functions5k, hfs, "test0001", wasmParams(2, 3), imports, 1'000'000, env.journal);
|
||||
BEAST_EXPECT(re && re->result == 5);
|
||||
}
|
||||
|
||||
@@ -1057,8 +866,8 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
Env env{*this};
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
std::shared_ptr<HostFunctions> hfs(new TestHostFunctions(env, 0));
|
||||
auto imports = createWasmImport(*hfs);
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto imports = createWasmImport(hfs);
|
||||
env.close();
|
||||
|
||||
{
|
||||
@@ -1067,7 +876,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
int64_t cost = -1,
|
||||
std::source_location const location = std::source_location::current()) {
|
||||
auto const lineStr = " (" + std::to_string(location.line()) + ")";
|
||||
auto re = engine.run(code, "all_instructions", {}, imports, hfs, 1'000'000, env.journal);
|
||||
auto re = engine.run(code, hfs, "all_instructions", {}, imports, 1'000'000, env.journal);
|
||||
if (BEAST_EXPECTS(re.has_value() == good, transToken(re.error()) + lineStr) && good)
|
||||
BEAST_EXPECTS(re->cost == cost, std::to_string(re->cost) + lineStr);
|
||||
};
|
||||
@@ -1555,8 +1364,6 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
testWasmLedgerSqn();
|
||||
|
||||
testWasmFib();
|
||||
testWasmSha();
|
||||
testWasmB58();
|
||||
|
||||
testHFCost();
|
||||
testEscrowWasmDN();
|
||||
@@ -1573,7 +1380,6 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
testWasmSectionCorruption();
|
||||
|
||||
testStartFunctionLoop();
|
||||
testBadAlloc();
|
||||
testBadAlign();
|
||||
testReturnType();
|
||||
testSwapBytes();
|
||||
@@ -1581,8 +1387,6 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
testParameterType();
|
||||
|
||||
testOpcodes();
|
||||
|
||||
// perfTest();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
#include <stdint.h>
|
||||
|
||||
static char const b58digits_ordered[] =
|
||||
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
||||
|
||||
uint8_t e_data[32 * 1024];
|
||||
|
||||
void *allocate(int sz)
|
||||
{
|
||||
static int idx = 0;
|
||||
if (idx >= 32)
|
||||
return 0;
|
||||
if (sz > 1024)
|
||||
return 0;
|
||||
return &e_data[idx++ << 10];
|
||||
}
|
||||
|
||||
void deallocate(void *p) {}
|
||||
|
||||
extern int32_t b58enco(char *b58, int32_t b58sz, void const *data,
|
||||
int32_t binsz)
|
||||
{
|
||||
uint8_t const *bin = data;
|
||||
int32_t carry;
|
||||
int32_t i, j, high, zcount = 0;
|
||||
int32_t size;
|
||||
|
||||
while (zcount < binsz && !bin[zcount])
|
||||
++zcount;
|
||||
|
||||
size = (binsz - zcount) * 138 / 100 + 1;
|
||||
uint8_t *buf = allocate(size);
|
||||
if (!buf)
|
||||
return 0;
|
||||
// memset(buf, 0, size);
|
||||
for (i = 0; i < size; ++i)
|
||||
buf[i] = 0;
|
||||
|
||||
for (i = zcount, high = size - 1; i < binsz; ++i, high = j)
|
||||
{
|
||||
for (carry = bin[i], j = size - 1; (j > high) || carry; --j)
|
||||
{
|
||||
carry += 256 * buf[j];
|
||||
buf[j] = carry % 58;
|
||||
carry /= 58;
|
||||
if (!j)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (j = 0; j < size && !buf[j]; ++j)
|
||||
;
|
||||
|
||||
if (b58sz <= zcount + size - j)
|
||||
return 0;
|
||||
|
||||
if (zcount)
|
||||
{
|
||||
// memset(b58, '1', zcount);
|
||||
for (i = 0; i < zcount; ++i)
|
||||
b58[i] = '1';
|
||||
}
|
||||
|
||||
for (i = zcount; j < size; ++i, ++j)
|
||||
b58[i] = b58digits_ordered[buf[j]];
|
||||
b58[i] = '\0';
|
||||
|
||||
return i + 1;
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
#include <stdint.h>
|
||||
|
||||
char buf[1024];
|
||||
|
||||
void *allocate(int sz)
|
||||
{
|
||||
if (!sz)
|
||||
return 0;
|
||||
|
||||
if (sz == 1)
|
||||
return ((void *)(8 * 1024 * 1024));
|
||||
if (sz == 2)
|
||||
return 0;
|
||||
if (sz == 3)
|
||||
return ((void *)(0xFFFFFFFF));
|
||||
|
||||
return &buf[sz];
|
||||
}
|
||||
|
||||
int32_t test(char *p, int32_t sz)
|
||||
{
|
||||
if (!sz)
|
||||
return 0;
|
||||
return p[0];
|
||||
}
|
||||
@@ -9,100 +9,6 @@ extern std::string const fibWasmHex =
|
||||
"0340200041036b100120016a2101200041026b220041044a0d000b200141"
|
||||
"016a0b";
|
||||
|
||||
extern std::string const b58WasmHex =
|
||||
"0061736d0100000001150460000060017f017f60017f0060047f7f7f7f01"
|
||||
"7f0305040001020305030100020607017f0041d0080b074906066d656d6f"
|
||||
"72790200115f5f7761736d5f63616c6c5f63746f7273000008616c6c6f63"
|
||||
"617465000106655f6461746103000a6465616c6c6f636174650002076235"
|
||||
"38656e636f00030ae8050402000b3401017f024020004180084a0d0041c0"
|
||||
"082802002200411f4a0d0041c008200041016a3602002000410a7441d008"
|
||||
"6a21010b20010b02000baa0501097f41d088060240200341004c0d000340"
|
||||
"200220066a2d00000d012003200641016a2206470d000b200321060b2003"
|
||||
"20066b220c418a016c41e4006d220741106a4170716b21090240200c4100"
|
||||
"480d00200741016a220a410771210541002104200741074f0440200a41f8"
|
||||
"ffff1f71210a0340200420096a4200370300200a200441086a2204470d00"
|
||||
"0b0b2005450d00200420096a21040340200441003a0000200441016a2104"
|
||||
"200541016b22050d000b0b200320064a0440200621052007210403402002"
|
||||
"20056a2d0000210802402004220a20074e0440200721042008450d010b20"
|
||||
"0721040340200420096a220b200b2d000041087420086a220b200b413a6d"
|
||||
"2208413a6c6b3a00002004450440410021040c020b200441016b2204200a"
|
||||
"4a0d00200b413a6b418d7f490d000b0b200541016a22052003470d000b0b"
|
||||
"200741016a22022103410021040240200c4100480d000340200420096a2d"
|
||||
"00000d012002200441016a2204470d000b200221040b2001200320066a20"
|
||||
"046b4a047f02402006450d002006410771210841002105200641084f0440"
|
||||
"200641787121010340200020056a42b1e2c48993a6cc9831370000200120"
|
||||
"0541086a2205470d000b0b2008450d00200020056a21050340200541313a"
|
||||
"0000200541016a2105200841016b22080d000b0b0240200420074a0d0020"
|
||||
"0420076a410171047f200405200020066a200420096a2d00004180086a2d"
|
||||
"00003a0000200641016a2106200441016a0b210520042007460d00200520"
|
||||
"096a2101200720056b2102200020066a2103417f21040340200320046a22"
|
||||
"0741016a200120046a220541016a2d00004180086a2d00003a0000200741"
|
||||
"026a200541026a2d00004180086a2d00003a00002002200441026a220447"
|
||||
"0d000b200420066a41016a21060b200020066a41003a0000200641016a05"
|
||||
"41000b0b0b4101004180080b3a3132333435363738394142434445464748"
|
||||
"4a4b4c4d4e505152535455565758595a6162636465666768696a6b6d6e6f"
|
||||
"707172737475767778797a00490f7461726765745f666561747572657304"
|
||||
"2b0f6d757461626c652d676c6f62616c732b087369676e2d6578742b0f72"
|
||||
"65666572656e63652d74797065732b0a6d756c746976616c7565";
|
||||
|
||||
extern std::string const sha512PureWasmHex =
|
||||
"0061736d0100000001130460000060017f017f60017f0060027f7f017f03"
|
||||
"05040001020305030100020607017f0041800d0b075006066d656d6f7279"
|
||||
"0200115f5f7761736d5f63616c6c5f63746f7273000008616c6c6f636174"
|
||||
"65000106655f6461746103000a6465616c6c6f6361746500020e73686135"
|
||||
"31325f70726f6365737300030aa4060402000b0f0041800d410020004180"
|
||||
"80024d1b0b02000b8b0602147e037f200141ff006a41ff014f044041c08c"
|
||||
"06211620014180016d41016bac210b41808d02290300210c41888d022903"
|
||||
"00210d41908d02290300210e41988d02290300210f41a08d022903002110"
|
||||
"41a88d02290300211141b08d02290300211241b88d022903002113034041"
|
||||
"002101200c21092013210a201221082011210520102103200f2115200e21"
|
||||
"06200d2102034020022107200120166a200020016a290000220242388620"
|
||||
"024280fe0383422886842002428080fc0783421886200242808080f80f83"
|
||||
"4208868484200242088842808080f80f832002421888428080fc07838420"
|
||||
"024228884280fe03832002423888848484220237030020014180086a2903"
|
||||
"00200a200322044232892004422e89852004421789857c2008220a200442"
|
||||
"7f858320052208200483847c7c20027c2203200922024224892002421e89"
|
||||
"85200242198985200220072006221485832006200783857c7c2109200320"
|
||||
"157c2103200421052006211520072106200141086a2201418001470d000b"
|
||||
"200020016a21004180092117411e210103402002210620162001410e6b41"
|
||||
"0f714103746a221820182903002016200141056b410f714103746a290300"
|
||||
"20162001410d6b410f714103746a2903002202423f892002423889852002"
|
||||
"420788857c7c20162001410f714103746a2903002202422d892002420389"
|
||||
"852002420688857c22023703002017290300200a20032205423289200542"
|
||||
"2e89852005421789857c2005427f852008832004200583847c7c20027c22"
|
||||
"03200922024224892002421e898520024219898520022006200785832006"
|
||||
"200783857c7c2109200320147c2103201741086a21172008210a20042108"
|
||||
"200521042007211420062107200141016a220141de00470d000b41b88d02"
|
||||
"200a20137c221337030041b08d02200820127c221237030041a88d022004"
|
||||
"20117c221137030041a08d02200320107c221037030041988d02200f2014"
|
||||
"7c220f37030041908d022007200e7c220e37030041888d022002200d7c22"
|
||||
"0d37030041808d022009200c7c220c370300200b420052200b42017d210b"
|
||||
"0d000b0b41808d020b0b880501004180080b800522ae28d7982f8a42cd65"
|
||||
"ef23914437712f3b4deccffbc0b5bcdb8981a5dbb5e938b548f35bc25639"
|
||||
"19d005b6f111f1599b4f19afa4823f9218816ddad55e1cab420203a398aa"
|
||||
"07d8be6f7045015b83128cb2e44ebe853124e2b4ffd5c37d0c556f897bf2"
|
||||
"745dbe72b196163bfeb1de803512c725a706dc9b942669cf74f19bc1d24a"
|
||||
"f19ec1699be4e3254f388647beefb5d58c8bc69dc10f659cac77cca10c24"
|
||||
"75022b596f2ce92d83e4a66eaa84744ad4fb41bddca9b05cb5531183da88"
|
||||
"f976abdf66ee52513e981032b42d6dc631a83f21fb98c82703b0e40eefbe"
|
||||
"c77f59bfc28fa83df30be0c625a70a934791a7d56f8203e05163ca06706e"
|
||||
"0e0a67292914fc2fd246850ab72726c9265c38211b2eed2ac45afc6d2c4d"
|
||||
"dfb3959d130d3853de63af8b54730a65a8b2773cbb0a6a76e6aeed472ec9"
|
||||
"c2813b358214852c72926403f14ca1e8bfa2013042bc4b661aa89197f8d0"
|
||||
"708b4bc230be5406a3516cc71852efd619e892d110a96555240699d62a20"
|
||||
"715785350ef4b8d1bb3270a06a10c8d0d2b816c1a41953ab4151086c371e"
|
||||
"99eb8edf4c774827a8489be1b5bcb034635ac9c5b30c1c39cb8a41e34aaa"
|
||||
"d84e73e363774fca9c5ba3b8b2d6f36f2e68fcb2ef5dee828f74602f1743"
|
||||
"6f63a57872abf0a11478c884ec39641a0802c78c281e6323faffbe90e9bd"
|
||||
"82deeb6c50a41579c6b2f7a3f9be2b5372e3f27871c69c6126eace3e27ca"
|
||||
"07c2c021c7b886d11eebe0cdd67ddaea78d16eee7f4f7df5ba6f1772aa67"
|
||||
"f006a698c8a2c57d630aae0df9be04983f111b471c13350b711b847d0423"
|
||||
"f577db289324c7407babca32bcbec9150abe9e3c4c0d109cc4671d43b642"
|
||||
"3ecbbed4c54c2a7e65fc9c297f59ecfad63aab6fcb5f1758474a8c19446c"
|
||||
"00490f7461726765745f6665617475726573042b0f6d757461626c652d67"
|
||||
"6c6f62616c732b087369676e2d6578742b0f7265666572656e63652d7479"
|
||||
"7065732b0a6d756c746976616c7565";
|
||||
|
||||
extern std::string const ledgerSqnWasmHex =
|
||||
"0061736d01000000010e0360027f7f017f6000006000017f02160103656e760e6765745f6c65646765725f73716e0000030302010205030100"
|
||||
"02063f0a7f01418088040b7f004180080b7f004180080b7f004180080b7f00418088040b7f004180080b7f00418088040b7f00418080080b7f"
|
||||
@@ -342,40 +248,6 @@ extern std::string const deepRecursionHex =
|
||||
"0061736d010000000105016000017f030201000608017f0141c0843d0b070a010666696e69"
|
||||
"736800000a16011400230045044041010f0b230041016b240010000b";
|
||||
|
||||
extern std::string const hfPerfTest =
|
||||
"0061736d0100000001190460057f7f7f7f7f017f60047f7f7f7f017f6000006000017f0236"
|
||||
"0303656e7609666c6f61745f6c6f67000003656e76057472616365000003656e7612747261"
|
||||
"63655f6f70617175655f666c6f617400010303020203050301000206aa011c7f0041b0090b"
|
||||
"7f004193090b7f0041b0080b7f0041c0080b7f0041e0080b7f004180090b7f004180080b7f"
|
||||
"004184080b7f004188080b7f00418c080b7f004190080b7f004194080b7f004198080b7f00"
|
||||
"419c080b7f0041a0080b7f0041a4080b7f0041a8080b7f0041ac080b7f00419b090b7f0041"
|
||||
"80080b7f0041b0110b7f0041b0110b7f0041b091040b7f004180080b7f0041b091040b7f00"
|
||||
"418080080b7f0041000b7f0041010b0785031f066d656d6f72790200115f5f7761736d5f63"
|
||||
"616c6c5f63746f727300030666696e697368000403627566030001610301086572725f6865"
|
||||
"61640302096572725f6461746131030305696e707574030406726573756c74030508495445"
|
||||
"525f4d4158030609484153485f53495a450307084143435f53495a4503080d43555252454e"
|
||||
"43595f53495a4503090b4b45594c45545f53495a45030a0a53465f4163636f756e74030b0e"
|
||||
"53465f44657374696e6174696f6e030c0853465f4d656d6f73030d0753465f4d656d6f030e"
|
||||
"0b53465f4d656d6f44617461030f0753465f4461746103101753465f417574686f72697a65"
|
||||
"43726564656e7469616c730311016203120c5f5f64736f5f68616e646c6503130a5f5f6461"
|
||||
"74615f656e6403140b5f5f737461636b5f6c6f7703150c5f5f737461636b5f686967680316"
|
||||
"0d5f5f676c6f62616c5f6261736503170b5f5f686561705f6261736503180a5f5f68656170"
|
||||
"5f656e6403190d5f5f6d656d6f72795f62617365031a0c5f5f7461626c655f62617365031b"
|
||||
"0a7c0202000b7701017f41807821000340200041b0116a4200370300200041086a22000d00"
|
||||
"0b41c0843d210002400340419309410841b009418008410010004108460440200041016b22"
|
||||
"000d010c020b0b41b008410f41c0084113410010011a0b41e0084111419309410810021a41"
|
||||
"8009411241b009410810021a41010b0b9f0104004180080b53809698002000000014000000"
|
||||
"1400000020000000010008000300080009000f000a000e000d0007001b0007001a000f0066"
|
||||
"6c6f61745f6c6f67206572726f7200696e76616c69642072657475726e2073697a650041e0"
|
||||
"080b11666c6f61745f6c6f6720696e7075743a20004180090b12666c6f61745f6c6f672072"
|
||||
"6573756c743a20004193090b10d48b29430a256d21d920c49ba5e353f8007f0970726f6475"
|
||||
"63657273010c70726f6365737365642d62790105636c616e675f31392e312e352d77617369"
|
||||
"2d73646b202868747470733a2f2f6769746875622e636f6d2f6c6c766d2f6c6c766d2d7072"
|
||||
"6f6a6563742061623462356132646235383239353861663165653330386137393063666462"
|
||||
"3432626432343732302900490f7461726765745f6665617475726573042b0f6d757461626c"
|
||||
"652d676c6f62616c732b087369676e2d6578742b0f7265666572656e63652d74797065732b"
|
||||
"0a6d756c746976616c7565";
|
||||
|
||||
extern std::string const allKeyletsWasmHex =
|
||||
"0061736d0100000001480960067f7f7f7f7f7f017f60047f7f7f7f017f60087f7f7f7f7f7f"
|
||||
"7f7f017f60037f7f7f017f60037f7f7e017f60057f7f7f7f7f017f60057f7f7f7f7f006000"
|
||||
@@ -1284,24 +1156,6 @@ extern std::string const startLoopHex =
|
||||
"0061736d010000000108026000006000017f03030200010712020573746172740000066669"
|
||||
"6e69736800010801000a0e02070003400c000b0b040041010b";
|
||||
|
||||
extern std::string const badAllocHex =
|
||||
"0061736d01000000010f0360000060017f017f60027f7f017f030403000102050301000206"
|
||||
"3e0a7f004180080b7f004180080b7f004180100b7f004180100b7f00418090040b7f004180"
|
||||
"080b7f00418090040b7f00418080080b7f0041000b7f0041010b07b9010e066d656d6f7279"
|
||||
"0200115f5f7761736d5f63616c6c5f63746f7273000008616c6c6f63617465000103627566"
|
||||
"0300047465737400020c5f5f64736f5f68616e646c6503010a5f5f646174615f656e640302"
|
||||
"0b5f5f737461636b5f6c6f7703030c5f5f737461636b5f6869676803040d5f5f676c6f6261"
|
||||
"6c5f6261736503050b5f5f686561705f6261736503060a5f5f686561705f656e6403070d5f"
|
||||
"5f6d656d6f72795f6261736503080c5f5f7461626c655f6261736503090a420302000b2c01"
|
||||
"017f024002400240024020000e0403000301020b41808080040f0b417f0f0b20004180086a"
|
||||
"21010b20010b1000200145044041000f0b20002c00000b007f0970726f647563657273010c"
|
||||
"70726f6365737365642d62790105636c616e675f31392e312e352d776173692d73646b2028"
|
||||
"68747470733a2f2f6769746875622e636f6d2f6c6c766d2f6c6c766d2d70726f6a65637420"
|
||||
"61623462356132646235383239353861663165653330386137393063666462343262643234"
|
||||
"3732302900490f7461726765745f6665617475726573042b0f6d757461626c652d676c6f62"
|
||||
"616c732b087369676e2d6578742b0f7265666572656e63652d74797065732b0a6d756c7469"
|
||||
"76616c7565";
|
||||
|
||||
extern std::string const badAlignWasmHex =
|
||||
"0061736d01000000011b046000017f60057f7f7f7f7f017f60067f7f7f7f7f7f017f600000022a0203656e760f666c6f61745f66726f6d5f75"
|
||||
"696e74000103656e760c636865636b5f6b65796c6574000203050403000000050301000306470b7f004180080b7f00418088020b7f00418008"
|
||||
|
||||
@@ -5,27 +5,14 @@
|
||||
#include <string>
|
||||
|
||||
extern std::string const ledgerSqnWasmHex;
|
||||
|
||||
extern std::string const allHostFunctionsWasmHex;
|
||||
|
||||
extern std::string const deepRecursionHex;
|
||||
extern std::string const allKeyletsWasmHex;
|
||||
extern std::string const codecovTestsWasmHex;
|
||||
|
||||
extern std::string const fibWasmHex;
|
||||
|
||||
extern std::string const b58WasmHex;
|
||||
|
||||
extern std::string const sha512PureWasmHex;
|
||||
|
||||
extern std::string const hfPerfTest;
|
||||
|
||||
extern std::string const allKeyletsWasmHex;
|
||||
|
||||
extern std::string const codecovTestsWasmHex;
|
||||
|
||||
extern std::string const floatTestsWasmHex;
|
||||
|
||||
extern std::string const float0Hex;
|
||||
|
||||
extern std::string const disabledFloatHex;
|
||||
|
||||
extern std::string const memoryPointerAtLimitHex;
|
||||
@@ -80,10 +67,10 @@ extern std::string const junkAfterSectionHex;
|
||||
extern std::string const invalidSectionIdHex;
|
||||
extern std::string const localVariableBombHex;
|
||||
|
||||
extern std::string const deepRecursionHex;
|
||||
extern std::string const infiniteLoopWasmHex;
|
||||
extern std::string const startLoopHex;
|
||||
|
||||
extern std::string const badAllocHex;
|
||||
extern std::string const badAlignWasmHex;
|
||||
|
||||
extern std::string const thousandParamsHex;
|
||||
|
||||
@@ -1,135 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static uint64_t const K512[] = {
|
||||
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f,
|
||||
0xe9b5dba58189dbbc, 0x3956c25bf348b538, 0x59f111f1b605d019,
|
||||
0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242,
|
||||
0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
|
||||
0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
|
||||
0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3,
|
||||
0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, 0x2de92c6f592b0275,
|
||||
0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
|
||||
0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f,
|
||||
0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
|
||||
0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc,
|
||||
0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
|
||||
0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6,
|
||||
0x92722c851482353b, 0xa2bfe8a14cf10364, 0xa81a664bbc423001,
|
||||
0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
|
||||
0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
|
||||
0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99,
|
||||
0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,
|
||||
0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc,
|
||||
0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
|
||||
0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915,
|
||||
0xc67178f2e372532b, 0xca273eceea26619c, 0xd186b8c721c0c207,
|
||||
0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba,
|
||||
0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
|
||||
0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
|
||||
0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,
|
||||
0x5fcb6fab3ad6faec, 0x6c44198c4a475817};
|
||||
|
||||
#define ROTATE(x, y) (((x) >> (y)) | ((x) << (64 - (y))))
|
||||
#define Sigma0(x) (ROTATE((x), 28) ^ ROTATE((x), 34) ^ ROTATE((x), 39))
|
||||
#define Sigma1(x) (ROTATE((x), 14) ^ ROTATE((x), 18) ^ ROTATE((x), 41))
|
||||
#define sigma0(x) (ROTATE((x), 1) ^ ROTATE((x), 8) ^ ((x) >> 7))
|
||||
#define sigma1(x) (ROTATE((x), 19) ^ ROTATE((x), 61) ^ ((x) >> 6))
|
||||
|
||||
#define Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z)))
|
||||
#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
||||
|
||||
static inline uint64_t B2U64(uint8_t val, uint8_t sh)
|
||||
{
|
||||
return ((uint64_t)val) << sh;
|
||||
}
|
||||
|
||||
void *allocate(int sz) { return malloc(sz); }
|
||||
void deallocate(void *p) { free(p); }
|
||||
|
||||
uint8_t e_data[32 * 1024];
|
||||
|
||||
uint8_t *sha512_process(uint8_t const *data, int32_t length)
|
||||
{
|
||||
static uint64_t state[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
uint64_t a, b, c, d, e, f, g, h, s0, s1, T1, T2;
|
||||
uint64_t X[16];
|
||||
|
||||
uint64_t blocks = length / 128;
|
||||
while (blocks--)
|
||||
{
|
||||
a = state[0];
|
||||
b = state[1];
|
||||
c = state[2];
|
||||
d = state[3];
|
||||
e = state[4];
|
||||
f = state[5];
|
||||
g = state[6];
|
||||
h = state[7];
|
||||
|
||||
unsigned i;
|
||||
for (i = 0; i < 16; i++)
|
||||
{
|
||||
X[i] = B2U64(data[0], 56) | B2U64(data[1], 48) | B2U64(data[2], 40) |
|
||||
B2U64(data[3], 32) | B2U64(data[4], 24) | B2U64(data[5], 16) |
|
||||
B2U64(data[6], 8) | B2U64(data[7], 0);
|
||||
data += 8;
|
||||
|
||||
T1 = h;
|
||||
T1 += Sigma1(e);
|
||||
T1 += Ch(e, f, g);
|
||||
T1 += K512[i];
|
||||
T1 += X[i];
|
||||
|
||||
T2 = Sigma0(a);
|
||||
T2 += Maj(a, b, c);
|
||||
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + T1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = T1 + T2;
|
||||
}
|
||||
|
||||
for (i = 16; i < 80; i++)
|
||||
{
|
||||
s0 = X[(i + 1) & 0x0f];
|
||||
s0 = sigma0(s0);
|
||||
s1 = X[(i + 14) & 0x0f];
|
||||
s1 = sigma1(s1);
|
||||
|
||||
T1 = X[i & 0xf] += s0 + s1 + X[(i + 9) & 0xf];
|
||||
T1 += h + Sigma1(e) + Ch(e, f, g) + K512[i];
|
||||
T2 = Sigma0(a) + Maj(a, b, c);
|
||||
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + T1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = T1 + T2;
|
||||
}
|
||||
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
state[4] += e;
|
||||
state[5] += f;
|
||||
state[6] += g;
|
||||
state[7] += h;
|
||||
}
|
||||
|
||||
return (uint8_t *)(state);
|
||||
}
|
||||
|
||||
// int main ()
|
||||
//{
|
||||
// return 0;
|
||||
// }
|
||||
@@ -104,56 +104,50 @@ struct HostFunctions
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::int64_t
|
||||
getGas()
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
void
|
||||
setGas(std::int64_t)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
beast::Journal
|
||||
getJournal()
|
||||
getJournal() const
|
||||
{
|
||||
return j_;
|
||||
}
|
||||
|
||||
virtual bool
|
||||
checkSelf() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual Expected<std::uint32_t, HostFunctionError>
|
||||
getLedgerSqn()
|
||||
getLedgerSqn() const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<std::uint32_t, HostFunctionError>
|
||||
getParentLedgerTime()
|
||||
getParentLedgerTime() const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Hash, HostFunctionError>
|
||||
getParentLedgerHash()
|
||||
getParentLedgerHash() const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<uint32_t, HostFunctionError>
|
||||
getBaseFee()
|
||||
getBaseFee() const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
isAmendmentEnabled(uint256 const& amendmentId)
|
||||
isAmendmentEnabled(uint256 const& amendmentId) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
isAmendmentEnabled(std::string_view const& amendmentName)
|
||||
isAmendmentEnabled(std::string_view const& amendmentName) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
@@ -165,73 +159,73 @@ struct HostFunctions
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
getTxField(SField const& fname)
|
||||
getTxField(SField const& fname) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
getCurrentLedgerObjField(SField const& fname)
|
||||
getCurrentLedgerObjField(SField const& fname) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
getLedgerObjField(int32_t cacheIdx, SField const& fname)
|
||||
getLedgerObjField(int32_t cacheIdx, SField const& fname) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
getTxNestedField(Slice const& locator)
|
||||
getTxNestedField(Slice const& locator) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
getCurrentLedgerObjNestedField(Slice const& locator)
|
||||
getCurrentLedgerObjNestedField(Slice const& locator) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
getLedgerObjNestedField(int32_t cacheIdx, Slice const& locator)
|
||||
getLedgerObjNestedField(int32_t cacheIdx, Slice const& locator) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
getTxArrayLen(SField const& fname)
|
||||
getTxArrayLen(SField const& fname) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
getCurrentLedgerObjArrayLen(SField const& fname)
|
||||
getCurrentLedgerObjArrayLen(SField const& fname) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
getLedgerObjArrayLen(int32_t cacheIdx, SField const& fname)
|
||||
getLedgerObjArrayLen(int32_t cacheIdx, SField const& fname) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
getTxNestedArrayLen(Slice const& locator)
|
||||
getTxNestedArrayLen(Slice const& locator) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
getCurrentLedgerObjNestedArrayLen(Slice const& locator)
|
||||
getCurrentLedgerObjNestedArrayLen(Slice const& locator) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
getLedgerObjNestedArrayLen(int32_t cacheIdx, Slice const& locator)
|
||||
getLedgerObjNestedArrayLen(int32_t cacheIdx, Slice const& locator) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
@@ -243,259 +237,259 @@ struct HostFunctions
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
checkSignature(Slice const& message, Slice const& signature, Slice const& pubkey)
|
||||
checkSignature(Slice const& message, Slice const& signature, Slice const& pubkey) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Hash, HostFunctionError>
|
||||
computeSha512HalfHash(Slice const& data)
|
||||
computeSha512HalfHash(Slice const& data) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
accountKeylet(AccountID const& account)
|
||||
accountKeylet(AccountID const& account) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
ammKeylet(Asset const& issue1, Asset const& issue2)
|
||||
ammKeylet(Asset const& issue1, Asset const& issue2) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
checkKeylet(AccountID const& account, std::uint32_t seq)
|
||||
checkKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
credentialKeylet(AccountID const& subject, AccountID const& issuer, Slice const& credentialType)
|
||||
credentialKeylet(AccountID const& subject, AccountID const& issuer, Slice const& credentialType) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
didKeylet(AccountID const& account)
|
||||
didKeylet(AccountID const& account) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
delegateKeylet(AccountID const& account, AccountID const& authorize)
|
||||
delegateKeylet(AccountID const& account, AccountID const& authorize) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
depositPreauthKeylet(AccountID const& account, AccountID const& authorize)
|
||||
depositPreauthKeylet(AccountID const& account, AccountID const& authorize) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
escrowKeylet(AccountID const& account, std::uint32_t seq)
|
||||
escrowKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
lineKeylet(AccountID const& account1, AccountID const& account2, Currency const& currency)
|
||||
lineKeylet(AccountID const& account1, AccountID const& account2, Currency const& currency) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
mptIssuanceKeylet(AccountID const& issuer, std::uint32_t seq)
|
||||
mptIssuanceKeylet(AccountID const& issuer, std::uint32_t seq) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
mptokenKeylet(MPTID const& mptid, AccountID const& holder)
|
||||
mptokenKeylet(MPTID const& mptid, AccountID const& holder) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
nftOfferKeylet(AccountID const& account, std::uint32_t seq)
|
||||
nftOfferKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
offerKeylet(AccountID const& account, std::uint32_t seq)
|
||||
offerKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
oracleKeylet(AccountID const& account, std::uint32_t docId)
|
||||
oracleKeylet(AccountID const& account, std::uint32_t docId) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
paychanKeylet(AccountID const& account, AccountID const& destination, std::uint32_t seq)
|
||||
paychanKeylet(AccountID const& account, AccountID const& destination, std::uint32_t seq) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
permissionedDomainKeylet(AccountID const& account, std::uint32_t seq)
|
||||
permissionedDomainKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
signersKeylet(AccountID const& account)
|
||||
signersKeylet(AccountID const& account) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
ticketKeylet(AccountID const& account, std::uint32_t seq)
|
||||
ticketKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
vaultKeylet(AccountID const& account, std::uint32_t seq)
|
||||
vaultKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
getNFT(AccountID const& account, uint256 const& nftId)
|
||||
getNFT(AccountID const& account, uint256 const& nftId) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
getNFTIssuer(uint256 const& nftId)
|
||||
getNFTIssuer(uint256 const& nftId) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<std::uint32_t, HostFunctionError>
|
||||
getNFTTaxon(uint256 const& nftId)
|
||||
getNFTTaxon(uint256 const& nftId) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
getNFTFlags(uint256 const& nftId)
|
||||
getNFTFlags(uint256 const& nftId) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
getNFTTransferFee(uint256 const& nftId)
|
||||
getNFTTransferFee(uint256 const& nftId) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<std::uint32_t, HostFunctionError>
|
||||
getNFTSerial(uint256 const& nftId)
|
||||
getNFTSerial(uint256 const& nftId) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
trace(std::string_view const& msg, Slice const& data, bool asHex)
|
||||
trace(std::string_view const& msg, Slice const& data, bool asHex) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
traceNum(std::string_view const& msg, int64_t data)
|
||||
traceNum(std::string_view const& msg, int64_t data) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
traceAccount(std::string_view const& msg, AccountID const& account)
|
||||
traceAccount(std::string_view const& msg, AccountID const& account) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
traceFloat(std::string_view const& msg, Slice const& data)
|
||||
traceFloat(std::string_view const& msg, Slice const& data) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
traceAmount(std::string_view const& msg, STAmount const& amount)
|
||||
traceAmount(std::string_view const& msg, STAmount const& amount) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
floatFromInt(int64_t x, int32_t mode)
|
||||
floatFromInt(int64_t x, int32_t mode) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
floatFromUint(uint64_t x, int32_t mode)
|
||||
floatFromUint(uint64_t x, int32_t mode) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
floatSet(int64_t mantissa, int32_t exponent, int32_t mode)
|
||||
floatSet(int64_t mantissa, int32_t exponent, int32_t mode) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<int32_t, HostFunctionError>
|
||||
floatCompare(Slice const& x, Slice const& y)
|
||||
floatCompare(Slice const& x, Slice const& y) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
floatAdd(Slice const& x, Slice const& y, int32_t mode)
|
||||
floatAdd(Slice const& x, Slice const& y, int32_t mode) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
floatSubtract(Slice const& x, Slice const& y, int32_t mode)
|
||||
floatSubtract(Slice const& x, Slice const& y, int32_t mode) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
floatMultiply(Slice const& x, Slice const& y, int32_t mode)
|
||||
floatMultiply(Slice const& x, Slice const& y, int32_t mode) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
floatDivide(Slice const& x, Slice const& y, int32_t mode)
|
||||
floatDivide(Slice const& x, Slice const& y, int32_t mode) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
floatRoot(Slice const& x, int32_t n, int32_t mode)
|
||||
floatRoot(Slice const& x, int32_t n, int32_t mode) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
floatPower(Slice const& x, int32_t n, int32_t mode)
|
||||
floatPower(Slice const& x, int32_t n, int32_t mode) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
virtual Expected<Bytes, HostFunctionError>
|
||||
floatLog(Slice const& x, int32_t mode)
|
||||
floatLog(Slice const& x, int32_t mode) const
|
||||
{
|
||||
return Unexpected(HostFunctionError::INTERNAL);
|
||||
}
|
||||
|
||||
@@ -7,44 +7,42 @@
|
||||
namespace xrpl {
|
||||
class WasmHostFunctionsImpl : public HostFunctions
|
||||
{
|
||||
ApplyContext& ctx;
|
||||
Keylet leKey;
|
||||
std::shared_ptr<SLE const> currentLedgerObj = nullptr;
|
||||
bool isLedgerObjCached = false;
|
||||
ApplyContext& ctx_;
|
||||
|
||||
Keylet leKey_;
|
||||
mutable std::optional<std::shared_ptr<SLE const>> currentLedgerObj_;
|
||||
|
||||
static int constexpr MAX_CACHE = 256;
|
||||
std::array<std::shared_ptr<SLE const>, MAX_CACHE> cache;
|
||||
std::array<std::shared_ptr<SLE const>, MAX_CACHE> cache_;
|
||||
|
||||
std::optional<Bytes> data_;
|
||||
|
||||
void const* rt_ = nullptr;
|
||||
|
||||
Expected<std::shared_ptr<SLE const>, HostFunctionError>
|
||||
getCurrentLedgerObj()
|
||||
getCurrentLedgerObj() const
|
||||
{
|
||||
if (!isLedgerObjCached)
|
||||
{
|
||||
isLedgerObjCached = true;
|
||||
currentLedgerObj = ctx.view().read(leKey);
|
||||
}
|
||||
if (currentLedgerObj)
|
||||
return currentLedgerObj;
|
||||
if (!currentLedgerObj_)
|
||||
currentLedgerObj_ = ctx_.view().read(leKey_);
|
||||
if (*currentLedgerObj_)
|
||||
return *currentLedgerObj_;
|
||||
return Unexpected(HostFunctionError::LEDGER_OBJ_NOT_FOUND);
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
normalizeCacheIndex(int32_t cacheIdx)
|
||||
normalizeCacheIndex(int32_t cacheIdx) const
|
||||
{
|
||||
--cacheIdx;
|
||||
if (cacheIdx < 0 || cacheIdx >= MAX_CACHE)
|
||||
return Unexpected(HostFunctionError::SLOT_OUT_RANGE);
|
||||
if (!cache[cacheIdx])
|
||||
if (!cache_[cacheIdx])
|
||||
return Unexpected(HostFunctionError::EMPTY_SLOT);
|
||||
return cacheIdx;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
void
|
||||
log(std::string_view const& msg, F&& dataFn)
|
||||
log(std::string_view const& msg, F&& dataFn) const
|
||||
{
|
||||
#ifdef DEBUG_OUTPUT
|
||||
auto& j = std::cerr;
|
||||
@@ -53,7 +51,7 @@ class WasmHostFunctionsImpl : public HostFunctions
|
||||
return;
|
||||
auto j = getJournal().trace();
|
||||
#endif
|
||||
j << "WasmTrace[" << to_short_string(leKey.key) << "]: " << msg << " " << dataFn();
|
||||
j << "WasmTrace[" << to_short_string(leKey_.key) << "]: " << msg << " " << dataFn();
|
||||
|
||||
#ifdef DEBUG_OUTPUT
|
||||
j << std::endl;
|
||||
@@ -61,7 +59,7 @@ class WasmHostFunctionsImpl : public HostFunctions
|
||||
}
|
||||
|
||||
public:
|
||||
WasmHostFunctionsImpl(ApplyContext& ct, Keylet const& leKey) : HostFunctions(ct.journal), ctx(ct), leKey(leKey)
|
||||
WasmHostFunctionsImpl(ApplyContext& ct, Keylet const& leKey) : HostFunctions(ct.journal), ctx_(ct), leKey_(leKey)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -77,6 +75,13 @@ public:
|
||||
return rt_;
|
||||
}
|
||||
|
||||
virtual bool
|
||||
checkSelf() const override
|
||||
{
|
||||
return !currentLedgerObj_ && !data_ &&
|
||||
std::ranges::find_if(cache_, [](auto& p) { return !!p; }) == cache_.end();
|
||||
}
|
||||
|
||||
std::optional<Bytes> const&
|
||||
getData() const
|
||||
{
|
||||
@@ -84,193 +89,193 @@ public:
|
||||
}
|
||||
|
||||
Expected<std::uint32_t, HostFunctionError>
|
||||
getLedgerSqn() override;
|
||||
getLedgerSqn() const override;
|
||||
|
||||
Expected<std::uint32_t, HostFunctionError>
|
||||
getParentLedgerTime() override;
|
||||
getParentLedgerTime() const override;
|
||||
|
||||
Expected<Hash, HostFunctionError>
|
||||
getParentLedgerHash() override;
|
||||
getParentLedgerHash() const override;
|
||||
|
||||
Expected<std::uint32_t, HostFunctionError>
|
||||
getBaseFee() override;
|
||||
getBaseFee() const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
isAmendmentEnabled(uint256 const& amendmentId) override;
|
||||
isAmendmentEnabled(uint256 const& amendmentId) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
isAmendmentEnabled(std::string_view const& amendmentName) override;
|
||||
isAmendmentEnabled(std::string_view const& amendmentName) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
cacheLedgerObj(uint256 const& objId, int32_t cacheIdx) override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
getTxField(SField const& fname) override;
|
||||
getTxField(SField const& fname) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
getCurrentLedgerObjField(SField const& fname) override;
|
||||
getCurrentLedgerObjField(SField const& fname) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
getLedgerObjField(int32_t cacheIdx, SField const& fname) override;
|
||||
getLedgerObjField(int32_t cacheIdx, SField const& fname) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
getTxNestedField(Slice const& locator) override;
|
||||
getTxNestedField(Slice const& locator) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
getCurrentLedgerObjNestedField(Slice const& locator) override;
|
||||
getCurrentLedgerObjNestedField(Slice const& locator) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
getLedgerObjNestedField(int32_t cacheIdx, Slice const& locator) override;
|
||||
getLedgerObjNestedField(int32_t cacheIdx, Slice const& locator) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
getTxArrayLen(SField const& fname) override;
|
||||
getTxArrayLen(SField const& fname) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
getCurrentLedgerObjArrayLen(SField const& fname) override;
|
||||
getCurrentLedgerObjArrayLen(SField const& fname) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
getLedgerObjArrayLen(int32_t cacheIdx, SField const& fname) override;
|
||||
getLedgerObjArrayLen(int32_t cacheIdx, SField const& fname) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
getTxNestedArrayLen(Slice const& locator) override;
|
||||
getTxNestedArrayLen(Slice const& locator) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
getCurrentLedgerObjNestedArrayLen(Slice const& locator) override;
|
||||
getCurrentLedgerObjNestedArrayLen(Slice const& locator) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
getLedgerObjNestedArrayLen(int32_t cacheIdx, Slice const& locator) override;
|
||||
getLedgerObjNestedArrayLen(int32_t cacheIdx, Slice const& locator) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
updateData(Slice const& data) override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
checkSignature(Slice const& message, Slice const& signature, Slice const& pubkey) override;
|
||||
checkSignature(Slice const& message, Slice const& signature, Slice const& pubkey) const override;
|
||||
|
||||
Expected<Hash, HostFunctionError>
|
||||
computeSha512HalfHash(Slice const& data) override;
|
||||
computeSha512HalfHash(Slice const& data) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
accountKeylet(AccountID const& account) override;
|
||||
accountKeylet(AccountID const& account) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
ammKeylet(Asset const& issue1, Asset const& issue2) override;
|
||||
ammKeylet(Asset const& issue1, Asset const& issue2) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
checkKeylet(AccountID const& account, std::uint32_t seq) override;
|
||||
checkKeylet(AccountID const& account, std::uint32_t seq) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
credentialKeylet(AccountID const& subject, AccountID const& issuer, Slice const& credentialType) override;
|
||||
credentialKeylet(AccountID const& subject, AccountID const& issuer, Slice const& credentialType) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
didKeylet(AccountID const& account) override;
|
||||
didKeylet(AccountID const& account) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
delegateKeylet(AccountID const& account, AccountID const& authorize) override;
|
||||
delegateKeylet(AccountID const& account, AccountID const& authorize) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
depositPreauthKeylet(AccountID const& account, AccountID const& authorize) override;
|
||||
depositPreauthKeylet(AccountID const& account, AccountID const& authorize) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
escrowKeylet(AccountID const& account, std::uint32_t seq) override;
|
||||
escrowKeylet(AccountID const& account, std::uint32_t seq) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
lineKeylet(AccountID const& account1, AccountID const& account2, Currency const& currency) override;
|
||||
lineKeylet(AccountID const& account1, AccountID const& account2, Currency const& currency) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
mptIssuanceKeylet(AccountID const& issuer, std::uint32_t seq) override;
|
||||
mptIssuanceKeylet(AccountID const& issuer, std::uint32_t seq) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
mptokenKeylet(MPTID const& mptid, AccountID const& holder) override;
|
||||
mptokenKeylet(MPTID const& mptid, AccountID const& holder) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
nftOfferKeylet(AccountID const& account, std::uint32_t seq) override;
|
||||
nftOfferKeylet(AccountID const& account, std::uint32_t seq) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
offerKeylet(AccountID const& account, std::uint32_t seq) override;
|
||||
offerKeylet(AccountID const& account, std::uint32_t seq) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
oracleKeylet(AccountID const& account, std::uint32_t docId) override;
|
||||
oracleKeylet(AccountID const& account, std::uint32_t docId) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
paychanKeylet(AccountID const& account, AccountID const& destination, std::uint32_t seq) override;
|
||||
paychanKeylet(AccountID const& account, AccountID const& destination, std::uint32_t seq) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
permissionedDomainKeylet(AccountID const& account, std::uint32_t seq) override;
|
||||
permissionedDomainKeylet(AccountID const& account, std::uint32_t seq) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
signersKeylet(AccountID const& account) override;
|
||||
signersKeylet(AccountID const& account) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
ticketKeylet(AccountID const& account, std::uint32_t seq) override;
|
||||
ticketKeylet(AccountID const& account, std::uint32_t seq) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
vaultKeylet(AccountID const& account, std::uint32_t seq) override;
|
||||
vaultKeylet(AccountID const& account, std::uint32_t seq) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
getNFT(AccountID const& account, uint256 const& nftId) override;
|
||||
getNFT(AccountID const& account, uint256 const& nftId) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
getNFTIssuer(uint256 const& nftId) override;
|
||||
getNFTIssuer(uint256 const& nftId) const override;
|
||||
|
||||
Expected<std::uint32_t, HostFunctionError>
|
||||
getNFTTaxon(uint256 const& nftId) override;
|
||||
getNFTTaxon(uint256 const& nftId) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
getNFTFlags(uint256 const& nftId) override;
|
||||
getNFTFlags(uint256 const& nftId) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
getNFTTransferFee(uint256 const& nftId) override;
|
||||
getNFTTransferFee(uint256 const& nftId) const override;
|
||||
|
||||
Expected<std::uint32_t, HostFunctionError>
|
||||
getNFTSerial(uint256 const& nftId) override;
|
||||
getNFTSerial(uint256 const& nftId) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
trace(std::string_view const& msg, Slice const& data, bool asHex) override;
|
||||
trace(std::string_view const& msg, Slice const& data, bool asHex) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
traceNum(std::string_view const& msg, int64_t data) override;
|
||||
traceNum(std::string_view const& msg, int64_t data) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
traceAccount(std::string_view const& msg, AccountID const& account) override;
|
||||
traceAccount(std::string_view const& msg, AccountID const& account) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
traceFloat(std::string_view const& msg, Slice const& data) override;
|
||||
traceFloat(std::string_view const& msg, Slice const& data) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
traceAmount(std::string_view const& msg, STAmount const& amount) override;
|
||||
traceAmount(std::string_view const& msg, STAmount const& amount) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
floatFromInt(int64_t x, int32_t mode) override;
|
||||
floatFromInt(int64_t x, int32_t mode) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
floatFromUint(uint64_t x, int32_t mode) override;
|
||||
floatFromUint(uint64_t x, int32_t mode) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
floatSet(int64_t mantissa, int32_t exponent, int32_t mode) override;
|
||||
floatSet(int64_t mantissa, int32_t exponent, int32_t mode) const override;
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
floatCompare(Slice const& x, Slice const& y) override;
|
||||
floatCompare(Slice const& x, Slice const& y) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
floatAdd(Slice const& x, Slice const& y, int32_t mode) override;
|
||||
floatAdd(Slice const& x, Slice const& y, int32_t mode) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
floatSubtract(Slice const& x, Slice const& y, int32_t mode) override;
|
||||
floatSubtract(Slice const& x, Slice const& y, int32_t mode) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
floatMultiply(Slice const& x, Slice const& y, int32_t mode) override;
|
||||
floatMultiply(Slice const& x, Slice const& y, int32_t mode) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
floatDivide(Slice const& x, Slice const& y, int32_t mode) override;
|
||||
floatDivide(Slice const& x, Slice const& y, int32_t mode) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
floatRoot(Slice const& x, int32_t n, int32_t mode) override;
|
||||
floatRoot(Slice const& x, int32_t n, int32_t mode) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
floatPower(Slice const& x, int32_t n, int32_t mode) override;
|
||||
floatPower(Slice const& x, int32_t n, int32_t mode) const override;
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
floatLog(Slice const& x, int32_t mode) override;
|
||||
floatLog(Slice const& x, int32_t mode) const override;
|
||||
};
|
||||
|
||||
namespace wasm_float {
|
||||
|
||||
@@ -173,52 +173,6 @@ wasmParamsHlp(std::vector<WasmParam>& v, std::int64_t p, Types&&... args)
|
||||
// wasmParamsHlp(v, std::forward<Types>(args)...);
|
||||
// }
|
||||
|
||||
template <class... Types>
|
||||
inline void
|
||||
wasmParamsHlp(std::vector<WasmParam>& v, std::uint8_t const* dt, std::int32_t sz, Types&&... args)
|
||||
{
|
||||
v.push_back({.type = WT_U8V, .of = {.u8v = {.d = dt, .sz = sz}}});
|
||||
wasmParamsHlp(v, std::forward<Types>(args)...);
|
||||
}
|
||||
|
||||
template <class... Types>
|
||||
inline void
|
||||
wasmParamsHlp(std::vector<WasmParam>& v, Bytes const& p, Types&&... args)
|
||||
{
|
||||
if (p.size() > std::numeric_limits<int32_t>::max())
|
||||
throw std::runtime_error("can't allocate memory, size: " + std::to_string(p.size())); // LCOV_EXCL_LINE
|
||||
|
||||
wasmParamsHlp(v, p.data(), static_cast<std::int32_t>(p.size()), std::forward<Types>(args)...);
|
||||
}
|
||||
|
||||
template <class... Types>
|
||||
inline void
|
||||
wasmParamsHlp(std::vector<WasmParam>& v, std::string_view const& p, Types&&... args)
|
||||
{
|
||||
if (p.size() > std::numeric_limits<int32_t>::max())
|
||||
throw std::runtime_error("can't allocate memory, size: " + std::to_string(p.size()));
|
||||
|
||||
wasmParamsHlp(
|
||||
v,
|
||||
reinterpret_cast<std::uint8_t const*>(p.data()),
|
||||
static_cast<std::int32_t>(p.size()),
|
||||
std::forward<Types>(args)...);
|
||||
}
|
||||
|
||||
template <class... Types>
|
||||
inline void
|
||||
wasmParamsHlp(std::vector<WasmParam>& v, std::string const& p, Types&&... args)
|
||||
{
|
||||
if (p.size() > std::numeric_limits<int32_t>::max())
|
||||
throw std::runtime_error("can't allocate memory, size: " + std::to_string(p.size())); // LCOV_EXCL_LINE
|
||||
|
||||
wasmParamsHlp(
|
||||
v,
|
||||
reinterpret_cast<std::uint8_t const*>(p.c_str()),
|
||||
static_cast<std::int32_t>(p.size()),
|
||||
std::forward<Types>(args)...);
|
||||
}
|
||||
|
||||
inline void
|
||||
wasmParamsHlp(std::vector<WasmParam>& v)
|
||||
{
|
||||
|
||||
@@ -24,7 +24,7 @@ class WasmiEngine;
|
||||
|
||||
class WasmEngine
|
||||
{
|
||||
std::unique_ptr<WasmiEngine> const impl;
|
||||
std::unique_ptr<WasmiEngine> const impl_;
|
||||
|
||||
WasmEngine();
|
||||
|
||||
@@ -43,20 +43,20 @@ public:
|
||||
|
||||
Expected<WasmResult<int32_t>, TER>
|
||||
run(Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName = {},
|
||||
std::vector<WasmParam> const& params = {},
|
||||
std::shared_ptr<ImportVec> const& imports = {},
|
||||
std::shared_ptr<HostFunctions> const& hfs = {},
|
||||
ImportVec const& imports = {},
|
||||
int64_t gasLimit = -1,
|
||||
beast::Journal j = beast::Journal{beast::Journal::getNullSink()});
|
||||
|
||||
NotTEC
|
||||
check(
|
||||
Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params = {},
|
||||
std::shared_ptr<ImportVec> const& imports = {},
|
||||
std::shared_ptr<HostFunctions> const& hfs = {},
|
||||
ImportVec const& imports = {},
|
||||
beast::Journal j = beast::Journal{beast::Journal::getNullSink()});
|
||||
|
||||
// Host functions helper functionality
|
||||
@@ -69,13 +69,13 @@ public:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::shared_ptr<ImportVec>
|
||||
ImportVec
|
||||
createWasmImport(HostFunctions& hfs);
|
||||
|
||||
Expected<EscrowResult, TER>
|
||||
runEscrowWasm(
|
||||
Bytes const& wasmCode,
|
||||
std::shared_ptr<HostFunctions> const& hfs,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName = ESCROW_FUNCTION_NAME,
|
||||
std::vector<WasmParam> const& params = {},
|
||||
int64_t gasLimit = -1);
|
||||
@@ -83,7 +83,7 @@ runEscrowWasm(
|
||||
NotTEC
|
||||
preflightEscrowWasm(
|
||||
Bytes const& wasmCode,
|
||||
std::shared_ptr<HostFunctions> const& hfs,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName = ESCROW_FUNCTION_NAME,
|
||||
std::vector<WasmParam> const& params = {});
|
||||
|
||||
|
||||
@@ -145,12 +145,7 @@ public:
|
||||
ModuleWrapper(ModuleWrapper&& o);
|
||||
ModuleWrapper&
|
||||
operator=(ModuleWrapper&& o);
|
||||
ModuleWrapper(
|
||||
StorePtr& s,
|
||||
Bytes const& wasmBin,
|
||||
bool instantiate,
|
||||
std::shared_ptr<ImportVec> const& imports,
|
||||
beast::Journal j);
|
||||
ModuleWrapper(StorePtr& s, Bytes const& wasmBin, bool instantiate, ImportVec const& imports, beast::Journal j);
|
||||
~ModuleWrapper() = default;
|
||||
|
||||
operator bool() const;
|
||||
@@ -175,7 +170,7 @@ public:
|
||||
|
||||
private:
|
||||
WasmExternVec
|
||||
buildImports(StorePtr& s, std::shared_ptr<ImportVec> const& imports);
|
||||
buildImports(StorePtr& s, ImportVec const& imports);
|
||||
};
|
||||
|
||||
class WasmiEngine
|
||||
@@ -187,10 +182,6 @@ class WasmiEngine
|
||||
|
||||
std::mutex m_; // 1 instance mutex
|
||||
|
||||
// to ensure lifetime during next executions with the same module
|
||||
std::shared_ptr<ImportVec> imports_;
|
||||
std::shared_ptr<HostFunctions> hfs_;
|
||||
|
||||
public:
|
||||
WasmiEngine();
|
||||
~WasmiEngine() = default;
|
||||
@@ -200,24 +191,24 @@ public:
|
||||
|
||||
Expected<WasmResult<int32_t>, TER>
|
||||
run(Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params,
|
||||
std::shared_ptr<ImportVec> const& imports,
|
||||
std::shared_ptr<HostFunctions> const& hfs,
|
||||
ImportVec const& imports,
|
||||
int64_t gas,
|
||||
beast::Journal j);
|
||||
|
||||
NotTEC
|
||||
check(
|
||||
Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params,
|
||||
std::shared_ptr<ImportVec> const& imports,
|
||||
std::shared_ptr<HostFunctions> const& hfs,
|
||||
ImportVec const& imports,
|
||||
beast::Journal j);
|
||||
|
||||
std::int64_t
|
||||
getGas();
|
||||
getGas() const;
|
||||
|
||||
// Host functions helper functionality
|
||||
wasm_trap_t*
|
||||
@@ -228,22 +219,30 @@ public:
|
||||
|
||||
private:
|
||||
InstanceWrapper const&
|
||||
getRT(int m = 0, int i = 0);
|
||||
getRT(int m = 0, int i = 0) const;
|
||||
|
||||
wmem
|
||||
getMem() const;
|
||||
|
||||
int32_t
|
||||
allocate(int32_t size);
|
||||
|
||||
Expected<WasmResult<int32_t>, TER>
|
||||
runHlp(Bytes const& wasmCode, std::string_view funcName, std::vector<WasmParam> const& params, int64_t gas);
|
||||
runHlp(
|
||||
Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params,
|
||||
ImportVec const& imports,
|
||||
int64_t gas);
|
||||
|
||||
NotTEC
|
||||
checkHlp(Bytes const& wasmCode, std::string_view funcName, std::vector<WasmParam> const& params);
|
||||
checkHlp(
|
||||
Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params,
|
||||
ImportVec const& imports);
|
||||
|
||||
int
|
||||
addModule(Bytes const& wasmCode, bool instantiate, int64_t gas);
|
||||
addModule(Bytes const& wasmCode, bool instantiate, ImportVec const& imports, int64_t gas);
|
||||
void
|
||||
clearModules();
|
||||
|
||||
@@ -256,7 +255,7 @@ private:
|
||||
makeModule(Bytes const& wasmCode, WasmExternVec const& imports = {});
|
||||
|
||||
FuncInfo
|
||||
getFunc(std::string_view funcName);
|
||||
getFunc(std::string_view funcName) const;
|
||||
|
||||
std::vector<wasm_val_t>
|
||||
convertParams(std::vector<WasmParam> const& params);
|
||||
|
||||
@@ -25,7 +25,7 @@ WasmHostFunctionsImpl::updateData(Slice const& data)
|
||||
// =========================================================
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::checkSignature(Slice const& message, Slice const& signature, Slice const& pubkey)
|
||||
WasmHostFunctionsImpl::checkSignature(Slice const& message, Slice const& signature, Slice const& pubkey) const
|
||||
{
|
||||
if (!publicKeyType(pubkey))
|
||||
return Unexpected(HostFunctionError::INVALID_PARAMS);
|
||||
@@ -35,7 +35,7 @@ WasmHostFunctionsImpl::checkSignature(Slice const& message, Slice const& signatu
|
||||
}
|
||||
|
||||
Expected<Hash, HostFunctionError>
|
||||
WasmHostFunctionsImpl::computeSha512HalfHash(Slice const& data)
|
||||
WasmHostFunctionsImpl::computeSha512HalfHash(Slice const& data) const
|
||||
{
|
||||
auto const hash = sha512Half(data);
|
||||
return hash;
|
||||
|
||||
@@ -480,67 +480,67 @@ floatLogImpl(Slice const& x, int32_t mode)
|
||||
// =========================================================
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::floatFromInt(int64_t x, int32_t mode)
|
||||
WasmHostFunctionsImpl::floatFromInt(int64_t x, int32_t mode) const
|
||||
{
|
||||
return wasm_float::floatFromIntImpl(x, mode);
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::floatFromUint(uint64_t x, int32_t mode)
|
||||
WasmHostFunctionsImpl::floatFromUint(uint64_t x, int32_t mode) const
|
||||
{
|
||||
return wasm_float::floatFromUintImpl(x, mode);
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::floatSet(int64_t mantissa, int32_t exponent, int32_t mode)
|
||||
WasmHostFunctionsImpl::floatSet(int64_t mantissa, int32_t exponent, int32_t mode) const
|
||||
{
|
||||
return wasm_float::floatSetImpl(mantissa, exponent, mode);
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::floatCompare(Slice const& x, Slice const& y)
|
||||
WasmHostFunctionsImpl::floatCompare(Slice const& x, Slice const& y) const
|
||||
{
|
||||
return wasm_float::floatCompareImpl(x, y);
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::floatAdd(Slice const& x, Slice const& y, int32_t mode)
|
||||
WasmHostFunctionsImpl::floatAdd(Slice const& x, Slice const& y, int32_t mode) const
|
||||
{
|
||||
return wasm_float::floatAddImpl(x, y, mode);
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::floatSubtract(Slice const& x, Slice const& y, int32_t mode)
|
||||
WasmHostFunctionsImpl::floatSubtract(Slice const& x, Slice const& y, int32_t mode) const
|
||||
{
|
||||
return wasm_float::floatSubtractImpl(x, y, mode);
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::floatMultiply(Slice const& x, Slice const& y, int32_t mode)
|
||||
WasmHostFunctionsImpl::floatMultiply(Slice const& x, Slice const& y, int32_t mode) const
|
||||
{
|
||||
return wasm_float::floatMultiplyImpl(x, y, mode);
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::floatDivide(Slice const& x, Slice const& y, int32_t mode)
|
||||
WasmHostFunctionsImpl::floatDivide(Slice const& x, Slice const& y, int32_t mode) const
|
||||
{
|
||||
return wasm_float::floatDivideImpl(x, y, mode);
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::floatRoot(Slice const& x, int32_t n, int32_t mode)
|
||||
WasmHostFunctionsImpl::floatRoot(Slice const& x, int32_t n, int32_t mode) const
|
||||
{
|
||||
return wasm_float::floatRootImpl(x, n, mode);
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::floatPower(Slice const& x, int32_t n, int32_t mode)
|
||||
WasmHostFunctionsImpl::floatPower(Slice const& x, int32_t n, int32_t mode) const
|
||||
{
|
||||
return wasm_float::floatPowerImpl(x, n, mode);
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::floatLog(Slice const& x, int32_t mode)
|
||||
WasmHostFunctionsImpl::floatLog(Slice const& x, int32_t mode) const
|
||||
{
|
||||
return wasm_float::floatLogImpl(x, mode);
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ WasmHostFunctionsImpl::cacheLedgerObj(uint256 const& objId, int32_t cacheIdx)
|
||||
if (cacheIdx == 0)
|
||||
{
|
||||
for (cacheIdx = 0; cacheIdx < MAX_CACHE; ++cacheIdx)
|
||||
if (!cache[cacheIdx])
|
||||
if (!cache_[cacheIdx])
|
||||
break;
|
||||
}
|
||||
else
|
||||
@@ -234,8 +234,8 @@ WasmHostFunctionsImpl::cacheLedgerObj(uint256 const& objId, int32_t cacheIdx)
|
||||
if (cacheIdx >= MAX_CACHE)
|
||||
return Unexpected(HostFunctionError::SLOTS_FULL);
|
||||
|
||||
cache[cacheIdx] = ctx.view().read(keylet);
|
||||
if (!cache[cacheIdx])
|
||||
cache_[cacheIdx] = ctx_.view().read(keylet);
|
||||
if (!cache_[cacheIdx])
|
||||
return Unexpected(HostFunctionError::LEDGER_OBJ_NOT_FOUND);
|
||||
return cacheIdx + 1; // return 1-based index
|
||||
}
|
||||
@@ -243,13 +243,13 @@ WasmHostFunctionsImpl::cacheLedgerObj(uint256 const& objId, int32_t cacheIdx)
|
||||
// Subsection: top level getters
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getTxField(SField const& fname)
|
||||
WasmHostFunctionsImpl::getTxField(SField const& fname) const
|
||||
{
|
||||
return detail::getAnyFieldData(ctx.tx.peekAtPField(fname));
|
||||
return detail::getAnyFieldData(ctx_.tx.peekAtPField(fname));
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getCurrentLedgerObjField(SField const& fname)
|
||||
WasmHostFunctionsImpl::getCurrentLedgerObjField(SField const& fname) const
|
||||
{
|
||||
auto const sle = getCurrentLedgerObj();
|
||||
if (!sle.has_value())
|
||||
@@ -258,20 +258,20 @@ WasmHostFunctionsImpl::getCurrentLedgerObjField(SField const& fname)
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getLedgerObjField(int32_t cacheIdx, SField const& fname)
|
||||
WasmHostFunctionsImpl::getLedgerObjField(int32_t cacheIdx, SField const& fname) const
|
||||
{
|
||||
auto const normalizedIdx = normalizeCacheIndex(cacheIdx);
|
||||
if (!normalizedIdx.has_value())
|
||||
return Unexpected(normalizedIdx.error());
|
||||
return detail::getAnyFieldData(cache[normalizedIdx.value()]->peekAtPField(fname));
|
||||
return detail::getAnyFieldData(cache_[normalizedIdx.value()]->peekAtPField(fname));
|
||||
}
|
||||
|
||||
// Subsection: nested getters
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getTxNestedField(Slice const& locator)
|
||||
WasmHostFunctionsImpl::getTxNestedField(Slice const& locator) const
|
||||
{
|
||||
auto const r = detail::locateField(ctx.tx, locator);
|
||||
auto const r = detail::locateField(ctx_.tx, locator);
|
||||
if (!r)
|
||||
return Unexpected(r.error());
|
||||
|
||||
@@ -279,7 +279,7 @@ WasmHostFunctionsImpl::getTxNestedField(Slice const& locator)
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getCurrentLedgerObjNestedField(Slice const& locator)
|
||||
WasmHostFunctionsImpl::getCurrentLedgerObjNestedField(Slice const& locator) const
|
||||
{
|
||||
auto const sle = getCurrentLedgerObj();
|
||||
if (!sle.has_value())
|
||||
@@ -293,13 +293,13 @@ WasmHostFunctionsImpl::getCurrentLedgerObjNestedField(Slice const& locator)
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getLedgerObjNestedField(int32_t cacheIdx, Slice const& locator)
|
||||
WasmHostFunctionsImpl::getLedgerObjNestedField(int32_t cacheIdx, Slice const& locator) const
|
||||
{
|
||||
auto const normalizedIdx = normalizeCacheIndex(cacheIdx);
|
||||
if (!normalizedIdx.has_value())
|
||||
return Unexpected(normalizedIdx.error());
|
||||
|
||||
auto const r = detail::locateField(*cache[normalizedIdx.value()], locator);
|
||||
auto const r = detail::locateField(*cache_[normalizedIdx.value()], locator);
|
||||
if (!r)
|
||||
return Unexpected(r.error());
|
||||
|
||||
@@ -309,12 +309,12 @@ WasmHostFunctionsImpl::getLedgerObjNestedField(int32_t cacheIdx, Slice const& lo
|
||||
// Subsection: array length getters
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getTxArrayLen(SField const& fname)
|
||||
WasmHostFunctionsImpl::getTxArrayLen(SField const& fname) const
|
||||
{
|
||||
if (fname.fieldType != STI_ARRAY && fname.fieldType != STI_VECTOR256)
|
||||
return Unexpected(HostFunctionError::NO_ARRAY);
|
||||
|
||||
auto const* field = ctx.tx.peekAtPField(fname);
|
||||
auto const* field = ctx_.tx.peekAtPField(fname);
|
||||
if (detail::noField(field))
|
||||
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
|
||||
|
||||
@@ -322,7 +322,7 @@ WasmHostFunctionsImpl::getTxArrayLen(SField const& fname)
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getCurrentLedgerObjArrayLen(SField const& fname)
|
||||
WasmHostFunctionsImpl::getCurrentLedgerObjArrayLen(SField const& fname) const
|
||||
{
|
||||
if (fname.fieldType != STI_ARRAY && fname.fieldType != STI_VECTOR256)
|
||||
return Unexpected(HostFunctionError::NO_ARRAY);
|
||||
@@ -339,7 +339,7 @@ WasmHostFunctionsImpl::getCurrentLedgerObjArrayLen(SField const& fname)
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getLedgerObjArrayLen(int32_t cacheIdx, SField const& fname)
|
||||
WasmHostFunctionsImpl::getLedgerObjArrayLen(int32_t cacheIdx, SField const& fname) const
|
||||
{
|
||||
if (fname.fieldType != STI_ARRAY && fname.fieldType != STI_VECTOR256)
|
||||
return Unexpected(HostFunctionError::NO_ARRAY);
|
||||
@@ -348,7 +348,7 @@ WasmHostFunctionsImpl::getLedgerObjArrayLen(int32_t cacheIdx, SField const& fnam
|
||||
if (!normalizedIdx.has_value())
|
||||
return Unexpected(normalizedIdx.error());
|
||||
|
||||
auto const* field = cache[normalizedIdx.value()]->peekAtPField(fname);
|
||||
auto const* field = cache_[normalizedIdx.value()]->peekAtPField(fname);
|
||||
if (detail::noField(field))
|
||||
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
|
||||
|
||||
@@ -358,9 +358,9 @@ WasmHostFunctionsImpl::getLedgerObjArrayLen(int32_t cacheIdx, SField const& fnam
|
||||
// Subsection: nested array length getters
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getTxNestedArrayLen(Slice const& locator)
|
||||
WasmHostFunctionsImpl::getTxNestedArrayLen(Slice const& locator) const
|
||||
{
|
||||
auto const r = detail::locateField(ctx.tx, locator);
|
||||
auto const r = detail::locateField(ctx_.tx, locator);
|
||||
if (!r)
|
||||
return Unexpected(r.error());
|
||||
|
||||
@@ -369,7 +369,7 @@ WasmHostFunctionsImpl::getTxNestedArrayLen(Slice const& locator)
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getCurrentLedgerObjNestedArrayLen(Slice const& locator)
|
||||
WasmHostFunctionsImpl::getCurrentLedgerObjNestedArrayLen(Slice const& locator) const
|
||||
{
|
||||
auto const sle = getCurrentLedgerObj();
|
||||
if (!sle.has_value())
|
||||
@@ -383,13 +383,13 @@ WasmHostFunctionsImpl::getCurrentLedgerObjNestedArrayLen(Slice const& locator)
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getLedgerObjNestedArrayLen(int32_t cacheIdx, Slice const& locator)
|
||||
WasmHostFunctionsImpl::getLedgerObjNestedArrayLen(int32_t cacheIdx, Slice const& locator) const
|
||||
{
|
||||
auto const normalizedIdx = normalizeCacheIndex(cacheIdx);
|
||||
if (!normalizedIdx.has_value())
|
||||
return Unexpected(normalizedIdx.error());
|
||||
|
||||
auto const r = detail::locateField(*cache[normalizedIdx.value()], locator);
|
||||
auto const r = detail::locateField(*cache_[normalizedIdx.value()], locator);
|
||||
if (!r)
|
||||
return Unexpected(r.error());
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
namespace xrpl {
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::accountKeylet(AccountID const& account)
|
||||
WasmHostFunctionsImpl::accountKeylet(AccountID const& account) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -15,7 +15,7 @@ WasmHostFunctionsImpl::accountKeylet(AccountID const& account)
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::ammKeylet(Asset const& issue1, Asset const& issue2)
|
||||
WasmHostFunctionsImpl::ammKeylet(Asset const& issue1, Asset const& issue2) const
|
||||
{
|
||||
if (issue1 == issue2)
|
||||
return Unexpected(HostFunctionError::INVALID_PARAMS);
|
||||
@@ -29,7 +29,7 @@ WasmHostFunctionsImpl::ammKeylet(Asset const& issue1, Asset const& issue2)
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::checkKeylet(AccountID const& account, std::uint32_t seq)
|
||||
WasmHostFunctionsImpl::checkKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -39,6 +39,7 @@ WasmHostFunctionsImpl::checkKeylet(AccountID const& account, std::uint32_t seq)
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::credentialKeylet(AccountID const& subject, AccountID const& issuer, Slice const& credentialType)
|
||||
const
|
||||
{
|
||||
if (!subject || !issuer)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -52,7 +53,7 @@ WasmHostFunctionsImpl::credentialKeylet(AccountID const& subject, AccountID cons
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::didKeylet(AccountID const& account)
|
||||
WasmHostFunctionsImpl::didKeylet(AccountID const& account) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -61,7 +62,7 @@ WasmHostFunctionsImpl::didKeylet(AccountID const& account)
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::delegateKeylet(AccountID const& account, AccountID const& authorize)
|
||||
WasmHostFunctionsImpl::delegateKeylet(AccountID const& account, AccountID const& authorize) const
|
||||
{
|
||||
if (!account || !authorize)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -72,7 +73,7 @@ WasmHostFunctionsImpl::delegateKeylet(AccountID const& account, AccountID const&
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::depositPreauthKeylet(AccountID const& account, AccountID const& authorize)
|
||||
WasmHostFunctionsImpl::depositPreauthKeylet(AccountID const& account, AccountID const& authorize) const
|
||||
{
|
||||
if (!account || !authorize)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -83,7 +84,7 @@ WasmHostFunctionsImpl::depositPreauthKeylet(AccountID const& account, AccountID
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::escrowKeylet(AccountID const& account, std::uint32_t seq)
|
||||
WasmHostFunctionsImpl::escrowKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -92,7 +93,7 @@ WasmHostFunctionsImpl::escrowKeylet(AccountID const& account, std::uint32_t seq)
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::lineKeylet(AccountID const& account1, AccountID const& account2, Currency const& currency)
|
||||
WasmHostFunctionsImpl::lineKeylet(AccountID const& account1, AccountID const& account2, Currency const& currency) const
|
||||
{
|
||||
if (!account1 || !account2)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -106,7 +107,7 @@ WasmHostFunctionsImpl::lineKeylet(AccountID const& account1, AccountID const& ac
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::mptIssuanceKeylet(AccountID const& issuer, std::uint32_t seq)
|
||||
WasmHostFunctionsImpl::mptIssuanceKeylet(AccountID const& issuer, std::uint32_t seq) const
|
||||
{
|
||||
if (!issuer)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -116,7 +117,7 @@ WasmHostFunctionsImpl::mptIssuanceKeylet(AccountID const& issuer, std::uint32_t
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::mptokenKeylet(MPTID const& mptid, AccountID const& holder)
|
||||
WasmHostFunctionsImpl::mptokenKeylet(MPTID const& mptid, AccountID const& holder) const
|
||||
{
|
||||
if (!mptid)
|
||||
return Unexpected(HostFunctionError::INVALID_PARAMS);
|
||||
@@ -128,7 +129,7 @@ WasmHostFunctionsImpl::mptokenKeylet(MPTID const& mptid, AccountID const& holder
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::nftOfferKeylet(AccountID const& account, std::uint32_t seq)
|
||||
WasmHostFunctionsImpl::nftOfferKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -137,7 +138,7 @@ WasmHostFunctionsImpl::nftOfferKeylet(AccountID const& account, std::uint32_t se
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::offerKeylet(AccountID const& account, std::uint32_t seq)
|
||||
WasmHostFunctionsImpl::offerKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -146,7 +147,7 @@ WasmHostFunctionsImpl::offerKeylet(AccountID const& account, std::uint32_t seq)
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::oracleKeylet(AccountID const& account, std::uint32_t documentId)
|
||||
WasmHostFunctionsImpl::oracleKeylet(AccountID const& account, std::uint32_t documentId) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -155,7 +156,7 @@ WasmHostFunctionsImpl::oracleKeylet(AccountID const& account, std::uint32_t docu
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::paychanKeylet(AccountID const& account, AccountID const& destination, std::uint32_t seq)
|
||||
WasmHostFunctionsImpl::paychanKeylet(AccountID const& account, AccountID const& destination, std::uint32_t seq) const
|
||||
{
|
||||
if (!account || !destination)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -166,7 +167,7 @@ WasmHostFunctionsImpl::paychanKeylet(AccountID const& account, AccountID const&
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::permissionedDomainKeylet(AccountID const& account, std::uint32_t seq)
|
||||
WasmHostFunctionsImpl::permissionedDomainKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -175,7 +176,7 @@ WasmHostFunctionsImpl::permissionedDomainKeylet(AccountID const& account, std::u
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::signersKeylet(AccountID const& account)
|
||||
WasmHostFunctionsImpl::signersKeylet(AccountID const& account) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -184,7 +185,7 @@ WasmHostFunctionsImpl::signersKeylet(AccountID const& account)
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::ticketKeylet(AccountID const& account, std::uint32_t seq)
|
||||
WasmHostFunctionsImpl::ticketKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -193,7 +194,7 @@ WasmHostFunctionsImpl::ticketKeylet(AccountID const& account, std::uint32_t seq)
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::vaultKeylet(AccountID const& account, std::uint32_t seq)
|
||||
WasmHostFunctionsImpl::vaultKeylet(AccountID const& account, std::uint32_t seq) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
|
||||
@@ -10,41 +10,41 @@ namespace xrpl {
|
||||
// =========================================================
|
||||
|
||||
Expected<std::uint32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getLedgerSqn()
|
||||
WasmHostFunctionsImpl::getLedgerSqn() const
|
||||
{
|
||||
return ctx.view().seq();
|
||||
return ctx_.view().seq();
|
||||
}
|
||||
|
||||
Expected<std::uint32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getParentLedgerTime()
|
||||
WasmHostFunctionsImpl::getParentLedgerTime() const
|
||||
{
|
||||
return ctx.view().parentCloseTime().time_since_epoch().count();
|
||||
return ctx_.view().parentCloseTime().time_since_epoch().count();
|
||||
}
|
||||
|
||||
Expected<Hash, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getParentLedgerHash()
|
||||
WasmHostFunctionsImpl::getParentLedgerHash() const
|
||||
{
|
||||
return ctx.view().header().parentHash;
|
||||
return ctx_.view().header().parentHash;
|
||||
}
|
||||
|
||||
Expected<std::uint32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getBaseFee()
|
||||
WasmHostFunctionsImpl::getBaseFee() const
|
||||
{
|
||||
return ctx.view().fees().base.drops();
|
||||
return ctx_.view().fees().base.drops();
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::isAmendmentEnabled(uint256 const& amendmentId)
|
||||
WasmHostFunctionsImpl::isAmendmentEnabled(uint256 const& amendmentId) const
|
||||
{
|
||||
return ctx.view().rules().enabled(amendmentId);
|
||||
return ctx_.view().rules().enabled(amendmentId);
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::isAmendmentEnabled(std::string_view const& amendmentName)
|
||||
WasmHostFunctionsImpl::isAmendmentEnabled(std::string_view const& amendmentName) const
|
||||
{
|
||||
auto const& table = ctx.registry.getAmendmentTable();
|
||||
auto const& table = ctx_.registry.getAmendmentTable();
|
||||
auto const amendment = table.find(std::string(amendmentName));
|
||||
return ctx.view().rules().enabled(amendment);
|
||||
return ctx_.view().rules().enabled(amendment);
|
||||
}
|
||||
|
||||
} // namespace xrpl
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace xrpl {
|
||||
// =========================================================
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getNFT(AccountID const& account, uint256 const& nftId)
|
||||
WasmHostFunctionsImpl::getNFT(AccountID const& account, uint256 const& nftId) const
|
||||
{
|
||||
if (!account)
|
||||
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
|
||||
@@ -19,7 +19,7 @@ WasmHostFunctionsImpl::getNFT(AccountID const& account, uint256 const& nftId)
|
||||
if (!nftId)
|
||||
return Unexpected(HostFunctionError::INVALID_PARAMS);
|
||||
|
||||
auto obj = nft::findToken(ctx.view(), account, nftId);
|
||||
auto obj = nft::findToken(ctx_.view(), account, nftId);
|
||||
if (!obj)
|
||||
return Unexpected(HostFunctionError::LEDGER_OBJ_NOT_FOUND);
|
||||
|
||||
@@ -32,7 +32,7 @@ WasmHostFunctionsImpl::getNFT(AccountID const& account, uint256 const& nftId)
|
||||
}
|
||||
|
||||
Expected<Bytes, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getNFTIssuer(uint256 const& nftId)
|
||||
WasmHostFunctionsImpl::getNFTIssuer(uint256 const& nftId) const
|
||||
{
|
||||
auto const issuer = nft::getIssuer(nftId);
|
||||
if (!issuer)
|
||||
@@ -42,25 +42,25 @@ WasmHostFunctionsImpl::getNFTIssuer(uint256 const& nftId)
|
||||
}
|
||||
|
||||
Expected<std::uint32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getNFTTaxon(uint256 const& nftId)
|
||||
WasmHostFunctionsImpl::getNFTTaxon(uint256 const& nftId) const
|
||||
{
|
||||
return nft::toUInt32(nft::getTaxon(nftId));
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getNFTFlags(uint256 const& nftId)
|
||||
WasmHostFunctionsImpl::getNFTFlags(uint256 const& nftId) const
|
||||
{
|
||||
return nft::getFlags(nftId);
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getNFTTransferFee(uint256 const& nftId)
|
||||
WasmHostFunctionsImpl::getNFTTransferFee(uint256 const& nftId) const
|
||||
{
|
||||
return nft::getTransferFee(nftId);
|
||||
}
|
||||
|
||||
Expected<std::uint32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::getNFTSerial(uint256 const& nftId)
|
||||
WasmHostFunctionsImpl::getNFTSerial(uint256 const& nftId) const
|
||||
{
|
||||
return nft::getSerial(nftId);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
namespace xrpl {
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::trace(std::string_view const& msg, Slice const& data, bool asHex)
|
||||
WasmHostFunctionsImpl::trace(std::string_view const& msg, Slice const& data, bool asHex) const
|
||||
{
|
||||
if (!asHex)
|
||||
{
|
||||
@@ -30,28 +30,28 @@ WasmHostFunctionsImpl::trace(std::string_view const& msg, Slice const& data, boo
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::traceNum(std::string_view const& msg, int64_t data)
|
||||
WasmHostFunctionsImpl::traceNum(std::string_view const& msg, int64_t data) const
|
||||
{
|
||||
log(msg, [data] { return data; });
|
||||
return 0;
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::traceAccount(std::string_view const& msg, AccountID const& account)
|
||||
WasmHostFunctionsImpl::traceAccount(std::string_view const& msg, AccountID const& account) const
|
||||
{
|
||||
log(msg, [&account] { return toBase58(account); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::traceFloat(std::string_view const& msg, Slice const& data)
|
||||
WasmHostFunctionsImpl::traceFloat(std::string_view const& msg, Slice const& data) const
|
||||
{
|
||||
log(msg, [&data] { return wasm_float::floatToString(data); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
Expected<int32_t, HostFunctionError>
|
||||
WasmHostFunctionsImpl::traceAmount(std::string_view const& msg, STAmount const& amount)
|
||||
WasmHostFunctionsImpl::traceAmount(std::string_view const& msg, STAmount const& amount) const
|
||||
{
|
||||
log(msg, [&amount] { return amount.getFullText(); });
|
||||
return 0;
|
||||
|
||||
@@ -89,21 +89,21 @@ setCommonHostFunctions(HostFunctions* hfs, ImportVec& i)
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
std::shared_ptr<ImportVec>
|
||||
ImportVec
|
||||
createWasmImport(HostFunctions& hfs)
|
||||
{
|
||||
std::shared_ptr<ImportVec> i(std::make_shared<ImportVec>());
|
||||
ImportVec import;
|
||||
|
||||
setCommonHostFunctions(&hfs, *i);
|
||||
WASM_IMPORT_FUNC2(*i, updateData, "update_data", &hfs, 1000);
|
||||
setCommonHostFunctions(&hfs, import);
|
||||
WASM_IMPORT_FUNC2(import, updateData, "update_data", &hfs, 1000);
|
||||
|
||||
return i;
|
||||
return import;
|
||||
}
|
||||
|
||||
Expected<EscrowResult, TER>
|
||||
runEscrowWasm(
|
||||
Bytes const& wasmCode,
|
||||
std::shared_ptr<HostFunctions> const& hfs,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params,
|
||||
int64_t gasLimit)
|
||||
@@ -112,7 +112,7 @@ runEscrowWasm(
|
||||
auto& vm = WasmEngine::instance();
|
||||
// vm.initMaxPages(MAX_PAGES);
|
||||
|
||||
auto const ret = vm.run(wasmCode, funcName, params, createWasmImport(*hfs), hfs, gasLimit, hfs->getJournal());
|
||||
auto const ret = vm.run(wasmCode, hfs, funcName, params, createWasmImport(hfs), gasLimit, hfs.getJournal());
|
||||
|
||||
// std::cout << "runEscrowWasm, mod size: " << wasmCode.size()
|
||||
// << ", gasLimit: " << gasLimit << ", funcName: " << funcName;
|
||||
@@ -134,7 +134,7 @@ runEscrowWasm(
|
||||
NotTEC
|
||||
preflightEscrowWasm(
|
||||
Bytes const& wasmCode,
|
||||
std::shared_ptr<HostFunctions> const& hfs,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params)
|
||||
{
|
||||
@@ -142,14 +142,14 @@ preflightEscrowWasm(
|
||||
auto& vm = WasmEngine::instance();
|
||||
// vm.initMaxPages(MAX_PAGES);
|
||||
|
||||
auto const ret = vm.check(wasmCode, funcName, params, createWasmImport(*hfs), hfs, hfs->getJournal());
|
||||
auto const ret = vm.check(wasmCode, hfs, funcName, params, createWasmImport(hfs), hfs.getJournal());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
WasmEngine::WasmEngine() : impl(std::make_unique<WasmiEngine>())
|
||||
WasmEngine::WasmEngine() : impl_(std::make_unique<WasmiEngine>())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -163,39 +163,39 @@ WasmEngine::instance()
|
||||
Expected<WasmResult<int32_t>, TER>
|
||||
WasmEngine::run(
|
||||
Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params,
|
||||
std::shared_ptr<ImportVec> const& imports,
|
||||
std::shared_ptr<HostFunctions> const& hfs,
|
||||
ImportVec const& imports,
|
||||
int64_t gasLimit,
|
||||
beast::Journal j)
|
||||
{
|
||||
return impl->run(wasmCode, funcName, params, imports, hfs, gasLimit, j);
|
||||
return impl_->run(wasmCode, hfs, funcName, params, imports, gasLimit, j);
|
||||
}
|
||||
|
||||
NotTEC
|
||||
WasmEngine::check(
|
||||
Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params,
|
||||
std::shared_ptr<ImportVec> const& imports,
|
||||
std::shared_ptr<HostFunctions> const& hfs,
|
||||
ImportVec const& imports,
|
||||
beast::Journal j)
|
||||
{
|
||||
return impl->check(wasmCode, funcName, params, imports, hfs, j);
|
||||
return impl_->check(wasmCode, hfs, funcName, params, imports, j);
|
||||
}
|
||||
|
||||
void*
|
||||
WasmEngine::newTrap(std::string const& msg)
|
||||
{
|
||||
return impl->newTrap(msg);
|
||||
return impl_->newTrap(msg);
|
||||
}
|
||||
|
||||
// LCOV_EXCL_START
|
||||
beast::Journal
|
||||
WasmEngine::getJournal() const
|
||||
{
|
||||
return impl->getJournal();
|
||||
return impl_->getJournal();
|
||||
}
|
||||
// LCOV_EXCL_STOP
|
||||
|
||||
|
||||
@@ -233,7 +233,7 @@ ModuleWrapper::ModuleWrapper(
|
||||
StorePtr& s,
|
||||
Bytes const& wasmBin,
|
||||
bool instantiate,
|
||||
std::shared_ptr<ImportVec> const& imports,
|
||||
ImportVec const& imports,
|
||||
beast::Journal j)
|
||||
: module_(init(s, wasmBin, j)), j_(j)
|
||||
{
|
||||
@@ -321,14 +321,14 @@ makeImpReturn(WasmImportFunc const& imp)
|
||||
}
|
||||
|
||||
WasmExternVec
|
||||
ModuleWrapper::buildImports(StorePtr& s, std::shared_ptr<ImportVec> const& imports)
|
||||
ModuleWrapper::buildImports(StorePtr& s, ImportVec const& imports)
|
||||
{
|
||||
WasmImporttypeVec importTypes;
|
||||
wasm_module_imports(module_.get(), &importTypes.vec_);
|
||||
|
||||
if (!importTypes.vec_.size)
|
||||
return {};
|
||||
if (!imports)
|
||||
if (imports.empty())
|
||||
throw std::runtime_error("Missing imports");
|
||||
|
||||
WasmExternVec wimports(importTypes.vec_.size);
|
||||
@@ -352,7 +352,7 @@ ModuleWrapper::buildImports(StorePtr& s, std::shared_ptr<ImportVec> const& impor
|
||||
// continue;
|
||||
|
||||
bool impSet = false;
|
||||
for (auto const& obj : *imports)
|
||||
for (auto const& obj : imports)
|
||||
{
|
||||
auto const& imp = obj.second;
|
||||
if (imp.name != fieldName)
|
||||
@@ -502,7 +502,7 @@ WasmiEngine::WasmiEngine() : engine_(init()), store_(nullptr, &wasm_store_delete
|
||||
}
|
||||
|
||||
int
|
||||
WasmiEngine::addModule(Bytes const& wasmCode, bool instantiate, int64_t gas)
|
||||
WasmiEngine::addModule(Bytes const& wasmCode, bool instantiate, ImportVec const& imports, int64_t gas)
|
||||
{
|
||||
moduleWrap_.reset();
|
||||
store_.reset(); // to free the memory before creating new store
|
||||
@@ -520,7 +520,7 @@ WasmiEngine::addModule(Bytes const& wasmCode, bool instantiate, int64_t gas)
|
||||
// LCOV_EXCL_STOP
|
||||
}
|
||||
|
||||
moduleWrap_ = std::make_unique<ModuleWrapper>(store_, wasmCode, instantiate, imports_, j_);
|
||||
moduleWrap_ = std::make_unique<ModuleWrapper>(store_, wasmCode, instantiate, imports, j_);
|
||||
|
||||
if (!moduleWrap_)
|
||||
throw std::runtime_error("can't create module wrapper"); // LCOV_EXCL_LINE
|
||||
@@ -535,7 +535,7 @@ WasmiEngine::addModule(Bytes const& wasmCode, bool instantiate, int64_t gas)
|
||||
// }
|
||||
|
||||
FuncInfo
|
||||
WasmiEngine::getFunc(std::string_view funcName)
|
||||
WasmiEngine::getFunc(std::string_view funcName) const
|
||||
{
|
||||
return moduleWrap_->getFunc(funcName);
|
||||
}
|
||||
@@ -556,19 +556,6 @@ WasmiEngine::convertParams(std::vector<WasmParam> const& params)
|
||||
case WT_I64:
|
||||
v.push_back(WASM_I64_VAL(p.of.i64));
|
||||
break;
|
||||
// LCOV_EXCL_STOP
|
||||
case WT_U8V: {
|
||||
auto mem = getMem();
|
||||
if (!mem.s)
|
||||
throw std::runtime_error("no memory exported"); // LCOV_EXCL_LINE
|
||||
auto const sz = p.of.u8v.sz;
|
||||
auto const ptr = allocate(sz);
|
||||
memcpy(mem.p + ptr, p.of.u8v.d, sz);
|
||||
v.push_back(WASM_I32_VAL(ptr));
|
||||
v.push_back(WASM_I32_VAL(sz));
|
||||
}
|
||||
break;
|
||||
// LCOV_EXCL_START
|
||||
default:
|
||||
throw std::runtime_error("unknown parameter type: " + std::to_string(p.type));
|
||||
break;
|
||||
@@ -695,22 +682,6 @@ WasmiEngine::call(FuncInfo const& f, std::vector<wasm_val_t>& in, std::int64_t p
|
||||
return call<NR>(f, in, std::forward<Types>(args)...);
|
||||
}
|
||||
|
||||
template <int NR, class... Types>
|
||||
WasmiResult
|
||||
WasmiEngine::call(FuncInfo const& f, std::vector<wasm_val_t>& in, uint8_t const* d, int32_t sz, Types&&... args)
|
||||
{
|
||||
auto mem = getMem();
|
||||
if (!mem.s)
|
||||
throw std::runtime_error("no memory exported"); // LCOV_EXCL_LINE
|
||||
|
||||
auto const ptr = allocate(sz);
|
||||
memcpy(mem.p + ptr, d, sz);
|
||||
|
||||
add_param(in, ptr);
|
||||
add_param(in, static_cast<int32_t>(sz));
|
||||
return call<NR>(f, in, std::forward<Types>(args)...);
|
||||
}
|
||||
|
||||
template <int NR, class... Types>
|
||||
WasmiResult
|
||||
WasmiEngine::call(FuncInfo const& f, std::vector<wasm_val_t>& in, Bytes const& p, Types&&... args)
|
||||
@@ -731,26 +702,19 @@ checkImports(ImportVec const& imports, HostFunctions* hfs)
|
||||
Expected<WasmResult<int32_t>, TER>
|
||||
WasmiEngine::run(
|
||||
Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params,
|
||||
std::shared_ptr<ImportVec> const& imports,
|
||||
std::shared_ptr<HostFunctions> const& hfs,
|
||||
ImportVec const& imports,
|
||||
int64_t gas,
|
||||
beast::Journal j)
|
||||
{
|
||||
j_ = j;
|
||||
|
||||
if (!wasmCode.empty())
|
||||
{ // save values for reuse
|
||||
imports_ = imports;
|
||||
hfs_ = hfs;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (imports_)
|
||||
checkImports(*imports_, hfs.get());
|
||||
return runHlp(wasmCode, funcName, params, gas);
|
||||
checkImports(imports, &hfs);
|
||||
return runHlp(wasmCode, hfs, funcName, params, imports, gas);
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
@@ -766,22 +730,29 @@ WasmiEngine::run(
|
||||
}
|
||||
|
||||
Expected<WasmResult<int32_t>, TER>
|
||||
WasmiEngine::runHlp(Bytes const& wasmCode, std::string_view funcName, std::vector<WasmParam> const& params, int64_t gas)
|
||||
WasmiEngine::runHlp(
|
||||
Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params,
|
||||
ImportVec const& imports,
|
||||
int64_t gas)
|
||||
{
|
||||
// currently only 1 module support, possible parallel UT run
|
||||
std::lock_guard<decltype(m_)> lg(m_);
|
||||
|
||||
if (wasmCode.empty())
|
||||
throw std::runtime_error("empty module");
|
||||
if (!hfs.checkSelf())
|
||||
throw std::runtime_error("hfs isn't clean");
|
||||
|
||||
// Create and instantiate the module.
|
||||
if (!wasmCode.empty())
|
||||
{
|
||||
[[maybe_unused]] int const m = addModule(wasmCode, true, gas);
|
||||
}
|
||||
[[maybe_unused]] int const m = addModule(wasmCode, true, imports, gas);
|
||||
|
||||
if (!moduleWrap_ || !moduleWrap_->instanceWrap_)
|
||||
throw std::runtime_error("no instance"); // LCOV_EXCL_LINE
|
||||
|
||||
if (hfs_)
|
||||
hfs_->setRT(&getRT());
|
||||
hfs.setRT(&getRT());
|
||||
|
||||
// Call main
|
||||
auto const f = getFunc(!funcName.empty() ? funcName : "_start");
|
||||
@@ -822,25 +793,18 @@ WasmiEngine::runHlp(Bytes const& wasmCode, std::string_view funcName, std::vecto
|
||||
NotTEC
|
||||
WasmiEngine::check(
|
||||
Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params,
|
||||
std::shared_ptr<ImportVec> const& imports,
|
||||
std::shared_ptr<HostFunctions> const& hfs,
|
||||
ImportVec const& imports,
|
||||
beast::Journal j)
|
||||
{
|
||||
j_ = j;
|
||||
|
||||
if (!wasmCode.empty())
|
||||
{
|
||||
imports_ = imports;
|
||||
hfs_ = hfs;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (imports_)
|
||||
checkImports(*imports_, hfs_.get());
|
||||
return checkHlp(wasmCode, funcName, params);
|
||||
checkImports(imports, &hfs);
|
||||
return checkHlp(wasmCode, hfs, funcName, params, imports);
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
@@ -857,7 +821,12 @@ WasmiEngine::check(
|
||||
}
|
||||
|
||||
NotTEC
|
||||
WasmiEngine::checkHlp(Bytes const& wasmCode, std::string_view funcName, std::vector<WasmParam> const& params)
|
||||
WasmiEngine::checkHlp(
|
||||
Bytes const& wasmCode,
|
||||
HostFunctions& hfs,
|
||||
std::string_view funcName,
|
||||
std::vector<WasmParam> const& params,
|
||||
ImportVec const& imports)
|
||||
{
|
||||
// currently only 1 module support, possible parallel UT run
|
||||
std::lock_guard<decltype(m_)> lg(m_);
|
||||
@@ -866,7 +835,7 @@ WasmiEngine::checkHlp(Bytes const& wasmCode, std::string_view funcName, std::vec
|
||||
if (wasmCode.empty())
|
||||
throw std::runtime_error("empty nodule");
|
||||
|
||||
int const m = addModule(wasmCode, false, -1);
|
||||
int const m = addModule(wasmCode, false, imports, -1);
|
||||
if ((m < 0) || !moduleWrap_)
|
||||
throw std::runtime_error("no module"); // LCOV_EXCL_LINE
|
||||
|
||||
@@ -883,7 +852,7 @@ WasmiEngine::checkHlp(Bytes const& wasmCode, std::string_view funcName, std::vec
|
||||
|
||||
// LCOV_EXCL_START
|
||||
std::int64_t
|
||||
WasmiEngine::getGas()
|
||||
WasmiEngine::getGas() const
|
||||
{
|
||||
return moduleWrap_ ? moduleWrap_->getGas() : -1;
|
||||
}
|
||||
@@ -896,32 +865,13 @@ WasmiEngine::getMem() const
|
||||
}
|
||||
|
||||
InstanceWrapper const&
|
||||
WasmiEngine::getRT(int m, int i)
|
||||
WasmiEngine::getRT(int m, int i) const
|
||||
{
|
||||
if (!moduleWrap_)
|
||||
throw std::runtime_error("no module");
|
||||
return moduleWrap_->getInstance(i);
|
||||
}
|
||||
|
||||
int32_t
|
||||
WasmiEngine::allocate(int32_t sz)
|
||||
{
|
||||
if (sz <= 0)
|
||||
throw std::runtime_error("can't allocate memory, " + std::to_string(sz) + " bytes");
|
||||
|
||||
auto res = call<1>(W_ALLOC, sz);
|
||||
|
||||
if (res.f || !res.r.vec_.size || (res.r.vec_.data[0].kind != WASM_I32))
|
||||
throw std::runtime_error("can't allocate memory, " + std::to_string(sz) + " bytes"); // LCOV_EXCL_LINE
|
||||
|
||||
int32_t const p = res.r.vec_.data[0].of.i32;
|
||||
auto const mem = getMem();
|
||||
if (p <= 0 || p + sz > mem.s)
|
||||
throw std::runtime_error("invalid memory allocation, " + std::to_string(sz) + " bytes");
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
wasm_trap_t*
|
||||
WasmiEngine::newTrap(std::string const& txt)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user