Skip to content

Commit 7bd6a2c

Browse files
devsnektargos
authored andcommitted
wasi: fast calls
PR-URL: #43697 Backport-PR-URL: #45908 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent e294410 commit 7bd6a2c

28 files changed

+850
-1184
lines changed

β€Žsrc/node_wasi.cc

+705-1,121
Large diffs are not rendered by default.

β€Žsrc/node_wasi.h

+135-60
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
#include "base_object.h"
77
#include "node_mem.h"
88
#include "uvwasi.h"
9+
#include "v8-fast-api-calls.h"
910

1011
namespace node {
1112
namespace wasi {
1213

14+
struct WasmMemory {
15+
char* data;
16+
size_t size;
17+
};
1318

1419
class WASI : public BaseObject,
1520
public mem::NgLibMemoryManager<WASI, uvwasi_mem_t> {
@@ -23,57 +28,120 @@ class WASI : public BaseObject,
2328
SET_MEMORY_INFO_NAME(WASI)
2429
SET_SELF_SIZE(WASI)
2530

26-
static void ArgsGet(const v8::FunctionCallbackInfo<v8::Value>& args);
27-
static void ArgsSizesGet(const v8::FunctionCallbackInfo<v8::Value>& args);
28-
static void ClockResGet(const v8::FunctionCallbackInfo<v8::Value>& args);
29-
static void ClockTimeGet(const v8::FunctionCallbackInfo<v8::Value>& args);
30-
static void EnvironGet(const v8::FunctionCallbackInfo<v8::Value>& args);
31-
static void EnvironSizesGet(const v8::FunctionCallbackInfo<v8::Value>& args);
32-
static void FdAdvise(const v8::FunctionCallbackInfo<v8::Value>& args);
33-
static void FdAllocate(const v8::FunctionCallbackInfo<v8::Value>& args);
34-
static void FdClose(const v8::FunctionCallbackInfo<v8::Value>& args);
35-
static void FdDatasync(const v8::FunctionCallbackInfo<v8::Value>& args);
36-
static void FdFdstatGet(const v8::FunctionCallbackInfo<v8::Value>& args);
37-
static void FdFdstatSetFlags(const v8::FunctionCallbackInfo<v8::Value>& args);
38-
static void FdFdstatSetRights(
39-
const v8::FunctionCallbackInfo<v8::Value>& args);
40-
static void FdFilestatGet(const v8::FunctionCallbackInfo<v8::Value>& args);
41-
static void FdFilestatSetSize(
42-
const v8::FunctionCallbackInfo<v8::Value>& args);
43-
static void FdFilestatSetTimes(
44-
const v8::FunctionCallbackInfo<v8::Value>& args);
45-
static void FdPread(const v8::FunctionCallbackInfo<v8::Value>& args);
46-
static void FdPrestatGet(const v8::FunctionCallbackInfo<v8::Value>& args);
47-
static void FdPrestatDirName(const v8::FunctionCallbackInfo<v8::Value>& args);
48-
static void FdPwrite(const v8::FunctionCallbackInfo<v8::Value>& args);
49-
static void FdRead(const v8::FunctionCallbackInfo<v8::Value>& args);
50-
static void FdReaddir(const v8::FunctionCallbackInfo<v8::Value>& args);
51-
static void FdRenumber(const v8::FunctionCallbackInfo<v8::Value>& args);
52-
static void FdSeek(const v8::FunctionCallbackInfo<v8::Value>& args);
53-
static void FdSync(const v8::FunctionCallbackInfo<v8::Value>& args);
54-
static void FdTell(const v8::FunctionCallbackInfo<v8::Value>& args);
55-
static void FdWrite(const v8::FunctionCallbackInfo<v8::Value>& args);
56-
static void PathCreateDirectory(
57-
const v8::FunctionCallbackInfo<v8::Value>& args);
58-
static void PathFilestatGet(const v8::FunctionCallbackInfo<v8::Value>& args);
59-
static void PathFilestatSetTimes(
60-
const v8::FunctionCallbackInfo<v8::Value>& args);
61-
static void PathLink(const v8::FunctionCallbackInfo<v8::Value>& args);
62-
static void PathOpen(const v8::FunctionCallbackInfo<v8::Value>& args);
63-
static void PathReadlink(const v8::FunctionCallbackInfo<v8::Value>& args);
64-
static void PathRemoveDirectory(
65-
const v8::FunctionCallbackInfo<v8::Value>& args);
66-
static void PathRename(const v8::FunctionCallbackInfo<v8::Value>& args);
67-
static void PathSymlink(const v8::FunctionCallbackInfo<v8::Value>& args);
68-
static void PathUnlinkFile(const v8::FunctionCallbackInfo<v8::Value>& args);
69-
static void PollOneoff(const v8::FunctionCallbackInfo<v8::Value>& args);
70-
static void ProcExit(const v8::FunctionCallbackInfo<v8::Value>& args);
71-
static void ProcRaise(const v8::FunctionCallbackInfo<v8::Value>& args);
72-
static void RandomGet(const v8::FunctionCallbackInfo<v8::Value>& args);
73-
static void SchedYield(const v8::FunctionCallbackInfo<v8::Value>& args);
74-
static void SockRecv(const v8::FunctionCallbackInfo<v8::Value>& args);
75-
static void SockSend(const v8::FunctionCallbackInfo<v8::Value>& args);
76-
static void SockShutdown(const v8::FunctionCallbackInfo<v8::Value>& args);
31+
static uint32_t ArgsGet(WASI&, WasmMemory, uint32_t, uint32_t);
32+
static uint32_t ArgsSizesGet(WASI&, WasmMemory, uint32_t, uint32_t);
33+
static uint32_t ClockResGet(WASI&, WasmMemory, uint32_t, uint32_t);
34+
static uint32_t ClockTimeGet(WASI&, WasmMemory, uint32_t, uint64_t, uint32_t);
35+
static uint32_t EnvironGet(WASI&, WasmMemory, uint32_t, uint32_t);
36+
static uint32_t EnvironSizesGet(WASI&, WasmMemory, uint32_t, uint32_t);
37+
static uint32_t FdAdvise(
38+
WASI&, WasmMemory, uint32_t, uint64_t, uint64_t, uint32_t);
39+
static uint32_t FdAllocate(WASI&, WasmMemory, uint32_t, uint64_t, uint64_t);
40+
static uint32_t FdClose(WASI&, WasmMemory, uint32_t);
41+
static uint32_t FdDatasync(WASI&, WasmMemory, uint32_t);
42+
static uint32_t FdFdstatGet(WASI&, WasmMemory, uint32_t, uint32_t);
43+
static uint32_t FdFdstatSetFlags(WASI&, WasmMemory, uint32_t, uint32_t);
44+
static uint32_t FdFdstatSetRights(
45+
WASI&, WasmMemory, uint32_t, uint64_t, uint64_t);
46+
static uint32_t FdFilestatGet(WASI&, WasmMemory, uint32_t, uint32_t);
47+
static uint32_t FdFilestatSetSize(WASI&, WasmMemory, uint32_t, uint64_t);
48+
static uint32_t FdFilestatSetTimes(
49+
WASI&, WasmMemory, uint32_t, uint64_t, uint64_t, uint32_t);
50+
static uint32_t FdPread(WASI&,
51+
WasmMemory memory,
52+
uint32_t,
53+
uint32_t,
54+
uint32_t,
55+
uint64_t,
56+
uint32_t);
57+
static uint32_t FdPrestatGet(WASI&, WasmMemory, uint32_t, uint32_t);
58+
static uint32_t FdPrestatDirName(
59+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t);
60+
static uint32_t FdPwrite(
61+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t, uint64_t, uint32_t);
62+
static uint32_t FdRead(
63+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t, uint32_t);
64+
static uint32_t FdReaddir(
65+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t, uint64_t, uint32_t);
66+
static uint32_t FdRenumber(WASI&, WasmMemory, uint32_t, uint32_t);
67+
static uint32_t FdSeek(
68+
WASI&, WasmMemory, uint32_t, int64_t, uint32_t, uint32_t);
69+
static uint32_t FdSync(WASI&, WasmMemory, uint32_t);
70+
static uint32_t FdTell(WASI&, WasmMemory, uint32_t, uint32_t);
71+
static uint32_t FdWrite(
72+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t, uint32_t);
73+
static uint32_t PathCreateDirectory(
74+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t);
75+
static uint32_t PathFilestatGet(
76+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
77+
static uint32_t PathFilestatSetTimes(WASI&,
78+
WasmMemory,
79+
uint32_t,
80+
uint32_t,
81+
uint32_t,
82+
uint32_t,
83+
uint64_t,
84+
uint64_t,
85+
uint32_t);
86+
static uint32_t PathLink(WASI&,
87+
WasmMemory,
88+
uint32_t,
89+
uint32_t,
90+
uint32_t,
91+
uint32_t,
92+
uint32_t,
93+
uint32_t,
94+
uint32_t);
95+
static uint32_t PathOpen(WASI&,
96+
WasmMemory,
97+
uint32_t,
98+
uint32_t,
99+
uint32_t,
100+
uint32_t,
101+
uint32_t,
102+
uint64_t,
103+
uint64_t,
104+
uint32_t,
105+
uint32_t);
106+
static uint32_t PathReadlink(WASI&,
107+
WasmMemory,
108+
uint32_t,
109+
uint32_t,
110+
uint32_t,
111+
uint32_t,
112+
uint32_t,
113+
uint32_t);
114+
static uint32_t PathRemoveDirectory(
115+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t);
116+
static uint32_t PathRename(WASI&,
117+
WasmMemory,
118+
uint32_t,
119+
uint32_t,
120+
uint32_t,
121+
uint32_t,
122+
uint32_t,
123+
uint32_t);
124+
static uint32_t PathSymlink(
125+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
126+
static uint32_t PathUnlinkFile(
127+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t);
128+
static uint32_t PollOneoff(
129+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t, uint32_t);
130+
static void ProcExit(WASI&, WasmMemory, uint32_t);
131+
static uint32_t ProcRaise(WASI&, WasmMemory, uint32_t);
132+
static uint32_t RandomGet(WASI&, WasmMemory, uint32_t, uint32_t);
133+
static uint32_t SchedYield(WASI&, WasmMemory);
134+
static uint32_t SockRecv(WASI&,
135+
WasmMemory,
136+
uint32_t,
137+
uint32_t,
138+
uint32_t,
139+
uint32_t,
140+
uint32_t,
141+
uint32_t);
142+
static uint32_t SockSend(
143+
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
144+
static uint32_t SockShutdown(WASI&, WasmMemory, uint32_t, uint32_t);
77145

78146
static void _SetMemory(const v8::FunctionCallbackInfo<v8::Value>& args);
79147

@@ -82,17 +150,24 @@ class WASI : public BaseObject,
82150
void IncreaseAllocatedSize(size_t size);
83151
void DecreaseAllocatedSize(size_t size);
84152

153+
// <typename FT, FT F> as a C++14 desugaring of `<auto F>`
154+
template <typename FT, FT F, typename R, typename... Args>
155+
class WasiFunction {
156+
public:
157+
static void SetFunction(Environment*,
158+
const char*,
159+
v8::Local<v8::FunctionTemplate>);
160+
161+
private:
162+
static R FastCallback(v8::Local<v8::Object> receiver,
163+
Args...,
164+
v8::FastApiCallbackOptions&);
165+
166+
static void SlowCallback(const v8::FunctionCallbackInfo<v8::Value>&);
167+
};
168+
85169
private:
86170
~WASI() override;
87-
inline void readUInt8(char* memory, uint8_t* value, uint32_t offset);
88-
inline void readUInt16(char* memory, uint16_t* value, uint32_t offset);
89-
inline void readUInt32(char* memory, uint32_t* value, uint32_t offset);
90-
inline void readUInt64(char* memory, uint64_t* value, uint32_t offset);
91-
inline void writeUInt8(char* memory, uint8_t value, uint32_t offset);
92-
inline void writeUInt16(char* memory, uint16_t value, uint32_t offset);
93-
inline void writeUInt32(char* memory, uint32_t value, uint32_t offset);
94-
inline void writeUInt64(char* memory, uint64_t value, uint32_t offset);
95-
uvwasi_errno_t backingStore(char** store, size_t* byte_length);
96171
uvwasi_t uvw_;
97172
v8::Global<v8::WasmMemoryObject> memory_;
98173
uvwasi_mem_t alloc_info_;

β€Žtest/wasi/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
CC = /opt/wasi-sdk/bin/clang
2-
TARGET = wasm32-unknown-wasi
2+
TARGET = wasm32-wasi
33
SYSROOT =
4+
CFLAGS = -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks
45

56
OBJ = $(patsubst c/%.c, wasm/%.wasm, $(wildcard c/*.c))
67
all: $(OBJ)
78

89
wasm/%.wasm : c/%.c
9-
$(CC) $< --target=$(TARGET) --sysroot=$(SYSROOT) -s -o $@
10+
$(CC) $< $(CFLAGS) --target=$(TARGET) --sysroot=$(SYSROOT) -s -o $@
1011

1112
.PHONY clean:
1213
rm -f $(OBJ)

β€Žtest/wasi/test-wasi.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ if (process.argv[2] === 'wasi-child') {
3535
const cp = require('child_process');
3636
const { checkoutEOL } = common;
3737

38-
function runWASI(options) {
38+
function innerRunWASI(options, args) {
3939
console.log('executing', options.test);
4040
const opts = {
4141
env: {
@@ -49,6 +49,7 @@ if (process.argv[2] === 'wasi-child') {
4949
opts.input = options.stdin;
5050

5151
const child = cp.spawnSync(process.execPath, [
52+
...args,
5253
'--experimental-wasi-unstable-preview1',
5354
__filename,
5455
'wasi-child',
@@ -60,6 +61,11 @@ if (process.argv[2] === 'wasi-child') {
6061
assert.strictEqual(child.stdout.toString(), options.stdout || '');
6162
}
6263

64+
function runWASI(options) {
65+
innerRunWASI(options, ['--no-turbo-fast-api-calls']);
66+
innerRunWASI(options, ['--turbo-fast-api-calls']);
67+
}
68+
6369
runWASI({ test: 'cant_dotdot' });
6470

6571
// Tests that are currently unsupported on IBM i PASE.

β€Žtest/wasi/wasm/cant_dotdot.wasm

-1.83 KB
Binary file not shown.

β€Žtest/wasi/wasm/clock_getres.wasm

-11.5 KB
Binary file not shown.

β€Žtest/wasi/wasm/create_symlink.wasm

-2.72 KB
Binary file not shown.

β€Žtest/wasi/wasm/exitcode.wasm

-11.6 KB
Binary file not shown.
-11.5 KB
Binary file not shown.

β€Žtest/wasi/wasm/follow_symlink.wasm

-2.9 KB
Binary file not shown.

β€Žtest/wasi/wasm/freopen.wasm

-2.53 KB
Binary file not shown.

β€Žtest/wasi/wasm/ftruncate.wasm

-1.84 KB
Binary file not shown.

β€Žtest/wasi/wasm/getentropy.wasm

-11.5 KB
Binary file not shown.

β€Žtest/wasi/wasm/getrusage.wasm

-11.5 KB
Binary file not shown.

β€Žtest/wasi/wasm/gettimeofday.wasm

-11.5 KB
Binary file not shown.

β€Žtest/wasi/wasm/link.wasm

-1.86 KB
Binary file not shown.

β€Žtest/wasi/wasm/main_args.wasm

-2.67 KB
Binary file not shown.

β€Žtest/wasi/wasm/notdir.wasm

-1.96 KB
Binary file not shown.

β€Žtest/wasi/wasm/poll.wasm

-1.76 KB
Binary file not shown.
-11.6 KB
Binary file not shown.

β€Žtest/wasi/wasm/read_file.wasm

-2.9 KB
Binary file not shown.
-2.9 KB
Binary file not shown.

β€Žtest/wasi/wasm/readdir.wasm

-1.45 KB
Binary file not shown.

β€Žtest/wasi/wasm/stat.wasm

-1.62 KB
Binary file not shown.

β€Žtest/wasi/wasm/stdin.wasm

-12.5 KB
Binary file not shown.

β€Žtest/wasi/wasm/symlink_escape.wasm

-1.85 KB
Binary file not shown.

β€Žtest/wasi/wasm/symlink_loop.wasm

-1.85 KB
Binary file not shown.

β€Žtest/wasi/wasm/write_file.wasm

-1.48 KB
Binary file not shown.

0 commit comments

Comments
Β (0)