Switch to WAMR (#5416)

* Switch to WAMR
This commit is contained in:
Olek
2025-05-01 18:02:06 -04:00
committed by GitHub
parent f03b5883bd
commit bb9bc764bc
18 changed files with 2454 additions and 829 deletions

View File

@@ -21,105 +21,21 @@
#include <xrpld/app/misc/WasmVM.h>
#include <wasmedge/wasmedge.h>
#include <iterator>
#include <iwasm/wasm_c_api.h>
namespace ripple {
namespace test {
/* Host function body definition. */
WasmEdge_Result
Add(void* Data,
const WasmEdge_CallingFrameContext* CallFrameCxt,
const WasmEdge_Value* In,
WasmEdge_Value* Out)
using Add_proto = int32_t(int32_t, int32_t);
wasm_trap_t*
Add(void* env, const wasm_val_vec_t* params, wasm_val_vec_t* results)
{
int32_t Val1 = WasmEdge_ValueGetI32(In[0]);
int32_t Val2 = WasmEdge_ValueGetI32(In[1]);
int32_t Val1 = params->data[0].of.i32;
int32_t Val2 = params->data[1].of.i32;
// printf("Host function \"Add\": %d + %d\n", Val1, Val2);
Out[0] = WasmEdge_ValueGenI32(Val1 + Val2);
return WasmEdge_Result_Success;
}
void
invokeAdd()
{
/* Create the VM context. */
WasmEdge_VMContext* VMCxt = WasmEdge_VMCreate(NULL, NULL);
// clang-format off
/* The WASM module buffer. */
uint8_t WASM[] = {/* WASM header */
0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00,
/* Type section */
0x01, 0x07, 0x01,
/* function type {i32, i32} -> {i32} */
0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F,
/* Import section */
0x02, 0x13, 0x01,
/* module name: "extern" */
0x06, 0x65, 0x78, 0x74, 0x65, 0x72, 0x6E,
/* extern name: "func-add" */
0x08, 0x66, 0x75, 0x6E, 0x63, 0x2D, 0x61, 0x64, 0x64,
/* import desc: func 0 */
0x00, 0x00,
/* Function section */
0x03, 0x02, 0x01, 0x00,
/* Export section */
0x07, 0x0A, 0x01,
/* export name: "addTwo" */
0x06, 0x61, 0x64, 0x64, 0x54, 0x77, 0x6F,
/* export desc: func 0 */
0x00, 0x01,
/* Code section */
0x0A, 0x0A, 0x01,
/* code body */
0x08, 0x00, 0x20, 0x00, 0x20, 0x01, 0x10, 0x00, 0x0B};
// clang-format on
/* Create the module instance. */
WasmEdge_String ExportName = WasmEdge_StringCreateByCString("extern");
WasmEdge_ModuleInstanceContext* HostModCxt =
WasmEdge_ModuleInstanceCreate(ExportName);
WasmEdge_ValType ParamList[2] = {
WasmEdge_ValTypeGenI32(), WasmEdge_ValTypeGenI32()};
WasmEdge_ValType ReturnList[1] = {WasmEdge_ValTypeGenI32()};
WasmEdge_FunctionTypeContext* HostFType =
WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
WasmEdge_FunctionInstanceContext* HostFunc =
WasmEdge_FunctionInstanceCreate(HostFType, Add, NULL, 0);
WasmEdge_FunctionTypeDelete(HostFType);
WasmEdge_String HostFuncName = WasmEdge_StringCreateByCString("func-add");
WasmEdge_ModuleInstanceAddFunction(HostModCxt, HostFuncName, HostFunc);
WasmEdge_StringDelete(HostFuncName);
WasmEdge_VMRegisterModuleFromImport(VMCxt, HostModCxt);
/* The parameters and returns arrays. */
WasmEdge_Value Params[2] = {
WasmEdge_ValueGenI32(1234), WasmEdge_ValueGenI32(5678)};
WasmEdge_Value Returns[1];
/* Function name. */
WasmEdge_String FuncName = WasmEdge_StringCreateByCString("addTwo");
/* Run the WASM function from buffer. */
WasmEdge_Result Res = WasmEdge_VMRunWasmFromBuffer(
VMCxt, WASM, sizeof(WASM), FuncName, Params, 2, Returns, 1);
if (WasmEdge_ResultOK(Res))
{
// printf("invokeAdd get the result: %d\n",
// WasmEdge_ValueGetI32(Returns[0]));
}
else
{
printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
}
/* Resources deallocations. */
WasmEdge_VMDelete(VMCxt);
WasmEdge_StringDelete(FuncName);
WasmEdge_ModuleInstanceDelete(HostModCxt);
results->data[0] = WASM_I32_VAL(Val1 + Val2);
return nullptr;
}
struct Wasm_test : public beast::unit_test::suite
@@ -128,8 +44,46 @@ struct Wasm_test : public beast::unit_test::suite
testWasmtimeLib()
{
testcase("wasmtime lib test");
invokeAdd();
BEAST_EXPECT(true);
// clang-format off
/* The WASM module buffer. */
wbytes const wasm = {/* WASM header */
0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00,
/* Type section */
0x01, 0x07, 0x01,
/* function type {i32, i32} -> {i32} */
0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F,
/* Import section */
0x02, 0x13, 0x01,
/* module name: "extern" */
0x06, 0x65, 0x78, 0x74, 0x65, 0x72, 0x6E,
/* extern name: "func-add" */
0x08, 0x66, 0x75, 0x6E, 0x63, 0x2D, 0x61, 0x64, 0x64,
/* import desc: func 0 */
0x00, 0x00,
/* Function section */
0x03, 0x02, 0x01, 0x00,
/* Export section */
0x07, 0x0A, 0x01,
/* export name: "addTwo" */
0x06, 0x61, 0x64, 0x64, 0x54, 0x77, 0x6F,
/* export desc: func 0 */
0x00, 0x01,
/* Code section */
0x0A, 0x0A, 0x01,
/* code body */
0x08, 0x00, 0x20, 0x00, 0x20, 0x01, 0x10, 0x00, 0x0B};
// clang-format on
auto& vm = WasmEngine::instance();
std::vector<WasmImportFunc> imports;
WasmImpFunc<Add_proto>(
imports, "func-add", reinterpret_cast<void*>(&Add));
auto res = vm.run(wasm, "addTwo", imports, wasmParams(1234, 5678));
// if (res) printf("invokeAdd get the result: %d\n", res.value());
BEAST_EXPECT(res.has_value() && res.value() == 6912);
}
void
@@ -1412,9 +1366,9 @@ struct Wasm_test : public beast::unit_test::suite
BadTestHostFunctions nfs(&env);
std::string funcName("ready");
auto re = runEscrowWasm(wasm, funcName, &nfs, 100000);
BEAST_EXPECT(re.error());
std::cout << "bad case (more than MAX_PAGES) result " << re.error()
<< std::endl;
if (BEAST_EXPECT(!re))
std::cout << "bad case (more than MAX_PAGES) result "
<< re.error() << std::endl;
}
{ // fail because recursion too deep
@@ -2143,9 +2097,9 @@ struct Wasm_test : public beast::unit_test::suite
TestHostFunctions nfs(&env);
std::string funcName("recursive");
auto re = runEscrowWasm(wasm, funcName, &nfs, 1000'000'000);
BEAST_EXPECT(re.error());
std::cout << "bad case (deep recursion) result " << re.error()
<< std::endl;
if (BEAST_EXPECT(re.error()))
std::cout << "bad case (deep recursion) result " << re.error()
<< std::endl;
}
}