Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/http/InternalState.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl<'a> InternalState<'a> {
&& buffer.len() > 16
&& buffer.len() < 1024 * 1024 * 1024
{
let estimated_size: u32 = u32::from_ne_bytes(
let estimated_size: u32 = u32::from_le_bytes(
buffer[buffer.len() - 4..][..4]
.try_into()
.expect("infallible: size matches"),
Expand Down
1 change: 0 additions & 1 deletion src/install/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,3 @@ bun_url.workspace = true
bun_uws.workspace = true
bun_which.workspace = true
bun_wyhash.workspace = true
bun_zlib.workspace = true
3 changes: 0 additions & 3 deletions src/install/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,6 @@ pub enum Error {
#[error(transparent)]
Transpiler(#[from] bun_transpiler::Error),
#[error(transparent)]
Zlib(#[from] bun_zlib::ZlibError),
#[error(transparent)]
Paths(#[from] bun_paths::Error),
#[error(transparent)]
PathOptions(#[from] bun_paths::path_options::Error),
Expand Down Expand Up @@ -418,7 +416,6 @@ impl Error {
Self::Parsers(e) => e.name(),
Self::Bunfig(e) => e.name(),
Self::Transpiler(e) => e.name(),
Self::Zlib(e) => <&'static str>::from(e),
Self::Paths(e) => e.name(),
Self::PathOptions(e) => <&'static str>::from(e),
Self::Fmt(_) => "FmtError",
Expand Down
103 changes: 37 additions & 66 deletions src/install/extract_tarball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use bun_resolver::fs::FileSystem;
use bun_sys::FdDirExt;
type Error = crate::Error;

const MAX_DECOMPRESSED_TARBALL_SIZE: usize = 2 * 1024 * 1024 * 1024;

pub struct ExtractTarball {
pub(crate) name: StringOrTinyString,
pub(crate) resolution: Resolution,
Expand Down Expand Up @@ -281,90 +279,63 @@ impl ExtractTarball {
};

use bun_libarchive::Archiver;
use bun_zlib as Zlib;
let mut zlib_pool = Npm::Registry::BodyPool::get();
zlib_pool.reset();
// `defer Npm.Registry.BodyPool.release(zlib_pool)` → PoolGuard's Drop releases.

let mut esimated_output_size: usize = 0;

let time_started_for_verbose_logs: u64 = if PackageManager::verbose_install() {
bun_core::Timespec::now_allow_mocked_time().ns()
} else {
0
};

{
// Last 4 bytes of a gzip-compressed file are the uncompressed size.
if tgz_bytes.len() > 16 {
// If the file claims to be larger than 16 bytes and smaller than 64 MB, we'll preallocate the buffer.
// If it's larger than that, we'll do it incrementally. We want to avoid OOMing.
let last_4_bytes: u32 = u32::from_ne_bytes(
tgz_bytes[tgz_bytes.len() - 4..][..4]
.try_into()
.expect("infallible: size matches"),
);
if last_4_bytes > 16 && last_4_bytes < 64 * 1024 * 1024 {
// It's okay if this fails. We will just allocate as we go and that will error if we run out of memory.
esimated_output_size = last_4_bytes as usize;
if zlib_pool.list.capacity() == 0 {
let _ = zlib_pool.list.try_reserve_exact(last_4_bytes as usize);
} else {
let _ = zlib_pool.ensure_unused_capacity(last_4_bytes as usize);
}
// libarchive gunzips on the fly (`BufferReadStream::open_read`),
// so hand it the compressed bytes and never buffer the full tar.
// Small tarballs still try libdeflate first for speed; the gzip
// ISIZE trailer (size mod 2^32) is only trusted when small.
Comment thread
robobun marked this conversation as resolved.
Resolved
let mut decompressed_in_memory = false;
if bun_core::FeatureFlags::is_libdeflate_enabled() && tgz_bytes.len() > 16 {
let isize: u32 = u32::from_le_bytes(
tgz_bytes[tgz_bytes.len() - 4..][..4]
.try_into()
.expect("infallible: size matches"),
);
Comment thread
robobun marked this conversation as resolved.
Resolved
if isize > 16 && isize < 64 * 1024 * 1024 {
if zlib_pool.list.capacity() == 0 {
let _ = zlib_pool.list.try_reserve_exact(isize as usize);
} else {
let _ = zlib_pool.ensure_unused_capacity(isize as usize);
}
}
}

let mut needs_to_decompress = true;
if bun_core::FeatureFlags::is_libdeflate_enabled()
&& zlib_pool.list.capacity() > 16
&& esimated_output_size > 0
{
use bun_libdeflate_sys::libdeflate;
if let Some(mut decompressor) = libdeflate::OwnedDecompressor::new() {
zlib_pool.list.clear();
let result = decompressor.decompress_to_vec(
tgz_bytes,
&mut zlib_pool.list,
libdeflate::Encoding::Gzip,
);
if result.status == libdeflate::Status::Success {
needs_to_decompress = false;
if zlib_pool.list.capacity() > 16 {
use bun_libdeflate_sys::libdeflate;
if let Some(mut decompressor) = libdeflate::OwnedDecompressor::new() {
zlib_pool.list.clear();
let result = decompressor.decompress_to_vec(
tgz_bytes,
&mut zlib_pool.list,
libdeflate::Encoding::Gzip,
);
if result.status == libdeflate::Status::Success {
decompressed_in_memory = true;
}
}
}
// If libdeflate fails for any reason, fallback to zlib.
}
}

if needs_to_decompress {
let tar_input: &[u8] = if decompressed_in_memory {
&zlib_pool.list
} else {
zlib_pool.list.clear();
let mut zlib_entry =
Zlib::ZlibReaderArrayList::init(tgz_bytes, &mut zlib_pool.list)?;
zlib_entry.max_output_size = MAX_DECOMPRESSED_TARBALL_SIZE;
if let Err(err) = zlib_entry.read_all(true) {
log.add_error_fmt(
None,
bun_ast::Loc::EMPTY,
format_args!(
"{} decompressing \"{}\" to \"{}\"",
err,
bun_fmt::s(name),
bun_core::fmt::fmt_path_u8(tmpname.as_bytes(), Default::default()),
),
);
return Err(crate::Error::InstallFailed);
}
}
tgz_bytes
};
Comment thread
robobun marked this conversation as resolved.
Resolved

if PackageManager::verbose_install() {
let decompressing_ended_at: u64 = bun_core::Timespec::now_allow_mocked_time().ns();
let elapsed = decompressing_ended_at - time_started_for_verbose_logs;
bun_core::pretty_errorln!(
"[{}] Extract {}<r> (decompressed {} tgz file in {})",
"[{}] Extract {}<r> ({} tgz file)",
bun_fmt::s(name),
bun_fmt::s(tmpname.as_bytes()),
bun_core::fmt::size(tgz_bytes.len(), Default::default()),
bun_core::fmt::fmt_duration_one_decimal(elapsed),
);
}

Expand Down Expand Up @@ -395,7 +366,7 @@ impl ExtractTarball {
};

let _ = Archiver::extract_to_dir(
&zlib_pool.list,
tar_input,
extract_destination.fd(),
None,
&mut dirname_reader,
Expand Down Expand Up @@ -433,7 +404,7 @@ impl ExtractTarball {
}
_ => {
let _ = Archiver::extract_to_dir(
&zlib_pool.list,
tar_input,
extract_destination.fd(),
None,
&mut (),
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/api/BunObject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2470,7 +2470,7 @@ pub mod JSZlib {
// +---+---+---+---+---+---+---+---+
// | CRC32 | ISIZE |
// +---+---+---+---+---+---+---+---+
let estimated_size: u32 = u32::from_ne_bytes(
let estimated_size: u32 = u32::from_le_bytes(
compressed[compressed.len() - 4..][..4]
.try_into()
.expect("infallible: size matches"),
Expand Down
2 changes: 1 addition & 1 deletion src/zlib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub struct ZlibReaderArrayList<'a> {
pub(crate) state: ZlibReaderArrayListState,
/// Decompression-bomb guard: `read_all` errors instead of growing the
/// output past this many bytes. Defaults to unbounded.
pub max_output_size: usize,
pub(crate) max_output_size: usize,
}

impl<'a> Drop for ZlibReaderArrayList<'a> {
Expand Down
Loading
Loading