1 | /* |
2 | * Copyright (C) 2016-2019 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "ThrowScope.h" |
28 | |
29 | #include "Exception.h" |
30 | #include "JSCInlines.h" |
31 | #include "VM.h" |
32 | |
33 | namespace JSC { |
34 | |
35 | #if ENABLE(EXCEPTION_SCOPE_VERIFICATION) |
36 | |
37 | ThrowScope::ThrowScope(VM& vm, ExceptionEventLocation location) |
38 | : ExceptionScope(vm, location) |
39 | { |
40 | m_vm.verifyExceptionCheckNeedIsSatisfied(m_recursionDepth, m_location); |
41 | } |
42 | |
43 | ThrowScope::~ThrowScope() |
44 | { |
45 | RELEASE_ASSERT(m_vm.m_topExceptionScope); |
46 | |
47 | if (!m_isReleased) |
48 | m_vm.verifyExceptionCheckNeedIsSatisfied(m_recursionDepth, m_location); |
49 | else { |
50 | // If we released the scope, that means we're letting our callers do the |
51 | // exception check. However, because our caller may be a LLInt or JIT |
52 | // function (which always checks for exceptions but won't clear the |
53 | // m_needExceptionCheck bit), we should clear m_needExceptionCheck here |
54 | // and let code below decide if we need to simulate a re-throw. |
55 | m_vm.m_needExceptionCheck = false; |
56 | } |
57 | |
58 | bool willBeHandleByLLIntOrJIT = false; |
59 | const void* previousScopeStackPosition = m_previousScope ? m_previousScope->stackPosition() : nullptr; |
60 | void* topEntryFrame = m_vm.topEntryFrame; |
61 | |
62 | // If the topEntryFrame was pushed on the stack after the previousScope was instantiated, |
63 | // then this throwScope will be returning to LLINT or JIT code that always do an exception |
64 | // check. In that case, skip the simulated throw because the LLInt and JIT will be |
65 | // checking for the exception their own way instead of calling ThrowScope::exception(). |
66 | if (topEntryFrame && previousScopeStackPosition > topEntryFrame) |
67 | willBeHandleByLLIntOrJIT = true; |
68 | |
69 | if (!willBeHandleByLLIntOrJIT) |
70 | simulateThrow(); |
71 | } |
72 | |
73 | Exception* ThrowScope::throwException(JSGlobalObject* globalObject, Exception* exception) |
74 | { |
75 | if (m_vm.exception() && m_vm.exception() != exception) |
76 | m_vm.verifyExceptionCheckNeedIsSatisfied(m_recursionDepth, m_location); |
77 | |
78 | return m_vm.throwException(globalObject, exception); |
79 | } |
80 | |
81 | Exception* ThrowScope::throwException(JSGlobalObject* globalObject, JSValue error) |
82 | { |
83 | if (!error.isCell() || !jsDynamicCast<Exception*>(m_vm, error.asCell())) |
84 | m_vm.verifyExceptionCheckNeedIsSatisfied(m_recursionDepth, m_location); |
85 | |
86 | return m_vm.throwException(globalObject, error); |
87 | } |
88 | |
89 | Exception* ThrowScope::throwException(JSGlobalObject* globalObject, JSObject* obj) |
90 | { |
91 | m_vm.verifyExceptionCheckNeedIsSatisfied(m_recursionDepth, m_location); |
92 | return m_vm.throwException(globalObject, obj); |
93 | } |
94 | |
95 | void ThrowScope::simulateThrow() |
96 | { |
97 | RELEASE_ASSERT(m_vm.m_topExceptionScope); |
98 | m_vm.m_simulatedThrowPointLocation = m_location; |
99 | m_vm.m_simulatedThrowPointRecursionDepth = m_recursionDepth; |
100 | m_vm.m_needExceptionCheck = true; |
101 | if (UNLIKELY(Options::dumpSimulatedThrows())) |
102 | m_vm.m_nativeStackTraceOfLastSimulatedThrow = StackTrace::captureStackTrace(Options::unexpectedExceptionStackTraceLimit()); |
103 | } |
104 | |
105 | #endif // ENABLE(EXCEPTION_SCOPE_VERIFICATION) |
106 | |
107 | } // namespace JSC |
108 | |