Skip to content

Commit f1d89a4

Browse files
authored
Remove usage of AtomicU64 in wasmparser (#1412)
Instead substitute this with `AtomicUsize` which panics on overflow to catch any possible issues on 32-bit platforms. This is motivated by rust-lang/rust#120588 which is pulling `wasmparser` into the Rust compiler and `AtomicU64` is not available on all the platforms the compiler itself is built for. I plan on backporting this to the `0.118.x` release track as well which is what's used by the `object` crate currently to avoid the need for a whole bunch of cascading updates.
1 parent e6ff972 commit f1d89a4

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

crates/wasmparser/src/validator/types.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use indexmap::{IndexMap, IndexSet};
1414
use std::collections::hash_map::Entry;
1515
use std::collections::{HashMap, HashSet};
1616
use std::ops::{Index, Range};
17-
use std::sync::atomic::{AtomicU64, Ordering};
17+
use std::sync::atomic::{AtomicUsize, Ordering};
1818
use std::{
1919
borrow::Borrow,
2020
hash::{Hash, Hasher},
@@ -1451,7 +1451,7 @@ pub struct ResourceId {
14511451
// per resource id is probably too expensive. To amortize that cost each
14521452
// top-level wasm component gets a single globally unique identifier, and
14531453
// then within a component contextually unique identifiers are handed out.
1454-
globally_unique_id: u64,
1454+
globally_unique_id: usize,
14551455

14561456
// A contextually unique id within the globally unique id above. This is
14571457
// allocated within a `TypeAlloc` with its own counter, and allocations of
@@ -2925,7 +2925,7 @@ pub(crate) struct TypeAlloc {
29252925

29262926
// This is assigned at creation of a `TypeAlloc` and then never changed.
29272927
// It's used in one entry for all `ResourceId`s contained within.
2928-
globally_unique_id: u64,
2928+
globally_unique_id: usize,
29292929

29302930
// This is a counter that's incremeneted each time `alloc_resource_id` is
29312931
// called.
@@ -2934,10 +2934,17 @@ pub(crate) struct TypeAlloc {
29342934

29352935
impl Default for TypeAlloc {
29362936
fn default() -> TypeAlloc {
2937-
static NEXT_GLOBAL_ID: AtomicU64 = AtomicU64::new(0);
2937+
static NEXT_GLOBAL_ID: AtomicUsize = AtomicUsize::new(0);
29382938
let mut ret = TypeAlloc {
29392939
list: TypeList::default(),
2940-
globally_unique_id: NEXT_GLOBAL_ID.fetch_add(1, Ordering::Relaxed),
2940+
globally_unique_id: {
2941+
let id = NEXT_GLOBAL_ID.fetch_add(1, Ordering::Relaxed);
2942+
if id > usize::MAX - 10_000 {
2943+
NEXT_GLOBAL_ID.store(usize::MAX - 10_000, Ordering::Relaxed);
2944+
panic!("overflow on the global id counter");
2945+
}
2946+
id
2947+
},
29412948
next_resource_id: 0,
29422949
};
29432950
ret.list.canonical_rec_groups = Some(Default::default());

0 commit comments

Comments
 (0)