Skip to content

Truly independent web browser

LibJS: Throw on an argument list longer than any call can take

sideshowbarker82ba30cd14b4
diff --git a/Libraries/LibJS/Rust/src/interpreter/interpreter_stack.rs b/Libraries/LibJS/Rust/src/interpreter/interpreter_stack.rs
index 6aec0a7007c..5034d454445 100644
--- a/Libraries/LibJS/Rust/src/interpreter/interpreter_stack.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/interpreter_stack.rs
@@ -13,6 +13,10 @@ use crate::layout::execution_context::ExecutionContext;
use crate::layout::value::Value;
use crate::layout::vm::InterpreterStack;
+/// More arguments than any call can pass: a frame holding them wouldn't fit in the interpreter stack, which each argument
+/// takes a value's room in.
+pub const MAXIMUM_ARGUMENT_COUNT: u64 = (InterpreterStackMemory::SIZE / size_of::<Value>()) as u64;
+
/// The memory execution contexts are bump-allocated from, by the runtime and by the interpreter itself.
pub struct InterpreterStackMemory {
memory: NonNull<u8>,
diff --git a/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs b/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
index 487c5aa1622..73d0403051e 100644
--- a/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
+++ b/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
@@ -253,8 +253,43 @@ pub fn create_list_from_array_like<'vm>(
// 3. Let len be ? LengthOfArrayLike(obj).
let length = length_of_array_like(vm, &array_like)?;
+ list_from_array_like(vm, &array_like, length, check_value)
+}
+
+/// CreateListFromArrayLike ( obj ), for the arguments of a call: An array-like longer than any call can take throws
+/// before any of its elements are read.
+pub fn create_argument_list_from_array_like(vm: &Vm, value: Value) -> ThrowCompletionOr<MarkedVec<'_, Value>> {
+ if !value.is_object() {
+ return vm.throw_completion(ErrorKind::TypeError, ErrorType::NotAnObject, &[&value]);
+ }
+ let array_like = value.as_object();
+ let length = length_of_array_like(vm, &array_like)?;
+ throw_if_too_many_arguments(vm, length)?;
+ list_from_array_like(vm, &array_like, length, None)
+}
+
+/// Throws where `argument_count` is more arguments than any call can pass.
+pub fn throw_if_too_many_arguments(vm: &Vm, argument_count: u64) -> ThrowCompletionOr<()> {
+ if argument_count > crate::interpreter::interpreter_stack::MAXIMUM_ARGUMENT_COUNT {
+ return vm.throw_completion(ErrorKind::InternalError, ErrorType::CallStackSizeExceeded, &[]);
+ }
+ Ok(())
+}
+
+/// Steps 4 through 7 of CreateListFromArrayLike.
+fn list_from_array_like<'vm>(
+ vm: &'vm Vm,
+ array_like: &Object,
+ length: u64,
+ check_value: Option<&dyn Fn(Value) -> ThrowCompletionOr<()>>,
+) -> ThrowCompletionOr<MarkedVec<'vm, Value>> {
// 4. Let list be a new empty List.
- let list = MarkedVec::with_capacity(vm, length as usize);
+ // NB: The list grows as its elements are read, past what it reserves: A length no list could hold, such as an
+ // ownKeys trap's, then fails on the elements it reads rather than on the reservation.
+ let list = MarkedVec::with_capacity(
+ vm,
+ length.min(crate::interpreter::interpreter_stack::MAXIMUM_ARGUMENT_COUNT) as usize,
+ );
// 5. Let index be 0.
// 6. Repeat, while index < len,
diff --git a/Libraries/LibJS/Rust/src/runtime/function_prototype.rs b/Libraries/LibJS/Rust/src/runtime/function_prototype.rs
index bd6529b9b5f..f774fb73e6e 100644
--- a/Libraries/LibJS/Rust/src/runtime/function_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/function_prototype.rs
@@ -45,7 +45,7 @@ use crate::layout::function_object::RawNativeFunction;
use crate::layout::object::Object;
use crate::layout::value::Value;
use crate::layout_forward::RawNativeFunctionPointer;
-use crate::runtime::abstract_operations::{call_function_object, length_of_array_like};
+use crate::runtime::abstract_operations::{call_function_object, length_of_array_like, throw_if_too_many_arguments};
use crate::runtime::bound_function::{
BOUND_FUNCTION_LENGTH_OFFSET, BOUND_FUNCTION_NAME_OFFSET, BoundFunction, LengthAndNameSlots,
};
@@ -247,6 +247,7 @@ impl FunctionPrototype {
// 4. Let argList be ? CreateListFromArrayLike(argArray).
let length = length_of_array_like(vm, &arg_array_object)?;
+ throw_if_too_many_arguments(vm, length)?;
record_forwarded_call_from_native(
vm,
function_value.as_cell(),
diff --git a/Libraries/LibJS/Rust/src/runtime/reflect_object.rs b/Libraries/LibJS/Rust/src/runtime/reflect_object.rs
index f67bc9c5cd3..5f0a1b71ba6 100644
--- a/Libraries/LibJS/Rust/src/runtime/reflect_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/reflect_object.rs
@@ -13,7 +13,7 @@ use crate::layout::function_object::RawNativeFunction;
use crate::layout::object::Object;
use crate::layout::value::Value;
use crate::layout_forward::RawNativeFunctionPointer;
-use crate::runtime::abstract_operations::{call_function_object, construct, create_list_from_array_like};
+use crate::runtime::abstract_operations::{call_function_object, construct, create_argument_list_from_array_like};
use crate::runtime::array::Array;
use crate::runtime::completion::ThrowCompletionOr;
use crate::runtime::error::ErrorKind;
@@ -108,7 +108,7 @@ impl ReflectObject {
}
// 2. Let args be ? CreateListFromArrayLike(argumentsList).
- let args = create_list_from_array_like(vm, arguments_list, None)?;
+ let args = create_argument_list_from_array_like(vm, arguments_list)?;
// 3. Perform PrepareForTailCall().
// 4. Return ? Call(target, thisArgument, args).
@@ -136,7 +136,7 @@ impl ReflectObject {
}
// 4. Let args be ? CreateListFromArrayLike(argumentsList).
- let args = create_list_from_array_like(vm, arguments_list, None)?;
+ let args = create_argument_list_from_array_like(vm, arguments_list)?;
// 5. Return ? Construct(target, args, newTarget).
Ok(Value::from_object(construct(
diff --git a/Tests/LibJS/Runtime/builtins/Function/Function.prototype.apply.js b/Tests/LibJS/Runtime/builtins/Function/Function.prototype.apply.js
index bcff5da0ce4..ace0619b8d7 100644
--- a/Tests/LibJS/Runtime/builtins/Function/Function.prototype.apply.js
+++ b/Tests/LibJS/Runtime/builtins/Function/Function.prototype.apply.js
@@ -116,6 +116,20 @@ describe("errors", () => {
Function.prototype.apply.call(null);
}).toThrowWithMessage(TypeError, "null is not a function");
});
+
+ test("an array-like longer than any call can take throws before its elements are read", () => {
+ [Infinity, 1e15, 2 ** 53 - 1, 2 ** 28].forEach(length => {
+ const arrayLike = {
+ length,
+ get 0() {
+ throw new Error("element read");
+ },
+ };
+ expect(() => {
+ (function () {}).apply(null, arrayLike);
+ }).toThrowWithMessage(InternalError, "Call stack size limit exceeded");
+ });
+ });
});
test("array-likes whose elements are read through internal methods", () => {
diff --git a/Tests/LibJS/Runtime/builtins/Proxy/Proxy.handler-ownKeys.js b/Tests/LibJS/Runtime/builtins/Proxy/Proxy.handler-ownKeys.js
index 6dba73c337a..8b2cee96457 100644
--- a/Tests/LibJS/Runtime/builtins/Proxy/Proxy.handler-ownKeys.js
+++ b/Tests/LibJS/Runtime/builtins/Proxy/Proxy.handler-ownKeys.js
@@ -3,6 +3,24 @@
describe("[[OwnPropertyKeys]] invariants", () => {
// TODO: Add tests for other [[OwnPropertyKeys]] trap invariants
+ test("result list of a huge length fails on its first element", () => {
+ const proxy = new Proxy(
+ {},
+ {
+ ownKeys() {
+ return { length: Infinity };
+ },
+ }
+ );
+
+ expect(() => {
+ Reflect.ownKeys(proxy);
+ }).toThrowWithMessage(
+ TypeError,
+ "Proxy handler's ownKeys trap violates invariant: the type of each result list element is either String or Symbol"
+ );
+ });
+
test("cannot report new property of non-extensible object", () => {
const target = Object.preventExtensions({});
const handler = {
diff --git a/Tests/LibJS/Runtime/builtins/Reflect/Reflect.apply.js b/Tests/LibJS/Runtime/builtins/Reflect/Reflect.apply.js
index c890733c0ab..e3234f71626 100644
--- a/Tests/LibJS/Runtime/builtins/Reflect/Reflect.apply.js
+++ b/Tests/LibJS/Runtime/builtins/Reflect/Reflect.apply.js
@@ -18,6 +18,20 @@ describe("errors", () => {
}).toThrowWithMessage(TypeError, `${value} is not an object`);
});
});
+
+ test("an array-like longer than any call can take throws before its elements are read", () => {
+ [Infinity, 1e15, 2 ** 53 - 1, 2 ** 28].forEach(length => {
+ const arrayLike = {
+ length,
+ get 0() {
+ throw new Error("element read");
+ },
+ };
+ expect(() => {
+ Reflect.apply(() => {}, undefined, arrayLike);
+ }).toThrowWithMessage(InternalError, "Call stack size limit exceeded");
+ });
+ });
});
describe("normal behavior", () => {
diff --git a/Tests/LibJS/Runtime/builtins/Reflect/Reflect.construct.js b/Tests/LibJS/Runtime/builtins/Reflect/Reflect.construct.js
index 34c949a9082..6a4908d12d4 100644
--- a/Tests/LibJS/Runtime/builtins/Reflect/Reflect.construct.js
+++ b/Tests/LibJS/Runtime/builtins/Reflect/Reflect.construct.js
@@ -26,6 +26,20 @@ describe("errors", () => {
}).toThrowWithMessage(TypeError, `${value} is not a constructor`);
});
});
+
+ test("an array-like longer than any call can take throws before its elements are read", () => {
+ [Infinity, 1e15, 2 ** 53 - 1, 2 ** 28].forEach(length => {
+ const arrayLike = {
+ length,
+ get 0() {
+ throw new Error("element read");
+ },
+ };
+ expect(() => {
+ Reflect.construct(function () {}, arrayLike);
+ }).toThrowWithMessage(InternalError, "Call stack size limit exceeded");
+ });
+ });
});
describe("normal behavior", () => {