From d80da98e074c98146ac0c528585c30f62c5e7ee2 Mon Sep 17 00:00:00 2001 From: Semenov Aleksandr Date: Mon, 7 Apr 2025 23:22:22 +0300 Subject: [PATCH] ANI: add detection for OutOfMemory for Application.ts Signed-off-by: Semenov Aleksandr --- arkoala-arkts/arkui/src/Application.ts | 1 + interop/src/arkts/InteropNativeModule.ts | 1 + interop/src/cpp/ani/convertors-ani.h | 29 ++++++++++++++++++++++ interop/src/cpp/common-interop.cc | 9 +++++++ interop/src/interop/InteropNativeModule.ts | 1 + 5 files changed, 41 insertions(+) diff --git a/arkoala-arkts/arkui/src/Application.ts b/arkoala-arkts/arkui/src/Application.ts index 537f025d65..5c06e8deee 100644 --- a/arkoala-arkts/arkui/src/Application.ts +++ b/arkoala-arkts/arkui/src/Application.ts @@ -296,6 +296,7 @@ export class Application { this.loopIteration(arg0, arg1) if (this.enableDumpTree) dumpTree(this.rootState!.value) } catch (error) { + InteropNativeModule._CheckOOMFatal(); InteropNativeModule._NativeLog(`Application.enter() error: ${error}`) if (error instanceof Error) { const stack = error.stack diff --git a/interop/src/arkts/InteropNativeModule.ts b/interop/src/arkts/InteropNativeModule.ts index 6196419742..2e1418d78f 100644 --- a/interop/src/arkts/InteropNativeModule.ts +++ b/interop/src/arkts/InteropNativeModule.ts @@ -55,5 +55,6 @@ export class InteropNativeModule { @ani.unsafe.Quick native static _CopyArray(data: KPointer, length: int64, args: KUint8ArrayPtr): void native static _ReportMemLeaks(): void + native static _CheckOOMFatal(): void } diff --git a/interop/src/cpp/ani/convertors-ani.h b/interop/src/cpp/ani/convertors-ani.h index 596891d6d9..9004c120f9 100644 --- a/interop/src/cpp/ani/convertors-ani.h +++ b/interop/src/cpp/ani/convertors-ani.h @@ -1924,4 +1924,33 @@ void getKoalaANICallbackDispatcher(ani_class* clazz, ani_static_method* method); return __VA_ARGS__; \ } while (0) + #define CHECK_OOM_FATAL(vmContext) \ + do { \ + ani_env* env = reinterpret_cast(vmContext); \ + auto tryAlloc = [env](){ \ + ani_status status = ANI_OK; \ + \ + status = env->CreateLocalScope(1); \ + if (ANI_OK != status) return status; \ + \ + const char bigString[4096] = {'B'}; \ + ani_string aniBigStr; \ + status = env->String_NewUTF8(bigString, 4096, &aniBigStr); \ + if (ANI_OK != status) return status; \ + \ + return env->DestroyLocalScope(); \ + }; \ + \ + ani_status status = tryAlloc(); \ + if (status == ANI_OK) { \ + return; \ + } \ + \ + if (ANI_OUT_OF_MEMORY == status) { \ + INTEROP_FATAL("ERROR: OutOfMemory is detected, application is aborting"); \ + } \ + \ + INTEROP_FATAL("ERROR: allocation check didn't passed, possibly OutOfMemory"); \ + } while (0) + #endif // KOALA_ANI diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index 4686a83143..61623f28ce 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -677,4 +677,13 @@ void impl_ReportMemLeaks() { } KOALA_INTEROP_V0(ReportMemLeaks) +void impl_CheckOOMFatal(KVMContext vmContext) { + #ifdef KOALA_ANI + CHECK_OOM_FATAL(vmContext); + #else + LOGE("CheckOOMFatal not implemented for this platform"); + #endif + } +KOALA_INTEROP_CTX_V0(CheckOOMFatal) + #endif \ No newline at end of file diff --git a/interop/src/interop/InteropNativeModule.ts b/interop/src/interop/InteropNativeModule.ts index ca82c8cc58..5e68c5d28c 100644 --- a/interop/src/interop/InteropNativeModule.ts +++ b/interop/src/interop/InteropNativeModule.ts @@ -41,6 +41,7 @@ export class InteropNativeModule { public static _SetForeignVMContext(context: KPointer): void { throw "method not loaded" } public static _ReadByte(data: KPointer, index: int32, length: bigint): int32 { throw "method not loaded" } public static _WriteByte(data: KPointer, index: int32, length: bigint, value: int32): void { throw "method not loaded" } + public static _CheckOOMFatal(): void { throw "method not loaded" } } export function loadInteropNativeModule() { -- Gitee