initial wasm builder for unit testing

This commit is contained in:
Richard Holland
2022-10-07 09:01:00 +00:00
parent 8662adcff8
commit eff650c922
2 changed files with 53 additions and 34 deletions

View File

@@ -20,47 +20,32 @@
#include <ripple/protocol/jss.h>
#include <test/jtx.h>
#include <test/jtx/hook.h>
#include <test/app/SetHook_wasm.h>
namespace ripple {
namespace test {
using TestHook = std::vector<uint8_t> const&;
class SetHook_test : public beast::unit_test::suite
{
private:
std::vector<uint8_t> const
accept_wasm =
{
/*
(module
(type (;0;) (func (param i32 i32 i64) (result i64)))
(type (;1;) (func (param i32 i32) (result i32)))
(type (;2;) (func (param i32) (result i64)))
(import "env" "accept" (func (;0;) (type 0)))
(import "env" "_g" (func (;1;) (type 1)))
(func (;2;) (type 2) (param i32) (result i64)
(local i32)
i32.const 0
i32.const 0
i64.const 0
call 0
drop
i32.const 1
i32.const 1
call 1
drop
i64.const 0)
(memory (;0;) 2)
(export "hook" (func 2)))
*/
0x00U,0x61U,0x73U,0x6dU,0x01U,0x00U,0x00U,0x00U,0x01U,0x13U,0x03U,0x60U,0x03U,0x7fU,0x7fU,0x7eU,0x01U,0x7eU,
0x60U,0x02U,0x7fU,0x7fU,0x01U,0x7fU,0x60U,0x01U,0x7fU,0x01U,0x7eU,0x02U,0x17U,0x02U,0x03U,0x65U,0x6eU,0x76U,
0x06U,0x61U,0x63U,0x63U,0x65U,0x70U,0x74U,0x00U,0x00U,0x03U,0x65U,0x6eU,0x76U,0x02U,0x5fU,0x67U,0x00U,0x01U,
0x03U,0x02U,0x01U,0x02U,0x05U,0x03U,0x01U,0x00U,0x02U,0x07U,0x08U,0x01U,0x04U,0x68U,0x6fU,0x6fU,0x6bU,0x00U,
0x02U,0x0aU,0x18U,0x01U,0x16U,0x01U,0x01U,0x7fU,0x41U,0x00U,0x41U,0x00U,0x42U,0x00U,0x10U,0x00U,0x1aU,0x41U,
0x01U,0x41U,0x01U,0x10U,0x01U,0x1aU,0x42U,0x00U,0x0bU
};
TestHook
accept_wasm =
wasm[
R"[test.hook](
#include <stdint.h>
extern int32_t _g (uint32_t id, uint32_t maxiter);
extern int64_t accept (uint32_t read_ptr, uint32_t read_len, int64_t error_code);
int64_t hook(uint32_t reserved )
{
accept(0,0,0);
_g(1,1);
}
)[test.hook]"
];
public:
@@ -89,7 +74,7 @@ public:
// Must have a "Hooks" field
env(ripple::test::jtx::hook(alice, {}, 0), ter(temMALFORMED));
// Must have at least one non-empty subfield
// Must have at least one non-empty subfield
env(ripple::test::jtx::hook(alice, {{}}, 0), ter(temMALFORMED));
// Trivial single hook

View File

@@ -0,0 +1,34 @@
#!/bin/bash
echo '
//This file is generated by build_test_hooks.h
#ifndef SETHOOK_WASM_INCLUDED
#define SETHOOK_WASM_INCLUDED
#include <map>
#include <stdint.h>
#include <string>
#include <vector>
namespace ripple {
namespace test {
std::map<std::string, std::vector<uint8_t>> wasm = {' > SetHook_wasm.h
cat SetHook_test.cpp | tr '\n' '\f' |
grep -Po 'R"\[test\.hook\](.*?)\[test\.hook\]"' |
sed -E 's/R"\[test\.hook\]\(//g' |
sed -E 's/\)\[test\.hook\]"[\f \t]*//g' |
while read -r line
do
echo -n '{ R"[test.hook](' >> SetHook_wasm.h
# tr '\f' '\n' <<< $line >> SetHook_wasm.h
cat <<< $line | tr -d '\n' | tr '\f' '\n' >> SetHook_wasm.h
echo ')[test.hook]",' >> SetHook_wasm.h
echo "{" >> SetHook_wasm.h
wasmcc -x c /dev/stdin -o /dev/stdout -O2 -Wl,--allow-undefined <<< `tr '\f' '\n' <<< $line` |
xxd -p -u -c 19 |
sed -E 's/../0x\0U,/g' | sed -E 's/^/ /g' >> SetHook_wasm.h
echo '}},' >> SetHook_wasm.h
echo >> SetHook_wasm.h
done
echo '};
}
}
#endif' >> SetHook_wasm.h