From 09a199ca898e34295eb9f371e19c83bad651853c Mon Sep 17 00:00:00 2001 From: Mees Delzenne Date: Fri, 16 Feb 2024 17:18:08 +0100 Subject: [PATCH 1/3] Make allocator unsafe and bump version --- Cargo.toml | 6 +++--- core/Cargo.toml | 4 ++-- core/src/allocator.rs | 2 +- macro/Cargo.toml | 6 +++--- sys/Cargo.toml | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a79343e3..d1c6c4e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rquickjs" -version = "0.4.3" +version = "0.5.0" authors = ["Mees Delzenne ", "K. "] edition = "2021" rust-version = "1.65" @@ -12,11 +12,11 @@ categories = ["api-bindings"] repository = "https://github.com/DelSkayn/rquickjs.git" [dependencies.rquickjs-core] -version = "0.4.3" +version = "0.5.0" path = "core" [dependencies.rquickjs-macro] -version = "0.4.3" +version = "0.5.0" path = "macro" optional = true diff --git a/core/Cargo.toml b/core/Cargo.toml index 2d706fca..75034157 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rquickjs-core" -version = "0.4.3" +version = "0.5.0" authors = ["Mees Delzenne ", "K. "] edition = "2021" license = "MIT" @@ -31,7 +31,7 @@ version = "0.4" optional = true [dependencies.rquickjs-sys] -version = "0.4.3" +version = "0.5.0" path = "../sys" [dependencies.dlopen] diff --git a/core/src/allocator.rs b/core/src/allocator.rs index de479dca..7dd09660 100644 --- a/core/src/allocator.rs +++ b/core/src/allocator.rs @@ -23,7 +23,7 @@ pub trait Allocator { unsafe fn realloc(&mut self, ptr: RawMemPtr, new_size: usize) -> RawMemPtr; /// Get usable size of allocated memory region - fn usable_size(ptr: RawMemPtr) -> usize + unsafe fn usable_size(ptr: RawMemPtr) -> usize where Self: Sized; } diff --git a/macro/Cargo.toml b/macro/Cargo.toml index b1ae17fd..c093722f 100644 --- a/macro/Cargo.toml +++ b/macro/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rquickjs-macro" -version = "0.4.3" +version = "0.5.0" authors = ["K. ", "Mees Delzenne "] edition = "2021" license = "MIT" @@ -37,7 +37,7 @@ optional = true [dependencies.rquickjs-core] path = "../core" -version = "0.4.3" +version = "0.5.0" features = ["loader"] [dev-dependencies.difference] @@ -45,7 +45,7 @@ version = "2" [dev-dependencies.rquickjs] path = ".." -version = "0.4.3" +version = "0.5.0" features = ["macro", "classes", "properties", "futures","phf"] [dev-dependencies.async-std] diff --git a/sys/Cargo.toml b/sys/Cargo.toml index df27ab2b..562fb49c 100644 --- a/sys/Cargo.toml +++ b/sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rquickjs-sys" -version = "0.4.3" +version = "0.5.0" authors = ["Mees Delzenne "] edition = "2021" license = "MIT" From ab066c3d689ba61492ff4fb24fe3ae46bf495e73 Mon Sep 17 00:00:00 2001 From: Mees Delzenne Date: Fri, 16 Feb 2024 17:30:41 +0100 Subject: [PATCH 2/3] Add docs --- core/src/allocator.rs | 27 +++++++++++++++++++++++++-- core/src/allocator/rust.rs | 10 +++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/core/src/allocator.rs b/core/src/allocator.rs index 7dd09660..37ccb027 100644 --- a/core/src/allocator.rs +++ b/core/src/allocator.rs @@ -11,18 +11,41 @@ pub use rust::RustAllocator; pub type RawMemPtr = *mut u8; /// The allocator interface +/// +/// # Safety +/// Failure to implement this trait correctly will result in undefined behavior. +/// - `alloc` must return a either a null pointer or a pointer to an available region of memory +/// atleast `size` bytes and aligned to the size of `usize`. +/// - `realloc` must either return a null pointer or return a pointer to an available region of +/// memory atleast `new_size` bytes and aligned to the size of `usize`. +/// - `usable_size` must return the amount of available memory for any allocation allocated with +/// this allocator. #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "allocator")))] -pub trait Allocator { +pub unsafe trait Allocator { /// Allocate new memory - unsafe fn alloc(&mut self, size: usize) -> RawMemPtr; + /// + /// + fn alloc(&mut self, size: usize) -> RawMemPtr; /// De-allocate previously allocated memory + /// + /// # Safety + /// Caller must ensure that the pointer that is being deallocated was allocated by the same + /// Allocator instance. unsafe fn dealloc(&mut self, ptr: RawMemPtr); /// Re-allocate previously allocated memory + /// + /// # Safety + /// Caller must ensure that the pointer points to an allocation that was allocated by the same + /// Allocator instance. unsafe fn realloc(&mut self, ptr: RawMemPtr, new_size: usize) -> RawMemPtr; /// Get usable size of allocated memory region + /// + /// # Safety + /// Caller must ensure that the pointer handed to this function points to an allocation + /// allocated by the same allocator instance. unsafe fn usable_size(ptr: RawMemPtr) -> usize where Self: Sized; diff --git a/core/src/allocator/rust.rs b/core/src/allocator/rust.rs index 7ba36dca..6eba6ed8 100644 --- a/core/src/allocator/rust.rs +++ b/core/src/allocator/rust.rs @@ -37,8 +37,8 @@ fn round_size(size: usize) -> usize { /// The allocator which uses Rust global allocator pub struct RustAllocator; -impl Allocator for RustAllocator { - unsafe fn alloc(&mut self, size: usize) -> RawMemPtr { +unsafe impl Allocator for RustAllocator { + fn alloc(&mut self, size: usize) -> RawMemPtr { let size = round_size(size); let alloc_size = size + HEADER_SIZE; let layout = if let Ok(layout) = Layout::from_size_align(alloc_size, ALLOC_ALIGN) { @@ -98,7 +98,7 @@ impl Allocator for RustAllocator { } #[allow(clippy::not_unsafe_ptr_arg_deref)] - fn usable_size(ptr: RawMemPtr) -> usize { + unsafe fn usable_size(ptr: RawMemPtr) -> usize { let ptr = unsafe { ptr.sub(HEADER_SIZE) }; let header = unsafe { &*(ptr as *const Header) }; header.size @@ -115,7 +115,7 @@ mod test { struct TestAllocator; - impl Allocator for TestAllocator { + unsafe impl Allocator for TestAllocator { unsafe fn alloc(&mut self, size: usize) -> crate::allocator::RawMemPtr { let res = RustAllocator.alloc(size); ALLOC_SIZE.fetch_add(RustAllocator::usable_size(res), Ordering::AcqRel); @@ -143,7 +143,7 @@ mod test { res } - fn usable_size(ptr: crate::allocator::RawMemPtr) -> usize + unsafe fn usable_size(ptr: crate::allocator::RawMemPtr) -> usize where Self: Sized, { From 3bd9f9106693e0eac6d6ef7a0a7a5dfb85484387 Mon Sep 17 00:00:00 2001 From: Mees Delzenne Date: Fri, 16 Feb 2024 17:38:34 +0100 Subject: [PATCH 3/3] Fix clippy --- core/src/allocator/rust.rs | 10 ++++++---- core/src/runtime/async.rs | 5 +++-- core/src/runtime/raw.rs | 5 +++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/core/src/allocator/rust.rs b/core/src/allocator/rust.rs index 6eba6ed8..4ea28cc5 100644 --- a/core/src/allocator/rust.rs +++ b/core/src/allocator/rust.rs @@ -116,10 +116,12 @@ mod test { struct TestAllocator; unsafe impl Allocator for TestAllocator { - unsafe fn alloc(&mut self, size: usize) -> crate::allocator::RawMemPtr { - let res = RustAllocator.alloc(size); - ALLOC_SIZE.fetch_add(RustAllocator::usable_size(res), Ordering::AcqRel); - res + fn alloc(&mut self, size: usize) -> crate::allocator::RawMemPtr { + unsafe { + let res = RustAllocator.alloc(size); + ALLOC_SIZE.fetch_add(RustAllocator::usable_size(res), Ordering::AcqRel); + res + } } unsafe fn dealloc(&mut self, ptr: crate::allocator::RawMemPtr) { diff --git a/core/src/runtime/async.rs b/core/src/runtime/async.rs index 650dad92..7ddeb257 100644 --- a/core/src/runtime/async.rs +++ b/core/src/runtime/async.rs @@ -286,11 +286,12 @@ impl AsyncRuntime { lock.drop_pending(); loop { - match lock.runtime.execute_pending_job().map_err(|e| { + let pending = lock.runtime.execute_pending_job().map_err(|e| { let ptr = NonNull::new(e) .expect("executing pending job returned a null context on error"); AsyncJobException(unsafe { AsyncContext::from_raw(ptr, self.clone()) }) - }) { + }); + match pending { Err(e) => { // SAFETY: Runtime is already locked so creating a context is safe. let ctx = unsafe { Ctx::from_ptr(e.0 .0.ctx.as_ptr()) }; diff --git a/core/src/runtime/raw.rs b/core/src/runtime/raw.rs index 22c7b1ad..daa45340 100644 --- a/core/src/runtime/raw.rs +++ b/core/src/runtime/raw.rs @@ -232,10 +232,11 @@ impl RawRuntime { _rt: *mut qjs::JSRuntime, opaque: *mut ::std::os::raw::c_void, ) -> ::std::os::raw::c_int { - let should_interrupt = match panic::catch_unwind(move || { + let catch_unwind = panic::catch_unwind(move || { let opaque = &mut *(opaque as *mut Opaque); opaque.interrupt_handler.as_mut().expect("handler is set")() - }) { + }); + let should_interrupt = match catch_unwind { Ok(should_interrupt) => should_interrupt, Err(panic) => { let opaque = &mut *(opaque as *mut Opaque);