ThreadContext.cpp 157 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeBasePch.h"
  6. #include "BackendApi.h"
  7. #include "ThreadServiceWrapper.h"
  8. #include "Types/TypePropertyCache.h"
  9. #include "Debug/DebuggingFlags.h"
  10. #include "Debug/DiagProbe.h"
  11. #include "Debug/DebugManager.h"
  12. #include "Chars.h"
  13. #include "CaseInsensitive.h"
  14. #include "CharSet.h"
  15. #include "CharMap.h"
  16. #include "StandardChars.h"
  17. #include "Base/ThreadContextTlsEntry.h"
  18. #include "Base/ThreadBoundThreadContextManager.h"
  19. #include "Language/SourceDynamicProfileManager.h"
  20. #include "Language/CodeGenRecyclableData.h"
  21. #include "Language/InterpreterStackFrame.h"
  22. #include "Language/JavascriptStackWalker.h"
  23. #include "Base/ScriptMemoryDumper.h"
  24. // SIMD_JS
  25. #include "Library/SimdLib.h"
  26. #if DBG
  27. #include "Memory/StressTest.h"
  28. #endif
  29. #ifdef DYNAMIC_PROFILE_MUTATOR
  30. #include "Language/DynamicProfileMutator.h"
  31. #endif
  32. #ifdef ENABLE_BASIC_TELEMETRY
  33. #include "Telemetry.h"
  34. #endif
  35. const int TotalNumberOfBuiltInProperties = Js::PropertyIds::_countJSOnlyProperty;
  36. /*
  37. * When we aren't adding any additional properties
  38. */
  39. void DefaultInitializeAdditionalProperties(ThreadContext *threadContext)
  40. {
  41. }
  42. /*
  43. *
  44. */
  45. void (*InitializeAdditionalProperties)(ThreadContext *threadContext) = DefaultInitializeAdditionalProperties;
  46. // To make sure the marker function doesn't get inlined, optimized away, or merged with other functions we disable optimization.
  47. // If this method ends up causing a perf problem in the future, we should replace it with asm versions which should be lighter.
  48. #pragma optimize("g", off)
  49. _NOINLINE extern "C" void* MarkerForExternalDebugStep()
  50. {
  51. // We need to return something here to prevent this function from being merged with other empty functions by the linker.
  52. static int __dummy;
  53. return &__dummy;
  54. }
  55. #pragma optimize("", on)
  56. CriticalSection ThreadContext::s_csThreadContext;
  57. size_t ThreadContext::processNativeCodeSize = 0;
  58. ThreadContext * ThreadContext::globalListFirst = nullptr;
  59. ThreadContext * ThreadContext::globalListLast = nullptr;
  60. THREAD_LOCAL uint ThreadContext::activeScriptSiteCount = 0;
  61. const Js::PropertyRecord * const ThreadContext::builtInPropertyRecords[] =
  62. {
  63. Js::BuiltInPropertyRecords::EMPTY,
  64. #define ENTRY_INTERNAL_SYMBOL(n) Js::BuiltInPropertyRecords::n,
  65. #define ENTRY_SYMBOL(n, d) Js::BuiltInPropertyRecords::n,
  66. #define ENTRY(n) Js::BuiltInPropertyRecords::n,
  67. #define ENTRY2(n, s) ENTRY(n)
  68. #include "Base/JnDirectFields.h"
  69. };
  70. ThreadContext::RecyclableData::RecyclableData(Recycler *const recycler) :
  71. soErrorObject(nullptr, nullptr, nullptr, true),
  72. oomErrorObject(nullptr, nullptr, nullptr, true),
  73. terminatedErrorObject(nullptr, nullptr, nullptr),
  74. typesWithProtoPropertyCache(recycler),
  75. propertyGuards(recycler, 128),
  76. oldEntryPointInfo(nullptr),
  77. returnedValueList(nullptr),
  78. constructorCacheInvalidationCount(0)
  79. {
  80. }
  81. ThreadContext::ThreadContext(AllocationPolicyManager * allocationPolicyManager, JsUtil::ThreadService::ThreadServiceCallback threadServiceCallback, bool enableExperimentalFeatures) :
  82. currentThreadId(::GetCurrentThreadId()),
  83. stackLimitForCurrentThread(0),
  84. stackProber(nullptr),
  85. isThreadBound(false),
  86. hasThrownPendingException(false),
  87. noScriptScope(false),
  88. heapEnum(nullptr),
  89. threadContextFlags(ThreadContextFlagNoFlag),
  90. JsUtil::DoublyLinkedListElement<ThreadContext>(),
  91. allocationPolicyManager(allocationPolicyManager),
  92. threadService(threadServiceCallback),
  93. isOptimizedForManyInstances(Js::Configuration::Global.flags.OptimizeForManyInstances),
  94. bgJit(Js::Configuration::Global.flags.BgJit),
  95. pageAllocator(allocationPolicyManager, PageAllocatorType_Thread, Js::Configuration::Global.flags, 0, PageAllocator::DefaultMaxFreePageCount,
  96. false
  97. #if ENABLE_BACKGROUND_PAGE_FREEING
  98. , &backgroundPageQueue
  99. #endif
  100. ),
  101. recycler(nullptr),
  102. hasCollectionCallBack(false),
  103. callDispose(true),
  104. #if ENABLE_NATIVE_CODEGEN
  105. jobProcessor(nullptr),
  106. #endif
  107. interruptPoller(nullptr),
  108. expirableCollectModeGcCount(-1),
  109. expirableObjectList(nullptr),
  110. expirableObjectDisposeList(nullptr),
  111. numExpirableObjects(0),
  112. disableExpiration(false),
  113. callRootLevel(0),
  114. nextTypeId((Js::TypeId)Js::Constants::ReservedTypeIds),
  115. entryExitRecord(nullptr),
  116. leafInterpreterFrame(nullptr),
  117. threadServiceWrapper(nullptr),
  118. temporaryArenaAllocatorCount(0),
  119. temporaryGuestArenaAllocatorCount(0),
  120. crefSContextForDiag(0),
  121. m_prereservedRegionAddr(0),
  122. scriptContextList(nullptr),
  123. scriptContextEverRegistered(false),
  124. #if DBG_DUMP || defined(PROFILE_EXEC)
  125. topLevelScriptSite(nullptr),
  126. #endif
  127. polymorphicCacheState(0),
  128. stackProbeCount(0),
  129. #ifdef BAILOUT_INJECTION
  130. bailOutByteCodeLocationCount(0),
  131. #endif
  132. sourceCodeSize(0),
  133. nativeCodeSize(0),
  134. threadAlloc(_u("TC"), GetPageAllocator(), Js::Throw::OutOfMemory),
  135. inlineCacheThreadInfoAllocator(_u("TC-InlineCacheInfo"), GetPageAllocator(), Js::Throw::OutOfMemory),
  136. isInstInlineCacheThreadInfoAllocator(_u("TC-IsInstInlineCacheInfo"), GetPageAllocator(), Js::Throw::OutOfMemory),
  137. equivalentTypeCacheInfoAllocator(_u("TC-EquivalentTypeCacheInfo"), GetPageAllocator(), Js::Throw::OutOfMemory),
  138. preReservedVirtualAllocator(),
  139. protoInlineCacheByPropId(&inlineCacheThreadInfoAllocator, 512),
  140. storeFieldInlineCacheByPropId(&inlineCacheThreadInfoAllocator, 256),
  141. isInstInlineCacheByFunction(&isInstInlineCacheThreadInfoAllocator, 128),
  142. registeredInlineCacheCount(0),
  143. unregisteredInlineCacheCount(0),
  144. prototypeChainEnsuredToHaveOnlyWritableDataPropertiesAllocator(_u("TC-ProtoWritableProp"), GetPageAllocator(), Js::Throw::OutOfMemory),
  145. standardUTF8Chars(0),
  146. standardUnicodeChars(0),
  147. hasUnhandledException(FALSE),
  148. hasCatchHandler(FALSE),
  149. disableImplicitFlags(DisableImplicitNoFlag),
  150. hasCatchHandlerToUserCode(false),
  151. caseInvariantPropertySet(nullptr),
  152. entryPointToBuiltInOperationIdCache(&threadAlloc, 0),
  153. #if ENABLE_NATIVE_CODEGEN
  154. #if !FLOATVAR
  155. codeGenNumberThreadAllocator(nullptr),
  156. xProcNumberPageSegmentManager(nullptr),
  157. #endif
  158. m_jitNumericProperties(nullptr),
  159. m_jitNeedsPropertyUpdate(false),
  160. #if DYNAMIC_INTERPRETER_THUNK || defined(ASMJS_PLAT)
  161. thunkPageAllocators(allocationPolicyManager, /* allocXData */ false, /* virtualAllocator */ nullptr, GetCurrentProcess()),
  162. #endif
  163. codePageAllocators(allocationPolicyManager, ALLOC_XDATA, GetPreReservedVirtualAllocator(), GetCurrentProcess()),
  164. #endif
  165. dynamicObjectEnumeratorCacheMap(&HeapAllocator::Instance, 16),
  166. //threadContextFlags(ThreadContextFlagNoFlag),
  167. #ifdef NTBUILD
  168. telemetryBlock(&localTelemetryBlock),
  169. #endif
  170. configuration(enableExperimentalFeatures),
  171. jsrtRuntime(nullptr),
  172. propertyMap(nullptr),
  173. rootPendingClose(nullptr),
  174. exceptionCode(0),
  175. isProfilingUserCode(true),
  176. loopDepth(0),
  177. redeferralState(InitialRedeferralState),
  178. gcSinceLastRedeferral(0),
  179. gcSinceCallCountsCollected(0),
  180. tridentLoadAddress(nullptr),
  181. m_remoteThreadContextInfo(nullptr),
  182. debugManager(nullptr)
  183. #if ENABLE_TTD
  184. , TTDContext(nullptr)
  185. , TTDExecutionInfo(nullptr)
  186. , TTDLog(nullptr)
  187. , TTDRootNestingCount(0)
  188. #endif
  189. #ifdef ENABLE_DIRECTCALL_TELEMETRY
  190. , directCallTelemetry(this)
  191. #endif
  192. #if ENABLE_JS_REENTRANCY_CHECK
  193. , noJsReentrancy(false)
  194. #endif
  195. {
  196. pendingProjectionContextCloseList = JsUtil::List<IProjectionContext*, ArenaAllocator>::New(GetThreadAlloc());
  197. hostScriptContextStack = Anew(GetThreadAlloc(), JsUtil::Stack<HostScriptContext*>, GetThreadAlloc());
  198. functionCount = 0;
  199. sourceInfoCount = 0;
  200. #if DBG || defined(RUNTIME_DATA_COLLECTION)
  201. scriptContextCount = 0;
  202. #endif
  203. isScriptActive = false;
  204. #ifdef ENABLE_CUSTOM_ENTROPY
  205. entropy.Initialize();
  206. #endif
  207. #if ENABLE_NATIVE_CODEGEN
  208. this->bailOutRegisterSaveSpace = AnewArrayZ(this->GetThreadAlloc(), Js::Var, GetBailOutRegisterSaveSlotCount());
  209. #endif
  210. #if defined(ENABLE_SIMDJS) && ENABLE_NATIVE_CODEGEN
  211. simdFuncInfoToOpcodeMap = Anew(this->GetThreadAlloc(), FuncInfoToOpcodeMap, this->GetThreadAlloc());
  212. simdOpcodeToSignatureMap = AnewArrayZ(this->GetThreadAlloc(), SimdFuncSignature, Js::Simd128OpcodeCount());
  213. {
  214. #define MACRO_SIMD_WMS(op, LayoutAsmJs, OpCodeAttrAsmJs, OpCodeAttr, ...) \
  215. AddSimdFuncToMaps(Js::OpCode::##op, __VA_ARGS__);
  216. #define MACRO_SIMD_EXTEND_WMS(op, LayoutAsmJs, OpCodeAttrAsmJs, OpCodeAttr, ...) MACRO_SIMD_WMS(op, LayoutAsmJs, OpCodeAttrAsmJs, OpCodeAttr, __VA_ARGS__)
  217. #include "ByteCode/OpCodesSimd.h"
  218. }
  219. #endif // defined(ENABLE_SIMDJS) && ENABLE_NATIVE_CODEGEN
  220. #if DBG_DUMP
  221. scriptSiteCount = 0;
  222. pageAllocator.debugName = _u("Thread");
  223. #endif
  224. #ifdef DYNAMIC_PROFILE_MUTATOR
  225. this->dynamicProfileMutator = DynamicProfileMutator::GetMutator();
  226. #endif
  227. PERF_COUNTER_INC(Basic, ThreadContext);
  228. #ifdef LEAK_REPORT
  229. this->rootTrackerScriptContext = nullptr;
  230. this->threadId = ::GetCurrentThreadId();
  231. #endif
  232. #ifdef NTBUILD
  233. memset(&localTelemetryBlock, 0, sizeof(localTelemetryBlock));
  234. #endif
  235. AutoCriticalSection autocs(ThreadContext::GetCriticalSection());
  236. ThreadContext::LinkToBeginning(this, &ThreadContext::globalListFirst, &ThreadContext::globalListLast);
  237. #if DBG
  238. // Since we created our page allocator while we were constructing this thread context
  239. // it will pick up the thread context id that is current on the thread. We need to update
  240. // that now.
  241. pageAllocator.UpdateThreadContextHandle((ThreadContextId)this);
  242. #endif
  243. #ifdef ENABLE_PROJECTION
  244. #if DBG_DUMP
  245. this->projectionMemoryInformation = nullptr;
  246. #endif
  247. #endif
  248. this->InitAvailableCommit();
  249. }
  250. void ThreadContext::InitAvailableCommit()
  251. {
  252. // Once per process: get the available commit for the process from the OS and push it to the AutoSystemInfo.
  253. // (This must be done lazily, outside DllMain. And it must be done from the Runtime, since the common lib
  254. // doesn't have access to the DelayLoadLibrary stuff.)
  255. ULONG64 commit;
  256. BOOL success = AutoSystemInfo::Data.GetAvailableCommit(&commit);
  257. if (!success)
  258. {
  259. commit = (ULONG64)-1;
  260. #ifdef NTBUILD
  261. APP_MEMORY_INFORMATION AppMemInfo;
  262. success = GetWinCoreProcessThreads()->GetProcessInformation(
  263. GetCurrentProcess(),
  264. ProcessAppMemoryInfo,
  265. &AppMemInfo,
  266. sizeof(AppMemInfo));
  267. if (success)
  268. {
  269. commit = AppMemInfo.AvailableCommit;
  270. }
  271. #endif
  272. AutoSystemInfo::Data.SetAvailableCommit(commit);
  273. }
  274. }
  275. void ThreadContext::SetStackProber(StackProber * stackProber)
  276. {
  277. this->stackProber = stackProber;
  278. if (stackProber != NULL && this->stackLimitForCurrentThread != Js::Constants::StackLimitForScriptInterrupt)
  279. {
  280. this->stackLimitForCurrentThread = stackProber->GetScriptStackLimit();
  281. }
  282. }
  283. size_t ThreadContext::GetScriptStackLimit() const
  284. {
  285. return stackProber->GetScriptStackLimit();
  286. }
  287. HANDLE
  288. ThreadContext::GetProcessHandle() const
  289. {
  290. return GetCurrentProcess();
  291. }
  292. intptr_t
  293. ThreadContext::GetThreadStackLimitAddr() const
  294. {
  295. return (intptr_t)GetAddressOfStackLimitForCurrentThread();
  296. }
  297. #if ENABLE_NATIVE_CODEGEN && defined(ENABLE_SIMDJS) && (defined(_M_IX86) || defined(_M_X64))
  298. intptr_t
  299. ThreadContext::GetSimdTempAreaAddr(uint8 tempIndex) const
  300. {
  301. return (intptr_t)&X86_TEMP_SIMD[tempIndex];
  302. }
  303. #endif
  304. intptr_t
  305. ThreadContext::GetDisableImplicitFlagsAddr() const
  306. {
  307. return (intptr_t)&disableImplicitFlags;
  308. }
  309. intptr_t
  310. ThreadContext::GetImplicitCallFlagsAddr() const
  311. {
  312. return (intptr_t)&implicitCallFlags;
  313. }
  314. ptrdiff_t
  315. ThreadContext::GetChakraBaseAddressDifference() const
  316. {
  317. return 0;
  318. }
  319. ptrdiff_t
  320. ThreadContext::GetCRTBaseAddressDifference() const
  321. {
  322. return 0;
  323. }
  324. IActiveScriptProfilerHeapEnum* ThreadContext::GetHeapEnum()
  325. {
  326. return heapEnum;
  327. }
  328. void ThreadContext::SetHeapEnum(IActiveScriptProfilerHeapEnum* newHeapEnum)
  329. {
  330. Assert((newHeapEnum != nullptr && heapEnum == nullptr) || (newHeapEnum == nullptr && heapEnum != nullptr));
  331. heapEnum = newHeapEnum;
  332. }
  333. void ThreadContext::ClearHeapEnum()
  334. {
  335. Assert(heapEnum != nullptr);
  336. heapEnum = nullptr;
  337. }
  338. void ThreadContext::GlobalInitialize()
  339. {
  340. for (int i = 0; i < _countof(builtInPropertyRecords); i++)
  341. {
  342. builtInPropertyRecords[i]->SetHash(JsUtil::CharacterBuffer<WCHAR>::StaticGetHashCode(builtInPropertyRecords[i]->GetBuffer(), builtInPropertyRecords[i]->GetLength()));
  343. }
  344. }
  345. ThreadContext::~ThreadContext()
  346. {
  347. {
  348. AutoCriticalSection autocs(ThreadContext::GetCriticalSection());
  349. ThreadContext::Unlink(this, &ThreadContext::globalListFirst, &ThreadContext::globalListLast);
  350. }
  351. #if ENABLE_TTD
  352. if(this->TTDContext != nullptr)
  353. {
  354. TT_HEAP_DELETE(TTD::ThreadContextTTD, this->TTDContext);
  355. this->TTDContext = nullptr;
  356. }
  357. if(this->TTDExecutionInfo != nullptr)
  358. {
  359. TT_HEAP_DELETE(TTD::ThreadContextTTD, this->TTDExecutionInfo);
  360. this->TTDExecutionInfo = nullptr;
  361. }
  362. if(this->TTDLog != nullptr)
  363. {
  364. TT_HEAP_DELETE(TTD::EventLog, this->TTDLog);
  365. this->TTDLog = nullptr;
  366. }
  367. #endif
  368. #ifdef LEAK_REPORT
  369. if (Js::Configuration::Global.flags.IsEnabled(Js::LeakReportFlag))
  370. {
  371. AUTO_LEAK_REPORT_SECTION(Js::Configuration::Global.flags, _u("Thread Context (%p): %s (TID: %d)"), this,
  372. this->GetRecycler()->IsInDllCanUnloadNow()? _u("DllCanUnloadNow") :
  373. this->GetRecycler()->IsInDetachProcess()? _u("DetachProcess") : _u("Destructor"), this->threadId);
  374. LeakReport::DumpUrl(this->threadId);
  375. }
  376. #endif
  377. if (interruptPoller)
  378. {
  379. HeapDelete(interruptPoller);
  380. interruptPoller = nullptr;
  381. }
  382. #if DBG
  383. // ThreadContext dtor may be running on a different thread.
  384. // Recycler may call finalizer that free temp Arenas, which will free pages back to
  385. // the page Allocator, which will try to suspend idle on a different thread.
  386. // So we need to disable idle decommit asserts.
  387. pageAllocator.ShutdownIdleDecommit();
  388. #endif
  389. // Allocating memory during the shutdown codepath is not preferred
  390. // so we'll close the page allocator before we release the GC
  391. // If any dispose is allocating memory during shutdown, that is a bug
  392. pageAllocator.Close();
  393. // The recycler need to delete before the background code gen thread
  394. // because that might run finalizer which need access to the background code gen thread.
  395. if (recycler != nullptr)
  396. {
  397. for (Js::ScriptContext *scriptContext = scriptContextList; scriptContext; scriptContext = scriptContext->next)
  398. {
  399. if (!scriptContext->IsActuallyClosed())
  400. {
  401. // We close ScriptContext here because anyhow HeapDelete(recycler) when disposing the
  402. // JavaScriptLibrary will close ScriptContext. Explicit close gives us chance to clear
  403. // other things to which ScriptContext holds reference to
  404. AssertMsg(!IsInScript(), "Can we be in script here?");
  405. scriptContext->MarkForClose();
  406. }
  407. }
  408. // If all scriptContext's have been closed, then the sourceProfileManagersByUrl
  409. // should have been released
  410. AssertMsg(this->recyclableData->sourceProfileManagersByUrl == nullptr ||
  411. this->recyclableData->sourceProfileManagersByUrl->Count() == 0, "There seems to have been a refcounting imbalance.");
  412. this->recyclableData->sourceProfileManagersByUrl = nullptr;
  413. this->recyclableData->oldEntryPointInfo = nullptr;
  414. if (this->recyclableData->symbolRegistrationMap != nullptr)
  415. {
  416. this->recyclableData->symbolRegistrationMap->Clear();
  417. this->recyclableData->symbolRegistrationMap = nullptr;
  418. }
  419. if (this->recyclableData->returnedValueList != nullptr)
  420. {
  421. this->recyclableData->returnedValueList->Clear();
  422. this->recyclableData->returnedValueList = nullptr;
  423. }
  424. if (this->propertyMap != nullptr)
  425. {
  426. HeapDelete(this->propertyMap);
  427. this->propertyMap = nullptr;
  428. }
  429. #if ENABLE_NATIVE_CODEGEN
  430. if (this->m_jitNumericProperties != nullptr)
  431. {
  432. HeapDelete(this->m_jitNumericProperties);
  433. this->m_jitNumericProperties = nullptr;
  434. }
  435. #endif
  436. // Unpin the memory for leak report so we don't report this as a leak.
  437. recyclableData.Unroot(recycler);
  438. #if defined(LEAK_REPORT) || defined(CHECK_MEMORY_LEAK)
  439. for (Js::ScriptContext *scriptContext = scriptContextList; scriptContext; scriptContext = scriptContext->next)
  440. {
  441. scriptContext->ClearSourceContextInfoMaps();
  442. scriptContext->ShutdownClearSourceLists();
  443. }
  444. #ifdef LEAK_REPORT
  445. // heuristically figure out which one is the root tracker script engine
  446. // and force close on it
  447. if (this->rootTrackerScriptContext != nullptr)
  448. {
  449. this->rootTrackerScriptContext->Close(false);
  450. }
  451. #endif
  452. #endif
  453. #if ENABLE_NATIVE_CODEGEN
  454. #if !FLOATVAR
  455. if (this->codeGenNumberThreadAllocator)
  456. {
  457. HeapDelete(this->codeGenNumberThreadAllocator);
  458. this->codeGenNumberThreadAllocator = nullptr;
  459. }
  460. if (this->xProcNumberPageSegmentManager)
  461. {
  462. HeapDelete(this->xProcNumberPageSegmentManager);
  463. this->xProcNumberPageSegmentManager = nullptr;
  464. }
  465. #endif
  466. #endif
  467. Assert(this->debugManager == nullptr);
  468. HeapDelete(recycler);
  469. }
  470. #if ENABLE_NATIVE_CODEGEN
  471. if(jobProcessor)
  472. {
  473. if(this->bgJit)
  474. {
  475. HeapDelete(static_cast<JsUtil::BackgroundJobProcessor *>(jobProcessor));
  476. }
  477. else
  478. {
  479. HeapDelete(static_cast<JsUtil::ForegroundJobProcessor *>(jobProcessor));
  480. }
  481. jobProcessor = nullptr;
  482. }
  483. #endif
  484. // Do not require all GC callbacks to be revoked, because Trident may not revoke if there
  485. // is a leak, and we don't want the leak to be masked by an assert
  486. #ifdef ENABLE_PROJECTION
  487. externalWeakReferenceCacheList.Clear(&HeapAllocator::Instance);
  488. #endif
  489. this->collectCallBackList.Clear(&HeapAllocator::Instance);
  490. this->protoInlineCacheByPropId.Reset();
  491. this->storeFieldInlineCacheByPropId.Reset();
  492. this->isInstInlineCacheByFunction.Reset();
  493. this->equivalentTypeCacheEntryPoints.Reset();
  494. this->prototypeChainEnsuredToHaveOnlyWritableDataPropertiesScriptContext.Reset();
  495. this->registeredInlineCacheCount = 0;
  496. this->unregisteredInlineCacheCount = 0;
  497. AssertMsg(this->GetHeapEnum() == nullptr, "Heap enumeration should have been cleared/closed by the ScriptSite.");
  498. if (this->GetHeapEnum() != nullptr)
  499. {
  500. this->ClearHeapEnum();
  501. }
  502. #ifdef BAILOUT_INJECTION
  503. if (Js::Configuration::Global.flags.IsEnabled(Js::BailOutByteCodeFlag)
  504. && Js::Configuration::Global.flags.BailOutByteCode.Empty())
  505. {
  506. Output::Print(_u("Bail out byte code location count: %d"), this->bailOutByteCodeLocationCount);
  507. }
  508. #endif
  509. Assert(processNativeCodeSize >= nativeCodeSize);
  510. ::InterlockedExchangeSubtract(&processNativeCodeSize, nativeCodeSize);
  511. PERF_COUNTER_DEC(Basic, ThreadContext);
  512. #ifdef DYNAMIC_PROFILE_MUTATOR
  513. if (this->dynamicProfileMutator != nullptr)
  514. {
  515. this->dynamicProfileMutator->Delete();
  516. }
  517. #endif
  518. #ifdef ENABLE_PROJECTION
  519. #if DBG_DUMP
  520. if (this->projectionMemoryInformation)
  521. {
  522. this->projectionMemoryInformation->Release();
  523. this->projectionMemoryInformation = nullptr;
  524. }
  525. #endif
  526. #endif
  527. }
  528. void
  529. ThreadContext::SetJSRTRuntime(void* runtime)
  530. {
  531. Assert(jsrtRuntime == nullptr);
  532. jsrtRuntime = runtime;
  533. #ifdef ENABLE_BASIC_TELEMETRY
  534. Telemetry::EnsureInitializeForJSRT();
  535. #endif
  536. }
  537. void ThreadContext::CloseForJSRT()
  538. {
  539. // This is used for JSRT APIs only.
  540. Assert(this->jsrtRuntime);
  541. #ifdef ENABLE_BASIC_TELEMETRY
  542. // log any relevant telemetry before disposing the current thread for cases which are properly shutdown
  543. Telemetry::OnJSRTThreadContextClose();
  544. #endif
  545. ShutdownThreads();
  546. }
  547. ThreadContext* ThreadContext::GetContextForCurrentThread()
  548. {
  549. ThreadContextTLSEntry * tlsEntry = ThreadContextTLSEntry::GetEntryForCurrentThread();
  550. if (tlsEntry != nullptr)
  551. {
  552. return static_cast<ThreadContext *>(tlsEntry->GetThreadContext());
  553. }
  554. return nullptr;
  555. }
  556. void ThreadContext::ValidateThreadContext()
  557. {
  558. #if DBG
  559. // verify the runtime pointer is valid.
  560. {
  561. BOOL found = FALSE;
  562. AutoCriticalSection autocs(ThreadContext::GetCriticalSection());
  563. ThreadContext* currentThreadContext = GetThreadContextList();
  564. while (currentThreadContext)
  565. {
  566. if (currentThreadContext == this)
  567. {
  568. return;
  569. }
  570. currentThreadContext = currentThreadContext->Next();
  571. }
  572. AssertMsg(found, "invalid thread context");
  573. }
  574. #endif
  575. }
  576. #if ENABLE_NATIVE_CODEGEN && defined(ENABLE_SIMDJS)
  577. void ThreadContext::AddSimdFuncToMaps(Js::OpCode op, ...)
  578. {
  579. Assert(simdFuncInfoToOpcodeMap != nullptr);
  580. Assert(simdOpcodeToSignatureMap != nullptr);
  581. va_list arguments;
  582. va_start(arguments, op);
  583. int argumentsCount = va_arg(arguments, int);
  584. AssertMsg(argumentsCount >= 0 && argumentsCount <= 20, "Invalid arguments count for SIMD opcode");
  585. if (argumentsCount == 0)
  586. {
  587. // no info to add
  588. return;
  589. }
  590. Js::FunctionInfo *funcInfo = va_arg(arguments, Js::FunctionInfo*);
  591. AddSimdFuncInfo(op, funcInfo);
  592. SimdFuncSignature simdFuncSignature;
  593. simdFuncSignature.valid = true;
  594. simdFuncSignature.argCount = argumentsCount - 2; // arg count to Simd func = argumentsCount - FuncInfo and return Type fields.
  595. simdFuncSignature.returnType = va_arg(arguments, ValueType);
  596. simdFuncSignature.args = AnewArrayZ(this->GetThreadAlloc(), ValueType, simdFuncSignature.argCount);
  597. for (uint iArg = 0; iArg < simdFuncSignature.argCount; iArg++)
  598. {
  599. simdFuncSignature.args[iArg] = va_arg(arguments, ValueType);
  600. }
  601. simdOpcodeToSignatureMap[Js::SIMDUtils::SimdOpcodeAsIndex(op)] = simdFuncSignature;
  602. va_end(arguments);
  603. }
  604. void ThreadContext::AddSimdFuncInfo(Js::OpCode op, Js::FunctionInfo *funcInfo)
  605. {
  606. // primary funcInfo
  607. simdFuncInfoToOpcodeMap->AddNew(funcInfo, op);
  608. // Entry points of SIMD loads/stores of non-full width all map to the same opcode. This is not captured in the opcode table, so add additional entry points here.
  609. switch (op)
  610. {
  611. case Js::OpCode::Simd128_LdArr_F4:
  612. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDFloat32x4Lib::EntryInfo::Load1, op);
  613. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDFloat32x4Lib::EntryInfo::Load2, op);
  614. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDFloat32x4Lib::EntryInfo::Load3, op);
  615. break;
  616. case Js::OpCode::Simd128_StArr_F4:
  617. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDFloat32x4Lib::EntryInfo::Store1, op);
  618. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDFloat32x4Lib::EntryInfo::Store2, op);
  619. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDFloat32x4Lib::EntryInfo::Store3, op);
  620. break;
  621. case Js::OpCode::Simd128_LdArr_I4:
  622. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDInt32x4Lib::EntryInfo::Load1, op);
  623. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDInt32x4Lib::EntryInfo::Load2, op);
  624. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDInt32x4Lib::EntryInfo::Load3, op);
  625. break;
  626. case Js::OpCode::Simd128_StArr_I4:
  627. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDInt32x4Lib::EntryInfo::Store1, op);
  628. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDInt32x4Lib::EntryInfo::Store2, op);
  629. simdFuncInfoToOpcodeMap->AddNew(&Js::SIMDInt32x4Lib::EntryInfo::Store3, op);
  630. break;
  631. }
  632. }
  633. Js::OpCode ThreadContext::GetSimdOpcodeFromFuncInfo(Js::FunctionInfo * funcInfo)
  634. {
  635. Assert(simdFuncInfoToOpcodeMap != nullptr);
  636. if (simdFuncInfoToOpcodeMap->ContainsKey(funcInfo))
  637. {
  638. return simdFuncInfoToOpcodeMap->Item(funcInfo);
  639. }
  640. return (Js::OpCode) 0;
  641. }
  642. void ThreadContext::GetSimdFuncSignatureFromOpcode(Js::OpCode op, SimdFuncSignature &funcSignature)
  643. {
  644. Assert(simdOpcodeToSignatureMap != nullptr);
  645. funcSignature = simdOpcodeToSignatureMap[Js::SIMDUtils::SimdOpcodeAsIndex(op)];
  646. }
  647. #endif
  648. class AutoRecyclerPtr : public AutoPtr<Recycler>
  649. {
  650. public:
  651. AutoRecyclerPtr(Recycler * ptr) : AutoPtr<Recycler>(ptr) {}
  652. ~AutoRecyclerPtr()
  653. {
  654. #if ENABLE_CONCURRENT_GC
  655. if (ptr != nullptr)
  656. {
  657. ptr->ShutdownThread();
  658. }
  659. #endif
  660. }
  661. };
  662. Recycler* ThreadContext::EnsureRecycler()
  663. {
  664. if (recycler == NULL)
  665. {
  666. AutoRecyclerPtr newRecycler(HeapNew(Recycler, GetAllocationPolicyManager(), &pageAllocator, Js::Throw::OutOfMemory, Js::Configuration::Global.flags));
  667. newRecycler->Initialize(isOptimizedForManyInstances, &threadService); // use in-thread GC when optimizing for many instances
  668. newRecycler->SetCollectionWrapper(this);
  669. #if ENABLE_NATIVE_CODEGEN
  670. // This may throw, so it needs to be after the recycler is initialized,
  671. // otherwise, the recycler dtor may encounter problems
  672. #if !FLOATVAR
  673. // TODO: we only need one of the following, one for OOP jit and one for in-proc BG JIT
  674. AutoPtr<CodeGenNumberThreadAllocator> localCodeGenNumberThreadAllocator(
  675. HeapNew(CodeGenNumberThreadAllocator, newRecycler));
  676. AutoPtr<XProcNumberPageSegmentManager> localXProcNumberPageSegmentManager(
  677. HeapNew(XProcNumberPageSegmentManager, newRecycler));
  678. #endif
  679. #endif
  680. this->recyclableData.Root(RecyclerNewZ(newRecycler, RecyclableData, newRecycler), newRecycler);
  681. if (this->IsThreadBound())
  682. {
  683. newRecycler->SetIsThreadBound();
  684. }
  685. // Assign the recycler to the ThreadContext after everything is initialized, because an OOM during initialization would
  686. // result in only partial initialization, so the 'recycler' member variable should remain null to cause full
  687. // reinitialization when requested later. Anything that happens after the Detach must have special cleanup code.
  688. this->recycler = newRecycler.Detach();
  689. try
  690. {
  691. #ifdef RECYCLER_WRITE_BARRIER
  692. #ifdef _M_X64_OR_ARM64
  693. if (!RecyclerWriteBarrierManager::OnThreadInit())
  694. {
  695. Js::Throw::OutOfMemory();
  696. }
  697. #endif
  698. #endif
  699. this->expirableObjectList = Anew(&this->threadAlloc, ExpirableObjectList, &this->threadAlloc);
  700. this->expirableObjectDisposeList = Anew(&this->threadAlloc, ExpirableObjectList, &this->threadAlloc);
  701. InitializePropertyMaps(); // has many dependencies on the recycler and other members of the thread context
  702. #if ENABLE_NATIVE_CODEGEN
  703. #if !FLOATVAR
  704. this->codeGenNumberThreadAllocator = localCodeGenNumberThreadAllocator.Detach();
  705. this->xProcNumberPageSegmentManager = localXProcNumberPageSegmentManager.Detach();
  706. #endif
  707. #endif
  708. }
  709. catch(...)
  710. {
  711. // Initialization failed, undo what was done above. Callees that throw must clean up after themselves.
  712. if (this->recyclableData != nullptr)
  713. {
  714. this->recyclableData.Unroot(this->recycler);
  715. }
  716. {
  717. // AutoRecyclerPtr's destructor takes care of shutting down the background thread and deleting the recycler
  718. AutoRecyclerPtr recyclerToDelete(this->recycler);
  719. this->recycler = nullptr;
  720. }
  721. throw;
  722. }
  723. JS_ETW(EventWriteJSCRIPT_GC_INIT(this->recycler, this->GetHiResTimer()->Now()));
  724. }
  725. #if DBG
  726. if (CONFIG_FLAG(RecyclerTest))
  727. {
  728. StressTester test(recycler);
  729. test.Run();
  730. }
  731. #endif
  732. return recycler;
  733. }
  734. Js::PropertyRecord const *
  735. ThreadContext::GetPropertyName(Js::PropertyId propertyId)
  736. {
  737. // This API should only be use on the main thread
  738. Assert(GetCurrentThreadContextId() == (ThreadContextId)this);
  739. return this->GetPropertyNameImpl<false>(propertyId);
  740. }
  741. Js::PropertyRecord const *
  742. ThreadContext::GetPropertyNameLocked(Js::PropertyId propertyId)
  743. {
  744. return GetPropertyNameImpl<true>(propertyId);
  745. }
  746. template <bool locked>
  747. Js::PropertyRecord const *
  748. ThreadContext::GetPropertyNameImpl(Js::PropertyId propertyId)
  749. {
  750. //TODO: Remove this when completely transformed to use PropertyRecord*. Currently this is only partially done,
  751. // and there are calls to GetPropertyName with InternalPropertyId.
  752. if (propertyId >= 0 && Js::IsInternalPropertyId(propertyId))
  753. {
  754. return Js::InternalPropertyRecords::GetInternalPropertyName(propertyId);
  755. }
  756. int propertyIndex = propertyId - Js::PropertyIds::_none;
  757. if (propertyIndex < 0 || propertyIndex > propertyMap->GetLastIndex())
  758. {
  759. propertyIndex = 0;
  760. }
  761. const Js::PropertyRecord * propertyRecord = nullptr;
  762. if (locked) { propertyMap->LockResize(); }
  763. bool found = propertyMap->TryGetValueAt(propertyIndex, &propertyRecord);
  764. if (locked) { propertyMap->UnlockResize(); }
  765. AssertMsg(found && propertyRecord != nullptr, "using invalid propertyid");
  766. return propertyRecord;
  767. }
  768. void
  769. ThreadContext::FindPropertyRecord(Js::JavascriptString *pstName, Js::PropertyRecord const ** propertyRecord)
  770. {
  771. LPCWSTR psz = pstName->GetSz();
  772. FindPropertyRecord(psz, pstName->GetLength(), propertyRecord);
  773. }
  774. void
  775. ThreadContext::FindPropertyRecord(__in LPCWSTR propertyName, __in int propertyNameLength, Js::PropertyRecord const ** propertyRecord)
  776. {
  777. EnterPinnedScope((volatile void **)propertyRecord);
  778. *propertyRecord = FindPropertyRecord(propertyName, propertyNameLength);
  779. LeavePinnedScope();
  780. }
  781. Js::PropertyRecord const *
  782. ThreadContext::GetPropertyRecord(Js::PropertyId propertyId)
  783. {
  784. return GetPropertyNameLocked(propertyId);
  785. }
  786. bool
  787. ThreadContext::IsNumericProperty(Js::PropertyId propertyId)
  788. {
  789. return GetPropertyRecord(propertyId)->IsNumeric();
  790. }
  791. const Js::PropertyRecord *
  792. ThreadContext::FindPropertyRecord(const char16 * propertyName, int propertyNameLength)
  793. {
  794. Js::PropertyRecord const * propertyRecord = nullptr;
  795. if (IsDirectPropertyName(propertyName, propertyNameLength))
  796. {
  797. propertyRecord = propertyNamesDirect[propertyName[0]];
  798. Assert(propertyRecord == propertyMap->LookupWithKey(Js::HashedCharacterBuffer<char16>(propertyName, propertyNameLength)));
  799. }
  800. else
  801. {
  802. propertyRecord = propertyMap->LookupWithKey(Js::HashedCharacterBuffer<char16>(propertyName, propertyNameLength));
  803. }
  804. return propertyRecord;
  805. }
  806. Js::PropertyRecord const *
  807. ThreadContext::UncheckedAddPropertyId(__in LPCWSTR propertyName, __in int propertyNameLength, bool bind, bool isSymbol)
  808. {
  809. return UncheckedAddPropertyId(JsUtil::CharacterBuffer<WCHAR>(propertyName, propertyNameLength), bind, isSymbol);
  810. }
  811. void ThreadContext::InitializePropertyMaps()
  812. {
  813. Assert(this->recycler != nullptr);
  814. Assert(this->recyclableData != nullptr);
  815. Assert(this->propertyMap == nullptr);
  816. Assert(this->caseInvariantPropertySet == nullptr);
  817. try
  818. {
  819. this->propertyMap = HeapNew(PropertyMap, &HeapAllocator::Instance, TotalNumberOfBuiltInProperties + 700);
  820. this->recyclableData->boundPropertyStrings = RecyclerNew(this->recycler, JsUtil::List<Js::PropertyRecord const*>, this->recycler);
  821. memset(propertyNamesDirect, 0, 128*sizeof(Js::PropertyRecord *));
  822. Js::JavascriptLibrary::InitializeProperties(this);
  823. InitializeAdditionalProperties(this);
  824. //Js::JavascriptLibrary::InitializeDOMProperties(this);
  825. }
  826. catch(...)
  827. {
  828. // Initialization failed, undo what was done above. Callees that throw must clean up after themselves. The recycler will
  829. // be trashed, so clear members that point to recyclable memory. Stuff in 'recyclableData' will be taken care of by the
  830. // recycler, and the 'recyclableData' instance will be trashed as well.
  831. if (this->propertyMap != nullptr)
  832. {
  833. HeapDelete(this->propertyMap);
  834. }
  835. this->propertyMap = nullptr;
  836. this->caseInvariantPropertySet = nullptr;
  837. memset(propertyNamesDirect, 0, 128*sizeof(Js::PropertyRecord *));
  838. throw;
  839. }
  840. }
  841. void ThreadContext::UncheckedAddBuiltInPropertyId()
  842. {
  843. for (int i = 0; i < _countof(builtInPropertyRecords); i++)
  844. {
  845. AddPropertyRecordInternal(builtInPropertyRecords[i]);
  846. }
  847. }
  848. bool
  849. ThreadContext::IsDirectPropertyName(const char16 * propertyName, int propertyNameLength)
  850. {
  851. return ((propertyNameLength == 1) && ((propertyName[0] & 0xFF80) == 0));
  852. }
  853. RecyclerWeakReference<const Js::PropertyRecord> *
  854. ThreadContext::CreatePropertyRecordWeakRef(const Js::PropertyRecord * propertyRecord)
  855. {
  856. RecyclerWeakReference<const Js::PropertyRecord> * propertyRecordWeakRef;
  857. if (propertyRecord->IsBound())
  858. {
  859. // Create a fake weak ref
  860. propertyRecordWeakRef = RecyclerNewLeaf(this->recycler, StaticPropertyRecordReference, propertyRecord);
  861. }
  862. else
  863. {
  864. propertyRecordWeakRef = recycler->CreateWeakReferenceHandle(propertyRecord);
  865. }
  866. return propertyRecordWeakRef;
  867. }
  868. Js::PropertyRecord const *
  869. ThreadContext::UncheckedAddPropertyId(JsUtil::CharacterBuffer<WCHAR> const& propertyName, bool bind, bool isSymbol)
  870. {
  871. #if ENABLE_TTD
  872. if(isSymbol & this->IsRuntimeInTTDMode())
  873. {
  874. if(this->TTDContext->GetActiveScriptContext() != nullptr && this->TTDContext->GetActiveScriptContext()->ShouldPerformReplayAction())
  875. {
  876. //We reload all properties that occour in the trace so they only way we get here in TTD mode is:
  877. //(1) if the program is creating a new symbol (which always gets a fresh id) and we should recreate it or
  878. //(2) if it is forcing arguments in debug parse mode (instead of regular which we recorded in)
  879. Js::PropertyId propertyId = Js::Constants::NoProperty;
  880. this->TTDLog->ReplaySymbolCreationEvent(&propertyId);
  881. //Don't recreate the symbol below, instead return the known symbol by looking up on the pid
  882. const Js::PropertyRecord* res = this->GetPropertyName(propertyId);
  883. AssertMsg(res != nullptr, "This should never happen!!!");
  884. return res;
  885. }
  886. }
  887. #endif
  888. this->propertyMap->EnsureCapacity();
  889. // Automatically bind direct (single-character) property names, so that they can be
  890. // stored in the direct property table
  891. if (IsDirectPropertyName(propertyName.GetBuffer(), propertyName.GetLength()))
  892. {
  893. bind = true;
  894. }
  895. // Create the PropertyRecord
  896. int length = propertyName.GetLength();
  897. uint bytelength = sizeof(char16) * length;
  898. uint32 indexVal = 0;
  899. // Symbol properties cannot be numeric since their description is not to be used!
  900. bool isNumeric = !isSymbol && Js::PropertyRecord::IsPropertyNameNumeric(propertyName.GetBuffer(), propertyName.GetLength(), &indexVal);
  901. uint hash = JsUtil::CharacterBuffer<WCHAR>::StaticGetHashCode(propertyName.GetBuffer(), propertyName.GetLength());
  902. size_t allocLength = bytelength + sizeof(char16) + (isNumeric ? sizeof(uint32) : 0);
  903. // If it's bound, create it in the thread arena, along with a fake weak ref
  904. Js::PropertyRecord * propertyRecord;
  905. if (bind)
  906. {
  907. propertyRecord = AnewPlus(GetThreadAlloc(), allocLength, Js::PropertyRecord, bytelength, isNumeric, hash, isSymbol);
  908. propertyRecord->isBound = true;
  909. }
  910. else
  911. {
  912. propertyRecord = RecyclerNewFinalizedLeafPlus(recycler, allocLength, Js::PropertyRecord, bytelength, isNumeric, hash, isSymbol);
  913. }
  914. // Copy string and numeric info
  915. char16* buffer = (char16 *)(propertyRecord + 1);
  916. js_memcpy_s(buffer, bytelength, propertyName.GetBuffer(), bytelength);
  917. buffer[length] = _u('\0');
  918. if (isNumeric)
  919. {
  920. *(uint32 *)(buffer + length + 1) = indexVal;
  921. Assert(propertyRecord->GetNumericValue() == indexVal);
  922. }
  923. Js::PropertyId propertyId = this->GetNextPropertyId();
  924. #if ENABLE_TTD
  925. if(isSymbol & this->IsRuntimeInTTDMode())
  926. {
  927. if(this->TTDContext->GetActiveScriptContext() != nullptr && this->TTDContext->GetActiveScriptContext()->ShouldPerformRecordAction())
  928. {
  929. this->TTDLog->RecordSymbolCreationEvent(propertyId);
  930. }
  931. }
  932. #endif
  933. propertyRecord->pid = propertyId;
  934. AddPropertyRecordInternal(propertyRecord);
  935. return propertyRecord;
  936. }
  937. void
  938. ThreadContext::AddPropertyRecordInternal(const Js::PropertyRecord * propertyRecord)
  939. {
  940. // At this point the PropertyRecord is constructed but not added to the map.
  941. const char16 * propertyName = propertyRecord->GetBuffer();
  942. int propertyNameLength = propertyRecord->GetLength();
  943. Js::PropertyId propertyId = propertyRecord->GetPropertyId();
  944. Assert(propertyId == GetNextPropertyId());
  945. Assert(!IsActivePropertyId(propertyId));
  946. #if DBG
  947. // Only Assert we can't find the property if we are not adding a symbol.
  948. // For a symbol, the propertyName is not used and may collide with something in the map already.
  949. if (!propertyRecord->IsSymbol())
  950. {
  951. Assert(FindPropertyRecord(propertyName, propertyNameLength) == nullptr);
  952. }
  953. #endif
  954. #if ENABLE_TTD
  955. if(this->IsRuntimeInTTDMode())
  956. {
  957. this->TTDLog->AddPropertyRecord(propertyRecord);
  958. }
  959. #endif
  960. // Add to the map
  961. propertyMap->Add(propertyRecord);
  962. #if ENABLE_NATIVE_CODEGEN
  963. if (m_jitNumericProperties)
  964. {
  965. if (propertyRecord->IsNumeric())
  966. {
  967. m_jitNumericProperties->Set(propertyRecord->GetPropertyId());
  968. m_jitNeedsPropertyUpdate = true;
  969. }
  970. }
  971. #endif
  972. PropertyRecordTrace(_u("Added property '%s' at 0x%08x, pid = %d\n"), propertyName, propertyRecord, propertyId);
  973. // Do not store the pid for symbols in the direct property name table.
  974. // We don't want property ids for symbols to be searchable anyway.
  975. if (!propertyRecord->IsSymbol() && IsDirectPropertyName(propertyName, propertyNameLength))
  976. {
  977. // Store the pids for single character properties in the propertyNamesDirect array.
  978. // This property record should have been created as bound by the caller.
  979. Assert(propertyRecord->IsBound());
  980. Assert(propertyNamesDirect[propertyName[0]] == nullptr);
  981. propertyNamesDirect[propertyName[0]] = propertyRecord;
  982. }
  983. if (caseInvariantPropertySet)
  984. {
  985. AddCaseInvariantPropertyRecord(propertyRecord);
  986. }
  987. // Check that everything was added correctly
  988. #if DBG
  989. // Only Assert we can find the property if we are not adding a symbol.
  990. // For a symbol, the propertyName is not used and we won't be able to look the pid up via name.
  991. if (!propertyRecord->IsSymbol())
  992. {
  993. Assert(FindPropertyRecord(propertyName, propertyNameLength) == propertyRecord);
  994. }
  995. // We will still be able to lookup the symbol property by the property id, so go ahead and check that.
  996. Assert(GetPropertyName(propertyRecord->GetPropertyId()) == propertyRecord);
  997. #endif
  998. JS_ETW_INTERNAL(EventWriteJSCRIPT_HOSTING_PROPERTYID_LIST(propertyRecord, propertyRecord->GetBuffer()));
  999. }
  1000. void
  1001. ThreadContext::AddCaseInvariantPropertyRecord(const Js::PropertyRecord * propertyRecord)
  1002. {
  1003. Assert(this->caseInvariantPropertySet != nullptr);
  1004. // Create a weak reference to the property record here (since we no longer use weak refs in the property map)
  1005. RecyclerWeakReference<const Js::PropertyRecord> * propertyRecordWeakRef = CreatePropertyRecordWeakRef(propertyRecord);
  1006. JsUtil::CharacterBuffer<WCHAR> newPropertyName(propertyRecord->GetBuffer(), propertyRecord->GetLength());
  1007. Js::CaseInvariantPropertyListWithHashCode* list;
  1008. if (!FindExistingPropertyRecord(newPropertyName, &list))
  1009. {
  1010. // This binds all the property string that is key in this map with no hope of reclaiming them
  1011. // TODO: do better
  1012. list = RecyclerNew(recycler, Js::CaseInvariantPropertyListWithHashCode, recycler, 1);
  1013. // Do the add first so that the list is non-empty and we can calculate its hashcode correctly
  1014. list->Add(propertyRecordWeakRef);
  1015. // This will calculate the hashcode
  1016. caseInvariantPropertySet->Add(list);
  1017. }
  1018. else
  1019. {
  1020. list->Add(propertyRecordWeakRef);
  1021. }
  1022. }
  1023. void
  1024. ThreadContext::BindPropertyRecord(const Js::PropertyRecord * propertyRecord)
  1025. {
  1026. if (!propertyRecord->IsBound())
  1027. {
  1028. Assert(!this->recyclableData->boundPropertyStrings->Contains(propertyRecord));
  1029. this->recyclableData->boundPropertyStrings->Add(propertyRecord);
  1030. // Cast around constness to set propertyRecord as bound
  1031. const_cast<Js::PropertyRecord *>(propertyRecord)->isBound = true;
  1032. }
  1033. }
  1034. void ThreadContext::GetOrAddPropertyId(__in LPCWSTR propertyName, __in int propertyNameLength, Js::PropertyRecord const ** propertyRecord)
  1035. {
  1036. GetOrAddPropertyId(JsUtil::CharacterBuffer<WCHAR>(propertyName, propertyNameLength), propertyRecord);
  1037. }
  1038. void ThreadContext::GetOrAddPropertyId(JsUtil::CharacterBuffer<WCHAR> const& propertyName, Js::PropertyRecord const ** propRecord)
  1039. {
  1040. EnterPinnedScope((volatile void **)propRecord);
  1041. *propRecord = GetOrAddPropertyRecord(propertyName);
  1042. LeavePinnedScope();
  1043. }
  1044. const Js::PropertyRecord *
  1045. ThreadContext::GetOrAddPropertyRecordImpl(JsUtil::CharacterBuffer<char16> propertyName, bool bind)
  1046. {
  1047. // Make sure the recycler is around so that we can take weak references to the property strings
  1048. EnsureRecycler();
  1049. const Js::PropertyRecord * propertyRecord;
  1050. FindPropertyRecord(propertyName.GetBuffer(), propertyName.GetLength(), &propertyRecord);
  1051. if (propertyRecord == nullptr)
  1052. {
  1053. propertyRecord = UncheckedAddPropertyId(propertyName, bind);
  1054. }
  1055. else
  1056. {
  1057. // PropertyRecord exists, but may not be bound. Bind now if requested.
  1058. if (bind)
  1059. {
  1060. BindPropertyRecord(propertyRecord);
  1061. }
  1062. }
  1063. Assert(propertyRecord != nullptr);
  1064. Assert(!bind || propertyRecord->IsBound());
  1065. return propertyRecord;
  1066. }
  1067. void ThreadContext::AddBuiltInPropertyRecord(const Js::PropertyRecord *propertyRecord)
  1068. {
  1069. this->AddPropertyRecordInternal(propertyRecord);
  1070. }
  1071. BOOL ThreadContext::IsNumericPropertyId(Js::PropertyId propertyId, uint32* value)
  1072. {
  1073. Js::PropertyRecord const * propertyRecord = this->GetPropertyName(propertyId);
  1074. Assert(propertyRecord != nullptr);
  1075. if (propertyRecord == nullptr || !propertyRecord->IsNumeric())
  1076. {
  1077. return false;
  1078. }
  1079. *value = propertyRecord->GetNumericValue();
  1080. return true;
  1081. }
  1082. bool ThreadContext::IsActivePropertyId(Js::PropertyId pid)
  1083. {
  1084. Assert(pid != Js::Constants::NoProperty);
  1085. if (Js::IsInternalPropertyId(pid))
  1086. {
  1087. return true;
  1088. }
  1089. int propertyIndex = pid - Js::PropertyIds::_none;
  1090. const Js::PropertyRecord * propertyRecord;
  1091. if (propertyMap->TryGetValueAt(propertyIndex, &propertyRecord) && propertyRecord != nullptr)
  1092. {
  1093. return true;
  1094. }
  1095. return false;
  1096. }
  1097. void ThreadContext::InvalidatePropertyRecord(const Js::PropertyRecord * propertyRecord)
  1098. {
  1099. InternalInvalidateProtoTypePropertyCaches(propertyRecord->GetPropertyId()); // use the internal version so we don't check for active property id
  1100. #if ENABLE_NATIVE_CODEGEN
  1101. if (propertyRecord->IsNumeric() && m_jitNumericProperties)
  1102. {
  1103. m_jitNumericProperties->Clear(propertyRecord->GetPropertyId());
  1104. m_jitNeedsPropertyUpdate = true;
  1105. }
  1106. #endif
  1107. this->propertyMap->Remove(propertyRecord);
  1108. PropertyRecordTrace(_u("Reclaimed property '%s' at 0x%08x, pid = %d\n"),
  1109. propertyRecord->GetBuffer(), propertyRecord, propertyRecord->GetPropertyId());
  1110. }
  1111. Js::PropertyId ThreadContext::GetNextPropertyId()
  1112. {
  1113. return this->propertyMap->GetNextIndex() + Js::PropertyIds::_none;
  1114. }
  1115. Js::PropertyId ThreadContext::GetMaxPropertyId()
  1116. {
  1117. auto maxPropertyId = this->propertyMap->Count() + Js::InternalPropertyIds::Count;
  1118. return maxPropertyId;
  1119. }
  1120. void ThreadContext::CreateNoCasePropertyMap()
  1121. {
  1122. Assert(caseInvariantPropertySet == nullptr);
  1123. caseInvariantPropertySet = RecyclerNew(recycler, PropertyNoCaseSetType, recycler, 173);
  1124. // Prevent the set from being reclaimed
  1125. // Individual items in the set can be reclaimed though since they're lists of weak references
  1126. // The lists themselves can be reclaimed when all the weak references in them are cleared
  1127. this->recyclableData->caseInvariantPropertySet = caseInvariantPropertySet;
  1128. // Note that we are allocating from the recycler below, so we may cause a GC at any time, which
  1129. // could cause PropertyRecords to be collected and removed from the propertyMap.
  1130. // Thus, don't use BaseDictionary::Map here, as it cannot tolerate changes while mapping.
  1131. // Instead, walk the PropertyRecord entries in index order. This will work even if a GC occurs.
  1132. for (int propertyIndex = 0; propertyIndex <= this->propertyMap->GetLastIndex(); propertyIndex++)
  1133. {
  1134. const Js::PropertyRecord * propertyRecord;
  1135. if (this->propertyMap->TryGetValueAt(propertyIndex, &propertyRecord) && propertyRecord != nullptr)
  1136. {
  1137. AddCaseInvariantPropertyRecord(propertyRecord);
  1138. }
  1139. }
  1140. }
  1141. JsUtil::List<const RecyclerWeakReference<Js::PropertyRecord const>*>*
  1142. ThreadContext::FindPropertyIdNoCase(Js::ScriptContext * scriptContext, LPCWSTR propertyName, int propertyNameLength)
  1143. {
  1144. return ThreadContext::FindPropertyIdNoCase(scriptContext, JsUtil::CharacterBuffer<WCHAR>(propertyName, propertyNameLength));
  1145. }
  1146. JsUtil::List<const RecyclerWeakReference<Js::PropertyRecord const>*>*
  1147. ThreadContext::FindPropertyIdNoCase(Js::ScriptContext * scriptContext, JsUtil::CharacterBuffer<WCHAR> const& propertyName)
  1148. {
  1149. if (caseInvariantPropertySet == nullptr)
  1150. {
  1151. this->CreateNoCasePropertyMap();
  1152. }
  1153. Js::CaseInvariantPropertyListWithHashCode* list;
  1154. if (FindExistingPropertyRecord(propertyName, &list))
  1155. {
  1156. return list;
  1157. }
  1158. return nullptr;
  1159. }
  1160. bool
  1161. ThreadContext::FindExistingPropertyRecord(_In_ JsUtil::CharacterBuffer<WCHAR> const& propertyName, Js::CaseInvariantPropertyListWithHashCode** list)
  1162. {
  1163. Js::CaseInvariantPropertyListWithHashCode* l = this->caseInvariantPropertySet->LookupWithKey(propertyName);
  1164. (*list) = l;
  1165. return (l != nullptr);
  1166. }
  1167. void ThreadContext::CleanNoCasePropertyMap()
  1168. {
  1169. if (this->caseInvariantPropertySet != nullptr)
  1170. {
  1171. this->caseInvariantPropertySet->MapAndRemoveIf([](Js::CaseInvariantPropertyListWithHashCode* value) -> bool {
  1172. if (value && value->Count() == 0)
  1173. {
  1174. // Remove entry
  1175. return true;
  1176. }
  1177. // Keep entry
  1178. return false;
  1179. });
  1180. }
  1181. }
  1182. void
  1183. ThreadContext::ForceCleanPropertyMap()
  1184. {
  1185. // No-op now that we no longer use weak refs
  1186. }
  1187. #if ENABLE_NATIVE_CODEGEN
  1188. JsUtil::JobProcessor *
  1189. ThreadContext::GetJobProcessor()
  1190. {
  1191. if(bgJit && isOptimizedForManyInstances)
  1192. {
  1193. return ThreadBoundThreadContextManager::GetSharedJobProcessor();
  1194. }
  1195. if (!jobProcessor)
  1196. {
  1197. if(bgJit && !isOptimizedForManyInstances)
  1198. {
  1199. jobProcessor = HeapNew(JsUtil::BackgroundJobProcessor, GetAllocationPolicyManager(), &threadService, false /*disableParallelThreads*/);
  1200. }
  1201. else
  1202. {
  1203. jobProcessor = HeapNew(JsUtil::ForegroundJobProcessor);
  1204. }
  1205. }
  1206. return jobProcessor;
  1207. }
  1208. #endif
  1209. void
  1210. ThreadContext::RegisterCodeGenRecyclableData(Js::CodeGenRecyclableData *const codeGenRecyclableData)
  1211. {
  1212. Assert(codeGenRecyclableData);
  1213. Assert(recyclableData);
  1214. // Linking must not be done concurrently with unlinking (caller must use lock)
  1215. recyclableData->codeGenRecyclableDatas.LinkToEnd(codeGenRecyclableData);
  1216. }
  1217. void
  1218. ThreadContext::UnregisterCodeGenRecyclableData(Js::CodeGenRecyclableData *const codeGenRecyclableData)
  1219. {
  1220. Assert(codeGenRecyclableData);
  1221. if(!recyclableData)
  1222. {
  1223. // The thread context's recyclable data may have already been released to the recycler if we're shutting down
  1224. return;
  1225. }
  1226. // Unlinking may be done from a background thread, but not concurrently with linking (caller must use lock). Partial unlink
  1227. // does not zero the previous and next links for the unlinked node so that the recycler can scan through the node from the
  1228. // main thread.
  1229. recyclableData->codeGenRecyclableDatas.UnlinkPartial(codeGenRecyclableData);
  1230. }
  1231. uint
  1232. ThreadContext::EnterScriptStart(Js::ScriptEntryExitRecord * record, bool doCleanup)
  1233. {
  1234. Recycler * recycler = this->GetRecycler();
  1235. Assert(recycler->IsReentrantState());
  1236. JS_ETW_INTERNAL(EventWriteJSCRIPT_RUN_START(this,0));
  1237. // Increment the callRootLevel early so that Dispose ran during FinishConcurrent will not close the current scriptContext
  1238. uint oldCallRootLevel = this->callRootLevel++;
  1239. if (oldCallRootLevel == 0)
  1240. {
  1241. Assert(!this->hasThrownPendingException);
  1242. RECORD_TIMESTAMP(lastScriptStartTime);
  1243. InterruptPoller *poller = this->interruptPoller;
  1244. if (poller)
  1245. {
  1246. poller->StartScript();
  1247. }
  1248. recycler->SetIsInScript(true);
  1249. if (doCleanup)
  1250. {
  1251. recycler->EnterIdleDecommit();
  1252. #if ENABLE_CONCURRENT_GC
  1253. recycler->FinishConcurrent<FinishConcurrentOnEnterScript>();
  1254. #endif
  1255. if (threadServiceWrapper == NULL)
  1256. {
  1257. // Reschedule the next collection at the start of the script.
  1258. recycler->ScheduleNextCollection();
  1259. }
  1260. }
  1261. }
  1262. this->PushEntryExitRecord(record);
  1263. AssertMsg(!this->IsScriptActive(),
  1264. "Missing EnterScriptEnd or LeaveScriptStart");
  1265. this->isScriptActive = true;
  1266. recycler->SetIsScriptActive(true);
  1267. #if DBG_DUMP
  1268. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::RunPhase))
  1269. {
  1270. Output::Trace(Js::RunPhase, _u("%p> EnterScriptStart(%p): Level %d\n"), ::GetCurrentThreadId(), this, this->callRootLevel);
  1271. Output::Flush();
  1272. }
  1273. #endif
  1274. return oldCallRootLevel;
  1275. }
  1276. void
  1277. ThreadContext::EnterScriptEnd(Js::ScriptEntryExitRecord * record, bool doCleanup)
  1278. {
  1279. #if DBG_DUMP
  1280. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::RunPhase))
  1281. {
  1282. Output::Trace(Js::RunPhase, _u("%p> EnterScriptEnd (%p): Level %d\n"), ::GetCurrentThreadId(), this, this->callRootLevel);
  1283. Output::Flush();
  1284. }
  1285. #endif
  1286. this->PopEntryExitRecord(record);
  1287. AssertMsg(this->IsScriptActive(),
  1288. "Missing EnterScriptStart or LeaveScriptEnd");
  1289. this->isScriptActive = false;
  1290. this->GetRecycler()->SetIsScriptActive(false);
  1291. this->callRootLevel--;
  1292. #ifdef EXCEPTION_CHECK
  1293. ExceptionCheck::SetHandledExceptionType(record->handledExceptionType);
  1294. #endif
  1295. #ifdef RECYCLER_MEMORY_VERIFY
  1296. recycler->Verify(Js::RunPhase);
  1297. #endif
  1298. if (this->callRootLevel == 0)
  1299. {
  1300. RECORD_TIMESTAMP(lastScriptEndTime);
  1301. this->GetRecycler()->SetIsInScript(false);
  1302. InterruptPoller *poller = this->interruptPoller;
  1303. if (poller)
  1304. {
  1305. poller->EndScript();
  1306. }
  1307. ClosePendingProjectionContexts();
  1308. ClosePendingScriptContexts();
  1309. Assert(rootPendingClose == nullptr);
  1310. if (this->hasThrownPendingException)
  1311. {
  1312. // We have some cases where the thread instant of JavascriptExceptionObject
  1313. // are ignored and not clear. To avoid leaks, clear it here when
  1314. // we are not in script, where no one should be using these JavascriptExceptionObject
  1315. this->ClearPendingOOMError();
  1316. this->ClearPendingSOError();
  1317. this->hasThrownPendingException = false;
  1318. }
  1319. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  1320. if (Js::Configuration::Global.flags.FreeRejittedCode)
  1321. #endif
  1322. {
  1323. // Since we're no longer in script, old entry points can now be collected
  1324. Js::FunctionEntryPointInfo* current = this->recyclableData->oldEntryPointInfo;
  1325. this->recyclableData->oldEntryPointInfo = nullptr;
  1326. // Clear out the next pointers so older entry points wont be held on
  1327. // as a false positive
  1328. while (current != nullptr)
  1329. {
  1330. Js::FunctionEntryPointInfo* next = current->nextEntryPoint;
  1331. current->nextEntryPoint = nullptr;
  1332. current = next;
  1333. }
  1334. }
  1335. if (doCleanup)
  1336. {
  1337. ThreadServiceWrapper* threadServiceWrapper = GetThreadServiceWrapper();
  1338. if (!threadServiceWrapper || !threadServiceWrapper->ScheduleNextCollectOnExit())
  1339. {
  1340. // Do the idle GC now if we fail schedule one.
  1341. recycler->CollectNow<CollectOnScriptExit>();
  1342. }
  1343. recycler->LeaveIdleDecommit();
  1344. }
  1345. }
  1346. if (doCleanup)
  1347. {
  1348. PHASE_PRINT_TRACE1(Js::DisposePhase, _u("[Dispose] NeedDispose in EnterScriptEnd: %d\n"), this->recycler->NeedDispose());
  1349. if (this->recycler->NeedDispose())
  1350. {
  1351. this->recycler->FinishDisposeObjectsNow<FinishDispose>();
  1352. }
  1353. }
  1354. JS_ETW_INTERNAL(EventWriteJSCRIPT_RUN_STOP(this,0));
  1355. }
  1356. void
  1357. ThreadContext::SetForceOneIdleCollection()
  1358. {
  1359. ThreadServiceWrapper* threadServiceWrapper = GetThreadServiceWrapper();
  1360. if (threadServiceWrapper)
  1361. {
  1362. threadServiceWrapper->SetForceOneIdleCollection();
  1363. }
  1364. }
  1365. BOOLEAN
  1366. ThreadContext::IsOnStack(void const *ptr)
  1367. {
  1368. #if defined(_M_IX86) && defined(_MSC_VER)
  1369. return ptr < (void*)__readfsdword(0x4) && ptr >= (void*)__readfsdword(0xE0C);
  1370. #elif defined(_M_AMD64) && defined(_MSC_VER)
  1371. return ptr < (void*)__readgsqword(0x8) && ptr >= (void*)__readgsqword(0x1478);
  1372. #elif defined(_M_ARM)
  1373. ULONG lowLimit, highLimit;
  1374. ::GetCurrentThreadStackLimits(&lowLimit, &highLimit);
  1375. bool isOnStack = (void*)lowLimit <= ptr && ptr < (void*)highLimit;
  1376. return isOnStack;
  1377. #elif defined(_M_ARM64)
  1378. ULONG64 lowLimit, highLimit;
  1379. ::GetCurrentThreadStackLimits(&lowLimit, &highLimit);
  1380. bool isOnStack = (void*)lowLimit <= ptr && ptr < (void*)highLimit;
  1381. return isOnStack;
  1382. #elif !defined(_MSC_VER)
  1383. return ::IsAddressOnStack((ULONG_PTR) ptr);
  1384. #else
  1385. AssertMsg(FALSE, "IsOnStack -- not implemented yet case");
  1386. Js::Throw::NotImplemented();
  1387. return false;
  1388. #endif
  1389. }
  1390. size_t
  1391. ThreadContext::GetStackLimitForCurrentThread() const
  1392. {
  1393. FAULTINJECT_SCRIPT_TERMINATION;
  1394. size_t limit = this->stackLimitForCurrentThread;
  1395. Assert(limit == Js::Constants::StackLimitForScriptInterrupt
  1396. || !this->GetStackProber()
  1397. || limit == this->GetStackProber()->GetScriptStackLimit());
  1398. return limit;
  1399. }
  1400. void
  1401. ThreadContext::SetStackLimitForCurrentThread(size_t limit)
  1402. {
  1403. this->stackLimitForCurrentThread = limit;
  1404. }
  1405. _NOINLINE //Win8 947081: might use wrong _AddressOfReturnAddress() if this and caller are inlined
  1406. bool
  1407. ThreadContext::IsStackAvailable(size_t size)
  1408. {
  1409. size_t sp = (size_t)_AddressOfReturnAddress();
  1410. size_t stackLimit = this->GetStackLimitForCurrentThread();
  1411. bool stackAvailable = (sp > size && (sp - size) > stackLimit);
  1412. // Verify that JIT'd frames didn't mess up the ABI stack alignment
  1413. Assert(((uintptr_t)sp & (AutoSystemInfo::StackAlign - 1)) == (sizeof(void*) & (AutoSystemInfo::StackAlign - 1)));
  1414. #if DBG
  1415. this->GetStackProber()->AdjustKnownStackLimit(sp, size);
  1416. #endif
  1417. FAULTINJECT_STACK_PROBE
  1418. if (stackAvailable)
  1419. {
  1420. return true;
  1421. }
  1422. if (sp <= stackLimit)
  1423. {
  1424. if (stackLimit == Js::Constants::StackLimitForScriptInterrupt)
  1425. {
  1426. if (sp <= this->GetStackProber()->GetScriptStackLimit())
  1427. {
  1428. // Take down the process if we cant recover from the stack overflow
  1429. Js::Throw::FatalInternalError();
  1430. }
  1431. }
  1432. }
  1433. return false;
  1434. }
  1435. _NOINLINE //Win8 947081: might use wrong _AddressOfReturnAddress() if this and caller are inlined
  1436. bool
  1437. ThreadContext::IsStackAvailableNoThrow(size_t size)
  1438. {
  1439. size_t sp = (size_t)_AddressOfReturnAddress();
  1440. size_t stackLimit = this->GetStackLimitForCurrentThread();
  1441. bool stackAvailable = (sp > stackLimit) && (sp > size) && ((sp - size) > stackLimit);
  1442. FAULTINJECT_STACK_PROBE
  1443. return stackAvailable;
  1444. }
  1445. /* static */ bool
  1446. ThreadContext::IsCurrentStackAvailable(size_t size)
  1447. {
  1448. ThreadContext *currentContext = GetContextForCurrentThread();
  1449. Assert(currentContext);
  1450. return currentContext->IsStackAvailable(size);
  1451. }
  1452. /*
  1453. returnAddress will be passed in the stackprobe call at the beginning of interpreter frame.
  1454. We need to probe the stack before we link up the InterpreterFrame structure in threadcontext,
  1455. and if we throw there, the stack walker might get confused when trying to identify a frame
  1456. is interpreter frame by comparing the current ebp in ebp chain with return address specified
  1457. in the last InterpreterFrame linked in threadcontext. We need to pass in the return address
  1458. of the probing frame to skip the right one (we need to skip first match in a->a->a recursion,
  1459. but not in a->b->a recursion).
  1460. */
  1461. void
  1462. ThreadContext::ProbeStackNoDispose(size_t size, Js::ScriptContext *scriptContext, PVOID returnAddress)
  1463. {
  1464. AssertCanHandleStackOverflow();
  1465. if (!this->IsStackAvailable(size))
  1466. {
  1467. if (this->IsExecutionDisabled())
  1468. {
  1469. // The probe failed because we hammered the stack limit to trigger script interrupt.
  1470. Assert(this->DoInterruptProbe());
  1471. throw Js::ScriptAbortException();
  1472. }
  1473. Js::Throw::StackOverflow(scriptContext, returnAddress);
  1474. }
  1475. #if defined(NTBUILD) || defined(__IOS__) || defined(__ANDROID__)
  1476. // Use every Nth stack probe as a QC trigger.
  1477. if (AutoSystemInfo::ShouldQCMoreFrequently() && this->HasInterruptPoller() && this->IsScriptActive())
  1478. {
  1479. ++(this->stackProbeCount);
  1480. if (this->stackProbeCount > ThreadContext::StackProbePollThreshold)
  1481. {
  1482. this->stackProbeCount = 0;
  1483. this->CheckInterruptPoll();
  1484. }
  1485. }
  1486. #endif
  1487. }
  1488. void
  1489. ThreadContext::ProbeStack(size_t size, Js::ScriptContext *scriptContext, PVOID returnAddress)
  1490. {
  1491. this->ProbeStackNoDispose(size, scriptContext, returnAddress);
  1492. // BACKGROUND-GC TODO: If we're stuck purely in JITted code, we should have the
  1493. // background GC thread modify the threads stack limit to trigger the runtime stack probe
  1494. if (this->callDispose && this->recycler->NeedDispose())
  1495. {
  1496. PHASE_PRINT_TRACE1(Js::DisposePhase, _u("[Dispose] NeedDispose in ProbeStack: %d\n"), this->recycler->NeedDispose());
  1497. this->recycler->FinishDisposeObjectsNow<FinishDisposeTimed>();
  1498. }
  1499. }
  1500. void
  1501. ThreadContext::ProbeStack(size_t size, Js::RecyclableObject * obj, Js::ScriptContext *scriptContext)
  1502. {
  1503. AssertCanHandleStackOverflowCall(obj->IsExternal() ||
  1504. (Js::JavascriptOperators::GetTypeId(obj) == Js::TypeIds_Function &&
  1505. Js::JavascriptFunction::FromVar(obj)->IsExternalFunction()));
  1506. if (!this->IsStackAvailable(size))
  1507. {
  1508. if (this->IsExecutionDisabled())
  1509. {
  1510. // The probe failed because we hammered the stack limit to trigger script interrupt.
  1511. Assert(this->DoInterruptProbe());
  1512. throw Js::ScriptAbortException();
  1513. }
  1514. if (obj->IsExternal() ||
  1515. (Js::JavascriptOperators::GetTypeId(obj) == Js::TypeIds_Function &&
  1516. Js::JavascriptFunction::FromVar(obj)->IsExternalFunction()))
  1517. {
  1518. Js::JavascriptError::ThrowStackOverflowError(scriptContext);
  1519. }
  1520. Js::Throw::StackOverflow(scriptContext, NULL);
  1521. }
  1522. }
  1523. void
  1524. ThreadContext::ProbeStack(size_t size)
  1525. {
  1526. Assert(this->IsScriptActive());
  1527. Js::ScriptEntryExitRecord *entryExitRecord = this->GetScriptEntryExit();
  1528. Assert(entryExitRecord);
  1529. Js::ScriptContext *scriptContext = entryExitRecord->scriptContext;
  1530. Assert(scriptContext);
  1531. this->ProbeStack(size, scriptContext);
  1532. }
  1533. /* static */ void
  1534. ThreadContext::ProbeCurrentStack(size_t size, Js::ScriptContext *scriptContext)
  1535. {
  1536. Assert(scriptContext != nullptr);
  1537. Assert(scriptContext->GetThreadContext() == GetContextForCurrentThread());
  1538. scriptContext->GetThreadContext()->ProbeStack(size, scriptContext);
  1539. }
  1540. /* static */ void
  1541. ThreadContext::ProbeCurrentStackNoDispose(size_t size, Js::ScriptContext *scriptContext)
  1542. {
  1543. Assert(scriptContext != nullptr);
  1544. Assert(scriptContext->GetThreadContext() == GetContextForCurrentThread());
  1545. scriptContext->GetThreadContext()->ProbeStackNoDispose(size, scriptContext);
  1546. }
  1547. template <bool leaveForHost>
  1548. void
  1549. ThreadContext::LeaveScriptStart(void * frameAddress)
  1550. {
  1551. Assert(this->IsScriptActive());
  1552. #if DBG_DUMP
  1553. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::RunPhase))
  1554. {
  1555. Output::Trace(Js::RunPhase, _u("%p> LeaveScriptStart(%p): Level %d\n"), ::GetCurrentThreadId(), this, this->callRootLevel);
  1556. Output::Flush();
  1557. }
  1558. #endif
  1559. Js::ScriptEntryExitRecord * entryExitRecord = this->GetScriptEntryExit();
  1560. AssertMsg(entryExitRecord && entryExitRecord->frameIdOfScriptExitFunction == nullptr,
  1561. "Missing LeaveScriptEnd or EnterScriptStart");
  1562. entryExitRecord->frameIdOfScriptExitFunction = frameAddress;
  1563. this->isScriptActive = false;
  1564. this->GetRecycler()->SetIsScriptActive(false);
  1565. AssertMsg(!(leaveForHost && this->IsDisableImplicitCall()),
  1566. "Disable implicit call should have been caught before leaving script for host");
  1567. // Save the implicit call flags
  1568. entryExitRecord->savedImplicitCallFlags = this->GetImplicitCallFlags();
  1569. // clear the hasReentered to detect if we have reentered into script
  1570. entryExitRecord->hasReentered = false;
  1571. #if DBG || defined(PROFILE_EXEC)
  1572. entryExitRecord->leaveForHost = leaveForHost;
  1573. #endif
  1574. #if DBG
  1575. entryExitRecord->leaveForAsyncHostOperation = false;
  1576. #endif
  1577. #ifdef PROFILE_EXEC
  1578. if (leaveForHost)
  1579. {
  1580. entryExitRecord->scriptContext->ProfileEnd(Js::RunPhase);
  1581. }
  1582. #endif
  1583. }
  1584. void ThreadContext::DisposeOnLeaveScript()
  1585. {
  1586. PHASE_PRINT_TRACE1(Js::DisposePhase, _u("[Dispose] NeedDispose in LeaveScriptStart: %d\n"), this->recycler->NeedDispose());
  1587. if (this->callDispose && this->recycler->NeedDispose())
  1588. {
  1589. this->recycler->FinishDisposeObjectsNow<FinishDispose>();
  1590. }
  1591. }
  1592. template <bool leaveForHost>
  1593. void
  1594. ThreadContext::LeaveScriptEnd(void * frameAddress)
  1595. {
  1596. Assert(!this->IsScriptActive());
  1597. #if DBG_DUMP
  1598. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::RunPhase))
  1599. {
  1600. Output::Trace(Js::RunPhase, _u("%p> LeaveScriptEnd(%p): Level %d\n"), ::GetCurrentThreadId(), this, this->callRootLevel);
  1601. Output::Flush();
  1602. }
  1603. #endif
  1604. Js::ScriptEntryExitRecord * entryExitRecord = this->GetScriptEntryExit();
  1605. AssertMsg(entryExitRecord && entryExitRecord->frameIdOfScriptExitFunction,
  1606. "LeaveScriptEnd without LeaveScriptStart");
  1607. AssertMsg(frameAddress == nullptr || frameAddress == entryExitRecord->frameIdOfScriptExitFunction,
  1608. "Mismatched script exit frames");
  1609. Assert(!!entryExitRecord->leaveForHost == leaveForHost);
  1610. entryExitRecord->frameIdOfScriptExitFunction = nullptr;
  1611. AssertMsg(!this->IsScriptActive(), "Missing LeaveScriptStart or LeaveScriptStart");
  1612. this->isScriptActive = true;
  1613. this->GetRecycler()->SetIsScriptActive(true);
  1614. Js::ImplicitCallFlags savedImplicitCallFlags = entryExitRecord->savedImplicitCallFlags;
  1615. if (leaveForHost)
  1616. {
  1617. savedImplicitCallFlags = (Js::ImplicitCallFlags)(savedImplicitCallFlags | Js::ImplicitCall_External);
  1618. }
  1619. else if (entryExitRecord->hasReentered)
  1620. {
  1621. savedImplicitCallFlags = (Js::ImplicitCallFlags)(savedImplicitCallFlags | Js::ImplicitCall_AsyncHostOperation);
  1622. }
  1623. // Restore the implicit call flags
  1624. this->SetImplicitCallFlags(savedImplicitCallFlags);
  1625. #ifdef PROFILE_EXEC
  1626. if (leaveForHost)
  1627. {
  1628. entryExitRecord->scriptContext->ProfileBegin(Js::RunPhase);
  1629. }
  1630. #endif
  1631. }
  1632. // explicit instantiations
  1633. template void ThreadContext::LeaveScriptStart<true>(void * frameAddress);
  1634. template void ThreadContext::LeaveScriptStart<false>(void * frameAddress);
  1635. template void ThreadContext::LeaveScriptEnd<true>(void * frameAddress);
  1636. template void ThreadContext::LeaveScriptEnd<false>(void * frameAddress);
  1637. void
  1638. ThreadContext::PushInterpreterFrame(Js::InterpreterStackFrame *interpreterFrame)
  1639. {
  1640. interpreterFrame->SetPreviousFrame(this->leafInterpreterFrame);
  1641. this->leafInterpreterFrame = interpreterFrame;
  1642. }
  1643. Js::InterpreterStackFrame *
  1644. ThreadContext::PopInterpreterFrame()
  1645. {
  1646. Js::InterpreterStackFrame *interpreterFrame = this->leafInterpreterFrame;
  1647. Assert(interpreterFrame);
  1648. this->leafInterpreterFrame = interpreterFrame->GetPreviousFrame();
  1649. return interpreterFrame;
  1650. }
  1651. BOOL
  1652. ThreadContext::ExecuteRecyclerCollectionFunctionCommon(Recycler * recycler, CollectionFunction function, CollectionFlags flags)
  1653. {
  1654. return __super::ExecuteRecyclerCollectionFunction(recycler, function, flags);
  1655. }
  1656. #if DBG
  1657. bool
  1658. ThreadContext::IsInAsyncHostOperation() const
  1659. {
  1660. if (!this->IsScriptActive())
  1661. {
  1662. Js::ScriptEntryExitRecord * lastRecord = this->entryExitRecord;
  1663. if (lastRecord != NULL)
  1664. {
  1665. return !!lastRecord->leaveForAsyncHostOperation;
  1666. }
  1667. }
  1668. return false;
  1669. }
  1670. #endif
  1671. #if ENABLE_NATIVE_CODEGEN
  1672. void
  1673. ThreadContext::SetJITConnectionInfo(HANDLE processHandle, void* serverSecurityDescriptor, UUID connectionId)
  1674. {
  1675. Assert(JITManager::GetJITManager()->IsOOPJITEnabled());
  1676. if (!JITManager::GetJITManager()->IsConnected())
  1677. {
  1678. // TODO: return HRESULT
  1679. JITManager::GetJITManager()->ConnectRpcServer(processHandle, serverSecurityDescriptor, connectionId);
  1680. }
  1681. }
  1682. bool
  1683. ThreadContext::EnsureJITThreadContext(bool allowPrereserveAlloc)
  1684. {
  1685. #if ENABLE_OOP_NATIVE_CODEGEN
  1686. Assert(JITManager::GetJITManager()->IsOOPJITEnabled());
  1687. if (!JITManager::GetJITManager()->IsConnected())
  1688. {
  1689. return false;
  1690. }
  1691. if (m_remoteThreadContextInfo)
  1692. {
  1693. return true;
  1694. }
  1695. ThreadContextDataIDL contextData;
  1696. HANDLE serverHandle = JITManager::GetJITManager()->GetServerHandle();
  1697. HANDLE jitTargetHandle = nullptr;
  1698. if (!DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(), serverHandle, &jitTargetHandle, 0, FALSE, DUPLICATE_SAME_ACCESS))
  1699. {
  1700. return false;
  1701. }
  1702. contextData.processHandle = (intptr_t)jitTargetHandle;
  1703. contextData.chakraBaseAddress = (intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr();
  1704. ucrtC99MathApis.Ensure();
  1705. contextData.crtBaseAddress = (intptr_t)ucrtC99MathApis.GetHandle();
  1706. contextData.threadStackLimitAddr = reinterpret_cast<intptr_t>(GetAddressOfStackLimitForCurrentThread());
  1707. contextData.bailOutRegisterSaveSpaceAddr = (intptr_t)bailOutRegisterSaveSpace;
  1708. contextData.disableImplicitFlagsAddr = (intptr_t)GetAddressOfDisableImplicitFlags();
  1709. contextData.implicitCallFlagsAddr = (intptr_t)GetAddressOfImplicitCallFlags();
  1710. contextData.scriptStackLimit = GetScriptStackLimit();
  1711. contextData.isThreadBound = IsThreadBound();
  1712. contextData.allowPrereserveAlloc = allowPrereserveAlloc;
  1713. #if defined(ENABLE_SIMDJS) && (_M_IX86 || _M_AMD64)
  1714. contextData.simdTempAreaBaseAddr = (intptr_t)GetSimdTempArea();
  1715. #endif
  1716. m_jitNumericProperties = HeapNew(BVSparse<HeapAllocator>, &HeapAllocator::Instance);
  1717. for (auto iter = propertyMap->GetIterator(); iter.IsValid(); iter.MoveNext())
  1718. {
  1719. if (iter.CurrentKey()->IsNumeric())
  1720. {
  1721. m_jitNumericProperties->Set(iter.CurrentKey()->GetPropertyId());
  1722. m_jitNeedsPropertyUpdate = true;
  1723. }
  1724. }
  1725. HRESULT hr = JITManager::GetJITManager()->InitializeThreadContext(&contextData, &m_remoteThreadContextInfo, &m_prereservedRegionAddr);
  1726. JITManager::HandleServerCallResult(hr, RemoteCallType::StateUpdate);
  1727. return m_remoteThreadContextInfo != nullptr;
  1728. #endif
  1729. }
  1730. #endif
  1731. #if ENABLE_TTD
  1732. void ThreadContext::InitTimeTravel(ThreadContext* threadContext, void* runtimeHandle, uint32 snapInterval, uint32 snapHistoryLength)
  1733. {
  1734. TTDAssert(!this->IsRuntimeInTTDMode(), "We should only init once.");
  1735. this->TTDContext = HeapNew(TTD::ThreadContextTTD, this, runtimeHandle, snapInterval, snapHistoryLength);
  1736. this->TTDLog = HeapNew(TTD::EventLog, this);
  1737. }
  1738. void ThreadContext::InitHostFunctionsAndTTData(bool record, bool replay, bool debug, size_t optTTUriLength, const char* optTTUri,
  1739. TTD::TTDOpenResourceStreamCallback openResourceStreamfp, TTD::TTDReadBytesFromStreamCallback readBytesFromStreamfp,
  1740. TTD::TTDWriteBytesToStreamCallback writeBytesToStreamfp, TTD::TTDFlushAndCloseStreamCallback flushAndCloseStreamfp,
  1741. TTD::TTDCreateExternalObjectCallback createExternalObjectfp,
  1742. TTD::TTDCreateJsRTContextCallback createJsRTContextCallbackfp, TTD::TTDReleaseJsRTContextCallback releaseJsRTContextCallbackfp, TTD::TTDSetActiveJsRTContext setActiveJsRTContextfp)
  1743. {
  1744. AssertMsg(this->IsRuntimeInTTDMode(), "Need to call init first.");
  1745. this->TTDContext->TTDataIOInfo = { openResourceStreamfp, readBytesFromStreamfp, writeBytesToStreamfp, flushAndCloseStreamfp, 0, nullptr };
  1746. this->TTDContext->TTDExternalObjectFunctions = { createExternalObjectfp, createJsRTContextCallbackfp, releaseJsRTContextCallbackfp, setActiveJsRTContextfp };
  1747. if(record)
  1748. {
  1749. TTDAssert(optTTUri == nullptr, "No URI is needed in record mode (the host explicitly provides it when writing.");
  1750. this->TTDLog->InitForTTDRecord();
  1751. }
  1752. else
  1753. {
  1754. TTDAssert(optTTUri != nullptr, "We need a URI in replay mode so we can initialize the log from it");
  1755. this->TTDLog->InitForTTDReplay(this->TTDContext->TTDataIOInfo, optTTUri, optTTUriLength, debug);
  1756. this->sourceInfoCount = this->TTDLog->GetSourceInfoCount();
  1757. }
  1758. #if !ENABLE_TTD_DIAGNOSTICS_TRACING
  1759. if(debug)
  1760. {
  1761. #endif
  1762. this->TTDExecutionInfo = HeapNew(TTD::ExecutionInfoManager);
  1763. #if !ENABLE_TTD_DIAGNOSTICS_TRACING
  1764. }
  1765. #endif
  1766. }
  1767. #endif
  1768. BOOL
  1769. ThreadContext::ExecuteRecyclerCollectionFunction(Recycler * recycler, CollectionFunction function, CollectionFlags flags)
  1770. {
  1771. // If the thread context doesn't have an associated Recycler set, don't do anything
  1772. if (this->recycler == nullptr)
  1773. {
  1774. return FALSE;
  1775. }
  1776. // Take etw rundown lock on this thread context. We can't collect entryPoints if we are in etw rundown.
  1777. AutoCriticalSection autocs(this->GetEtwRundownCriticalSection());
  1778. // Disable calling dispose from leave script or the stack probe
  1779. // while we're executing the recycler wrapper
  1780. AutoRestoreValue<bool> callDispose(&this->callDispose, false);
  1781. BOOL ret = FALSE;
  1782. #if ENABLE_TTD
  1783. //
  1784. //TODO: We lose any events that happen in the callbacks (such as JsRelease) which may be a problem in the future.
  1785. // It may be possible to defer the collection of these objects to an explicit collection at the yield loop (same for weak set/map).
  1786. // We already indirectly do this for ScriptContext collection (but that is buggy so needs to be fixed too).
  1787. //
  1788. if(this->IsRuntimeInTTDMode())
  1789. {
  1790. this->TTDLog->PushMode(TTD::TTDMode::ExcludedExecutionTTAction);
  1791. }
  1792. #endif
  1793. if (!this->IsScriptActive())
  1794. {
  1795. Assert(!this->IsDisableImplicitCall() || this->IsInAsyncHostOperation());
  1796. ret = this->ExecuteRecyclerCollectionFunctionCommon(recycler, function, flags);
  1797. // Make sure that we finish a collect that is activated outside of script, since
  1798. // we won't have exit script to schedule it
  1799. if (!this->IsInScript() && recycler->CollectionInProgress()
  1800. && ((flags & CollectOverride_DisableIdleFinish) == 0) && threadServiceWrapper)
  1801. {
  1802. threadServiceWrapper->ScheduleFinishConcurrent();
  1803. }
  1804. }
  1805. else
  1806. {
  1807. void * frameAddr = nullptr;
  1808. GET_CURRENT_FRAME_ID(frameAddr);
  1809. // We may need stack to call out from Dispose or QC
  1810. if (!this->IsDisableImplicitCall()) // otherwise Dispose/QC disabled
  1811. {
  1812. // If we don't have stack space to call out from Dispose or QC,
  1813. // don't throw, simply return false. This gives SnailAlloc a better
  1814. // chance of allocating in low stack-space situations (like allocating
  1815. // a StackOverflowException object)
  1816. if (!this->IsStackAvailableNoThrow(Js::Constants::MinStackCallout))
  1817. {
  1818. return false;
  1819. }
  1820. }
  1821. this->LeaveScriptStart<false>(frameAddr);
  1822. ret = this->ExecuteRecyclerCollectionFunctionCommon(recycler, function, flags);
  1823. this->LeaveScriptEnd<false>(frameAddr);
  1824. if (this->callRootLevel != 0)
  1825. {
  1826. this->CheckScriptInterrupt();
  1827. }
  1828. }
  1829. #if ENABLE_TTD
  1830. if(this->IsRuntimeInTTDMode())
  1831. {
  1832. this->TTDLog->PopMode(TTD::TTDMode::ExcludedExecutionTTAction);
  1833. }
  1834. #endif
  1835. return ret;
  1836. }
  1837. void
  1838. ThreadContext::DisposeObjects(Recycler * recycler)
  1839. {
  1840. if (this->IsDisableImplicitCall())
  1841. {
  1842. // Don't dispose objects when implicit calls are disabled, since disposing may cause implicit calls. Objects will remain
  1843. // in the dispose queue and will be disposed later when implicit calls are not disabled.
  1844. return;
  1845. }
  1846. // We shouldn't DisposeObjects in NoScriptScope as this might lead to script execution.
  1847. // Callers of DisposeObjects should ensure !IsNoScriptScope() before calling DisposeObjects.
  1848. if (this->IsNoScriptScope())
  1849. {
  1850. FromDOM_NoScriptScope_fatal_error();
  1851. }
  1852. if (!this->IsScriptActive())
  1853. {
  1854. __super::DisposeObjects(recycler);
  1855. }
  1856. else
  1857. {
  1858. void * frameAddr = nullptr;
  1859. GET_CURRENT_FRAME_ID(frameAddr);
  1860. // We may need stack to call out from Dispose
  1861. this->ProbeStack(Js::Constants::MinStackCallout);
  1862. this->LeaveScriptStart<false>(frameAddr);
  1863. __super::DisposeObjects(recycler);
  1864. this->LeaveScriptEnd<false>(frameAddr);
  1865. }
  1866. }
  1867. void
  1868. ThreadContext::PushEntryExitRecord(Js::ScriptEntryExitRecord * record)
  1869. {
  1870. AssertMsg(record, "Didn't provide a script entry record to push");
  1871. Assert(record->next == nullptr);
  1872. Js::ScriptEntryExitRecord * lastRecord = this->entryExitRecord;
  1873. if (lastRecord != nullptr)
  1874. {
  1875. // If we enter script again, we should have leave with leaveForHost or leave for dispose.
  1876. Assert(lastRecord->leaveForHost || lastRecord->leaveForAsyncHostOperation);
  1877. lastRecord->hasReentered = true;
  1878. record->next = lastRecord;
  1879. // these are on stack, which grows down. if this condition doesn't hold, then the list somehow got messed up
  1880. if (!IsOnStack(lastRecord) || (uintptr_t)record >= (uintptr_t)lastRecord)
  1881. {
  1882. EntryExitRecord_Corrupted_fatal_error();
  1883. }
  1884. }
  1885. this->entryExitRecord = record;
  1886. }
  1887. void ThreadContext::PopEntryExitRecord(Js::ScriptEntryExitRecord * record)
  1888. {
  1889. AssertMsg(record && record == this->entryExitRecord, "Mismatch script entry/exit");
  1890. // these are on stack, which grows down. if this condition doesn't hold, then the list somehow got messed up
  1891. Js::ScriptEntryExitRecord * next = this->entryExitRecord->next;
  1892. if (next && (!IsOnStack(next) || (uintptr_t)this->entryExitRecord >= (uintptr_t)next))
  1893. {
  1894. EntryExitRecord_Corrupted_fatal_error();
  1895. }
  1896. this->entryExitRecord = next;
  1897. }
  1898. BOOL ThreadContext::ReserveStaticTypeIds(__in int first, __in int last)
  1899. {
  1900. if ( nextTypeId <= first )
  1901. {
  1902. nextTypeId = (Js::TypeId) last;
  1903. return TRUE;
  1904. }
  1905. else
  1906. {
  1907. return FALSE;
  1908. }
  1909. }
  1910. Js::TypeId ThreadContext::ReserveTypeIds(int count)
  1911. {
  1912. Js::TypeId firstTypeId = nextTypeId;
  1913. nextTypeId = (Js::TypeId)(nextTypeId + count);
  1914. return firstTypeId;
  1915. }
  1916. Js::TypeId ThreadContext::CreateTypeId()
  1917. {
  1918. return nextTypeId = (Js::TypeId)(nextTypeId + 1);
  1919. }
  1920. WellKnownHostType ThreadContext::GetWellKnownHostType(Js::TypeId typeId)
  1921. {
  1922. if (this->wellKnownHostTypeHTMLAllCollectionTypeId == typeId)
  1923. {
  1924. return WellKnownHostType_HTMLAllCollection;
  1925. }
  1926. return WellKnownHostType_Invalid;
  1927. }
  1928. void ThreadContext::SetWellKnownHostTypeId(WellKnownHostType wellKnownType, Js::TypeId typeId)
  1929. {
  1930. AssertMsg(WellKnownHostType_HTMLAllCollection == wellKnownType, "ThreadContext::SetWellKnownHostTypeId called on type other than HTMLAllCollection");
  1931. if (WellKnownHostType_HTMLAllCollection == wellKnownType)
  1932. {
  1933. this->wellKnownHostTypeHTMLAllCollectionTypeId = typeId;
  1934. #if ENABLE_NATIVE_CODEGEN
  1935. if (this->m_remoteThreadContextInfo)
  1936. {
  1937. HRESULT hr = JITManager::GetJITManager()->SetWellKnownHostTypeId(this->m_remoteThreadContextInfo, (int)typeId);
  1938. JITManager::HandleServerCallResult(hr, RemoteCallType::StateUpdate);
  1939. }
  1940. #endif
  1941. }
  1942. }
  1943. void ThreadContext::EnsureDebugManager()
  1944. {
  1945. if (this->debugManager == nullptr)
  1946. {
  1947. this->debugManager = HeapNew(Js::DebugManager, this, this->GetAllocationPolicyManager());
  1948. }
  1949. InterlockedIncrement(&crefSContextForDiag);
  1950. Assert(this->debugManager != nullptr);
  1951. }
  1952. void ThreadContext::ReleaseDebugManager()
  1953. {
  1954. Assert(crefSContextForDiag > 0);
  1955. Assert(this->debugManager != nullptr);
  1956. LONG lref = InterlockedDecrement(&crefSContextForDiag);
  1957. if (lref == 0)
  1958. {
  1959. if (this->recyclableData != nullptr)
  1960. {
  1961. this->recyclableData->returnedValueList = nullptr;
  1962. }
  1963. if (this->debugManager != nullptr)
  1964. {
  1965. this->debugManager->Close();
  1966. HeapDelete(this->debugManager);
  1967. this->debugManager = nullptr;
  1968. }
  1969. }
  1970. }
  1971. Js::TempArenaAllocatorObject *
  1972. ThreadContext::GetTemporaryAllocator(LPCWSTR name)
  1973. {
  1974. AssertCanHandleOutOfMemory();
  1975. if (temporaryArenaAllocatorCount != 0)
  1976. {
  1977. temporaryArenaAllocatorCount--;
  1978. Js::TempArenaAllocatorObject * allocator = recyclableData->temporaryArenaAllocators[temporaryArenaAllocatorCount];
  1979. recyclableData->temporaryArenaAllocators[temporaryArenaAllocatorCount] = nullptr;
  1980. return allocator;
  1981. }
  1982. return Js::TempArenaAllocatorObject::Create(this);
  1983. }
  1984. void
  1985. ThreadContext::ReleaseTemporaryAllocator(Js::TempArenaAllocatorObject * tempAllocator)
  1986. {
  1987. if (temporaryArenaAllocatorCount < MaxTemporaryArenaAllocators)
  1988. {
  1989. tempAllocator->GetAllocator()->Reset();
  1990. recyclableData->temporaryArenaAllocators[temporaryArenaAllocatorCount] = tempAllocator;
  1991. temporaryArenaAllocatorCount++;
  1992. return;
  1993. }
  1994. tempAllocator->Dispose(false);
  1995. }
  1996. Js::TempGuestArenaAllocatorObject *
  1997. ThreadContext::GetTemporaryGuestAllocator(LPCWSTR name)
  1998. {
  1999. AssertCanHandleOutOfMemory();
  2000. if (temporaryGuestArenaAllocatorCount != 0)
  2001. {
  2002. temporaryGuestArenaAllocatorCount--;
  2003. Js::TempGuestArenaAllocatorObject * allocator = recyclableData->temporaryGuestArenaAllocators[temporaryGuestArenaAllocatorCount];
  2004. allocator->AdviseInUse();
  2005. recyclableData->temporaryGuestArenaAllocators[temporaryGuestArenaAllocatorCount] = nullptr;
  2006. return allocator;
  2007. }
  2008. return Js::TempGuestArenaAllocatorObject::Create(this);
  2009. }
  2010. void
  2011. ThreadContext::ReleaseTemporaryGuestAllocator(Js::TempGuestArenaAllocatorObject * tempGuestAllocator)
  2012. {
  2013. if (temporaryGuestArenaAllocatorCount < MaxTemporaryArenaAllocators)
  2014. {
  2015. tempGuestAllocator->AdviseNotInUse();
  2016. recyclableData->temporaryGuestArenaAllocators[temporaryGuestArenaAllocatorCount] = tempGuestAllocator;
  2017. temporaryGuestArenaAllocatorCount++;
  2018. return;
  2019. }
  2020. tempGuestAllocator->Dispose(false);
  2021. }
  2022. void
  2023. ThreadContext::AddToPendingScriptContextCloseList(Js::ScriptContext * scriptContext)
  2024. {
  2025. Assert(scriptContext != nullptr);
  2026. if (rootPendingClose == nullptr)
  2027. {
  2028. rootPendingClose = scriptContext;
  2029. return;
  2030. }
  2031. // Prepend to the list.
  2032. scriptContext->SetNextPendingClose(rootPendingClose);
  2033. rootPendingClose = scriptContext;
  2034. }
  2035. void
  2036. ThreadContext::RemoveFromPendingClose(Js::ScriptContext * scriptContext)
  2037. {
  2038. Assert(scriptContext != nullptr);
  2039. if (rootPendingClose == nullptr)
  2040. {
  2041. // We already sent a close message, ignore the notification.
  2042. return;
  2043. }
  2044. // Base case: The root is being removed. Move the root along.
  2045. if (scriptContext == rootPendingClose)
  2046. {
  2047. rootPendingClose = rootPendingClose->GetNextPendingClose();
  2048. return;
  2049. }
  2050. Js::ScriptContext * currScriptContext = rootPendingClose;
  2051. Js::ScriptContext * nextScriptContext = nullptr;
  2052. while (currScriptContext)
  2053. {
  2054. nextScriptContext = currScriptContext->GetNextPendingClose();
  2055. if (!nextScriptContext)
  2056. {
  2057. break;
  2058. }
  2059. if (nextScriptContext == scriptContext) {
  2060. // The next pending close ScriptContext is the one to be removed - set prev->next to next->next
  2061. currScriptContext->SetNextPendingClose(nextScriptContext->GetNextPendingClose());
  2062. return;
  2063. }
  2064. currScriptContext = nextScriptContext;
  2065. }
  2066. // We expect to find scriptContext in the pending close list.
  2067. Assert(false);
  2068. }
  2069. void ThreadContext::ClosePendingScriptContexts()
  2070. {
  2071. Js::ScriptContext * scriptContext = rootPendingClose;
  2072. if (scriptContext == nullptr)
  2073. {
  2074. return;
  2075. }
  2076. Js::ScriptContext * nextScriptContext;
  2077. do
  2078. {
  2079. nextScriptContext = scriptContext->GetNextPendingClose();
  2080. scriptContext->Close(false);
  2081. scriptContext = nextScriptContext;
  2082. }
  2083. while (scriptContext);
  2084. rootPendingClose = nullptr;
  2085. }
  2086. void
  2087. ThreadContext::AddToPendingProjectionContextCloseList(IProjectionContext *projectionContext)
  2088. {
  2089. pendingProjectionContextCloseList->Add(projectionContext);
  2090. }
  2091. void
  2092. ThreadContext::RemoveFromPendingClose(IProjectionContext* projectionContext)
  2093. {
  2094. pendingProjectionContextCloseList->Remove(projectionContext);
  2095. }
  2096. void ThreadContext::ClosePendingProjectionContexts()
  2097. {
  2098. IProjectionContext* projectionContext;
  2099. for (int i = 0 ; i < pendingProjectionContextCloseList->Count(); i++)
  2100. {
  2101. projectionContext = pendingProjectionContextCloseList->Item(i);
  2102. projectionContext->Close();
  2103. }
  2104. pendingProjectionContextCloseList->Clear();
  2105. }
  2106. void
  2107. ThreadContext::RegisterScriptContext(Js::ScriptContext *scriptContext)
  2108. {
  2109. // NOTE: ETW rundown thread may be reading the scriptContextList concurrently. We don't need to
  2110. // lock access because we only insert to the front here.
  2111. scriptContext->next = this->scriptContextList;
  2112. if (this->scriptContextList)
  2113. {
  2114. Assert(this->scriptContextList->prev == NULL);
  2115. this->scriptContextList->prev = scriptContext;
  2116. }
  2117. scriptContext->prev = NULL;
  2118. this->scriptContextList = scriptContext;
  2119. if(NoJIT())
  2120. {
  2121. scriptContext->ForceNoNative();
  2122. }
  2123. #if DBG || defined(RUNTIME_DATA_COLLECTION)
  2124. scriptContextCount++;
  2125. #endif
  2126. scriptContextEverRegistered = true;
  2127. }
  2128. void
  2129. ThreadContext::UnregisterScriptContext(Js::ScriptContext *scriptContext)
  2130. {
  2131. // NOTE: ETW rundown thread may be reading the scriptContextList concurrently. Since this function
  2132. // is only called by ~ScriptContext() which already synchronized to ETW rundown, we are safe here.
  2133. if (scriptContext == this->scriptContextList)
  2134. {
  2135. Assert(scriptContext->prev == NULL);
  2136. this->scriptContextList = scriptContext->next;
  2137. }
  2138. else
  2139. {
  2140. scriptContext->prev->next = scriptContext->next;
  2141. }
  2142. if (scriptContext->next)
  2143. {
  2144. scriptContext->next->prev = scriptContext->prev;
  2145. }
  2146. scriptContext->prev = nullptr;
  2147. scriptContext->next = nullptr;
  2148. #if DBG || defined(RUNTIME_DATA_COLLECTION)
  2149. scriptContextCount--;
  2150. #endif
  2151. }
  2152. ThreadContext::CollectCallBack *
  2153. ThreadContext::AddRecyclerCollectCallBack(RecyclerCollectCallBackFunction callback, void * context)
  2154. {
  2155. AutoCriticalSection autocs(&csCollectionCallBack);
  2156. CollectCallBack * collectCallBack = this->collectCallBackList.PrependNode(&HeapAllocator::Instance);
  2157. collectCallBack->callback = callback;
  2158. collectCallBack->context = context;
  2159. this->hasCollectionCallBack = true;
  2160. return collectCallBack;
  2161. }
  2162. void
  2163. ThreadContext::RemoveRecyclerCollectCallBack(ThreadContext::CollectCallBack * collectCallBack)
  2164. {
  2165. AutoCriticalSection autocs(&csCollectionCallBack);
  2166. this->collectCallBackList.RemoveElement(&HeapAllocator::Instance, collectCallBack);
  2167. this->hasCollectionCallBack = !this->collectCallBackList.Empty();
  2168. }
  2169. void
  2170. ThreadContext::PreCollectionCallBack(CollectionFlags flags)
  2171. {
  2172. #ifdef PERF_COUNTERS
  2173. PHASE_PRINT_TESTTRACE1(Js::DeferParsePhase, _u("TestTrace: deferparse - # of func: %d # deferparsed: %d\n"), PerfCounter::CodeCounterSet::GetTotalFunctionCounter().GetValue(), PerfCounter::CodeCounterSet::GetDeferredFunctionCounter().GetValue());
  2174. #endif
  2175. // This needs to be done before ClearInlineCaches since that method can empty the list of
  2176. // script contexts with inline caches
  2177. this->ClearScriptContextCaches();
  2178. // Clear up references to types to avoid keep them alive
  2179. this->ClearPrototypeChainEnsuredToHaveOnlyWritableDataPropertiesCaches();
  2180. // Clean up unused memory before we start collecting
  2181. this->CleanNoCasePropertyMap();
  2182. this->TryEnterExpirableCollectMode();
  2183. const BOOL concurrent = flags & CollectMode_Concurrent;
  2184. const BOOL partial = flags & CollectMode_Partial;
  2185. if (!partial)
  2186. {
  2187. // Integrate allocated pages from background JIT threads
  2188. #if ENABLE_NATIVE_CODEGEN
  2189. #if !FLOATVAR
  2190. if (codeGenNumberThreadAllocator)
  2191. {
  2192. codeGenNumberThreadAllocator->Integrate();
  2193. }
  2194. if (this->xProcNumberPageSegmentManager)
  2195. {
  2196. this->xProcNumberPageSegmentManager->Integrate();
  2197. }
  2198. #endif
  2199. #endif
  2200. }
  2201. RecyclerCollectCallBackFlags callBackFlags = (RecyclerCollectCallBackFlags)
  2202. ((concurrent ? Collect_Begin_Concurrent : Collect_Begin) | (partial? Collect_Begin_Partial : Collect_Begin));
  2203. CollectionCallBack(callBackFlags);
  2204. }
  2205. void
  2206. ThreadContext::PreSweepCallback()
  2207. {
  2208. #ifdef PERSISTENT_INLINE_CACHES
  2209. ClearInlineCachesWithDeadWeakRefs();
  2210. #else
  2211. ClearInlineCaches();
  2212. #endif
  2213. ClearIsInstInlineCaches();
  2214. ClearEquivalentTypeCaches();
  2215. ClearForInCaches();
  2216. this->dynamicObjectEnumeratorCacheMap.Clear();
  2217. }
  2218. void
  2219. ThreadContext::CollectionCallBack(RecyclerCollectCallBackFlags flags)
  2220. {
  2221. DListBase<CollectCallBack>::Iterator i(&this->collectCallBackList);
  2222. while (i.Next())
  2223. {
  2224. i.Data().callback(i.Data().context, flags);
  2225. }
  2226. }
  2227. void
  2228. ThreadContext::WaitCollectionCallBack()
  2229. {
  2230. // Avoid taking the lock if there are no call back
  2231. if (hasCollectionCallBack)
  2232. {
  2233. AutoCriticalSection autocs(&csCollectionCallBack);
  2234. CollectionCallBack(Collect_Wait);
  2235. }
  2236. }
  2237. void
  2238. ThreadContext::PostCollectionCallBack()
  2239. {
  2240. CollectionCallBack(Collect_End);
  2241. TryExitExpirableCollectMode();
  2242. // Recycler is null in the case where the ThreadContext is in the process of creating the recycler and
  2243. // we have a GC triggered (say because the -recyclerStress flag is passed in)
  2244. if (this->recycler != NULL)
  2245. {
  2246. if (this->recycler->InCacheCleanupCollection())
  2247. {
  2248. this->recycler->ClearCacheCleanupCollection();
  2249. for (Js::ScriptContext *scriptContext = scriptContextList; scriptContext; scriptContext = scriptContext->next)
  2250. {
  2251. scriptContext->CleanupWeakReferenceDictionaries();
  2252. }
  2253. }
  2254. }
  2255. }
  2256. void
  2257. ThreadContext::PostSweepRedeferralCallBack()
  2258. {
  2259. if (this->DoTryRedeferral())
  2260. {
  2261. HRESULT hr = S_OK;
  2262. BEGIN_TRANSLATE_OOM_TO_HRESULT
  2263. {
  2264. this->TryRedeferral();
  2265. }
  2266. END_TRANSLATE_OOM_TO_HRESULT(hr);
  2267. }
  2268. this->UpdateRedeferralState();
  2269. }
  2270. bool
  2271. ThreadContext::DoTryRedeferral() const
  2272. {
  2273. if (PHASE_FORCE1(Js::RedeferralPhase) || PHASE_STRESS1(Js::RedeferralPhase))
  2274. {
  2275. return true;
  2276. }
  2277. if (PHASE_OFF1(Js::RedeferralPhase))
  2278. {
  2279. return false;
  2280. }
  2281. switch (this->redeferralState)
  2282. {
  2283. case InitialRedeferralState:
  2284. return false;
  2285. case StartupRedeferralState:
  2286. return gcSinceCallCountsCollected >= StartupRedeferralInactiveThreshold;
  2287. case MainRedeferralState:
  2288. return gcSinceCallCountsCollected >= MainRedeferralInactiveThreshold;
  2289. default:
  2290. Assert(0);
  2291. return false;
  2292. };
  2293. }
  2294. bool
  2295. ThreadContext::DoRedeferFunctionBodies() const
  2296. {
  2297. #if ENABLE_TTD
  2298. if (this->IsRuntimeInTTDMode())
  2299. {
  2300. return false;
  2301. }
  2302. #endif
  2303. if (PHASE_FORCE1(Js::RedeferralPhase) || PHASE_STRESS1(Js::RedeferralPhase))
  2304. {
  2305. return true;
  2306. }
  2307. if (PHASE_OFF1(Js::RedeferralPhase))
  2308. {
  2309. return false;
  2310. }
  2311. switch (this->redeferralState)
  2312. {
  2313. case InitialRedeferralState:
  2314. return false;
  2315. case StartupRedeferralState:
  2316. return gcSinceLastRedeferral >= StartupRedeferralCheckInterval;
  2317. case MainRedeferralState:
  2318. return gcSinceLastRedeferral >= MainRedeferralCheckInterval;
  2319. default:
  2320. Assert(0);
  2321. return false;
  2322. };
  2323. }
  2324. uint
  2325. ThreadContext::GetRedeferralCollectionInterval() const
  2326. {
  2327. switch(this->redeferralState)
  2328. {
  2329. case InitialRedeferralState:
  2330. return InitialRedeferralDelay;
  2331. case StartupRedeferralState:
  2332. return StartupRedeferralCheckInterval;
  2333. case MainRedeferralState:
  2334. return MainRedeferralCheckInterval;
  2335. default:
  2336. Assert(0);
  2337. return (uint)-1;
  2338. }
  2339. }
  2340. uint
  2341. ThreadContext::GetRedeferralInactiveThreshold() const
  2342. {
  2343. switch(this->redeferralState)
  2344. {
  2345. case InitialRedeferralState:
  2346. return InitialRedeferralDelay;
  2347. case StartupRedeferralState:
  2348. return StartupRedeferralInactiveThreshold;
  2349. case MainRedeferralState:
  2350. return MainRedeferralInactiveThreshold;
  2351. default:
  2352. Assert(0);
  2353. return (uint)-1;
  2354. }
  2355. }
  2356. void
  2357. ThreadContext::TryRedeferral()
  2358. {
  2359. bool doRedefer = this->DoRedeferFunctionBodies();
  2360. // Collect the set of active functions.
  2361. ActiveFunctionSet *pActiveFuncs = nullptr;
  2362. if (doRedefer)
  2363. {
  2364. pActiveFuncs = Anew(this->GetThreadAlloc(), ActiveFunctionSet, this->GetThreadAlloc());
  2365. this->GetActiveFunctions(pActiveFuncs);
  2366. #if DBG
  2367. this->redeferredFunctions = 0;
  2368. this->recoveredBytes = 0;
  2369. #endif
  2370. }
  2371. uint inactiveThreshold = this->GetRedeferralInactiveThreshold();
  2372. Js::ScriptContext *scriptContext;
  2373. for (scriptContext = GetScriptContextList(); scriptContext; scriptContext = scriptContext->next)
  2374. {
  2375. if (scriptContext->IsClosed())
  2376. {
  2377. continue;
  2378. }
  2379. scriptContext->RedeferFunctionBodies(pActiveFuncs, inactiveThreshold);
  2380. }
  2381. if (pActiveFuncs)
  2382. {
  2383. Adelete(this->GetThreadAlloc(), pActiveFuncs);
  2384. #if DBG
  2385. if (PHASE_STATS1(Js::RedeferralPhase) && this->redeferredFunctions)
  2386. {
  2387. Output::Print(_u("Redeferred: %d, Bytes: 0x%x\n"), this->redeferredFunctions, this->recoveredBytes);
  2388. }
  2389. #endif
  2390. }
  2391. }
  2392. void
  2393. ThreadContext::GetActiveFunctions(ActiveFunctionSet * pActiveFuncs)
  2394. {
  2395. if (!this->IsInScript() || this->entryExitRecord == nullptr)
  2396. {
  2397. return;
  2398. }
  2399. Js::JavascriptStackWalker walker(GetScriptContextList(), TRUE, NULL, true);
  2400. Js::JavascriptFunction *function = nullptr;
  2401. while (walker.GetCallerWithoutInlinedFrames(&function))
  2402. {
  2403. if (function->GetFunctionInfo()->HasBody())
  2404. {
  2405. Js::FunctionBody *body = function->GetFunctionInfo()->GetFunctionBody();
  2406. body->UpdateActiveFunctionSet(pActiveFuncs, nullptr);
  2407. }
  2408. }
  2409. }
  2410. void
  2411. ThreadContext::UpdateRedeferralState()
  2412. {
  2413. uint inactiveThreshold = this->GetRedeferralInactiveThreshold();
  2414. uint collectInterval = this->GetRedeferralCollectionInterval();
  2415. if (this->gcSinceCallCountsCollected >= inactiveThreshold)
  2416. {
  2417. this->gcSinceCallCountsCollected = 0;
  2418. if (this->gcSinceLastRedeferral >= collectInterval)
  2419. {
  2420. // Advance state
  2421. switch (this->redeferralState)
  2422. {
  2423. case InitialRedeferralState:
  2424. this->redeferralState = StartupRedeferralState;
  2425. break;
  2426. case StartupRedeferralState:
  2427. this->redeferralState = MainRedeferralState;
  2428. break;
  2429. case MainRedeferralState:
  2430. break;
  2431. default:
  2432. Assert(0);
  2433. break;
  2434. }
  2435. this->gcSinceLastRedeferral = 0;
  2436. }
  2437. }
  2438. else
  2439. {
  2440. this->gcSinceCallCountsCollected++;
  2441. this->gcSinceLastRedeferral++;
  2442. }
  2443. }
  2444. #ifdef FAULT_INJECTION
  2445. void
  2446. ThreadContext::DisposeScriptContextByFaultInjectionCallBack()
  2447. {
  2448. if (FAULTINJECT_SCRIPT_TERMINATION_ON_DISPOSE) {
  2449. int scriptContextToClose = -1;
  2450. /* inject only if we have more than 1 script context*/
  2451. uint totalScriptCount = GetScriptContextCount();
  2452. if (totalScriptCount > 1) {
  2453. if (Js::Configuration::Global.flags.FaultInjectionScriptContextToTerminateCount > 0)
  2454. {
  2455. scriptContextToClose = Js::Configuration::Global.flags.FaultInjectionScriptContextToTerminateCount % totalScriptCount;
  2456. for (Js::ScriptContext *scriptContext = GetScriptContextList(); scriptContext; scriptContext = scriptContext->next)
  2457. {
  2458. if (scriptContextToClose-- == 0)
  2459. {
  2460. scriptContext->DisposeScriptContextByFaultInjection();
  2461. break;
  2462. }
  2463. }
  2464. }
  2465. else
  2466. {
  2467. fwprintf(stderr, _u("***FI: FaultInjectionScriptContextToTerminateCount Failed, Value should be > 0. \n"));
  2468. }
  2469. }
  2470. }
  2471. }
  2472. #endif
  2473. #pragma region "Expirable Object Methods"
  2474. void
  2475. ThreadContext::TryExitExpirableCollectMode()
  2476. {
  2477. // If this feature is turned off or if we're already in profile collection mode, do nothing
  2478. // We also do nothing if expiration is explicitly disabled by someone lower down the stack
  2479. if (PHASE_OFF1(Js::ExpirableCollectPhase) || !InExpirableCollectMode() || this->disableExpiration)
  2480. {
  2481. return;
  2482. }
  2483. if (InExpirableCollectMode())
  2484. {
  2485. OUTPUT_TRACE(Js::ExpirableCollectPhase, _u("Checking to see whether to complete Expirable Object Collection: GC Count is %d\n"), this->expirableCollectModeGcCount);
  2486. if (this->expirableCollectModeGcCount > 0)
  2487. {
  2488. this->expirableCollectModeGcCount--;
  2489. }
  2490. if (this->expirableCollectModeGcCount == 0 &&
  2491. (this->recycler->InCacheCleanupCollection() || CONFIG_FLAG(ForceExpireOnNonCacheCollect)))
  2492. {
  2493. OUTPUT_TRACE(Js::ExpirableCollectPhase, _u("Completing Expirable Object Collection\n"));
  2494. ExpirableObjectList::Iterator expirableObjectIterator(this->expirableObjectList);
  2495. while (expirableObjectIterator.Next())
  2496. {
  2497. ExpirableObject* object = expirableObjectIterator.Data();
  2498. Assert(object);
  2499. if (!object->IsObjectUsed())
  2500. {
  2501. object->Expire();
  2502. }
  2503. }
  2504. // Leave expirable collection mode
  2505. expirableCollectModeGcCount = -1;
  2506. }
  2507. }
  2508. }
  2509. bool
  2510. ThreadContext::InExpirableCollectMode()
  2511. {
  2512. // We're in expirable collect if we have expirable objects registered,
  2513. // and expirableCollectModeGcCount is not negative
  2514. // and when debugger is attaching, it might have set the function to deferredParse.
  2515. return (expirableObjectList != nullptr &&
  2516. numExpirableObjects > 0 &&
  2517. expirableCollectModeGcCount >= 0 &&
  2518. (this->GetDebugManager() != nullptr &&
  2519. !this->GetDebugManager()->IsDebuggerAttaching()));
  2520. }
  2521. void
  2522. ThreadContext::TryEnterExpirableCollectMode()
  2523. {
  2524. // If this feature is turned off or if we're already in profile collection mode, do nothing
  2525. if (PHASE_OFF1(Js::ExpirableCollectPhase) || InExpirableCollectMode())
  2526. {
  2527. OUTPUT_TRACE(Js::ExpirableCollectPhase, _u("Not running Expirable Object Collection\n"));
  2528. return;
  2529. }
  2530. double entryPointCollectionThreshold = Js::Configuration::Global.flags.ExpirableCollectionTriggerThreshold / 100.0;
  2531. double currentThreadNativeCodeRatio = ((double) GetCodeSize()) / Js::Constants::MaxThreadJITCodeHeapSize;
  2532. OUTPUT_TRACE(Js::ExpirableCollectPhase, _u("Current native code ratio: %f\n"), currentThreadNativeCodeRatio);
  2533. if (currentThreadNativeCodeRatio > entryPointCollectionThreshold)
  2534. {
  2535. OUTPUT_TRACE(Js::ExpirableCollectPhase, _u("Setting up Expirable Object Collection\n"));
  2536. this->expirableCollectModeGcCount = Js::Configuration::Global.flags.ExpirableCollectionGCCount;
  2537. ExpirableObjectList::Iterator expirableObjectIterator(this->expirableObjectList);
  2538. while (expirableObjectIterator.Next())
  2539. {
  2540. ExpirableObject* object = expirableObjectIterator.Data();
  2541. Assert(object);
  2542. object->EnterExpirableCollectMode();
  2543. }
  2544. if (this->entryExitRecord != nullptr)
  2545. {
  2546. // If we're in script, we will do a stack walk, find the JavascriptFunction's on the stack
  2547. // and mark their entry points as being used so that we don't prematurely expire them
  2548. Js::ScriptContext* topScriptContext = this->entryExitRecord->scriptContext;
  2549. Js::JavascriptStackWalker walker(topScriptContext, TRUE);
  2550. Js::JavascriptFunction* javascriptFunction = nullptr;
  2551. while (walker.GetCallerWithoutInlinedFrames(&javascriptFunction))
  2552. {
  2553. if (javascriptFunction != nullptr && Js::ScriptFunction::Is(javascriptFunction))
  2554. {
  2555. Js::ScriptFunction* scriptFunction = (Js::ScriptFunction*) javascriptFunction;
  2556. Js::FunctionEntryPointInfo* entryPointInfo = scriptFunction->GetFunctionEntryPointInfo();
  2557. entryPointInfo->SetIsObjectUsed();
  2558. scriptFunction->GetFunctionBody()->MapEntryPoints([](int index, Js::FunctionEntryPointInfo* entryPoint){
  2559. entryPoint->SetIsObjectUsed();
  2560. });
  2561. }
  2562. }
  2563. }
  2564. }
  2565. }
  2566. void
  2567. ThreadContext::RegisterExpirableObject(ExpirableObject* object)
  2568. {
  2569. Assert(this->expirableObjectList);
  2570. Assert(object->registrationHandle == nullptr);
  2571. ExpirableObject** registrationData = this->expirableObjectList->PrependNode();
  2572. (*registrationData) = object;
  2573. object->registrationHandle = (void*) registrationData;
  2574. OUTPUT_VERBOSE_TRACE(Js::ExpirableCollectPhase, _u("Registered 0x%p\n"), object);
  2575. numExpirableObjects++;
  2576. }
  2577. void
  2578. ThreadContext::UnregisterExpirableObject(ExpirableObject* object)
  2579. {
  2580. Assert(this->expirableObjectList);
  2581. Assert(object->registrationHandle != nullptr);
  2582. Assert(this->expirableObjectList->HasElement(
  2583. (ExpirableObject* const *)PointerValue(object->registrationHandle)));
  2584. ExpirableObject** registrationData = (ExpirableObject**)PointerValue(object->registrationHandle);
  2585. Assert(*registrationData == object);
  2586. this->expirableObjectList->MoveElementTo(registrationData, this->expirableObjectDisposeList);
  2587. object->registrationHandle = nullptr;
  2588. OUTPUT_VERBOSE_TRACE(Js::ExpirableCollectPhase, _u("Unregistered 0x%p\n"), object);
  2589. numExpirableObjects--;
  2590. }
  2591. void
  2592. ThreadContext::DisposeExpirableObject(ExpirableObject* object)
  2593. {
  2594. Assert(this->expirableObjectDisposeList);
  2595. Assert(object->registrationHandle == nullptr);
  2596. this->expirableObjectDisposeList->Remove(object);
  2597. OUTPUT_VERBOSE_TRACE(Js::ExpirableCollectPhase, _u("Disposed 0x%p\n"), object);
  2598. }
  2599. #pragma endregion
  2600. void
  2601. ThreadContext::ClearScriptContextCaches()
  2602. {
  2603. for (Js::ScriptContext *scriptContext = scriptContextList; scriptContext != nullptr; scriptContext = scriptContext->next)
  2604. {
  2605. scriptContext->ClearScriptContextCaches();
  2606. }
  2607. }
  2608. #ifdef PERSISTENT_INLINE_CACHES
  2609. void
  2610. ThreadContext::ClearInlineCachesWithDeadWeakRefs()
  2611. {
  2612. for (Js::ScriptContext *scriptContext = scriptContextList; scriptContext != nullptr; scriptContext = scriptContext->next)
  2613. {
  2614. scriptContext->ClearInlineCachesWithDeadWeakRefs();
  2615. }
  2616. if (PHASE_TRACE1(Js::InlineCachePhase))
  2617. {
  2618. size_t size = 0;
  2619. size_t freeListSize = 0;
  2620. size_t polyInlineCacheSize = 0;
  2621. uint scriptContextCount = 0;
  2622. for (Js::ScriptContext *scriptContext = scriptContextList;
  2623. scriptContext;
  2624. scriptContext = scriptContext->next)
  2625. {
  2626. scriptContextCount++;
  2627. size += scriptContext->GetInlineCacheAllocator()->AllocatedSize();
  2628. freeListSize += scriptContext->GetInlineCacheAllocator()->FreeListSize();
  2629. #ifdef POLY_INLINE_CACHE_SIZE_STATS
  2630. polyInlineCacheSize += scriptContext->GetInlineCacheAllocator()->GetPolyInlineCacheSize();
  2631. #endif
  2632. };
  2633. printf("Inline cache arena: total = %5I64u KB, free list = %5I64u KB, poly caches = %5I64u KB, script contexts = %u\n",
  2634. static_cast<uint64>(size / 1024), static_cast<uint64>(freeListSize / 1024), static_cast<uint64>(polyInlineCacheSize / 1024), scriptContextCount);
  2635. }
  2636. }
  2637. void
  2638. ThreadContext::ClearInvalidatedUniqueGuards()
  2639. {
  2640. // If a propertyGuard was invalidated, make sure to remove it's entry from unique property guard table of other property records.
  2641. PropertyGuardDictionary &guards = this->recyclableData->propertyGuards;
  2642. guards.Map([this](Js::PropertyRecord const * propertyRecord, PropertyGuardEntry* entry, const RecyclerWeakReference<const Js::PropertyRecord>* weakRef)
  2643. {
  2644. entry->uniqueGuards.MapAndRemoveIf([=](RecyclerWeakReference<Js::PropertyGuard>* guardWeakRef)
  2645. {
  2646. Js::PropertyGuard* guard = guardWeakRef->Get();
  2647. bool shouldRemove = guard != nullptr && !guard->IsValid();
  2648. if (shouldRemove)
  2649. {
  2650. if (PHASE_TRACE1(Js::TracePropertyGuardsPhase) || PHASE_VERBOSE_TRACE1(Js::FixedMethodsPhase))
  2651. {
  2652. Output::Print(_u("FixedFields: invalidating guard: name: %s, address: 0x%p, value: 0x%p, value address: 0x%p\n"),
  2653. propertyRecord->GetBuffer(), guard, guard->GetValue(), guard->GetAddressOfValue());
  2654. Output::Flush();
  2655. }
  2656. if (PHASE_TESTTRACE1(Js::TracePropertyGuardsPhase) || PHASE_VERBOSE_TESTTRACE1(Js::FixedMethodsPhase))
  2657. {
  2658. Output::Print(_u("FixedFields: invalidating guard: name: %s, value: 0x%p\n"),
  2659. propertyRecord->GetBuffer(), guard->GetValue());
  2660. Output::Flush();
  2661. }
  2662. }
  2663. return shouldRemove;
  2664. });
  2665. });
  2666. }
  2667. void
  2668. ThreadContext::ClearInlineCaches()
  2669. {
  2670. if (PHASE_TRACE1(Js::InlineCachePhase))
  2671. {
  2672. size_t size = 0;
  2673. size_t freeListSize = 0;
  2674. size_t polyInlineCacheSize = 0;
  2675. uint scriptContextCount = 0;
  2676. for (Js::ScriptContext *scriptContext = scriptContextList;
  2677. scriptContext;
  2678. scriptContext = scriptContext->next)
  2679. {
  2680. scriptContextCount++;
  2681. size += scriptContext->GetInlineCacheAllocator()->AllocatedSize();
  2682. freeListSize += scriptContext->GetInlineCacheAllocator()->FreeListSize();
  2683. #ifdef POLY_INLINE_CACHE_SIZE_STATS
  2684. polyInlineCacheSize += scriptContext->GetInlineCacheAllocator()->GetPolyInlineCacheSize();
  2685. #endif
  2686. };
  2687. printf("Inline cache arena: total = %5I64u KB, free list = %5I64u KB, poly caches = %5I64u KB, script contexts = %u\n",
  2688. static_cast<uint64>(size / 1024), static_cast<uint64>(freeListSize / 1024), static_cast<uint64>(polyInlineCacheSize / 1024), scriptContextCount);
  2689. }
  2690. Js::ScriptContext *scriptContext = this->scriptContextList;
  2691. while (scriptContext != nullptr)
  2692. {
  2693. scriptContext->ClearInlineCaches();
  2694. scriptContext = scriptContext->next;
  2695. }
  2696. inlineCacheThreadInfoAllocator.Reset();
  2697. protoInlineCacheByPropId.ResetNoDelete();
  2698. storeFieldInlineCacheByPropId.ResetNoDelete();
  2699. registeredInlineCacheCount = 0;
  2700. unregisteredInlineCacheCount = 0;
  2701. }
  2702. #endif //PERSISTENT_INLINE_CACHES
  2703. void
  2704. ThreadContext::ClearIsInstInlineCaches()
  2705. {
  2706. Js::ScriptContext *scriptContext = this->scriptContextList;
  2707. while (scriptContext != nullptr)
  2708. {
  2709. scriptContext->ClearIsInstInlineCaches();
  2710. scriptContext = scriptContext->next;
  2711. }
  2712. isInstInlineCacheThreadInfoAllocator.Reset();
  2713. isInstInlineCacheByFunction.ResetNoDelete();
  2714. }
  2715. void
  2716. ThreadContext::ClearForInCaches()
  2717. {
  2718. Js::ScriptContext *scriptContext = this->scriptContextList;
  2719. while (scriptContext != nullptr)
  2720. {
  2721. scriptContext->ClearForInCaches();
  2722. scriptContext = scriptContext->next;
  2723. }
  2724. }
  2725. void
  2726. ThreadContext::ClearEquivalentTypeCaches()
  2727. {
  2728. #if ENABLE_NATIVE_CODEGEN
  2729. // Called from PreSweepCallback to clear pointers to types that have no live object references left.
  2730. // The EquivalentTypeCache used to keep these types alive, but this caused memory growth in cases where
  2731. // entry points stayed around for a long time.
  2732. // In future we may want to pin the reference/guard type to the entry point, but that choice will depend
  2733. // on a use case where pinning the type helps us optimize. Lacking that, clearing the guard type is a
  2734. // simpler short-term solution.
  2735. // Note that clearing unmarked types from the cache and guard is needed for correctness if the cache doesn't keep
  2736. // the types alive.
  2737. FOREACH_DLISTBASE_ENTRY_EDITING(Js::EntryPointInfo *, entryPoint, &equivalentTypeCacheEntryPoints, iter)
  2738. {
  2739. bool isLive = entryPoint->ClearEquivalentTypeCaches();
  2740. if (!isLive)
  2741. {
  2742. iter.RemoveCurrent(&equivalentTypeCacheInfoAllocator);
  2743. }
  2744. }
  2745. NEXT_DLISTBASE_ENTRY_EDITING;
  2746. // Note: Don't reset the list, because we're only clearing the dead types from these caches.
  2747. // There may still be type references we need to keep an eye on.
  2748. #endif
  2749. }
  2750. Js::EntryPointInfo **
  2751. ThreadContext::RegisterEquivalentTypeCacheEntryPoint(Js::EntryPointInfo * entryPoint)
  2752. {
  2753. return equivalentTypeCacheEntryPoints.PrependNode(&equivalentTypeCacheInfoAllocator, entryPoint);
  2754. }
  2755. void
  2756. ThreadContext::UnregisterEquivalentTypeCacheEntryPoint(Js::EntryPointInfo ** entryPoint)
  2757. {
  2758. equivalentTypeCacheEntryPoints.RemoveElement(&equivalentTypeCacheInfoAllocator, entryPoint);
  2759. }
  2760. void
  2761. ThreadContext::RegisterProtoInlineCache(Js::InlineCache * inlineCache, Js::PropertyId propertyId)
  2762. {
  2763. if (PHASE_TRACE1(Js::TraceInlineCacheInvalidationPhase))
  2764. {
  2765. Output::Print(_u("InlineCacheInvalidation: registering proto cache 0x%p for property %s(%u)\n"),
  2766. inlineCache, GetPropertyName(propertyId)->GetBuffer(), propertyId);
  2767. Output::Flush();
  2768. }
  2769. RegisterInlineCache(protoInlineCacheByPropId, inlineCache, propertyId);
  2770. }
  2771. void
  2772. ThreadContext::RegisterStoreFieldInlineCache(Js::InlineCache * inlineCache, Js::PropertyId propertyId)
  2773. {
  2774. if (PHASE_TRACE1(Js::TraceInlineCacheInvalidationPhase))
  2775. {
  2776. Output::Print(_u("InlineCacheInvalidation: registering store field cache 0x%p for property %s(%u)\n"),
  2777. inlineCache, GetPropertyName(propertyId)->GetBuffer(), propertyId);
  2778. Output::Flush();
  2779. }
  2780. RegisterInlineCache(storeFieldInlineCacheByPropId, inlineCache, propertyId);
  2781. }
  2782. void
  2783. ThreadContext::RegisterInlineCache(InlineCacheListMapByPropertyId& inlineCacheMap, Js::InlineCache * inlineCache, Js::PropertyId propertyId)
  2784. {
  2785. InlineCacheList* inlineCacheList;
  2786. if (!inlineCacheMap.TryGetValue(propertyId, &inlineCacheList))
  2787. {
  2788. inlineCacheList = Anew(&this->inlineCacheThreadInfoAllocator, InlineCacheList, &this->inlineCacheThreadInfoAllocator);
  2789. inlineCacheMap.AddNew(propertyId, inlineCacheList);
  2790. }
  2791. Js::InlineCache** inlineCacheRef = inlineCacheList->PrependNode();
  2792. Assert(inlineCacheRef != nullptr);
  2793. *inlineCacheRef = inlineCache;
  2794. inlineCache->invalidationListSlotPtr = inlineCacheRef;
  2795. this->registeredInlineCacheCount++;
  2796. }
  2797. void ThreadContext::NotifyInlineCacheBatchUnregistered(uint count)
  2798. {
  2799. this->unregisteredInlineCacheCount += count;
  2800. // Negative or 0 InlineCacheInvalidationListCompactionThreshold forces compaction all the time.
  2801. if (CONFIG_FLAG(InlineCacheInvalidationListCompactionThreshold) <= 0 ||
  2802. this->registeredInlineCacheCount / this->unregisteredInlineCacheCount < (uint)CONFIG_FLAG(InlineCacheInvalidationListCompactionThreshold))
  2803. {
  2804. CompactInlineCacheInvalidationLists();
  2805. }
  2806. }
  2807. void
  2808. ThreadContext::InvalidateProtoInlineCaches(Js::PropertyId propertyId)
  2809. {
  2810. InlineCacheList* inlineCacheList;
  2811. if (protoInlineCacheByPropId.TryGetValueAndRemove(propertyId, &inlineCacheList))
  2812. {
  2813. if (PHASE_TRACE1(Js::TraceInlineCacheInvalidationPhase))
  2814. {
  2815. Output::Print(_u("InlineCacheInvalidation: invalidating proto caches for property %s(%u)\n"),
  2816. GetPropertyName(propertyId)->GetBuffer(), propertyId);
  2817. Output::Flush();
  2818. }
  2819. InvalidateAndDeleteInlineCacheList(inlineCacheList);
  2820. }
  2821. }
  2822. void
  2823. ThreadContext::InvalidateStoreFieldInlineCaches(Js::PropertyId propertyId)
  2824. {
  2825. InlineCacheList* inlineCacheList;
  2826. if (storeFieldInlineCacheByPropId.TryGetValueAndRemove(propertyId, &inlineCacheList))
  2827. {
  2828. if (PHASE_TRACE1(Js::TraceInlineCacheInvalidationPhase))
  2829. {
  2830. Output::Print(_u("InlineCacheInvalidation: invalidating store field caches for property %s(%u)\n"),
  2831. GetPropertyName(propertyId)->GetBuffer(), propertyId);
  2832. Output::Flush();
  2833. }
  2834. InvalidateAndDeleteInlineCacheList(inlineCacheList);
  2835. }
  2836. }
  2837. void
  2838. ThreadContext::InvalidateAndDeleteInlineCacheList(InlineCacheList* inlineCacheList)
  2839. {
  2840. Assert(inlineCacheList != nullptr);
  2841. uint cacheCount = 0;
  2842. uint nullCacheCount = 0;
  2843. FOREACH_SLISTBASE_ENTRY(Js::InlineCache*, inlineCache, inlineCacheList)
  2844. {
  2845. cacheCount++;
  2846. if (inlineCache != nullptr)
  2847. {
  2848. if (PHASE_VERBOSE_TRACE1(Js::TraceInlineCacheInvalidationPhase))
  2849. {
  2850. Output::Print(_u("InlineCacheInvalidation: invalidating cache 0x%p\n"), inlineCache);
  2851. Output::Flush();
  2852. }
  2853. memset(inlineCache, 0, sizeof(Js::InlineCache));
  2854. }
  2855. else
  2856. {
  2857. nullCacheCount++;
  2858. }
  2859. }
  2860. NEXT_SLISTBASE_ENTRY;
  2861. Adelete(&this->inlineCacheThreadInfoAllocator, inlineCacheList);
  2862. this->registeredInlineCacheCount = this->registeredInlineCacheCount > cacheCount ? this->registeredInlineCacheCount - cacheCount : 0;
  2863. this->unregisteredInlineCacheCount = this->unregisteredInlineCacheCount > nullCacheCount ? this->unregisteredInlineCacheCount - nullCacheCount : 0;
  2864. }
  2865. void
  2866. ThreadContext::CompactInlineCacheInvalidationLists()
  2867. {
  2868. #if DBG
  2869. uint countOfNodesToCompact = this->unregisteredInlineCacheCount;
  2870. this->totalUnregisteredCacheCount = 0;
  2871. #endif
  2872. Assert(this->unregisteredInlineCacheCount > 0);
  2873. CompactProtoInlineCaches();
  2874. if (this->unregisteredInlineCacheCount > 0)
  2875. {
  2876. CompactStoreFieldInlineCaches();
  2877. }
  2878. Assert(countOfNodesToCompact == this->totalUnregisteredCacheCount);
  2879. }
  2880. void
  2881. ThreadContext::CompactProtoInlineCaches()
  2882. {
  2883. protoInlineCacheByPropId.MapUntil([this](Js::PropertyId propertyId, InlineCacheList* inlineCacheList)
  2884. {
  2885. CompactInlineCacheList(inlineCacheList);
  2886. return this->unregisteredInlineCacheCount == 0;
  2887. });
  2888. }
  2889. void
  2890. ThreadContext::CompactStoreFieldInlineCaches()
  2891. {
  2892. storeFieldInlineCacheByPropId.MapUntil([this](Js::PropertyId propertyId, InlineCacheList* inlineCacheList)
  2893. {
  2894. CompactInlineCacheList(inlineCacheList);
  2895. return this->unregisteredInlineCacheCount == 0;
  2896. });
  2897. }
  2898. void
  2899. ThreadContext::CompactInlineCacheList(InlineCacheList* inlineCacheList)
  2900. {
  2901. Assert(inlineCacheList != nullptr);
  2902. uint cacheCount = 0;
  2903. FOREACH_SLISTBASE_ENTRY_EDITING(Js::InlineCache*, inlineCache, inlineCacheList, iterator)
  2904. {
  2905. if (inlineCache == nullptr)
  2906. {
  2907. iterator.RemoveCurrent();
  2908. cacheCount++;
  2909. }
  2910. }
  2911. NEXT_SLISTBASE_ENTRY_EDITING;
  2912. #if DBG
  2913. this->totalUnregisteredCacheCount += cacheCount;
  2914. #endif
  2915. if (cacheCount > 0)
  2916. {
  2917. AssertMsg(this->unregisteredInlineCacheCount >= cacheCount, "Some codepaths didn't unregistered the inlineCaches which might leak memory.");
  2918. this->unregisteredInlineCacheCount = this->unregisteredInlineCacheCount > cacheCount ?
  2919. this->unregisteredInlineCacheCount - cacheCount : 0;
  2920. AssertMsg(this->registeredInlineCacheCount >= cacheCount, "Some codepaths didn't registered the inlineCaches which might leak memory.");
  2921. this->registeredInlineCacheCount = this->registeredInlineCacheCount > cacheCount ?
  2922. this->registeredInlineCacheCount - cacheCount : 0;
  2923. }
  2924. }
  2925. #if DBG
  2926. bool
  2927. ThreadContext::IsProtoInlineCacheRegistered(const Js::InlineCache* inlineCache, Js::PropertyId propertyId)
  2928. {
  2929. return IsInlineCacheRegistered(protoInlineCacheByPropId, inlineCache, propertyId);
  2930. }
  2931. bool
  2932. ThreadContext::IsStoreFieldInlineCacheRegistered(const Js::InlineCache* inlineCache, Js::PropertyId propertyId)
  2933. {
  2934. return IsInlineCacheRegistered(storeFieldInlineCacheByPropId, inlineCache, propertyId);
  2935. }
  2936. bool
  2937. ThreadContext::IsInlineCacheRegistered(InlineCacheListMapByPropertyId& inlineCacheMap, const Js::InlineCache* inlineCache, Js::PropertyId propertyId)
  2938. {
  2939. InlineCacheList* inlineCacheList;
  2940. if (inlineCacheMap.TryGetValue(propertyId, &inlineCacheList))
  2941. {
  2942. return IsInlineCacheInList(inlineCache, inlineCacheList);
  2943. }
  2944. else
  2945. {
  2946. return false;
  2947. }
  2948. }
  2949. bool
  2950. ThreadContext::IsInlineCacheInList(const Js::InlineCache* inlineCache, const InlineCacheList* inlineCacheList)
  2951. {
  2952. Assert(inlineCache != nullptr);
  2953. Assert(inlineCacheList != nullptr);
  2954. FOREACH_SLISTBASE_ENTRY(Js::InlineCache*, curInlineCache, inlineCacheList)
  2955. {
  2956. if (curInlineCache == inlineCache)
  2957. {
  2958. return true;
  2959. }
  2960. }
  2961. NEXT_SLISTBASE_ENTRY;
  2962. return false;
  2963. }
  2964. #endif
  2965. #if ENABLE_NATIVE_CODEGEN
  2966. ThreadContext::PropertyGuardEntry*
  2967. ThreadContext::EnsurePropertyGuardEntry(const Js::PropertyRecord* propertyRecord, bool& foundExistingEntry)
  2968. {
  2969. PropertyGuardDictionary &guards = this->recyclableData->propertyGuards;
  2970. PropertyGuardEntry* entry;
  2971. foundExistingEntry = guards.TryGetValue(propertyRecord, &entry);
  2972. if (!foundExistingEntry)
  2973. {
  2974. entry = RecyclerNew(GetRecycler(), PropertyGuardEntry, GetRecycler());
  2975. guards.UncheckedAdd(CreatePropertyRecordWeakRef(propertyRecord), entry);
  2976. }
  2977. return entry;
  2978. }
  2979. Js::PropertyGuard*
  2980. ThreadContext::RegisterSharedPropertyGuard(Js::PropertyId propertyId)
  2981. {
  2982. Assert(IsActivePropertyId(propertyId));
  2983. const Js::PropertyRecord * propertyRecord = GetPropertyName(propertyId);
  2984. bool foundExistingGuard;
  2985. PropertyGuardEntry* entry = EnsurePropertyGuardEntry(propertyRecord, foundExistingGuard);
  2986. if (entry->sharedGuard == nullptr)
  2987. {
  2988. entry->sharedGuard = Js::PropertyGuard::New(GetRecycler());
  2989. }
  2990. Js::PropertyGuard* guard = entry->sharedGuard;
  2991. PHASE_PRINT_VERBOSE_TRACE1(Js::FixedMethodsPhase, _u("FixedFields: registered shared guard: name: %s, address: 0x%p, value: 0x%p, value address: 0x%p, %s\n"),
  2992. propertyRecord->GetBuffer(), guard, guard->GetValue(), guard->GetAddressOfValue(), foundExistingGuard ? _u("existing") : _u("new"));
  2993. PHASE_PRINT_TESTTRACE1(Js::FixedMethodsPhase, _u("FixedFields: registered shared guard: name: %s, value: 0x%p, %s\n"),
  2994. propertyRecord->GetBuffer(), guard->GetValue(), foundExistingGuard ? _u("existing") : _u("new"));
  2995. return guard;
  2996. }
  2997. void
  2998. ThreadContext::RegisterLazyBailout(Js::PropertyId propertyId, Js::EntryPointInfo* entryPoint)
  2999. {
  3000. const Js::PropertyRecord * propertyRecord = GetPropertyName(propertyId);
  3001. bool foundExistingGuard;
  3002. PropertyGuardEntry* entry = EnsurePropertyGuardEntry(propertyRecord, foundExistingGuard);
  3003. if (!entry->entryPoints)
  3004. {
  3005. entry->entryPoints = RecyclerNew(recycler, PropertyGuardEntry::EntryPointDictionary, recycler, /*capacity*/ 3);
  3006. }
  3007. entry->entryPoints->UncheckedAdd(entryPoint, NULL);
  3008. }
  3009. void
  3010. ThreadContext::RegisterUniquePropertyGuard(Js::PropertyId propertyId, Js::PropertyGuard* guard)
  3011. {
  3012. Assert(IsActivePropertyId(propertyId));
  3013. Assert(guard != nullptr);
  3014. RecyclerWeakReference<Js::PropertyGuard>* guardWeakRef = this->recycler->CreateWeakReferenceHandle(guard);
  3015. RegisterUniquePropertyGuard(propertyId, guardWeakRef);
  3016. }
  3017. void
  3018. ThreadContext::RegisterUniquePropertyGuard(Js::PropertyId propertyId, RecyclerWeakReference<Js::PropertyGuard>* guardWeakRef)
  3019. {
  3020. Assert(IsActivePropertyId(propertyId));
  3021. Assert(guardWeakRef != nullptr);
  3022. Js::PropertyGuard* guard = guardWeakRef->Get();
  3023. Assert(guard != nullptr);
  3024. const Js::PropertyRecord * propertyRecord = GetPropertyName(propertyId);
  3025. bool foundExistingGuard;
  3026. PropertyGuardEntry* entry = EnsurePropertyGuardEntry(propertyRecord, foundExistingGuard);
  3027. entry->uniqueGuards.Item(guardWeakRef);
  3028. if (PHASE_TRACE1(Js::TracePropertyGuardsPhase) || PHASE_VERBOSE_TRACE1(Js::FixedMethodsPhase))
  3029. {
  3030. Output::Print(_u("FixedFields: registered unique guard: name: %s, address: 0x%p, value: 0x%p, value address: 0x%p, %s entry\n"),
  3031. propertyRecord->GetBuffer(), guard, guard->GetValue(), guard->GetAddressOfValue(), foundExistingGuard ? _u("existing") : _u("new"));
  3032. Output::Flush();
  3033. }
  3034. if (PHASE_TESTTRACE1(Js::TracePropertyGuardsPhase) || PHASE_VERBOSE_TESTTRACE1(Js::FixedMethodsPhase))
  3035. {
  3036. Output::Print(_u("FixedFields: registered unique guard: name: %s, value: 0x%p, %s entry\n"),
  3037. propertyRecord->GetBuffer(), guard->GetValue(), foundExistingGuard ? _u("existing") : _u("new"));
  3038. Output::Flush();
  3039. }
  3040. }
  3041. void
  3042. ThreadContext::RegisterConstructorCache(Js::PropertyId propertyId, Js::ConstructorCache* cache)
  3043. {
  3044. Assert(Js::ConstructorCache::GetOffsetOfGuardValue() == Js::PropertyGuard::GetOffsetOfValue());
  3045. Assert(Js::ConstructorCache::GetSizeOfGuardValue() == Js::PropertyGuard::GetSizeOfValue());
  3046. RegisterUniquePropertyGuard(propertyId, reinterpret_cast<Js::PropertyGuard*>(cache));
  3047. }
  3048. void
  3049. ThreadContext::InvalidatePropertyGuardEntry(const Js::PropertyRecord* propertyRecord, PropertyGuardEntry* entry, bool isAllPropertyGuardsInvalidation)
  3050. {
  3051. Assert(entry != nullptr);
  3052. if (entry->sharedGuard != nullptr)
  3053. {
  3054. Js::PropertyGuard* guard = entry->sharedGuard;
  3055. if (PHASE_TRACE1(Js::TracePropertyGuardsPhase) || PHASE_VERBOSE_TRACE1(Js::FixedMethodsPhase))
  3056. {
  3057. Output::Print(_u("FixedFields: invalidating guard: name: %s, address: 0x%p, value: 0x%p, value address: 0x%p\n"),
  3058. propertyRecord->GetBuffer(), guard, guard->GetValue(), guard->GetAddressOfValue());
  3059. Output::Flush();
  3060. }
  3061. if (PHASE_TESTTRACE1(Js::TracePropertyGuardsPhase) || PHASE_VERBOSE_TESTTRACE1(Js::FixedMethodsPhase))
  3062. {
  3063. Output::Print(_u("FixedFields: invalidating guard: name: %s, value: 0x%p\n"), propertyRecord->GetBuffer(), guard->GetValue());
  3064. Output::Flush();
  3065. }
  3066. guard->Invalidate();
  3067. }
  3068. uint count = 0;
  3069. entry->uniqueGuards.Map([&count, propertyRecord](RecyclerWeakReference<Js::PropertyGuard>* guardWeakRef)
  3070. {
  3071. Js::PropertyGuard* guard = guardWeakRef->Get();
  3072. if (guard != nullptr)
  3073. {
  3074. if (PHASE_TRACE1(Js::TracePropertyGuardsPhase) || PHASE_VERBOSE_TRACE1(Js::FixedMethodsPhase))
  3075. {
  3076. Output::Print(_u("FixedFields: invalidating guard: name: %s, address: 0x%p, value: 0x%p, value address: 0x%p\n"),
  3077. propertyRecord->GetBuffer(), guard, guard->GetValue(), guard->GetAddressOfValue());
  3078. Output::Flush();
  3079. }
  3080. if (PHASE_TESTTRACE1(Js::TracePropertyGuardsPhase) || PHASE_VERBOSE_TESTTRACE1(Js::FixedMethodsPhase))
  3081. {
  3082. Output::Print(_u("FixedFields: invalidating guard: name: %s, value: 0x%p\n"),
  3083. propertyRecord->GetBuffer(), guard->GetValue());
  3084. Output::Flush();
  3085. }
  3086. guard->Invalidate();
  3087. count++;
  3088. }
  3089. });
  3090. entry->uniqueGuards.Clear();
  3091. // Count no. of invalidations done so far. Exclude if this is all property guards invalidation in which case
  3092. // the unique Guards will be cleared anyway.
  3093. if (!isAllPropertyGuardsInvalidation)
  3094. {
  3095. this->recyclableData->constructorCacheInvalidationCount += count;
  3096. if (this->recyclableData->constructorCacheInvalidationCount > (uint)CONFIG_FLAG(ConstructorCacheInvalidationThreshold))
  3097. {
  3098. // TODO: In future, we should compact the uniqueGuards dictionary so this function can be called from PreCollectionCallback
  3099. // instead
  3100. this->ClearInvalidatedUniqueGuards();
  3101. this->recyclableData->constructorCacheInvalidationCount = 0;
  3102. }
  3103. }
  3104. if (entry->entryPoints && entry->entryPoints->Count() > 0)
  3105. {
  3106. Js::JavascriptStackWalker stackWalker(this->GetScriptContextList());
  3107. Js::JavascriptFunction* caller;
  3108. while (stackWalker.GetCaller(&caller, /*includeInlineFrames*/ false))
  3109. {
  3110. // If the current frame is already from a bailout - we do not need to do on stack invalidation
  3111. if (caller != nullptr && Js::ScriptFunction::Is(caller) && !stackWalker.GetCurrentFrameFromBailout())
  3112. {
  3113. BYTE dummy;
  3114. Js::FunctionEntryPointInfo* functionEntryPoint = caller->GetFunctionBody()->GetDefaultFunctionEntryPointInfo();
  3115. if (functionEntryPoint->IsInNativeAddressRange((DWORD_PTR)stackWalker.GetInstructionPointer()))
  3116. {
  3117. if (entry->entryPoints->TryGetValue(functionEntryPoint, &dummy))
  3118. {
  3119. functionEntryPoint->DoLazyBailout(stackWalker.GetCurrentAddressOfInstructionPointer(),
  3120. caller->GetFunctionBody(), propertyRecord);
  3121. }
  3122. }
  3123. }
  3124. }
  3125. entry->entryPoints->Map([=](Js::EntryPointInfo* info, BYTE& dummy, const RecyclerWeakReference<Js::EntryPointInfo>* infoWeakRef)
  3126. {
  3127. OUTPUT_TRACE2(Js::LazyBailoutPhase, info->GetFunctionBody(), _u("Lazy bailout - Invalidation due to property: %s \n"), propertyRecord->GetBuffer());
  3128. info->Invalidate(true);
  3129. });
  3130. entry->entryPoints->Clear();
  3131. }
  3132. }
  3133. void
  3134. ThreadContext::InvalidatePropertyGuards(Js::PropertyId propertyId)
  3135. {
  3136. const Js::PropertyRecord* propertyRecord = GetPropertyName(propertyId);
  3137. PropertyGuardDictionary &guards = this->recyclableData->propertyGuards;
  3138. PropertyGuardEntry* entry;
  3139. if (guards.TryGetValueAndRemove(propertyRecord, &entry))
  3140. {
  3141. InvalidatePropertyGuardEntry(propertyRecord, entry, false);
  3142. }
  3143. }
  3144. void
  3145. ThreadContext::InvalidateAllPropertyGuards()
  3146. {
  3147. PropertyGuardDictionary &guards = this->recyclableData->propertyGuards;
  3148. if (guards.Count() > 0)
  3149. {
  3150. guards.Map([this](Js::PropertyRecord const * propertyRecord, PropertyGuardEntry* entry, const RecyclerWeakReference<const Js::PropertyRecord>* weakRef)
  3151. {
  3152. InvalidatePropertyGuardEntry(propertyRecord, entry, true);
  3153. });
  3154. guards.Clear();
  3155. }
  3156. }
  3157. #endif
  3158. void
  3159. ThreadContext::InvalidateAllProtoInlineCaches()
  3160. {
  3161. protoInlineCacheByPropId.Map([this](Js::PropertyId propertyId, InlineCacheList* inlineCacheList)
  3162. {
  3163. InvalidateAndDeleteInlineCacheList(inlineCacheList);
  3164. });
  3165. protoInlineCacheByPropId.Reset();
  3166. }
  3167. #if DBG
  3168. // Verifies if object is registered in any proto InlineCache
  3169. bool
  3170. ThreadContext::IsObjectRegisteredInProtoInlineCaches(Js::DynamicObject * object)
  3171. {
  3172. return protoInlineCacheByPropId.MapUntil([object](Js::PropertyId propertyId, InlineCacheList* inlineCacheList)
  3173. {
  3174. FOREACH_SLISTBASE_ENTRY(Js::InlineCache*, inlineCache, inlineCacheList)
  3175. {
  3176. if (inlineCache != nullptr && !inlineCache->IsEmpty())
  3177. {
  3178. // Verify this object is not present in prototype chain of inlineCache's type
  3179. bool isObjectPresentOnPrototypeChain =
  3180. Js::JavascriptOperators::MapObjectAndPrototypesUntil<true>(inlineCache->GetType()->GetPrototype(), [=](Js::RecyclableObject* prototype)
  3181. {
  3182. return prototype == object;
  3183. });
  3184. if (isObjectPresentOnPrototypeChain) {
  3185. return true;
  3186. }
  3187. }
  3188. }
  3189. NEXT_SLISTBASE_ENTRY;
  3190. return false;
  3191. });
  3192. }
  3193. // Verifies if object is registered in any storeField InlineCache
  3194. bool
  3195. ThreadContext::IsObjectRegisteredInStoreFieldInlineCaches(Js::DynamicObject * object)
  3196. {
  3197. return storeFieldInlineCacheByPropId.MapUntil([object](Js::PropertyId propertyId, InlineCacheList* inlineCacheList)
  3198. {
  3199. FOREACH_SLISTBASE_ENTRY(Js::InlineCache*, inlineCache, inlineCacheList)
  3200. {
  3201. if (inlineCache != nullptr && !inlineCache->IsEmpty())
  3202. {
  3203. // Verify this object is not present in prototype chain of inlineCache's type
  3204. bool isObjectPresentOnPrototypeChain =
  3205. Js::JavascriptOperators::MapObjectAndPrototypesUntil<true>(inlineCache->GetType()->GetPrototype(), [=](Js::RecyclableObject* prototype)
  3206. {
  3207. return prototype == object;
  3208. });
  3209. if (isObjectPresentOnPrototypeChain) {
  3210. return true;
  3211. }
  3212. }
  3213. }
  3214. NEXT_SLISTBASE_ENTRY;
  3215. return false;
  3216. });
  3217. }
  3218. #endif
  3219. bool
  3220. ThreadContext::AreAllProtoInlineCachesInvalidated()
  3221. {
  3222. return protoInlineCacheByPropId.Count() == 0;
  3223. }
  3224. void
  3225. ThreadContext::InvalidateAllStoreFieldInlineCaches()
  3226. {
  3227. storeFieldInlineCacheByPropId.Map([this](Js::PropertyId propertyId, InlineCacheList* inlineCacheList)
  3228. {
  3229. InvalidateAndDeleteInlineCacheList(inlineCacheList);
  3230. });
  3231. storeFieldInlineCacheByPropId.Reset();
  3232. }
  3233. bool
  3234. ThreadContext::AreAllStoreFieldInlineCachesInvalidated()
  3235. {
  3236. return storeFieldInlineCacheByPropId.Count() == 0;
  3237. }
  3238. #if DBG
  3239. bool
  3240. ThreadContext::IsIsInstInlineCacheRegistered(Js::IsInstInlineCache * inlineCache, Js::Var function)
  3241. {
  3242. Assert(inlineCache != nullptr);
  3243. Assert(function != nullptr);
  3244. Js::IsInstInlineCache* firstInlineCache;
  3245. if (this->isInstInlineCacheByFunction.TryGetValue(function, &firstInlineCache))
  3246. {
  3247. return IsIsInstInlineCacheInList(inlineCache, firstInlineCache);
  3248. }
  3249. else
  3250. {
  3251. return false;
  3252. }
  3253. }
  3254. #endif
  3255. void
  3256. ThreadContext::RegisterIsInstInlineCache(Js::IsInstInlineCache * inlineCache, Js::Var function)
  3257. {
  3258. Assert(function != nullptr);
  3259. Assert(inlineCache != nullptr);
  3260. // We should never cross-register or re-register a cache that is already on some invalidation list (for its function or some other function).
  3261. // Every cache must be first cleared and unregistered before being registered again.
  3262. AssertMsg(inlineCache->function == nullptr, "We should only register instance-of caches that have not yet been populated.");
  3263. Js::IsInstInlineCache** inlineCacheRef = nullptr;
  3264. if (this->isInstInlineCacheByFunction.TryGetReference(function, &inlineCacheRef))
  3265. {
  3266. AssertMsg(!IsIsInstInlineCacheInList(inlineCache, *inlineCacheRef), "Why are we registering a cache that is already registered?");
  3267. inlineCache->next = *inlineCacheRef;
  3268. *inlineCacheRef = inlineCache;
  3269. }
  3270. else
  3271. {
  3272. inlineCache->next = nullptr;
  3273. this->isInstInlineCacheByFunction.Add(function, inlineCache);
  3274. }
  3275. }
  3276. void
  3277. ThreadContext::UnregisterIsInstInlineCache(Js::IsInstInlineCache * inlineCache, Js::Var function)
  3278. {
  3279. Assert(inlineCache != nullptr);
  3280. Js::IsInstInlineCache** inlineCacheRef = nullptr;
  3281. if (this->isInstInlineCacheByFunction.TryGetReference(function, &inlineCacheRef))
  3282. {
  3283. Assert(*inlineCacheRef != nullptr);
  3284. if (inlineCache == *inlineCacheRef)
  3285. {
  3286. *inlineCacheRef = (*inlineCacheRef)->next;
  3287. if (*inlineCacheRef == nullptr)
  3288. {
  3289. this->isInstInlineCacheByFunction.Remove(function);
  3290. }
  3291. }
  3292. else
  3293. {
  3294. Js::IsInstInlineCache * prevInlineCache;
  3295. Js::IsInstInlineCache * curInlineCache;
  3296. for (prevInlineCache = *inlineCacheRef, curInlineCache = (*inlineCacheRef)->next; curInlineCache != nullptr;
  3297. prevInlineCache = curInlineCache, curInlineCache = curInlineCache->next)
  3298. {
  3299. if (curInlineCache == inlineCache)
  3300. {
  3301. prevInlineCache->next = curInlineCache->next;
  3302. return;
  3303. }
  3304. }
  3305. AssertMsg(false, "Why are we unregistering a cache that is not registered?");
  3306. }
  3307. }
  3308. }
  3309. void
  3310. ThreadContext::InvalidateIsInstInlineCacheList(Js::IsInstInlineCache* inlineCacheList)
  3311. {
  3312. Assert(inlineCacheList != nullptr);
  3313. Js::IsInstInlineCache* curInlineCache;
  3314. Js::IsInstInlineCache* nextInlineCache;
  3315. for (curInlineCache = inlineCacheList; curInlineCache != nullptr; curInlineCache = nextInlineCache)
  3316. {
  3317. if (PHASE_VERBOSE_TRACE1(Js::TraceInlineCacheInvalidationPhase))
  3318. {
  3319. Output::Print(_u("InlineCacheInvalidation: invalidating instanceof cache 0x%p\n"), curInlineCache);
  3320. Output::Flush();
  3321. }
  3322. // Stash away the next cache before we zero out the current one (including its next pointer).
  3323. nextInlineCache = curInlineCache->next;
  3324. // Clear the current cache to invalidate it.
  3325. memset(curInlineCache, 0, sizeof(Js::IsInstInlineCache));
  3326. }
  3327. }
  3328. void
  3329. ThreadContext::InvalidateIsInstInlineCachesForFunction(Js::Var function)
  3330. {
  3331. Js::IsInstInlineCache* inlineCacheList;
  3332. if (this->isInstInlineCacheByFunction.TryGetValueAndRemove(function, &inlineCacheList))
  3333. {
  3334. InvalidateIsInstInlineCacheList(inlineCacheList);
  3335. }
  3336. }
  3337. void
  3338. ThreadContext::InvalidateAllIsInstInlineCaches()
  3339. {
  3340. isInstInlineCacheByFunction.Map([this](const Js::Var function, Js::IsInstInlineCache* inlineCacheList)
  3341. {
  3342. InvalidateIsInstInlineCacheList(inlineCacheList);
  3343. });
  3344. isInstInlineCacheByFunction.Clear();
  3345. }
  3346. bool
  3347. ThreadContext::AreAllIsInstInlineCachesInvalidated() const
  3348. {
  3349. return isInstInlineCacheByFunction.Count() == 0;
  3350. }
  3351. #if DBG
  3352. bool
  3353. ThreadContext::IsIsInstInlineCacheInList(const Js::IsInstInlineCache* inlineCache, const Js::IsInstInlineCache* inlineCacheList)
  3354. {
  3355. Assert(inlineCache != nullptr);
  3356. Assert(inlineCacheList != nullptr);
  3357. for (const Js::IsInstInlineCache* curInlineCache = inlineCacheList; curInlineCache != nullptr; curInlineCache = curInlineCache->next)
  3358. {
  3359. if (curInlineCache == inlineCache)
  3360. {
  3361. return true;
  3362. }
  3363. }
  3364. return false;
  3365. }
  3366. #endif
  3367. void ThreadContext::RegisterTypeWithProtoPropertyCache(const Js::PropertyId propertyId, Js::Type *const type)
  3368. {
  3369. Assert(propertyId != Js::Constants::NoProperty);
  3370. Assert(IsActivePropertyId(propertyId));
  3371. Assert(type);
  3372. PropertyIdToTypeHashSetDictionary &typesWithProtoPropertyCache = recyclableData->typesWithProtoPropertyCache;
  3373. TypeHashSet *typeHashSet;
  3374. if(!typesWithProtoPropertyCache.TryGetValue(propertyId, &typeHashSet))
  3375. {
  3376. typeHashSet = RecyclerNew(recycler, TypeHashSet, recycler);
  3377. typesWithProtoPropertyCache.Add(propertyId, typeHashSet);
  3378. }
  3379. typeHashSet->Item(type, false);
  3380. }
  3381. void ThreadContext::InvalidateProtoTypePropertyCaches(const Js::PropertyId propertyId)
  3382. {
  3383. Assert(propertyId != Js::Constants::NoProperty);
  3384. Assert(IsActivePropertyId(propertyId));
  3385. InternalInvalidateProtoTypePropertyCaches(propertyId);
  3386. }
  3387. void ThreadContext::InternalInvalidateProtoTypePropertyCaches(const Js::PropertyId propertyId)
  3388. {
  3389. // Get the hash set of registered types associated with the property ID, invalidate each type in the hash set, and
  3390. // remove the property ID and its hash set from the map
  3391. PropertyIdToTypeHashSetDictionary &typesWithProtoPropertyCache = recyclableData->typesWithProtoPropertyCache;
  3392. TypeHashSet *typeHashSet;
  3393. if(typesWithProtoPropertyCache.Count() != 0 && typesWithProtoPropertyCache.TryGetValueAndRemove(propertyId, &typeHashSet))
  3394. {
  3395. DoInvalidateProtoTypePropertyCaches(propertyId, typeHashSet);
  3396. }
  3397. }
  3398. void ThreadContext::InvalidateAllProtoTypePropertyCaches()
  3399. {
  3400. PropertyIdToTypeHashSetDictionary &typesWithProtoPropertyCache = recyclableData->typesWithProtoPropertyCache;
  3401. if (typesWithProtoPropertyCache.Count() > 0)
  3402. {
  3403. typesWithProtoPropertyCache.Map([this](Js::PropertyId propertyId, TypeHashSet * typeHashSet)
  3404. {
  3405. DoInvalidateProtoTypePropertyCaches(propertyId, typeHashSet);
  3406. });
  3407. typesWithProtoPropertyCache.Clear();
  3408. }
  3409. }
  3410. void ThreadContext::DoInvalidateProtoTypePropertyCaches(const Js::PropertyId propertyId, TypeHashSet *const typeHashSet)
  3411. {
  3412. Assert(propertyId != Js::Constants::NoProperty);
  3413. Assert(typeHashSet);
  3414. typeHashSet->Map(
  3415. [propertyId](Js::Type *const type, const bool unused, const RecyclerWeakReference<Js::Type>*)
  3416. {
  3417. type->GetPropertyCache()->ClearIfPropertyIsOnAPrototype(propertyId);
  3418. });
  3419. }
  3420. Js::ScriptContext **
  3421. ThreadContext::RegisterPrototypeChainEnsuredToHaveOnlyWritableDataPropertiesScriptContext(Js::ScriptContext * scriptContext)
  3422. {
  3423. return prototypeChainEnsuredToHaveOnlyWritableDataPropertiesScriptContext.PrependNode(&prototypeChainEnsuredToHaveOnlyWritableDataPropertiesAllocator, scriptContext);
  3424. }
  3425. void
  3426. ThreadContext::UnregisterPrototypeChainEnsuredToHaveOnlyWritableDataPropertiesScriptContext(Js::ScriptContext ** scriptContext)
  3427. {
  3428. prototypeChainEnsuredToHaveOnlyWritableDataPropertiesScriptContext.RemoveElement(&prototypeChainEnsuredToHaveOnlyWritableDataPropertiesAllocator, scriptContext);
  3429. }
  3430. void
  3431. ThreadContext::ClearPrototypeChainEnsuredToHaveOnlyWritableDataPropertiesCaches()
  3432. {
  3433. bool hasItem = false;
  3434. FOREACH_DLISTBASE_ENTRY(Js::ScriptContext *, scriptContext, &prototypeChainEnsuredToHaveOnlyWritableDataPropertiesScriptContext)
  3435. {
  3436. scriptContext->ClearPrototypeChainEnsuredToHaveOnlyWritableDataPropertiesCaches();
  3437. hasItem = true;
  3438. }
  3439. NEXT_DLISTBASE_ENTRY;
  3440. if (!hasItem)
  3441. {
  3442. return;
  3443. }
  3444. prototypeChainEnsuredToHaveOnlyWritableDataPropertiesScriptContext.Reset();
  3445. prototypeChainEnsuredToHaveOnlyWritableDataPropertiesAllocator.Reset();
  3446. }
  3447. BOOL ThreadContext::HasPreviousHostScriptContext()
  3448. {
  3449. return hostScriptContextStack->Count() != 0;
  3450. }
  3451. HostScriptContext* ThreadContext::GetPreviousHostScriptContext()
  3452. {
  3453. return hostScriptContextStack->Peek();
  3454. }
  3455. void ThreadContext::PushHostScriptContext(HostScriptContext* topProvider)
  3456. {
  3457. // script engine can be created coming from GetDispID, so push/pop can be
  3458. // happening after the first round of enterscript as well. we might need to
  3459. // revisit the whole callRootLevel but probably not now.
  3460. // Assert(HasPreviousHostScriptContext() || callRootLevel == 0);
  3461. hostScriptContextStack->Push(topProvider);
  3462. }
  3463. HostScriptContext* ThreadContext::PopHostScriptContext()
  3464. {
  3465. return hostScriptContextStack->Pop();
  3466. // script engine can be created coming from GetDispID, so push/pop can be
  3467. // happening after the first round of enterscript as well. we might need to
  3468. // revisit the whole callRootLevel but probably not now.
  3469. // Assert(HasPreviousHostScriptContext() || callRootLevel == 0);
  3470. }
  3471. #if DBG || defined(PROFILE_EXEC)
  3472. bool
  3473. ThreadContext::AsyncHostOperationStart(void * suspendRecord)
  3474. {
  3475. bool wasInAsync = false;
  3476. Assert(!this->IsScriptActive());
  3477. Js::ScriptEntryExitRecord * lastRecord = this->entryExitRecord;
  3478. if (lastRecord != NULL)
  3479. {
  3480. if (!lastRecord->leaveForHost)
  3481. {
  3482. #if DBG
  3483. wasInAsync = !!lastRecord->leaveForAsyncHostOperation;
  3484. lastRecord->leaveForAsyncHostOperation = true;
  3485. #endif
  3486. #ifdef PROFILE_EXEC
  3487. lastRecord->scriptContext->ProfileSuspend(Js::RunPhase, (Js::Profiler::SuspendRecord *)suspendRecord);
  3488. #endif
  3489. }
  3490. else
  3491. {
  3492. Assert(!lastRecord->leaveForAsyncHostOperation);
  3493. }
  3494. }
  3495. return wasInAsync;
  3496. }
  3497. void
  3498. ThreadContext::AsyncHostOperationEnd(bool wasInAsync, void * suspendRecord)
  3499. {
  3500. Assert(!this->IsScriptActive());
  3501. Js::ScriptEntryExitRecord * lastRecord = this->entryExitRecord;
  3502. if (lastRecord != NULL)
  3503. {
  3504. if (!lastRecord->leaveForHost)
  3505. {
  3506. Assert(lastRecord->leaveForAsyncHostOperation);
  3507. #if DBG
  3508. lastRecord->leaveForAsyncHostOperation = wasInAsync;
  3509. #endif
  3510. #ifdef PROFILE_EXEC
  3511. lastRecord->scriptContext->ProfileResume((Js::Profiler::SuspendRecord *)suspendRecord);
  3512. #endif
  3513. }
  3514. else
  3515. {
  3516. Assert(!lastRecord->leaveForAsyncHostOperation);
  3517. Assert(!wasInAsync);
  3518. }
  3519. }
  3520. }
  3521. #endif
  3522. #ifdef RECYCLER_DUMP_OBJECT_GRAPH
  3523. void DumpRecyclerObjectGraph()
  3524. {
  3525. ThreadContext * threadContext = ThreadContext::GetContextForCurrentThread();
  3526. if (threadContext == nullptr)
  3527. {
  3528. Output::Print(_u("No thread context"));
  3529. }
  3530. threadContext->GetRecycler()->DumpObjectGraph();
  3531. }
  3532. #endif
  3533. #if ENABLE_NATIVE_CODEGEN
  3534. bool ThreadContext::IsNativeAddressHelper(void * pCodeAddr, Js::ScriptContext* currentScriptContext)
  3535. {
  3536. bool isNativeAddr = false;
  3537. if (currentScriptContext && currentScriptContext->GetJitFuncRangeCache() != nullptr)
  3538. {
  3539. isNativeAddr = currentScriptContext->GetJitFuncRangeCache()->IsNativeAddr(pCodeAddr);
  3540. }
  3541. for (Js::ScriptContext *scriptContext = scriptContextList; scriptContext && !isNativeAddr; scriptContext = scriptContext->next)
  3542. {
  3543. if (scriptContext == currentScriptContext || scriptContext->GetJitFuncRangeCache() == nullptr)
  3544. {
  3545. continue;
  3546. }
  3547. isNativeAddr = scriptContext->GetJitFuncRangeCache()->IsNativeAddr(pCodeAddr);
  3548. }
  3549. return isNativeAddr;
  3550. }
  3551. BOOL ThreadContext::IsNativeAddress(void * pCodeAddr, Js::ScriptContext* currentScriptContext)
  3552. {
  3553. #if ENABLE_OOP_NATIVE_CODEGEN
  3554. if (JITManager::GetJITManager()->IsOOPJITEnabled())
  3555. {
  3556. if (PreReservedVirtualAllocWrapper::IsInRange((void*)m_prereservedRegionAddr, pCodeAddr))
  3557. {
  3558. return true;
  3559. }
  3560. if (IsAllJITCodeInPreReservedRegion())
  3561. {
  3562. return false;
  3563. }
  3564. if (AutoSystemInfo::IsJscriptModulePointer(pCodeAddr))
  3565. {
  3566. return false;
  3567. }
  3568. #if DBG
  3569. boolean result;
  3570. HRESULT hr = JITManager::GetJITManager()->IsNativeAddr(this->m_remoteThreadContextInfo, (intptr_t)pCodeAddr, &result);
  3571. JITManager::HandleServerCallResult(hr, RemoteCallType::HeapQuery);
  3572. #endif
  3573. bool isNativeAddr = IsNativeAddressHelper(pCodeAddr, currentScriptContext);
  3574. #if DBG
  3575. Assert(result == (isNativeAddr? TRUE:FALSE));
  3576. #endif
  3577. return isNativeAddr;
  3578. }
  3579. else
  3580. #endif
  3581. {
  3582. PreReservedVirtualAllocWrapper *preReservedVirtualAllocWrapper = this->GetPreReservedVirtualAllocator();
  3583. if (preReservedVirtualAllocWrapper->IsInRange(pCodeAddr))
  3584. {
  3585. return TRUE;
  3586. }
  3587. if (!this->IsAllJITCodeInPreReservedRegion())
  3588. {
  3589. #if DBG
  3590. AutoCriticalSection autoLock(&this->codePageAllocators.cs);
  3591. #endif
  3592. bool isNativeAddr = IsNativeAddressHelper(pCodeAddr, currentScriptContext);
  3593. #if DBG
  3594. Assert(this->codePageAllocators.IsInNonPreReservedPageAllocator(pCodeAddr) == isNativeAddr);
  3595. #endif
  3596. return isNativeAddr;
  3597. }
  3598. return FALSE;
  3599. }
  3600. }
  3601. #endif
  3602. #if ENABLE_PROFILE_INFO
  3603. void ThreadContext::EnsureSourceProfileManagersByUrlMap()
  3604. {
  3605. if(this->recyclableData->sourceProfileManagersByUrl == nullptr)
  3606. {
  3607. this->EnsureRecycler();
  3608. this->recyclableData->sourceProfileManagersByUrl = RecyclerNew(GetRecycler(), SourceProfileManagersByUrlMap, GetRecycler());
  3609. }
  3610. }
  3611. //
  3612. // Returns the cache profile manager for the URL and hash combination for a particular dynamic script. There is a ref count added for every script context
  3613. // that references the shared profile manager info.
  3614. //
  3615. Js::SourceDynamicProfileManager* ThreadContext::GetSourceDynamicProfileManager(_In_z_ const WCHAR* url, _In_ uint hash, _Inout_ bool* addRef)
  3616. {
  3617. EnsureSourceProfileManagersByUrlMap();
  3618. Js::SourceDynamicProfileManager* profileManager = nullptr;
  3619. SourceDynamicProfileManagerCache* managerCache;
  3620. bool newCache = false;
  3621. if(!this->recyclableData->sourceProfileManagersByUrl->TryGetValue(url, &managerCache))
  3622. {
  3623. if(this->recyclableData->sourceProfileManagersByUrl->Count() >= INMEMORY_CACHE_MAX_URL)
  3624. {
  3625. return nullptr;
  3626. }
  3627. managerCache = RecyclerNewZ(this->GetRecycler(), SourceDynamicProfileManagerCache);
  3628. newCache = true;
  3629. }
  3630. bool createProfileManager = false;
  3631. if(!managerCache->sourceProfileManagerMap)
  3632. {
  3633. managerCache->sourceProfileManagerMap = RecyclerNew(this->GetRecycler(), SourceDynamicProfileManagerMap, this->GetRecycler());
  3634. createProfileManager = true;
  3635. }
  3636. else
  3637. {
  3638. createProfileManager = !managerCache->sourceProfileManagerMap->TryGetValue(hash, &profileManager);
  3639. }
  3640. if(createProfileManager)
  3641. {
  3642. if(managerCache->sourceProfileManagerMap->Count() < INMEMORY_CACHE_MAX_PROFILE_MANAGER)
  3643. {
  3644. profileManager = RecyclerNewZ(this->GetRecycler(), Js::SourceDynamicProfileManager, this->GetRecycler());
  3645. managerCache->sourceProfileManagerMap->Add(hash, profileManager);
  3646. }
  3647. }
  3648. else
  3649. {
  3650. profileManager->Reuse();
  3651. }
  3652. if(!*addRef)
  3653. {
  3654. managerCache->AddRef();
  3655. *addRef = true;
  3656. OUTPUT_VERBOSE_TRACE(Js::DynamicProfilePhase, _u("Addref dynamic source profile manger - Url: %s\n"), url);
  3657. }
  3658. if (newCache)
  3659. {
  3660. // Let's make a copy of the URL because there is no guarantee this URL will remain alive in the future.
  3661. size_t lengthInChars = wcslen(url) + 1;
  3662. WCHAR* urlCopy = RecyclerNewArrayLeaf(GetRecycler(), WCHAR, lengthInChars);
  3663. js_memcpy_s(urlCopy, lengthInChars * sizeof(WCHAR), url, lengthInChars * sizeof(WCHAR));
  3664. this->recyclableData->sourceProfileManagersByUrl->Add(urlCopy, managerCache);
  3665. }
  3666. return profileManager;
  3667. }
  3668. //
  3669. // Decrement the ref count for this URL and cleanup the corresponding record if there are no other references to it.
  3670. //
  3671. uint ThreadContext::ReleaseSourceDynamicProfileManagers(const WCHAR* url)
  3672. {
  3673. // If we've already freed the recyclable data, we're shutting down the thread context so skip clean up
  3674. if (this->recyclableData == nullptr) return 0;
  3675. SourceDynamicProfileManagerCache* managerCache = this->recyclableData->sourceProfileManagersByUrl->Lookup(url, nullptr);
  3676. uint refCount = 0;
  3677. if(managerCache) // manager cache may be null we exceeded -INMEMORY_CACHE_MAX_URL
  3678. {
  3679. refCount = managerCache->Release();
  3680. OUTPUT_VERBOSE_TRACE(Js::DynamicProfilePhase, _u("Release dynamic source profile manger %d Url: %s\n"), refCount, url);
  3681. Output::Flush();
  3682. if(refCount == 0)
  3683. {
  3684. this->recyclableData->sourceProfileManagersByUrl->Remove(url);
  3685. }
  3686. }
  3687. return refCount;
  3688. }
  3689. #endif
  3690. void ThreadContext::EnsureSymbolRegistrationMap()
  3691. {
  3692. if (this->recyclableData->symbolRegistrationMap == nullptr)
  3693. {
  3694. this->EnsureRecycler();
  3695. this->recyclableData->symbolRegistrationMap = RecyclerNew(GetRecycler(), SymbolRegistrationMap, GetRecycler());
  3696. }
  3697. }
  3698. const Js::PropertyRecord* ThreadContext::GetSymbolFromRegistrationMap(const char16* stringKey)
  3699. {
  3700. this->EnsureSymbolRegistrationMap();
  3701. return this->recyclableData->symbolRegistrationMap->Lookup(stringKey, nullptr);
  3702. }
  3703. const Js::PropertyRecord* ThreadContext::AddSymbolToRegistrationMap(const char16* stringKey, charcount_t stringLength)
  3704. {
  3705. this->EnsureSymbolRegistrationMap();
  3706. const Js::PropertyRecord* propertyRecord = this->UncheckedAddPropertyId(stringKey, stringLength, /*bind*/false, /*isSymbol*/true);
  3707. Assert(propertyRecord);
  3708. // The key is the PropertyRecord's buffer (the PropertyRecord itself) which is being pinned as long as it's in this map.
  3709. // If that's ever not the case, we'll need to duplicate the key here and put that in the map instead.
  3710. this->recyclableData->symbolRegistrationMap->Add(propertyRecord->GetBuffer(), propertyRecord);
  3711. return propertyRecord;
  3712. }
  3713. #if ENABLE_TTD
  3714. JsUtil::BaseDictionary<const char16*, const Js::PropertyRecord*, Recycler, PowerOf2SizePolicy>* ThreadContext::GetSymbolRegistrationMap_TTD()
  3715. {
  3716. //This adds a little memory but makes simplifies other logic -- maybe revise later
  3717. this->EnsureSymbolRegistrationMap();
  3718. return this->recyclableData->symbolRegistrationMap;
  3719. }
  3720. #endif
  3721. void ThreadContext::ClearImplicitCallFlags()
  3722. {
  3723. SetImplicitCallFlags(Js::ImplicitCall_None);
  3724. }
  3725. void ThreadContext::ClearImplicitCallFlags(Js::ImplicitCallFlags flags)
  3726. {
  3727. Assert((flags & Js::ImplicitCall_None) == 0);
  3728. SetImplicitCallFlags((Js::ImplicitCallFlags)(implicitCallFlags & ~flags));
  3729. }
  3730. void ThreadContext::CheckAndResetImplicitCallAccessorFlag()
  3731. {
  3732. Js::ImplicitCallFlags accessorCallFlag = (Js::ImplicitCallFlags)(Js::ImplicitCall_Accessor & ~Js::ImplicitCall_None);
  3733. if ((GetImplicitCallFlags() & accessorCallFlag) != 0)
  3734. {
  3735. ClearImplicitCallFlags(accessorCallFlag);
  3736. AddImplicitCallFlags(Js::ImplicitCall_NonProfiledAccessor);
  3737. }
  3738. }
  3739. bool ThreadContext::HasNoSideEffect(Js::RecyclableObject * function) const
  3740. {
  3741. Js::FunctionInfo::Attributes attributes = Js::FunctionInfo::GetAttributes(function);
  3742. return this->HasNoSideEffect(function, attributes);
  3743. }
  3744. bool ThreadContext::HasNoSideEffect(Js::RecyclableObject * function, Js::FunctionInfo::Attributes attributes) const
  3745. {
  3746. if (((attributes & Js::FunctionInfo::CanBeHoisted) != 0)
  3747. || ((attributes & Js::FunctionInfo::HasNoSideEffect) != 0 && !IsDisableImplicitException()))
  3748. {
  3749. Assert((attributes & Js::FunctionInfo::HasNoSideEffect) != 0);
  3750. return true;
  3751. }
  3752. return false;
  3753. }
  3754. bool
  3755. ThreadContext::RecordImplicitException()
  3756. {
  3757. // Record the exception in the implicit call flag
  3758. AddImplicitCallFlags(Js::ImplicitCall_Exception);
  3759. if (IsDisableImplicitException())
  3760. {
  3761. // Indicate that we shouldn't throw if ImplicitExceptions have been disabled
  3762. return false;
  3763. }
  3764. // Disabling implicit exception when disabling implicit calls can result in valid exceptions not being thrown.
  3765. // Instead we tell not to throw only if an implicit call happened and they are disabled. This is to cover the case
  3766. // of an exception being thrown because an implicit call not executed left the execution in a bad state.
  3767. // Since there is an implicit call, we expect to bailout and handle this operation in the interpreter instead.
  3768. bool hasImplicitCallHappened = IsDisableImplicitCall() && (GetImplicitCallFlags() & ~Js::ImplicitCall_Exception);
  3769. return !hasImplicitCallHappened;
  3770. }
  3771. void ThreadContext::SetThreadServiceWrapper(ThreadServiceWrapper* inThreadServiceWrapper)
  3772. {
  3773. AssertMsg(threadServiceWrapper == NULL || inThreadServiceWrapper == NULL, "double set ThreadServiceWrapper");
  3774. threadServiceWrapper = inThreadServiceWrapper;
  3775. }
  3776. ThreadServiceWrapper* ThreadContext::GetThreadServiceWrapper()
  3777. {
  3778. return threadServiceWrapper;
  3779. }
  3780. uint ThreadContext::GetRandomNumber()
  3781. {
  3782. #ifdef ENABLE_CUSTOM_ENTROPY
  3783. return (uint)GetEntropy().GetRand();
  3784. #else
  3785. uint randomNumber = 0;
  3786. errno_t e = rand_s(&randomNumber);
  3787. Assert(e == 0);
  3788. return randomNumber;
  3789. #endif
  3790. }
  3791. #if defined(ENABLE_JS_ETW) && defined(NTBUILD)
  3792. void ThreadContext::EtwLogPropertyIdList()
  3793. {
  3794. propertyMap->Map([&](const Js::PropertyRecord* propertyRecord){
  3795. EventWriteJSCRIPT_HOSTING_PROPERTYID_LIST(propertyRecord, propertyRecord->GetBuffer());
  3796. });
  3797. }
  3798. #endif
  3799. #ifdef ENABLE_PROJECTION
  3800. void ThreadContext::AddExternalWeakReferenceCache(ExternalWeakReferenceCache *externalWeakReferenceCache)
  3801. {
  3802. this->externalWeakReferenceCacheList.Prepend(&HeapAllocator::Instance, externalWeakReferenceCache);
  3803. }
  3804. void ThreadContext::RemoveExternalWeakReferenceCache(ExternalWeakReferenceCache *externalWeakReferenceCache)
  3805. {
  3806. Assert(!externalWeakReferenceCacheList.Empty());
  3807. this->externalWeakReferenceCacheList.Remove(&HeapAllocator::Instance, externalWeakReferenceCache);
  3808. }
  3809. void ThreadContext::MarkExternalWeakReferencedObjects(bool inPartialCollect)
  3810. {
  3811. SListBase<ExternalWeakReferenceCache *, HeapAllocator>::Iterator iteratorWeakRefCache(&this->externalWeakReferenceCacheList);
  3812. while (iteratorWeakRefCache.Next())
  3813. {
  3814. iteratorWeakRefCache.Data()->MarkNow(recycler, inPartialCollect);
  3815. }
  3816. }
  3817. void ThreadContext::ResolveExternalWeakReferencedObjects()
  3818. {
  3819. SListBase<ExternalWeakReferenceCache *, HeapAllocator>::Iterator iteratorWeakRefCache(&this->externalWeakReferenceCacheList);
  3820. while (iteratorWeakRefCache.Next())
  3821. {
  3822. iteratorWeakRefCache.Data()->ResolveNow(recycler);
  3823. }
  3824. }
  3825. #if DBG_DUMP
  3826. void ThreadContext::RegisterProjectionMemoryInformation(IProjectionContextMemoryInfo* projectionContextMemoryInfo)
  3827. {
  3828. Assert(this->projectionMemoryInformation == nullptr || this->projectionMemoryInformation == projectionContextMemoryInfo);
  3829. this->projectionMemoryInformation = projectionContextMemoryInfo;
  3830. }
  3831. void ThreadContext::DumpProjectionContextMemoryStats(LPCWSTR headerMsg, bool forceDetailed)
  3832. {
  3833. if (this->projectionMemoryInformation)
  3834. {
  3835. this->projectionMemoryInformation->DumpCurrentStats(headerMsg, forceDetailed);
  3836. }
  3837. }
  3838. IProjectionContextMemoryInfo* ThreadContext::GetProjectionContextMemoryInformation()
  3839. {
  3840. return this->projectionMemoryInformation;
  3841. }
  3842. #endif
  3843. #endif
  3844. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  3845. Js::Var ThreadContext::GetMemoryStat(Js::ScriptContext* scriptContext)
  3846. {
  3847. ScriptMemoryDumper dumper(scriptContext);
  3848. return dumper.Dump();
  3849. }
  3850. void ThreadContext::SetAutoProxyName(LPCWSTR objectName)
  3851. {
  3852. recyclableData->autoProxyName = objectName;
  3853. }
  3854. #endif
  3855. //
  3856. // Regex helpers
  3857. //
  3858. UnifiedRegex::StandardChars<uint8>* ThreadContext::GetStandardChars(__inout_opt uint8* dummy)
  3859. {
  3860. if (standardUTF8Chars == 0)
  3861. {
  3862. ArenaAllocator* allocator = GetThreadAlloc();
  3863. standardUTF8Chars = Anew(allocator, UnifiedRegex::UTF8StandardChars, allocator);
  3864. }
  3865. return standardUTF8Chars;
  3866. }
  3867. UnifiedRegex::StandardChars<char16>* ThreadContext::GetStandardChars(__inout_opt char16* dummy)
  3868. {
  3869. if (standardUnicodeChars == 0)
  3870. {
  3871. ArenaAllocator* allocator = GetThreadAlloc();
  3872. standardUnicodeChars = Anew(allocator, UnifiedRegex::UnicodeStandardChars, allocator);
  3873. }
  3874. return standardUnicodeChars;
  3875. }
  3876. void ThreadContext::CheckScriptInterrupt()
  3877. {
  3878. if (TestThreadContextFlag(ThreadContextFlagCanDisableExecution))
  3879. {
  3880. if (this->IsExecutionDisabled())
  3881. {
  3882. Assert(this->DoInterruptProbe());
  3883. throw Js::ScriptAbortException();
  3884. }
  3885. }
  3886. else
  3887. {
  3888. this->CheckInterruptPoll();
  3889. }
  3890. }
  3891. void ThreadContext::CheckInterruptPoll()
  3892. {
  3893. // Disable QC when implicit calls are disabled since the host can do anything before returning back, like servicing the
  3894. // message loop, which may in turn cause script code to be executed and implicit calls to be made
  3895. if (!IsDisableImplicitCall())
  3896. {
  3897. InterruptPoller *poller = this->interruptPoller;
  3898. if (poller)
  3899. {
  3900. poller->CheckInterruptPoll();
  3901. }
  3902. }
  3903. }
  3904. void *
  3905. ThreadContext::GetDynamicObjectEnumeratorCache(Js::DynamicType const * dynamicType)
  3906. {
  3907. void * data;
  3908. return this->dynamicObjectEnumeratorCacheMap.TryGetValue(dynamicType, &data)? data : nullptr;
  3909. }
  3910. void
  3911. ThreadContext::AddDynamicObjectEnumeratorCache(Js::DynamicType const * dynamicType, void * cache)
  3912. {
  3913. this->dynamicObjectEnumeratorCacheMap.Item(dynamicType, cache);
  3914. }
  3915. InterruptPoller::InterruptPoller(ThreadContext *tc) :
  3916. threadContext(tc),
  3917. lastPollTick(0),
  3918. lastResetTick(0),
  3919. isDisabled(FALSE)
  3920. {
  3921. tc->SetInterruptPoller(this);
  3922. }
  3923. void InterruptPoller::CheckInterruptPoll()
  3924. {
  3925. if (!isDisabled)
  3926. {
  3927. Js::ScriptEntryExitRecord *entryExitRecord = this->threadContext->GetScriptEntryExit();
  3928. if (entryExitRecord)
  3929. {
  3930. Js::ScriptContext *scriptContext = entryExitRecord->scriptContext;
  3931. if (scriptContext)
  3932. {
  3933. this->TryInterruptPoll(scriptContext);
  3934. }
  3935. }
  3936. }
  3937. }
  3938. void InterruptPoller::GetStatementCount(ULONG *pluHi, ULONG *pluLo)
  3939. {
  3940. DWORD resetTick = this->lastResetTick;
  3941. DWORD pollTick = this->lastPollTick;
  3942. DWORD elapsed;
  3943. elapsed = pollTick - resetTick;
  3944. ULONGLONG statements = (ULONGLONG)elapsed * InterruptPoller::TicksToStatements;
  3945. *pluLo = (ULONG)statements;
  3946. *pluHi = (ULONG)(statements >> 32);
  3947. }
  3948. void ThreadContext::DisableExecution()
  3949. {
  3950. Assert(TestThreadContextFlag(ThreadContextFlagCanDisableExecution));
  3951. // Hammer the stack limit with a value that will cause script abort on the next stack probe.
  3952. this->SetStackLimitForCurrentThread(Js::Constants::StackLimitForScriptInterrupt);
  3953. return;
  3954. }
  3955. void ThreadContext::EnableExecution()
  3956. {
  3957. Assert(this->GetStackProber());
  3958. // Restore the normal stack limit.
  3959. this->SetStackLimitForCurrentThread(this->GetStackProber()->GetScriptStackLimit());
  3960. // It's possible that the host disabled execution after the script threw an exception
  3961. // of it's own, so we shouldn't clear that. Only exceptions for script termination
  3962. // should be cleared.
  3963. if (GetRecordedException() == GetPendingTerminatedErrorObject())
  3964. {
  3965. SetRecordedException(NULL);
  3966. }
  3967. }
  3968. bool ThreadContext::TestThreadContextFlag(ThreadContextFlags contextFlag) const
  3969. {
  3970. return (this->threadContextFlags & contextFlag) != 0;
  3971. }
  3972. void ThreadContext::SetThreadContextFlag(ThreadContextFlags contextFlag)
  3973. {
  3974. this->threadContextFlags = (ThreadContextFlags)(this->threadContextFlags | contextFlag);
  3975. }
  3976. void ThreadContext::ClearThreadContextFlag(ThreadContextFlags contextFlag)
  3977. {
  3978. this->threadContextFlags = (ThreadContextFlags)(this->threadContextFlags & ~contextFlag);
  3979. }
  3980. #ifdef ENABLE_GLOBALIZATION
  3981. Js::DelayLoadWinRtString * ThreadContext::GetWinRTStringLibrary()
  3982. {
  3983. delayLoadWinRtString.EnsureFromSystemDirOnly();
  3984. return &delayLoadWinRtString;
  3985. }
  3986. #ifdef ENABLE_PROJECTION
  3987. Js::DelayLoadWinRtError * ThreadContext::GetWinRTErrorLibrary()
  3988. {
  3989. delayLoadWinRtError.EnsureFromSystemDirOnly();
  3990. return &delayLoadWinRtError;
  3991. }
  3992. Js::DelayLoadWinRtTypeResolution* ThreadContext::GetWinRTTypeResolutionLibrary()
  3993. {
  3994. delayLoadWinRtTypeResolution.EnsureFromSystemDirOnly();
  3995. return &delayLoadWinRtTypeResolution;
  3996. }
  3997. Js::DelayLoadWinRtRoParameterizedIID* ThreadContext::GetWinRTRoParameterizedIIDLibrary()
  3998. {
  3999. delayLoadWinRtRoParameterizedIID.EnsureFromSystemDirOnly();
  4000. return &delayLoadWinRtRoParameterizedIID;
  4001. }
  4002. #endif
  4003. #if defined(ENABLE_INTL_OBJECT) || defined(ENABLE_ES6_CHAR_CLASSIFIER)
  4004. Js::WindowsGlobalizationAdapter* ThreadContext::GetWindowsGlobalizationAdapter()
  4005. {
  4006. return &windowsGlobalizationAdapter;
  4007. }
  4008. Js::DelayLoadWindowsGlobalization* ThreadContext::GetWindowsGlobalizationLibrary()
  4009. {
  4010. delayLoadWindowsGlobalizationLibrary.Ensure(this->GetWinRTStringLibrary());
  4011. return &delayLoadWindowsGlobalizationLibrary;
  4012. }
  4013. #endif
  4014. #ifdef ENABLE_FOUNDATION_OBJECT
  4015. Js::WindowsFoundationAdapter* ThreadContext::GetWindowsFoundationAdapter()
  4016. {
  4017. return &windowsFoundationAdapter;
  4018. }
  4019. Js::DelayLoadWinRtFoundation* ThreadContext::GetWinRtFoundationLibrary()
  4020. {
  4021. delayLoadWinRtFoundationLibrary.EnsureFromSystemDirOnly();
  4022. return &delayLoadWinRtFoundationLibrary;
  4023. }
  4024. #endif
  4025. #endif // ENABLE_GLOBALIZATION
  4026. // Despite the name, callers expect this to return the highest propid + 1.
  4027. uint ThreadContext::GetHighestPropertyNameIndex() const
  4028. {
  4029. return propertyMap->GetLastIndex() + 1 + Js::InternalPropertyIds::Count;
  4030. }
  4031. #if defined(CHECK_MEMORY_LEAK) || defined(LEAK_REPORT)
  4032. void ThreadContext::ReportAndCheckLeaksOnProcessDetach()
  4033. {
  4034. bool needReportOrCheck = false;
  4035. #ifdef LEAK_REPORT
  4036. needReportOrCheck = needReportOrCheck || Js::Configuration::Global.flags.IsEnabled(Js::LeakReportFlag);
  4037. #endif
  4038. #ifdef CHECK_MEMORY_LEAK
  4039. needReportOrCheck = needReportOrCheck ||
  4040. (Js::Configuration::Global.flags.CheckMemoryLeak && MemoryLeakCheck::IsEnableOutput());
  4041. #endif
  4042. if (!needReportOrCheck)
  4043. {
  4044. return;
  4045. }
  4046. // Report leaks even if this is a force termination and we have not clean up the thread
  4047. // This is call during process detach. No one should be creating new thread context.
  4048. // So don't need to take the lock
  4049. ThreadContext * current = GetThreadContextList();
  4050. while (current)
  4051. {
  4052. #if DBG
  4053. current->pageAllocator.ClearConcurrentThreadId();
  4054. #endif
  4055. Recycler * recycler = current->GetRecycler();
  4056. #ifdef LEAK_REPORT
  4057. if (Js::Configuration::Global.flags.IsEnabled(Js::LeakReportFlag))
  4058. {
  4059. AUTO_LEAK_REPORT_SECTION(Js::Configuration::Global.flags, _u("Thread Context (%p): Process Termination (TID: %d)"), current, current->threadId);
  4060. LeakReport::DumpUrl(current->threadId);
  4061. // Heuristically figure out which one is the root tracker script engine
  4062. // and force close on it
  4063. if (current->rootTrackerScriptContext != nullptr)
  4064. {
  4065. current->rootTrackerScriptContext->Close(false);
  4066. }
  4067. recycler->ReportLeaksOnProcessDetach();
  4068. }
  4069. #endif
  4070. #ifdef CHECK_MEMORY_LEAK
  4071. recycler->CheckLeaksOnProcessDetach(_u("Process Termination"));
  4072. #endif
  4073. current = current->Next();
  4074. }
  4075. }
  4076. #endif
  4077. #ifdef LEAK_REPORT
  4078. void
  4079. ThreadContext::SetRootTrackerScriptContext(Js::ScriptContext * scriptContext)
  4080. {
  4081. Assert(this->rootTrackerScriptContext == nullptr);
  4082. this->rootTrackerScriptContext = scriptContext;
  4083. scriptContext->isRootTrackerScriptContext = true;
  4084. }
  4085. void
  4086. ThreadContext::ClearRootTrackerScriptContext(Js::ScriptContext * scriptContext)
  4087. {
  4088. Assert(this->rootTrackerScriptContext == scriptContext);
  4089. this->rootTrackerScriptContext->isRootTrackerScriptContext = false;
  4090. this->rootTrackerScriptContext = nullptr;
  4091. }
  4092. #endif
  4093. AutoTagNativeLibraryEntry::AutoTagNativeLibraryEntry(Js::RecyclableObject* function, Js::CallInfo callInfo, PCWSTR name, void* addr)
  4094. {
  4095. // Save function/callInfo values (for StackWalker). Compiler may stackpack/optimize them for built-in native functions.
  4096. entry.function = function;
  4097. entry.callInfo = callInfo;
  4098. entry.name = name;
  4099. entry.addr = addr;
  4100. ThreadContext* threadContext = function->GetScriptContext()->GetThreadContext();
  4101. threadContext->PushNativeLibraryEntry(&entry);
  4102. }
  4103. AutoTagNativeLibraryEntry::~AutoTagNativeLibraryEntry()
  4104. {
  4105. ThreadContext* threadContext = entry.function->GetScriptContext()->GetThreadContext();
  4106. Assert(threadContext->PeekNativeLibraryEntry() == &entry);
  4107. threadContext->PopNativeLibraryEntry();
  4108. }