Truly independent web browser
LibJS: Use the getrandom and memchr crates instead of libc calls
Andreas Kling5cf88e9a9607
diff --git a/Cargo.lock b/Cargo.lockindex f3fec85f24a..93993a4fa2d 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -881,6 +881,7 @@ dependencies = ["fARM64","flapc","foldhash 0.1.5",+ "getrandom","hashbrown 0.16.1","iced-x86","indexmap",@@ -891,6 +892,7 @@ dependencies = ["libjs_runtime_macros","libregex_rust","libunicode_rust",+ "memchr","num-bigint","num-integer","num-traits",diff --git a/Libraries/LibJS/Rust/Cargo.toml b/Libraries/LibJS/Rust/Cargo.tomlindex ac829a7f409..c50fe541300 100644--- a/Libraries/LibJS/Rust/Cargo.toml+++ b/Libraries/LibJS/Rust/Cargo.toml@@ -19,6 +19,7 @@ thread-sanitizer = [][dependencies]ak = { path = "../../../AK/Rust" }foldhash = "0.1"+getrandom = "0.4"hashbrown = { version = "0.16.1", default-features = false }indexmap = "2"ladybird_allocator = { path = "../../RustAllocator", optional = true }@@ -26,6 +27,7 @@ libc = "0.2"libjs_abi = { path = "../ABI" }libjs_jit = { path = "../JIT/Rust", optional = true }libjs_runtime_macros = { path = "macros" }+memchr = "2"libregex_rust = { path = "../../LibRegex/Rust", default-features = false }libunicode_rust = { path = "../../LibUnicode/Rust", default-features = false }num-bigint = "0.4"diff --git a/Libraries/LibJS/Rust/src/random.rs b/Libraries/LibJS/Rust/src/random.rsindex b9655d710bd..04a8987540b 100644--- a/Libraries/LibJS/Rust/src/random.rs+++ b/Libraries/LibJS/Rust/src/random.rs@@ -9,57 +9,7 @@/// AK's csprng(): fills `buffer` from the best CSPRNG of the platform.fn csprng(buffer: &mut [u8]) {- #[cfg(any(- target_vendor = "apple",- target_os = "freebsd",- target_os = "openbsd",- target_os = "netbsd"- ))]- {- // SAFETY: The buffer is valid for writes of its whole length.- unsafe { libc::arc4random_buf(buffer.as_mut_ptr().cast(), buffer.len()) };- }-- #[cfg(any(target_os = "linux", target_os = "android"))]- {- let mut remaining = buffer;- while !remaining.is_empty() {- // SAFETY: The buffer is valid for writes of its whole length.- let result = unsafe { libc::getrandom(remaining.as_mut_ptr().cast(), remaining.len(), 0) };- // EINTR can be handled safely by just trying again. Others are fatal- if result == -1 {- let error = std::io::Error::last_os_error();- assert!(error.raw_os_error() == Some(libc::EINTR), "getrandom failed: {error}");- continue;- }- remaining = &mut remaining[result as usize..];- }- }-- #[cfg(windows)]- {- // SAFETY: The buffer is valid for writes of its whole length. ProcessPrng is documented to always succeed.- unsafe { ProcessPrng(buffer.as_mut_ptr(), buffer.len()) };- }-- #[cfg(not(any(- target_vendor = "apple",- target_os = "freebsd",- target_os = "openbsd",- target_os = "netbsd",- target_os = "linux",- target_os = "android",- windows- )))]- compile_error!("This build target doesn't have a CSPRNG interface specified in random.rs.");-}--// Not every Windows SDK has an import library for bcryptprimitives.dll, so rustc generates one, as the standard library-// does for its own use of ProcessPrng.-#[cfg(windows)]-#[link(name = "bcryptprimitives", kind = "raw-dylib")]-unsafe extern "system" {- fn ProcessPrng(data: *mut u8, length: usize) -> i32;+ getrandom::fill(buffer).expect("the platform CSPRNG fills buffers");}/// AK::get_random<u64>().diff --git a/Libraries/LibJS/Rust/src/utf16.rs b/Libraries/LibJS/Rust/src/utf16.rsindex d9d46b798c6..a26f3e7b653 100644--- a/Libraries/LibJS/Rust/src/utf16.rs+++ b/Libraries/LibJS/Rust/src/utf16.rs@@ -248,10 +248,7 @@ impl<'a> Utf16View<'a> {match (self, needle) {(Self::Ascii(haystack), Utf16View::Ascii(needle)) => {find_subslice(haystack, needle, start_offset, |candidates, first_byte| {- // SAFETY: The pointer and length describe the candidates slice.- let found =- unsafe { libc::memchr(candidates.as_ptr().cast(), first_byte.into(), candidates.len()) };- (!found.is_null()).then(|| found as usize - candidates.as_ptr() as usize)+ memchr::memchr(first_byte, candidates)})}(Self::Utf16(haystack), Utf16View::Utf16(needle)) => {