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