wasm-stepping-liftoff.js 5.11 KB
Newer Older
1 2 3 4
// Copyright 2020 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.

5 6
utils.load('test/inspector/wasm-inspector-test.js');

7 8
let {session, contextGroup, Protocol} =
    InspectorTest.start('Tests stepping through wasm scripts by byte offsets');
9
session.setupScriptMap();
10 11 12 13 14 15 16

var builder = new WasmModuleBuilder();

var func_a_idx =
    builder.addFunction('wasm_A', kSig_v_i).addBody([kExprNop, kExprNop]).index;

// wasm_B calls wasm_A <param0> times.
17
var func_b = builder.addFunction('wasm_B', kSig_v_i)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    .addBody([
      // clang-format off
      kExprLoop, kWasmStmt,               // while
        kExprLocalGet, 0,                 // -
        kExprIf, kWasmStmt,               // if <param0> != 0
          kExprLocalGet, 0,               // -
          kExprI32Const, 1,               // -
          kExprI32Sub,                    // -
          kExprLocalSet, 0,               // decrease <param0>
          ...wasmI32Const(1024),          // some longer i32 const (2 byte imm)
          kExprCallFunction, func_a_idx,  // -
          kExprBr, 1,                     // continue
          kExprEnd,                       // -
        kExprEnd,                         // break
      // clang-format on
    ])
    .exportAs('main');

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
let fact = builder.addFunction('fact', kSig_i_i)
    .addLocals({i32_count: 1})
    .addBody([
    // clang-format off
    kExprLocalGet, 0,
    kExprIf, kWasmI32,               // if <param0> != 0
      kExprLocalGet, 0,
      kExprI32Const, 1,
      kExprI32Sub,
      kExprCallFunction, 2,
      kExprLocalGet, 0,
      kExprI32Mul,                   //   return fact(<param0> - 1) * <param0>
    kExprElse,                       // else
      kExprI32Const, 1,              //   return 1
    kExprEnd,
    // clang-format on
  ])
  .exportAs('fact');
54 55 56 57 58 59 60 61 62

var module_bytes = builder.toArray();

(async function test() {
  for (const action of ['stepInto', 'stepOver', 'stepOut', 'resume'])
    InspectorTest.logProtocolCommandCalls('Debugger.' + action);

  await Protocol.Debugger.enable();
  InspectorTest.log('Setting up global instance variable.');
63
  WasmInspectorTest.instantiate(module_bytes);
64 65 66 67 68 69
  const [, {params: wasmScript}] = await Protocol.Debugger.onceScriptParsed(2);

  InspectorTest.log('Got wasm script: ' + wasmScript.url);

  // Set the breakpoint on a non-breakable position. This should resolve to the
  // next instruction.
70
  var offset = func_b.body_offset + 15;
71
  InspectorTest.log(
72 73 74 75
      `Setting breakpoint on offset ` + offset + ` (should be propagated to ` +
        (offset + 1) + `, the offset of the call), url ${wasmScript.url}`);
  let bpmsg = await Protocol.Debugger.setBreakpoint({
    location: {scriptId: wasmScript.scriptId, lineNumber: 0, columnNumber: offset}
76 77
  });

78
  InspectorTest.logMessage(bpmsg.result.actualLocation);
79
  Protocol.Runtime.evaluate({ expression: 'instance.exports.main(4)' });
80 81 82 83
  await waitForPauseAndStep('stepInto');  // into call to wasm_A
  await waitForPauseAndStep('stepOver');  // over first nop
  await waitForPauseAndStep('stepOut');   // out of wasm_A
  await waitForPauseAndStep('stepOut');   // out of wasm_B, stop on breakpoint
84
  await waitForPauseAndStep('stepOver');  // over call
85
  await waitForPauseAndStep('stepInto');  // == stepOver br
86
  await waitForPauseAndStep('resume');    // to next breakpoint (3rd iteration)
87
  await waitForPauseAndStep('stepInto');  // into wasm_A
88
  await waitForPauseAndStep('stepOut');   // out to wasm_B
89 90 91 92 93 94
  // Now step 10 times, until we are in wasm_A again.
  for (let i = 0; i < 10; ++i) await waitForPauseAndStep('stepInto');
  // 3 more times, back to wasm_B.
  for (let i = 0; i < 3; ++i) await waitForPauseAndStep('stepInto');
  // Then just resume.
  await waitForPauseAndStep('resume');
95
  InspectorTest.log('exports.main returned!');
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

  InspectorTest.log('Test stepping over a recursive call');
  // Set a breakpoint at the recursive call and run.
  offset = fact.body_offset + 9; // Offset of the recursive call instruction.
  InspectorTest.log(
      `Setting breakpoint on the recursive call instruction @+` + offset +
      `, url ${wasmScript.url}`);
  bpmsg = await Protocol.Debugger.setBreakpoint({
    location: {scriptId: wasmScript.scriptId, lineNumber: 0, columnNumber: offset}
  });
  actualLocation = bpmsg.result.actualLocation;
  InspectorTest.logMessage(actualLocation);
  Protocol.Runtime.evaluate({ expression: 'instance.exports.fact(4)' });
  await waitForPause();

  // Remove the breakpoint before stepping over.
  InspectorTest.log('Removing breakpoint');
  let breakpointId = bpmsg.result.breakpointId;
  await Protocol.Debugger.removeBreakpoint({breakpointId});
  await Protocol.Debugger.stepOver();
  await waitForPauseAndStep('resume');
117 118 119 120 121
  InspectorTest.log('Finished!');
})().catch(reason => InspectorTest.log(`Failed: ${reason}`))
    .finally(InspectorTest.completeTest);

async function waitForPauseAndStep(stepAction) {
122 123 124 125 126
  await waitForPause();
  Protocol.Debugger[stepAction]();
}

async function waitForPause() {
127
  const {params: {callFrames}} = await Protocol.Debugger.oncePaused();
128
  await session.logSourceLocation(callFrames[0].location);
129
}