| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/wasm/wasm-macro-gen.h" | 5 #include "src/wasm/wasm-macro-gen.h" |
| 6 | 6 |
| 7 #include "test/cctest/cctest.h" | 7 #include "test/cctest/cctest.h" |
| 8 #include "test/cctest/compiler/value-helper.h" | 8 #include "test/cctest/compiler/value-helper.h" |
| 9 #include "test/cctest/wasm/test-signatures.h" | 9 #include "test/cctest/wasm/test-signatures.h" |
| 10 #include "test/cctest/wasm/wasm-run-utils.h" | 10 #include "test/cctest/wasm/wasm-run-utils.h" |
| 11 | 11 |
| 12 using namespace v8::base; | 12 using namespace v8::base; |
| 13 using namespace v8::internal; | 13 using namespace v8::internal; |
| 14 using namespace v8::internal::compiler; | 14 using namespace v8::internal::compiler; |
| 15 using namespace v8::internal::wasm; | 15 using namespace v8::internal::wasm; |
| 16 | 16 |
| 17 using v8::Local; | 17 using v8::Local; |
| 18 using v8::Utils; | 18 using v8::Utils; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 #define CHECK_CSTREQ(exp, found) \ | 22 #define CHECK_CSTREQ(exp, found) \ |
| 23 do { \ | 23 do { \ |
| 24 const char* exp_ = (exp); \ | 24 const char* exp_ = (exp); \ |
| 25 const char* found_ = (found); \ | 25 const char* found_ = (found); \ |
| 26 if (V8_UNLIKELY(strcmp(exp_, found_) != 0)) { \ | 26 DCHECK_NOT_NULL(exp); \ |
| 27 if (V8_UNLIKELY(found_ == nullptr || strcmp(exp_, found_) != 0)) { \ |
| 27 V8_Fatal(__FILE__, __LINE__, \ | 28 V8_Fatal(__FILE__, __LINE__, \ |
| 28 "Check failed: (%s) != (%s) ('%s' vs '%s').", #exp, #found, \ | 29 "Check failed: (%s) != (%s) ('%s' vs '%s').", #exp, #found, \ |
| 29 exp_, found_); \ | 30 exp_, found_ ? found_ : "<null>"); \ |
| 30 } \ | 31 } \ |
| 31 } while (0) | 32 } while (0) |
| 32 | 33 |
| 33 void PrintStackTrace(v8::Local<v8::StackTrace> stack) { | 34 void PrintStackTrace(v8::Local<v8::StackTrace> stack) { |
| 34 printf("Stack Trace (length %d):\n", stack->GetFrameCount()); | 35 printf("Stack Trace (length %d):\n", stack->GetFrameCount()); |
| 35 for (int i = 0, e = stack->GetFrameCount(); i != e; ++i) { | 36 for (int i = 0, e = stack->GetFrameCount(); i != e; ++i) { |
| 36 v8::Local<v8::StackFrame> frame = stack->GetFrame(i); | 37 v8::Local<v8::StackFrame> frame = stack->GetFrame(i); |
| 37 v8::Local<v8::String> script = frame->GetScriptName(); | 38 v8::Local<v8::String> script = frame->GetScriptName(); |
| 38 v8::Local<v8::String> func = frame->GetFunctionName(); | 39 v8::Local<v8::String> func = frame->GetFunctionName(); |
| 39 printf("[%d] (%s) %s:%d:%d\n", i, | 40 printf("[%d] (%s) %s:%d:%d\n", i, |
| 40 script.IsEmpty() ? "<null>" : *v8::String::Utf8Value(script), | 41 script.IsEmpty() ? "<null>" : *v8::String::Utf8Value(script), |
| 41 func.IsEmpty() ? "<null>" : *v8::String::Utf8Value(func), | 42 func.IsEmpty() ? "<null>" : *v8::String::Utf8Value(func), |
| 42 frame->GetLineNumber(), frame->GetColumn()); | 43 frame->GetLineNumber(), frame->GetColumn()); |
| 43 } | 44 } |
| 44 } | 45 } |
| 45 | 46 |
| 46 struct ExceptionInfo { | 47 struct ExceptionInfo { |
| 47 const char* funcName; | 48 const char* func_name; |
| 48 int lineNr; | 49 int line_nr; |
| 50 int column; |
| 49 }; | 51 }; |
| 50 | 52 |
| 51 template <int N> | 53 template <int N> |
| 52 void CheckExceptionInfos(Isolate* isolate, Handle<Object> exc, | 54 void CheckExceptionInfos(Isolate* isolate, Handle<Object> exc, |
| 53 const ExceptionInfo (&excInfos)[N]) { | 55 const ExceptionInfo (&excInfos)[N]) { |
| 54 // Check that it's indeed an Error object. | 56 // Check that it's indeed an Error object. |
| 55 CHECK(Object::IsErrorObject(isolate, exc)); | 57 CHECK(Object::IsErrorObject(isolate, exc)); |
| 56 | 58 |
| 57 // Extract stack frame from the exception. | 59 // Extract stack frame from the exception. |
| 58 Local<v8::Value> localExc = Utils::ToLocal(exc); | 60 Local<v8::Value> localExc = Utils::ToLocal(exc); |
| 59 v8::Local<v8::StackTrace> stack = v8::Exception::GetStackTrace(localExc); | 61 v8::Local<v8::StackTrace> stack = v8::Exception::GetStackTrace(localExc); |
| 60 PrintStackTrace(stack); | 62 PrintStackTrace(stack); |
| 61 CHECK(!stack.IsEmpty()); | 63 CHECK(!stack.IsEmpty()); |
| 62 CHECK_EQ(N, stack->GetFrameCount()); | 64 CHECK_EQ(N, stack->GetFrameCount()); |
| 63 | 65 |
| 64 for (int frameNr = 0; frameNr < N; ++frameNr) { | 66 for (int frameNr = 0; frameNr < N; ++frameNr) { |
| 65 v8::Local<v8::StackFrame> frame = stack->GetFrame(frameNr); | 67 v8::Local<v8::StackFrame> frame = stack->GetFrame(frameNr); |
| 66 v8::String::Utf8Value funName(frame->GetFunctionName()); | 68 v8::String::Utf8Value funName(frame->GetFunctionName()); |
| 67 CHECK_CSTREQ(excInfos[frameNr].funcName, *funName); | 69 CHECK_CSTREQ(excInfos[frameNr].func_name, *funName); |
| 68 CHECK_EQ(excInfos[frameNr].lineNr, frame->GetLineNumber()); | 70 CHECK_EQ(excInfos[frameNr].line_nr, frame->GetLineNumber()); |
| 71 CHECK_EQ(excInfos[frameNr].column, frame->GetColumn()); |
| 69 } | 72 } |
| 70 } | 73 } |
| 71 | 74 |
| 72 } // namespace | 75 } // namespace |
| 73 | 76 |
| 74 // Call from JS to WASM to JS and throw an Error from JS. | 77 // Call from JS to WASM to JS and throw an Error from JS. |
| 75 TEST(CollectDetailedWasmStack_ExplicitThrowFromJs) { | 78 TEST(CollectDetailedWasmStack_ExplicitThrowFromJs) { |
| 76 TestSignatures sigs; | 79 TestSignatures sigs; |
| 77 TestingModule module; | 80 TestingModule module; |
| 78 | 81 |
| 79 // Initialize WasmFunctionCompiler first, since it sets up the HandleScope. | 82 // Initialize WasmFunctionCompiler first, since it sets up the HandleScope. |
| 80 WasmFunctionCompiler comp1(sigs.v_v(), &module); | 83 WasmFunctionCompiler comp1(sigs.v_v(), &module); |
| 81 | 84 |
| 82 uint32_t js_throwing_index = module.AddJsFunction( | 85 uint32_t js_throwing_index = module.AddJsFunction( |
| 83 sigs.v_v(), | 86 sigs.v_v(), |
| 84 "(function js() {\n function a() {\n throw new Error(); };\n a(); })"); | 87 "(function js() {\n function a() {\n throw new Error(); };\n a(); })"); |
| 85 | 88 |
| 86 BUILD(comp1, WASM_CALL_FUNCTION0(js_throwing_index)); | 89 // Add a nop such that we don't always get position 1. |
| 90 BUILD(comp1, WASM_NOP, WASM_CALL_FUNCTION0(js_throwing_index)); |
| 87 uint32_t wasm_index = comp1.CompileAndAdd(); | 91 uint32_t wasm_index = comp1.CompileAndAdd(); |
| 88 | 92 |
| 89 WasmFunctionCompiler comp2(sigs.v_v(), &module); | 93 WasmFunctionCompiler comp2(sigs.v_v(), &module); |
| 90 BUILD(comp2, WASM_CALL_FUNCTION0(wasm_index)); | 94 BUILD(comp2, WASM_CALL_FUNCTION0(wasm_index)); |
| 91 uint32_t wasm_index_2 = comp2.CompileAndAdd(); | 95 uint32_t wasm_index_2 = comp2.CompileAndAdd(); |
| 92 | 96 |
| 93 Handle<JSFunction> js_wasm_wrapper = module.WrapCode(wasm_index_2); | 97 Handle<JSFunction> js_wasm_wrapper = module.WrapCode(wasm_index_2); |
| 94 | 98 |
| 95 Handle<JSFunction> js_trampoline = Handle<JSFunction>::cast( | 99 Handle<JSFunction> js_trampoline = Handle<JSFunction>::cast( |
| 96 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( | 100 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( |
| 97 CompileRun("(function callFn(fn) { fn(); })")))); | 101 CompileRun("(function callFn(fn) { fn(); })")))); |
| 98 | 102 |
| 99 Isolate* isolate = js_wasm_wrapper->GetIsolate(); | 103 Isolate* isolate = js_wasm_wrapper->GetIsolate(); |
| 100 isolate->SetCaptureStackTraceForUncaughtExceptions(true, 10, | 104 isolate->SetCaptureStackTraceForUncaughtExceptions(true, 10, |
| 101 v8::StackTrace::kOverview); | 105 v8::StackTrace::kOverview); |
| 102 Handle<Object> global(isolate->context()->global_object(), isolate); | 106 Handle<Object> global(isolate->context()->global_object(), isolate); |
| 103 MaybeHandle<Object> maybe_exc; | 107 MaybeHandle<Object> maybe_exc; |
| 104 Handle<Object> args[] = {js_wasm_wrapper}; | 108 Handle<Object> args[] = {js_wasm_wrapper}; |
| 105 MaybeHandle<Object> returnObjMaybe = | 109 MaybeHandle<Object> returnObjMaybe = |
| 106 Execution::TryCall(isolate, js_trampoline, global, 1, args, &maybe_exc); | 110 Execution::TryCall(isolate, js_trampoline, global, 1, args, &maybe_exc); |
| 107 CHECK(returnObjMaybe.is_null()); | 111 CHECK(returnObjMaybe.is_null()); |
| 108 | 112 |
| 109 // Line number is 1-based, with 0 == kNoLineNumberInfo. | 113 // Line number is 1-based, with 0 == kNoLineNumberInfo. |
| 110 ExceptionInfo expected_exceptions[] = { | 114 ExceptionInfo expected_exceptions[] = { |
| 111 {"a", 3}, // Comment to prevent clang-format complaints | 115 {"a", 3, 8}, // - |
| 112 {"js", 4}, // - | 116 {"js", 4, 2}, // - |
| 113 {"<WASM>", 0}, // - | 117 {"<WASM>", static_cast<int>(wasm_index), 2}, // - |
| 114 {"<WASM>", 0}, // - | 118 {"<WASM>", static_cast<int>(wasm_index_2), 1}, // - |
| 115 {"callFn", 1}}; | 119 {"callFn", 1, 24} // - |
| 120 }; |
| 116 CheckExceptionInfos(isolate, maybe_exc.ToHandleChecked(), | 121 CheckExceptionInfos(isolate, maybe_exc.ToHandleChecked(), |
| 117 expected_exceptions); | 122 expected_exceptions); |
| 118 } | 123 } |
| 119 | 124 |
| 120 // Trigger a trap in WASM, stack should be JS -> WASM -> WASM. | 125 // Trigger a trap in WASM, stack should be JS -> WASM -> WASM. |
| 121 TEST(CollectDetailedWasmStack_WasmError) { | 126 TEST(CollectDetailedWasmStack_WasmError) { |
| 122 TestSignatures sigs; | 127 TestSignatures sigs; |
| 123 TestingModule module; | 128 TestingModule module; |
| 124 | 129 |
| 125 WasmFunctionCompiler comp1(sigs.i_v(), &module, | 130 WasmFunctionCompiler comp1(sigs.i_v(), &module, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 145 v8::StackTrace::kOverview); | 150 v8::StackTrace::kOverview); |
| 146 Handle<Object> global(isolate->context()->global_object(), isolate); | 151 Handle<Object> global(isolate->context()->global_object(), isolate); |
| 147 MaybeHandle<Object> maybe_exc; | 152 MaybeHandle<Object> maybe_exc; |
| 148 Handle<Object> args[] = {js_wasm_wrapper}; | 153 Handle<Object> args[] = {js_wasm_wrapper}; |
| 149 MaybeHandle<Object> maybe_return_obj = | 154 MaybeHandle<Object> maybe_return_obj = |
| 150 Execution::TryCall(isolate, js_trampoline, global, 1, args, &maybe_exc); | 155 Execution::TryCall(isolate, js_trampoline, global, 1, args, &maybe_exc); |
| 151 CHECK(maybe_return_obj.is_null()); | 156 CHECK(maybe_return_obj.is_null()); |
| 152 | 157 |
| 153 // Line number is 1-based, with 0 == kNoLineNumberInfo. | 158 // Line number is 1-based, with 0 == kNoLineNumberInfo. |
| 154 ExceptionInfo expected_exceptions[] = { | 159 ExceptionInfo expected_exceptions[] = { |
| 155 {"<WASM>", 0}, // Comment to prevent clang-format complaints. | 160 // TODO(clemens): position should be 1 |
| 156 {"<WASM>", 0}, | 161 {"<WASM>", static_cast<int>(wasm_index), -1}, // - |
| 157 {"callFn", 1}}; | 162 {"<WASM>", static_cast<int>(wasm_index_2), 1}, // - |
| 163 {"callFn", 1, 24} //- |
| 164 }; |
| 158 CheckExceptionInfos(isolate, maybe_exc.ToHandleChecked(), | 165 CheckExceptionInfos(isolate, maybe_exc.ToHandleChecked(), |
| 159 expected_exceptions); | 166 expected_exceptions); |
| 160 } | 167 } |
| OLD | NEW |