• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/runtime.h"
17 #include <memory>
18 
19 #include "ecmascript/checkpoint/thread_state_transition.h"
20 #include "ecmascript/jit/jit.h"
21 #include "ecmascript/jspandafile/program_object.h"
22 #include "ecmascript/mem/mem_map_allocator.h"
23 namespace panda::ecmascript {
24 using PGOProfilerManager = pgo::PGOProfilerManager;
25 
26 int32_t Runtime::vmCount_ = 0;
27 int32_t Runtime::destroyCount_ = 0;
28 bool Runtime::firstVmCreated_ = false;
29 Mutex *Runtime::vmCreationLock_ = new Mutex();
30 Runtime *Runtime::instance_ = nullptr;
31 
GetInstance()32 Runtime *Runtime::GetInstance()
33 {
34     ASSERT(instance_ != nullptr);
35     return instance_;
36 }
37 
~Runtime()38 Runtime::~Runtime()
39 {
40     LockHolder lock(constpoolLock_);
41     auto iter = globalSharedConstpools_.begin();
42     while (iter != globalSharedConstpools_.end()) {
43         LOG_ECMA(INFO) << "remove js pandafile by vm destruct, file:" << iter->first->GetJSPandaFileDesc();
44         JSPandaFileManager::GetInstance()->RemoveJSPandaFile(iter->first);
45         iter->second.clear();
46         iter++;
47     }
48     globalSharedConstpools_.clear();
49     delete externalRegisteredStringTable_;
50     externalRegisteredStringTable_ = nullptr;
51 }
52 
CreateIfFirstVm(const JSRuntimeOptions & options)53 void Runtime::CreateIfFirstVm(const JSRuntimeOptions &options)
54 {
55     LockHolder lock(*vmCreationLock_);
56     if (!firstVmCreated_) {
57         Log::Initialize(options);
58         EcmaVM::InitializeIcuData(options);
59         MemMapAllocator::GetInstance()->Initialize(ecmascript::DEFAULT_REGION_SIZE);
60         PGOProfilerManager::GetInstance()->Initialize(options.GetPGOProfilerPath(),
61                                                       options.GetPGOHotnessThreshold());
62         ASSERT(instance_ == nullptr);
63         instance_ = new Runtime();
64         SharedHeap::CreateNewInstance();
65         DaemonThread::CreateNewInstance();
66         firstVmCreated_ = true;
67     }
68 }
69 
InitializeIfFirstVm(EcmaVM * vm)70 void Runtime::InitializeIfFirstVm(EcmaVM *vm)
71 {
72     {
73         LockHolder lock(*vmCreationLock_);
74         if (++vmCount_ == 1) {
75             ThreadManagedScope managedScope(vm->GetAssociatedJSThread());
76             PreInitialization(vm);
77             bool isEnableFastJit = vm->GetJSOptions().IsEnableJIT() && vm->GetJSOptions().GetEnableAsmInterpreter();
78             bool isEnableBaselineJit =
79                 vm->GetJSOptions().IsEnableBaselineJIT() && vm->GetJSOptions().GetEnableAsmInterpreter();
80             Jit::GetInstance()->SetEnableOrDisable(vm->GetJSOptions(), isEnableFastJit, isEnableBaselineJit);
81             vm->Initialize();
82             PostInitialization(vm);
83         }
84     }
85     if (!vm->IsInitialized()) {
86         ThreadManagedScope managedScope(vm->GetAssociatedJSThread());
87         vm->Initialize();
88     }
89 }
90 
PreInitialization(const EcmaVM * vm)91 void Runtime::PreInitialization(const EcmaVM *vm)
92 {
93     mainThread_ = vm->GetAssociatedJSThread();
94     mainThread_->SetMainThread();
95     nativeAreaAllocator_ = std::make_unique<NativeAreaAllocator>();
96     heapRegionAllocator_ = std::make_unique<HeapRegionAllocator>();
97     stringTable_ = std::make_unique<EcmaStringTable>();
98     SharedHeap::GetInstance()->Initialize(nativeAreaAllocator_.get(), heapRegionAllocator_.get(),
99         const_cast<EcmaVM*>(vm)->GetJSOptions(), DaemonThread::GetInstance());
100 }
101 
PostInitialization(const EcmaVM * vm)102 void Runtime::PostInitialization(const EcmaVM *vm)
103 {
104     // Use the main thread's globalconst after it has initialized,
105     // and copy shared parts to other thread's later.
106     sharedConstInited_ = true;
107     globalEnv_ = vm->GetGlobalEnv().GetTaggedValue();
108     globalConst_.CopySharedConstantsFrom(mainThread_->GlobalConstants());
109     SharedHeap::GetInstance()->PostInitialization(&globalConst_, const_cast<EcmaVM*>(vm)->GetJSOptions());
110     SharedModuleManager::GetInstance()->Initialize();
111 }
112 
DestroyIfLastVm()113 void Runtime::DestroyIfLastVm()
114 {
115     LockHolder lock(*vmCreationLock_);
116     if (--vmCount_ <= 0) {
117         SharedModuleManager::GetInstance()->SharedNativeObjDestory();
118         SharedHeap::GetInstance()->WaitAllTasksFinishedAfterAllJSThreadEliminated();
119         DaemonThread::DestroyInstance();
120         SharedHeap::DestroyInstance();
121         AnFileDataManager::GetInstance()->SafeDestroyAllData();
122         MemMapAllocator::GetInstance()->Finalize();
123         PGOProfilerManager::GetInstance()->Destroy();
124         SharedModuleManager::GetInstance()->Destroy();
125         Jit::GetInstance()->Destroy();
126         ASSERT(instance_ != nullptr);
127         delete instance_;
128         instance_ = nullptr;
129         firstVmCreated_ = false;
130     }
131 }
132 
RegisterThread(JSThread * newThread)133 void Runtime::RegisterThread(JSThread* newThread)
134 {
135     LockHolder lock(threadsLock_);
136     ASSERT(std::find(threads_.begin(), threads_.end(), newThread) == threads_.end());
137     threads_.emplace_back(newThread);
138     // send all current suspended requests to the new thread
139     for (uint32_t i = 0; i < suspendNewCount_; i++) {
140         newThread->SuspendThread(true);
141     }
142 }
143 
144 // Note: currently only called when thread is to be destroyed.
UnregisterThread(JSThread * thread)145 void Runtime::UnregisterThread(JSThread* thread)
146 {
147     LockHolder lock(threadsLock_);
148     ASSERT(std::find(threads_.begin(), threads_.end(), thread) != threads_.end());
149     ASSERT(!thread->IsInRunningState());
150     threads_.remove(thread);
151 }
152 
SuspendAll(JSThread * current)153 void Runtime::SuspendAll(JSThread *current)
154 {
155     ASSERT(current != nullptr);
156     ASSERT(!current->IsInRunningState());
157 #ifndef NDEBUG
158     ASSERT(!current->HasLaunchedSuspendAll());
159     current->LaunchSuspendAll();
160 #endif
161     SuspendAllThreadsImpl(current);
162 }
163 
ResumeAll(JSThread * current)164 void Runtime::ResumeAll(JSThread *current)
165 {
166     ASSERT(current != nullptr);
167     ASSERT(!current->IsInRunningState());
168 #ifndef NDEBUG
169     ASSERT(current->HasLaunchedSuspendAll());
170     current->CompleteSuspendAll();
171 #endif
172     ResumeAllThreadsImpl(current);
173 }
174 
SuspendAllThreadsImpl(JSThread * current)175 void Runtime::SuspendAllThreadsImpl(JSThread *current)
176 {
177     SuspendBarrier barrier;
178     for (uint32_t iterCount = 1U;; ++iterCount) {
179         {
180             LockHolder lock(threadsLock_);
181             if (suspendNewCount_ == 0) {
182                 suspendNewCount_++;
183                 if (threads_.size() == 1) {
184                     ASSERT(current == mainThread_ || current->IsDaemonThread());
185                     return;
186                 }
187                 if (threads_.size() < 1) {
188                     return;
189                 }
190                 barrier.Initialize(threads_.size() - 1);
191                 for (const auto& thread: threads_) {
192                     if (thread == current) {
193                         continue;
194                     }
195                     thread->SuspendThread(+1, &barrier);
196                     // The two flags, SUSPEND_REQUEST and ACTIVE_BARRIER, are set by Suspend-Thread guarded by
197                     // suspendLock_, so the target thread-I may do not see these two flags in time. As a result, it
198                     // can switch its state freely without responding to the ACTIVE_BARRIER flag and the
199                     // suspend-thread will always wait it. However, as long as it sees the flags, the actions of
200                     // passing barrier will be triggered. When the thread-I switches from NON_RUNNING to RUNNING,
201                     // it will firstly pass the barrier and then be blocked by the SUSPEND_REQUEST flag. If the
202                     // thread-I switches from RUNNING to NON_RUNNING, it will switch the state and then act on the
203                     // barrier. If the thread-I go to checkpoint in RUNNING state, it will act on the barrier
204                     // and be blocked by SUSPEND_REQUEST flag.
205                     if (thread->IsSuspended()) {
206                         // Because of the multi-threads situation, currently thread-I may be in RUNNING state or is
207                         // goding to be RUNNING state even inside this branch. In both scenarios, for instance of
208                         // RUNNING state, according to the modification order of atomic-variable stateAndFlags_,
209                         // thread-I will see the SUSPEND_REQUEST and ACTIVE_BARRIER and act on them before switching
210                         // to RUNNING. Besides, notice the using of suspendLock_ inside PassSuspendBarrier(), there
211                         // is not data-race for passing barrier.
212                         thread->PassSuspendBarrier();
213                     }
214                 }
215                 break;
216             }
217         }
218         if (iterCount < MAX_SUSPEND_RETRIES) {
219             LockHolder lock(threadsLock_);
220             if (suspendNewCount_ != 0) {
221                 // Someone has already suspended all threads.
222                 // Wait until it finishes.
223                 threadSuspendCondVar_.Wait(&threadsLock_);
224             }
225         } else {
226             LOG_ECMA(FATAL) << "Too many SuspendAll retries!";
227             UNREACHABLE();
228         }
229     }
230     barrier.Wait();
231 }
232 
ResumeAllThreadsImpl(JSThread * current)233 void Runtime::ResumeAllThreadsImpl(JSThread *current)
234 {
235     LockHolder lock(threadsLock_);
236     if (suspendNewCount_ > 0) {
237         suspendNewCount_--;
238     }
239     if (suspendNewCount_ == 0) {
240         // Signal to waiting to suspend threads
241         threadSuspendCondVar_.Signal();
242     }
243     for (const auto& thread : threads_) {
244         if (thread != current) {
245             thread->ResumeThread(true);
246         }
247     }
248 }
249 
IterateSerializeRoot(const RootVisitor & v)250 void Runtime::IterateSerializeRoot(const RootVisitor &v)
251 {
252     LockHolder lock(serializeLock_);
253     for (auto &it : serializeRootMap_) {
254         it.second->Iterate(v);
255     }
256 }
257 
HasCachedConstpool(const JSPandaFile * jsPandaFile)258 bool Runtime::HasCachedConstpool(const JSPandaFile *jsPandaFile)
259 {
260     LockHolder lock(constpoolLock_);
261     return globalSharedConstpools_.find(jsPandaFile) != globalSharedConstpools_.end();
262 }
263 
FindConstpool(const JSPandaFile * jsPandaFile,int32_t index)264 JSTaggedValue Runtime::FindConstpool(const JSPandaFile *jsPandaFile, int32_t index)
265 {
266     LockHolder lock(constpoolLock_);
267     return FindConstpoolUnlocked(jsPandaFile, index);
268 }
269 
FindConstpoolUnlocked(const JSPandaFile * jsPandaFile,int32_t index)270 JSTaggedValue Runtime::FindConstpoolUnlocked(const JSPandaFile *jsPandaFile, int32_t index)
271 {
272     auto iter = globalSharedConstpools_.find(jsPandaFile);
273     if (iter == globalSharedConstpools_.end()) {
274         return JSTaggedValue::Hole();
275     }
276     auto constpoolIter = iter->second.find(index);
277     if (constpoolIter == iter->second.end()) {
278         return JSTaggedValue::Hole();
279     }
280     return constpoolIter->second;
281 }
282 
AddOrUpdateConstpool(const JSPandaFile * jsPandaFile,JSHandle<ConstantPool> constpool,int32_t index)283 JSHandle<ConstantPool> Runtime::AddOrUpdateConstpool(const JSPandaFile *jsPandaFile,
284                                                      JSHandle<ConstantPool> constpool,
285                                                      int32_t index)
286 {
287     LockHolder lock(constpoolLock_);
288     if (globalSharedConstpools_.find(jsPandaFile) == globalSharedConstpools_.end()) {
289         globalSharedConstpools_[jsPandaFile] = CMap<int32_t, JSTaggedValue>();
290     }
291     auto &constpoolMap = globalSharedConstpools_[jsPandaFile];
292     auto iter = constpoolMap.find(index);
293     if (iter == constpoolMap.end()) {
294         int32_t constpoolIndex = GetAndIncreaseSharedConstpoolCount();
295         constpool->SetUnsharedConstpoolIndex(JSTaggedValue(constpoolIndex));
296         constpoolMap.insert({index, constpool.GetTaggedValue()});
297         return constpool;
298     }
299     return JSHandle<ConstantPool>(reinterpret_cast<uintptr_t>(&iter->second));
300 }
301 
FindConstpools(const JSPandaFile * jsPandaFile)302 std::optional<std::reference_wrapper<CMap<int32_t, JSTaggedValue>>> Runtime::FindConstpools(
303     const JSPandaFile *jsPandaFile)
304 {
305     LockHolder lock(constpoolLock_);
306     auto iter = globalSharedConstpools_.find(jsPandaFile);
307     if (iter == globalSharedConstpools_.end()) {
308         return std::nullopt;
309     }
310     return iter->second;
311 }
312 
ProcessNativeDeleteInSharedGC(const WeakRootVisitor & visitor)313 void Runtime::ProcessNativeDeleteInSharedGC(const WeakRootVisitor &visitor)
314 {
315     // No need lock here, only shared gc will sweep shared constpool, meanwhile other threads are suspended.
316     auto iterator = globalSharedConstpools_.begin();
317     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "Constpools:" + std::to_string(globalSharedConstpools_.size()));
318     while (iterator != globalSharedConstpools_.end()) {
319         auto &constpools = iterator->second;
320         auto constpoolIter = constpools.begin();
321         while (constpoolIter != constpools.end()) {
322             JSTaggedValue constpoolVal = constpoolIter->second;
323             if (constpoolVal.IsHeapObject()) {
324                 TaggedObject *obj = constpoolVal.GetTaggedObject();
325                 auto fwd = visitor(obj);
326                 if (fwd == nullptr) {
327                     int32_t constpoolIndex =
328                         ConstantPool::Cast(constpoolVal.GetTaggedObject())->GetUnsharedConstpoolIndex();
329                     ASSERT(0 <= constpoolIndex && constpoolIndex != ConstantPool::CONSTPOOL_TYPE_FLAG &&
330                         constpoolIndex < UNSHARED_CONSTANTPOOL_COUNT);
331                     EraseUnusedConstpool(iterator->first, constpoolIter->first, constpoolIndex);
332                     constpoolIter = constpools.erase(constpoolIter);
333                     // when shared constpool is not referenced by any objects,
334                     // global unshared constpool count can be reuse.
335                     freeSharedConstpoolIndex_.insert(constpoolIndex);
336                     continue;
337                 }
338                 if (fwd != reinterpret_cast<TaggedObject *>(obj)) {
339                     constpoolIter->second = JSTaggedValue(fwd);
340                 }
341             }
342             ++constpoolIter;
343         }
344         if (constpools.size() == 0) {
345             LOG_ECMA(INFO) << "remove js pandafile by gc, file:" << iterator->first->GetJSPandaFileDesc();
346             JSPandaFileManager::GetInstance()->RemoveJSPandaFile(iterator->first);
347             iterator = globalSharedConstpools_.erase(iterator);
348         } else {
349             ++iterator;
350         }
351     }
352 }
353 
EraseUnusedConstpool(const JSPandaFile * jsPandaFile,int32_t index,int32_t constpoolIndex)354 void Runtime::EraseUnusedConstpool(const JSPandaFile *jsPandaFile, int32_t index, int32_t constpoolIndex)
355 {
356     GCIterateThreadList([jsPandaFile, index, constpoolIndex](JSThread* thread) {
357         ASSERT(!thread->IsInRunningState());
358         auto context = thread->GetCurrentEcmaContext();
359         context->EraseUnusedConstpool(jsPandaFile, index, constpoolIndex);
360     });
361 }
362 }  // namespace panda::ecmascript
363