test-wasm-trap-position.cc 5.5 KB
Newer Older
1 2 3 4
// Copyright 2016 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
#include "include/v8-function.h"
6
#include "src/api/api-inl.h"
7
#include "src/codegen/assembler-inl.h"
8
#include "src/objects/call-site-info-inl.h"
eholk's avatar
eholk committed
9
#include "src/trap-handler/trap-handler.h"
10 11 12
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/value-helper.h"
#include "test/cctest/wasm/wasm-run-utils.h"
13
#include "test/common/wasm/test-signatures.h"
14
#include "test/common/wasm/wasm-macro-gen.h"
15

16 17 18
namespace v8 {
namespace internal {
namespace wasm {
19
namespace test_wasm_trap_position {
20 21 22 23 24 25

using v8::Local;
using v8::Utils;

namespace {

26 27 28 29 30 31 32 33 34
#define CHECK_CSTREQ(exp, found)                                              \
  do {                                                                        \
    const char* exp_ = (exp);                                                 \
    const char* found_ = (found);                                             \
    DCHECK_NOT_NULL(exp);                                                     \
    if (V8_UNLIKELY(found_ == nullptr || strcmp(exp_, found_) != 0)) {        \
      FATAL("Check failed: (%s) != (%s) ('%s' vs '%s').", #exp, #found, exp_, \
            found_ ? found_ : "<null>");                                      \
    }                                                                         \
35
  } while (false)
36 37 38 39 40 41 42 43

struct ExceptionInfo {
  const char* func_name;
  int line_nr;
  int column;
};

template <int N>
44
void CheckExceptionInfos(v8::internal::Isolate* isolate, Handle<Object> exc,
45 46
                         const ExceptionInfo (&excInfos)[N]) {
  // Check that it's indeed an Error object.
47
  CHECK(exc->IsJSError());
48

49
  exc->Print();
50
  // Extract stack frame from the exception.
51
  auto stack = isolate->GetSimpleStackTrace(Handle<JSObject>::cast(exc));
52
  CHECK_EQ(N, stack->length());
53

54
  for (int i = 0; i < N; ++i) {
55 56 57
    Handle<CallSiteInfo> info(CallSiteInfo::cast(stack->get(i)), isolate);
    auto func_name =
        Handle<String>::cast(CallSiteInfo::GetFunctionName(info))->ToCString();
58
    CHECK_CSTREQ(excInfos[i].func_name, func_name.get());
59 60
    CHECK_EQ(excInfos[i].line_nr, CallSiteInfo::GetLineNumber(info));
    CHECK_EQ(excInfos[i].column, CallSiteInfo::GetColumnNumber(info));
61 62 63
  }
}

64 65
#undef CHECK_CSTREQ

66 67 68
}  // namespace

// Trigger a trap for executing unreachable.
69
WASM_COMPILED_EXEC_TEST(Unreachable) {
70
  // Create a WasmRunner with stack checks and traps enabled.
71
  WasmRunner<void> r(execution_tier, nullptr, "main", kRuntimeExceptionSupport);
72
  TestSignatures sigs;
73 74 75

  BUILD(r, WASM_UNREACHABLE);
  uint32_t wasm_index = r.function()->func_index;
76

77
  Handle<JSFunction> js_wasm_wrapper = r.builder().WrapCode(wasm_index);
78 79 80 81 82 83 84 85

  Handle<JSFunction> js_trampoline = Handle<JSFunction>::cast(
      v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
          CompileRun("(function callFn(fn) { fn(); })"))));

  Isolate* isolate = js_wasm_wrapper->GetIsolate();
  isolate->SetCaptureStackTraceForUncaughtExceptions(true, 10,
                                                     v8::StackTrace::kOverview);
86
  Handle<Object> global(isolate->context().global_object(), isolate);
87 88 89
  MaybeHandle<Object> maybe_exc;
  Handle<Object> args[] = {js_wasm_wrapper};
  MaybeHandle<Object> returnObjMaybe =
90 91
      Execution::TryCall(isolate, js_trampoline, global, 1, args,
                         Execution::MessageHandling::kReport, &maybe_exc);
92 93 94
  CHECK(returnObjMaybe.is_null());

  ExceptionInfo expected_exceptions[] = {
95 96
      {"main", 1, 7},    // --
      {"callFn", 1, 24}  // --
97
  };
98 99
  CheckExceptionInfos(isolate, maybe_exc.ToHandleChecked(),
                      expected_exceptions);
100 101 102
}

// Trigger a trap for loading from out-of-bounds.
103
WASM_COMPILED_EXEC_TEST(IllegalLoad) {
104
  WasmRunner<void> r(execution_tier, nullptr, "main", kRuntimeExceptionSupport);
105
  TestSignatures sigs;
106

107
  r.builder().AddMemory(0L);
108 109 110 111 112

  BUILD(r, WASM_IF(WASM_ONE, WASM_SEQ(WASM_LOAD_MEM(MachineType::Int32(),
                                                    WASM_I32V_1(-3)),
                                      WASM_DROP)));
  uint32_t wasm_index_1 = r.function()->func_index;
113

114
  WasmFunctionCompiler& f2 = r.NewFunction<void>("call_main");
115
  // Insert a NOP such that the position of the call is not one.
116 117
  BUILD(f2, WASM_NOP, WASM_CALL_FUNCTION0(wasm_index_1));
  uint32_t wasm_index_2 = f2.function_index();
118

119
  Handle<JSFunction> js_wasm_wrapper = r.builder().WrapCode(wasm_index_2);
120 121 122 123 124 125 126 127

  Handle<JSFunction> js_trampoline = Handle<JSFunction>::cast(
      v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
          CompileRun("(function callFn(fn) { fn(); })"))));

  Isolate* isolate = js_wasm_wrapper->GetIsolate();
  isolate->SetCaptureStackTraceForUncaughtExceptions(true, 10,
                                                     v8::StackTrace::kOverview);
128
  Handle<Object> global(isolate->context().global_object(), isolate);
129 130 131
  MaybeHandle<Object> maybe_exc;
  Handle<Object> args[] = {js_wasm_wrapper};
  MaybeHandle<Object> returnObjMaybe =
132 133
      Execution::TryCall(isolate, js_trampoline, global, 1, args,
                         Execution::MessageHandling::kReport, &maybe_exc);
134 135 136
  CHECK(returnObjMaybe.is_null());

  ExceptionInfo expected_exceptions[] = {
137 138 139
      {"main", 1, 13},       // --
      {"call_main", 1, 30},  // --
      {"callFn", 1, 24}      // --
140
  };
141 142
  CheckExceptionInfos(isolate, maybe_exc.ToHandleChecked(),
                      expected_exceptions);
143
}
144

145
}  // namespace test_wasm_trap_position
146 147 148
}  // namespace wasm
}  // namespace internal
}  // namespace v8