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
888 lines (754 loc) · 28.8 KB
/
ch.cpp
File metadata and controls
888 lines (754 loc) · 28.8 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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
//-------------------------------------------------------------------------------------------------------
// 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"
#ifdef _WIN32
#include <process.h>
#endif
unsigned int MessageBase::s_messageCount = 0;
Debugger* Debugger::debugger = nullptr;
#ifdef _WIN32
LPCWSTR hostName = _u("ch.exe");
#else
LPCWSTR hostName = _u("ch");
#endif
JsRuntimeHandle chRuntime = JS_INVALID_RUNTIME_HANDLE;
BOOL doTTRecord = false;
BOOL doTTDebug = false;
byte ttUri[MAX_PATH * sizeof(WCHAR)];
size_t ttUriByteLength = 0;
UINT32 snapInterval = MAXUINT32;
UINT32 snapHistoryLength = MAXUINT32;
LPCWSTR connectionUuidString = NULL;
UINT32 startEventCount = 1;
extern "C"
HRESULT __stdcall OnChakraCoreLoadedEntry(TestHooks& testHooks)
{
return ChakraRTInterface::OnChakraCoreLoaded(testHooks);
}
JsRuntimeAttributes jsrtAttributes = JsRuntimeAttributeAllowScriptInterrupt;
int HostExceptionFilter(int exceptionCode, _EXCEPTION_POINTERS *ep)
{
ChakraRTInterface::NotifyUnhandledException(ep);
#if ENABLE_NATIVE_CODEGEN && _WIN32
JITProcessManager::TerminateJITServer();
#endif
bool crashOnException = false;
ChakraRTInterface::GetCrashOnExceptionFlag(&crashOnException);
if (exceptionCode == EXCEPTION_BREAKPOINT || (crashOnException && exceptionCode != 0xE06D7363))
{
return EXCEPTION_CONTINUE_SEARCH;
}
fwprintf(stderr, _u("FATAL ERROR: %ls failed due to exception code %x\n"), hostName, exceptionCode);
_flushall();
// Exception happened, so we probably didn't clean up properly,
// Don't exit normally, just terminate
TerminateProcess(::GetCurrentProcess(), exceptionCode);
return EXCEPTION_CONTINUE_SEARCH;
}
void __stdcall PrintUsageFormat()
{
wprintf(_u("\nUsage: %s [flaglist] <source file>\n"), hostName);
}
void __stdcall PrintUsage()
{
#if !defined(ENABLE_DEBUG_CONFIG_OPTIONS)
wprintf(_u("\nUsage: %s <source file> %s"), hostName,
_u("\n[flaglist] is not supported for Release mode\n"));
#else
PrintUsageFormat();
wprintf(_u("Try '%s -?' for help\n"), hostName);
#endif
}
// On success the param byteCodeBuffer will be allocated in the function.
HRESULT GetSerializedBuffer(LPCSTR fileContents, JsValueRef *byteCodeBuffer)
{
HRESULT hr = S_OK;
JsValueRef scriptSource;
IfJsErrorFailLog(ChakraRTInterface::JsCreateExternalArrayBuffer((void*)fileContents,
(unsigned int)strlen(fileContents), nullptr, nullptr, &scriptSource));
IfJsErrorFailLog(ChakraRTInterface::JsSerialize(scriptSource, byteCodeBuffer,
JsParseScriptAttributeNone));
Error:
return hr;
}
HRESULT CreateLibraryByteCodeHeader(LPCSTR contentsRaw, DWORD lengthBytes, LPCWSTR bcFullPath, LPCSTR libraryNameNarrow)
{
HANDLE bcFileHandle = nullptr;
JsValueRef bufferVal;
BYTE *bcBuffer = nullptr;
unsigned int bcBufferSize = 0;
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";
HRESULT hr = GetSerializedBuffer(contentsRaw, &bufferVal);
if (FAILED(hr)) return hr;
IfJsrtErrorHR(ChakraRTInterface::JsGetArrayBufferStorage(bufferVal, &bcBuffer, &bcBufferSize));
bcFileHandle = CreateFile(bcFullPath, GENERIC_WRITE, FILE_SHARE_DELETE,
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (bcFileHandle == INVALID_HANDLE_VALUE)
{
return E_FAIL;
}
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));
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, _countof(scratch), " 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), _countof(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), _countof(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);
}
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->InsertSorted(msg);
}
static bool CHAKRA_CALLBACK DummyJsSerializedScriptLoadUtf8Source(
JsSourceContext sourceContext,
JsValueRef* scriptBuffer,
JsParseScriptAttributes *parseAttributes)
{
const char* script = reinterpret_cast<const char*>(sourceContext);
size_t length = strlen(script);
// sourceContext is source ptr, see RunScript below
if (ChakraRTInterface::JsCreateExternalArrayBuffer((void*)script, (unsigned int)length,
[](void* data)
{
// sourceContext is source ptr, see RunScript below
// source memory was originally allocated with malloc() in
// Helpers::LoadScriptFromFile. No longer needed, free() it.
free(data);
}, nullptr, scriptBuffer) != JsNoError)
{
return false;
}
*parseAttributes = JsParseScriptAttributeNone;
return true;
}
HRESULT RunScript(const char* fileName, LPCSTR fileContents, JsValueRef bufferValue, char *fullPath)
{
HRESULT hr = S_OK;
MessageQueue * messageQueue = new MessageQueue();
WScriptJsrt::AddMessageQueue(messageQueue);
IfJsErrorFailLog(ChakraRTInterface::JsSetPromiseContinuationCallback(PromiseContinuationCallback, (void*)messageQueue));
if(strlen(fileName) >= 14 && strcmp(fileName + strlen(fileName) - 14, "ttdSentinal.js") == 0)
{
#if !ENABLE_TTD
wprintf(_u("Sential js file is only ok when in TTDebug mode!!!\n"));
return E_FAIL;
#else
if(!doTTDebug)
{
wprintf(_u("Sential js file is only ok when in TTDebug mode!!!\n"));
return E_FAIL;
}
ChakraRTInterface::JsTTDStart();
try
{
JsTTDMoveMode moveMode = JsTTDMoveMode::JsTTDMoveKthEvent;
int64_t snapEventTime = -1;
int64_t nextEventTime = -2;
while(true)
{
JsErrorCode error = ChakraRTInterface::JsTTDGetSnapTimeTopLevelEventMove(chRuntime, moveMode, startEventCount, &nextEventTime, &snapEventTime, nullptr);
if(error != JsNoError)
{
if(error == JsErrorCategoryUsage)
{
wprintf(_u("Start time not in log range.\n"));
}
return error;
}
IfFailedReturn(ChakraRTInterface::JsTTDMoveToTopLevelEvent(chRuntime, moveMode, snapEventTime, nextEventTime));
JsErrorCode res = ChakraRTInterface::JsTTDReplayExecution(&moveMode, &nextEventTime);
//handle any uncaught exception by immediately time-traveling to the throwing line in the debugger -- in replay just report and exit
if(res == JsErrorCategoryScript)
{
wprintf(_u("An unhandled script exception occoured!!!\n"));
ExitProcess(0);
}
if(nextEventTime == -1)
{
wprintf(_u("\nReached end of Execution -- Exiting.\n"));
break;
}
}
}
catch(...)
{
wprintf(_u("Terminal exception in Replay -- exiting.\n"));
ExitProcess(0);
}
#endif
}
else
{
Assert(fileContents != nullptr || bufferValue != nullptr);
JsErrorCode runScript;
JsValueRef fname;
IfJsErrorFailLog(ChakraRTInterface::JsCreateString(fullPath,
strlen(fullPath), &fname));
if(bufferValue != nullptr)
{
runScript = ChakraRTInterface::JsRunSerialized(
bufferValue,
DummyJsSerializedScriptLoadUtf8Source,
reinterpret_cast<JsSourceContext>(fileContents),
// Use source ptr as sourceContext
fname, nullptr /*result*/);
}
else
{
JsValueRef scriptSource;
IfJsErrorFailLog(ChakraRTInterface::JsCreateExternalArrayBuffer((void*)fileContents,
(unsigned int)strlen(fileContents),
[](void *data)
{
free(data);
}, nullptr, &scriptSource));
#if ENABLE_TTD
if(doTTRecord)
{
ChakraRTInterface::JsTTDStart();
}
runScript = ChakraRTInterface::JsRun(scriptSource,
WScriptJsrt::GetNextSourceContext(), fname,
JsParseScriptAttributeNone, nullptr /*result*/);
if (runScript == JsErrorCategoryUsage)
{
wprintf(_u("FATAL ERROR: Core was compiled without ENABLE_TTD is defined. CH is trying to use TTD interface\n"));
abort();
}
#else
runScript = ChakraRTInterface::JsRun(scriptSource,
WScriptJsrt::GetNextSourceContext(), fname,
JsParseScriptAttributeNone,
nullptr /*result*/);
#endif
}
//Do a yield after the main script body executes
ChakraRTInterface::JsTTDNotifyYield();
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 ENABLE_TTD
if(doTTRecord)
{
ChakraRTInterface::JsTTDEmitRecording();
ChakraRTInterface::JsTTDStop();
}
#endif
if (messageQueue != nullptr)
{
messageQueue->RemoveAll();
// clean up possible pinned exception object on exit to avoid potential leak
bool hasException;
if (ChakraRTInterface::JsHasException(&hasException) == JsNoError && hasException)
{
JsValueRef exception = JS_INVALID_REFERENCE;
ChakraRTInterface::JsGetAndClearException(&exception);
}
delete messageQueue;
}
return hr;
}
static HRESULT CreateRuntime(JsRuntimeHandle *runtime)
{
HRESULT hr = E_FAIL;
IfJsErrorFailLog(ChakraRTInterface::JsCreateRuntime(jsrtAttributes, nullptr, runtime));
#ifndef _WIN32
// On Posix, malloc may not return NULL even if there is no
// memory left. However, kernel will send SIGKILL to process
// in case we use that `not actually available` memory address.
// (See posix man malloc and OOM)
size_t memoryLimit;
if (PlatformAgnostic::SystemInfo::GetTotalRam(&memoryLimit))
{
IfJsErrorFailLog(ChakraRTInterface::JsSetRuntimeMemoryLimit(*runtime, memoryLimit));
}
#endif
hr = S_OK;
Error:
return hr;
}
HRESULT CreateAndRunSerializedScript(const char* fileName, LPCSTR fileContents, char *fullPath)
{
HRESULT hr = S_OK;
JsRuntimeHandle runtime = JS_INVALID_RUNTIME_HANDLE;
JsContextRef context = JS_INVALID_REFERENCE, current = JS_INVALID_REFERENCE;
JsValueRef bufferVal;
IfFailGo(GetSerializedBuffer(fileContents, &bufferVal));
// Bytecode buffer is created in one runtime and will be executed on different runtime.
IfFailGo(CreateRuntime(&runtime));
chRuntime = 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, bufferVal, fullPath));
Error:
if (current != JS_INVALID_REFERENCE)
{
ChakraRTInterface::JsSetCurrentContext(current);
}
if (runtime != JS_INVALID_RUNTIME_HANDLE)
{
ChakraRTInterface::JsDisposeRuntime(runtime);
}
return hr;
}
HRESULT ExecuteTest(const char* fileName)
{
HRESULT hr = S_OK;
LPCSTR fileContents = nullptr;
JsRuntimeHandle runtime = JS_INVALID_RUNTIME_HANDLE;
UINT lengthBytes = 0;
if(strlen(fileName) >= 14 && strcmp(fileName + strlen(fileName) - 14, "ttdSentinal.js") == 0)
{
#if !ENABLE_TTD
wprintf(_u("Sentinel js file is only ok when in TTDebug mode!!!\n"));
return E_FAIL;
#else
if(!doTTDebug)
{
wprintf(_u("Sentinel js file is only ok when in TTDebug mode!!!\n"));
return E_FAIL;
}
jsrtAttributes = static_cast<JsRuntimeAttributes>(jsrtAttributes | JsRuntimeAttributeEnableExperimentalFeatures);
IfJsErrorFailLog(ChakraRTInterface::JsTTDCreateReplayRuntime(jsrtAttributes, ttUri, ttUriByteLength, Helpers::TTInitializeForWriteLogStreamCallback, Helpers::TTCreateStreamCallback, Helpers::TTReadBytesFromStreamCallback, Helpers::TTWriteBytesToStreamCallback, Helpers::TTFlushAndCloseStreamCallback, nullptr, &runtime));
chRuntime = runtime;
JsContextRef context = JS_INVALID_REFERENCE;
IfJsErrorFailLog(ChakraRTInterface::JsTTDCreateContext(runtime, true, &context));
IfJsErrorFailLog(ChakraRTInterface::JsSetCurrentContext(context));
IfFailGo(RunScript(fileName, fileContents, nullptr, nullptr));
unsigned int rcount = 0;
IfJsErrorFailLog(ChakraRTInterface::JsSetCurrentContext(nullptr));
ChakraRTInterface::JsRelease(context, &rcount);
AssertMsg(rcount == 0, "Should only have had 1 ref from replay code and one ref from current context??");
#endif
}
else
{
LPCOLESTR contentsRaw = nullptr;
char fullPath[_MAX_PATH];
size_t len = 0;
hr = Helpers::LoadScriptFromFile(fileName, fileContents, &lengthBytes);
contentsRaw; lengthBytes; // Unused for now.
IfFailGo(hr);
if (HostConfigFlags::flags.GenerateLibraryByteCodeHeaderIsEnabled)
{
jsrtAttributes = (JsRuntimeAttributes)(jsrtAttributes | JsRuntimeAttributeSerializeLibraryByteCode);
}
#if ENABLE_TTD
if (doTTRecord)
{
//Ensure we run with experimental features (as that is what Node does right now).
jsrtAttributes = static_cast<JsRuntimeAttributes>(jsrtAttributes | JsRuntimeAttributeEnableExperimentalFeatures);
IfJsErrorFailLog(ChakraRTInterface::JsTTDCreateRecordRuntime(jsrtAttributes, ttUri, ttUriByteLength, snapInterval, snapHistoryLength, Helpers::TTInitializeForWriteLogStreamCallback, Helpers::TTCreateStreamCallback, Helpers::TTReadBytesFromStreamCallback, Helpers::TTWriteBytesToStreamCallback, Helpers::TTFlushAndCloseStreamCallback, nullptr, &runtime));
chRuntime = runtime;
JsContextRef context = JS_INVALID_REFERENCE;
IfJsErrorFailLog(ChakraRTInterface::JsTTDCreateContext(runtime, true, &context));
#if ENABLE_TTD
//We need this here since this context is created in record
IfJsErrorFailLog(ChakraRTInterface::JsSetObjectBeforeCollectCallback(context, nullptr, WScriptJsrt::JsContextBeforeCollectCallback));
#endif
IfJsErrorFailLog(ChakraRTInterface::JsSetCurrentContext(context));
}
else
{
AssertMsg(!doTTDebug, "Should be handled in the else case above!!!");
IfJsErrorFailLog(ChakraRTInterface::JsCreateRuntime(jsrtAttributes, nullptr, &runtime));
chRuntime = runtime;
if (HostConfigFlags::flags.DebugLaunch)
{
Debugger* debugger = Debugger::GetDebugger(runtime);
debugger->StartDebugging(runtime);
}
JsContextRef context = JS_INVALID_REFERENCE;
IfJsErrorFailLog(ChakraRTInterface::JsCreateContext(runtime, &context));
//Don't need collect callback since this is always in replay
IfJsErrorFailLog(ChakraRTInterface::JsSetCurrentContext(context));
}
#else
IfJsErrorFailLog(ChakraRTInterface::JsCreateRuntime(jsrtAttributes, nullptr, &runtime));
chRuntime = runtime;
if (HostConfigFlags::flags.DebugLaunch)
{
Debugger* debugger = Debugger::GetDebugger(runtime);
debugger->StartDebugging(runtime);
}
JsContextRef context = JS_INVALID_REFERENCE;
IfJsErrorFailLog(ChakraRTInterface::JsCreateContext(runtime, &context));
IfJsErrorFailLog(ChakraRTInterface::JsSetCurrentContext(context));
#endif
#ifdef DEBUG
ChakraRTInterface::SetCheckOpHelpersFlag(true);
#endif
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
ChakraRTInterface::SetOOPCFGRegistrationFlag(false);
#endif
if (!WScriptJsrt::Initialize())
{
IfFailGo(E_FAIL);
}
if (_fullpath(fullPath, fileName, _MAX_PATH) == nullptr)
{
IfFailGo(E_FAIL);
}
// canonicalize that path name to lower case for the profile storage
// REVIEW: Assuming no utf8 characters here
len = strlen(fullPath);
for (size_t i = 0; i < len; i++)
{
fullPath[i] = (char)tolower(fullPath[i]);
}
if (HostConfigFlags::flags.GenerateLibraryByteCodeHeaderIsEnabled)
{
if (HostConfigFlags::flags.GenerateLibraryByteCodeHeader != nullptr && *HostConfigFlags::flags.GenerateLibraryByteCodeHeader != _u('\0'))
{
CHAR libraryName[_MAX_PATH];
CHAR ext[_MAX_EXT];
_splitpath_s(fullPath, NULL, 0, NULL, 0, libraryName, _countof(libraryName), ext, _countof(ext));
IfFailGo(CreateLibraryByteCodeHeader(fileContents, lengthBytes, HostConfigFlags::flags.GenerateLibraryByteCodeHeader, libraryName));
}
else
{
fwprintf(stderr, _u("FATAL ERROR: -GenerateLibraryByteCodeHeader must provide the file name, i.e., -GenerateLibraryByteCodeHeader:<bytecode file name>, exiting\n"));
IfFailGo(E_FAIL);
}
}
else if (HostConfigFlags::flags.SerializedIsEnabled)
{
CreateAndRunSerializedScript(fileName, fileContents, fullPath);
}
else
{
IfFailGo(RunScript(fileName, fileContents, nullptr, fullPath));
}
}
Error:
if (Debugger::debugger != nullptr)
{
Debugger::debugger->CompareOrWriteBaselineFile(fileName);
Debugger::CloseDebugger();
}
ChakraRTInterface::JsSetCurrentContext(nullptr);
if (runtime != JS_INVALID_RUNTIME_HANDLE)
{
ChakraRTInterface::JsDisposeRuntime(runtime);
}
_flushall();
return hr;
}
HRESULT ExecuteTestWithMemoryCheck(char* 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
#ifdef _WIN32
__try
{
hr = ExecuteTest(fileName);
}
__except (HostExceptionFilter(GetExceptionCode(), GetExceptionInformation()))
{
Assert(false);
}
#else
// REVIEW: Do we need a SEH handler here?
hr = ExecuteTest(fileName);
if (FAILED(hr)) exit(0);
#endif // _WIN32
_flushall();
#ifdef CHECK_MEMORY_LEAK
ChakraRTInterface::SetEnableCheckMemoryLeakOutput(true);
#endif
return hr;
}
#ifdef _WIN32
bool HandleJITServerFlag(int& argc, _Inout_updates_to_(argc, argc) LPWSTR argv[])
{
LPCWSTR flag = L"-jitserver:";
LPCWSTR flagWithoutColon = L"-jitserver";
size_t flagLen = wcslen(flag);
int i = 0;
for (i = 1; i < argc; ++i)
{
if (!_wcsicmp(argv[i], flagWithoutColon))
{
connectionUuidString = L"";
break;
}
else if (!_wcsnicmp(argv[i], flag, flagLen))
{
connectionUuidString = argv[i] + flagLen;
if (wcslen(connectionUuidString) == 0)
{
fwprintf(stdout, L"[FAILED]: must pass a UUID to -jitserver:\n");
return false;
}
else
{
break;
}
}
}
if (i == argc)
{
return false;
}
// remove this flag now
HostConfigFlags::RemoveArg(argc, argv, i);
return true;
}
typedef HRESULT(WINAPI *JsInitializeJITServerPtr)(UUID* connectionUuid, void* securityDescriptor, void* alpcSecurityDescriptor);
int _cdecl RunJITServer(int argc, __in_ecount(argc) LPWSTR argv[])
{
ChakraRTInterface::ArgInfo argInfo = { argc, argv, PrintUsage, nullptr };
HINSTANCE chakraLibrary = nullptr;
bool success = ChakraRTInterface::LoadChakraDll(&argInfo, &chakraLibrary);
if (!success)
{
wprintf(L"\nDll load failed\n");
return ERROR_DLL_INIT_FAILED;
}
UUID connectionUuid;
DWORD status = UuidFromStringW((RPC_WSTR)connectionUuidString, &connectionUuid);
if (status != RPC_S_OK)
{
return status;
}
JsInitializeJITServerPtr initRpcServer = (JsInitializeJITServerPtr)GetProcAddress(chakraLibrary, "JsInitializeJITServer");
HRESULT hr = initRpcServer(&connectionUuid, nullptr, nullptr);
if (FAILED(hr))
{
wprintf(L"InitializeJITServer failed by 0x%x\n", hr);
return hr;
}
if (chakraLibrary)
{
ChakraRTInterface::UnloadChakraDll(chakraLibrary);
}
return 0;
}
#endif
unsigned int WINAPI StaticThreadProc(void *lpParam)
{
ChakraRTInterface::ArgInfo* argInfo = static_cast<ChakraRTInterface::ArgInfo* >(lpParam);
return ExecuteTestWithMemoryCheck(argInfo->filename);
}
#ifndef _WIN32
static char16** argv = nullptr;
int main(int argc, char** c_argv)
{
PAL_InitializeChakraCore(argc, c_argv);
argv = new char16*[argc];
for (int i = 0; i < argc; i++)
{
NarrowStringToWideDynamic(c_argv[i], &argv[i]);
}
#else
#define PAL_Shutdown()
int _cdecl wmain(int argc, __in_ecount(argc) LPWSTR argv[])
{
#endif
#ifdef _WIN32
bool runJITServer = HandleJITServerFlag(argc, argv);
#endif
if (argc < 2)
{
PrintUsage();
PAL_Shutdown();
return EXIT_FAILURE;
}
#ifdef _WIN32
if (runJITServer)
{
return RunJITServer(argc, argv);
}
#endif
int cpos = 1;
for(int i = 1; i < argc; ++i)
{
if(wcsstr(argv[i], _u("-TTRecord=")) == argv[i])
{
doTTRecord = true;
wchar* ruri = argv[i] + wcslen(_u("-TTRecord="));
Helpers::GetTTDDirectory(ruri, &ttUriByteLength, ttUri);
}
else if(wcsstr(argv[i], _u("-TTDebug=")) == argv[i])
{
doTTDebug = true;
wchar* ruri = argv[i] + wcslen(_u("-TTDebug="));
Helpers::GetTTDDirectory(ruri, &ttUriByteLength, ttUri);
}
else if(wcsstr(argv[i], _u("-TTSnapInterval=")) == argv[i])
{
LPCWSTR intervalStr = argv[i] + wcslen(_u("-TTSnapInterval="));
snapInterval = (UINT32)_wtoi(intervalStr);
}
else if(wcsstr(argv[i], _u("-TTHistoryLength=")) == argv[i])
{
LPCWSTR historyStr = argv[i] + wcslen(_u("-TTHistoryLength="));
snapHistoryLength = (UINT32)_wtoi(historyStr);
}
else if(wcsstr(argv[i], _u("-TTDStartEvent=")) == argv[i])
{
LPCWSTR startEventStr = argv[i] + wcslen(_u("-TTDStartEvent="));
startEventCount = (UINT32)_wtoi(startEventStr);
}
else
{
argv[cpos] = argv[i];
cpos++;
}
}
argc = cpos;
if(doTTRecord & doTTDebug)
{
fwprintf(stderr, _u("Cannot run in record and debug at same time!!!"));
ExitProcess(0);
}
HostConfigFlags::pfnPrintUsage = PrintUsageFormat;
// The following code is present to make sure we don't load
// jscript9.dll etc with ch. Since that isn't a concern on non-Windows
// builds, it's safe to conditionally compile it out.
#ifdef _WIN32
ATOM lock = ::AddAtom(szChakraCoreLock);
AssertMsg(lock, "failed to lock chakracore.dll");
#endif // _WIN32
HostConfigFlags::HandleArgsFlag(argc, argv);
ChakraRTInterface::ArgInfo argInfo = { argc, argv, PrintUsage, nullptr };
HINSTANCE chakraLibrary = nullptr;
bool success = ChakraRTInterface::LoadChakraDll(&argInfo, &chakraLibrary);
#if defined(CHAKRA_STATIC_LIBRARY) && !defined(NDEBUG)
// handle command line flags
OnChakraCoreLoaded();
#endif
if (argInfo.filename == nullptr)
{
WideStringToNarrowDynamic(argv[1], &argInfo.filename);
}
HRESULT exitCode = E_FAIL;
if (success)
{
#ifdef _WIN32
#if ENABLE_NATIVE_CODEGEN
if (HostConfigFlags::flags.OOPJIT)
{
// TODO: Error checking
JITProcessManager::StartRpcServer(argc, argv);
ChakraRTInterface::ConnectJITServer(JITProcessManager::GetRpcProccessHandle(), nullptr, JITProcessManager::GetRpcConnectionId());
}
#endif
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);
DWORD threadExitCode;
GetExitCodeThread(threadHandle, &threadExitCode);
exitCode = (HRESULT)threadExitCode;
CloseHandle(threadHandle);
}
else
{
fwprintf(stderr, _u("FATAL ERROR: failed to create worker thread error code %d, exiting\n"), errno);
AssertMsg(false, "failed to create worker thread");
}
#else
// On linux, execute on the same thread
exitCode = ExecuteTestWithMemoryCheck(argInfo.filename);
#endif
#if ENABLE_NATIVE_CODEGEN && defined(_WIN32)
JITProcessManager::StopRpcServer(chakraLibrary);
#endif
ChakraRTInterface::UnloadChakraDll(chakraLibrary);
}
#if ENABLE_NATIVE_CODEGEN && defined(_WIN32)
else
{
JITProcessManager::TerminateJITServer();
}
#endif
PAL_Shutdown();
return (int)exitCode;
}