OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/wasm/wasm-macro-gen.h" |
| 6 |
| 7 #include "test/cctest/cctest.h" |
| 8 #include "test/cctest/compiler/value-helper.h" |
| 9 #include "test/cctest/wasm/test-signatures.h" |
| 10 #include "test/cctest/wasm/wasm-run-utils.h" |
| 11 |
| 12 using namespace v8::base; |
| 13 using namespace v8::internal; |
| 14 using namespace v8::internal::compiler; |
| 15 using namespace v8::internal::wasm; |
| 16 |
| 17 using v8::Local; |
| 18 using v8::Utils; |
| 19 |
| 20 namespace { |
| 21 |
| 22 #define CHECK_CSTREQ(exp, found) \ |
| 23 do { \ |
| 24 const char* exp_ = (exp); \ |
| 25 const char* found_ = (found); \ |
| 26 if (V8_UNLIKELY(strcmp(exp_, found_) != 0)) { \ |
| 27 V8_Fatal(__FILE__, __LINE__, \ |
| 28 "Check failed: (%s) != (%s) ('%s' vs '%s').", #exp, #found, \ |
| 29 exp_, found_); \ |
| 30 } \ |
| 31 } while (0) |
| 32 |
| 33 void PrintStackTrace(v8::Local<v8::StackTrace> stack) { |
| 34 printf("Stack Trace (length %d):\n", stack->GetFrameCount()); |
| 35 for (int i = 0, e = stack->GetFrameCount(); i != e; ++i) { |
| 36 v8::Local<v8::StackFrame> frame = stack->GetFrame(i); |
| 37 v8::Local<v8::String> script = frame->GetScriptName(); |
| 38 v8::Local<v8::String> func = frame->GetFunctionName(); |
| 39 printf("[%d] (%s) %s:%d:%d\n", i, |
| 40 script.IsEmpty() ? "<null>" : *v8::String::Utf8Value(script), |
| 41 func.IsEmpty() ? "<null>" : *v8::String::Utf8Value(func), |
| 42 frame->GetLineNumber(), frame->GetColumn()); |
| 43 } |
| 44 } |
| 45 |
| 46 struct ExceptionInfo { |
| 47 const char* funcName; |
| 48 int lineNr; |
| 49 }; |
| 50 |
| 51 template <int N> |
| 52 void CheckExceptionInfos(Isolate* isolate, Handle<Object> exc, |
| 53 const ExceptionInfo (&excInfos)[N]) { |
| 54 // check that it's indeed an Error object |
| 55 CHECK(Object::IsErrorObject(isolate, exc)); |
| 56 |
| 57 // extract stack frame from exception |
| 58 Local<v8::Value> localExc = Utils::ToLocal(exc); |
| 59 v8::Local<v8::StackTrace> stack = v8::Exception::GetStackTrace(localExc); |
| 60 PrintStackTrace(stack); |
| 61 CHECK(!stack.IsEmpty()); |
| 62 CHECK_EQ(N, stack->GetFrameCount()); |
| 63 |
| 64 for (int frameNr = 0; frameNr < N; ++frameNr) { |
| 65 v8::Local<v8::StackFrame> frame = stack->GetFrame(frameNr); |
| 66 v8::String::Utf8Value funName(frame->GetFunctionName()); |
| 67 CHECK_CSTREQ(excInfos[frameNr].funcName, *funName); |
| 68 CHECK_EQ(excInfos[frameNr].lineNr, frame->GetLineNumber()); |
| 69 } |
| 70 } |
| 71 |
| 72 } // namespace |
| 73 |
| 74 // Call from JS to WASM to JS and throw an Error from JS. |
| 75 TEST(CollectDetailedWasmStack_ExplicitThrowFromJs) { |
| 76 TestSignatures sigs; |
| 77 TestingModule module; |
| 78 |
| 79 WasmFunctionCompiler comp1(sigs.v_v(), &module); |
| 80 |
| 81 uint32_t jsThrowingIndex = module.AddJsFunction( |
| 82 sigs.v_v(), |
| 83 "(function js() {\n function a() {\n throw new Error(); };\n a(); })"); |
| 84 |
| 85 BUILD(comp1, WASM_CALL_FUNCTION0(jsThrowingIndex)); |
| 86 uint32_t wasmIndex = comp1.CompileAndAdd(); |
| 87 |
| 88 WasmFunctionCompiler comp2(sigs.v_v(), &module); |
| 89 BUILD(comp2, WASM_CALL_FUNCTION0(wasmIndex)); |
| 90 uint32_t wasmIndex2 = comp2.CompileAndAdd(); |
| 91 |
| 92 Handle<JSFunction> jsWasmWrapper = module.WrapCode(wasmIndex2); |
| 93 |
| 94 Handle<JSFunction> jsTrampoline = Handle<JSFunction>::cast( |
| 95 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( |
| 96 CompileRun("(function callFn(fn) { fn(); })")))); |
| 97 |
| 98 Isolate* isolate = jsWasmWrapper->GetIsolate(); |
| 99 isolate->SetCaptureStackTraceForUncaughtExceptions(true, 10, |
| 100 v8::StackTrace::kOverview); |
| 101 Handle<Object> global(isolate->context()->global_object(), isolate); |
| 102 MaybeHandle<Object> excMaybe; |
| 103 Handle<Object> args[] = {jsWasmWrapper}; |
| 104 MaybeHandle<Object> returnObjMaybe = |
| 105 Execution::TryCall(isolate, jsTrampoline, global, 1, args, &excMaybe); |
| 106 CHECK(returnObjMaybe.is_null()); |
| 107 |
| 108 // line number is 1-based, with 0 == kNoLineNumberInfo |
| 109 ExceptionInfo expectedExceptions[] = { |
| 110 {"a", 3}, // comment to prevent clang-format complaints |
| 111 {"js", 4}, // - |
| 112 {"<WASM>", 0}, // - |
| 113 {"<WASM>", 0}, // - |
| 114 {"callFn", 1}}; |
| 115 CheckExceptionInfos(isolate, excMaybe.ToHandleChecked(), expectedExceptions); |
| 116 } |
OLD | NEW |