-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathJavascriptExceptionObject.cpp
More file actions
269 lines (237 loc) · 10.9 KB
/
JavascriptExceptionObject.cpp
File metadata and controls
269 lines (237 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeLanguagePch.h"
#include "Library/Functions/StackScriptFunction.h"
namespace Js
{
void JavascriptExceptionObject::FillError(Js::JavascriptExceptionContext& exceptionContext, ScriptContext *scriptContext, HostWrapperCreateFuncType hostWrapperCreateFunc)
{
this->scriptContext = scriptContext;
this->exceptionContext = exceptionContext;
this->SetHostWrapperCreateFunc(hostWrapperCreateFunc);
}
void JavascriptExceptionObject::ClearError()
{
Assert(this->isPendingExceptionObject);
memset(this, 0, sizeof(JavascriptExceptionObject));
this->isPendingExceptionObject = true;
}
JavascriptExceptionObject* JavascriptExceptionObject::CloneIfStaticExceptionObject(ScriptContext* scriptContext)
{
Assert(scriptContext);
ThreadContext *threadContext = scriptContext->GetThreadContext();
JavascriptExceptionObject* exceptionObject = this;
if (this == threadContext->GetPendingOOMErrorObject())
{
AssertMsg(this->thrownObject == NULL, "ThrownObject should be NULL since at time of OOM we will not be able to allocate the thrownObject");
// Let's hope that unwinding has released enough pointers that the
// recycler will find some memory to allocate the real OutOfMemory object.
// If not, it will rethrow outOfMemory
Var thrownObject = scriptContext->GetLibrary()->CreateOutOfMemoryError();
exceptionObject = RecyclerNew(scriptContext->GetRecycler(),
JavascriptExceptionObject,
thrownObject,
scriptContext,
&this->exceptionContext);
threadContext->ClearPendingOOMError();
}
if (this == threadContext->GetPendingSOErrorObject())
{
Var thrownObject = NULL;
if (this->thrownObject == NULL)
{
AssertMsg(threadContext->IsJSRT(), "ThrownObject could be NULL for Jsrt scenarios because it is cleared in ~EnterScriptEnd. For non-jsrt cases, we should always have an allocated thrown object.");
thrownObject = scriptContext->GetLibrary()->CreateStackOverflowError();
}
else
{
thrownObject = this->GetThrownObject(scriptContext);
}
exceptionObject = RecyclerNew(scriptContext->GetRecycler(),
JavascriptExceptionObject,
thrownObject,
scriptContext,
&this->exceptionContext);
threadContext->ClearPendingSOError();
}
return exceptionObject;
}
// Returns NULL if the exception object is the static out of memory object.
Var JavascriptExceptionObject::GetThrownObject(ScriptContext * requestingScriptContext)
{
// requestingScriptContext == this->scriptContext when we have A->(cross site thunk)B->(IDispatch)A using and nested A window return
// exception backup. we can go back down to normal code path below.
if (requestingScriptContext != nullptr && hostWrapperCreateFunc != nullptr && (requestingScriptContext != this->scriptContext))
{
return hostWrapperCreateFunc(thrownObject, scriptContext, requestingScriptContext);
}
// We can have cross script context throw in both fastDOM and IE8 mode now.
if (requestingScriptContext && (thrownObject != nullptr))
{
Var rethrownObject = CrossSite::MarshalVar(requestingScriptContext, thrownObject);
// For now, there is no known host for which we need to support cross-domain
// scenario for JSRT. So skip the cross domain check for now.
if (scriptContext->GetThreadContext()->IsJSRT())
{
return rethrownObject;
}
if (rethrownObject)
{
if (VarIs<JavascriptError>(rethrownObject))
{
JavascriptError* jsErrorObject = VarTo<JavascriptError>(rethrownObject);
if (jsErrorObject->GetScriptContext() != requestingScriptContext )
{
Assert(requestingScriptContext->GetHostScriptContext());
HRESULT hr = requestingScriptContext->GetHostScriptContext()->CheckCrossDomainScriptContext(jsErrorObject->GetScriptContext());
if ( S_OK != hr )
{
JavascriptError* jsNewErrorObject = requestingScriptContext->GetLibrary()->CreateTypeError();
JavascriptError::SetErrorMessage(jsNewErrorObject, VBSERR_PermissionDenied, nullptr, requestingScriptContext);
return jsNewErrorObject;
}
}
}
else
{
if (VarIs<RecyclableObject>(rethrownObject))
{
if (CrossSite::NeedMarshalVar(rethrownObject, requestingScriptContext))
{
Assert(requestingScriptContext->GetHostScriptContext());
HRESULT hrSecurityCheck = requestingScriptContext->GetHostScriptContext()->CheckCrossDomainScriptContext(((RecyclableObject*)rethrownObject)->GetScriptContext());
if (hrSecurityCheck != S_OK)
{
AssertMsg(hrSecurityCheck != E_ACCESSDENIED, "Invalid cross domain throw. HRESULT must either be S_OK or !E_ACCESSDENIED.");
// DOM should not throw cross domain object at all. This is defend in depth that we'll return something in requestScriptContext if they do throw
// something bad.
return requestingScriptContext->GetLibrary()->GetUndefined();
}
}
}
}
}
return rethrownObject;
}
return thrownObject;
}
FunctionBody* JavascriptExceptionObject::GetFunctionBody() const
{
// If it is a throwing function; it must be deserialized
if (exceptionContext.ThrowingFunction())
{
ParseableFunctionInfo *info = exceptionContext.ThrowingFunction()->GetParseableFunctionInfo();
if (info->IsFunctionBody())
{
return info->GetFunctionBody();
}
}
return nullptr;
}
JavascriptExceptionContext::StackFrame::StackFrame(JavascriptFunction* func, const JavascriptStackWalker& walker, bool initArgumentTypes)
{
this->functionBody = func->GetFunctionBody();
if (this->functionBody)
{
this->byteCodeOffset = walker.GetByteCodeOffset();
}
else
{
this->name = walker.GetCurrentNativeLibraryEntryName();
}
if (this->functionBody && initArgumentTypes)
{
this->argumentTypes.Init(walker);
}
}
bool JavascriptExceptionContext::StackFrame::IsScriptFunction() const
{
return functionBody != nullptr;
}
// Get function body -- available for script functions, null for native library builtin functions.
FunctionBody* JavascriptExceptionContext::StackFrame::GetFunctionBody() const
{
return functionBody;
}
LPCWSTR JavascriptExceptionContext::StackFrame::GetFunctionName() const
{
return IsScriptFunction() ?
GetFunctionBody()->GetExternalDisplayName() : PointerValue(this->name);
}
// Get function name with arguments info. Used by script WER.
HRESULT JavascriptExceptionContext::StackFrame::GetFunctionNameWithArguments(_In_ LPCWSTR *outResult) const
{
PCWSTR name = GetFunctionName();
HRESULT hr = S_OK;
if (IsScriptFunction())
{
hr = argumentTypes.ToString(name, functionBody->GetScriptContext(), outResult);
}
else
{
*outResult = name;
}
return hr;
}
void JavascriptExceptionContext::SetThrowingFunction(JavascriptFunction * function, uint32 byteCodeOffset, void * returnAddress)
{
// Unfortunately, window.onerror can ask for argument.callee.caller
// and we will return the thrown function, but the stack already unwound.
// We will need to just box the function
m_throwingFunction = StackScriptFunction::EnsureBoxed(BOX_PARAM(function, returnAddress, _u("throw")));
m_throwingFunctionByteCodeOffset = byteCodeOffset;
}
#if ENABLE_DEBUG_STACK_BACK_TRACE
void JavascriptExceptionObject::FillStackBackTrace()
{
// Note: this->scriptContext can be NULL when we throw Out Of Memory exception.
if (this->stackBackTrace == NULL && this->scriptContext != NULL)
{
Recycler* recycler = scriptContext->GetThreadContext()->GetRecycler();
HRESULT hr = NOERROR;
BEGIN_TRANSLATE_OOM_TO_HRESULT_NESTED
{
this->stackBackTrace = StackBackTrace::Capture(recycler, JavascriptExceptionObject::StackToSkip, JavascriptExceptionObject::StackTraceDepth);
}
END_TRANSLATE_OOM_TO_HRESULT(hr)
}
}
#endif
void JavascriptExceptionObject::Insert(
Field(JavascriptExceptionObject*)* head, JavascriptExceptionObject* item)
{
Assert(!item->next);
item->next = *head;
*head = item;
}
void JavascriptExceptionObject::Remove(
Field(JavascriptExceptionObject*)* head, JavascriptExceptionObject* item)
{
// Typically Insert/Remove happens in reversed order and item should be
// the front one. Loop the whole list to prevent unexpected order messup.
for (auto p = head; *p; p = &(*p)->next)
{
if (*p == item)
{
*p = item->next;
item->next = nullptr;
return;
}
}
Assert(false); // item not in list unexpected
}
//
// Support JavascriptException implementation
//
void SaveTempUncaughtException(ThreadContext* threadContext, Js::JavascriptExceptionObject* exceptionObject)
{
threadContext->SaveTempUncaughtException(exceptionObject);
}
void ClearTempUncaughtException(ThreadContext* threadContext, Js::JavascriptExceptionObject* exceptionObject)
{
threadContext->ClearTempUncaughtException(exceptionObject);
}
}