1 // Copyright 2019 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "test/wasm-api-tests/wasm-api-test.h"
6 
7 #include "src/execution/isolate.h"
8 #include "src/wasm/c-api.h"
9 #include "src/wasm/module-decoder.h"
10 #include "src/wasm/wasm-engine.h"
11 
12 #include <iostream>
13 
14 namespace v8 {
15 namespace internal {
16 namespace wasm {
17 
18 using ::wasm::Frame;
19 using ::wasm::Message;
20 
21 namespace {
22 
FailCallback(void * env,const Val args[],Val results[])23 own<Trap> FailCallback(void* env, const Val args[], Val results[]) {
24   Store* store = reinterpret_cast<Store*>(env);
25   Message message = Message::make(std::string("callback abort"));
26   return Trap::make(store, message);
27 }
28 
ExpectMessage(const char * expected,const Message & message)29 void ExpectMessage(const char* expected, const Message& message) {
30   size_t len = strlen(expected);
31   EXPECT_EQ(len, message.size());
32   EXPECT_EQ(0, strncmp(expected, message.get(), len));
33 }
34 
35 }  // namespace
36 
TEST_F(WasmCapiTest,Traps)37 TEST_F(WasmCapiTest, Traps) {
38   FLAG_experimental_wasm_eh = true;
39   ValueType i32_type[] = {kWasmI32};
40   FunctionSig sig(1, 0, i32_type);
41   uint32_t callback_index =
42       builder()->AddImport(base::CStrVector("callback"), &sig);
43   byte code[] = {WASM_CALL_FUNCTION0(callback_index)};
44   AddExportedFunction(base::CStrVector("callback"), code, sizeof(code), &sig);
45 
46   byte code2[] = {WASM_CALL_FUNCTION0(3)};
47   AddExportedFunction(base::CStrVector("unreachable"), code2, sizeof(code2),
48                       &sig);
49   // The first constant is a 4-byte dummy so that the {unreachable} trap
50   // has a more interesting offset. This is called by code2.
51   byte code3[] = {WASM_I32V_3(0), WASM_UNREACHABLE, WASM_I32V_1(1)};
52   AddFunction(code3, sizeof(code3), &sig);
53 
54   // Check that traps returned from a C callback are uncatchable in Wasm.
55   byte code4[] = {WASM_TRY_CATCH_ALL_T(
56       kWasmI32, WASM_CALL_FUNCTION0(callback_index), WASM_I32V(42))};
57   AddExportedFunction(base::CStrVector("uncatchable"), code4, sizeof(code4),
58                       &sig);
59 
60   own<FuncType> func_type =
61       FuncType::make(ownvec<ValType>::make(),
62                      ownvec<ValType>::make(ValType::make(::wasm::I32)));
63   own<Func> cpp_callback = Func::make(store(), func_type.get(), FailCallback,
64                                       reinterpret_cast<void*>(store()));
65   Extern* imports[] = {cpp_callback.get()};
66   Instantiate(imports);
67 
68   // Use internal machinery to parse the module to find the function offsets.
69   // This makes the test more robust than hardcoding them.
70   i::Isolate* isolate =
71       reinterpret_cast<::wasm::StoreImpl*>(store())->i_isolate();
72   ModuleResult result = DecodeWasmModule(
73       WasmFeatures::All(), wire_bytes()->begin(), wire_bytes()->end(), false,
74       ModuleOrigin::kWasmOrigin, isolate->counters(),
75       isolate->metrics_recorder(), v8::metrics::Recorder::ContextId::Empty(),
76       DecodingMethod::kSync, GetWasmEngine()->allocator());
77   ASSERT_TRUE(result.ok());
78   const WasmFunction* func1 = &result.value()->functions[1];
79   const WasmFunction* func2 = &result.value()->functions[2];
80   const WasmFunction* func3 = &result.value()->functions[3];
81   const uint32_t func1_offset = func1->code.offset();
82   const uint32_t func2_offset = func2->code.offset();
83   const uint32_t func3_offset = func3->code.offset();
84 
85   Func* cpp_trapping_func = GetExportedFunction(0);
86   own<Trap> cpp_trap = cpp_trapping_func->call();
87   EXPECT_NE(nullptr, cpp_trap.get());
88   ExpectMessage("Uncaught Error: callback abort", cpp_trap->message());
89   own<Frame> frame = cpp_trap->origin();
90   EXPECT_TRUE(frame->instance()->same(instance()));
91   EXPECT_EQ(1u, frame->func_index());
92   EXPECT_EQ(1u, frame->func_offset());
93   EXPECT_EQ(func1_offset + frame->func_offset(), frame->module_offset());
94   ownvec<Frame> trace = cpp_trap->trace();
95   EXPECT_EQ(1u, trace.size());
96   frame.reset(trace[0].release());
97   EXPECT_TRUE(frame->instance()->same(instance()));
98   EXPECT_EQ(1u, frame->func_index());
99   EXPECT_EQ(1u, frame->func_offset());
100   EXPECT_EQ(func1_offset + frame->func_offset(), frame->module_offset());
101 
102   Func* wasm_trapping_func = GetExportedFunction(1);
103   own<Trap> wasm_trap = wasm_trapping_func->call();
104   EXPECT_NE(nullptr, wasm_trap.get());
105   ExpectMessage("Uncaught RuntimeError: unreachable", wasm_trap->message());
106   frame = wasm_trap->origin();
107   EXPECT_TRUE(frame->instance()->same(instance()));
108   EXPECT_EQ(3u, frame->func_index());
109   EXPECT_EQ(5u, frame->func_offset());
110   EXPECT_EQ(func3_offset + frame->func_offset(), frame->module_offset());
111   trace = wasm_trap->trace();
112   EXPECT_EQ(2u, trace.size());
113 
114   frame.reset(trace[0].release());
115   EXPECT_TRUE(frame->instance()->same(instance()));
116   EXPECT_EQ(3u, frame->func_index());
117   EXPECT_EQ(5u, frame->func_offset());
118   EXPECT_EQ(func3_offset + frame->func_offset(), frame->module_offset());
119 
120   frame.reset(trace[1].release());
121   EXPECT_TRUE(frame->instance()->same(instance()));
122   EXPECT_EQ(2u, frame->func_index());
123   EXPECT_EQ(1u, frame->func_offset());
124   EXPECT_EQ(func2_offset + frame->func_offset(), frame->module_offset());
125 
126   Func* wasm_uncatchable_func = GetExportedFunction(2);
127   Val* args = nullptr;
128   Val results[1] = {Val(3.14)};  // Sentinel value.
129   own<Trap> uncatchable_trap = wasm_uncatchable_func->call(args, results);
130   EXPECT_NE(nullptr, uncatchable_trap.get());
131   EXPECT_EQ(::wasm::F64, results[0].kind());
132 }
133 
134 }  // namespace wasm
135 }  // namespace internal
136 }  // namespace v8
137