forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch.cpp
More file actions
535 lines (447 loc) · 16.6 KB
/
ch.cpp
File metadata and controls
535 lines (447 loc) · 16.6 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "core/AtomLockGuids.h"
unsigned int MessageBase::s_messageCount = 0;
DebuggerController* Debugger::debuggerController = nullptr;
LPCWSTR hostName = L"ch.exe";
extern "C"
HRESULT __stdcall OnChakraCoreLoadedEntry(TestHooks& testHooks)
{
return ChakraRTInterface::OnChakraCoreLoaded(testHooks);
}
JsRuntimeAttributes jsrtAttributes = JsRuntimeAttributeAllowScriptInterrupt;
LPCWSTR JsErrorCodeToString(JsErrorCode jsErrorCode)
{
switch (jsErrorCode)
{
case JsNoError:
return L"JsNoError";
break;
case JsErrorInvalidArgument:
return L"JsErrorInvalidArgument";
break;
case JsErrorNullArgument:
return L"JsErrorNullArgument";
break;
case JsErrorNoCurrentContext:
return L"JsErrorNoCurrentContext";
break;
case JsErrorInExceptionState:
return L"JsErrorInExceptionState";
break;
case JsErrorNotImplemented:
return L"JsErrorNotImplemented";
break;
case JsErrorWrongThread:
return L"JsErrorWrongThread";
break;
case JsErrorRuntimeInUse:
return L"JsErrorRuntimeInUse";
break;
case JsErrorBadSerializedScript:
return L"JsErrorBadSerializedScript";
break;
case JsErrorInDisabledState:
return L"JsErrorInDisabledState";
break;
case JsErrorCannotDisableExecution:
return L"JsErrorCannotDisableExecution";
break;
case JsErrorHeapEnumInProgress:
return L"JsErrorHeapEnumInProgress";
break;
case JsErrorOutOfMemory:
return L"JsErrorOutOfMemory";
break;
case JsErrorScriptException:
return L"JsErrorScriptException";
break;
case JsErrorScriptCompile:
return L"JsErrorScriptCompile";
break;
case JsErrorScriptTerminated:
return L"JsErrorScriptTerminated";
break;
case JsErrorFatal:
return L"JsErrorFatal";
break;
default:
return L"<unknown>";
break;
}
}
int HostExceptionFilter(int exceptionCode, _EXCEPTION_POINTERS *ep)
{
ChakraRTInterface::NotifyUnhandledException(ep);
bool crashOnException = false;
ChakraRTInterface::GetCrashOnExceptionFlag(&crashOnException);
if (exceptionCode == EXCEPTION_BREAKPOINT || (crashOnException && exceptionCode != 0xE06D7363))
{
return EXCEPTION_CONTINUE_SEARCH;
}
fwprintf(stderr, L"FATAL ERROR: %ls failed due to exception code %x\n", hostName, exceptionCode);
fflush(stderr);
return EXCEPTION_EXECUTE_HANDLER;
}
void __stdcall PrintUsageFormat()
{
wprintf(L"\nUsage: ch.exe [flaglist] filename\n");
}
void __stdcall PrintUsage()
{
PrintUsageFormat();
wprintf(L"Try 'ch.exe -?' for help\n");
}
// On success the param byteCodeBuffer will be allocated in the function.
// The caller of this function should de-allocate the memory.
HRESULT GetSerializedBuffer(LPCOLESTR fileContents, __out BYTE **byteCodeBuffer, __out DWORD *byteCodeBufferSize)
{
HRESULT hr = S_OK;
*byteCodeBuffer = nullptr;
*byteCodeBufferSize = 0;
BYTE *bcBuffer = nullptr;
DWORD bcBufferSize = 0;
IfJsErrorFailLog(ChakraRTInterface::JsSerializeScript(fileContents, bcBuffer, &bcBufferSize));
// Above call will return the size of the buffer only, once succeed we need to allocate memory of that much and call it again.
if (bcBufferSize == 0)
{
AssertMsg(false, "bufferSize should not be zero");
IfFailGo(E_FAIL);
}
bcBuffer = new BYTE[bcBufferSize];
DWORD newBcBufferSize = bcBufferSize;
IfJsErrorFailLog(ChakraRTInterface::JsSerializeScript(fileContents, bcBuffer, &newBcBufferSize));
Assert(bcBufferSize == newBcBufferSize);
Error:
if (hr != S_OK)
{
// In the failure release the buffer
if (bcBuffer != nullptr)
{
delete[] bcBuffer;
}
}
else
{
*byteCodeBuffer = bcBuffer;
*byteCodeBufferSize = bcBufferSize;
}
return hr;
}
HRESULT CreateLibraryByteCodeHeader(LPCOLESTR fileContents, BYTE * contentsRaw, DWORD lengthBytes, LPCWSTR bcFullPath, LPCWSTR libraryNameWide)
{
HRESULT hr = S_OK;
HANDLE bcFileHandle = nullptr;
BYTE *bcBuffer = nullptr;
DWORD bcBufferSize = 0;
IfFailGo(GetSerializedBuffer(fileContents, &bcBuffer, &bcBufferSize));
bcFileHandle = CreateFile(bcFullPath, GENERIC_WRITE, FILE_SHARE_DELETE, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (bcFileHandle == INVALID_HANDLE_VALUE)
{
IfFailGo(E_FAIL);
}
DWORD written;
// For validating the header file against the library file
auto outputStr =
"//-------------------------------------------------------------------------------------------------------\r\n"
"// Copyright (C) Microsoft. All rights reserved.\r\n"
"// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.\r\n"
"//-------------------------------------------------------------------------------------------------------\r\n"
"#if 0\r\n";
IfFalseGo(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr));
IfFalseGo(WriteFile(bcFileHandle, contentsRaw, lengthBytes, &written, nullptr));
if (lengthBytes < 2 || contentsRaw[lengthBytes - 2] != '\r' || contentsRaw[lengthBytes - 1] != '\n')
{
outputStr = "\r\n#endif\r\n";
}
else
{
outputStr = "#endif\r\n";
}
IfFalseGo(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr));
// Write out the bytecode
outputStr = "namespace Js\r\n{\r\n const char Library_Bytecode_";
IfFalseGo(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr));
size_t convertedChars;
char libraryNameNarrow[MAX_PATH + 1];
IfFalseGo((wcstombs_s(&convertedChars, libraryNameNarrow, libraryNameWide, _TRUNCATE) == 0));
IfFalseGo(WriteFile(bcFileHandle, libraryNameNarrow, (DWORD)strlen(libraryNameNarrow), &written, nullptr));
outputStr = "[] = {\r\n/* 00000000 */";
IfFalseGo(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr));
for (unsigned int i = 0; i < bcBufferSize; i++)
{
char scratch[6];
auto scratchLen = sizeof(scratch);
int num = _snprintf_s(scratch, scratchLen, " 0x%02X", bcBuffer[i]);
Assert(num == 5);
IfFalseGo(WriteFile(bcFileHandle, scratch, (DWORD)(scratchLen - 1), &written, nullptr));
// Add a comma and a space if this is not the last item
if (i < bcBufferSize - 1)
{
char commaSpace[2];
_snprintf_s(commaSpace, sizeof(commaSpace), ","); // close quote, new line, offset and open quote
IfFalseGo(WriteFile(bcFileHandle, commaSpace, (DWORD)strlen(commaSpace), &written, nullptr));
}
// Add a line break every 16 scratches, primarily so the compiler doesn't complain about the string being too long.
// Also, won't add for the last scratch
if (i % 16 == 15 && i < bcBufferSize - 1)
{
char offset[17];
_snprintf_s(offset, sizeof(offset), "\r\n/* %08X */", i + 1); // close quote, new line, offset and open quote
IfFalseGo(WriteFile(bcFileHandle, offset, (DWORD)strlen(offset), &written, nullptr));
}
}
outputStr = "};\r\n\r\n";
IfFalseGo(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr));
outputStr = "}\r\n";
IfFalseGo(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr));
Error:
if (bcFileHandle != nullptr)
{
CloseHandle(bcFileHandle);
}
if (bcBuffer != nullptr)
{
delete[] bcBuffer;
}
return hr;
}
static void CALLBACK PromiseContinuationCallback(JsValueRef task, void *callbackState)
{
Assert(task != JS_INVALID_REFERENCE);
Assert(callbackState != JS_INVALID_REFERENCE);
MessageQueue * messageQueue = (MessageQueue *)callbackState;
WScriptJsrt::CallbackMessage *msg = new WScriptJsrt::CallbackMessage(0, task);
messageQueue->Push(msg);
}
HRESULT RunScript(LPCWSTR fileName, LPCWSTR fileContents, BYTE *bcBuffer, wchar_t *fullPath)
{
HRESULT hr = S_OK;
MessageQueue * messageQueue = new MessageQueue();
WScriptJsrt::AddMessageQueue(messageQueue);
IfJsErrorFailLog(ChakraRTInterface::JsSetPromiseContinuationCallback(PromiseContinuationCallback, (void*)messageQueue));
Assert(fileContents != nullptr || bcBuffer != nullptr);
JsErrorCode runScript;
if (bcBuffer != nullptr)
{
runScript = ChakraRTInterface::JsRunSerializedScript(fileContents, bcBuffer, WScriptJsrt::GetNextSourceContext(), fullPath, nullptr /*result*/);
}
else
{
runScript = ChakraRTInterface::JsRunScript(fileContents, WScriptJsrt::GetNextSourceContext(), fullPath, nullptr /*result*/);
}
if (runScript != JsNoError)
{
WScriptJsrt::PrintException(fileName, runScript);
}
else
{
// Repeatedly flush the message queue until it's empty. It is necessary to loop on this
// because setTimeout can add scripts to execute.
do
{
IfFailGo(messageQueue->ProcessAll(fileName));
} while (!messageQueue->IsEmpty());
}
Error:
if (messageQueue != nullptr)
{
delete messageQueue;
}
return hr;
}
HRESULT CreateAndRunSerializedScript(LPCWSTR fileName, LPCWSTR fileContents, wchar_t *fullPath)
{
HRESULT hr = S_OK;
JsRuntimeHandle runtime = JS_INVALID_RUNTIME_HANDLE;
JsContextRef context = JS_INVALID_REFERENCE, current = JS_INVALID_REFERENCE;
BYTE *bcBuffer = nullptr;
DWORD bcBufferSize = 0;
IfFailGo(GetSerializedBuffer(fileContents, &bcBuffer, &bcBufferSize));
// Bytecode buffer is created in one runtime and will be executed on different runtime.
IfJsErrorFailLog(ChakraRTInterface::JsCreateRuntime(jsrtAttributes, nullptr, &runtime));
IfJsErrorFailLog(ChakraRTInterface::JsCreateContext(runtime, &context));
IfJsErrorFailLog(ChakraRTInterface::JsGetCurrentContext(¤t));
IfJsErrorFailLog(ChakraRTInterface::JsSetCurrentContext(context));
// Initialized the WScript object on the new context
if (!WScriptJsrt::Initialize())
{
IfFailGo(E_FAIL);
}
IfFailGo(RunScript(fileName, fileContents, bcBuffer, fullPath));
Error:
if (bcBuffer != nullptr)
{
delete[] bcBuffer;
}
if (current != JS_INVALID_REFERENCE)
{
ChakraRTInterface::JsSetCurrentContext(current);
}
if (runtime != JS_INVALID_RUNTIME_HANDLE)
{
ChakraRTInterface::JsDisposeRuntime(runtime);
}
return hr;
}
HRESULT ExecuteTest(LPCWSTR fileName)
{
HRESULT hr = S_OK;
LPCWSTR fileContents = nullptr;
JsRuntimeHandle runtime = JS_INVALID_RUNTIME_HANDLE;
bool isUtf8 = false;
LPCOLESTR contentsRaw = nullptr;
UINT lengthBytes = 0;
hr = Helpers::LoadScriptFromFile(fileName, fileContents, &isUtf8, &contentsRaw, &lengthBytes);
contentsRaw; lengthBytes; // Unused for now.
IfFailGo(hr);
if (HostConfigFlags::flags.GenerateLibraryByteCodeHeaderIsEnabled)
{
jsrtAttributes = (JsRuntimeAttributes)(jsrtAttributes | JsRuntimeAttributeSerializeLibraryByteCode);
}
IfJsErrorFailLog(ChakraRTInterface::JsCreateRuntime(jsrtAttributes, nullptr, &runtime));
JsContextRef context = JS_INVALID_REFERENCE;
#if DBG
Debugger *debugger = new Debugger(runtime);
IfJsErrorFailLog(ChakraRTInterface::JsDiagStartDebugging(runtime, Debugger::JsDiagDebugEventHandler, nullptr));
#endif
IfJsErrorFailLog(ChakraRTInterface::JsCreateContext(runtime, &context));
IfJsErrorFailLog(ChakraRTInterface::JsSetCurrentContext(context));
if (!WScriptJsrt::Initialize())
{
IfFailGo(E_FAIL);
}
wchar_t fullPath[_MAX_PATH];
if (_wfullpath(fullPath, fileName, _MAX_PATH) == nullptr)
{
IfFailGo(E_FAIL);
}
// canonicalize that path name to lower case for the profile storage
size_t len = wcslen(fullPath);
for (size_t i = 0; i < len; i++)
{
fullPath[i] = towlower(fullPath[i]);
}
if (HostConfigFlags::flags.GenerateLibraryByteCodeHeaderIsEnabled)
{
if (isUtf8)
{
if (HostConfigFlags::flags.GenerateLibraryByteCodeHeader != nullptr && *HostConfigFlags::flags.GenerateLibraryByteCodeHeader != L'\0')
{
WCHAR libraryName[_MAX_PATH];
WCHAR ext[_MAX_EXT];
_wsplitpath_s(fullPath, NULL, 0, NULL, 0, libraryName, _countof(libraryName), ext, _countof(ext));
IfFailGo(CreateLibraryByteCodeHeader(fileContents, (BYTE*)contentsRaw, lengthBytes, HostConfigFlags::flags.GenerateLibraryByteCodeHeader, libraryName));
}
else
{
fwprintf(stderr, L"FATAL ERROR: -GenerateLibraryByteCodeHeader must provide the file name, i.e., -GenerateLibraryByteCodeHeader:<bytecode file name>, exiting\n");
IfFailGo(E_FAIL);
}
}
else
{
fwprintf(stderr, L"FATAL ERROR: GenerateLibraryByteCodeHeader flag can only be used on UTF8 file, exiting\n");
IfFailGo(E_FAIL);
}
}
else if (HostConfigFlags::flags.SerializedIsEnabled)
{
if (isUtf8)
{
CreateAndRunSerializedScript(fileName, fileContents, fullPath);
}
else
{
fwprintf(stderr, L"FATAL ERROR: Serialized flag can only be used on UTF8 file, exiting\n");
IfFailGo(E_FAIL);
}
}
else
{
IfFailGo(RunScript(fileName, fileContents, nullptr, fullPath));
}
Error:
#if DBG
debugger = nullptr;
#endif
ChakraRTInterface::JsSetCurrentContext(nullptr);
if (runtime != JS_INVALID_RUNTIME_HANDLE)
{
ChakraRTInterface::JsDisposeRuntime(runtime);
}
_flushall();
return hr;
}
HRESULT ExecuteTestWithMemoryCheck(BSTR fileName)
{
HRESULT hr = E_FAIL;
#ifdef CHECK_MEMORY_LEAK
// Always check memory leak, unless user specified the flag already
if (!ChakraRTInterface::IsEnabledCheckMemoryFlag())
{
ChakraRTInterface::SetCheckMemoryLeakFlag(true);
}
// Disable the output in case an unhandled exception happens
// We will re-enable it if there is no unhandled exceptions
ChakraRTInterface::SetEnableCheckMemoryLeakOutput(false);
#endif
__try
{
hr = ExecuteTest(fileName);
}
__except (HostExceptionFilter(GetExceptionCode(), GetExceptionInformation()))
{
_flushall();
// Exception happened, so we probably didn't clean up properly,
// Don't exit normally, just terminate
TerminateProcess(::GetCurrentProcess(), GetExceptionCode());
}
_flushall();
#ifdef CHECK_MEMORY_LEAK
ChakraRTInterface::SetEnableCheckMemoryLeakOutput(true);
#endif
return hr;
}
unsigned int WINAPI StaticThreadProc(void *lpParam)
{
ChakraRTInterface::ArgInfo* argInfo = static_cast<ChakraRTInterface::ArgInfo* >(lpParam);
_endthreadex(ExecuteTestWithMemoryCheck(*(argInfo->filename)));
return 0;
}
int _cdecl wmain(int argc, __in_ecount(argc) LPWSTR argv[])
{
if (argc < 2)
{
PrintUsage();
return EXIT_FAILURE;
}
HostConfigFlags::pfnPrintUsage = PrintUsageFormat;
ATOM lock = ::AddAtom(szChakraCoreLock);
AssertMsg(lock, "failed to lock chakracore.dll");
HostConfigFlags::HandleArgsFlag(argc, argv);
CComBSTR fileName;
ChakraRTInterface::ArgInfo argInfo = { argc, argv, PrintUsage, &fileName.m_str };
HINSTANCE chakraLibrary = ChakraRTInterface::LoadChakraDll(argInfo);
if (chakraLibrary != nullptr)
{
HANDLE threadHandle;
threadHandle = reinterpret_cast<HANDLE>(_beginthreadex(0, 0, &StaticThreadProc, &argInfo, STACK_SIZE_PARAM_IS_A_RESERVATION, 0));
if (threadHandle != nullptr)
{
DWORD waitResult = WaitForSingleObject(threadHandle, INFINITE);
Assert(waitResult == WAIT_OBJECT_0);
CloseHandle(threadHandle);
}
else
{
fwprintf(stderr, L"FATAL ERROR: failed to create worker thread error code %d, exiting\n", errno);
AssertMsg(false, "failed to create worker thread");
}
ChakraRTInterface::UnloadChakraDll(chakraLibrary);
}
return 0;
}