summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/wasm/interpreter-mixed.js
blob: 27df605d4669049f52d0b87339badc61790c9d33 (plain)
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
// Copyright 2017 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.

// Flags: --allow-natives-syntax

load('test/mjsunit/wasm/wasm-module-builder.js');

// =============================================================================
// Tests in this file test the interaction between the wasm interpreter and
// compiled code.
// =============================================================================

// The stack trace contains file path, replace it by "file".
let stripPath = s => s.replace(/[^ (]*interpreter-mixed\.js/g, 'file');

function checkStack(stack, expected_lines) {
  print('stack: ' + stack);
  let lines = stack.split('\n');
  assertEquals(expected_lines.length, lines.length);
  for (let i = 0; i < lines.length; ++i) {
    let test =
        typeof expected_lines[i] == 'string' ? assertEquals : assertMatches;
    test(expected_lines[i], lines[i], 'line ' + i);
  }
}

(function testMemoryGrowBetweenInterpretedAndCompiled() {
  // grow_memory can be called from interpreted or compiled code, and changes
  // should be reflected in either execution.
  var builder = new WasmModuleBuilder();
  var grow_body = [kExprLocalGet, 0, kExprMemoryGrow, kMemoryZero];
  var load_body = [kExprLocalGet, 0, kExprI32LoadMem, 0, 0];
  var store_body = [kExprLocalGet, 0, kExprLocalGet, 1, kExprI32StoreMem, 0, 0];
  builder.addFunction('grow_memory', kSig_i_i).addBody(grow_body).exportFunc();
  builder.addFunction('load', kSig_i_i).addBody(load_body).exportFunc();
  builder.addFunction('store', kSig_v_ii).addBody(store_body).exportFunc();
  var grow_interp_function =
      builder.addFunction('grow_memory_interpreted', kSig_i_i)
          .addBody(grow_body)
          .exportFunc();
  var load_interp_function = builder.addFunction('load_interpreted', kSig_i_i)
                                 .addBody(load_body)
                                 .exportFunc();
  var kNumPages = 2;
  var kMaxPages = 10;
  builder.addMemory(kNumPages, kMaxPages, false);
  var instance = builder.instantiate();
  var exp = instance.exports;
  %RedirectToWasmInterpreter(instance, grow_interp_function.index);
  %RedirectToWasmInterpreter(instance, load_interp_function.index);

  // Initially, we can load from offset 12, but not OOB.
  var oob_index = kNumPages * kPageSize;
  var initial_interpreted = %WasmNumInterpretedCalls(instance);
  assertEquals(0, exp.load(12));
  assertEquals(0, exp.load_interpreted(12));
  assertTraps(kTrapMemOutOfBounds, () => exp.load(oob_index));
  assertTraps(
      kTrapMemOutOfBounds, () => exp.load_interpreted(oob_index));
  // Grow by 2 pages from compiled code, and ensure that this is reflected in
  // the interpreter.
  assertEquals(kNumPages, exp.grow_memory(2));
  kNumPages += 2;
  assertEquals(kNumPages, exp.grow_memory_interpreted(0));
  assertEquals(kNumPages, exp.grow_memory(0));
  // Now we can load from the previous OOB index.
  assertEquals(0, exp.load(oob_index));
  assertEquals(0, exp.load_interpreted(oob_index));
  // Set new OOB index and ensure that it traps.
  oob_index = kNumPages * kPageSize;
  assertTraps(kTrapMemOutOfBounds, () => exp.load(oob_index));
  assertTraps(
      kTrapMemOutOfBounds, () => exp.load_interpreted(oob_index));
  // Grow by another page in the interpreter, and ensure that this is reflected
  // in compiled code.
  assertEquals(kNumPages, exp.grow_memory_interpreted(1));
  kNumPages += 1;
  assertEquals(kNumPages, exp.grow_memory_interpreted(0));
  assertEquals(kNumPages, exp.grow_memory(0));
  // Now we can store to the previous OOB index and read it back in both
  // environments.
  exp.store(oob_index, 47);
  assertEquals(47, exp.load(oob_index));
  assertEquals(47, exp.load_interpreted(oob_index));
  // We cannot grow beyong kMaxPages.
  assertEquals(-1, exp.grow_memory(kMaxPages - kNumPages + 1));
  assertEquals(-1, exp.grow_memory_interpreted(kMaxPages - kNumPages + 1));
  // Overall, we executed 9 functions in the interpreter.
  assertEquals(initial_interpreted + 9, %WasmNumInterpretedCalls(instance));
})();

function createTwoInstancesCallingEachOther(inner_throws = false) {
  let builder1 = new WasmModuleBuilder();

  let id_imp = builder1.addImport('q', 'id', kSig_i_i);
  let plus_one = builder1.addFunction('plus_one', kSig_i_i)
                     .addBody([
                       kExprLocalGet, 0,  // -
                       kExprI32Const, 1,  // -
                       kExprI32Add,       // -
                       kExprCallFunction, id_imp
                     ])
                     .exportFunc();
  function imp(i) {
    if (inner_throws) throw new Error('i=' + i);
    return i;
  }
  let instance1 = builder1.instantiate({q: {id: imp}});

  let builder2 = new WasmModuleBuilder();

  let plus_one_imp = builder2.addImport('q', 'plus_one', kSig_i_i);
  let plus_two = builder2.addFunction('plus_two', kSig_i_i)
                     .addBody([
                       // Call import, add one more.
                       kExprLocalGet, 0,                 // -
                       kExprCallFunction, plus_one_imp,  // -
                       kExprI32Const, 1,                 // -
                       kExprI32Add
                     ])
                     .exportFunc();

  let instance2 =
      builder2.instantiate({q: {plus_one: instance1.exports.plus_one}});
  return [instance1, instance2];
}

function redirectToInterpreter(
    instance1, instance2, redirect_plus_one, redirect_plus_two) {
  // Redirect functions to the interpreter.
  if (redirect_plus_one) {
    %RedirectToWasmInterpreter(instance1,
                               parseInt(instance1.exports.plus_one.name));
  }
  if (redirect_plus_two) {
    %RedirectToWasmInterpreter(instance2,
                               parseInt(instance2.exports.plus_two.name));
  }
}

(function testImportFromOtherInstance() {
  print("testImportFromOtherInstance");
  // Three runs: Break in instance 1, break in instance 2, or both.
  for (let run = 0; run < 3; ++run) {
    print(" - run " + run);
    let [instance1, instance2] = createTwoInstancesCallingEachOther();

    let interpreted_before_1 = %WasmNumInterpretedCalls(instance1);
    let interpreted_before_2 = %WasmNumInterpretedCalls(instance2);
    // Call plus_two, which calls plus_one.
    assertEquals(9, instance2.exports.plus_two(7));

    // Nothing interpreted:
    assertEquals(interpreted_before_1, %WasmNumInterpretedCalls(instance1));
    assertEquals(interpreted_before_2, %WasmNumInterpretedCalls(instance2));

    // Now redirect functions to the interpreter.
    redirectToInterpreter(instance1, instance2, run != 1, run != 0);

    // Call plus_two, which calls plus_one.
    assertEquals(9, instance2.exports.plus_two(7));

    // TODO(6668): Fix patching of instances which imported others' code.
    //assertEquals(interpreted_before_1 + (run == 1 ? 0 : 1),
    //             %WasmNumInterpretedCalls(instance1));
    assertEquals(interpreted_before_2 + (run == 0 ? 0 : 1),
                 %WasmNumInterpretedCalls(instance2));
  }
})();

(function testStackTraceThroughCWasmEntry() {
  print("testStackTraceThroughCWasmEntry");
  for (let run = 0; run < 3; ++run) {
    print(" - run " + run);
    let [instance1, instance2] = createTwoInstancesCallingEachOther(true);
    redirectToInterpreter(instance1, instance2, run != 1, run != 0);

    try {
      // Call plus_two, which calls plus_one.
      instance2.exports.plus_two(7);
      assertUnreachable('should trap because of unreachable instruction');
    } catch (e) {
      checkStack(stripPath(e.stack), [
        'Error: i=8',                                                // -
        /^    at imp \(file:\d+:29\)$/,                              // -
        '    at plus_one (wasm-function[1]:0x3b)',                   // -
        '    at plus_two (wasm-function[1]:0x3e)',                   // -
        /^    at testStackTraceThroughCWasmEntry \(file:\d+:25\)$/,  // -
        /^    at file:\d+:3$/
      ]);
    }
  }
})();

(function testInterpreterPreservedOnTierUp() {
  print(arguments.callee.name);
  var builder = new WasmModuleBuilder();
  var fun_body = [kExprI32Const, 23];
  var fun = builder.addFunction('fun', kSig_i_v).addBody(fun_body).exportFunc();
  var instance = builder.instantiate();
  var exp = instance.exports;

  // Initially the interpreter is not being called.
  var initial_interpreted = %WasmNumInterpretedCalls(instance);
  assertEquals(23, exp.fun());
  assertEquals(initial_interpreted + 0, %WasmNumInterpretedCalls(instance));

  // Redirection will cause the interpreter to be called.
  %RedirectToWasmInterpreter(instance, fun.index);
  assertEquals(23, exp.fun());
  assertEquals(initial_interpreted + 1, %WasmNumInterpretedCalls(instance));

  // Requesting a tier-up still ensure the interpreter is being called.
  %WasmTierUpFunction(instance, fun.index);
  assertEquals(23, exp.fun());
  assertEquals(initial_interpreted + 2, %WasmNumInterpretedCalls(instance));
})();