From 9d2c5efc7c3602af2eaeef8c34c948fbfb98c95f Mon Sep 17 00:00:00 2001 From: lhc Date: Tue, 15 Jul 2025 20:39:09 +0800 Subject: [PATCH] Add exception check Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICMJSC?from=project-issue Signed-off-by: lhc Change-Id: Id4924b9c162abd14a551989d069bba3b2a5c8674 --- ecmascript/builtins/builtins_arraybuffer.cpp | 4 ---- ecmascript/builtins/builtins_arraybuffer.h | 4 ---- ecmascript/napi/jsnapi_expo.cpp | 4 ++++ .../allocatearraybuffer/allocatearraybuffer.js | 17 ----------------- .../allocatearraybuffer/expect_output.txt | 2 -- 5 files changed, 4 insertions(+), 27 deletions(-) diff --git a/ecmascript/builtins/builtins_arraybuffer.cpp b/ecmascript/builtins/builtins_arraybuffer.cpp index 44ca29e3d4..a4bf0f7a52 100644 --- a/ecmascript/builtins/builtins_arraybuffer.cpp +++ b/ecmascript/builtins/builtins_arraybuffer.cpp @@ -229,10 +229,6 @@ JSTaggedValue BuiltinsArrayBuffer::AllocateArrayBuffer(JSThread *thread, const J if (byteLength > INT_MAX) { THROW_RANGE_ERROR_AND_RETURN(thread, "Out of range", JSTaggedValue::Exception()); } - uint64_t totalNativeSize = static_cast(thread->GetNativeAreaAllocator()->GetArrayBufferNativeSize()); - if (UNLIKELY(totalNativeSize > MAX_NATIVE_SIZE_LIMIT)) { - THROW_RANGE_ERROR_AND_RETURN(thread, NATIVE_SIZE_OUT_OF_LIMIT_MESSAGE, JSTaggedValue::Exception()); - } uint32_t arrayByteLength = static_cast(byteLength); JSHandle arrayBuffer(obj); // 6. Set obj’s [[ArrayBufferData]] internal slot to block. diff --git a/ecmascript/builtins/builtins_arraybuffer.h b/ecmascript/builtins/builtins_arraybuffer.h index 1a0f5ff025..2fdc5d4369 100644 --- a/ecmascript/builtins/builtins_arraybuffer.h +++ b/ecmascript/builtins/builtins_arraybuffer.h @@ -113,10 +113,6 @@ public: DataViewType type, bool littleEndian); static void *GetDataPointFromBuffer(JSTaggedValue arrBuf, uint32_t byteOffset = 0); -protected: - static constexpr uint64_t MAX_NATIVE_SIZE_LIMIT = 4_GB; - static constexpr char const *NATIVE_SIZE_OUT_OF_LIMIT_MESSAGE = "total array buffer size out of limit(4_GB)"; - private: #define BUILTIN_ARRAY_BUFFER_ENTRY(name, func, length, id) \ base::BuiltinFunctionEntry::Create((name), (BuiltinsArrayBuffer::func), (length), (BUILTINS_STUB_ID(id))), diff --git a/ecmascript/napi/jsnapi_expo.cpp b/ecmascript/napi/jsnapi_expo.cpp index 322720e8f1..54fd09c757 100644 --- a/ecmascript/napi/jsnapi_expo.cpp +++ b/ecmascript/napi/jsnapi_expo.cpp @@ -2370,6 +2370,10 @@ Local StringRef::EncodeIntoUint8Array(const EcmaVM *vm) JSHandle obj = TypedArrayHelper::FastCreateTypedArray(thread, thread->GlobalConstants()->GetHandledUint8ArrayString(), length - 1, DataViewType::UINT8); + if (JSNApi::HasPendingException(vm)) { + LOG_ECMA(ERROR) << "JSNapi EncodeIntoUint8Array: Create TypedArray failed"; + return Undefined(vm); + } JSHandle arrayBuffer(thread, JSTypedArray::Cast(*obj)->GetViewedArrayBufferOrByteArray()); JSTaggedValue bufferData = JSHandle::Cast(arrayBuffer)->GetArrayBufferData(); void *buffer = JSNativePointer::Cast(bufferData.GetTaggedObject())->GetExternalPointer(); diff --git a/test/moduletest/allocatearraybuffer/allocatearraybuffer.js b/test/moduletest/allocatearraybuffer/allocatearraybuffer.js index 9475338d24..0e5a07d549 100644 --- a/test/moduletest/allocatearraybuffer/allocatearraybuffer.js +++ b/test/moduletest/allocatearraybuffer/allocatearraybuffer.js @@ -40,23 +40,6 @@ for (let i = 0; i < v32.length; i++) { const v36 = this.Atomics; print(v36.and(v32, this, v36)); -function f() { - let a1 = new ArrayBuffer(1025*1025*1025); - let a2 = new ArrayBuffer(1025*1025*1025); - let a3 = new ArrayBuffer(1025*1025*1025); - let a4 = new ArrayBuffer(1025*1025*1025); - try { - let arr = new ArrayBuffer(10); - print(a1.byteLength + a2.byteLength + a3.byteLength + a4.byteLength + arr.byteLength); - } catch (error) { - print(error); - } -} -f(); -ArkTools.forceFullGC(); -f(); -ArkTools.forceFullGC(); - function SendableArrayBufferTest() { try { const o1 = {}; diff --git a/test/moduletest/allocatearraybuffer/expect_output.txt b/test/moduletest/allocatearraybuffer/expect_output.txt index 6b6c6971a7..5fd9e962a3 100644 --- a/test/moduletest/allocatearraybuffer/expect_output.txt +++ b/test/moduletest/allocatearraybuffer/expect_output.txt @@ -14,6 +14,4 @@ undefined NaN 1 -RangeError: total array buffer size out of limit(4_GB) -RangeError: total array buffer size out of limit(4_GB) TypeError: shared ctor cannot assign unshared newTarget \ No newline at end of file -- Gitee