1 /**
2 * Copyright (c) 2021-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 "plugins/ets/runtime/napi/ets_napi_helpers.h"
17
18 #include "libpandafile/shorty_iterator.h"
19 #include "macros.h"
20 #include "plugins/ets/runtime/ets_coroutine.h"
21 #include "plugins/ets/runtime/mem/ets_reference.h"
22 #include "plugins/ets/runtime/napi/ets_napi.h"
23 #include "plugins/ets/runtime/types/ets_method.h"
24 #include "plugins/ets/runtime/types/ets_object.h"
25 #include "plugins/ets/runtime/types/ets_promise.h"
26 #include "plugins/ets/runtime/ets_handle_scope.h"
27 #include "plugins/ets/runtime/ets_handle.h"
28 #include "plugins/ets/runtime/ets_class_linker_extension.h"
29 #include "runtime/arch/helpers.h"
30 #include "runtime/include/managed_thread.h"
31 #include "runtime/include/method.h"
32 #include "runtime/include/runtime.h"
33 #include "runtime/include/runtime_notification.h"
34
35 #include <cstdint>
36
37 namespace ark::ets::napi {
38 namespace {
39 using Type = panda_file::Type;
40 using TypeId = panda_file::Type::TypeId;
41
42 using ExtArchTraits = arch::ExtArchTraits<RUNTIME_ARCH>;
43 using ArgReader = arch::ArgReader<RUNTIME_ARCH>;
44 using ArgCounter = arch::ArgCounter<RUNTIME_ARCH>;
45
46 class ArgWriter : public arch::ArgWriter<RUNTIME_ARCH> {
47 public:
ArgWriter(Span<uint8_t> gprArgs,Span<uint8_t> fprArgs,uint8_t * stackArgs)48 ArgWriter(Span<uint8_t> gprArgs, Span<uint8_t> fprArgs, uint8_t *stackArgs)
49 : arch::ArgWriter<RUNTIME_ARCH>(gprArgs, fprArgs, stackArgs)
50 {
51 }
52 ~ArgWriter() = default;
53
54 template <class T>
Write(T v)55 ALWAYS_INLINE typename std::enable_if_t<std::is_same<T, ObjectHeader **>::value, void> Write(T v)
56 {
57 arch::ArgWriter<RUNTIME_ARCH>::Write<EtsReference *>(
58 EtsReferenceStorage::NewEtsStackRef(reinterpret_cast<EtsObject **>(v)));
59 }
60
61 template <class T>
Write(T v)62 ALWAYS_INLINE typename std::enable_if_t<!std::is_same<T, ObjectHeader **>::value, void> Write(T v)
63 {
64 // Check T is not some kind of pointer to ObjectHeader
65 static_assert(!std::is_same_v<ObjectHeader, std::remove_cv_t<typename helpers::RemoveAllPointers<T>::type>>);
66 arch::ArgWriter<RUNTIME_ARCH>::Write(v);
67 }
68
69 NO_COPY_SEMANTIC(ArgWriter);
70 NO_MOVE_SEMANTIC(ArgWriter);
71 };
72 } // namespace
73
74 extern "C" void EtsNapiEntryPoint();
75
GetEtsNapiEntryPoint()76 const void *GetEtsNapiEntryPoint()
77 {
78 return reinterpret_cast<const void *>(EtsNapiEntryPoint);
79 }
80
81 extern "C" void EtsNapiCriticalNativeEntryPoint();
82
GetEtsNapiCriticalEntryPoint()83 const void *GetEtsNapiCriticalEntryPoint()
84 {
85 return reinterpret_cast<const void *>(EtsNapiCriticalNativeEntryPoint);
86 }
87
EtsNapiCalcStackArgsSpaceSize(Method * method,bool isCritical)88 extern "C" uint32_t EtsNapiCalcStackArgsSpaceSize(Method *method, bool isCritical)
89 {
90 ASSERT(method != nullptr);
91
92 ArgCounter counter;
93 if (!isCritical) {
94 counter.Count<EtsEnv *>(); // EtsEnv arg
95 counter.Count<ObjectHeader *>(); // class or this arg
96 }
97
98 panda_file::ShortyIterator it(method->GetShorty());
99 ++it; // Skip the return type
100 panda_file::ShortyIterator end;
101 while (it != end) {
102 Type type = *it++;
103 switch (type.GetId()) {
104 case TypeId::U1:
105 counter.Count<bool>();
106 break;
107 case TypeId::I8:
108 case TypeId::U8:
109 counter.Count<uint8_t>();
110 break;
111 case TypeId::I16:
112 case TypeId::U16:
113 counter.Count<uint16_t>();
114 break;
115 case TypeId::I32:
116 case TypeId::U32:
117 counter.Count<uint32_t>();
118 break;
119 case TypeId::F32:
120 counter.Count<float>();
121 break;
122 case TypeId::F64:
123 counter.Count<double>();
124 break;
125 case TypeId::I64:
126 case TypeId::U64:
127 counter.Count<uint64_t>();
128 break;
129 case TypeId::REFERENCE:
130 counter.Count<ObjectHeader *>();
131 break;
132 default:
133 UNREACHABLE();
134 break;
135 }
136 }
137
138 return counter.GetStackSpaceSize();
139 }
140
141 // Disable warning because the function uses ARCH_COPY_METHOD_ARGS macro.
142 // The macro uses computed goto
143 #if defined(__clang__)
144 #pragma clang diagnostic push
145 #pragma clang diagnostic ignored "-Wgnu-label-as-value"
146 #elif defined(__GNUC__)
147 #pragma GCC diagnostic push
148 #pragma GCC diagnostic ignored "-Wpedantic"
149 #endif
150
151 // input stack structure output stack structure
152 // +-------+ <- out_args +-------+
153 // | ... | | x0-x7 |
154 // +-------+ <- in_reg_args +-------+
155 // | x0-x7 | | d0-d7 |
156 // +-------+ +-------+
157 // | d0-d7 | | stack |
158 // +-------+ | arg 0 |
159 // | ... | +-------+
160 // +-------+ <- in_stack_args | ... |
161 // | stack | +-------+
162 // | arg 0 | | stack |
163 // +-------+ | arg N |
164 // | ... | +-------+
EtsNapiBeginCritical(Method * method,uint8_t * inRegsArgs,uint8_t * inStackArgs,uint8_t * outArgs,ManagedThread * thread)165 extern "C" void EtsNapiBeginCritical(Method *method, uint8_t *inRegsArgs, uint8_t *inStackArgs, uint8_t *outArgs,
166 ManagedThread *thread)
167 {
168 ASSERT(method->IsStatic());
169 ASSERT(thread == ManagedThread::GetCurrent());
170
171 Span<uint8_t> inGprArgs(inRegsArgs, ExtArchTraits::GP_ARG_NUM_BYTES);
172 Span<uint8_t> inFprArgs(inGprArgs.end(), ExtArchTraits::FP_ARG_NUM_BYTES);
173 ArgReader argReader(inGprArgs, inFprArgs, inStackArgs);
174
175 Span<uint8_t> outGprArgs(outArgs, ExtArchTraits::GP_ARG_NUM_BYTES);
176 Span<uint8_t> outFprArgs(outGprArgs.end(), ExtArchTraits::FP_ARG_NUM_BYTES);
177 auto outStackArgs = outFprArgs.end();
178 ArgWriter argWriter(outGprArgs, outFprArgs, outStackArgs);
179
180 argReader.Read<Method *>(); // Skip method
181
182 if (UNLIKELY(method->GetNativePointer() == nullptr)) {
183 auto coroutine = EtsCoroutine::CastFromThread(thread);
184 coroutine->GetPandaVM()->ResolveNativeMethod(method);
185 }
186
187 ARCH_COPY_METHOD_ARGS(method, argReader, argWriter);
188
189 Runtime::GetCurrent()->GetNotificationManager()->MethodEntryEvent(thread, method);
190 }
191
192 // Input stack =======> Output stack
193 // 0xFFFF
194 // | | | |
195 // | Prev frame | | Prev frame |
196 // | ... | | ... |
197 // +------------------------+ +------------------------+
198 // | ... | | ... |
199 // | stack args | | stack args | <--------+
200 // | ... | | ... | |
201 // +---+---+----------------+ <- in_stack_args --> +----------------+---+---+ |
202 // | | | LR | | LR | | | |
203 // | | | FP | | FP | | | |
204 // | | | Method * | | Method * | | | |
205 // | | c | FLAGS | | FLAGS | c | | |
206 // | | f +----------------+ +----------------+ f | | |
207 // | | r | ... | | ... | r | | |
208 // | | a | locals | | locals | a | | |
209 // | | m | ... | | ... | m | | |
210 // | | e +----------------+ +----------------+ e | | |
211 // | N | | ... | | ... | | N | |
212 // | A | | callee saved | | callee saved | | A | |
213 // | P | | ... | | ... | | P | |
214 // | I +---+----------------+ +----------------+---+ I | |
215 // | | ... | | ... | | |
216 // | | float args | | float args | | |
217 // | f | ... | | ... | f | |
218 // | r +--------------------+ +--------------------+ r | |
219 // | a | ... | | ... | a | |
220 // | m | general args | | general args | m | <----+ |
221 // | e | ... | | ... | e | | |
222 // | | arg0|Method* | | arg0|class(opt) | | | |
223 // | +--------------------+ <-- in_regs_args --> +--------------------+ | | | References
224 // | | | | ... | | | | to ObjectHeader *s
225 // | | | | NAPI float args | | | | on the stack
226 // | | | | (on regs) | | | |
227 // | | | | ... | | | |
228 // | | | +--------------------+ | | |
229 // | | | | ... | | | |
230 // | | space for | | NAPI general args | | -----+ |
231 // | | NAPI args | | (on regs) | | |
232 // | | | | ... | | |
233 // | | | +--------------------+ | |
234 // | | | | ... | | |
235 // | | | | NAPI args | | ---------+
236 // | | | | (on stack) | |
237 // | | | | ... | |
238 // +---+--------------------+ <- out_stack_args -> +--------------------+---+
239 // | | | |
240 // 0x0000
EtsNapiBegin(Method * method,uint8_t * inRegsArgs,uint8_t * inStackArgs,uint8_t * outStackArgs,ManagedThread * thread)241 extern "C" uint8_t *EtsNapiBegin(Method *method, uint8_t *inRegsArgs, uint8_t *inStackArgs, uint8_t *outStackArgs,
242 ManagedThread *thread)
243 {
244 ASSERT(!method->IsSynchronized());
245 ASSERT(thread == ManagedThread::GetCurrent());
246
247 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
248 auto outRegsArgs = inRegsArgs - ExtArchTraits ::FP_ARG_NUM_BYTES - ExtArchTraits ::GP_ARG_NUM_BYTES;
249
250 ASSERT(outStackArgs <= outRegsArgs);
251 ASSERT(outRegsArgs < inRegsArgs);
252 ASSERT(inRegsArgs < inStackArgs);
253
254 Span<uint8_t> inGprArgs(inRegsArgs, ExtArchTraits::GP_ARG_NUM_BYTES);
255 Span<uint8_t> inFprArgs(inGprArgs.end(), ExtArchTraits::FP_ARG_NUM_BYTES);
256 ArgReader argReader(inGprArgs, inFprArgs, inStackArgs);
257
258 Span<uint8_t> outGprArgs(outRegsArgs, ExtArchTraits::GP_ARG_NUM_BYTES);
259 Span<uint8_t> outFprArgs(outGprArgs.end(), ExtArchTraits::FP_ARG_NUM_BYTES);
260 ArgWriter argWriter(outGprArgs, outFprArgs, outStackArgs);
261
262 argReader.Read<Method *>(); // Skip method
263
264 EtsReference *classOrThisRef = nullptr;
265 if (method->IsStatic()) {
266 // Handle class object
267 auto classObj = EtsClass::FromRuntimeClass(method->GetClass())->AsObject();
268 ASSERT(classObj != nullptr);
269
270 // Replace the method pointer (Method *) with a pointer to the class object
271 auto classPtr = reinterpret_cast<EtsObject **>(inRegsArgs);
272 *classPtr = classObj;
273
274 classOrThisRef = EtsReferenceStorage::NewEtsStackRef(classPtr);
275 } else {
276 ASSERT(method->GetNumArgs() != 0);
277 ASSERT(!method->GetArgType(0).IsPrimitive());
278
279 // Handle this arg
280 auto thisPtr = const_cast<EtsObject **>(argReader.ReadPtr<EtsObject *>());
281 classOrThisRef = EtsReferenceStorage::NewEtsStackRef(thisPtr);
282 }
283
284 auto coroutine = EtsCoroutine::CastFromThread(thread);
285 auto etsNapiEnv = coroutine->GetEtsNapiEnv();
286
287 // Prepare eTS NAPI args
288 argWriter.Write(static_cast<EtsEnv *>(etsNapiEnv));
289 argWriter.Write(classOrThisRef);
290 ARCH_COPY_METHOD_ARGS(method, argReader, argWriter);
291
292 // Completed the preparation of eTS NAPI arguments on the stack
293
294 // ATTENTION!!!
295 // Don't move the following code above, because only from this point on,
296 // the stack of the current frame is correct, so we can safely walk it
297 // (e.g. stop at a safepoint and walk the stack args of NAPI frame)
298
299 if (UNLIKELY(method->GetNativePointer() == nullptr)) {
300 coroutine->GetPandaVM()->ResolveNativeMethod(method);
301 }
302
303 Runtime::GetCurrent()->GetNotificationManager()->MethodEntryEvent(thread, method);
304
305 constexpr uint32_t MAX_LOCAL_REF = 4096;
306 if (UNLIKELY(!etsNapiEnv->GetEtsReferenceStorage()->PushLocalEtsFrame(MAX_LOCAL_REF))) {
307 LOG(FATAL, RUNTIME) << "eTS NAPI push local frame failed";
308 }
309
310 auto etsMethod = EtsMethod::FromRuntimeMethod(method);
311 if (!etsMethod->IsFastNative()) {
312 thread->NativeCodeBegin();
313 }
314
315 return outRegsArgs;
316 }
317
318 #if defined(__clang__)
319 #pragma clang diagnostic pop
320 #elif defined(__GNUC__)
321 #pragma GCC diagnostic pop
322 #endif
323
EtsNapiEnd(Method * method,ManagedThread * thread,bool isFastNative)324 extern "C" void EtsNapiEnd(Method *method, ManagedThread *thread, bool isFastNative)
325 {
326 ASSERT(method != nullptr);
327 ASSERT(!method->IsSynchronized());
328 ASSERT(thread == ManagedThread::GetCurrent());
329
330 if (!isFastNative) {
331 thread->NativeCodeEnd();
332 }
333
334 Runtime::GetCurrent()->GetNotificationManager()->MethodExitEvent(thread, method);
335
336 auto coroutine = EtsCoroutine::CastFromThread(thread);
337 auto storage = coroutine->GetEtsNapiEnv()->GetEtsReferenceStorage();
338 storage->PopLocalEtsFrame(nullptr);
339 }
340
EtsNapiObjEnd(Method * method,EtsReference * etsRef,ManagedThread * thread,bool isFastNative)341 extern "C" EtsObject *EtsNapiObjEnd(Method *method, EtsReference *etsRef, ManagedThread *thread, bool isFastNative)
342 {
343 ASSERT(method != nullptr);
344 ASSERT(!method->IsSynchronized());
345 ASSERT(thread == ManagedThread::GetCurrent());
346
347 // End native scope first to get into managed scope for object manipulation
348 if (!isFastNative) {
349 thread->NativeCodeEnd();
350 }
351
352 Runtime::GetCurrent()->GetNotificationManager()->MethodExitEvent(thread, method);
353
354 EtsObject *ret = nullptr;
355
356 auto coroutine = EtsCoroutine::CastFromThread(thread);
357 auto storage = coroutine->GetEtsNapiEnv()->GetEtsReferenceStorage();
358 if (etsRef != nullptr) {
359 ret = storage->GetEtsObject(etsRef);
360 }
361
362 storage->PopLocalEtsFrame(nullptr);
363
364 return ret;
365 }
366
IsEtsMethodFastNative(Method * method)367 extern "C" bool IsEtsMethodFastNative(Method *method)
368 {
369 return EtsMethod::FromRuntimeMethod(method)->IsFastNative();
370 }
371
372 // Disable warning because the function uses ARCH_COPY_METHOD_ARGS macro.
373 // The macro uses computed goto
374 #if defined(__clang__)
375 #pragma clang diagnostic push
376 #pragma clang diagnostic ignored "-Wgnu-label-as-value"
377 #elif defined(__GNUC__)
378 #pragma GCC diagnostic push
379 #pragma GCC diagnostic ignored "-Wpedantic"
380 #endif
EtsAsyncCall(Method * method,EtsCoroutine * currentCoro,uint8_t * regArgs,uint8_t * stackArgs)381 extern "C" ObjectPointerType EtsAsyncCall(Method *method, EtsCoroutine *currentCoro, uint8_t *regArgs,
382 uint8_t *stackArgs)
383 {
384 PandaEtsVM *vm = currentCoro->GetPandaVM();
385 EtsClassLinker *etsClassLinker = vm->GetClassLinker();
386 Method *impl = etsClassLinker->GetAsyncImplMethod(method, currentCoro);
387 if (impl == nullptr) {
388 ASSERT(currentCoro->HasPendingException());
389 // Exception is thrown by GetAsyncImplMethod
390 return 0;
391 }
392 ASSERT(!currentCoro->HasPendingException());
393
394 EtsPromise *promise = EtsPromise::Create(currentCoro);
395 if (UNLIKELY(promise == nullptr)) {
396 ThrowOutOfMemoryError(currentCoro, "Cannot allocate Promise");
397 return 0;
398 }
399 auto *coroManager = currentCoro->GetCoroutineManager();
400 auto promiseRef = vm->GetGlobalObjectStorage()->Add(promise, mem::Reference::ObjectType::GLOBAL);
401 auto evt = Runtime::GetCurrent()->GetInternalAllocator()->New<CompletionEvent>(promiseRef, coroManager);
402
403 auto *cm = currentCoro->GetCoroutineManager();
404
405 PandaVector<Value> args;
406 args.reserve(method->GetNumArgs());
407 Span<uint8_t> gprArgs(regArgs, ExtArchTraits::GP_ARG_NUM_BYTES);
408 Span<uint8_t> fprArgs(gprArgs.end(), ExtArchTraits::FP_ARG_NUM_BYTES);
409 ArgReader argReader(gprArgs, fprArgs, stackArgs);
410 argReader.Read<Method *>(); // Skip method
411 if (method->IsStatic()) {
412 // Replace the method pointer (Method *) by the pointer to the object class
413 // to satisfy stack walker
414 auto classObj = EtsClass::FromRuntimeClass(method->GetClass())->AsObject();
415 ASSERT(classObj != nullptr);
416 auto classPtr = reinterpret_cast<EtsObject **>(regArgs);
417 *classPtr = classObj;
418 } else {
419 // Handle this arg
420 ASSERT(method->GetNumArgs() != 0);
421 ASSERT(!method->GetArgType(0).IsPrimitive());
422 args.push_back(Value(*const_cast<ObjectHeader **>(argReader.ReadPtr<ObjectHeader *>())));
423 }
424
425 arch::ValueWriter writer(&args);
426 ARCH_COPY_METHOD_ARGS(method, argReader, writer);
427
428 [[maybe_unused]] EtsHandleScope scope(currentCoro);
429 EtsHandle<EtsPromise> promiseHandle(currentCoro, promise);
430 auto *coro = cm->Launch(evt, impl, std::move(args), CoroutineLaunchMode::SAME_WORKER);
431 if (UNLIKELY(coro == nullptr)) {
432 ASSERT(currentCoro->HasPendingException());
433 // OOM is thrown by Launch
434 Runtime::GetCurrent()->GetInternalAllocator()->Delete(evt);
435 return 0;
436 }
437 cm->Schedule();
438 return ToObjPtr(promiseHandle.GetPtr());
439 }
440 #if defined(__clang__)
441 #pragma clang diagnostic pop
442 #elif defined(__GNUC__)
443 #pragma GCC diagnostic pop
444 #endif
445 } // namespace ark::ets::napi
446