mirror of
https://github.com/XRPLF/rippled.git
synced 2026-06-05 01:37:00 +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:
@@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user