-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
JavascriptStackWalker.cpp
1611 lines (1412 loc) · 60.6 KB
/
JavascriptStackWalker.cpp
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeLanguagePch.h"
#include "Language/JavascriptFunctionArgIndex.h"
#include "Language/InterpreterStackFrame.h"
#define FAligned(VALUE, TYPE) ((((LONG_PTR)VALUE) & (sizeof(TYPE)-1)) == 0)
#define AlignIt(VALUE, TYPE) (~(~((LONG_PTR)(VALUE) + (sizeof(TYPE)-1)) | (sizeof(TYPE)-1)))
namespace Js
{
Js::ArgumentsObject * JavascriptCallStackLayout::GetArgumentsObject() const
{
return (Js::ArgumentsObject *)((void **)this)[JavascriptFunctionArgIndex_ArgumentsObject];
}
Js::Var* JavascriptCallStackLayout::GetArgumentsObjectLocation() const
{
return (Js::Var *)&((void **)this)[JavascriptFunctionArgIndex_ArgumentsObject];
}
void JavascriptCallStackLayout::SetArgumentsObject(Js::ArgumentsObject * obj)
{
((void **)this)[JavascriptFunctionArgIndex_ArgumentsObject] = obj;
}
Js::Var JavascriptCallStackLayout::GetOffset(int offset) const
{
Js::Var *varPtr = (Js::Var *)(((char *)this) + offset);
Assert(FAligned(varPtr, Js::Var));
return *varPtr;
}
double JavascriptCallStackLayout::GetDoubleAtOffset(int offset) const
{
double *dblPtr = (double *)(((char *)this) + offset);
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
if (Js::Configuration::Global.flags.IsEnabled(Js::CheckAlignmentFlag))
{
Assert(FAligned(dblPtr, double));
}
#endif
return *dblPtr;
}
int32 JavascriptCallStackLayout::GetInt32AtOffset(int offset) const
{
int32 *intPtr = (int32 *)(((char *)this) + offset);
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
if (Js::Configuration::Global.flags.IsEnabled(Js::CheckAlignmentFlag))
{
Assert(FAligned(intPtr, int32));
}
#endif
return *intPtr;
}
SIMDValue JavascriptCallStackLayout::GetSimdValueAtOffset(int offset) const
{
return *((SIMDValue *)(((char *)this) + offset));
}
char * JavascriptCallStackLayout::GetValueChangeOffset(int offset) const
{
Js::Var *varPtr = (Js::Var *)(((char *)this) + offset);
Assert(FAligned(varPtr, Js::Var));
return (char *)varPtr;
}
ForInObjectEnumerator * JavascriptCallStackLayout::GetForInObjectEnumeratorArrayAtOffset(int offset) const
{
return (ForInObjectEnumerator *)(((char *)this) + offset);
}
JavascriptCallStackLayout *JavascriptCallStackLayout::FromFramePointer(void *const framePointer)
{
return
reinterpret_cast<JavascriptCallStackLayout *>(
static_cast<void **>(framePointer) + (JavascriptFunctionArgIndex_Function - JavascriptFunctionArgIndex_Frame));
}
void* const JavascriptCallStackLayout::ToFramePointer(JavascriptCallStackLayout* callstackLayout)
{
return
reinterpret_cast<void * const>(
reinterpret_cast<void **>(callstackLayout) - (JavascriptFunctionArgIndex_Function - JavascriptFunctionArgIndex_Frame));
}
Js::Var* JavascriptCallStackLayout::GetArgv() const
{
return const_cast<Js::Var*>(&this->args[0]);
}
ScriptContext* JavascriptStackWalker::GetCurrentScriptContext() const
{
return this->GetCurrentInterpreterFrame() ? this->GetCurrentInterpreterFrame()->GetScriptContext() : this->scriptContext;
}
Var JavascriptStackWalker::GetCurrentArgumentsObject() const
{
#if ENABLE_PROFILE_INFO
if (interpreterFrame)
#else
Assert(interpreterFrame);
#endif
{
return interpreterFrame->GetArgumentsObject();
}
#if ENABLE_NATIVE_CODEGEN
else
{
if (inlinedFramesBeingWalked)
{
return inlinedFrameWalker.GetArgumentsObject();
}
else
{
return this->GetCurrentNativeArgumentsObject();
}
}
#endif
}
void JavascriptStackWalker::SetCurrentArgumentsObject(Var args)
{
#if ENABLE_NATIVE_CODEGEN
if (interpreterFrame)
#else
Assert(interpreterFrame);
#endif
{
interpreterFrame->SetArgumentsObject(args);
}
#if ENABLE_NATIVE_CODEGEN
else
{
if (inlinedFramesBeingWalked)
{
inlinedFrameWalker.SetArgumentsObject(args);
}
else
{
this->SetCurrentNativeArgumentsObject(args);
}
}
#endif
}
Var JavascriptStackWalker::GetPermanentArguments() const
{
Assert(IsJavascriptFrame());
AssertMsg(this->GetCurrentFunction()->IsScriptFunction(), "GetPermanentArguments should not be called for non-script function as there is no slot allocated for it.");
const uint32 paramCount = GetCallInfo().Count;
if (paramCount == 0)
{
// glob function doesn't allocate ArgumentsObject slot on stack
return nullptr;
}
// Get the heap-allocated args for this frame.
Var args = this->GetCurrentArgumentsObject();
if (args && ArgumentsObject::Is(args))
{
args = ((ArgumentsObject*)args)->GetHeapArguments();
}
return args;
}
BOOL JavascriptStackWalker::WalkToArgumentsFrame(ArgumentsObject *args)
{
// Move the walker up the stack until we find the given arguments object on the frame.
while (this->Walk(/*includeInlineFrame*/ true))
{
if (this->IsJavascriptFrame())
{
Var currArgs = this->GetCurrentArgumentsObject();
if (currArgs == args)
{
return TRUE;
}
}
}
return FALSE;
}
void JavascriptStackWalker::GetThis(Var* pVarThis, int moduleId) const
{
#if ENABLE_NATIVE_CODEGEN
if (inlinedFramesBeingWalked)
{
if (inlinedFrameWalker.GetArgc() == 0)
{
*pVarThis = JavascriptOperators::OP_GetThis(this->scriptContext->GetLibrary()->GetUndefined(), moduleId, scriptContext);
}
else
{
*pVarThis = inlinedFrameWalker.GetThisObject();
Assert(*pVarThis);
}
}
else
#endif
{
const CallInfo callInfo = this->GetCallInfo();
if (callInfo.Count == 0)
{
*pVarThis = JavascriptOperators::OP_GetThis(scriptContext->GetLibrary()->GetUndefined(), moduleId, scriptContext);
}
else
{
*pVarThis = this->GetThisFromFrame();
}
}
if (*pVarThis == nullptr)
{
*pVarThis = this->scriptContext->GetLibrary()->GetNull();
}
}
BOOL IsEval(CallInfo callInfo)
{
return (callInfo.Flags & CallFlags_Eval) != 0;
}
BOOL JavascriptStackWalker::IsCallerGlobalFunction() const
{
const CallInfo callInfo = this->GetCallInfo();
JavascriptFunction* function = this->GetCurrentFunction();
if (IsLibraryStackFrameEnabled(this->scriptContext) && !function->IsScriptFunction())
{
return false; // native library code can't be global function
}
FunctionInfo* funcInfo = function->GetFunctionInfo();
if (funcInfo->HasParseableInfo())
{
return funcInfo->GetParseableFunctionInfo()->GetIsGlobalFunc() || IsEval(callInfo);
}
else
{
AssertMsg(FALSE, "Here we should only have script functions which were already parsed/deserialized.");
return callInfo.Count == 0 || IsEval(callInfo);
}
}
BOOL JavascriptStackWalker::IsEvalCaller() const
{
const CallInfo callInfo = this->GetCallInfo();
return (callInfo.Flags & CallFlags_Eval) != 0;
}
Var JavascriptStackWalker::GetCurrentNativeArgumentsObject() const
{
Assert(this->IsJavascriptFrame() && this->interpreterFrame == nullptr);
return this->GetCurrentArgv()[JavascriptFunctionArgIndex_ArgumentsObject];
}
void JavascriptStackWalker::SetCurrentNativeArgumentsObject(Var args)
{
Assert(this->IsJavascriptFrame() && this->interpreterFrame == nullptr);
this->GetCurrentArgv()[JavascriptFunctionArgIndex_ArgumentsObject] = args;
}
Js::Var * JavascriptStackWalker::GetJavascriptArgs(bool boxArgsAndDeepCopy) const
{
Assert(this->IsJavascriptFrame());
#if ENABLE_NATIVE_CODEGEN
if (inlinedFramesBeingWalked)
{
return inlinedFrameWalker.GetArgv(/* includeThis */ false, boxArgsAndDeepCopy);
}
else
#endif
if (this->GetCurrentFunction()->GetFunctionInfo()->IsCoroutine())
{
JavascriptGenerator* gen = JavascriptGenerator::FromVar(this->GetCurrentArgv()[JavascriptFunctionArgIndex_This]);
return gen->GetArguments().Values;
}
else
{
return &this->GetCurrentArgv()[JavascriptFunctionArgIndex_SecondScriptArg];
}
}
uint32 JavascriptStackWalker::GetByteCodeOffset() const
{
uint32 offset = 0;
if (this->IsJavascriptFrame())
{
if (this->interpreterFrame)
{
if (this->TryGetByteCodeOffsetFromInterpreterFrame(offset))
{
return offset;
}
}
#if ENABLE_NATIVE_CODEGEN
if (TryGetByteCodeOffsetFromNativeFrame(offset))
{
return offset;
}
#endif
}
return offset;
}
bool JavascriptStackWalker::TryGetByteCodeOffsetFromInterpreterFrame(uint32& offset) const
{
#if ENABLE_NATIVE_CODEGEN
if (this->lastInternalFrameInfo.codeAddress != nullptr)
{
return false;
}
#endif
offset = this->interpreterFrame->GetReader()->GetCurrentOffset();
if (offset == 0)
{
// This will be the case when we are broken on the debugger on very first statement (due to async break).
// Or the interpreter loop can throw OOS on entrance before executing bytecode.
}
else
{
// Note : For many cases, we move the m_currentLocation of ByteCodeReader already to next available opcode.
// This could create problem in binding the exception to proper line offset.
// Reducing by 1 will make sure the current offset falls under, current executing opcode.
offset--;
}
return true;
}
#if ENABLE_NATIVE_CODEGEN
bool JavascriptStackWalker::TryGetByteCodeOffsetFromNativeFrame(uint32& offset) const
{
DWORD_PTR pCodeAddr;
if (this->lastInternalFrameInfo.codeAddress != nullptr)
{
pCodeAddr = (DWORD_PTR)this->lastInternalFrameInfo.codeAddress;
}
else
{
pCodeAddr = (DWORD_PTR)this->GetCurrentCodeAddr();
}
// If the current instruction's return address is the beginning of the next statement then we will show error for the next line, which would be completely wrong.
// The quick fix would be to look the address which is at least lesser than current return address.
// Assert to verify at what places this can happen.
Assert(pCodeAddr);
if (pCodeAddr)
{
#if defined(_M_ARM)
// Note that DWORD_PTR is not actually a pointer type (!) but is simple unsigned long/__int64 (see BaseTsd.h).
// Thus, decrement would be by 1 byte and not 4 bytes as in pointer arithmetic. That's exactly what we need.
// For ARM the 'return address' is always odd and is 'next instr addr' + 1 byte, so to get to the BLX instr, we need to subtract 2 bytes from it.
AssertMsg(pCodeAddr % 2 == 1, "Got even number for pCodeAddr! It's expected to be return address, which should be odd.");
pCodeAddr--;
#endif
pCodeAddr--;
}
bool usedInternalFrameInfo = false;
uint loopNum = GetLoopNumber(usedInternalFrameInfo);
JavascriptFunction *function = nullptr;
FunctionBody *inlinee = nullptr;
function = usedInternalFrameInfo ? this->GetCachedInternalFrameInfo().function : this->GetCurrentFunctionFromPhysicalFrame();
// If there are inlined frames on the stack, we have to be able to return the byte code offsets of those inlined calls
// from their respective callers. But, we can use the current native address as IP for only the topmost inlined frame.
// TryGetByteCodeOffsetOfInlinee takes care of these conditions and sets up the offset of an inlinee in 'offset', if the
// current inlinee frame is not the topmost of the inlinee frames.
if (HasInlinedFramesOnStack() && TryGetByteCodeOffsetOfInlinee(function, loopNum, pCodeAddr, &inlinee, offset, usedInternalFrameInfo))
{
return true;
}
StatementData data;
if (function->GetFunctionBody() && function->GetFunctionBody()->GetMatchingStatementMapFromNativeAddress(pCodeAddr, data, loopNum, inlinee))
{
offset = data.bytecodeBegin;
return true;
}
return false;
}
uint JavascriptStackWalker::GetLoopNumber(bool& usedInternalFrameInfo) const
{
uint loopNum = LoopHeader::NoLoop;
if (this->lastInternalFrameInfo.codeAddress != nullptr)
{
if (this->lastInternalFrameInfo.frameType == InternalFrameType_LoopBody)
{
AnalysisAssert(this->interpreterFrame);
loopNum = this->interpreterFrame->GetCurrentLoopNum();
Assert(loopNum != LoopHeader::NoLoop);
usedInternalFrameInfo = true;
}
}
else
{
if (this->IsCurrentPhysicalFrameForLoopBody())
{
// Internal frame but codeAddress on lastInternalFrameInfo not set. We must be in an inlined frame in the loop body.
Assert(this->tempInterpreterFrame);
loopNum = this->tempInterpreterFrame->GetCurrentLoopNum();
Assert(loopNum != LoopHeader::NoLoop);
usedInternalFrameInfo = false;
}
}
return loopNum;
}
bool JavascriptStackWalker::TryGetByteCodeOffsetOfInlinee(Js::JavascriptFunction* parentFunction, uint loopNum, DWORD_PTR pCodeAddr, Js::FunctionBody** inlinee, uint32& offset, bool useInternalFrameInfo) const
{
// For inlined frames, translation from native offset -> source code happens in two steps.
// The native offset is first translated into a statement index using the physical frame's
// source context info. This statement index is then looked up in the *inlinee*'s source
// context info to get the bytecode offset.
//
// For all inlined frames contained within a physical frame we have only one offset == (IP - entry).
// Since we can't use that to get the other inlined callers' IPs, we save the IP of all inlined
// callers in their "callinfo" (See InlineeCallInfo). The top most inlined frame uses the IP
// of the physical frame. All other inlined frames use the InlineeStartOffset stored in their call info
// to calculate the byte code offset of the callsite of the inlinee they called.
StatementData data;
uint32 inlineeOffset = 0;
*inlinee = InlinedFramesBeingWalked() ? inlinedFrameWalker.GetFunctionObject()->GetFunctionBody() : nullptr;
InlinedFrameWalker tmpFrameWalker;
if (InlinedFramesBeingWalked())
{
// Inlined frames are being walked right now. The top most frame is where the IP is.
if (!inlinedFrameWalker.IsTopMostFrame())
{
inlineeOffset = inlinedFrameWalker.GetCurrentInlineeOffset();
}
}
else if (ScriptFunction::Test(parentFunction) && HasInlinedFramesOnStack())
{
// Inlined frames are not being walked right now. However, if there
// are inlined frames on the stack the InlineeCallInfo of the first inlined frame
// has the native offset of the current physical frame.
Assert(!*inlinee);
InlinedFrameWalker::FromPhysicalFrame(tmpFrameWalker, currentFrame, ScriptFunction::FromVar(parentFunction), PreviousInterpreterFrameIsFromBailout(), loopNum, this, useInternalFrameInfo, false /*noAlloc*/);
inlineeOffset = tmpFrameWalker.GetBottomMostInlineeOffset();
tmpFrameWalker.Close();
}
if (inlineeOffset != 0 &&
parentFunction->GetFunctionBody()->GetMatchingStatementMapFromNativeOffset(pCodeAddr, inlineeOffset, data, loopNum, *inlinee))
{
offset = data.bytecodeBegin;
return true;
}
return false;
}
bool JavascriptStackWalker::PreviousInterpreterFrameIsFromBailout() const
{
if (lastInternalFrameInfo.codeAddress)
{
return lastInternalFrameInfo.previousInterpreterFrameIsFromBailout;
}
return this->previousInterpreterFrameIsFromBailout;
}
bool JavascriptStackWalker::InlinedFramesBeingWalked() const
{
if (lastInternalFrameInfo.codeAddress)
{
return false;
}
return this->inlinedFramesBeingWalked;
}
bool JavascriptStackWalker::HasInlinedFramesOnStack() const
{
if (lastInternalFrameInfo.codeAddress)
{
return lastInternalFrameInfo.hasInlinedFramesOnStack;
}
return this->hasInlinedFramesOnStack;
}
#endif
bool JavascriptStackWalker::GetSourcePosition(const WCHAR** sourceFileName, ULONG* line, LONG* column)
{
uint byteCodeoffset = this->GetByteCodeOffset();
if(byteCodeoffset)
{
Js::FunctionBody* functionBody = this->GetCurrentFunction()->GetFunctionBody();
if (functionBody->GetLineCharOffset(byteCodeoffset, line, column))
{
if(functionBody->GetUtf8SourceInfo()->IsDynamic())
{
*sourceFileName = _u("Dynamic Code");
}
else
{
*sourceFileName = functionBody->GetUtf8SourceInfo()->GetSrcInfo()->sourceContextInfo->url;
}
return true;
}
}
return false;
}
Js::JavascriptFunction * JavascriptStackWalker::UpdateFrame(bool includeInlineFrames)
{
this->isJavascriptFrame = this->CheckJavascriptFrame(includeInlineFrames);
if (this->IsJavascriptFrame())
{
// In case we have a cross site thunk, update the script context
Js::JavascriptFunction *function = this->GetCurrentFunction();
#if ENABLE_NATIVE_CODEGEN
bool isCurrentPhysicalFrameForLoopBody = this->IsCurrentPhysicalFrameForLoopBody();
#endif
if (this->interpreterFrame)
{
#if ENABLE_NATIVE_CODEGEN
if (lastInternalFrameInfo.codeAddress != nullptr)
{
this->previousInterpreterFrameIsForLoopBody = true;
}
#endif
// We might've bailed out of an inlinee, so check if there were any inlinees.
if (this->interpreterFrame->TestFlags(InterpreterStackFrameFlags_FromBailOut))
{
previousInterpreterFrameIsFromBailout = true;
#if ENABLE_NATIVE_CODEGEN
Assert(!inlinedFramesBeingWalked);
if (includeInlineFrames)
{
int loopNum = -1;
if (isCurrentPhysicalFrameForLoopBody)
{
loopNum = this->tempInterpreterFrame->GetCurrentLoopNum();
}
bool hasInlinedFramesOnStack = InlinedFrameWalker::FromPhysicalFrame(inlinedFrameWalker, currentFrame,
ScriptFunction::FromVar(function), true /*fromBailout*/, loopNum, this, false /*useInternalFrameInfo*/, false /*noAlloc*/);
if (hasInlinedFramesOnStack)
{
// We're now back in the state where currentFrame == physical frame of the inliner, but
// since interpreterFrame != null, we'll pick values from the interpreterFrame (the bailout
// frame of the inliner). Set a flag to tell the stack walker that it needs to start from the
// inlinee frames on the stack when Walk() is called.
this->inlinedFramesBeingWalked = inlinedFrameWalker.Next(inlinedFrameCallInfo);
this->hasInlinedFramesOnStack = hasInlinedFramesOnStack;
Assert(inlinedFramesBeingWalked);
Assert(StackScriptFunction::GetCurrentFunctionObject(this->interpreterFrame->GetJavascriptFunction()) == inlinedFrameWalker.GetFunctionObject());
}
else
{
Assert(!isCurrentPhysicalFrameForLoopBody);
}
}
else if (isCurrentPhysicalFrameForLoopBody)
{
// Getting here is only possible when the current interpreterFrame is for a function which
// encountered a bailout after getting inlined in a jitted loop body. If we are not including
// inlined frames in the stack walk, we need to set the codeAddress on lastInternalFrameInfo,
// which would have otherwise been set upon closing the inlinedFrameWalker, now.
// Note that we already have an assert in CheckJavascriptFrame to ensure this.
SetCachedInternalFrameInfo(InternalFrameType_LoopBody, function, false /*hasInlinedFramesOnStack*/, true /*previousInterpreterFrameIsFromBailout*/);
}
#else
// How did we bail out when JIT was disabled?
Assert(false);
#endif
}
else
{
Assert(StackScriptFunction::GetCurrentFunctionObject(this->interpreterFrame->GetJavascriptFunction()) == function);
previousInterpreterFrameIsFromBailout = false;
}
}
else if (!this->isNativeLibraryFrame)
{
#if ENABLE_NATIVE_CODEGEN
Assert(!HasInlinedFramesOnStack() || (includeInlineFrames && isCurrentPhysicalFrameForLoopBody));
if (!HasInlinedFramesOnStack() && includeInlineFrames)
{
// Check whether there are inlined frames nested in this native frame. The corresponding check for
// a jitted loop body frame should have been done in CheckJavascriptFrame
Assert(lastInternalFrameInfo.codeAddress == nullptr);
bool inlinedFramesFound = InlinedFrameWalker::FromPhysicalFrame(
inlinedFrameWalker,
currentFrame,
ScriptFunction::FromVar(function),
false, // fromBailout
-1, // loopNum
nullptr,// walker
false, // useInternalFrameInfo
false // noAlloc
);
if (inlinedFramesFound)
{
this->inlinedFramesBeingWalked = inlinedFrameWalker.Next(inlinedFrameCallInfo);
this->hasInlinedFramesOnStack = true;
Assert(inlinedFramesBeingWalked);
}
}
#endif
}
this->scriptContext = function->GetScriptContext();
return function;
}
return nullptr;
}
#if ENABLE_NATIVE_CODEGEN
void JavascriptStackWalker::WalkAndClearInlineeFrameCallInfoOnException(void *tryHandlerAddrOfReturnAddr)
{
// Walk the stack and when we find the first native frame, we clear the inlinee's callinfo for this frame
// It is sufficient we stop at the first native frame which had the enclosing try-catch
// TODO : Revisit when we start inlining functions with try-catch/try-finally
while (this->Walk(true))
{
if (JavascriptFunction::IsNativeAddress(this->scriptContext, this->currentFrame.GetInstructionPointer()))
{
if (HasInlinedFramesOnStack())
{
for (int index = inlinedFrameWalker.GetFrameCount() - 1; index >= 0; index--)
{
auto inlinedFrame = inlinedFrameWalker.GetFrameAtIndex(index);
inlinedFrame->callInfo.Clear();
}
}
}
if (this->currentFrame.GetAddressOfReturnAddress() == tryHandlerAddrOfReturnAddr)
{
break;
}
}
}
#endif
// Note: noinline is to make sure that when we unwind to the unwindToAddress, there is at least one frame to unwind.
_NOINLINE
JavascriptStackWalker::JavascriptStackWalker(ScriptContext * scriptContext, bool useEERContext, PVOID returnAddress, bool _forceFullWalk /*=false*/) :
inlinedFrameCallInfo(CallFlags_None, 0), shouldDetectPartiallyInitializedInterpreterFrame(true), forceFullWalk(_forceFullWalk),
previousInterpreterFrameIsFromBailout(false), previousInterpreterFrameIsForLoopBody(false), hasInlinedFramesOnStack(false)
{
if (scriptContext == NULL)
{
Throw::InternalError();
}
this->scriptContext = scriptContext;
// Pull the current script state from the thread context.
ThreadContext * threadContext = scriptContext->GetThreadContext();
this->entryExitRecord = threadContext->GetScriptEntryExit();
this->nativeLibraryEntry = threadContext->PeekNativeLibraryEntry();
this->prevNativeLibraryEntry = nullptr;
this->interpreterFrame = NULL;
this->isJavascriptFrame = false;
this->isNativeLibraryFrame = false;
if (entryExitRecord->frameIdOfScriptExitFunction != NULL)
{
// We're currently outside the script, so grab the frame from which we left.
this->scriptContext = entryExitRecord->scriptContext;
this->isInitialFrame = this->currentFrame.InitializeByFrameId(entryExitRecord->frameIdOfScriptExitFunction, this->scriptContext);
}
else
{
// Just start with the caller
this->isInitialFrame = this->currentFrame.InitializeByReturnAddress(_ReturnAddress(), this->scriptContext);
}
if (useEERContext)
{
this->tempInterpreterFrame = this->scriptContext->GetThreadContext()->GetLeafInterpreterFrame();
}
else
{
// We need to generate stack for the passed script context, so use the leaf interpreter frame for passed script context
this->tempInterpreterFrame = scriptContext->GetThreadContext()->GetLeafInterpreterFrame();
}
inlinedFramesBeingWalked = false;
}
BOOL JavascriptStackWalker::Walk(bool includeInlineFrames)
{
// Walk one frame up the call stack.
this->interpreterFrame = NULL;
#if ENABLE_NATIVE_CODEGEN
if (lastInternalFrameInfo.codeAddress != nullptr && this->previousInterpreterFrameIsForLoopBody)
{
this->previousInterpreterFrameIsForLoopBody = false;
ClearCachedInternalFrameInfo();
}
if (inlinedFramesBeingWalked)
{
Assert(includeInlineFrames);
inlinedFramesBeingWalked = inlinedFrameWalker.Next(inlinedFrameCallInfo);
if (!inlinedFramesBeingWalked)
{
inlinedFrameWalker.Close();
if ((this->IsCurrentPhysicalFrameForLoopBody()))
{
// Done walking inlined frames in a loop body, cache the native code address now
// in order to skip the loop body frame.
this->SetCachedInternalFrameInfo(InternalFrameType_LoopBody, this->GetCurrentFunctionFromPhysicalFrame(), true /*hasInlinedFramesOnStack*/, previousInterpreterFrameIsFromBailout);
isJavascriptFrame = false;
}
}
return true;
}
#endif
this->hasInlinedFramesOnStack = false;
if (this->isInitialFrame)
{
this->isInitialFrame = false; // Only walk initial frame once
}
else if (!this->currentFrame.Next())
{
this->isJavascriptFrame = false;
return false;
}
// If we're at the entry from a host frame, hop to the frame from which we left the script.
if (AlignAndCheckAddressOfReturnAddressMatch(this->currentFrame.GetAddressOfInstructionPointer(), this->entryExitRecord->addrOfReturnAddrOfScriptEntryFunction))
{
BOOL hasCaller = this->entryExitRecord->hasCaller || this->forceFullWalk;
#ifdef CHECK_STACKWALK_EXCEPTION
BOOL ignoreStackWalkException = this->entryExitRecord->ignoreStackWalkException;
#endif
this->entryExitRecord = this->entryExitRecord->next;
if (this->entryExitRecord == NULL)
{
this->isJavascriptFrame = false;
return false;
}
if (!hasCaller && !this->scriptContext->IsDiagnosticsScriptContext())
{
#ifdef CHECK_STACKWALK_EXCEPTION
if (!ignoreStackWalkException)
{
AssertMsg(false, "walk pass no caller frame");
}
#endif
this->isJavascriptFrame = false;
return false;
}
this->scriptContext = this->entryExitRecord->scriptContext;
this->currentFrame.SkipToFrame(this->entryExitRecord->frameIdOfScriptExitFunction);
}
this->UpdateFrame(includeInlineFrames);
return true;
}
BOOL JavascriptStackWalker::GetCallerWithoutInlinedFrames(_Out_opt_ JavascriptFunction ** ppFunc)
{
return GetCaller(ppFunc, /*includeInlineFrames*/ false);
}
BOOL JavascriptStackWalker::GetCaller(_Out_opt_ JavascriptFunction ** ppFunc, bool includeInlineFrames)
{
while (this->Walk(includeInlineFrames))
{
if (this->IsJavascriptFrame())
{
Assert(entryExitRecord != NULL);
if (ppFunc)
{
*ppFunc = this->GetCurrentFunction();
}
AssertMsg(!this->shouldDetectPartiallyInitializedInterpreterFrame, "must have skipped first frame if needed");
return true;
}
}
if (ppFunc)
{
*ppFunc = nullptr;
}
return false;
}
BOOL JavascriptStackWalker::GetNonLibraryCodeCaller(_Out_opt_ JavascriptFunction ** ppFunc)
{
while (this->GetCaller(ppFunc))
{
Assert(ppFunc != nullptr);
__analysis_assume(ppFunc != nullptr);
if (!(*ppFunc)->IsLibraryCode())
{
return true;
}
}
return false;
}
/*static*/
bool JavascriptStackWalker::IsLibraryStackFrameEnabled(Js::ScriptContext * scriptContext)
{
Assert(scriptContext != nullptr);
return CONFIG_FLAG(LibraryStackFrame);
}
// Check if a function is a display caller: user code, or native library / boundary script library code
bool JavascriptStackWalker::IsDisplayCaller(JavascriptFunction* func)
{
FunctionBody* body = func->GetFunctionBody();
if (IsLibraryStackFrameEnabled(func->GetScriptContext()))
{
return !func->IsScriptFunction() || !body->GetUtf8SourceInfo()->GetIsLibraryCode() || body->IsPublicLibraryCode();
}
else
{
return !body->GetUtf8SourceInfo()->GetIsLibraryCode();
}
}
bool JavascriptStackWalker::GetDisplayCaller(_Out_opt_ JavascriptFunction ** ppFunc)
{
while (this->GetCaller(ppFunc))
{
Assert(ppFunc != nullptr);
__analysis_assume(ppFunc != nullptr);
if (IsDisplayCaller(*ppFunc))
{
return true;
}
}
return false;
}
PCWSTR JavascriptStackWalker::GetCurrentNativeLibraryEntryName() const
{
Assert(IsLibraryStackFrameEnabled(this->scriptContext)
&& this->prevNativeLibraryEntry
&& this->prevNativeLibraryEntry->next == this->nativeLibraryEntry);
return this->prevNativeLibraryEntry->name;
}
// WalkToTarget skips internal frames
BOOL JavascriptStackWalker::WalkToTarget(JavascriptFunction * funcTarget)
{
// Walk up the call stack until we find the frame that belongs to the given function.
while (this->Walk(/*includeInlineFrames*/ true))
{
if (this->IsJavascriptFrame() && this->GetCurrentFunction() == funcTarget)
{
// Skip internal names
Assert( !(this->GetCallInfo().Flags & CallFlags_InternalFrame) );
return true;
}
}
return false;
}
bool JavascriptStackWalker::AlignAndCheckAddressOfReturnAddressMatch(void* addressOfReturnAddress, void* nativeLibraryEntryAddress)
{
return addressOfReturnAddress == nativeLibraryEntryAddress
#if defined(_M_IX86)
// Under some odd cases on x86, addressOfReturnAddress and stashed entry address need to be aligned.
// This happens when code is generated using two stack pointers. One or both have the address of
// return address offset by 4, 8, or 12.
|| (((uint)nativeLibraryEntryAddress - (uint)addressOfReturnAddress < 0x10) &&
*(void**)addressOfReturnAddress == *(void**)nativeLibraryEntryAddress
)
#endif
;
}
void ** JavascriptStackWalker::GetCurrentArgv() const
{
Assert(this->IsJavascriptFrame());
Assert(this->interpreterFrame != nullptr ||
(this->prevNativeLibraryEntry && AlignAndCheckAddressOfReturnAddressMatch(this->currentFrame.GetAddressOfReturnAddress(), this->prevNativeLibraryEntry->addr)) ||
JavascriptFunction::IsNativeAddress(this->scriptContext, (void*)this->currentFrame.GetInstructionPointer()));
bool isNativeAddr = (this->interpreterFrame == nullptr) &&
(!this->prevNativeLibraryEntry || !AlignAndCheckAddressOfReturnAddressMatch(this->currentFrame.GetAddressOfReturnAddress(), this->prevNativeLibraryEntry->addr));
void ** argv = currentFrame.GetArgv(isNativeAddr, false /*shouldCheckForNativeAddr*/);
Assert(argv);
return argv;
}
bool JavascriptStackWalker::CheckJavascriptFrame(bool includeInlineFrames)
{
this->isNativeLibraryFrame = false; // Clear previous result
void * codeAddr = this->currentFrame.GetInstructionPointer();
if (this->tempInterpreterFrame && codeAddr == this->tempInterpreterFrame->GetReturnAddress())
{
bool isBailoutInterpreter = this->tempInterpreterFrame->TestFlags(Js::InterpreterStackFrameFlags_FromBailOut);
// We need to skip over the first interpreter frame on the stack if it is the partially initialized frame
// otherwise it is a real frame and we should continue.
// For fully initialized frames (PushPopHelper was called) the thunk stack addr is equal or below addressOfReturnAddress
// as the latter one is obtained in InterpreterStackFrame::InterpreterThunk called by the thunk.
bool isPartiallyInitializedFrame = this->shouldDetectPartiallyInitializedInterpreterFrame &&
this->currentFrame.GetAddressOfReturnAddress(isBailoutInterpreter /*isCurrentContextNative*/, false /*shouldCheckForNativeAddr*/) < this->tempInterpreterFrame->GetAddressOfReturnAddress();
this->shouldDetectPartiallyInitializedInterpreterFrame = false;
if (isPartiallyInitializedFrame)
{
return false; // Skip it.
}
void ** argv = this->currentFrame.GetArgv(isBailoutInterpreter /*isCurrentContextNative*/, false /*shouldCheckForNativeAddr*/);
if (argv == nullptr)
{
// NOTE: When we switch to walking the stack ourselves and skip non engine frames, this should never happen.
return false;
}
this->interpreterFrame = this->tempInterpreterFrame;
this->tempInterpreterFrame = this->interpreterFrame->GetPreviousFrame();
#if ENABLE_NATIVE_CODEGEN
#if DBG
if (((CallInfo const *)&argv[JavascriptFunctionArgIndex_CallInfo])->Flags & CallFlags_InternalFrame)
{
// The return address of the interpreterFrame is the same as the entryPoint for a jitted loop body.
// This can only ever happen when we have bailed out from a function inlined in the loop body. We
// wouldn't have created a new interpreterFrame if the bailout were from the loop body itself.
Assert(this->interpreterFrame->TestFlags(Js::InterpreterStackFrameFlags_FromBailOut));
InlinedFrameWalker tmpFrameWalker;
Assert(InlinedFrameWalker::FromPhysicalFrame(tmpFrameWalker, currentFrame, Js::ScriptFunction::FromVar(argv[JavascriptFunctionArgIndex_Function]),
true /*fromBailout*/, this->tempInterpreterFrame->GetCurrentLoopNum(), this, false /*useInternalFrameInfo*/, true /*noAlloc*/));
tmpFrameWalker.Close();
}
#endif //DBG
#endif //ENABLE_NATIVE_CODEGEN
return true;
}
if (IsLibraryStackFrameEnabled(this->scriptContext) && this->nativeLibraryEntry)
{
void* addressOfReturnAddress = this->currentFrame.GetAddressOfReturnAddress();
void* nativeLibraryEntryAddress = this->nativeLibraryEntry->addr;
AssertMsg(addressOfReturnAddress <= nativeLibraryEntryAddress, "Missed matching native library entry?");
if (AlignAndCheckAddressOfReturnAddressMatch(addressOfReturnAddress, nativeLibraryEntryAddress))
{
this->isNativeLibraryFrame = true;
this->shouldDetectPartiallyInitializedInterpreterFrame = false;
this->prevNativeLibraryEntry = this->nativeLibraryEntry; // Saves match in prevNativeLibraryEntry
this->nativeLibraryEntry = this->nativeLibraryEntry->next;
return true;
}
}
#if ENABLE_NATIVE_CODEGEN
BOOL isNativeAddr = JavascriptFunction::IsNativeAddress(this->scriptContext, codeAddr);
if (isNativeAddr)
{
this->shouldDetectPartiallyInitializedInterpreterFrame = false;
void ** argv = this->currentFrame.GetArgv(true /*isCurrentContextNative*/, false /*shouldCheckForNativeAddr*/);
if (argv == nullptr)
{
// NOTE: When we switch to walking the stack ourselves and skip non engine frames, this should never happen.
return false;
}
ScriptFunction* funcObj = Js::ScriptFunction::FromVar(argv[JavascriptFunctionArgIndex_Function]);
if (funcObj->GetFunctionBody()->GetIsAsmjsMode())
{
return false;
}