fix clang-tidy issues

This commit is contained in:
Mayukha Vadari
2026-05-19 15:11:55 -04:00
parent e77934302a
commit b664989cfb
22 changed files with 1397 additions and 1191 deletions

View File

@@ -14,27 +14,27 @@ namespace xrpl {
enum class HostFunctionError : int32_t {
INTERNAL = -1,
FIELD_NOT_FOUND = -2,
BUFFER_TOO_SMALL = -3,
NO_ARRAY = -4,
NOT_LEAF_FIELD = -5,
LOCATOR_MALFORMED = -6,
SLOT_OUT_RANGE = -7,
SLOTS_FULL = -8,
EMPTY_SLOT = -9,
LEDGER_OBJ_NOT_FOUND = -10,
FieldNotFound = -2,
BufferTooSmall = -3,
NoArray = -4,
NotLeafField = -5,
LocatorMalformed = -6,
SlotOutRange = -7,
SlotsFull = -8,
EmptySlot = -9,
LedgerObjNotFound = -10,
DECODING = -11,
DATA_FIELD_TOO_LARGE = -12,
POINTER_OUT_OF_BOUNDS = -13,
NO_MEM_EXPORTED = -14,
INVALID_PARAMS = -15,
INVALID_ACCOUNT = -16,
INVALID_FIELD = -17,
INDEX_OUT_OF_BOUNDS = -18,
FLOAT_INPUT_MALFORMED = -19,
FLOAT_COMPUTATION_ERROR = -20,
NO_RUNTIME = -21,
OUT_OF_GAS = -22,
DataFieldTooLarge = -12,
PointerOutOfBounds = -13,
NoMemExported = -14,
InvalidParams = -15,
InvalidAccount = -16,
InvalidField = -17,
IndexOutOfBounds = -18,
FloatInputMalformed = -19,
FloatComputationError = -20,
NoRuntime = -21,
OutOfGas = -22,
};
using FloatPair = std::pair<int64_t, int32_t>;
@@ -97,9 +97,9 @@ floatPowerImpl(Slice const& x, int32_t n, int32_t mode);
// Intended to work only through wasm runtime. Don't call them directly, except with unit tests
struct HostFunctions
{
beast::Journal j_;
beast::Journal j;
HostFunctions(beast::Journal j = beast::Journal{beast::Journal::getNullSink()}) : j_(j)
HostFunctions(beast::Journal j = beast::Journal{beast::Journal::getNullSink()}) : j(j)
{
}
@@ -109,19 +109,19 @@ struct HostFunctions
{
}
virtual void*
[[nodiscard]] virtual void*
getRT() const
{
return nullptr;
}
beast::Journal
[[nodiscard]] beast::Journal
getJournal() const
{
return j_;
return j;
}
virtual bool
[[nodiscard]] virtual bool
checkSelf() const
{
return true;

View File

@@ -13,8 +13,8 @@ class WasmHostFunctionsImpl : public HostFunctions
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_;
static int constexpr maxCache = 256;
std::array<std::shared_ptr<SLE const>, maxCache> cache_;
std::optional<Bytes> data_;
@@ -27,17 +27,17 @@ class WasmHostFunctionsImpl : public HostFunctions
currentLedgerObj_ = ctx_.view().read(leKey_);
if (*currentLedgerObj_)
return *currentLedgerObj_;
return Unexpected(HostFunctionError::LEDGER_OBJ_NOT_FOUND);
return Unexpected(HostFunctionError::LedgerObjNotFound);
}
Expected<int32_t, HostFunctionError>
normalizeCacheIndex(int32_t cacheIdx) const
{
--cacheIdx;
if (cacheIdx < 0 || cacheIdx >= MAX_CACHE)
return Unexpected(HostFunctionError::SLOT_OUT_RANGE);
if (cacheIdx < 0 || cacheIdx >= maxCache)
return Unexpected(HostFunctionError::SlotOutRange);
if (!cache_[cacheIdx])
return Unexpected(HostFunctionError::EMPTY_SLOT);
return Unexpected(HostFunctionError::EmptySlot);
return cacheIdx;
}
@@ -52,7 +52,7 @@ class WasmHostFunctionsImpl : public HostFunctions
return;
auto j = getJournal().trace();
#endif
j << "WasmTrace[" << to_short_string(leKey_.key) << "]: " << msg << " " << dataFn();
j << "WasmTrace[" << toShortString(leKey_.key) << "]: " << msg << " " << dataFn();
#ifdef DEBUG_OUTPUT
j << std::endl;
@@ -65,19 +65,19 @@ public:
{
}
virtual void
void
setRT(void* rt) override
{
rt_ = rt;
}
virtual void*
void*
getRT() const override
{
return rt_;
}
virtual bool
bool
checkSelf() const override
{
return !currentLedgerObj_ && !data_ &&

View File

@@ -18,7 +18,7 @@ namespace xrpl {
using Bytes = std::vector<std::uint8_t>;
using Hash = xrpl::uint256;
struct wmem
struct Wmem
{
std::uint8_t* p = nullptr;
std::size_t s = 0;
@@ -30,13 +30,13 @@ struct WasmResult
T result;
int64_t cost;
};
typedef WasmResult<int32_t> EscrowResult;
using EscrowResult = WasmResult<int32_t>;
struct WasmRuntimeWrapper
{
virtual ~WasmRuntimeWrapper() = default;
virtual wmem
virtual Wmem
getMem() = 0;
virtual std::int64_t
@@ -48,7 +48,7 @@ struct WasmRuntimeWrapper
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
enum WasmTypes { WT_I32, WT_I64 };
enum WasmTypes { WtI32, WtI64 };
struct WasmImportFunc
{
@@ -61,8 +61,8 @@ struct WasmImportFunc
uint32_t gas = 0;
};
typedef std::pair<void*, WasmImportFunc> WasmUserData;
typedef std::unordered_map<std::string_view, WasmUserData> ImportVec;
using WasmUserData = std::pair<void*, WasmImportFunc>;
using ImportVec = std::unordered_map<std::string_view, WasmUserData>;
#define WASM_IMPORT_FUNC(v, f, ...) \
WasmImpFunc<f##_proto>(v, #f, reinterpret_cast<void*>(&f##_wrap), ##__VA_ARGS__)
@@ -71,43 +71,61 @@ typedef std::unordered_map<std::string_view, WasmUserData> ImportVec;
#define WASM_IMPORT_FUNC2(v, f, n, ...) \
WasmImpFunc<f##_proto>(v, n, reinterpret_cast<void*>(&f##_wrap), ##__VA_ARGS__)
template <int N, int C, typename mpl>
template <int N, int C, typename Mpl>
void
WasmImpArgs(WasmImportFunc& e)
{
if constexpr (N < C)
{
using at = typename boost::mpl::at_c<mpl, N>::type;
using at = typename boost::mpl::at_c<Mpl, N>::type;
if constexpr (std::is_pointer_v<at>)
e.params.push_back(WT_I32);
{
e.params.push_back(WtI32);
}
else if constexpr (std::is_same_v<at, std::int32_t>)
e.params.push_back(WT_I32);
{
e.params.push_back(WtI32);
}
else if constexpr (std::is_same_v<at, std::int64_t>)
e.params.push_back(WT_I64);
{
e.params.push_back(WtI64);
}
else
{
static_assert(std::is_pointer_v<at>, "Unsupported argument type");
}
return WasmImpArgs<N + 1, C, mpl>(e);
return WasmImpArgs<N + 1, C, Mpl>(e);
}
return;
}
template <typename rt>
template <typename Rt>
void
WasmImpRet(WasmImportFunc& e)
{
if constexpr (std::is_pointer_v<rt>)
e.result = WT_I32;
else if constexpr (std::is_same_v<rt, std::int32_t>)
e.result = WT_I32;
else if constexpr (std::is_same_v<rt, std::int64_t>)
e.result = WT_I64;
else if constexpr (std::is_void_v<rt>)
if constexpr (std::is_pointer_v<Rt>)
{
e.result = WtI32;
}
else if constexpr (std::is_same_v<Rt, std::int32_t>)
{
e.result = WtI32;
}
else if constexpr (std::is_same_v<Rt, std::int64_t>)
{
e.result = WtI64;
}
else if constexpr (std::is_void_v<Rt>)
{
e.result.reset();
#if (defined(__GNUC__) && (__GNUC__ >= 14)) || \
((defined(__clang_major__)) && (__clang_major__ >= 18))
}
else
{
static_assert(false, "Unsupported return type");
}
#endif
}
@@ -129,17 +147,17 @@ template <typename F>
void
WasmImpFunc(
ImportVec& v,
std::string_view imp_name,
void* f_wrap,
std::string_view impName,
void* fWrap,
void* data = nullptr,
uint32_t gas = 0)
{
WasmImportFunc e;
e.name = imp_name;
e.wrap = f_wrap;
e.name = impName;
e.wrap = fWrap;
e.gas = gas;
WasmImpFuncHelper<F>(e);
v.emplace(imp_name, std::make_pair(data, std::move(e)));
v.emplace(impName, std::make_pair(data, std::move(e)));
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -148,7 +166,7 @@ struct WasmParam
{
// We are not supporting float/double
WasmTypes type = WT_I32;
WasmTypes type = WtI32;
union
{
std::int32_t i32;
@@ -160,7 +178,7 @@ template <class... Types>
inline void
wasmParamsHlp(std::vector<WasmParam>& v, std::int32_t p, Types&&... args)
{
v.push_back({.type = WT_I32, .of = {.i32 = p}});
v.push_back({.type = WtI32, .of = {.i32 = p}});
wasmParamsHlp(v, std::forward<Types>(args)...);
}
@@ -168,7 +186,7 @@ template <class... Types>
inline void
wasmParamsHlp(std::vector<WasmParam>& v, std::int64_t p, Types&&... args)
{
v.push_back({.type = WT_I64, .of = {.i64 = p}});
v.push_back({.type = WtI64, .of = {.i64 = p}});
wasmParamsHlp(v, std::forward<Types>(args)...);
}
@@ -188,29 +206,29 @@ wasmParams(Types&&... args)
return v;
}
template <typename T, size_t size = sizeof(T)>
inline constexpr T
template <typename T, size_t Size = sizeof(T)>
constexpr T
adjustWasmEndianessHlp(T x)
{
static_assert(std::is_integral<T>::value, "Only integral types");
if constexpr (size > 1)
static_assert(std::is_integral_v<T>, "Only integral types");
if constexpr (Size > 1)
{
using U = std::make_unsigned<T>::type;
using U = std::make_unsigned_t<T>;
U u = static_cast<U>(x);
U const low = (u & 0xFF) << ((size - 1) << 3);
u = adjustWasmEndianessHlp<U, size - 1>(u >> 8);
U const low = (u & 0xFF) << ((Size - 1) << 3);
u = adjustWasmEndianessHlp<U, Size - 1>(u >> 8);
return static_cast<T>(low | u);
}
return x;
}
template <typename T, size_t size = sizeof(T)>
inline constexpr T
template <typename T, size_t Size = sizeof(T)>
constexpr T
adjustWasmEndianess(T x)
{
// LCOV_EXCL_START
static_assert(std::is_integral<T>::value, "Only integral types");
static_assert(std::is_integral_v<T>, "Only integral types");
if constexpr (std::endian::native == std::endian::big)
{
return adjustWasmEndianessHlp(x);

View File

@@ -6,19 +6,19 @@
namespace xrpl {
std::string_view inline constexpr W_ENV = "env";
std::string_view inline constexpr W_HOST_LIB = "host_lib";
std::string_view inline constexpr W_MEM = "memory";
std::string_view inline constexpr W_STORE = "store";
std::string_view inline constexpr W_LOAD = "load";
std::string_view inline constexpr W_SIZE = "size";
std::string_view inline constexpr W_ALLOC = "allocate";
std::string_view inline constexpr W_DEALLOC = "deallocate";
std::string_view inline constexpr W_PROC_EXIT = "proc_exit";
std::string_view inline constexpr wEnv = "env";
std::string_view inline constexpr wHostLib = "host_lib";
std::string_view inline constexpr wMem = "memory";
std::string_view inline constexpr wStore = "store";
std::string_view inline constexpr wLoad = "load";
std::string_view inline constexpr wSize = "size";
std::string_view inline constexpr wAlloc = "allocate";
std::string_view inline constexpr wDealloc = "deallocate";
std::string_view inline constexpr wProcExit = "proc_exit";
std::string_view inline constexpr ESCROW_FUNCTION_NAME = "finish";
std::string_view inline constexpr escrowFunctionName = "finish";
uint32_t inline constexpr MAX_PAGES = 128; // 8MB = 64KB*128
uint32_t inline constexpr maxPages = 128; // 8MB = 64KB*128
class WasmiEngine;
@@ -61,7 +61,7 @@ public:
void*
newTrap(std::string const& txt = std::string());
beast::Journal
[[nodiscard]] beast::Journal
getJournal() const;
};
@@ -75,14 +75,14 @@ runEscrowWasm(
Bytes const& wasmCode,
HostFunctions& hfs,
int64_t gasLimit,
std::string_view funcName = ESCROW_FUNCTION_NAME,
std::string_view funcName = escrowFunctionName,
std::vector<WasmParam> const& params = {});
NotTEC
preflightEscrowWasm(
Bytes const& wasmCode,
HostFunctions& hfs,
std::string_view funcName = ESCROW_FUNCTION_NAME,
std::string_view funcName = escrowFunctionName,
std::vector<WasmParam> const& params = {});
} // namespace xrpl

View File

@@ -67,7 +67,7 @@ public:
return &vec_;
}
T const*
[[nodiscard]] T const*
get() const
{
return &vec_;
@@ -89,13 +89,13 @@ public:
return vec_.data[i];
}
size_t
[[nodiscard]] size_t
size() const
{
return vec_.size;
}
bool
[[nodiscard]] bool
empty() const
{
return vec_.size == 0u;
@@ -121,7 +121,7 @@ struct WasmiResult
WasmValVec r;
bool f{false}; // failure flag
WasmiResult(unsigned N = 0) : r(N)
WasmiResult(unsigned n = 0) : r(n)
{
}
@@ -141,11 +141,11 @@ using FuncInfo = std::pair<wasm_func_t const*, wasm_functype_t const*>;
struct InstanceWrapper
{
wasm_store_t* store_ = nullptr;
WasmExternVec exports_;
mutable int memIdx_ = -1;
InstancePtr instance_;
beast::Journal j_ = beast::Journal(beast::Journal::getNullSink());
wasm_store_t* store = nullptr;
WasmExternVec exports;
mutable int memIdx = -1;
InstancePtr instance;
beast::Journal j = beast::Journal(beast::Journal::getNullSink());
private:
static InstancePtr
@@ -176,7 +176,7 @@ public:
FuncInfo
getFunc(std::string_view funcName, WasmExporttypeVec const& exportTypes) const;
wmem
Wmem
getMem() const;
std::int64_t
@@ -188,10 +188,10 @@ public:
struct ModuleWrapper
{
ModulePtr module_;
InstanceWrapper instanceWrap_;
WasmExporttypeVec exportTypes_;
beast::Journal j_ = beast::Journal(beast::Journal::getNullSink());
ModulePtr module;
InstanceWrapper instanceWrap;
WasmExporttypeVec exportTypes;
beast::Journal j = beast::Journal(beast::Journal::getNullSink());
private:
static ModulePtr
@@ -218,7 +218,7 @@ public:
wasm_functype_t*
getFuncType(std::string_view funcName) const;
wmem
Wmem
getMem() const;
InstanceWrapper&
@@ -269,21 +269,21 @@ public:
ImportVec const& imports,
beast::Journal j);
std::int64_t
[[nodiscard]] std::int64_t
getGas() const;
// Host functions helper functionality
wasm_trap_t*
newTrap(std::string const& msg);
beast::Journal
[[nodiscard]] beast::Journal
getJournal() const;
private:
InstanceWrapper&
[[nodiscard]] InstanceWrapper&
getRT(int m = 0, int i = 0) const;
wmem
[[nodiscard]] Wmem
getMem() const;
Expected<WasmResult<int32_t>, TER>
@@ -318,7 +318,7 @@ private:
int32_t
makeModule(Bytes const& wasmCode, WasmExternVec const& imports = {});
FuncInfo
[[nodiscard]] FuncInfo
getFunc(std::string_view funcName) const;
static std::vector<wasm_val_t>
@@ -328,9 +328,9 @@ private:
compareParamTypes(wasm_valtype_vec_t const* ftp, std::vector<wasm_val_t> const& p);
static void
add_param(std::vector<wasm_val_t>& in, int32_t p);
addParam(std::vector<wasm_val_t>& in, int32_t p);
static void
add_param(std::vector<wasm_val_t>& in, int64_t p);
addParam(std::vector<wasm_val_t>& in, int64_t p);
template <int NR, class... Types>
inline WasmiResult

View File

@@ -1,7 +1,15 @@
#include <xrpl/protocol/STBitString.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/tx/wasm/HostFuncImpl.h>
#include <xrpl/basics/Expected.h>
#include <xrpl/basics/Slice.h>
#include <xrpl/protocol/Protocol.h>
#include <xrpl/protocol/PublicKey.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/tx/wasm/HostFunc.h>
#include <xrpl/tx/wasm/ParamsHelper.h>
#include <cstdint>
namespace xrpl {
// =========================================================
@@ -13,7 +21,7 @@ WasmHostFunctionsImpl::updateData(Slice const& data)
{
if (data.size() > maxWasmDataLength)
{
return Unexpected(HostFunctionError::DATA_FIELD_TOO_LARGE);
return Unexpected(HostFunctionError::DataFieldTooLarge);
}
data_ = Bytes(data.begin(), data.end());
return data_->size();
@@ -30,7 +38,7 @@ WasmHostFunctionsImpl::checkSignature(
Slice const& pubkey) const
{
if (!publicKeyType(pubkey))
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
PublicKey const pk(pubkey);
return verify(pk, message, signature);

View File

@@ -1,8 +1,19 @@
#include <xrpl/basics/Expected.h>
#include <xrpl/basics/Number.h>
#include <xrpl/basics/Slice.h>
#include <xrpl/protocol/SField.h>
#include <xrpl/protocol/STBitString.h>
#include <xrpl/protocol/STNumber.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/protocol/Serializer.h>
#include <xrpl/tx/wasm/HostFunc.h>
#include <xrpl/tx/wasm/HostFuncImpl.h>
#include <xrpl/tx/wasm/ParamsHelper.h>
#include <boost/algorithm/hex.hpp>
#include <cstdint>
#include <iterator>
#include <string>
#include <utility>
#ifdef _DEBUG
// #define DEBUG_OUTPUT 1
@@ -48,7 +59,7 @@ public:
{
try
{
Number n;
Number const n;
if constexpr (std::is_signed_v<T>)
{
n = Number(static_cast<int64_t>(mantissa), exponent);
@@ -102,25 +113,25 @@ public:
struct FloatState
{
Number::rounding_mode oldMode_;
bool good_ = false;
Number::RoundingMode oldMode;
bool good = false;
FloatState(int32_t mode) : oldMode_(Number::getround())
FloatState(int32_t mode) : oldMode(Number::getround())
{
if (mode < Number::rounding_mode::to_nearest || mode > Number::rounding_mode::upward)
if (mode < Number::RoundingMode::ToNearest || mode > Number::RoundingMode::Upward)
return;
Number::setround(static_cast<Number::rounding_mode>(mode));
good_ = true;
Number::setround(static_cast<Number::RoundingMode>(mode));
good = true;
}
~FloatState()
{
Number::setround(oldMode_);
Number::setround(oldMode);
}
operator bool() const
{
return good_;
return good;
}
};
@@ -130,7 +141,7 @@ std::string
floatToString(Slice const& data)
{
// set default mode as we don't expect it will be used here
detail::FloatState const rm(Number::rounding_mode::to_nearest);
detail::FloatState const rm(Number::RoundingMode::to_nearest);
detail::WasmNumber const num(data);
if (!num)
{
@@ -150,18 +161,18 @@ floatFromIntImpl(int64_t x, int32_t mode)
{
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const num(x);
if (!num)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); // LCOV_EXCL_LINE
return Unexpected(HostFunctionError::FloatInputMalformed); // LCOV_EXCL_LINE
auto const r = num.toBytes();
return r;
}
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}
@@ -173,18 +184,18 @@ floatFromUintImpl(uint64_t x, int32_t mode)
{
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const num(x);
if (!num)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); // LCOV_EXCL_LINE
return Unexpected(HostFunctionError::FloatInputMalformed); // LCOV_EXCL_LINE
auto const r = num.toBytes();
return r;
}
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}
@@ -196,18 +207,18 @@ floatFromSTAmountImpl(STAmount const& x, int32_t mode)
{
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const num(static_cast<Number>(x));
if (!num)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); // LCOV_EXCL_LINE
return Unexpected(HostFunctionError::FloatInputMalformed); // LCOV_EXCL_LINE
auto const r = num.toBytes();
return r;
}
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}
@@ -219,18 +230,18 @@ floatFromSTNumberImpl(STNumber const& x, int32_t mode)
{
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const num(x.value());
if (!num)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); // LCOV_EXCL_LINE
return Unexpected(HostFunctionError::FloatInputMalformed); // LCOV_EXCL_LINE
auto const r = num.toBytes();
return r;
}
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}
@@ -242,18 +253,18 @@ floatToIntImpl(Slice const& x, int32_t mode)
{
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const num(x);
if (!num)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); // LCOV_EXCL_LINE
return Unexpected(HostFunctionError::FloatInputMalformed); // LCOV_EXCL_LINE
int64_t const r(num);
return r;
}
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}
@@ -263,20 +274,20 @@ floatToMantExpImpl(Slice const& x)
{
try
{
detail::FloatState const rm(Number::rounding_mode::to_nearest);
detail::FloatState const rm(Number::RoundingMode::to_nearest);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const num(x);
if (!num)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); // LCOV_EXCL_LINE
return Unexpected(HostFunctionError::FloatInputMalformed); // LCOV_EXCL_LINE
return FloatPair(num.mantissa(), num.exponent());
}
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}
@@ -288,15 +299,15 @@ floatFromMantExpImpl(int64_t mantissa, int32_t exponent, int32_t mode)
{
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const num(mantissa, exponent);
if (!num)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
return num.toBytes();
}
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
}
@@ -306,14 +317,14 @@ floatCompareImpl(Slice const& x, Slice const& y)
try
{
// set default mode as we don't expect it will be used here
detail::FloatState const rm(Number::rounding_mode::to_nearest);
detail::FloatState const rm(Number::RoundingMode::to_nearest);
detail::WasmNumber const xx(x);
if (!xx)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const yy(y);
if (!yy)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
if (xx < yy)
return 2;
if (xx == yy)
@@ -323,7 +334,7 @@ floatCompareImpl(Slice const& x, Slice const& y)
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}
@@ -335,14 +346,14 @@ floatAddImpl(Slice const& x, Slice const& y, int32_t mode)
{
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const xx(x);
if (!xx)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const yy(y);
if (!yy)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const res = xx + yy;
return res.toBytes();
@@ -350,7 +361,7 @@ floatAddImpl(Slice const& x, Slice const& y, int32_t mode)
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}
@@ -362,13 +373,13 @@ floatSubtractImpl(Slice const& x, Slice const& y, int32_t mode)
{
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const xx(x);
if (!xx)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const yy(y);
if (!yy)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const res = xx - yy;
return res.toBytes();
@@ -376,7 +387,7 @@ floatSubtractImpl(Slice const& x, Slice const& y, int32_t mode)
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}
@@ -388,13 +399,13 @@ floatMultiplyImpl(Slice const& x, Slice const& y, int32_t mode)
{
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const xx(x);
if (!xx)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const yy(y);
if (!yy)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const res = xx * yy;
return res.toBytes();
@@ -402,7 +413,7 @@ floatMultiplyImpl(Slice const& x, Slice const& y, int32_t mode)
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}
@@ -414,20 +425,20 @@ floatDivideImpl(Slice const& x, Slice const& y, int32_t mode)
{
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const xx(x);
if (!xx)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const yy(y);
if (!yy)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const res = xx / yy;
return res.toBytes();
}
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
}
@@ -437,15 +448,15 @@ floatRootImpl(Slice const& x, int32_t n, int32_t mode)
try
{
if (n < 1)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const xx(x);
if (!xx)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const res(root(xx, n));
@@ -454,7 +465,7 @@ floatRootImpl(Slice const& x, int32_t n, int32_t mode)
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}
@@ -464,18 +475,18 @@ floatPowerImpl(Slice const& x, int32_t n, int32_t mode)
{
try
{
if ((n < 0) || (n > Number::maxExponent))
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
if ((n < 0) || (n > Number::kMaxExponent))
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::FloatState const rm(mode);
if (!rm)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
detail::WasmNumber const xx(x);
if (!xx)
return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED);
return Unexpected(HostFunctionError::FloatInputMalformed);
if (xx == Number() && (n == 0))
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
detail::WasmNumber const res(power(xx, n, 1));
@@ -484,7 +495,7 @@ floatPowerImpl(Slice const& x, int32_t n, int32_t mode)
// LCOV_EXCL_START
catch (...)
{
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
return Unexpected(HostFunctionError::FloatComputationError);
}
// LCOV_EXCL_STOP
}

View File

@@ -1,10 +1,27 @@
#include <xrpl/basics/Expected.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/MPTIssue.h>
#include <xrpl/protocol/Protocol.h>
#include <xrpl/protocol/SField.h>
#include <xrpl/protocol/STBase.h>
#include <xrpl/protocol/STBitString.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/protocol/STObject.h>
#include <xrpl/protocol/Serializer.h>
#include <xrpl/tx/wasm/HostFunc.h>
#include <xrpl/tx/wasm/HostFuncImpl.h>
#include <xrpl/tx/wasm/ParamsHelper.h>
#include <cstdint>
#include <cstring>
#include <utility>
#include <variant>
namespace xrpl {
typedef std::variant<STBase const*, uint256 const*> FieldValue;
using FieldValue = std::variant<STBase const*, uint256 const*>;
template <class T>
Bytes
@@ -23,7 +40,7 @@ static Expected<Bytes, HostFunctionError>
getAnyFieldData(STBase const* obj)
{
if (obj == nullptr)
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
return Unexpected(HostFunctionError::FieldNotFound);
auto const stype = obj->getSType();
switch (stype)
@@ -31,13 +48,13 @@ getAnyFieldData(STBase const* obj)
// LCOV_EXCL_START
case STI_UNKNOWN:
case STI_NOTPRESENT:
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
return Unexpected(HostFunctionError::FieldNotFound);
// LCOV_EXCL_STOP
case STI_OBJECT:
case STI_ARRAY:
case STI_VECTOR256:
return Unexpected(HostFunctionError::NOT_LEAF_FIELD);
return Unexpected(HostFunctionError::NotLeafField);
case STI_ACCOUNT: {
auto const* account(static_cast<STAccount const*>(obj)); // NOLINT
@@ -125,7 +142,7 @@ static Expected<FieldValue, HostFunctionError>
locateField(STObject const& obj, Slice const& locator)
{
if (locator.empty() || ((locator.size() & 3) != 0u)) // must be multiple of 4
return Unexpected(HostFunctionError::LOCATOR_MALFORMED);
return Unexpected(HostFunctionError::LocatorMalformed);
static_assert(maxWasmParamLength % sizeof(int32_t) == 0);
int32_t locBuf[maxWasmParamLength / sizeof(int32_t)];
@@ -151,12 +168,12 @@ locateField(STObject const& obj, Slice const& locator)
int32_t const sfieldCode = adjustWasmEndianess(locPtr[0]);
auto const it = knownSFields.find(sfieldCode);
if (it == knownSFields.end())
return Unexpected(HostFunctionError::INVALID_FIELD);
return Unexpected(HostFunctionError::InvalidField);
auto const& fname(*it->second);
field = obj.peekAtPField(fname);
if (noField(field))
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
return Unexpected(HostFunctionError::FieldNotFound);
}
for (int i = 1; i < locSize; ++i)
@@ -167,7 +184,7 @@ locateField(STObject const& obj, Slice const& locator)
{
auto const* arr = static_cast<STArray const*>(field); // NOLINT
if (sfieldCode < 0 || std::cmp_greater_equal(sfieldCode, arr->size()))
return Unexpected(HostFunctionError::INDEX_OUT_OF_BOUNDS);
return Unexpected(HostFunctionError::IndexOutOfBounds);
field = &(arr->operator[](sfieldCode));
}
else if (STI_OBJECT == field->getSType())
@@ -176,7 +193,7 @@ locateField(STObject const& obj, Slice const& locator)
auto const it = knownSFields.find(sfieldCode);
if (it == knownSFields.end())
return Unexpected(HostFunctionError::INVALID_FIELD);
return Unexpected(HostFunctionError::InvalidField);
auto const& fname(*it->second);
field = o->peekAtPField(fname);
@@ -185,16 +202,16 @@ locateField(STObject const& obj, Slice const& locator)
{
auto const* v = static_cast<STVector256 const*>(field); // NOLINT
if (sfieldCode < 0 || std::cmp_greater_equal(sfieldCode, v->size()))
return Unexpected(HostFunctionError::INDEX_OUT_OF_BOUNDS);
return Unexpected(HostFunctionError::IndexOutOfBounds);
return FieldValue(&(v->operator[](sfieldCode)));
}
else // simple field must be the last one
{
return Unexpected(HostFunctionError::LOCATOR_MALFORMED);
return Unexpected(HostFunctionError::LocatorMalformed);
}
if (noField(field))
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
return Unexpected(HostFunctionError::FieldNotFound);
}
return FieldValue(field);
@@ -212,19 +229,19 @@ getArrayLen(FieldValue const& variantField)
}
// uint256 is not an array so that variant should still return NO_ARRAY
return Unexpected(HostFunctionError::NO_ARRAY); // LCOV_EXCL_LINE
return Unexpected(HostFunctionError::NoArray); // LCOV_EXCL_LINE
}
Expected<int32_t, HostFunctionError>
WasmHostFunctionsImpl::cacheLedgerObj(uint256 const& objId, int32_t cacheIdx)
{
auto const& keylet = keylet::unchecked(objId);
if (cacheIdx < 0 || cacheIdx > MAX_CACHE)
return Unexpected(HostFunctionError::SLOT_OUT_RANGE);
if (cacheIdx < 0 || cacheIdx > maxCache)
return Unexpected(HostFunctionError::SlotOutRange);
if (cacheIdx == 0)
{
for (cacheIdx = 0; cacheIdx < MAX_CACHE; ++cacheIdx)
for (cacheIdx = 0; cacheIdx < maxCache; ++cacheIdx)
{
if (!cache_[cacheIdx])
break;
@@ -235,12 +252,12 @@ WasmHostFunctionsImpl::cacheLedgerObj(uint256 const& objId, int32_t cacheIdx)
cacheIdx--; // convert to 0-based index
}
if (cacheIdx >= MAX_CACHE)
return Unexpected(HostFunctionError::SLOTS_FULL);
if (cacheIdx >= maxCache)
return Unexpected(HostFunctionError::SlotsFull);
cache_[cacheIdx] = ctx_.view().read(keylet);
if (!cache_[cacheIdx])
return Unexpected(HostFunctionError::LEDGER_OBJ_NOT_FOUND);
return Unexpected(HostFunctionError::LedgerObjNotFound);
return cacheIdx + 1; // return 1-based index
}
@@ -316,11 +333,11 @@ Expected<int32_t, HostFunctionError>
WasmHostFunctionsImpl::getTxArrayLen(SField const& fname) const
{
if (fname.fieldType != STI_ARRAY && fname.fieldType != STI_VECTOR256)
return Unexpected(HostFunctionError::NO_ARRAY);
return Unexpected(HostFunctionError::NoArray);
auto const* field = ctx_.tx.peekAtPField(fname);
if (noField(field))
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
return Unexpected(HostFunctionError::FieldNotFound);
return getArrayLen(field);
}
@@ -329,7 +346,7 @@ Expected<int32_t, HostFunctionError>
WasmHostFunctionsImpl::getCurrentLedgerObjArrayLen(SField const& fname) const
{
if (fname.fieldType != STI_ARRAY && fname.fieldType != STI_VECTOR256)
return Unexpected(HostFunctionError::NO_ARRAY);
return Unexpected(HostFunctionError::NoArray);
auto const sle = getCurrentLedgerObj();
if (!sle.has_value())
@@ -337,7 +354,7 @@ WasmHostFunctionsImpl::getCurrentLedgerObjArrayLen(SField const& fname) const
auto const* field = sle.value()->peekAtPField(fname);
if (noField(field))
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
return Unexpected(HostFunctionError::FieldNotFound);
return getArrayLen(field);
}
@@ -346,7 +363,7 @@ Expected<int32_t, HostFunctionError>
WasmHostFunctionsImpl::getLedgerObjArrayLen(int32_t cacheIdx, SField const& fname) const
{
if (fname.fieldType != STI_ARRAY && fname.fieldType != STI_VECTOR256)
return Unexpected(HostFunctionError::NO_ARRAY);
return Unexpected(HostFunctionError::NoArray);
auto const normalizedIdx = normalizeCacheIndex(cacheIdx);
if (!normalizedIdx.has_value())
@@ -354,7 +371,7 @@ WasmHostFunctionsImpl::getLedgerObjArrayLen(int32_t cacheIdx, SField const& fnam
auto const* field = cache_[normalizedIdx.value()]->peekAtPField(fname);
if (noField(field))
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
return Unexpected(HostFunctionError::FieldNotFound);
return getArrayLen(field);
}

View File

@@ -1,6 +1,17 @@
#include <xrpl/protocol/STBitString.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/basics/Expected.h>
#include <xrpl/basics/Slice.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/MPTIssue.h>
#include <xrpl/protocol/Protocol.h>
#include <xrpl/protocol/UintTypes.h>
#include <xrpl/protocol/jss.h>
#include <xrpl/tx/wasm/HostFunc.h>
#include <xrpl/tx/wasm/HostFuncImpl.h>
#include <xrpl/tx/wasm/ParamsHelper.h>
#include <cstdint>
namespace xrpl {
@@ -8,7 +19,7 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::accountKeylet(AccountID const& account) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::account(account);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -17,11 +28,11 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::ammKeylet(Asset const& issue1, Asset const& issue2) const
{
if (issue1 == issue2)
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
// note: this should be removed with the MPT DEX amendment
if (issue1.holds<MPTIssue>() || issue2.holds<MPTIssue>())
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
auto const keylet = keylet::amm(issue1, issue2);
return Bytes{keylet.key.begin(), keylet.key.end()};
@@ -31,7 +42,7 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::checkKeylet(AccountID const& account, std::uint32_t seq) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::check(account, seq);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -43,10 +54,10 @@ WasmHostFunctionsImpl::credentialKeylet(
Slice const& credentialType) const
{
if (!subject || !issuer)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
if (credentialType.empty() || credentialType.size() > maxCredentialTypeLength)
return Unexpected(HostFunctionError::INVALID_PARAMS);
if (credentialType.empty() || credentialType.size() > kMaxCredentialTypeLength)
return Unexpected(HostFunctionError::InvalidParams);
auto const keylet = keylet::credential(subject, issuer, credentialType);
@@ -57,7 +68,7 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::didKeylet(AccountID const& account) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::did(account);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -66,9 +77,9 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::delegateKeylet(AccountID const& account, AccountID const& authorize) const
{
if (!account || !authorize)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
if (account == authorize)
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
auto const keylet = keylet::delegate(account, authorize);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -78,9 +89,9 @@ WasmHostFunctionsImpl::depositPreauthKeylet(AccountID const& account, AccountID
const
{
if (!account || !authorize)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
if (account == authorize)
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
auto const keylet = keylet::depositPreauth(account, authorize);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -89,7 +100,7 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::escrowKeylet(AccountID const& account, std::uint32_t seq) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::escrow(account, seq);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -101,11 +112,11 @@ WasmHostFunctionsImpl::lineKeylet(
Currency const& currency) const
{
if (!account1 || !account2)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
if (account1 == account2)
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
if (currency.isZero())
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
auto const keylet = keylet::line(account1, account2, currency);
return Bytes{keylet.key.begin(), keylet.key.end()};
@@ -115,7 +126,7 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::mptIssuanceKeylet(AccountID const& issuer, std::uint32_t seq) const
{
if (!issuer)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::mptIssuance(seq, issuer);
return Bytes{keylet.key.begin(), keylet.key.end()};
@@ -125,9 +136,9 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::mptokenKeylet(MPTID const& mptid, AccountID const& holder) const
{
if (!mptid)
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
if (!holder)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::mptoken(mptid, holder);
return Bytes{keylet.key.begin(), keylet.key.end()};
@@ -137,7 +148,7 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::nftOfferKeylet(AccountID const& account, std::uint32_t seq) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::nftoffer(account, seq);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -146,7 +157,7 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::offerKeylet(AccountID const& account, std::uint32_t seq) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::offer(account, seq);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -155,7 +166,7 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::oracleKeylet(AccountID const& account, std::uint32_t documentId) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::oracle(account, documentId);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -167,9 +178,9 @@ WasmHostFunctionsImpl::paychanKeylet(
std::uint32_t seq) const
{
if (!account || !destination)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
if (account == destination)
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
auto const keylet = keylet::payChan(account, destination, seq);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -178,7 +189,7 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::permissionedDomainKeylet(AccountID const& account, std::uint32_t seq) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::permissionedDomain(account, seq);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -187,7 +198,7 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::signersKeylet(AccountID const& account) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::signers(account);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -196,8 +207,8 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::ticketKeylet(AccountID const& account, std::uint32_t seq) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
auto const keylet = keylet::ticket(account, seq);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = jss::ticket(account, seq);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -205,7 +216,7 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::vaultKeylet(AccountID const& account, std::uint32_t seq) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::vault(account, seq);
return Bytes{keylet.key.begin(), keylet.key.end()};
}

View File

@@ -1,6 +1,13 @@
#include <xrpl/basics/Expected.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/ledger/AmendmentTable.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/tx/wasm/HostFunc.h>
#include <xrpl/tx/wasm/HostFuncImpl.h>
#include <xrpl/tx/wasm/ParamsHelper.h>
#include <cstdint>
#include <string>
#include <string_view>
namespace xrpl {

View File

@@ -1,7 +1,15 @@
#include <xrpl/basics/Expected.h>
#include <xrpl/basics/Slice.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/ledger/helpers/NFTokenHelpers.h>
#include <xrpl/protocol/STBitString.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/SField.h>
#include <xrpl/protocol/nft.h>
#include <xrpl/tx/wasm/HostFunc.h>
#include <xrpl/tx/wasm/HostFuncImpl.h>
#include <xrpl/tx/wasm/ParamsHelper.h>
#include <cstdint>
namespace xrpl {
@@ -13,18 +21,18 @@ Expected<Bytes, HostFunctionError>
WasmHostFunctionsImpl::getNFT(AccountID const& account, uint256 const& nftId) const
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
if (!nftId)
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
auto obj = nft::findToken(ctx_.view(), account, nftId);
if (!obj)
return Unexpected(HostFunctionError::LEDGER_OBJ_NOT_FOUND);
return Unexpected(HostFunctionError::LedgerObjNotFound);
auto objUri = obj->at(~sfURI);
if (!objUri)
return Unexpected(HostFunctionError::FIELD_NOT_FOUND);
return Unexpected(HostFunctionError::FieldNotFound);
Slice const s = objUri->value();
return Bytes(s.begin(), s.end());
@@ -35,7 +43,7 @@ WasmHostFunctionsImpl::getNFTIssuer(uint256 const& nftId) const
{
auto const issuer = nft::getIssuer(nftId);
if (!issuer)
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
return Bytes{issuer.begin(), issuer.end()};
}

View File

@@ -1,7 +1,17 @@
#include <xrpl/protocol/STBitString.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/basics/Expected.h>
#include <xrpl/basics/Slice.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/STAmount.h>
#include <xrpl/tx/wasm/HostFunc.h>
#include <xrpl/tx/wasm/HostFuncImpl.h>
#include <boost/algorithm/hex.hpp>
#include <cstdint>
#include <iterator>
#include <string>
#include <string_view>
#ifdef _DEBUG
// #define DEBUG_OUTPUT 1
#endif

View File

@@ -1,15 +1,37 @@
#include <xrpl/tx/wasm/HostFuncWrapper.h>
#include <xrpl/basics/Expected.h>
#include <xrpl/basics/Slice.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/Issue.h>
#include <xrpl/protocol/KeyType.h>
#include <xrpl/protocol/Protocol.h>
#include <xrpl/protocol/PublicKey.h>
#include <xrpl/protocol/SField.h>
#include <xrpl/protocol/STAmount.h>
#include <xrpl/protocol/STNumber.h>
#include <xrpl/protocol/SecretKey.h>
#include <xrpl/protocol/Seed.h>
#include <xrpl/protocol/Serializer.h>
#include <xrpl/protocol/UintTypes.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/tx/wasm/HostFunc.h>
#include <xrpl/tx/wasm/HostFuncWrapper.h>
#include <xrpl/tx/wasm/ParamsHelper.h>
#include <xrpl/tx/wasm/WasmVM.h>
#include <wasm.h>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <exception>
#include <functional>
#include <optional>
#include <string_view>
#include <type_traits>
#include <utility>
namespace xrpl {
@@ -28,21 +50,21 @@ setData(
return 0; // LCOV_EXCL_LINE
if (dst < 0 || dstSize < 0 || (src == nullptr) || srcSize < 0)
return HfErrorToInt(HostFunctionError::INVALID_PARAMS);
return HfErrorToInt(HostFunctionError::InvalidParams);
if (srcSize > maxWasmDataLength)
return HfErrorToInt(HostFunctionError::DATA_FIELD_TOO_LARGE);
return HfErrorToInt(HostFunctionError::DataFieldTooLarge);
auto const memory = (runtime != nullptr) ? runtime->getMem() : wmem();
auto const memory = (runtime != nullptr) ? runtime->getMem() : Wmem();
// LCOV_EXCL_START
if (memory.s == 0u)
return HfErrorToInt(HostFunctionError::NO_MEM_EXPORTED);
return HfErrorToInt(HostFunctionError::NoMemExported);
// LCOV_EXCL_STOP
if ((int64_t)dst + dstSize > memory.s)
return HfErrorToInt(HostFunctionError::POINTER_OUT_OF_BOUNDS);
return HfErrorToInt(HostFunctionError::PointerOutOfBounds);
if (srcSize > dstSize)
return HfErrorToInt(HostFunctionError::BUFFER_TOO_SMALL);
return HfErrorToInt(HostFunctionError::BufferTooSmall);
memcpy(memory.p + dst, src, srcSize);
@@ -76,7 +98,7 @@ getDataUnsigned(IW* runtime, wasm_val_vec_t const* params, int32_t& i)
if (!r)
return Unexpected(r.error());
if (r->size() != sizeof(T))
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
T x;
uintptr_t const p = reinterpret_cast<uintptr_t>(r->data());
@@ -116,7 +138,7 @@ getDataSField(IW* runtime, wasm_val_vec_t const* params, int32_t& i)
i++;
if (it == m.end())
{
return Unexpected(HostFunctionError::INVALID_FIELD);
return Unexpected(HostFunctionError::InvalidField);
}
return *it->second;
}
@@ -129,22 +151,22 @@ getDataSlice(IW* runtime, wasm_val_vec_t const* params, int32_t& i, bool isUpdat
int64_t const size = params->data[i + 1].of.i32;
i += 2;
if (ptr < 0 || size < 0)
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
if (!size)
return Slice();
if (size > (isUpdate ? maxWasmDataLength : maxWasmParamLength))
return Unexpected(HostFunctionError::DATA_FIELD_TOO_LARGE);
return Unexpected(HostFunctionError::DataFieldTooLarge);
auto const memory = runtime ? runtime->getMem() : wmem();
auto const memory = runtime ? runtime->getMem() : Wmem();
// LCOV_EXCL_START
if (!memory.s)
return Unexpected(HostFunctionError::NO_MEM_EXPORTED);
return Unexpected(HostFunctionError::NoMemExported);
// LCOV_EXCL_STOP
if (ptr + size > memory.s)
return Unexpected(HostFunctionError::POINTER_OUT_OF_BOUNDS);
return Unexpected(HostFunctionError::PointerOutOfBounds);
Slice data(memory.p + ptr, size);
return data;
@@ -160,9 +182,9 @@ getDataUInt256(IW* runtime, wasm_val_vec_t const* params, int32_t& i)
return Unexpected(slice.error());
}
if (slice->size() != uint256::bytes)
if (slice->size() != Bytes)
{
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
}
return uint256::fromVoid(slice->data());
}
@@ -177,9 +199,9 @@ getDataAccountID(IW* runtime, wasm_val_vec_t const* params, int32_t& i)
return Unexpected(slice.error());
}
if (slice->size() != AccountID::bytes)
if (slice->size() != Bytes)
{
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
}
return AccountID::fromVoid(slice->data());
@@ -195,9 +217,9 @@ getDataCurrency(IW* runtime, wasm_val_vec_t const* params, int32_t& i)
return Unexpected(slice.error());
}
if (slice->size() != Currency::bytes)
if (slice->size() != Bytes)
{
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
}
return Currency::fromVoid(slice->data());
@@ -213,33 +235,32 @@ getDataAsset(IW* runtime, wasm_val_vec_t const* params, int32_t& i)
return Unexpected(slice.error());
}
if (slice->size() == MPTID::bytes)
if (slice->size() == Bytes)
{
auto const mptid = MPTID::fromVoid(slice->data());
return Asset{mptid};
}
if (slice->size() == Currency::bytes)
if (slice->size() == Bytes)
{
auto const currency = Currency::fromVoid(slice->data());
auto const issue = Issue{currency, xrpAccount()};
if (!issue.native())
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
return Asset{issue};
}
if (slice->size() == (AccountID::bytes + Currency::bytes))
if (slice->size() == (Bytes + Bytes))
{
auto const issue = Issue(
Currency::fromVoid(slice->data()),
AccountID::fromVoid(slice->data() + Currency::bytes));
auto const issue =
Issue(Currency::fromVoid(slice->data()), AccountID::fromVoid(slice->data() + Bytes));
if (issue.native())
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
return Asset{issue};
}
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
}
template <class IW>
@@ -486,7 +507,7 @@ isAmendmentEnabled_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t*
return hfResult(results, slice.error());
}
if (slice->size() == uint256::bytes)
if (slice->size() == Bytes)
{
if (auto const ret = hf->isAmendmentEnabled(uint256::fromVoid(slice->data()));
ret && *ret == 1)
@@ -496,7 +517,7 @@ isAmendmentEnabled_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t*
if (slice->size() > 64)
{
return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE);
return hfResult(results, HostFunctionError::DataFieldTooLarge);
}
auto const str = std::string_view(reinterpret_cast<char const*>(slice->data()), slice->size());
@@ -1104,9 +1125,9 @@ mptokenKeylet_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* resu
return hfResult(results, slice.error());
}
if (slice->size() != MPTID::bytes)
if (slice->size() != Bytes)
{
return hfResult(results, HostFunctionError::INVALID_PARAMS);
return hfResult(results, HostFunctionError::InvalidParams);
}
auto const mptid = MPTID::fromVoid(slice->data());
@@ -1440,12 +1461,12 @@ trace_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
int64_t const len = (int64_t)params->data[1].of.i32 + params->data[3].of.i32;
if (len < 0)
{
return hfResult(results, HostFunctionError::INVALID_PARAMS);
return hfResult(results, HostFunctionError::InvalidParams);
}
if (std::cmp_greater(len, maxWasmParamLength))
{
return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE);
return hfResult(results, HostFunctionError::DataFieldTooLarge);
}
auto const msg = getDataString(runtime, params, index);
@@ -1468,7 +1489,7 @@ trace_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
if (*asHex != 0 && *asHex != 1)
{
return hfResult(results, HostFunctionError::INVALID_PARAMS);
return hfResult(results, HostFunctionError::InvalidParams);
}
return returnResult(runtime, params, results, hf->trace(*msg, *data, *asHex != 0), index);
@@ -1486,12 +1507,12 @@ traceNum_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
int32_t const len = params->data[1].of.i32;
if (len < 0)
{
return hfResult(results, HostFunctionError::INVALID_PARAMS);
return hfResult(results, HostFunctionError::InvalidParams);
}
if (std::cmp_greater(len, maxWasmParamLength))
{
return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE);
return hfResult(results, HostFunctionError::DataFieldTooLarge);
}
auto const msg = getDataString(runtime, params, index);
@@ -1520,12 +1541,12 @@ traceAccount_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* resul
int32_t const len = params->data[1].of.i32;
if (len < 0)
{
return hfResult(results, HostFunctionError::INVALID_PARAMS);
return hfResult(results, HostFunctionError::InvalidParams);
}
if (std::cmp_greater(len, maxWasmParamLength))
{
return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE);
return hfResult(results, HostFunctionError::DataFieldTooLarge);
}
int i = 0;
@@ -1555,12 +1576,12 @@ traceFloat_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results
int32_t const len = params->data[1].of.i32;
if (len < 0)
{
return hfResult(results, HostFunctionError::INVALID_PARAMS);
return hfResult(results, HostFunctionError::InvalidParams);
}
if (std::cmp_greater(len, maxWasmParamLength))
{
return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE);
return hfResult(results, HostFunctionError::DataFieldTooLarge);
}
int i = 0;
@@ -1590,12 +1611,12 @@ traceAmount_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* result
int32_t const len = params->data[1].of.i32;
if (len < 0)
{
return hfResult(results, HostFunctionError::INVALID_PARAMS);
return hfResult(results, HostFunctionError::InvalidParams);
}
if (std::cmp_greater(len, maxWasmParamLength))
{
return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE);
return hfResult(results, HostFunctionError::DataFieldTooLarge);
}
int i = 0;
@@ -1617,7 +1638,7 @@ traceAmount_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* result
std::optional<STAmount> amount;
try
{
amount = STAmount(serialIter, sfGeneric);
amount = STAmount(serialIter, kSfGeneric);
}
catch (std::exception const&)
{
@@ -1626,7 +1647,7 @@ traceAmount_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* result
if (!amount)
{
return hfResult(results, HostFunctionError::INVALID_PARAMS);
return hfResult(results, HostFunctionError::InvalidParams);
}
return returnResult(runtime, params, results, hf->traceAmount(*msg, *amount), i);
@@ -1693,14 +1714,14 @@ floatFromSTAmount_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t*
std::optional<STAmount> amount;
try
{
amount = STAmount(serialIter, sfGeneric);
amount = STAmount(serialIter, kSfGeneric);
}
catch (std::exception const&)
{
amount = std::nullopt;
}
if (!amount)
return hfResult(results, HostFunctionError::INVALID_PARAMS);
return hfResult(results, HostFunctionError::InvalidParams);
i = 4;
auto const rounding = getDataInt32(runtime, params, i);
@@ -1728,14 +1749,14 @@ floatFromSTNumber_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t*
std::optional<STNumber> num;
try
{
num = STNumber(serialIter, sfGeneric);
num = STNumber(serialIter, kSfGeneric);
}
catch (std::exception const&)
{
num = std::nullopt;
}
if (!num)
return hfResult(results, HostFunctionError::INVALID_PARAMS);
return hfResult(results, HostFunctionError::InvalidParams);
i = 4;
auto const rounding = getDataInt32(runtime, params, i);
@@ -1993,15 +2014,15 @@ namespace test {
class MockWasmRuntimeWrapper
{
wmem mem_;
Wmem mem_;
public:
MockWasmRuntimeWrapper(wmem memory) : mem_(memory)
MockWasmRuntimeWrapper(Wmem memory) : mem_(memory)
{
}
// Mock methods to simulate the behavior of WasmRuntimeWrapper
wmem
[[nodiscard]] Wmem
getMem() const
{
return mem_;
@@ -2014,7 +2035,7 @@ testGetDataIncrement()
wasm_val_t values[4];
std::array<std::uint8_t, 128> buffer = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
MockWasmRuntimeWrapper runtime(wmem{buffer.data(), buffer.size()});
MockWasmRuntimeWrapper runtime(Wmem{.p = buffer.data(), .s = buffer.size()});
{
// test int32_t
@@ -2083,7 +2104,7 @@ testGetDataIncrement()
{
// test account
AccountID const id(
calcAccountID(generateKeyPair(KeyType::secp256k1, generateSeed("alice")).first));
calcAccountID(generateKeyPair(KeyType::Secp256k1, generateSeed("alice")).first));
wasm_val_vec_t const params = {2, &values[0]};
@@ -2123,7 +2144,7 @@ testGetDataIncrement()
values[1] = WASM_I32_VAL(c.bytes);
memcpy(&buffer[0], c.data(), c.bytes);
int index = 0;
int const index = 0;
auto const result = getDataCurrency(&runtime, &params, index);
if (!result || result.value() != c || index != 2)
return false;

View File

@@ -1,13 +1,18 @@
#include <xrpl/tx/wasm/WasmVM.h>
#include <xrpl/basics/Expected.h>
#include <xrpl/beast/utility/Journal.h>
#include <xrpl/protocol/TER.h>
#include <xrpl/tx/wasm/ParamsHelper.h>
#include <cstdint>
#include <string>
#include <vector>
#ifdef _DEBUG
// #define DEBUG_OUTPUT 1
#endif
#include <xrpl/basics/Log.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/LedgerFormats.h>
#include <xrpl/tx/wasm/HostFunc.h>
#include <xrpl/tx/wasm/HostFuncWrapper.h>
#include <xrpl/tx/wasm/WasmiVM.h>
#include <memory>

View File

@@ -1,7 +1,31 @@
#include <xrpl/basics/Log.h>
#include <xrpl/tx/wasm/WasmiVM.h>
#include <xrpl/basics/Expected.h>
#include <xrpl/basics/contract.h>
#include <xrpl/beast/utility/Journal.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 <wasmi/config.h>
#include <wasmi/error.h>
#include <wasm.h>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <exception>
#include <limits>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#ifdef _DEBUG
// #define DEBUG_OUTPUT 1
@@ -13,7 +37,7 @@ namespace xrpl {
namespace {
void
print_wasm_error(std::string_view msg, wasm_trap_t* trap, beast::Journal jlog)
printWasmError(std::string_view msg, wasm_trap_t* trap, beast::Journal jlog)
{
#ifdef DEBUG_OUTPUT
auto& j = std::cerr;
@@ -22,23 +46,23 @@ print_wasm_error(std::string_view msg, wasm_trap_t* trap, beast::Journal jlog)
if (jlog.active(beast::severities::kWarning))
#endif
{
wasm_byte_vec_t error_message WASM_EMPTY_VEC;
wasm_byte_vec_t errorMessage WASM_EMPTY_VEC;
if (trap != nullptr)
wasm_trap_message(trap, &error_message);
wasm_trap_message(trap, &errorMessage);
if (error_message.size != 0u)
if (errorMessage.size != 0u)
{
j << "WASMI Error: " << msg << ", "
<< std::string_view(error_message.data, error_message.size - 1);
<< std::string_view(errorMessage.data, errorMessage.size - 1);
}
else
{
j << "WASMI Error: " << msg;
}
if (error_message.size != 0u)
wasm_byte_vec_delete(&error_message);
if (errorMessage.size != 0u)
wasm_byte_vec_delete(&errorMessage);
}
if (trap != nullptr)
@@ -54,28 +78,28 @@ print_wasm_error(std::string_view msg, wasm_trap_t* trap, beast::Journal jlog)
struct WasmiRuntimeWrapper : public WasmRuntimeWrapper
{
InstanceWrapper& iw_;
InstanceWrapper& iw;
WasmiRuntimeWrapper(InstanceWrapper& iw) : iw_(iw)
WasmiRuntimeWrapper(InstanceWrapper& iw) : iw(iw)
{
}
virtual wmem
Wmem
getMem() override
{
return iw_.getMem();
return iw.getMem();
}
virtual std::int64_t
std::int64_t
getGas() override
{
return iw_.getGas();
return iw.getGas();
}
virtual std::int64_t
std::int64_t
setGas(std::int64_t gas) override
{
return iw_.setGas(gas);
return iw.setGas(gas);
}
};
@@ -93,19 +117,19 @@ InstanceWrapper::init(
if (!mi || (trap != nullptr))
{
print_wasm_error("can't create instance", trap, j);
printWasmError("can't create instance", trap, j);
throw std::runtime_error("can't create instance");
}
wasm_instance_exports(mi.get(), expt.get());
return mi;
}
InstanceWrapper::InstanceWrapper() : instance_(nullptr, &wasm_instance_delete)
InstanceWrapper::InstanceWrapper() : instance(nullptr, &wasm_instance_delete)
{
}
// LCOV_EXCL_START
InstanceWrapper::InstanceWrapper(InstanceWrapper&& o) : instance_(nullptr, &wasm_instance_delete)
InstanceWrapper::InstanceWrapper(InstanceWrapper&& o) : instance(nullptr, &wasm_instance_delete)
{
*this = std::move(o);
}
@@ -116,7 +140,7 @@ InstanceWrapper::InstanceWrapper(
ModulePtr& m,
WasmExternVec const& imports,
beast::Journal j)
: store_(s.get()), instance_(init(s, m, exports_, imports, j)), j_(j)
: store(s.get()), instance(init(s, m, exports, imports, j)), j(j)
{
}
@@ -126,14 +150,14 @@ InstanceWrapper::operator=(InstanceWrapper&& o)
if (this == &o)
return *this; // LCOV_EXCL_LINE
store_ = o.store_;
o.store_ = nullptr;
exports_ = std::move(o.exports_);
memIdx_ = o.memIdx_;
o.memIdx_ = -1;
instance_ = std::move(o.instance_);
store = o.store;
o.store = nullptr;
exports = std::move(o.exports);
memIdx = o.memIdx;
o.memIdx = -1;
instance = std::move(o.instance);
j_ = o.j_;
j = o.j;
return *this;
}
@@ -141,7 +165,7 @@ InstanceWrapper::operator=(InstanceWrapper&& o)
InstanceWrapper::
operator bool() const
{
return static_cast<bool>(instance_);
return static_cast<bool>(instance);
}
FuncInfo
@@ -150,12 +174,12 @@ InstanceWrapper::getFunc(std::string_view funcName, WasmExporttypeVec const& exp
wasm_func_t const* f = nullptr;
wasm_functype_t const* ft = nullptr;
if (!instance_)
if (!instance)
throw std::runtime_error("no instance"); // LCOV_EXCL_LINE
if (exportTypes.empty())
throw std::runtime_error("no export"); // LCOV_EXCL_LINE
if (exportTypes.size() != exports_.size())
if (exportTypes.size() != exports.size())
throw std::runtime_error("invalid export"); // LCOV_EXCL_LINE
for (unsigned i = 0; i < exportTypes.size(); ++i)
@@ -169,7 +193,7 @@ InstanceWrapper::getFunc(std::string_view funcName, WasmExporttypeVec const& exp
if (funcName != std::string_view(name->data, name->size))
continue;
auto const* exn(exports_[i]);
auto const* exn(exports[i]);
if (wasm_extern_kind(exn) != WASM_EXTERN_FUNC)
throw std::runtime_error("invalid export"); // LCOV_EXCL_LINE
@@ -185,23 +209,25 @@ InstanceWrapper::getFunc(std::string_view funcName, WasmExporttypeVec const& exp
return {f, ft};
}
wmem
Wmem
InstanceWrapper::getMem() const
{
if (memIdx_ >= 0)
if (memIdx >= 0)
{
auto* e(exports_[memIdx_]);
auto* e(exports[memIdx]);
wasm_memory_t* mem = wasm_extern_as_memory(e);
return {reinterpret_cast<std::uint8_t*>(wasm_memory_data(mem)), wasm_memory_data_size(mem)};
return {
.p = reinterpret_cast<std::uint8_t*>(wasm_memory_data(mem)),
.s = wasm_memory_data_size(mem)};
}
wasm_memory_t* mem = nullptr;
for (int i = 0; i < exports_.size(); ++i)
for (int i = 0; i < exports.size(); ++i)
{
auto* e(exports_[i]);
auto* e(exports[i]);
if (wasm_extern_kind(e) == WASM_EXTERN_MEMORY)
{
memIdx_ = i;
memIdx = i;
mem = wasm_extern_as_memory(e);
break;
}
@@ -210,32 +236,34 @@ InstanceWrapper::getMem() const
if (mem == nullptr)
return {}; // LCOV_EXCL_LINE
return {reinterpret_cast<std::uint8_t*>(wasm_memory_data(mem)), wasm_memory_data_size(mem)};
return {
.p = reinterpret_cast<std::uint8_t*>(wasm_memory_data(mem)),
.s = wasm_memory_data_size(mem)};
}
std::int64_t
InstanceWrapper::getGas() const
{
if (store_ == nullptr)
if (store == nullptr)
return -1; // LCOV_EXCL_LINE
std::uint64_t gas = 0;
wasm_store_get_fuel(store_, &gas);
wasm_store_get_fuel(store, &gas);
return static_cast<std::int64_t>(gas);
}
std::int64_t
InstanceWrapper::setGas(std::int64_t gas) const
{
if (store_ == nullptr)
if (store == nullptr)
return -1; // LCOV_EXCL_LINE
if (gas < 0)
gas = std::numeric_limits<decltype(gas)>::max();
wasmi_error_t* err = wasm_store_set_fuel(store_, static_cast<std::uint64_t>(gas));
wasmi_error_t* err = wasm_store_set_fuel(store, static_cast<std::uint64_t>(gas));
if (err != nullptr)
{
// LCOV_EXCL_START
print_wasm_error("Can't set instance gas", nullptr, j_);
printWasmError("Can't set instance gas", nullptr, j);
wasmi_error_delete(err);
return -1;
// LCOV_EXCL_STOP
@@ -258,11 +286,11 @@ ModuleWrapper::init(StorePtr& s, Bytes const& wasmBin, beast::Journal j)
}
// LCOV_EXCL_START
ModuleWrapper::ModuleWrapper() : module_(nullptr, &wasm_module_delete)
ModuleWrapper::ModuleWrapper() : module(nullptr, &wasm_module_delete)
{
}
ModuleWrapper::ModuleWrapper(ModuleWrapper&& o) : module_(nullptr, &wasm_module_delete)
ModuleWrapper::ModuleWrapper(ModuleWrapper&& o) : module(nullptr, &wasm_module_delete)
{
*this = std::move(o);
}
@@ -274,9 +302,9 @@ ModuleWrapper::ModuleWrapper(
bool instantiate,
ImportVec const& imports,
beast::Journal j)
: module_(init(s, wasmBin, j)), j_(j)
: module(init(s, wasmBin, j)), j(j)
{
wasm_module_exports(module_.get(), exportTypes_.get());
wasm_module_exports(module.get(), exportTypes.get());
auto wimports = buildImports(s, imports);
if (instantiate)
{
@@ -291,10 +319,10 @@ ModuleWrapper::operator=(ModuleWrapper&& o)
if (this == &o)
return *this;
module_ = std::move(o.module_);
instanceWrap_ = std::move(o.instanceWrap_);
exportTypes_ = std::move(o.exportTypes_);
j_ = o.j_;
module = std::move(o.module);
instanceWrap = std::move(o.instanceWrap);
exportTypes = std::move(o.exportTypes);
j = o.j;
return *this;
}
@@ -302,7 +330,7 @@ ModuleWrapper::operator=(ModuleWrapper&& o)
ModuleWrapper::
operator bool() const
{
return instanceWrap_;
return instanceWrap;
}
// LCOV_EXCL_STOP
@@ -321,10 +349,10 @@ makeImpParams(WasmImportFunc const& imp)
auto const vt = imp.params[i];
switch (vt)
{
case WT_I32:
case WtI32:
v[i] = wasm_valtype_new_i32();
break;
case WT_I64:
case WtI64:
v[i] = wasm_valtype_new_i64();
break;
// LCOV_EXCL_START
@@ -345,11 +373,11 @@ makeImpReturn(WasmImportFunc const& imp)
WasmValtypeVec v(1);
switch (*imp.result)
{
case WT_I32:
case WtI32:
v[0] = wasm_valtype_new_i32();
break;
// LCOV_EXCL_START
case WT_I64:
case WtI64:
v[0] = wasm_valtype_new_i64();
break;
default:
@@ -363,7 +391,7 @@ WasmExternVec
ModuleWrapper::buildImports(StorePtr& s, ImportVec const& imports) const
{
WasmImporttypeVec importTypes;
wasm_module_imports(module_.get(), importTypes.get());
wasm_module_imports(module.get(), importTypes.get());
if (importTypes.empty())
return {};
@@ -396,7 +424,7 @@ ModuleWrapper::buildImports(StorePtr& s, ImportVec const& imports) const
auto const it = imports.find(fieldName);
if (it == imports.end())
{
print_wasm_error("Import not found: " + std::string(fieldName), nullptr, j_);
printWasmError("Import not found: " + std::string(fieldName), nullptr, j);
continue; // print all missed import
}
@@ -430,11 +458,11 @@ ModuleWrapper::buildImports(StorePtr& s, ImportVec const& imports) const
if (impCnt != importTypes.size())
{
print_wasm_error(
printWasmError(
std::string("Imports not finished: ") + std::to_string(impCnt) + "/" +
std::to_string(importTypes.size()),
nullptr,
j_);
j);
Throw<std::runtime_error>("Missing imports");
}
@@ -444,43 +472,43 @@ ModuleWrapper::buildImports(StorePtr& s, ImportVec const& imports) const
FuncInfo
ModuleWrapper::getFunc(std::string_view funcName) const
{
return instanceWrap_.getFunc(funcName, exportTypes_);
return instanceWrap.getFunc(funcName, exportTypes);
}
wasm_functype_t*
ModuleWrapper::getFuncType(std::string_view funcName) const
{
for (size_t i = 0; i < exportTypes_.size(); i++)
for (size_t i = 0; i < exportTypes.size(); i++)
{
auto const* exp_type(exportTypes_[i]);
wasm_name_t const* name = wasm_exporttype_name(exp_type);
wasm_externtype_t const* exn_type = wasm_exporttype_type(exp_type);
if (wasm_externtype_kind(exn_type) == WASM_EXTERN_FUNC &&
auto const* expType(exportTypes[i]);
wasm_name_t const* name = wasm_exporttype_name(expType);
wasm_externtype_t const* exnType = wasm_exporttype_type(expType);
if (wasm_externtype_kind(exnType) == WASM_EXTERN_FUNC &&
funcName == std::string_view(name->data, name->size))
{
return wasm_externtype_as_functype(const_cast<wasm_externtype_t*>(exn_type));
return wasm_externtype_as_functype(const_cast<wasm_externtype_t*>(exnType));
}
}
throw std::runtime_error("can't find function <" + std::string(funcName) + ">");
}
wmem
Wmem
ModuleWrapper::getMem() const
{
return instanceWrap_.getMem();
return instanceWrap.getMem();
}
InstanceWrapper&
ModuleWrapper::getInstance(int)
{
return instanceWrap_;
return instanceWrap;
}
int
ModuleWrapper::addInstance(StorePtr& s, WasmExternVec const& imports)
{
instanceWrap_ = {s, module_, imports, j_};
instanceWrap = {s, module, imports, j};
return 0;
}
@@ -497,7 +525,7 @@ ModuleWrapper::addInstance(StorePtr& s, WasmExternVec const& imports)
std::int64_t
ModuleWrapper::getGas() const
{
return instanceWrap_ ? instanceWrap_.getGas() : -1;
return instanceWrap ? instanceWrap.getGas() : -1;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -552,7 +580,7 @@ WasmiEngine::addModule(
{
moduleWrap_.reset();
store_.reset(); // to free the memory before creating new store
store_ = {wasm_store_new_with_memory_max_pages(engine_.get(), MAX_PAGES), &wasm_store_delete};
store_ = {wasm_store_new_with_memory_max_pages(engine_.get(), maxPages), &wasm_store_delete};
if (gas < 0)
gas = std::numeric_limits<decltype(gas)>::max();
@@ -560,7 +588,7 @@ WasmiEngine::addModule(
if (err != nullptr)
{
// LCOV_EXCL_START
print_wasm_error("Error setting gas", nullptr, j_);
printWasmError("Error setting gas", nullptr, j_);
wasmi_error_delete(err);
throw std::runtime_error("can't set gas");
// LCOV_EXCL_STOP
@@ -595,11 +623,11 @@ WasmiEngine::convertParams(std::vector<WasmParam> const& params)
{
switch (p.type)
{
case WT_I32:
case WtI32:
v.push_back(WASM_I32_VAL(p.of.i32));
break;
// LCOV_EXCL_START
case WT_I64:
case WtI64:
v.push_back(WASM_I64_VAL(p.of.i64));
break;
default:
@@ -631,7 +659,7 @@ WasmiEngine::compareParamTypes(wasm_valtype_vec_t const* ftp, std::vector<wasm_v
// LCOV_EXCL_START
void
WasmiEngine::add_param(std::vector<wasm_val_t>& in, int32_t p)
WasmiEngine::addParam(std::vector<wasm_val_t>& in, int32_t p)
{
in.emplace_back();
auto& el(in.back());
@@ -642,7 +670,7 @@ WasmiEngine::add_param(std::vector<wasm_val_t>& in, int32_t p)
// LCOV_EXCL_STOP
void
WasmiEngine::add_param(std::vector<wasm_val_t>& in, int64_t p)
WasmiEngine::addParam(std::vector<wasm_val_t>& in, int64_t p)
{
in.emplace_back();
auto& el(in.back());
@@ -700,7 +728,7 @@ WasmiEngine::call(FuncInfo const& f, std::vector<wasm_val_t>& in)
if (trap)
{
ret.f = true;
print_wasm_error("failure to call func", trap, j_);
printWasmError("failure to call func", trap, j_);
}
return ret;
@@ -710,7 +738,7 @@ template <int NR, class... Types>
WasmiResult
WasmiEngine::call(FuncInfo const& f, std::vector<wasm_val_t>& in, std::int32_t p, Types&&... args)
{
add_param(in, p);
addParam(in, p);
return call<NR>(f, in, std::forward<Types>(args)...);
}
@@ -718,7 +746,7 @@ template <int NR, class... Types>
WasmiResult
WasmiEngine::call(FuncInfo const& f, std::vector<wasm_val_t>& in, std::int64_t p, Types&&... args)
{
add_param(in, p);
addParam(in, p);
return call<NR>(f, in, std::forward<Types>(args)...);
}
@@ -759,12 +787,12 @@ WasmiEngine::run(
}
catch (std::exception const& e)
{
print_wasm_error(std::string("exception: ") + e.what(), nullptr, j);
printWasmError(std::string("exception: ") + e.what(), nullptr, j);
}
// LCOV_EXCL_START
catch (...)
{
print_wasm_error(std::string("exception: unknown"), nullptr, j);
printWasmError(std::string("exception: unknown"), nullptr, j);
}
// LCOV_EXCL_STOP
return Unexpected<TER>(tecFAILED_PROCESSING);
@@ -781,7 +809,7 @@ WasmiEngine::runHlp(
beast::Journal j)
{
// currently only 1 module support, possible parallel UT run
std::lock_guard<decltype(m_)> const lg(m_);
std::scoped_lock const lg(m_);
j_ = j;
if (wasmCode.empty())
@@ -792,7 +820,7 @@ WasmiEngine::runHlp(
// Create and instantiate the module.
[[maybe_unused]] int const m = addModule(wasmCode, true, imports, gas);
if (!moduleWrap_ || !moduleWrap_->instanceWrap_)
if (!moduleWrap_ || !moduleWrap_->instanceWrap)
throw std::runtime_error("no instance"); // LCOV_EXCL_LINE
auto clear = [](HostFunctions* p) { p->setRT(nullptr); };
@@ -833,7 +861,7 @@ WasmiEngine::runHlp(
if (gas == -1)
gas = std::numeric_limits<decltype(gas)>::max();
WasmResult<int32_t> const ret{res.r[0].of.i32, gas - moduleWrap_->getGas()};
WasmResult<int32_t> const ret{.result = res.r[0].of.i32, .cost = gas - moduleWrap_->getGas()};
// #ifdef DEBUG_OUTPUT
// auto& j = std::cerr;
@@ -861,12 +889,12 @@ WasmiEngine::check(
}
catch (std::exception const& e)
{
print_wasm_error(std::string("exception: ") + e.what(), nullptr, j);
printWasmError(std::string("exception: ") + e.what(), nullptr, j);
}
// LCOV_EXCL_START
catch (...)
{
print_wasm_error(std::string("exception: unknown"), nullptr, j);
printWasmError(std::string("exception: unknown"), nullptr, j);
}
// LCOV_EXCL_STOP
@@ -883,7 +911,7 @@ WasmiEngine::checkHlp(
beast::Journal j)
{
// currently only 1 module support, possible parallel UT run
std::lock_guard<decltype(m_)> const lg(m_);
std::scoped_lock const lg(m_);
j_ = j;
// Create and instantiate the module.
@@ -913,10 +941,10 @@ WasmiEngine::getGas() const
}
// LCOV_EXCL_STOP
wmem
Wmem
WasmiEngine::getMem() const
{
return moduleWrap_ ? moduleWrap_->getMem() : wmem();
return moduleWrap_ ? moduleWrap_->getMem() : Wmem();
}
InstanceWrapper&

File diff suppressed because it is too large Load Diff

View File

@@ -8,36 +8,34 @@
#include <xrpl/tx/wasm/HostFunc.h>
#include <xrpl/tx/wasm/WasmVM.h>
namespace xrpl {
namespace test {
namespace xrpl::test {
struct TestLedgerDataProvider : public HostFunctions
{
jtx::Env& env_;
void* rt_ = nullptr;
jtx::Env& env;
void* rt = nullptr;
public:
TestLedgerDataProvider(jtx::Env& env) : HostFunctions(env.journal), env_(env)
TestLedgerDataProvider(jtx::Env& env) : HostFunctions(env.journal), env(env)
{
}
virtual void
void
setRT(void* rt) override
{
rt_ = rt;
rt = rt;
}
virtual void*
[[nodiscard]] void*
getRT() const override
{
return rt_;
return rt;
}
Expected<std::uint32_t, HostFunctionError>
getLedgerSqn() const override
{
return env_.current()->seq();
return env.current()->seq();
}
};
@@ -46,28 +44,28 @@ struct TestHostFunctions : public HostFunctions
test::jtx::Env& env_;
AccountID accountID_;
Bytes data_;
int clock_drift_ = 0;
void* rt_ = nullptr;
int clock_drift = 0;
void* rt = nullptr;
public:
TestHostFunctions(test::jtx::Env& env, int cd = 0)
: HostFunctions(env.journal), env_(env), clock_drift_(cd)
: HostFunctions(env.journal), env_(env), clock_drift(cd)
{
accountID_ = env_.master.id();
std::string t = "10000";
data_ = Bytes{t.begin(), t.end()};
}
virtual void
void
setRT(void* rt) override
{
rt_ = rt;
rt = rt;
}
virtual void*
[[nodiscard]] void*
getRT() const override
{
return rt_;
return rt;
}
Expected<std::uint32_t, HostFunctionError>
@@ -106,7 +104,7 @@ public:
return 1;
}
virtual Expected<int32_t, HostFunctionError>
Expected<int32_t, HostFunctionError>
cacheLedgerObj(uint256 const& objId, int32_t cacheIdx) override
{
return 1;
@@ -116,8 +114,10 @@ public:
getTxField(SField const& fname) const override
{
if (fname == sfAccount)
{
return Bytes(accountID_.begin(), accountID_.end());
else if (fname == sfFee)
}
if (fname == sfFee)
{
int64_t x = 235;
uint8_t const* p = reinterpret_cast<uint8_t const*>(&x);
@@ -141,8 +141,10 @@ public:
{
auto const& sn = fname.getName();
if (sn == "Destination" || sn == "Account")
{
return Bytes(accountID_.begin(), accountID_.end());
else if (sn == "Data")
}
if (sn == "Data")
return data_;
else if (sn == "FinishAfter")
{
@@ -163,7 +165,7 @@ public:
uint8_t const* p = reinterpret_cast<uint8_t const*>(&x);
return Bytes{p, p + sizeof(x)};
}
else if (fname == sfAccount)
if (fname == sfAccount)
{
return Bytes(accountID_.begin(), accountID_.end());
}
@@ -282,7 +284,7 @@ public:
accountKeylet(AccountID const& account) const override
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::account(account);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -291,9 +293,9 @@ public:
ammKeylet(Asset const& issue1, Asset const& issue2) const override
{
if (issue1 == issue2)
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
if (issue1.holds<MPTIssue>() || issue2.holds<MPTIssue>())
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
auto const keylet = keylet::amm(issue1, issue2);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -302,7 +304,7 @@ public:
checkKeylet(AccountID const& account, std::uint32_t seq) const override
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::check(account, seq);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -312,8 +314,8 @@ public:
const override
{
if (!subject || !issuer || credentialType.empty() ||
credentialType.size() > maxCredentialTypeLength)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
credentialType.size() > kMaxCredentialTypeLength)
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::credential(subject, issuer, credentialType);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -322,7 +324,7 @@ public:
escrowKeylet(AccountID const& account, std::uint32_t seq) const override
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::escrow(account, seq);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -331,7 +333,7 @@ public:
oracleKeylet(AccountID const& account, std::uint32_t documentId) const override
{
if (!account)
return Unexpected(HostFunctionError::INVALID_ACCOUNT);
return Unexpected(HostFunctionError::InvalidAccount);
auto const keylet = keylet::oracle(account, documentId);
return Bytes{keylet.key.begin(), keylet.key.end()};
}
@@ -341,7 +343,7 @@ public:
{
if (!account || !nftId)
{
return Unexpected(HostFunctionError::INVALID_PARAMS);
return Unexpected(HostFunctionError::InvalidParams);
}
std::string s = "https://ripple.com";
@@ -476,7 +478,7 @@ public:
return wasm_float::floatToIntImpl(x, mode);
}
virtual Expected<FloatPair, HostFunctionError>
Expected<FloatPair, HostFunctionError>
floatToMantExp(Slice const& x) const override
{
return wasm_float::floatToMantExpImpl(x);
@@ -533,22 +535,21 @@ public:
struct TestHostFunctionsSink : public TestHostFunctions
{
test::StreamSink sink_;
void const* rt_ = nullptr;
test::StreamSink sink;
void const* rt = nullptr;
public:
explicit TestHostFunctionsSink(test::jtx::Env& env, int cd = 0)
: TestHostFunctions(env, cd), sink_(beast::severities::kDebug)
{
j_ = beast::Journal(sink_);
j = beast::Journal(sink);
}
test::StreamSink&
getSink()
{
return sink_;
return sink;
}
};
} // namespace test
} // namespace xrpl
} // namespace xrpl::test

View File

@@ -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

View File

@@ -2,7 +2,9 @@
#include <test/app/wasm_fixtures/fixtures.h>
extern std::string const functions5kHex =
#include <string>
extern std::string const kFunctions5kHex =
"0061736d0100000001070160027f7f017f038a27882700000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

View File

@@ -2,7 +2,9 @@
#include <test/app/wasm_fixtures/fixtures.h>
extern std::string const locals10kHex =
#include <string>
extern std::string const kLocals10kHex =
"0061736d0100000001070160027f7f017f03020100070801047465737400000a9b8a0601978a06018e4e7f20002001"
"6a2102200120026a2103200220036a2104200320046a2105200420056a2106200520066a2107200620076a21082007"
"20086a2109200820096a210a2009200a6a210b200a200b6a210c200b200c6a210d200c200d6a210e200d200e6a210f"

View File

@@ -2,12 +2,14 @@
#include <test/app/wasm_fixtures/fixtures.h>
extern std::string const fibWasmHex =
#include <string>
extern std::string const kFibWasmHex =
"0061736d0100000001090260000060017f017f0303020001071b02115f5f7761736d5f63616c6c5f63746f72730000"
"0366696200010a440202000b3f01017f200045044041000f0b2000410348044041010f0b200041026a210003402000"
"41036b100120016a2101200041026b220041044a0d000b200141016a0b";
extern std::string const ledgerSqnWasmHex =
extern std::string const kLedgerSqnWasmHex =
"0061736d01000000010e0360027f7f017f6000006000017f02160103656e760e6765745f6c65646765725f73716e00"
"0003030201020503010002063f0a7f01418088040b7f004180080b7f004180080b7f004180080b7f00418088040b7f"
"004180080b7f00418088040b7f00418080080b7f0041000b7f0041010b07aa010c066d656d6f72790200115f5f7761"
@@ -21,7 +23,7 @@ extern std::string const ledgerSqnWasmHex =
"61373930636664623432626432343732302900490f7461726765745f6665617475726573042b0f6d757461626c652d"
"676c6f62616c732b087369676e2d6578742b0f7265666572656e63652d74797065732b0a6d756c746976616c7565";
extern std::string const allHostFunctionsWasmHex =
extern std::string const kAllHostFunctionsWasmHex =
"0061736d0100000001540c60027f7f017f60037f7f7f017f60047f7f7f7f017f60017f017f60067f7f7f7f7f7f017f"
"60037f7f7f0060057f7f7f7f7f017f60037f7f7e017f60087f7f7f7f7f7f7f7f017f60017f0060027f7f006000017f"
"02ae061a08686f73745f6c69620c6765745f74785f6669656c64000108686f73745f6c69620974726163655f6e756d"
@@ -197,11 +199,11 @@ extern std::string const allHostFunctionsWasmHex =
"65737365642d6279010572757374631d312e38372e30202831373036376539616320323032352d30352d303929002c"
"0f7461726765745f6665617475726573022b0f6d757461626c652d676c6f62616c732b087369676e2d657874";
extern std::string const deepRecursionHex =
extern std::string const kDeepRecursionHex =
"0061736d010000000105016000017f030201000608017f0141c0843d0b070a010666696e69736800000a1601140023"
"0045044041010f0b230041016b240010000b";
extern std::string const allKeyletsWasmHex =
extern std::string const kAllKeyletsWasmHex =
"0061736d0100000001480960067f7f7f7f7f7f017f60047f7f7f7f017f60087f7f7f7f7f7f7f7f017f60037f7f7f01"
"7f60037f7f7e017f60057f7f7f7f7f017f60057f7f7f7f7f006000017f60037f7f7f000284051808686f73745f6c69"
"620974726163655f6e756d000408686f73745f6c6962057472616365000508686f73745f6c69621063616368655f6c"
@@ -395,7 +397,7 @@ extern std::string const allKeyletsWasmHex =
"20323032352d30352d303929002c0f7461726765745f6665617475726573022b0f6d757461626c652d676c6f62616c"
"732b087369676e2d657874";
extern std::string const codecovTestsWasmHex =
extern std::string const kCodecovTestsWasmHex =
"0061736d01000000015a0c60057f7f7f7f7f017f60037f7f7e017f60027f7f017f60037f7f7f017f60047f7f7f7f01"
"7f60017f017f60067f7f7f7f7f7f017f60087f7f7f7f7f7f7f7f017f60077f7f7f7f7f7f7f017f60047f7f7f7f0060"
"00006000017f02840d3b08686f73745f6c6962057472616365000008686f73745f6c69620974726163655f6e756d00"
@@ -822,7 +824,7 @@ extern std::string const codecovTestsWasmHex =
"6976616c75652b0f6d757461626c652d676c6f62616c732b136e6f6e7472617070696e672d6670746f696e742b0f72"
"65666572656e63652d74797065732b087369676e2d657874";
extern std::string const floatTestsWasmHex =
extern std::string const kFloatTestsWasmHex =
"0061736d0100000001490960057f7f7f7f7f017f60077f7f7f7f7f7f7f017f60067f7f7f7f7f7f017f60047e7f7f7f"
"017f60057e7f7f7f7f017f60047f7f7f7f017f60037f7f7e017f60037f7f7f006000017f02fa021008686f73745f6c"
"6962057472616365000008686f73745f6c69620e666c6f61745f66726f6d5f696e74000308686f73745f6c69620f66"
@@ -1002,7 +1004,7 @@ extern std::string const floatTestsWasmHex =
"1d312e38392e30202832393438333838336520323032352d30382d303429002c0f7461726765745f66656174757265"
"73022b0f6d757461626c652d676c6f62616c732b087369676e2d657874";
extern std::string const float0Hex =
extern std::string const kFloat0Hex =
"0061736d0100000001290560057f7f7f7f7f017f60047e7f7f7f017f60077f7f7f7f7f7f7f017f60047f7f7f7f017f"
"6000017f025f0408686f73745f6c6962057472616365000008686f73745f6c69620e666c6f61745f66726f6d5f696e"
"74000108686f73745f6c69620e666c6f61745f7375627472616374000208686f73745f6c69620d666c6f61745f636f"
@@ -1034,7 +1036,7 @@ extern std::string const float0Hex =
"626c652d676c6f62616c732b136e6f6e7472617070696e672d6670746f696e742b0f7265666572656e63652d747970"
"65732b087369676e2d657874";
extern std::string const disabledFloatHex =
extern std::string const kDisabledFloatHex =
"0061736d010000000108026000006000017f03030200010503010002063e0a7f004180080b7f004180080b7f004180"
"100b7f004180100b7f00418090040b7f004180080b7f00418090040b7f00418080080b7f0041000b7f0041010b07b0"
"010d066d656d6f72790200115f5f7761736d5f63616c6c5f63746f727300000666696e69736800010362756603000c"
@@ -1043,143 +1045,143 @@ extern std::string const disabledFloatHex =
"656e6403070d5f5f6d656d6f72795f6261736503080c5f5f7461626c655f6261736503090a150202000b1000430000"
"00c54300200045921a41010b";
extern std::string const memoryPointerAtLimitHex =
extern std::string const kMemoryPointerAtLimitHex =
"0061736d010000000105016000017f030201000503010001070a010666696e69736800000a0e010c0041ffff032d00"
"001a41010b";
extern std::string const memoryPointerOverLimitHex =
extern std::string const kMemoryPointerOverLimitHex =
"0061736d010000000105016000017f030201000503010001070a010666696e69736800000a0e010c00418080042d00"
"001a41010b";
extern std::string const memoryOffsetOverLimitHex =
extern std::string const kMemoryOffsetOverLimitHex =
"0061736d010000000105016000017f030201000503010001071302066d656d6f727902000666696e69736800000a0e"
"010c00410028028080041a41010b";
extern std::string const memoryEndOfWordOverLimitHex =
extern std::string const kMemoryEndOfWordOverLimitHex =
"0061736d010000000105016000017f030201000503010001071302066d656d6f727902000666696e69736800000a0e"
"010c0041feff032802001a41010b";
extern std::string const memoryGrow0To1PageHex =
extern std::string const kMemoryGrow0To1PageHex =
"0061736d010000000105016000017f030201000503010000071302066d656d6f727902000666696e69736800000a0b"
"010900410140001a41010b";
extern std::string const memoryGrow1To0PageHex =
extern std::string const kMemoryGrow1To0PageHex =
"0061736d010000000105016000017f030201000503010001071302066d656d6f727902000666696e69736800000a13"
"011100417f4000417f460440417f0f0b41010b";
extern std::string const memoryLastByteOf8MBHex =
extern std::string const kMemoryLastByteOf8MbHex =
"0061736d010000000105016000017f030201000506010180018001071302066d656d6f727902000666696e69736800"
"000a0f010d0041ffffff032d00001a41010b";
extern std::string const memoryGrow1MoreThan8MBHex =
extern std::string const kMemoryGrow1MoreThan8MbHex =
"0061736d010000000105016000017f03020100050401008001071302066d656d6f727902000666696e69736800000a"
"1301110041014000417f460440417f0f0b41010b";
extern std::string const memoryGrow0MoreThan8MBHex =
extern std::string const kMemoryGrow0MoreThan8MbHex =
"0061736d010000000105016000017f03020100050401008001071302066d656d6f727902000666696e69736800000a"
"1301110041004000417f460440417f0f0b41010b";
extern std::string const memoryInit1MoreThan8MBHex =
extern std::string const kMemoryInit1MoreThan8MbHex =
"0061736d010000000105016000017f030201000506010181018101071302066d656d6f727902000666696e69736800"
"000a0f010d0041ffffff032d00001a41010b";
extern std::string const memoryNegativeAddressHex =
extern std::string const kMemoryNegativeAddressHex =
"0061736d010000000105016000017f030201000506010180018001071302066d656d6f727902000666696e69736800"
"000a0c010a00417f2d00001a41010b";
extern std::string const table64ElementsHex =
extern std::string const kTable64ElementsHex =
"0061736d010000000108026000006000017f0303020001040401700040070a010666696e6973680001094601004100"
"0b40000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000a090202000b040041010b";
extern std::string const table65ElementsHex =
extern std::string const kTable65ElementsHex =
"0061736d010000000108026000006000017f0303020001040401700041070a010666696e6973680001094701004100"
"0b41000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000a090202000b040041010b";
extern std::string const table2TablesHex =
extern std::string const kTable2TablesHex =
"0061736d010000000108026000006000017f03030200010409027001010170010101070a010666696e697368000109"
"0f020041000b0100020141000b0001000a090202000b040041010b";
extern std::string const table0ElementsHex =
extern std::string const kTable0ElementsHex =
"0061736d010000000105016000017f03020100040401700000070a010666696e69736800000a0601040041010b";
extern std::string const tableUintMaxHex =
extern std::string const kTableUintMaxHex =
"0061736d010000000105016000017f030201000408017000ffffffff0f070a010666696e69736800000a0601040041"
"010b";
extern std::string const proposalMutableGlobalHex =
extern std::string const kProposalMutableGlobalHex =
"0061736d010000000105016000017f030201000606017f0141000b07140207636f756e74657203000666696e697368"
"00000a0d010b00230041016a240041010b";
extern std::string const proposalGcStructNewHex =
extern std::string const kProposalGcStructNewHex =
"0061736d01000000010b026000017f5f027f017f0103020100070a010666696e69736800000a0a010800fb01011a41"
"010b";
extern std::string const proposalMultiValueHex =
extern std::string const kProposalMultiValueHex =
"0061736d010000000110036000027f7f6000017f60027f7f017f0303020001070a010666696e69736800010a140206"
"00410a41140b0b00100002026a411e460b0b";
extern std::string const proposalSignExtHex =
extern std::string const kProposalSignExtHex =
"0061736d010000000105016000017f03020100070a010666696e69736800000a0b01090041ff01c0417f460b";
extern std::string const proposalFloatToIntHex =
extern std::string const kProposalFloatToIntHex =
"0061736d010000000105016000017f03020100070a010666696e69736800000a1201100043f9021550fc0041ffffff"
"ff07460b";
extern std::string const proposalBulkMemoryHex =
extern std::string const kProposalBulkMemoryHex =
"0061736d010000000105016000017f030201000503010001071302066d656d6f727902000666696e69736800000a1f"
"011d004100412a3a000041e40041004101fc0a000041e4002d0000412a460b";
extern std::string const proposalRefTypesHex =
extern std::string const kProposalRefTypesHex =
"0061736d010000000105016000017f020f0103656e76057461626c65016f000103020100070a010666696e69736800"
"000a0c010a004100d06f260041010b";
extern std::string const proposalTailCallHex =
extern std::string const kProposalTailCallHex =
"0061736d010000000105016000017f0303020000070a010666696e69736800010a0b02040041010b040012000b";
extern std::string const proposalExtendedConstHex =
extern std::string const kProposalExtendedConstHex =
"0061736d010000000105016000017f030201000609017f00410a41206a0b070a010666696e69736800000a09010700"
"2300412a460b";
extern std::string const proposalMultiMemoryHex =
extern std::string const kProposalMultiMemoryHex =
"0061736d010000000105016000017f0302010005050200000001070a010666696e69736800000a060104003f010b";
extern std::string const proposalCustomPageSizesHex =
extern std::string const kProposalCustomPageSizesHex =
"0061736d010000000105016000017f0302010005040108010a070a010666696e69736800000a0601040041010b0010"
"046e616d65010901000666696e697368";
extern std::string const proposalMemory64Hex =
extern std::string const kProposalMemory64Hex =
"0061736d010000000105016000017f030201000503010401070a010666696e69736800000a10010e004200412a3a00"
"003f004201510b0010046e616d65010901000666696e697368";
extern std::string const proposalWideArithmeticHex =
extern std::string const kProposalWideArithmeticHex =
"0061736d010000000105016000017f03020100070a010666696e69736800000a0e010c0042014202fc161a1a41010b"
"0010046e616d65010901000666696e697368";
extern std::string const trapDivideBy0Hex =
extern std::string const kTrapDivideBy0Hex =
"0061736d010000000105016000017f03020100070a010666696e69736800000a0c010a00412a41006d1a41010b";
extern std::string const trapIntOverflowHex =
extern std::string const kTrapIntOverflowHex =
"0061736d010000000105016000017f03020100070a010666696e69736800000a0d010b00418080808078417f6d0b";
extern std::string const trapUnreachableHex =
extern std::string const kTrapUnreachableHex =
"0061736d010000000105016000017f03020100070a010666696e69736800000a070105000041010b";
extern std::string const trapNullCallHex =
extern std::string const kTrapNullCallHex =
"0061736d010000000105016000017f03020100040401700001070a010666696e69736800000a090107004100110000"
"0b";
extern std::string const trapFuncSigMismatchHex =
extern std::string const kTrapFuncSigMismatchHex =
"0061736d010000000108026000006000017f0303020001040401700001070a010666696e6973680001090701004100"
"0b01000a0d020300010b070041001101000b";
extern std::string const wasiGetTimeHex =
extern std::string const kWasiGetTimeHex =
"0061736d01000000010c0260037f7e7f017f6000017f02290116776173695f736e617073686f745f70726576696577"
"310e636c6f636b5f74696d655f6765740000030201010503010001071302066d656d6f727902000666696e69736800"
"010a16011400410042e8074100100045047f410105417f0b0b";
extern std::string const wasiPrintHex =
extern std::string const kWasiPrintHex =
"0061736d01000000010d0260047f7f7f7f017f6000017f02230116776173695f736e617073686f745f707265766965"
"77310866645f77726974650000030201010503010001071302066d656d6f727902000666696e69736800010a1d011b"
"01017f411821004101410041012000100045047f410105417f0b0b0b1e030041100b0648656c6c6f0a0041000b0410"
@@ -1190,8 +1192,8 @@ extern std::string const wasiPrintHex =
// rust or wat sources.
// Wasm code magic number is "0061736d", and the only valid version is 1.
extern std::string const badMagicNumberHex = "1061736d01000000";
extern std::string const badVersionNumberHex = "0061736d02000000";
extern std::string const kBadMagicNumberHex = "1061736d01000000";
extern std::string const kBadVersionNumberHex = "0061736d02000000";
// Corruption Test: lyingHeader
// Scenario: A section declares it is 2GB long, but the file ends immediately.
@@ -1201,7 +1203,7 @@ extern std::string const badVersionNumberHex = "0061736d02000000";
// # Type Section (ID 1)
// # Size: LEB128 encoded 2GB (0x80 0x80 0x80 0x80 0x08)
// data += b'\x01\x80\x80\x80\x80\x08'
extern std::string const lyingHeaderHex = "0061736d01000000018080808008";
extern std::string const kLyingHeaderHex = "0061736d01000000018080808008";
// Corruption Test: neverEndingNumber
// Scenario: An LEB128 integer that never has a stop bit (byte < 0x80).
@@ -1211,7 +1213,7 @@ extern std::string const lyingHeaderHex = "0061736d01000000018080808008";
// data += b'\x01\x05'
// # Vector count: Infinite stream of 0x80 (100 bytes)
// data += b'\x80' * 100
extern std::string const neverEndingNumberHex =
extern std::string const kNeverEndingNumberHex =
"0061736d01000000010580808080808080808080808080808080808080808080808080808080808080808080808080"
"8080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080"
"80808080808080808080808080808080";
@@ -1226,7 +1228,7 @@ extern std::string const neverEndingNumberHex =
// # Vector Count: 0xFF 0xFF 0xFF 0xFF 0x0F (4,294,967,295 items)
// data += b'\xff\xff\xff\xff\x0f'
// # No actual items follow...
extern std::string const vectorLieHex = "0061736d010000000105ffffffff0f";
extern std::string const kVectorLieHex = "0061736d010000000105ffffffff0f";
// Corruption Test: sectionOrdering
// Scenario: Sections appear out of order
@@ -1238,7 +1240,7 @@ extern std::string const vectorLieHex = "0061736d010000000105ffffffff0f";
// data += b'\x0a\x02\x00\x0b'
// # Function Section (ID 3) - usually 3rd
// data += b'\x03\x02\x00\x00'
extern std::string const sectionOrderingHex = "0061736d010000000a02000b03020000";
extern std::string const kSectionOrderingHex = "0061736d010000000a02000b03020000";
// Corruption Test: ghostPayload
// Scenario: Valid headers, but file is truncated in the middle of a payload.
@@ -1251,7 +1253,7 @@ extern std::string const sectionOrderingHex = "0061736d010000000a02000b03020000"
// # Start of a type definition (0x60 = func)
// data += b'\x60'
// # File ends abruptly here (missing params/results)
extern std::string const ghostPayloadHex = "0061736d01000000010a0160";
extern std::string const kGhostPayloadHex = "0061736d01000000010a0160";
// Corruption Test: junkAfterSection
// Scenario: Section declares size X, but logical content finishes at X-5.
@@ -1265,7 +1267,7 @@ extern std::string const ghostPayloadHex = "0061736d01000000010a0160";
// data += b'\x01\x60\x00\x00'
// # Remaining 6 bytes are junk padding within the section size
// data += b'\x00' * 6
extern std::string const junkAfterSectionHex = "0061736d01000000010a01600000000000000000";
extern std::string const kJunkAfterSectionHex = "0061736d01000000010a01600000000000000000";
// Corruption Test: invalidSectionId
// Scenario: A section ID that doesn't exist (0xFF).
@@ -1273,7 +1275,7 @@ extern std::string const junkAfterSectionHex = "0061736d01000000010a016000000000
// data = b'\x00\x61\x73\x6d\x01\x00\x00\x00'
// # Section ID 0xFF, Size 1
// data += b'\xff\x01\x00'
extern std::string const invalidSectionIdHex = "0061736d01000000ff0100";
extern std::string const kInvalidSectionIdHex = "0061736d01000000ff0100";
// Corruption Test: localVariableBomb
// Scenario: A function declares 4 billion local variables.
@@ -1296,10 +1298,10 @@ extern std::string const invalidSectionIdHex = "0061736d01000000ff0100";
// data += b'\xff\xff\xff\xff\x0f\x7f'
// # Instruction: end (0x0b)
// data += b'\x0b'
extern std::string const localVariableBombHex =
extern std::string const kLocalVariableBombHex =
"0061736d01000000010401600000030201000a0f010d01ffffffff0f7f0b";
extern std::string const infiniteLoopWasmHex =
extern std::string const kInfiniteLoopWasmHex =
"0061736d010000000108026000006000017f030302000105030100020638097f004180080b7f004180080b7f004180"
"080b7f00418088040b7f004180080b7f00418088040b7f00418080080b7f0041000b7f0041010b07a8010c066d656d"
"6f72790200115f5f7761736d5f63616c6c5f63746f72730000046c6f6f7000010c5f5f64736f5f68616e646c650300"
@@ -1312,11 +1314,11 @@ extern std::string const infiniteLoopWasmHex =
"3732302900490f7461726765745f6665617475726573042b0f6d757461626c652d676c6f62616c732b087369676e2d"
"6578742b0f7265666572656e63652d74797065732b0a6d756c746976616c7565";
extern std::string const startLoopHex =
extern std::string const kStartLoopHex =
"0061736d010000000108026000006000017f030302000107120205737461727400000666696e69736800010801000a"
"0e02070003400c000b0b040041010b";
extern std::string const badAlignWasmHex =
extern std::string const kBadAlignWasmHex =
"0061736d01000000011b046000017f60057f7f7f7f7f017f60067f7f7f7f7f7f017f600000022a0203656e760f666c"
"6f61745f66726f6d5f75696e74000103656e760c636865636b5f6b65796c6574000203050403000000050301000306"
"470b7f004180080b7f00418088020b7f004180080b7f00418088040b7f00418088040b7f00418088080b7f00418008"
@@ -1337,7 +1339,7 @@ extern std::string const badAlignWasmHex =
"7475726573042b0f6d757461626c652d676c6f62616c732b087369676e2d6578742b0f7265666572656e63652d7479"
"7065732b0a6d756c746976616c7565";
extern std::string const thousandParamsHex =
extern std::string const kThousandParamsHex =
"0061736d0100000001f1070260000060e8077f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f"
"7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f"
"7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f"
@@ -1453,7 +1455,7 @@ extern std::string const thousandParamsHex =
"32343732302900490f7461726765745f6665617475726573042b0f6d757461626c652d676c6f62616c732b08736967"
"6e2d6578742b0f7265666572656e63652d74797065732b0a6d756c746976616c7565";
extern std::string const thousand1ParamsHex =
extern std::string const kThousand1ParamsHex =
"0061736d0100000001f2070260000060e9077f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f"
"7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f"
"7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f"
@@ -1569,13 +1571,13 @@ extern std::string const thousand1ParamsHex =
"623432626432343732302900490f7461726765745f6665617475726573042b0f6d757461626c652d676c6f62616c73"
"2b087369676e2d6578742b0f7265666572656e63652d74797065732b0a6d756c746976616c7565";
extern std::string const opcReservedHex =
extern std::string const kOpcReservedHex =
"0061736d010000000105016000017f03030200000404017000010503010001060b027f0141000b7e0142000b071401"
"10616c6c5f696e737472756374696f6e7300010907010041000b01000a53020400412a0b4c02017f017e0101010101"
"0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101"
"01010101010101010101010101010101410b0b0b0a010041000b0474657374";
extern std::string const impExpHex =
extern std::string const kImpExpHex =
"0061736d0100000001100360027f7f017f6000017f60017f017f02330203656e760e6765745f6c65646765725f7371"
"6e000003656e76166765745f706172656e745f6c65646765725f686173680000030403010201050301000107310406"
"6d656d6f72790200096578705f66756e63310002096578705f66756e633200030c746573745f696d706f7274730004"

View File

@@ -4,80 +4,80 @@
#include <string>
extern std::string const ledgerSqnWasmHex;
extern std::string const allHostFunctionsWasmHex;
extern std::string const allKeyletsWasmHex;
extern std::string const codecovTestsWasmHex;
extern std::string const kLedgerSqnWasmHex;
extern std::string const kAllHostFunctionsWasmHex;
extern std::string const kAllKeyletsWasmHex;
extern std::string const kCodecovTestsWasmHex;
extern std::string const fibWasmHex;
extern std::string const kFibWasmHex;
extern std::string const floatTestsWasmHex;
extern std::string const float0Hex;
extern std::string const disabledFloatHex;
extern std::string const kFloatTestsWasmHex;
extern std::string const kFloat0Hex;
extern std::string const kDisabledFloatHex;
extern std::string const memoryPointerAtLimitHex;
extern std::string const memoryPointerOverLimitHex;
extern std::string const memoryOffsetOverLimitHex;
extern std::string const memoryEndOfWordOverLimitHex;
extern std::string const memoryGrow0To1PageHex;
extern std::string const memoryGrow1To0PageHex;
extern std::string const memoryLastByteOf8MBHex;
extern std::string const memoryGrow1MoreThan8MBHex;
extern std::string const memoryGrow0MoreThan8MBHex;
extern std::string const memoryInit1MoreThan8MBHex;
extern std::string const memoryNegativeAddressHex;
extern std::string const kMemoryPointerAtLimitHex;
extern std::string const kMemoryPointerOverLimitHex;
extern std::string const kMemoryOffsetOverLimitHex;
extern std::string const kMemoryEndOfWordOverLimitHex;
extern std::string const kMemoryGrow0To1PageHex;
extern std::string const kMemoryGrow1To0PageHex;
extern std::string const kMemoryLastByteOf8MbHex;
extern std::string const kMemoryGrow1MoreThan8MbHex;
extern std::string const kMemoryGrow0MoreThan8MbHex;
extern std::string const kMemoryInit1MoreThan8MbHex;
extern std::string const kMemoryNegativeAddressHex;
extern std::string const table64ElementsHex;
extern std::string const table65ElementsHex;
extern std::string const table2TablesHex;
extern std::string const table0ElementsHex;
extern std::string const tableUintMaxHex;
extern std::string const kTable64ElementsHex;
extern std::string const kTable65ElementsHex;
extern std::string const kTable2TablesHex;
extern std::string const kTable0ElementsHex;
extern std::string const kTableUintMaxHex;
extern std::string const proposalMutableGlobalHex;
extern std::string const proposalGcStructNewHex;
extern std::string const proposalMultiValueHex;
extern std::string const proposalSignExtHex;
extern std::string const proposalFloatToIntHex;
extern std::string const proposalBulkMemoryHex;
extern std::string const proposalRefTypesHex;
extern std::string const proposalTailCallHex;
extern std::string const proposalExtendedConstHex;
extern std::string const proposalMultiMemoryHex;
extern std::string const proposalCustomPageSizesHex;
extern std::string const proposalMemory64Hex;
extern std::string const proposalWideArithmeticHex;
extern std::string const kProposalMutableGlobalHex;
extern std::string const kProposalGcStructNewHex;
extern std::string const kProposalMultiValueHex;
extern std::string const kProposalSignExtHex;
extern std::string const kProposalFloatToIntHex;
extern std::string const kProposalBulkMemoryHex;
extern std::string const kProposalRefTypesHex;
extern std::string const kProposalTailCallHex;
extern std::string const kProposalExtendedConstHex;
extern std::string const kProposalMultiMemoryHex;
extern std::string const kProposalCustomPageSizesHex;
extern std::string const kProposalMemory64Hex;
extern std::string const kProposalWideArithmeticHex;
extern std::string const trapDivideBy0Hex;
extern std::string const trapIntOverflowHex;
extern std::string const trapUnreachableHex;
extern std::string const trapNullCallHex;
extern std::string const trapFuncSigMismatchHex;
extern std::string const kTrapDivideBy0Hex;
extern std::string const kTrapIntOverflowHex;
extern std::string const kTrapUnreachableHex;
extern std::string const kTrapNullCallHex;
extern std::string const kTrapFuncSigMismatchHex;
extern std::string const wasiGetTimeHex;
extern std::string const wasiPrintHex;
extern std::string const kWasiGetTimeHex;
extern std::string const kWasiPrintHex;
extern std::string const badMagicNumberHex;
extern std::string const badVersionNumberHex;
extern std::string const lyingHeaderHex;
extern std::string const neverEndingNumberHex;
extern std::string const vectorLieHex;
extern std::string const sectionOrderingHex;
extern std::string const ghostPayloadHex;
extern std::string const junkAfterSectionHex;
extern std::string const invalidSectionIdHex;
extern std::string const localVariableBombHex;
extern std::string const kBadMagicNumberHex;
extern std::string const kBadVersionNumberHex;
extern std::string const kLyingHeaderHex;
extern std::string const kNeverEndingNumberHex;
extern std::string const kVectorLieHex;
extern std::string const kSectionOrderingHex;
extern std::string const kGhostPayloadHex;
extern std::string const kJunkAfterSectionHex;
extern std::string const kInvalidSectionIdHex;
extern std::string const kLocalVariableBombHex;
extern std::string const deepRecursionHex;
extern std::string const infiniteLoopWasmHex;
extern std::string const startLoopHex;
extern std::string const kDeepRecursionHex;
extern std::string const kInfiniteLoopWasmHex;
extern std::string const kStartLoopHex;
extern std::string const badAlignWasmHex;
extern std::string const kBadAlignWasmHex;
extern std::string const thousandParamsHex;
extern std::string const thousand1ParamsHex;
extern std::string const locals10kHex;
extern std::string const functions5kHex;
extern std::string const kThousandParamsHex;
extern std::string const kThousand1ParamsHex;
extern std::string const kLocals10kHex;
extern std::string const kFunctions5kHex;
extern std::string const opcReservedHex;
extern std::string const kOpcReservedHex;
extern std::string const impExpHex;
extern std::string const kImpExpHex;