mirror of
https://github.com/XRPLF/rippled.git
synced 2026-06-05 01:37:00 +00:00
fix clang-tidy issues
This commit is contained in:
@@ -1,27 +1,49 @@
|
||||
#include <test/app/wasm_fixtures/fixtures.h>
|
||||
#include <test/jtx/Env.h>
|
||||
|
||||
#include <xrpl/basics/Expected.h>
|
||||
#include <xrpl/beast/unit_test/suite.h>
|
||||
#include <xrpl/protocol/SField.h>
|
||||
#include <xrpl/protocol/TER.h>
|
||||
#include <xrpl/tx/wasm/HostFunc.h>
|
||||
#include <xrpl/tx/wasm/ParamsHelper.h>
|
||||
#include <xrpl/tx/wasm/WasmVM.h>
|
||||
|
||||
#include <boost/algorithm/hex.hpp>
|
||||
|
||||
#include <wasm.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#ifdef _DEBUG
|
||||
// #define DEBUG_OUTPUT 1
|
||||
#endif
|
||||
|
||||
#include <test/app/TestHostFunctions.h>
|
||||
|
||||
#include <xrpl/tx/wasm/HostFuncWrapper.h>
|
||||
|
||||
#include <source_location>
|
||||
|
||||
namespace xrpl {
|
||||
namespace test {
|
||||
namespace xrpl::test {
|
||||
|
||||
bool
|
||||
testGetDataIncrement();
|
||||
|
||||
using Add_proto = int32_t(int32_t, int32_t);
|
||||
static wasm_trap_t*
|
||||
Add(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
|
||||
add(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
|
||||
{
|
||||
int32_t const Val1 = params->data[0].of.i32;
|
||||
int32_t const Val2 = params->data[1].of.i32;
|
||||
int32_t const val1 = params->data[0].of.i32;
|
||||
int32_t const val2 = params->data[1].of.i32;
|
||||
// printf("Host function \"Add\": %d + %d\n", Val1, Val2);
|
||||
results->data[0] = WASM_I32_VAL(Val1 + Val2);
|
||||
results->data[0] = WASM_I32_VAL(val1 + val2);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -76,27 +98,27 @@ uleb128(IT&& it)
|
||||
std::pair<unsigned, unsigned>
|
||||
getSection(Bytes const& module, std::uint8_t n)
|
||||
{
|
||||
static std::uint8_t const hdr[] = {0x00, 0x61, 0x73, 0x6D};
|
||||
static std::uint8_t const ver[] = {0x01, 0x00, 0x00, 0x00};
|
||||
static std::uint8_t const lastSec = 12;
|
||||
static std::uint8_t const kHdr[] = {0x00, 0x61, 0x73, 0x6D};
|
||||
static std::uint8_t const kVer[] = {0x01, 0x00, 0x00, 0x00};
|
||||
static std::uint8_t const kLastSec = 12;
|
||||
|
||||
// sections:
|
||||
// 0: "Custom", 1: "Type", 2: "Import", 3: "Function", 4: "Table", 5: "Memory", 6: "Global",
|
||||
// 7: "Export", 8: "Start", 9: "Element", 10: "Code", 11: "Data", 12: "DataCount"
|
||||
|
||||
if (module.size() < sizeof(hdr) + sizeof(ver) + 2)
|
||||
if (module.size() < sizeof(kHdr) + sizeof(kVer) + 2)
|
||||
return {0, 0};
|
||||
if (memcmp(module.data(), hdr, sizeof(hdr)) != 0)
|
||||
if (memcmp(module.data(), kHdr, sizeof(kHdr)) != 0)
|
||||
return {0, 0};
|
||||
if (memcmp(module.data() + sizeof(hdr), ver, sizeof(ver)) != 0)
|
||||
if (memcmp(module.data() + sizeof(kHdr), kVer, sizeof(kVer)) != 0)
|
||||
return {0, 0};
|
||||
|
||||
unsigned pos = sizeof(hdr) + sizeof(ver); // sections start
|
||||
unsigned pos = sizeof(kHdr) + sizeof(kVer); // sections start
|
||||
for (; pos < module.size();)
|
||||
{
|
||||
auto const start = pos;
|
||||
std::uint8_t const byte = module[pos++];
|
||||
if (byte > lastSec)
|
||||
if (byte > kLastSec)
|
||||
return {0, 0};
|
||||
|
||||
auto [sz, cnt] = uleb128(module.cbegin() + pos);
|
||||
@@ -127,7 +149,7 @@ runFinishFunction(std::string const& code)
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
struct Wasm_test : public beast::unit_test::suite
|
||||
struct Wasm_test : public beast::unit_test::Suite
|
||||
{
|
||||
void
|
||||
checkResult(
|
||||
@@ -188,7 +210,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
HostFunctions hfs;
|
||||
ImportVec imports;
|
||||
WasmImpFunc<Add_proto>(imports, "func-add", reinterpret_cast<void*>(&Add), &hfs);
|
||||
WasmImpFunc<Add_proto>(imports, "func-add", reinterpret_cast<void*>(&add), &hfs);
|
||||
|
||||
auto re = vm.run(wasm, hfs, 10'000'000, "addTwo", wasmParams(1234, 5678), imports);
|
||||
|
||||
@@ -240,7 +262,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
"732b087369676e2d6578742b0f7265666572656e63652d74797065732b0a"
|
||||
"6d756c746976616c7565");
|
||||
|
||||
auto const re = preflightEscrowWasm(badWasm, hfs, ESCROW_FUNCTION_NAME);
|
||||
auto const re = preflightEscrowWasm(badWasm, hfs, escrowFunctionName);
|
||||
BEAST_EXPECT(!isTesSuccess(re));
|
||||
}
|
||||
}
|
||||
@@ -250,7 +272,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
testcase("Wasm get ledger sequence");
|
||||
|
||||
auto ledgerSqnWasm = hexToBytes(ledgerSqnWasmHex);
|
||||
auto ledgerSqnWasm = hexToBytes(kLedgerSqnWasmHex);
|
||||
|
||||
using namespace test::jtx;
|
||||
|
||||
@@ -260,8 +282,8 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
WASM_IMPORT_FUNC2(imports, getLedgerSqn, "get_ledger_sqn", &hfs, 33);
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
auto re = engine.run(
|
||||
ledgerSqnWasm, hfs, 1'000'000, ESCROW_FUNCTION_NAME, {}, imports, env.journal);
|
||||
auto re =
|
||||
engine.run(ledgerSqnWasm, hfs, 1'000'000, escrowFunctionName, {}, imports, env.journal);
|
||||
|
||||
checkResult(re, 0, 440);
|
||||
|
||||
@@ -269,7 +291,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
env.close();
|
||||
|
||||
// empty module, throwing exception
|
||||
re = engine.run({}, hfs, 1'000'000, ESCROW_FUNCTION_NAME, {}, imports, env.journal);
|
||||
re = engine.run({}, hfs, 1'000'000, escrowFunctionName, {}, imports, env.journal);
|
||||
BEAST_EXPECT(!re);
|
||||
env.close();
|
||||
}
|
||||
@@ -279,7 +301,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
testcase("Wasm import/export functions");
|
||||
|
||||
auto impExpWasm = hexToBytes(impExpHex);
|
||||
auto impExpWasm = hexToBytes(kImpExpHex);
|
||||
|
||||
using namespace test::jtx;
|
||||
|
||||
@@ -344,7 +366,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
testcase("Wasm fibo");
|
||||
|
||||
auto const fibWasm = hexToBytes(fibWasmHex);
|
||||
auto const fibWasm = hexToBytes(kFibWasmHex);
|
||||
auto& engine = WasmEngine::instance();
|
||||
HostFunctions hfs;
|
||||
|
||||
@@ -362,7 +384,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
Env env(*this);
|
||||
{
|
||||
auto const allHostFuncWasm = hexToBytes(allHostFunctionsWasmHex);
|
||||
auto const allHostFuncWasm = hexToBytes(kAllHostFunctionsWasmHex);
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
@@ -372,7 +394,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
i.second.second.gas = 0;
|
||||
|
||||
auto re = engine.run(
|
||||
allHostFuncWasm, hfs, 1'000'000, ESCROW_FUNCTION_NAME, {}, imp, env.journal);
|
||||
allHostFuncWasm, hfs, 1'000'000, escrowFunctionName, {}, imp, env.journal);
|
||||
|
||||
checkResult(re, 1, 27'080);
|
||||
|
||||
@@ -386,7 +408,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
env.close();
|
||||
|
||||
{
|
||||
auto const allHostFuncWasm = hexToBytes(allHostFunctionsWasmHex);
|
||||
auto const allHostFuncWasm = hexToBytes(kAllHostFunctionsWasmHex);
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
@@ -394,7 +416,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
auto const imp = createWasmImport(hfs);
|
||||
|
||||
auto re = engine.run(
|
||||
allHostFuncWasm, hfs, 1'000'000, ESCROW_FUNCTION_NAME, {}, imp, env.journal);
|
||||
allHostFuncWasm, hfs, 1'000'000, escrowFunctionName, {}, imp, env.journal);
|
||||
|
||||
checkResult(re, 1, 70'340);
|
||||
|
||||
@@ -403,7 +425,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
// not enough gas
|
||||
{
|
||||
auto const allHostFuncWasm = hexToBytes(allHostFunctionsWasmHex);
|
||||
auto const allHostFuncWasm = hexToBytes(kAllHostFunctionsWasmHex);
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
@@ -411,7 +433,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
auto const imp = createWasmImport(hfs);
|
||||
|
||||
auto re =
|
||||
engine.run(allHostFuncWasm, hfs, 200, ESCROW_FUNCTION_NAME, {}, imp, env.journal);
|
||||
engine.run(allHostFuncWasm, hfs, 200, escrowFunctionName, {}, imp, env.journal);
|
||||
|
||||
if (BEAST_EXPECT(!re))
|
||||
{
|
||||
@@ -428,20 +450,20 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
testcase("escrow wasm devnet test");
|
||||
|
||||
auto const allHFWasm = hexToBytes(allHostFunctionsWasmHex);
|
||||
auto const allHFWasm = hexToBytes(kAllHostFunctionsWasmHex);
|
||||
|
||||
using namespace test::jtx;
|
||||
Env env{*this};
|
||||
{
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, 100'000, ESCROW_FUNCTION_NAME, {});
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, 100'000, escrowFunctionName, {});
|
||||
checkResult(re, 1, 70'340);
|
||||
}
|
||||
|
||||
{
|
||||
// Invalid gas limit (0) should be rejected (boundary condition)
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, -1, ESCROW_FUNCTION_NAME, {});
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, -1, escrowFunctionName, {});
|
||||
BEAST_EXPECT(!re.has_value());
|
||||
BEAST_EXPECT(re.error() == temBAD_AMOUNT);
|
||||
}
|
||||
@@ -449,7 +471,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
// Invalid gas limit (-1) should be rejected
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, 0, ESCROW_FUNCTION_NAME, {});
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, 0, escrowFunctionName, {});
|
||||
BEAST_EXPECT(!re.has_value());
|
||||
BEAST_EXPECT(re.error() == temBAD_AMOUNT);
|
||||
}
|
||||
@@ -458,7 +480,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
// max<int64_t>() gas
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto re = runEscrowWasm(
|
||||
allHFWasm, hfs, std::numeric_limits<int64_t>::max(), ESCROW_FUNCTION_NAME, {});
|
||||
allHFWasm, hfs, std::numeric_limits<int64_t>::max(), escrowFunctionName, {});
|
||||
checkResult(re, 1, 70'340);
|
||||
}
|
||||
|
||||
@@ -471,12 +493,12 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
Expected<Bytes, HostFunctionError>
|
||||
getTxField(SField const& fname) const override
|
||||
{
|
||||
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
|
||||
return Unexpected(HostFunctionError::FieldNotFound);
|
||||
}
|
||||
};
|
||||
|
||||
FieldNotFoundHostFunctions hfs(env);
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, 100'000, ESCROW_FUNCTION_NAME, {});
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, 100'000, escrowFunctionName, {});
|
||||
checkResult(re, -201, 28'965);
|
||||
}
|
||||
|
||||
@@ -494,7 +516,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
};
|
||||
|
||||
OversizedFieldHostFunctions hfs(env);
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, 100'000, ESCROW_FUNCTION_NAME, {});
|
||||
auto re = runEscrowWasm(allHFWasm, hfs, 100'000, escrowFunctionName, {});
|
||||
checkResult(re, -201, 28'965);
|
||||
}
|
||||
|
||||
@@ -502,7 +524,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
#ifndef DEBUG_OUTPUT
|
||||
{ // fail because recursion too deep
|
||||
|
||||
auto const deepWasm = hexToBytes(deepRecursionHex);
|
||||
auto const deepWasm = hexToBytes(kDeepRecursionHex);
|
||||
|
||||
TestHostFunctionsSink hfs(env);
|
||||
std::string const funcName("finish");
|
||||
@@ -530,7 +552,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
#endif
|
||||
|
||||
{ // infinite loop
|
||||
auto const infiniteLoopWasm = hexToBytes(infiniteLoopWasmHex);
|
||||
auto const infiniteLoopWasm = hexToBytes(kInfiniteLoopWasmHex);
|
||||
std::string const funcName("loop");
|
||||
TestHostFunctions hfs(env, 0);
|
||||
|
||||
@@ -544,7 +566,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
{
|
||||
// expected import not provided
|
||||
auto const lgrSqnWasm = hexToBytes(ledgerSqnWasmHex);
|
||||
auto const lgrSqnWasm = hexToBytes(kLedgerSqnWasmHex);
|
||||
TestLedgerDataProvider hfs(env);
|
||||
ImportVec imports;
|
||||
WASM_IMPORT_FUNC2(imports, getLedgerSqn, "get_ledger_sqn2", &hfs);
|
||||
@@ -552,14 +574,14 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
auto re = engine.run(
|
||||
lgrSqnWasm, hfs, 1'000'000, ESCROW_FUNCTION_NAME, {}, imports, env.journal);
|
||||
lgrSqnWasm, hfs, 1'000'000, escrowFunctionName, {}, imports, env.journal);
|
||||
|
||||
BEAST_EXPECT(!re);
|
||||
}
|
||||
|
||||
{
|
||||
// bad import format
|
||||
auto const lgrSqnWasm = hexToBytes(ledgerSqnWasmHex);
|
||||
auto const lgrSqnWasm = hexToBytes(kLedgerSqnWasmHex);
|
||||
TestLedgerDataProvider hfs(env);
|
||||
ImportVec imports;
|
||||
WASM_IMPORT_FUNC2(imports, getLedgerSqn, "get_ledger_sqn", &hfs);
|
||||
@@ -568,14 +590,14 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
auto& engine = WasmEngine::instance();
|
||||
|
||||
auto re = engine.run(
|
||||
lgrSqnWasm, hfs, 1'000'000, ESCROW_FUNCTION_NAME, {}, imports, env.journal);
|
||||
lgrSqnWasm, hfs, 1'000'000, escrowFunctionName, {}, imports, env.journal);
|
||||
|
||||
BEAST_EXPECT(!re);
|
||||
}
|
||||
|
||||
{
|
||||
// bad function name
|
||||
auto const lgrSqnWasm = hexToBytes(ledgerSqnWasmHex);
|
||||
auto const lgrSqnWasm = hexToBytes(kLedgerSqnWasmHex);
|
||||
TestLedgerDataProvider hfs(env);
|
||||
ImportVec imports;
|
||||
WASM_IMPORT_FUNC2(imports, getLedgerSqn, "get_ledger_sqn", &hfs);
|
||||
@@ -598,7 +620,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
Env env(*this);
|
||||
{
|
||||
auto const floatTestWasm = hexToBytes(floatTestsWasmHex);
|
||||
auto const floatTestWasm = hexToBytes(kFloatTestsWasmHex);
|
||||
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto re = runEscrowWasm(floatTestWasm, hfs, 200'000, funcName, {});
|
||||
@@ -607,7 +629,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
}
|
||||
|
||||
{
|
||||
auto const float0Wasm = hexToBytes(float0Hex);
|
||||
auto const float0Wasm = hexToBytes(kFloat0Hex);
|
||||
|
||||
TestHostFunctions hfs(env, 0);
|
||||
auto re = runEscrowWasm(float0Wasm, hfs, 100'000, funcName, {});
|
||||
@@ -625,11 +647,11 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
Env env{*this};
|
||||
|
||||
auto const codecovWasm = hexToBytes(codecovTestsWasmHex);
|
||||
auto const codecovWasm = hexToBytes(kCodecovTestsWasmHex);
|
||||
TestHostFunctions hfs(env, 0);
|
||||
|
||||
auto const allowance = 220'169;
|
||||
auto re = runEscrowWasm(codecovWasm, hfs, allowance, ESCROW_FUNCTION_NAME, {});
|
||||
auto re = runEscrowWasm(codecovWasm, hfs, allowance, escrowFunctionName, {});
|
||||
|
||||
checkResult(re, 1, allowance);
|
||||
}
|
||||
@@ -642,7 +664,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
using namespace test::jtx;
|
||||
Env env{*this};
|
||||
|
||||
auto disabledFloatWasm = hexToBytes(disabledFloatHex);
|
||||
auto disabledFloatWasm = hexToBytes(kDisabledFloatHex);
|
||||
std::string const funcName("finish");
|
||||
TestHostFunctions hfs(env, 0);
|
||||
|
||||
@@ -670,82 +692,82 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
testWasmMemory()
|
||||
{
|
||||
testcase("Wasm additional memory limit tests");
|
||||
BEAST_EXPECT(runFinishFunction(memoryPointerAtLimitHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(memoryPointerOverLimitHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(memoryOffsetOverLimitHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(memoryEndOfWordOverLimitHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(memoryGrow0To1PageHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(memoryGrow1To0PageHex).value() == -1);
|
||||
BEAST_EXPECT(runFinishFunction(memoryLastByteOf8MBHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(memoryGrow1MoreThan8MBHex).value() == -1);
|
||||
BEAST_EXPECT(runFinishFunction(memoryGrow0MoreThan8MBHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(memoryInit1MoreThan8MBHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(memoryNegativeAddressHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kMemoryPointerAtLimitHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(kMemoryPointerOverLimitHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kMemoryOffsetOverLimitHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kMemoryEndOfWordOverLimitHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kMemoryGrow0To1PageHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(kMemoryGrow1To0PageHex).value() == -1);
|
||||
BEAST_EXPECT(runFinishFunction(kMemoryLastByteOf8MbHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(kMemoryGrow1MoreThan8MbHex).value() == -1);
|
||||
BEAST_EXPECT(runFinishFunction(kMemoryGrow0MoreThan8MbHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(kMemoryInit1MoreThan8MbHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kMemoryNegativeAddressHex).has_value() == false);
|
||||
}
|
||||
|
||||
void
|
||||
testWasmTable()
|
||||
{
|
||||
testcase("Wasm table limit tests");
|
||||
BEAST_EXPECT(runFinishFunction(table64ElementsHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(table65ElementsHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(table2TablesHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(table0ElementsHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(tableUintMaxHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kTable64ElementsHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(kTable65ElementsHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kTable2TablesHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kTable0ElementsHex).value() == 1);
|
||||
BEAST_EXPECT(runFinishFunction(kTableUintMaxHex).has_value() == false);
|
||||
}
|
||||
|
||||
void
|
||||
testWasmProposal()
|
||||
{
|
||||
testcase("Wasm disabled proposal tests");
|
||||
BEAST_EXPECT(runFinishFunction(proposalMutableGlobalHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalGcStructNewHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalMultiValueHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalSignExtHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalFloatToIntHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalBulkMemoryHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalRefTypesHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalTailCallHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalExtendedConstHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalMultiMemoryHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalCustomPageSizesHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalMemory64Hex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(proposalWideArithmeticHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalMutableGlobalHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalGcStructNewHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalMultiValueHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalSignExtHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalFloatToIntHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalBulkMemoryHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalRefTypesHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalTailCallHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalExtendedConstHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalMultiMemoryHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalCustomPageSizesHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalMemory64Hex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kProposalWideArithmeticHex).has_value() == false);
|
||||
}
|
||||
|
||||
void
|
||||
testWasmTrap()
|
||||
{
|
||||
testcase("Wasm trap tests");
|
||||
BEAST_EXPECT(runFinishFunction(trapDivideBy0Hex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(trapIntOverflowHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(trapUnreachableHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(trapNullCallHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(trapFuncSigMismatchHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kTrapDivideBy0Hex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kTrapIntOverflowHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kTrapUnreachableHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kTrapNullCallHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kTrapFuncSigMismatchHex).has_value() == false);
|
||||
}
|
||||
|
||||
void
|
||||
testWasmWasi()
|
||||
{
|
||||
testcase("Wasm Wasi tests");
|
||||
BEAST_EXPECT(runFinishFunction(wasiGetTimeHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(wasiPrintHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kWasiGetTimeHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kWasiPrintHex).has_value() == false);
|
||||
}
|
||||
|
||||
void
|
||||
testWasmSectionCorruption()
|
||||
{
|
||||
testcase("Wasm Section Corruption tests");
|
||||
BEAST_EXPECT(runFinishFunction(badMagicNumberHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(badVersionNumberHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(lyingHeaderHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(neverEndingNumberHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(vectorLieHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(sectionOrderingHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(ghostPayloadHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(junkAfterSectionHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(invalidSectionIdHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(localVariableBombHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kBadMagicNumberHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kBadVersionNumberHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kLyingHeaderHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kNeverEndingNumberHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kVectorLieHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kSectionOrderingHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kGhostPayloadHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kJunkAfterSectionHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kInvalidSectionIdHex).has_value() == false);
|
||||
BEAST_EXPECT(runFinishFunction(kLocalVariableBombHex).has_value() == false);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -756,7 +778,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
using namespace test::jtx;
|
||||
Env env(*this);
|
||||
|
||||
auto const startLoopWasm = hexToBytes(startLoopHex);
|
||||
auto const startLoopWasm = hexToBytes(kStartLoopHex);
|
||||
TestLedgerDataProvider hfs(env);
|
||||
ImportVec const imports;
|
||||
|
||||
@@ -764,8 +786,8 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
auto checkRes = engine.check(startLoopWasm, hfs, "finish", {}, imports, env.journal);
|
||||
BEAST_EXPECTS(checkRes == tesSUCCESS, std::to_string(TERtoInt(checkRes)));
|
||||
|
||||
auto re = engine.run(
|
||||
startLoopWasm, hfs, 1'000'000, ESCROW_FUNCTION_NAME, {}, imports, env.journal);
|
||||
auto re =
|
||||
engine.run(startLoopWasm, hfs, 1'000'000, escrowFunctionName, {}, imports, env.journal);
|
||||
BEAST_EXPECTS(re.error() == tecFAILED_PROCESSING, std::to_string(TERtoInt(re.error())));
|
||||
}
|
||||
|
||||
@@ -775,7 +797,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
testcase("Wasm Bad Align");
|
||||
|
||||
// bad_align.c
|
||||
auto const badAlignWasm = hexToBytes(badAlignWasmHex);
|
||||
auto const badAlignWasm = hexToBytes(kBadAlignWasmHex);
|
||||
|
||||
using namespace test::jtx;
|
||||
|
||||
@@ -816,7 +838,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
"071302066d656d6f727902000666696e69736800000a0a01"
|
||||
"08004280808080100b";
|
||||
auto const wasm = hexToBytes(wasmHex);
|
||||
auto const re = runEscrowWasm(wasm, hfs, 100'000, ESCROW_FUNCTION_NAME, {});
|
||||
auto const re = runEscrowWasm(wasm, hfs, 100'000, escrowFunctionName, {});
|
||||
BEAST_EXPECT(!re);
|
||||
}
|
||||
|
||||
@@ -832,7 +854,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
"0061736d01000000010401600000030201000503010001071302066d656d6f"
|
||||
"727902000666696e69736800000a050103000f0b";
|
||||
auto const wasm = hexToBytes(wasmHex);
|
||||
auto const re = runEscrowWasm(wasm, hfs, 100'000, ESCROW_FUNCTION_NAME, {});
|
||||
auto const re = runEscrowWasm(wasm, hfs, 100'000, escrowFunctionName, {});
|
||||
BEAST_EXPECT(!re);
|
||||
}
|
||||
|
||||
@@ -847,7 +869,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
"6d6f727902000666696e69736800000a10010e0041808080800141ff818080"
|
||||
"010b";
|
||||
auto const wasm = hexToBytes(wasmHex);
|
||||
auto const re = runEscrowWasm(wasm, hfs, 100'000, ESCROW_FUNCTION_NAME, {});
|
||||
auto const re = runEscrowWasm(wasm, hfs, 100'000, escrowFunctionName, {});
|
||||
BEAST_EXPECT(!re);
|
||||
}
|
||||
}
|
||||
@@ -909,53 +931,53 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
testcase("Wasm swap bytes");
|
||||
|
||||
uint64_t const SWAP_DATAU64 = 0x123456789abcdeffull;
|
||||
uint64_t const REVERSE_SWAP_DATAU64 = 0xffdebc9a78563412ull;
|
||||
int64_t const SWAP_DATAI64 = 0x123456789abcdeffll;
|
||||
int64_t const REVERSE_SWAP_DATAI64 = 0xffdebc9a78563412ll;
|
||||
uint64_t const swapDataU64 = 0x123456789abcdeffull;
|
||||
uint64_t const reverseSwapDataU64 = 0xffdebc9a78563412ull;
|
||||
int64_t const swapDataI64 = 0x123456789abcdeffll;
|
||||
int64_t const reverseSwapDataI64 = 0xffdebc9a78563412ll;
|
||||
|
||||
uint32_t const SWAP_DATAU32 = 0x12789aff;
|
||||
uint32_t const REVERSE_SWAP_DATAU32 = 0xff9a7812;
|
||||
int32_t const SWAP_DATAI32 = 0x12789aff;
|
||||
int32_t const REVERSE_SWAP_DATAI32 = 0xff9a7812;
|
||||
uint32_t const swapDataU32 = 0x12789aff;
|
||||
uint32_t const reverseSwapDataU32 = 0xff9a7812;
|
||||
int32_t const swapDataI32 = 0x12789aff;
|
||||
int32_t const reverseSwapDataI32 = 0xff9a7812;
|
||||
|
||||
uint16_t const SWAP_DATAU16 = 0x12ff;
|
||||
uint16_t const REVERSE_SWAP_DATAU16 = 0xff12;
|
||||
int16_t const SWAP_DATAI16 = 0x12ff;
|
||||
int16_t const REVERSE_SWAP_DATAI16 = 0xff12;
|
||||
uint16_t const swapDataU16 = 0x12ff;
|
||||
uint16_t const reverseSwapDataU16 = 0xff12;
|
||||
int16_t const swapDataI16 = 0x12ff;
|
||||
int16_t const reverseSwapDataI16 = 0xff12;
|
||||
|
||||
uint64_t b1 = SWAP_DATAU64;
|
||||
int64_t b2 = SWAP_DATAI64;
|
||||
uint64_t b1 = swapDataU64;
|
||||
int64_t b2 = swapDataI64;
|
||||
b1 = adjustWasmEndianessHlp(b1);
|
||||
b2 = adjustWasmEndianessHlp(b2);
|
||||
BEAST_EXPECT(b1 == REVERSE_SWAP_DATAU64);
|
||||
BEAST_EXPECT(b2 == REVERSE_SWAP_DATAI64);
|
||||
BEAST_EXPECT(b1 == reverseSwapDataU64);
|
||||
BEAST_EXPECT(b2 == reverseSwapDataI64);
|
||||
b1 = adjustWasmEndianessHlp(b1);
|
||||
b2 = adjustWasmEndianessHlp(b2);
|
||||
BEAST_EXPECT(b1 == SWAP_DATAU64);
|
||||
BEAST_EXPECT(b2 == SWAP_DATAI64);
|
||||
BEAST_EXPECT(b1 == swapDataU64);
|
||||
BEAST_EXPECT(b2 == swapDataI64);
|
||||
|
||||
uint32_t b3 = SWAP_DATAU32;
|
||||
int32_t b4 = SWAP_DATAI32;
|
||||
uint32_t b3 = swapDataU32;
|
||||
int32_t b4 = swapDataI32;
|
||||
b3 = adjustWasmEndianessHlp(b3);
|
||||
b4 = adjustWasmEndianessHlp(b4);
|
||||
BEAST_EXPECT(b3 == REVERSE_SWAP_DATAU32);
|
||||
BEAST_EXPECT(b4 == REVERSE_SWAP_DATAI32);
|
||||
BEAST_EXPECT(b3 == reverseSwapDataU32);
|
||||
BEAST_EXPECT(b4 == reverseSwapDataI32);
|
||||
b3 = adjustWasmEndianessHlp(b3);
|
||||
b4 = adjustWasmEndianessHlp(b4);
|
||||
BEAST_EXPECT(b3 == SWAP_DATAU32);
|
||||
BEAST_EXPECT(b4 == SWAP_DATAI32);
|
||||
BEAST_EXPECT(b3 == swapDataU32);
|
||||
BEAST_EXPECT(b4 == swapDataI32);
|
||||
|
||||
uint16_t b5 = SWAP_DATAU16;
|
||||
int16_t b6 = SWAP_DATAI16;
|
||||
uint16_t b5 = swapDataU16;
|
||||
int16_t b6 = swapDataI16;
|
||||
b5 = adjustWasmEndianessHlp(b5);
|
||||
b6 = adjustWasmEndianessHlp(b6);
|
||||
BEAST_EXPECT(b5 == REVERSE_SWAP_DATAU16);
|
||||
BEAST_EXPECT(b6 == REVERSE_SWAP_DATAI16);
|
||||
BEAST_EXPECT(b5 == reverseSwapDataU16);
|
||||
BEAST_EXPECT(b6 == reverseSwapDataI16);
|
||||
b5 = adjustWasmEndianessHlp(b5);
|
||||
b6 = adjustWasmEndianessHlp(b6);
|
||||
BEAST_EXPECT(b5 == SWAP_DATAU16);
|
||||
BEAST_EXPECT(b6 == SWAP_DATAI16);
|
||||
BEAST_EXPECT(b5 == swapDataU16);
|
||||
BEAST_EXPECT(b6 == swapDataI16);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -963,8 +985,8 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
testcase("Wasm Many params");
|
||||
|
||||
auto const params1k = hexToBytes(thousandParamsHex);
|
||||
auto const params1k1 = hexToBytes(thousand1ParamsHex);
|
||||
auto const params1k = hexToBytes(kThousandParamsHex);
|
||||
auto const params1k1 = hexToBytes(kThousand1ParamsHex);
|
||||
|
||||
using namespace test::jtx;
|
||||
|
||||
@@ -976,7 +998,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
std::vector<WasmParam> params;
|
||||
params.reserve(1000);
|
||||
for (int i = 0; i < 1000; ++i)
|
||||
params.push_back({.type = WT_I32, .of = {.i32 = 2 * i}});
|
||||
params.push_back({.type = WtI32, .of = {.i32 = 2 * i}});
|
||||
|
||||
auto& engine = WasmEngine::instance();
|
||||
{
|
||||
@@ -985,14 +1007,14 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
}
|
||||
|
||||
// add 1 more parameter, module can't be created now
|
||||
params.push_back({.type = WT_I32, .of = {.i32 = 2 * 1000}});
|
||||
params.push_back({.type = WtI32, .of = {.i32 = 2 * 1000}});
|
||||
{
|
||||
auto re = engine.run(params1k1, hfs, 1'000'000, "test", params, imports, env.journal);
|
||||
BEAST_EXPECT(!re);
|
||||
}
|
||||
|
||||
// function that create 10k local variables
|
||||
auto const locals10k = hexToBytes(locals10kHex);
|
||||
auto const locals10k = hexToBytes(kLocals10kHex);
|
||||
{
|
||||
auto re = engine.run(
|
||||
locals10k, hfs, 1'000'000, "test", wasmParams(0, 1), imports, env.journal);
|
||||
@@ -1000,7 +1022,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
}
|
||||
|
||||
// module has 5k functions
|
||||
auto const functions5k = hexToBytes(functions5kHex);
|
||||
auto const functions5k = hexToBytes(kFunctions5kHex);
|
||||
{
|
||||
auto re = engine.run(
|
||||
functions5k, hfs, 1'000'000, "test0001", wasmParams(2, 3), imports, env.journal);
|
||||
@@ -1015,11 +1037,11 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
{
|
||||
using namespace test::jtx;
|
||||
|
||||
unsigned const RESERVED = 64;
|
||||
unsigned const reserved = 64;
|
||||
std::uint8_t const nop = 0x01;
|
||||
std::array<std::uint8_t, 16> const codeMarker = {
|
||||
nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop};
|
||||
auto const opcReserved = hexToBytes(opcReservedHex);
|
||||
auto const opcReserved = hexToBytes(kOpcReservedHex);
|
||||
|
||||
Env env{*this};
|
||||
auto& engine = WasmEngine::instance();
|
||||
@@ -1096,7 +1118,7 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
if (!BEAST_EXPECTS(!codeRange.empty(), lineStr))
|
||||
return;
|
||||
|
||||
if (!BEAST_EXPECTS(codeSnap.size() < RESERVED, lineStr))
|
||||
if (!BEAST_EXPECTS(codeSnap.size() < reserved, lineStr))
|
||||
return;
|
||||
auto it = codeRange.begin();
|
||||
for (auto x : codeSnap)
|
||||
@@ -1554,5 +1576,4 @@ struct Wasm_test : public beast::unit_test::suite
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(Wasm, app, xrpl);
|
||||
|
||||
} // namespace test
|
||||
} // namespace xrpl
|
||||
} // namespace xrpl::test
|
||||
|
||||
Reference in New Issue
Block a user