2 * Copyright (C) 2016-2019 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "ThrowScope.h"
29 #include "Exception.h"
30 #include "JSCInlines.h"
35 #if ENABLE(EXCEPTION_SCOPE_VERIFICATION)
37 ThrowScope::ThrowScope(VM& vm, ExceptionEventLocation location)
38 : ExceptionScope(vm, location)
40 m_vm.verifyExceptionCheckNeedIsSatisfied(m_recursionDepth, m_location);
43 ThrowScope::~ThrowScope()
45 RELEASE_ASSERT(m_vm.m_topExceptionScope);
48 m_vm.verifyExceptionCheckNeedIsSatisfied(m_recursionDepth, m_location);
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;
58 bool willBeHandleByLLIntOrJIT = false;
59 const void* previousScopeStackPosition = m_previousScope ? m_previousScope->stackPosition() : nullptr;
60 void* topEntryFrame = m_vm.topEntryFrame;
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;
69 if (!willBeHandleByLLIntOrJIT)
73 Exception* ThrowScope::throwException(ExecState* exec, Exception* exception)
75 if (m_vm.exception() && m_vm.exception() != exception)
76 m_vm.verifyExceptionCheckNeedIsSatisfied(m_recursionDepth, m_location);
78 return m_vm.throwException(exec, exception);
81 Exception* ThrowScope::throwException(ExecState* exec, JSValue error)
83 if (!error.isCell() || !jsDynamicCast<Exception*>(m_vm, error.asCell()))
84 m_vm.verifyExceptionCheckNeedIsSatisfied(m_recursionDepth, m_location);
86 return m_vm.throwException(exec, error);
89 Exception* ThrowScope::throwException(ExecState* exec, JSObject* obj)
91 m_vm.verifyExceptionCheckNeedIsSatisfied(m_recursionDepth, m_location);
92 return m_vm.throwException(exec, obj);
95 void ThrowScope::simulateThrow()
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());
105 #endif // ENABLE(EXCEPTION_SCOPE_VERIFICATION)