-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rust/rolldown): prevent segment fault in
generate_bundle
by rem…
…oving unsafe code (#1905) <!-- Thank you for contributing! --> ### Description Another fix compared to #1904 for the segment fault problem. <!-- Please insert your description here and provide especially info about the "what" this PR is solving -->
- Loading branch information
Showing
6 changed files
with
123 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ pub mod types; | |
pub mod utils; | ||
mod worker_manager; | ||
pub use oxc_transform_napi; | ||
mod type_aliases; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use std::sync::Mutex; | ||
|
||
use rolldown_utils::unique_arc::{UniqueArc, WeakRef}; | ||
|
||
pub type UniqueArcMutex<T> = UniqueArc<Mutex<T>>; | ||
pub type WeakRefMutex<T> = WeakRef<Mutex<T>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ pub mod rustc_hash; | |
pub mod sanitize_file_name; | ||
pub mod xxhash; | ||
pub use bitset::BitSet; | ||
pub mod unique_arc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::sync::{Arc, Weak}; | ||
|
||
use crate::debug::pretty_type_name; | ||
|
||
#[derive(Debug)] | ||
pub struct UniqueArc<T: ?Sized>(Arc<T>); | ||
|
||
impl<T> UniqueArc<T> { | ||
pub fn new(value: T) -> Self { | ||
Self(Arc::new(value)) | ||
} | ||
|
||
/// # Panics | ||
/// - Don't call this method while calling the `WeakRef#with_inner` method. | ||
pub fn into_inner(self) -> T { | ||
Arc::into_inner(self.0).unwrap_or_else(|| { | ||
let t_name = pretty_type_name::<T>(); | ||
panic!("UniqueArc<{t_name}> has multiple references") | ||
}) | ||
} | ||
|
||
pub fn weak_ref(&self) -> WeakRef<T> { | ||
WeakRef(Arc::downgrade(&self.0)) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct WeakRef<T: ?Sized>(Weak<T>); | ||
|
||
impl<T> WeakRef<T> { | ||
/// This API pattern is intended to prevent users from getting `Arc<T>` and storing it. | ||
/// This will cause `UniqueArc#into_inner` panic if `Arc<T>` got stored. | ||
pub fn try_with_inner<F, R>(&self, f: F) -> Option<R> | ||
where | ||
F: FnOnce(&T) -> R, | ||
{ | ||
self.0.upgrade().map(|arc| f(&*arc)) | ||
} | ||
|
||
pub fn with_inner<F, R>(&self, f: F) -> R | ||
where | ||
F: FnOnce(&T) -> R, | ||
{ | ||
self.try_with_inner(f).unwrap_or_else(|| { | ||
let t_name = pretty_type_name::<T>(); | ||
panic!( | ||
"UniqueArc<{t_name}> is already dropped. You can't access it by this WeakRef<{t_name}>", | ||
) | ||
}) | ||
} | ||
} | ||
|
||
impl<T> Clone for WeakRef<T> { | ||
fn clone(&self) -> Self { | ||
Self(Weak::clone(&self.0)) | ||
} | ||
} |