Skip to content

Commit b40fcb1

Browse files
committed
Remove usage of AtomicU64 in wasmparser (bytecodealliance#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 39a6029 commit b40fcb1

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
@@ -2899,7 +2899,7 @@ pub(crate) struct TypeAlloc {
28992899

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

29042904
// This is a counter that's incremeneted each time `alloc_resource_id` is
29052905
// called.
@@ -2908,10 +2908,17 @@ pub(crate) struct TypeAlloc {
29082908

29092909
impl Default for TypeAlloc {
29102910
fn default() -> TypeAlloc {
2911-
static NEXT_GLOBAL_ID: AtomicU64 = AtomicU64::new(0);
2911+
static NEXT_GLOBAL_ID: AtomicUsize = AtomicUsize::new(0);
29122912
let mut ret = TypeAlloc {
29132913
list: TypeList::default(),
2914-
globally_unique_id: NEXT_GLOBAL_ID.fetch_add(1, Ordering::Relaxed),
2914+
globally_unique_id: {
2915+
let id = NEXT_GLOBAL_ID.fetch_add(1, Ordering::Relaxed);
2916+
if id > usize::MAX - 10_000 {
2917+
NEXT_GLOBAL_ID.store(usize::MAX - 10_000, Ordering::Relaxed);
2918+
panic!("overflow on the global id counter");
2919+
}
2920+
id
2921+
},
29152922
next_resource_id: 0,
29162923
};
29172924
ret.list.canonical_rec_groups = Some(Default::default());

0 commit comments

Comments
 (0)