Skip to content

Commit dd6c178

Browse files
committed
[GR-33278] Throw RangeError when byteLength passed to ArrayBuffer::NewBackingStore() is too large.
PullRequest: js/2151
2 parents 35a10cf + 2a337a5 commit dd6c178

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

graal-nodejs/deps/v8/src/graal/graal_isolate.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ GraalIsolate::GraalIsolate(JavaVM* jvm, JNIEnv* env, v8::Isolate::CreateParams c
681681
ACCESS_METHOD(GraalAccessMethod::array_buffer_byte_length, "arrayBufferByteLength", "(Ljava/lang/Object;)J")
682682
ACCESS_METHOD(GraalAccessMethod::array_buffer_new, "arrayBufferNew", "(Ljava/lang/Object;I)Ljava/lang/Object;")
683683
ACCESS_METHOD(GraalAccessMethod::array_buffer_new_buffer, "arrayBufferNew", "(Ljava/lang/Object;Ljava/lang/Object;J)Ljava/lang/Object;")
684-
ACCESS_METHOD(GraalAccessMethod::array_buffer_new_backing_store, "arrayBufferNewBackingStore", "(I)Ljava/lang/Object;")
684+
ACCESS_METHOD(GraalAccessMethod::array_buffer_new_backing_store, "arrayBufferNewBackingStore", "(J)Ljava/lang/Object;")
685685
ACCESS_METHOD(GraalAccessMethod::array_buffer_get_contents, "arrayBufferGetContents", "(Ljava/lang/Object;)Ljava/lang/Object;")
686686
ACCESS_METHOD(GraalAccessMethod::array_buffer_view_buffer, "arrayBufferViewBuffer", "(Ljava/lang/Object;)Ljava/lang/Object;")
687687
ACCESS_METHOD(GraalAccessMethod::array_buffer_view_byte_length, "arrayBufferViewByteLength", "(Ljava/lang/Object;)I")

graal-nodejs/deps/v8/src/graal/v8.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3453,7 +3453,7 @@ namespace v8 {
34533453

34543454
std::unique_ptr<BackingStore> ArrayBuffer::NewBackingStore(Isolate* isolate, size_t byte_length) {
34553455
GraalIsolate* graal_isolate = reinterpret_cast<GraalIsolate*> (isolate);
3456-
JNI_CALL(jobject, java_buffer, graal_isolate, GraalAccessMethod::array_buffer_new_backing_store, Object, (jint) byte_length);
3456+
JNI_CALL(jobject, java_buffer, graal_isolate, GraalAccessMethod::array_buffer_new_backing_store, Object, (jlong) byte_length);
34573457
JNIEnv* env = graal_isolate->GetJNIEnv();
34583458
jobject java_store = env->NewGlobalRef(java_buffer);
34593459
env->DeleteLocalRef(java_buffer);

graal-nodejs/mx.graal-nodejs/com.oracle.truffle.trufflenode/src/com/oracle/truffle/trufflenode/GraalJSAccess.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,11 @@ public Object arrayBufferNew(Object context, int byteLength) {
12441244
return JSArrayBuffer.createDirectArrayBuffer(realm.getContext(), realm, byteLength);
12451245
}
12461246

1247-
public Object arrayBufferNewBackingStore(int byteLength) {
1248-
return DirectByteBufferHelper.allocateDirect(byteLength);
1247+
public Object arrayBufferNewBackingStore(long byteLength) {
1248+
if (byteLength > Integer.MAX_VALUE || byteLength < 0) {
1249+
throw Errors.createRangeError("Cannot create a Buffer larger than 2147483647 bytes");
1250+
}
1251+
return DirectByteBufferHelper.allocateDirect((int) byteLength);
12491252
}
12501253

12511254
public long arrayBufferByteLength(Object arrayBuffer) {

graal-nodejs/test/graal/unit/arraybuffer.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -92,4 +92,11 @@ ArrayBufferViewNewTest(BigInt64Array, 8)
9292
ArrayBufferViewNewTest(BigUint64Array, 8)
9393
ArrayBufferViewNewTest(DataView, 1)
9494

95+
// Extracted from a test of sodium npm package
96+
EXPORT_TO_JS(NewBackingStoreSodium) {
97+
Isolate* isolate = args.GetIsolate();
98+
// Check that we do not crash
99+
ArrayBuffer::NewBackingStore(isolate, 4294967173u); // -123 interpreted as unsigned
100+
}
101+
95102
#undef SUITE

graal-nodejs/test/graal/unit/arraybuffer.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -113,4 +113,14 @@ describe('ArrayBuffer', function () {
113113
});
114114
}
115115
});
116+
describe('NewBackingStore', function () {
117+
it('should not crash for a very large byte length', function() {
118+
try {
119+
// should either succeed or throw RangeError
120+
module.ArrayBuffer_NewBackingStoreSodium();
121+
} catch (e) {
122+
assert.ok(e instanceof RangeError);
123+
}
124+
});
125+
});
116126
});

0 commit comments

Comments
 (0)