From 26dcab239b0a3490b0db3a6901217f6275d077b9 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 13 Nov 2026 17:00:04 +0000 Subject: [PATCH 1/2] Don't panic of null pointer with offset 0 --- libcc2rs/src/rc.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index 1acb9f304a..02e5f44b70 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -986,6 +986,7 @@ impl Ptr { pub fn with_slice_mut(&self, len: usize, f: impl FnOnce(&mut [T]) -> R) -> R { let off = self.offset; match &self.kind { + PtrKind::Null if len == 0 => f(&mut []), PtrKind::Null => panic!("ub: null pointer"), PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => { assert!(off == 0 && len <= 1, "ub: with_slice_mut out of bounds"); @@ -1036,6 +1037,7 @@ impl Ptr { pub fn with_slice(&self, len: usize, f: impl FnOnce(&[T]) -> R) -> R { let off = self.offset; match &self.kind { + PtrKind::Null if len == 0 => f(&[]), PtrKind::Null => panic!("ub: null pointer"), PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => { assert!(off == 0 && len <= 1, "ub: with_slice out of bounds"); From ea3a221aabc178202d4ed21113763b5f8dcada7e Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 13 Nov 2026 17:00:57 +0000 Subject: [PATCH 2/2] Add safe shim for brotli --- libcc2rs/Cargo.toml | 2 + rules/brotli/shim.rs | 180 +++++++++++++++++++++++++++++++++++ rules/brotli/tgt_refcount.rs | 105 +++++++------------- 3 files changed, 217 insertions(+), 70 deletions(-) create mode 100644 rules/brotli/shim.rs diff --git a/libcc2rs/Cargo.toml b/libcc2rs/Cargo.toml index a1761abf65..aaaf4baf90 100644 --- a/libcc2rs/Cargo.toml +++ b/libcc2rs/Cargo.toml @@ -9,3 +9,5 @@ libc = "0.2" nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] } jiff = "0.2" sprintf = "0.4" +brotli = "8" +brotli-decompressor = "5" diff --git a/rules/brotli/shim.rs b/rules/brotli/shim.rs new file mode 100644 index 0000000000..35ca180c5e --- /dev/null +++ b/rules/brotli/shim.rs @@ -0,0 +1,180 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#![allow(non_snake_case)] + +use crate::{ByteRepr, Ptr}; +use brotli::enc::backward_references::BrotliEncoderMode; +use brotli::enc::{BrotliCompress, BrotliEncoderParams, StandardAlloc}; +use brotli_decompressor::{BrotliDecompress, BrotliDecompressStream, BrotliResult}; + +const BROTLI_DECODER_RESULT_ERROR: u32 = 0; +const BROTLI_DECODER_RESULT_SUCCESS: u32 = 1; +const BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT: u32 = 2; +const BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: u32 = 3; + +pub struct BrotliDecoderState { + pub state: brotli_decompressor::BrotliState, + taken: Ptr, +} + +impl ByteRepr for BrotliDecoderState {} + +impl Default for BrotliDecoderState { + fn default() -> Self { + Self { + state: brotli_decompressor::BrotliState::new_strict( + StandardAlloc::default(), + StandardAlloc::default(), + StandardAlloc::default(), + ), + taken: Ptr::null(), + } + } +} + +impl BrotliDecoderState { + fn release_taken(&mut self) { + self.taken.delete(); + self.taken = Ptr::null(); + } +} + +pub fn BrotliEncoderCompress( + quality: i32, + lgwin: i32, + mode: u32, + input_size: usize, + input_buffer: Ptr, + encoded_size: Ptr, + encoded_buffer: Ptr, +) -> i32 { + let params = BrotliEncoderParams { + quality, + lgwin, + mode: match mode { + 1 => BrotliEncoderMode::BROTLI_MODE_TEXT, + 2 => BrotliEncoderMode::BROTLI_MODE_FONT, + _ => BrotliEncoderMode::BROTLI_MODE_GENERIC, + }, + size_hint: input_size, + large_window: lgwin > 24, + ..Default::default() + }; + let input = input_buffer.with_slice(input_size, |s| s.to_vec()); + let mut out = Vec::new(); + match BrotliCompress(&mut input.as_slice(), &mut out, ¶ms) { + Ok(_) if out.len() <= encoded_size.read() => { + encoded_buffer.with_slice_mut(out.len(), |d| d.copy_from_slice(&out)); + encoded_size.write(out.len()); + 1 + } + _ => { + encoded_size.write(0); + 0 + } + } +} + +pub fn BrotliDecoderDecompress( + encoded_size: usize, + encoded_buffer: Ptr, + decoded_size: Ptr, + decoded_buffer: Ptr, +) -> u32 { + let input = encoded_buffer.with_slice(encoded_size, |s| s.to_vec()); + let mut out = Vec::new(); + match BrotliDecompress(&mut input.as_slice(), &mut out) { + Ok(()) if out.len() <= decoded_size.read() => { + decoded_buffer.with_slice_mut(out.len(), |d| d.copy_from_slice(&out)); + decoded_size.write(out.len()); + BROTLI_DECODER_RESULT_SUCCESS + } + _ => BROTLI_DECODER_RESULT_ERROR, + } +} + +pub fn BrotliDecoderDestroyInstance(state: Ptr) { + if state.is_null() { + return; + } + state.with_mut(|s| s.release_taken()); + state.delete(); +} + +pub fn BrotliDecoderDecompressStream( + state: Ptr, + available_in: Ptr, + next_in: Ptr>, + available_out: Ptr, + next_out: Ptr>, + total_out: Ptr, +) -> u32 { + let in_ptr = next_in.read(); + let input = in_ptr.with_slice(available_in.read(), |s| s.to_vec()); + let out_ptr = if next_out.is_null() { + Ptr::null() + } else { + next_out.read() + }; + let mut out = vec![ + 0u8; + if out_ptr.is_null() { + 0 + } else { + available_out.read() + } + ]; + let mut avail_in = input.len(); + let mut avail_out = available_out.read(); + let mut input_offset = 0; + let mut output_offset = 0; + let mut total = 0; + let result = state.with_mut(|s| { + s.release_taken(); + BrotliDecompressStream( + &mut avail_in, + &mut input_offset, + &input, + &mut avail_out, + &mut output_offset, + &mut out, + &mut total, + &mut s.state, + ) + }); + if output_offset > 0 { + out_ptr.with_slice_mut(output_offset, |d| d.copy_from_slice(&out[..output_offset])); + next_out.write(out_ptr.offset(output_offset)); + } + if input_offset > 0 { + next_in.write(in_ptr.offset(input_offset)); + } + available_in.write(avail_in); + available_out.write(avail_out); + if !total_out.is_null() { + total_out.write(total); + } + match result { + BrotliResult::ResultSuccess => BROTLI_DECODER_RESULT_SUCCESS, + BrotliResult::NeedsMoreInput => BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT, + BrotliResult::NeedsMoreOutput => BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT, + BrotliResult::ResultFailure => BROTLI_DECODER_RESULT_ERROR, + } +} + +pub fn BrotliDecoderTakeOutput(state: Ptr, size: Ptr) -> Ptr { + let mut n = size.read(); + let taken = state.with_mut(|s| { + s.release_taken(); + let out = brotli_decompressor::BrotliDecoderTakeOutput(&mut s.state, &mut n); + s.taken = Ptr::alloc_array(out.to_vec().into_boxed_slice()); + s.taken.clone() + }); + size.write(n); + taken +} + +pub fn BrotliDecoderVersion() -> u32 { + brotli::enc::encode::BrotliEncoderVersion() +} diff --git a/rules/brotli/tgt_refcount.rs b/rules/brotli/tgt_refcount.rs index 9addd780fa..bbfb1aa294 100644 --- a/rules/brotli/tgt_refcount.rs +++ b/rules/brotli/tgt_refcount.rs @@ -2,105 +2,70 @@ // Distributed under the MIT license that can be found in the LICENSE file. use libcc2rs::*; -use std::cell::RefCell; -use std::rc::Rc; + +fn t3() -> Ptr { + Ptr::null() +} + +fn t4() -> Ptr { + Ptr::null() +} + +fn t6() -> BrotliDecoderState { + Default::default() +} + +fn t7() -> BrotliDecoderState { + Default::default() +} fn f2( a0: libc::c_int, a1: libc::c_int, - a2: brotli_sys::BrotliEncoderMode, + a2: ::brotli_sys::BrotliEncoderMode, a3: usize, a4: Ptr, a5: Ptr, a6: Ptr, ) -> libc::c_int { - // Compress into a buffer bounded by the input size, as the output buffer - // may be much larger and is expensive to borrow if reinterpreted. - let mut __out_len = a5.read(); - let __max = unsafe { ::brotli_sys::BrotliEncoderMaxCompressedSize(a3) }; - let mut __out = vec![0u8; if __max == 0 { __out_len } else { __out_len.min(__max) }]; - __out_len = __out.len(); - let __ok = a4.with_slice(a3, |__in| unsafe { - ::brotli_sys::BrotliEncoderCompress( - a0, - a1, - a2, - a3, - __in.as_ptr(), - &mut __out_len, - __out.as_mut_ptr(), - ) - }); - if __ok != 0 { - a6.with_slice_mut(__out_len, |__s| __s.copy_from_slice(&__out[..__out_len])); - a5.write(__out_len); - } - __ok + BrotliEncoderCompress(a0, a1, a2, a3, a4, a5, a6) } fn f5(a0: usize, a1: Ptr, a2: Ptr, a3: Ptr) -> ::brotli_sys::BrotliDecoderResult { - let __out_len = a2.read(); - a1.with_slice(a0, |__in| { - a2.with_mut(|_v2| { - a3.with_slice_mut(__out_len, |__out| unsafe { - ::brotli_sys::BrotliDecoderDecompress( - a0, - __in.as_ptr(), - _v2 as *mut usize, - __out.as_mut_ptr(), - ) - }) - }) - }) + BrotliDecoderDecompress(a0, a1, a2, a3) } fn f6( a0: ::brotli_sys::brotli_alloc_func, a1: ::brotli_sys::brotli_free_func, a2: *mut std::ffi::c_void, -) -> *mut ::brotli_sys::BrotliDecoderState { - unsafe { ::brotli_sys::BrotliDecoderCreateInstance(None, None, std::ptr::null_mut()) } +) -> Ptr { + Ptr::alloc(BrotliDecoderState::default()) } -fn f7(a0: *mut ::brotli_sys::BrotliDecoderState) { - unsafe { ::brotli_sys::BrotliDecoderDestroyInstance(a0) } +fn f7(a0: Ptr) { + BrotliDecoderDestroyInstance(a0) } fn f8( - a0: *mut ::brotli_sys::BrotliDecoderState, + a0: Ptr, a1: Ptr, a2: Ptr>, a3: Ptr, a4: Ptr>, a5: Ptr, ) -> ::brotli_sys::BrotliDecoderResult { - let __in = a2.read(); - let __in_len = a1.read(); - let __r = __in.with_slice(__in_len, |__s| { - a1.with_mut(|_v1| { - a3.with_mut(|_v3| unsafe { - ::brotli_sys::BrotliDecoderDecompressStream( - a0, - _v1, - &mut __s.as_ptr(), - _v3, - std::ptr::null_mut(), - std::ptr::null_mut(), - ) - }) - }) - }); - a2.write(__in.offset(__in_len - a1.read())); - __r + BrotliDecoderDecompressStream(a0, a1, a2, a3, a4, a5) +} + +fn f9(a0: Ptr, a1: Ptr) -> Ptr { + BrotliDecoderTakeOutput(a0, a1) +} + +fn f35(a0: Ptr) -> ::brotli_sys::BrotliDecoderErrorCode { + a0.with(|__s| __s.state.error_code as i32) } -fn f9(a0: *mut ::brotli_sys::BrotliDecoderState, a1: Ptr) -> Ptr { - unsafe { - a1.with_mut(|_v1| { - let output: *const u8 = ::brotli_sys::BrotliDecoderTakeOutput(a0, _v1 as *mut usize); - let slice = std::slice::from_raw_parts(output, *_v1); - let result: Ptr> = Ptr::alloc(slice.to_vec()); - result.decay() - }) - } +fn f36() -> u32 { + BrotliDecoderVersion() }