2019-09-16 12:21:11 +00:00
|
|
|
// Copyright 2019 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-09-16 11:10:21 +00:00
|
|
|
// Flags: --allow-natives-syntax --randomize-all-allocations
|
2020-01-30 15:16:13 +00:00
|
|
|
// Flags: --wasm-max-initial-code-space-reservation=1
|
2019-09-16 12:21:11 +00:00
|
|
|
|
2020-08-12 15:22:16 +00:00
|
|
|
// Disable tier-up, to reduce execution time of this test (Liftoff generates
|
|
|
|
// much bigger code, thus reaches the four code spaces much faster).
|
|
|
|
// Flags: --no-wasm-tier-up
|
|
|
|
|
2019-09-16 12:21:11 +00:00
|
|
|
load('test/mjsunit/wasm/wasm-module-builder.js');
|
|
|
|
|
|
|
|
// Instantiate bigger modules, until at least four separate code spaces have
|
|
|
|
// been allocated.
|
|
|
|
// Each function calls through many of the previous functions to execute the
|
|
|
|
// jump table(s) sufficiently.
|
|
|
|
|
|
|
|
let num_functions = 50;
|
|
|
|
while (true) {
|
|
|
|
print(`Trying ${num_functions} functions...`);
|
|
|
|
if (num_functions > 1e6) {
|
|
|
|
throw new Error('We should have hit four code spaces by now');
|
|
|
|
}
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
builder.addMemory(1, 1, false);
|
2019-10-08 12:38:48 +00:00
|
|
|
builder.addFunction('f0', kSig_i_i).addBody([kExprLocalGet, 0]);
|
2019-09-16 12:21:11 +00:00
|
|
|
// Generate some code per function to fill the code space.
|
|
|
|
// Each function contains a number of loads that will not be executed
|
|
|
|
// (inside an "if (i == 0)" block). They increase the code size a bit so we
|
|
|
|
// do not need too many functions.
|
|
|
|
// Each function f<n> with argument {i} then calls f<n/10> with argument
|
|
|
|
// {i + 1} and returns whatever that function returns.
|
|
|
|
const body_template = [
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprLocalGet, 0, kExprI32Eqz, kExprIf, kWasmVoid, // if (i == 0)
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0 // get i
|
2019-09-16 12:21:11 +00:00
|
|
|
];
|
|
|
|
for (let i = 0; i < 1000; ++i) body_template.push(kExprI32LoadMem, 0, 0);
|
|
|
|
body_template.push(
|
|
|
|
kExprDrop, kExprEnd, // end if
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprI32Const, 1, kExprI32Add, // i + 1
|
2019-09-16 12:21:11 +00:00
|
|
|
kExprCallFunction // call f<?>
|
|
|
|
);
|
|
|
|
for (let i = 1; i < num_functions; ++i) {
|
|
|
|
const body = body_template.slice();
|
|
|
|
body.push(...wasmSignedLeb(Math.floor(i / 10)));
|
|
|
|
builder.addFunction('f' + i, kSig_i_i).addBody(body);
|
|
|
|
}
|
|
|
|
builder.addExport('f', num_functions - 1);
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
let expected = 17;
|
|
|
|
for (let i = num_functions - 1; i > 0; i = Math.floor(i / 10)) ++expected;
|
|
|
|
assertEquals(expected, instance.exports.f(17));
|
|
|
|
const num_code_spaces = %WasmNumCodeSpaces(instance);
|
|
|
|
print(`--> ${num_code_spaces} code spaces.`);
|
|
|
|
if (num_code_spaces >= 4) break;
|
|
|
|
num_functions *= 2;
|
|
|
|
}
|