-
Notifications
You must be signed in to change notification settings - Fork 243
/
args_test.go
211 lines (187 loc) · 5.99 KB
/
args_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package wasi_snapshot_preview1
import (
"testing"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/internal/testing/require"
)
func Test_argsGet(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithArgs("a", "bc"))
defer r.Close(testCtx)
argv := uint32(7) // arbitrary offset
argvBuf := uint32(1) // arbitrary offset
expectedMemory := []byte{
'?', // argvBuf is after this
'a', 0, 'b', 'c', 0, // null terminated "a", "bc"
'?', // argv is after this
1, 0, 0, 0, // little endian-encoded offset of "a"
3, 0, 0, 0, // little endian-encoded offset of "bc"
'?', // stopped after encoding
}
maskMemory(t, testCtx, mod, len(expectedMemory))
// Invoke argsGet and check the memory side effects.
requireErrno(t, ErrnoSuccess, mod, functionArgsGet, uint64(argv), uint64(argvBuf))
require.Equal(t, `
--> proxy.args_get(argv=7,argv_buf=1)
==> wasi_snapshot_preview1.args_get(argv=7,argv_buf=1)
<== ESUCCESS
<-- (0)
`, "\n"+log.String())
actual, ok := mod.Memory().Read(testCtx, 0, uint32(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
func Test_argsGet_Errors(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithArgs("a", "bc"))
defer r.Close(testCtx)
memorySize := mod.Memory().Size(testCtx)
validAddress := uint32(0) // arbitrary valid address as arguments to args_get. We chose 0 here.
tests := []struct {
name string
argv, argvBuf uint32
expectedLog string
}{
{
name: "out-of-memory argv",
argv: memorySize,
argvBuf: validAddress,
expectedLog: `
--> proxy.args_get(argv=65536,argv_buf=0)
==> wasi_snapshot_preview1.args_get(argv=65536,argv_buf=0)
<== EFAULT
<-- (21)
`,
},
{
name: "out-of-memory argvBuf",
argv: validAddress,
argvBuf: memorySize,
expectedLog: `
--> proxy.args_get(argv=0,argv_buf=65536)
==> wasi_snapshot_preview1.args_get(argv=0,argv_buf=65536)
<== EFAULT
<-- (21)
`,
},
{
name: "argv exceeds the maximum valid address by 1",
// 4*argCount is the size of the result of the pointers to args, 4 is the size of uint32
argv: memorySize - 4*2 + 1,
argvBuf: validAddress,
expectedLog: `
--> proxy.args_get(argv=65529,argv_buf=0)
==> wasi_snapshot_preview1.args_get(argv=65529,argv_buf=0)
<== EFAULT
<-- (21)
`,
},
{
name: "argvBuf exceeds the maximum valid address by 1",
argv: validAddress,
// "a", "bc" size = size of "a0bc0" = 5
argvBuf: memorySize - 5 + 1,
expectedLog: `
--> proxy.args_get(argv=0,argv_buf=65532)
==> wasi_snapshot_preview1.args_get(argv=0,argv_buf=65532)
<== EFAULT
<-- (21)
`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
requireErrno(t, ErrnoFault, mod, functionArgsGet, uint64(tc.argv), uint64(tc.argvBuf))
require.Equal(t, tc.expectedLog, "\n"+log.String())
})
}
}
func Test_argsSizesGet(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithArgs("a", "bc"))
defer r.Close(testCtx)
resultArgc := uint32(1) // arbitrary offset
resultArgvBufSize := uint32(6) // arbitrary offset
expectedMemory := []byte{
'?', // resultArgc is after this
0x2, 0x0, 0x0, 0x0, // little endian-encoded arg count
'?', // resultArgvBufSize is after this
0x5, 0x0, 0x0, 0x0, // little endian-encoded size of null terminated strings
'?', // stopped after encoding
}
maskMemory(t, testCtx, mod, len(expectedMemory))
// Invoke argsSizesGet and check the memory side effects.
requireErrno(t, ErrnoSuccess, mod, functionArgsSizesGet, uint64(resultArgc), uint64(resultArgvBufSize))
require.Equal(t, `
--> proxy.args_sizes_get(result.argc=1,result.argv_buf_size=6)
==> wasi_snapshot_preview1.args_sizes_get(result.argc=1,result.argv_buf_size=6)
<== ESUCCESS
<-- (0)
`, "\n"+log.String())
actual, ok := mod.Memory().Read(testCtx, 0, uint32(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
func Test_argsSizesGet_Errors(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithArgs("a", "bc"))
defer r.Close(testCtx)
memorySize := mod.Memory().Size(testCtx)
validAddress := uint32(0) // arbitrary valid address as arguments to args_sizes_get. We chose 0 here.
tests := []struct {
name string
argc, argvBufSize uint32
expectedLog string
}{
{
name: "out-of-memory argc",
argc: memorySize,
argvBufSize: validAddress,
expectedLog: `
--> proxy.args_sizes_get(result.argc=65536,result.argv_buf_size=0)
==> wasi_snapshot_preview1.args_sizes_get(result.argc=65536,result.argv_buf_size=0)
<== EFAULT
<-- (21)
`,
},
{
name: "out-of-memory argvBufSize",
argc: validAddress,
argvBufSize: memorySize,
expectedLog: `
--> proxy.args_sizes_get(result.argc=0,result.argv_buf_size=65536)
==> wasi_snapshot_preview1.args_sizes_get(result.argc=0,result.argv_buf_size=65536)
<== EFAULT
<-- (21)
`,
},
{
name: "argc exceeds the maximum valid address by 1",
argc: memorySize - 4 + 1, // 4 is the size of uint32, the type of the count of args
argvBufSize: validAddress,
expectedLog: `
--> proxy.args_sizes_get(result.argc=65533,result.argv_buf_size=0)
==> wasi_snapshot_preview1.args_sizes_get(result.argc=65533,result.argv_buf_size=0)
<== EFAULT
<-- (21)
`,
},
{
name: "argvBufSize exceeds the maximum valid size by 1",
argc: validAddress,
argvBufSize: memorySize - 4 + 1, // 4 is count of bytes to encode uint32le
expectedLog: `
--> proxy.args_sizes_get(result.argc=0,result.argv_buf_size=65533)
==> wasi_snapshot_preview1.args_sizes_get(result.argc=0,result.argv_buf_size=65533)
<== EFAULT
<-- (21)
`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
requireErrno(t, ErrnoFault, mod, functionArgsSizesGet, uint64(tc.argc), uint64(tc.argvBufSize))
require.Equal(t, tc.expectedLog, "\n"+log.String())
})
}
}