Skip to content

Commit 5ed2857

Browse files
committed
improve safety
1 parent 31a068f commit 5ed2857

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

turbopack/crates/turbo-tasks/src/id.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ macro_rules! define_id {
4141
}
4242
/// Constructs a wrapper type from the numeric identifier.
4343
///
44-
/// # Safety
45-
///
46-
/// The passed `id` must not be zero.
44+
/// Returns `None` if the provided `id` is zero, otherwise returns
45+
/// `Some(Self)` containing the wrapped non-zero identifier.
4746
pub fn new(id: $primitive) -> Option<Self> {
4847
NonZero::<$primitive>::new(id).map(|id| Self{id})
4948
}
@@ -161,7 +160,7 @@ impl TaskId {
161160
}
162161

163162
macro_rules! make_serializable {
164-
($ty:ty, $get_object:path, $visitor_name:ident) => {
163+
($ty:ty, $get_object:path, $validate_type_id:path, $visitor_name:ident) => {
165164
impl Serialize for $ty {
166165
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
167166
where
@@ -193,8 +192,16 @@ macro_rules! make_serializable {
193192
where
194193
E: serde::de::Error,
195194
{
196-
Self::Value::new(v)
197-
.ok_or_else(|| E::unknown_variant(&format!("{v}"), &["a non zero u32"]))
195+
match Self::Value::new(v) {
196+
Some(value) => {
197+
if let Some(error) = $validate_type_id(value) {
198+
Err(E::custom(error))
199+
} else {
200+
Ok(value)
201+
}
202+
}
203+
None => Err(E::unknown_variant(&format!("{v}"), &["a non zero u32"])),
204+
}
198205
}
199206
}
200207

@@ -209,10 +216,21 @@ macro_rules! make_serializable {
209216
};
210217
}
211218

212-
make_serializable!(ValueTypeId, registry::get_value_type, ValueTypeVisitor);
213-
make_serializable!(TraitTypeId, registry::get_trait, TraitTypeVisitor);
219+
make_serializable!(
220+
ValueTypeId,
221+
registry::get_value_type,
222+
registry::validate_value_type_id,
223+
ValueTypeVisitor
224+
);
225+
make_serializable!(
226+
TraitTypeId,
227+
registry::get_trait,
228+
registry::validate_trait_type_id,
229+
TraitTypeVisitor
230+
);
214231
make_serializable!(
215232
FunctionId,
216233
registry::get_native_function,
234+
registry::validate_function_id,
217235
FunctionTypeVisitor
218236
);

turbopack/crates/turbo-tasks/src/registry.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::num::NonZeroU32;
22

3+
use anyhow::Error;
34
use once_cell::sync::Lazy;
45
use rustc_hash::{FxHashMap, FxHashSet};
56

@@ -52,6 +53,17 @@ pub fn get_function_id(func: &'static NativeFunction) -> FunctionId {
5253
.expect("function isn't registered")
5354
}
5455

56+
pub fn validate_function_id(id: FunctionId) -> Option<Error> {
57+
let len = FUNCTIONS.id_to_value.len();
58+
if *id as usize <= len {
59+
None
60+
} else {
61+
Some(anyhow::anyhow!(
62+
"Invalid function type id, {id} expected a value <= {len}"
63+
))
64+
}
65+
}
66+
5567
struct Values {
5668
id_to_value: Box<[&'static ValueType]>,
5769
value_to_id: FxHashMap<&'static ValueType, ValueTypeId>,
@@ -100,6 +112,17 @@ pub fn get_value_type(id: ValueTypeId) -> &'static ValueType {
100112
VALUES.id_to_value[*id as usize - 1]
101113
}
102114

115+
pub fn validate_value_type_id(id: ValueTypeId) -> Option<Error> {
116+
let len = VALUES.id_to_value.len();
117+
if *id as usize <= len {
118+
None
119+
} else {
120+
Some(anyhow::anyhow!(
121+
"Invalid value type id, {id} expected a value <= {len}"
122+
))
123+
}
124+
}
125+
103126
struct Traits {
104127
id_to_trait: Box<[&'static TraitType]>,
105128
trait_to_id: FxHashMap<&'static TraitType, TraitTypeId>,
@@ -144,3 +167,13 @@ pub fn get_trait_type_id(trait_type: &'static TraitType) -> TraitTypeId {
144167
pub fn get_trait(id: TraitTypeId) -> &'static TraitType {
145168
TRAITS.id_to_trait[*id as usize - 1]
146169
}
170+
pub fn validate_trait_type_id(id: TraitTypeId) -> Option<Error> {
171+
let len = TRAITS.id_to_trait.len();
172+
if *id as usize <= len {
173+
None
174+
} else {
175+
Some(anyhow::anyhow!(
176+
"Invalid trait type id, {id} expected a value <= {len}"
177+
))
178+
}
179+
}

0 commit comments

Comments
 (0)