@@ -25,10 +25,6 @@ use crate::support::long;
2525
2626unsafe extern "C" {
2727 fn v8__ArrayBuffer__Allocator__NewDefaultAllocator ( ) -> * mut Allocator ;
28- fn v8__ArrayBuffer__Allocator__NewRustAllocator (
29- handle : * const c_void ,
30- vtable : * const RustAllocatorVtable < c_void > ,
31- ) -> * mut Allocator ;
3228 fn v8__ArrayBuffer__Allocator__DELETE ( this : * mut Allocator ) ;
3329 fn v8__ArrayBuffer__New__with_byte_length (
3430 isolate : * mut RealIsolate ,
@@ -60,7 +56,6 @@ unsafe extern "C" {
6056 deleter : BackingStoreDeleterCallback ,
6157 deleter_data : * mut c_void ,
6258 ) -> * mut BackingStore ;
63-
6459 fn v8__BackingStore__Data ( this : * const BackingStore ) -> * mut c_void ;
6560 fn v8__BackingStore__ByteLength ( this : * const BackingStore ) -> usize ;
6661 fn v8__BackingStore__IsShared ( this : * const BackingStore ) -> bool ;
@@ -108,6 +103,15 @@ unsafe extern "C" {
108103 ) -> long ;
109104}
110105
106+ // Rust allocator feature is only available in non-sandboxed mode
107+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
108+ unsafe extern "C" {
109+ fn v8__ArrayBuffer__Allocator__NewRustAllocator (
110+ handle : * const c_void ,
111+ vtable : * const RustAllocatorVtable < c_void > ,
112+ ) -> * mut Allocator ;
113+ }
114+
111115/// A thread-safe allocator that V8 uses to allocate |ArrayBuffer|'s memory.
112116 /// The allocator is a global V8 setting. It has to be set via
113117/// Isolate::CreateParams.
@@ -130,6 +134,7 @@ unsafe extern "C" {
130134pub struct Allocator ( Opaque ) ;
131135
132136/// A wrapper around the V8 Allocator class.
137+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
133138#[ repr( C ) ]
134139pub struct RustAllocatorVtable < T > {
135140 pub allocate : unsafe extern "C" fn ( handle : & T , len : usize ) -> * mut c_void ,
@@ -172,7 +177,10 @@ pub fn new_default_allocator() -> UniqueRef<Allocator> {
172177/// Creates an allocator managed by Rust code.
173178///
174179/// Marked `unsafe` because the caller must ensure that `handle` is valid and matches what `vtable` expects.
180+ ///
181+ /// Not usable in sandboxed mode
175182#[ inline( always) ]
183+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
176184pub unsafe fn new_rust_allocator < T : Sized + Send + Sync + ' static > (
177185 handle : * const T ,
178186 vtable : & ' static RustAllocatorVtable < T > ,
@@ -187,6 +195,7 @@ pub unsafe fn new_rust_allocator<T: Sized + Send + Sync + 'static>(
187195}
188196
189197#[ test]
198+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
190199fn test_rust_allocator ( ) {
191200 use std:: sync:: Arc ;
192201 use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
@@ -226,6 +235,10 @@ fn test_rust_allocator() {
226235
227236#[ test]
228237fn test_default_allocator ( ) {
238+ crate :: V8 :: initialize_platform (
239+ crate :: new_default_platform ( 0 , false ) . make_shared ( ) ,
240+ ) ;
241+ crate :: V8 :: initialize ( ) ;
229242 new_default_allocator ( ) ;
230243}
231244
@@ -241,6 +254,7 @@ pub type BackingStoreDeleterCallback = unsafe extern "C" fn(
241254 deleter_data : * mut c_void ,
242255) ;
243256
257+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
244258pub ( crate ) mod sealed {
245259 pub trait Rawable {
246260 fn byte_len ( & mut self ) -> usize ;
@@ -249,6 +263,7 @@ pub(crate) mod sealed {
249263 }
250264}
251265
266+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
252267macro_rules! rawable {
253268 ( $ty: ty) => {
254269 impl sealed:: Rawable for Box <[ $ty] > {
@@ -289,17 +304,28 @@ macro_rules! rawable {
289304 } ;
290305}
291306
307+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
292308rawable ! ( u8 ) ;
309+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
293310rawable ! ( u16 ) ;
311+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
294312rawable ! ( u32 ) ;
313+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
295314rawable ! ( u64 ) ;
315+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
296316rawable ! ( i8 ) ;
317+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
297318rawable ! ( i16 ) ;
319+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
298320rawable ! ( i32 ) ;
321+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
299322rawable ! ( i64 ) ;
323+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
300324rawable ! ( f32 ) ;
325+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
301326rawable ! ( f64 ) ;
302327
328+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
303329impl < T : Sized > sealed:: Rawable for Box < T >
304330where
305331 T : AsMut < [ u8 ] > ,
@@ -548,7 +574,10 @@ impl ArrayBuffer {
548574 ///
549575 /// The result can be later passed to ArrayBuffer::New. The raw pointer
550576 /// to the buffer must not be passed again to any V8 API function.
577+ ///
578+ /// Not available in Sandbox Mode, see new_backing_store_from_bytes for a potential alternative
551579 #[ inline( always) ]
580+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
552581 pub fn new_backing_store_from_boxed_slice (
553582 data : Box < [ u8 ] > ,
554583 ) -> UniqueRef < BackingStore > {
@@ -562,7 +591,10 @@ impl ArrayBuffer {
562591 ///
563592 /// The result can be later passed to ArrayBuffer::New. The raw pointer
564593 /// to the buffer must not be passed again to any V8 API function.
594+ ///
595+ /// Not available in Sandbox Mode, see new_backing_store_from_bytes for a potential alternative
565596 #[ inline( always) ]
597+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
566598 pub fn new_backing_store_from_vec ( data : Vec < u8 > ) -> UniqueRef < BackingStore > {
567599 Self :: new_backing_store_from_bytes ( data)
568600 }
@@ -575,6 +607,12 @@ impl ArrayBuffer {
575607 /// `Box<[u8]>`, and `Vec<u8>`. This will also support most other mutable bytes containers (including `bytes::BytesMut`),
576608 /// though these buffers will need to be boxed to manage ownership of memory.
577609 ///
610+ /// Not available in sandbox mode. Sandbox mode requires data to be allocated
611+ /// within the sandbox's address space. Within sandbox mode, consider the below alternatives
612+ ///
613+ /// 1. consider using new_backing_store and BackingStore::data() followed by doing a std::ptr::copy to copy the data into a BackingStore.
614+ /// 2. If you truly do have data that is allocated inside the sandbox address space, consider using the unsafe new_backing_store_from_ptr API
615+ ///
578616 /// ```
579617 /// // Vector of bytes
580618 /// let backing_store = v8::ArrayBuffer::new_backing_store_from_bytes(vec![1, 2, 3]);
@@ -585,6 +623,7 @@ impl ArrayBuffer {
585623 /// let backing_store = v8::ArrayBuffer::new_backing_store_from_bytes(Box::new(bytes::BytesMut::new()));
586624 /// ```
587625 #[ inline( always) ]
626+ #[ cfg( not( feature = "v8_enable_sandbox" ) ) ]
588627 pub fn new_backing_store_from_bytes < T > (
589628 mut bytes : T ,
590629 ) -> UniqueRef < BackingStore >
@@ -620,6 +659,12 @@ impl ArrayBuffer {
620659 ///
621660 /// SAFETY: This API consumes raw pointers so is inherently
622661 /// unsafe. Usually you should use new_backing_store_from_boxed_slice.
662+ ///
663+ /// WARNING: Using sandbox mode has extra limitations that may cause crashes
664+ /// or memory safety violations if this API is used incorrectly:
665+ ///
666+ /// 1. Sandbox mode requires data to be allocated within the sandbox's address space.
667+ /// 2. It is very easy to cause memory safety errors when using this API with sandbox mode
623668 #[ inline( always) ]
624669 pub unsafe fn new_backing_store_from_ptr (
625670 data_ptr : * mut c_void ,
0 commit comments