Repository navigation
fix(wintertc): enforce max_buffer_size and fallible allocation during ArrayBuffer deserialization #5670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abdulhamid999
wants to merge
1
commit into
boa-dev:main
Choose a base branch
from
abdulhamid999:fix/structured-clone-max-buffer-size-5652
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(wintertc): enforce max_buffer_size and fallible allocation during ArrayBuffer deserialization #5670
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| use super::*; | ||
| use boa_engine::context::HostHooks; | ||
| use boa_engine::object::builtins::JsArrayBuffer; | ||
| use std::rc::Rc; | ||
|
|
||
| struct LimitedBufferHooks(u64); | ||
|
|
||
| impl HostHooks for LimitedBufferHooks { | ||
| fn max_buffer_size(&self, _context: &mut Context) -> u64 { | ||
| self.0 | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn array_buffer_deserialization_respects_max_buffer_size() { | ||
| let hooks = Rc::new(LimitedBufferHooks(16)); | ||
| let mut context = Context::builder().host_hooks(hooks).build().unwrap(); | ||
|
|
||
| // Create an ArrayBuffer value store with 32 bytes (exceeds 16 byte limit) | ||
| let payload = AlignedVec::from_slice(0, &[0x42; 32]); | ||
| let store = JsValueStore::new(ValueStoreInner::ArrayBuffer(payload)); | ||
|
|
||
| let result = store.try_into_js(&mut context); | ||
| assert!(result.is_err()); | ||
| let err_str = result.unwrap_err().to_string(); | ||
| assert!( | ||
| err_str.contains("cannot allocate a buffer that exceeds the maximum buffer size"), | ||
| "expected max buffer size RangeError, got: {err_str}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn array_buffer_deserialization_within_max_buffer_size() { | ||
| let hooks = Rc::new(LimitedBufferHooks(16)); | ||
| let mut context = Context::builder().host_hooks(hooks).build().unwrap(); | ||
|
|
||
| let data = [1, 2, 3, 4, 5, 6, 7, 8]; | ||
| let payload = AlignedVec::from_slice(0, &data); | ||
| let store = JsValueStore::new(ValueStoreInner::ArrayBuffer(payload)); | ||
|
|
||
| let result = store.try_into_js(&mut context).unwrap(); | ||
| let obj = result.as_object().unwrap(); | ||
| let ab = JsArrayBuffer::from_object(obj.clone()).unwrap(); | ||
| assert_eq!(ab.byte_length(), 8); | ||
| assert_eq!(ab.data().unwrap().as_ref(), &data); | ||
| } | ||
|
|
||
| #[test] | ||
| fn array_buffer_roundtrip_clone() { | ||
| let mut context = Context::default(); | ||
|
|
||
| let data = [10, 20, 30, 40, 50]; | ||
| let original = JsArrayBuffer::new(data.len(), &mut context).unwrap(); | ||
| original.data_mut().unwrap().copy_from_slice(&data); | ||
|
|
||
| let js_val: JsValue = original.into(); | ||
| let store = JsValueStore::try_from_js(&js_val, &mut context, vec![]).unwrap(); | ||
| let cloned_val = store.try_into_js(&mut context).unwrap(); | ||
|
|
||
| let cloned_obj = cloned_val.as_object().unwrap(); | ||
| let cloned_ab = JsArrayBuffer::from_object(cloned_obj.clone()).unwrap(); | ||
|
|
||
| assert_eq!(cloned_ab.byte_length(), 5); | ||
| assert_eq!(cloned_ab.data().unwrap().as_ref(), &data); | ||
| } | ||
|
|
||
| #[test] | ||
| fn empty_array_buffer_roundtrip() { | ||
| let mut context = Context::default(); | ||
|
|
||
| let original = JsArrayBuffer::new(0, &mut context).unwrap(); | ||
| let js_val: JsValue = original.into(); | ||
| let store = JsValueStore::try_from_js(&js_val, &mut context, vec![]).unwrap(); | ||
| let cloned_val = store.try_into_js(&mut context).unwrap(); | ||
|
|
||
| let cloned_obj = cloned_val.as_object().unwrap(); | ||
| let cloned_ab = JsArrayBuffer::from_object(cloned_obj.clone()).unwrap(); | ||
|
|
||
| assert_eq!(cloned_ab.byte_length(), 0); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remark: consider adding a small concise comment/note why
from_byte_blockisn't used here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added an explanatory note as suggested! We use JsArrayBuffer::new instead of rom_byte_block so that HostHooks::max_buffer_size is enforced and memory reservation uses fallible allocation via CreateByteDataBlock. Also rebased onto latest main. Thanks for the review!