forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_memory.js
431 lines (391 loc) · 14.4 KB
/
test_memory.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import Builder from '../Builder.js';
import * as assert from '../assert.js';
const pageSize = 64 * 1024;
const maxPageCount = (2**32) / pageSize;
function binaryShouldNotParse(builder) {
const bin = builder.WebAssembly().get();
let threw = false;
try {
const module = new WebAssembly.Module(bin);
} catch(e) {
threw = true;
}
assert.truthy(threw);
}
{
// Can't declare more than one memory.
const builder = (new Builder())
.Type().End()
.Import().Memory("imp", "memory", {initial: 20}).End()
.Function().End()
.Memory().InitialMaxPages(1, 1).End()
.Export().End()
.Code()
.End();
binaryShouldNotParse(builder);
}
{
// Can't declare more than one memory.
const builder = (new Builder())
.Type().End()
.Import()
.Memory("imp", "memory", {initial: 20})
.Memory("imp", "memory", {initial: 30})
.End()
.Function().End()
.Export().End()
.Code()
.End();
binaryShouldNotParse(builder);
}
{
// initial must be <= maximum.
const builder = (new Builder())
.Type().End()
.Import()
.Memory("imp", "memory", {initial: 20, maximum: 19})
.End()
.Function().End()
.Export().End()
.Code()
.End();
binaryShouldNotParse(builder);
}
{
// No loads when no memory defined.
const builder = (new Builder())
.Type().End()
.Import().End()
.Function().End()
.Export().Function("foo").End()
.Code()
.Function("foo", { params: ["i32"], ret: "i32" })
.GetLocal(0)
.I32Load(2, 0)
.Return()
.End()
.End();
binaryShouldNotParse(builder);
}
{
// No stores when no memory defined.
const builder = (new Builder())
.Type().End()
.Import()
.End()
.Function().End()
.Export().Function("foo").End()
.Code()
.Function("foo", { params: ["i32"] })
.GetLocal(0)
.GetLocal(0)
.I32Store(2, 0)
.Return()
.End()
.End();
binaryShouldNotParse(builder);
}
{
// Can't export an undefined memory.
const builder = (new Builder())
.Type().End()
.Function().End()
.Export()
.Memory("memory", 0)
.End()
.Code()
.End();
binaryShouldNotParse(builder);
}
{
// Can't export a non-zero memory index.
const builder = (new Builder())
.Type().End()
.Import().Memory("imp", "memory", {initial: 20}).End()
.Function().End()
.Export()
.Memory("memory", 1)
.End()
.Code()
.End();
binaryShouldNotParse(builder);
}
{
assert.throws(() => new WebAssembly.Memory(20), TypeError, "WebAssembly.Memory expects its first argument to be an object");
new WebAssembly.Memory({initial:1}, {});
}
function test(f) {
noInline(f);
for (let i = 0; i < 2; i++) {
f(i);
}
}
test(function() {
const memoryDescription = {initial: 2, maximum: 2};
const builder = (new Builder())
.Type().End()
.Import().Memory("imp", "memory", memoryDescription).End()
.Function().End()
.Export()
.Function("foo")
.End()
.Code()
.Function("foo", { params: ["i32", "i32"], ret: "i32" })
.GetLocal(1)
.GetLocal(0)
.I32Store(2, 0)
.GetLocal(1)
.I32Load(2, 0)
.Return()
.End()
.End();
const bin = builder.WebAssembly().get();
const module = new WebAssembly.Module(bin);
const memory = new WebAssembly.Memory(memoryDescription);
const instance = new WebAssembly.Instance(module, { imp: { memory: memory } });
const foo = instance.exports.foo;
// foo(value, address)
const bytes = memoryDescription.initial * pageSize;
for (let i = 0; i < (bytes/4); i++) {
let value = i + 1;
let address = i * 4;
let result = foo(value, address);
assert.truthy(result === value);
let arrayBuffer = memory.buffer;
let buffer = new Uint32Array(arrayBuffer);
assert.truthy(buffer[i] === value);
}
});
test(function() {
const memoryDescription = {initial: 2, maximum: 2};
const builder = (new Builder())
.Type().End()
.Import().Memory("imp", "memory", memoryDescription).End()
.Function().End()
.Export()
.Function("foo")
.End()
.Code()
.Function("foo", { params: ["i32", "i32"], ret: "i32"})
.GetLocal(1)
.GetLocal(0)
.I32Store8(0, 0)
.GetLocal(1)
.I32Load8U(0, 0)
.Return()
.End()
.End();
const bin = builder.WebAssembly().get();
const module = new WebAssembly.Module(bin);
const memory = new WebAssembly.Memory(memoryDescription);
const instance = new WebAssembly.Instance(module, { imp: { memory: memory } });
const foo = instance.exports.foo;
// foo(value, address)
const bytes = memoryDescription.initial * pageSize;
for (let i = 0; i < bytes; i++) {
let value = (i + 1);
let address = i;
let result = foo(value, address);
let expectedValue = (value & ((2**8) - 1));
assert.truthy(result === expectedValue);
let arrayBuffer = memory.buffer;
let buffer = new Uint8Array(arrayBuffer);
assert.truthy(buffer[i] === expectedValue);
}
});
test(function() {
const memoryDescription = {initial: 2, maximum: 2};
const builder = (new Builder())
.Type().End()
.Import().Memory("imp", "memory", memoryDescription).End()
.Function().End()
.Export()
.Function("foo")
.End()
.Code()
.Function("foo", { params: ["f32", "i32"], ret: "f32"})
.GetLocal(1)
.GetLocal(0)
.F32Store(2, 0)
.GetLocal(1)
.F32Load(2, 0)
.Return()
.End()
.End();
const bin = builder.WebAssembly().get();
const module = new WebAssembly.Module(bin);
const memory = new WebAssembly.Memory(memoryDescription);
const instance = new WebAssembly.Instance(module, { imp: { memory: memory } });
const foo = instance.exports.foo;
// foo(value, address)
const bytes = memoryDescription.initial * pageSize;
for (let i = 0; i < (bytes/4); i++) {
let value = i + 1 + .0128213781289;
assert.truthy(value !== Math.fround(value));
let address = i * 4;
let result = foo(value, address);
let expectedValue = Math.fround(result);
assert.truthy(result === expectedValue);
let arrayBuffer = memory.buffer;
let buffer = new Float32Array(arrayBuffer);
assert.truthy(buffer[i] === expectedValue);
}
});
test(function() {
const memoryDescription = {initial: 2, maximum: 2};
const builder = (new Builder())
.Type().End()
.Import().Memory("imp", "memory", memoryDescription).End()
.Function().End()
.Export()
.Function("foo")
.End()
.Code()
.Function("foo", { params: ["f64", "i32"], ret: "f64"})
.GetLocal(1)
.GetLocal(0)
.F64Store(3, 0)
.GetLocal(1)
.F64Load(3, 0)
.Return()
.End()
.End();
const bin = builder.WebAssembly().get();
const module = new WebAssembly.Module(bin);
const memory = new WebAssembly.Memory(memoryDescription);
const instance = new WebAssembly.Instance(module, { imp: { memory: memory } });
const foo = instance.exports.foo;
// foo(value, address)
const bytes = memoryDescription.initial * pageSize;
for (let i = 0; i < (bytes/8); i++) {
let value = i + 1 + .0128213781289;
let address = i * 8;
let result = foo(value, address);
let expectedValue = result;
assert.truthy(result === expectedValue);
let arrayBuffer = memory.buffer;
let buffer = new Float64Array(arrayBuffer);
assert.truthy(buffer[i] === expectedValue);
}
});
test(function() {
const memoryDescription = {initial: 20, maximum: 25};
const builder = (new Builder())
.Type().End()
.Import().Memory("imp", "memory", memoryDescription).End()
.Function().End()
.Export()
.Function("foo")
.End()
.Code()
.Function("foo", { params: ["f64", "i32"], ret: "f64"})
.GetLocal(1)
.GetLocal(0)
.F64Store(3, 0)
.GetLocal(1)
.F64Load(3, 0)
.Return()
.End()
.End();
const bin = builder.WebAssembly().get();
const module = new WebAssembly.Module(bin);
assert.throws(() => new WebAssembly.Instance(module, 20), TypeError, `second argument to WebAssembly.Instance must be undefined or an Object`);
assert.throws(() => new WebAssembly.Instance(module, {}), TypeError, `import imp:memory must be an object`);
assert.throws(() => new WebAssembly.Instance(module, {imp: { } }), WebAssembly.LinkError, `Memory import imp:memory is not an instance of WebAssembly.Memory`);
assert.throws(() => new WebAssembly.Instance(module, {imp: { memory: 20 } }), WebAssembly.LinkError, `Memory import imp:memory is not an instance of WebAssembly.Memory`);
assert.throws(() => new WebAssembly.Instance(module, {imp: { memory: [] } }), WebAssembly.LinkError, `Memory import imp:memory is not an instance of WebAssembly.Memory`);
assert.throws(() => new WebAssembly.Instance(module, {imp: { memory: new WebAssembly.Memory({initial: 19, maximum: 25}) } }), WebAssembly.LinkError, `Memory import imp:memory provided a 'size' that is smaller than the module's declared 'initial' import memory size`);
assert.throws(() => new WebAssembly.Instance(module, {imp: { memory: new WebAssembly.Memory({initial: 20}) } }), WebAssembly.LinkError, `Memory import imp:memory did not have a 'maximum' but the module requires that it does`);
assert.throws(() => new WebAssembly.Instance(module, {imp: { memory: new WebAssembly.Memory({initial: 20, maximum: 26}) } }), WebAssembly.LinkError, `Memory import imp:memory provided a 'maximum' that is larger than the module's declared 'maximum' import memory size`);
});
test(function() {
const memoryDescription = {initial: 20};
const builder = (new Builder())
.Type().End()
.Import().Memory("imp", "memory", memoryDescription).End()
.Function().End()
.Export()
.Function("foo")
.End()
.Code()
.Function("foo", { params: ["f64", "i32"], ret: "f64"})
.GetLocal(1)
.GetLocal(0)
.F64Store(3, 0)
.GetLocal(1)
.F64Load(3, 0)
.Return()
.End()
.End();
const bin = builder.WebAssembly().get();
const module = new WebAssembly.Module(bin);
function testMemImportError(instanceObj, expectedError) {
assert.throws(() => new WebAssembly.Instance(module, instanceObj), WebAssembly.LinkError, expectedError);
}
testMemImportError({imp: { memory: new WebAssembly.Memory({initial: 19, maximum: 25}) } }, `Memory import imp:memory provided a 'size' that is smaller than the module's declared 'initial' import memory size`);
testMemImportError({imp: { memory: new WebAssembly.Memory({initial: 19}) } }, `Memory import imp:memory provided a 'size' that is smaller than the module's declared 'initial' import memory size`);
// This should not throw.
new WebAssembly.Instance(module, {imp: {memory: new WebAssembly.Memory({initial:20})}});
new WebAssembly.Instance(module, {imp: {memory: new WebAssembly.Memory({initial:20, maximum:20})}});
});
{
const builder = (new Builder())
.Type().End()
.Import().Memory("imp", "memory", {initial: 20}).End()
.Function().End()
.Export()
.Memory("memory", 0)
.Memory("memory2", 0)
.End()
.Code()
.End();
const bin = builder.WebAssembly().get();
const module = new WebAssembly.Module(bin);
const memory = new WebAssembly.Memory({initial: 20});
const instance = new WebAssembly.Instance(module, {imp: {memory}});
assert.truthy(memory === instance.exports.memory);
assert.truthy(memory === instance.exports.memory2);
}
{
const builder = (new Builder())
.Type().End()
.Function().End()
.Memory().InitialMaxPages(20, 25).End()
.Export()
.Memory("memory", 0)
.Memory("memory2", 0)
.End()
.Code()
.End();
const bin = builder.WebAssembly().get();
const module = new WebAssembly.Module(bin);
const instance = new WebAssembly.Instance(module);
assert.eq(instance.exports.memory, instance.exports.memory2);
assert.eq(instance.exports.memory.buffer.byteLength, 20 * pageSize);
assert.truthy(instance.exports.memory instanceof WebAssembly.Memory);
}
test(function() {
assert.throws(() => {
const m = new WebAssembly.Memory({minimum:40, maximum: 100});
m.type.call({});
}, TypeError, "WebAssembly.Memory.prototype.buffer getter called with non WebAssembly.Memory |this| value");
const memory = new WebAssembly.Memory({initial: 20});
assert.eq(Object.keys(memory.type()).length, 2);
assert.eq(memory.type().minimum, 20);
assert.eq(memory.type().shared, false);
const memory2 = new WebAssembly.Memory({minimum:40, maximum: 100});
assert.eq(Object.keys(memory2.type()).length, 3);
assert.eq(memory2.type().minimum, 40);
assert.eq(memory2.type().maximum, 100);
assert.eq(memory.type().shared, false);
const memory3 = new WebAssembly.Memory(memory2.type());
assert.eq(Object.keys(memory2.type()).length, Object.keys(memory3.type()).length);
assert.eq(memory2.type().minimum, memory3.type().minimum);
assert.eq(memory2.type().maximum, memory3.type().maximum);
assert.eq(memory.type().shared, false);
const memory4 = new WebAssembly.Memory({minimum: 10, maximum: 20, shared:true});
assert.eq(Object.keys(memory4.type()).length, 3);
assert.eq(memory4.type().minimum, 10);
assert.eq(memory4.type().maximum, 20);
assert.eq(memory4.type().shared, true);
})