From f1d89a4a3417bca142fbaa3b0ab9dad7d49a1d63 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 12 Feb 2024 08:42:22 -0600 Subject: [PATCH] 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 https://github.com/rust-lang/rust/pull/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. --- crates/wasmparser/src/validator/types.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/wasmparser/src/validator/types.rs b/crates/wasmparser/src/validator/types.rs index acab322d17..043469f58a 100644 --- a/crates/wasmparser/src/validator/types.rs +++ b/crates/wasmparser/src/validator/types.rs @@ -14,7 +14,7 @@ use indexmap::{IndexMap, IndexSet}; use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; use std::ops::{Index, Range}; -use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::atomic::{AtomicUsize, Ordering}; use std::{ borrow::Borrow, hash::{Hash, Hasher}, @@ -1451,7 +1451,7 @@ pub struct ResourceId { // per resource id is probably too expensive. To amortize that cost each // top-level wasm component gets a single globally unique identifier, and // then within a component contextually unique identifiers are handed out. - globally_unique_id: u64, + globally_unique_id: usize, // A contextually unique id within the globally unique id above. This is // allocated within a `TypeAlloc` with its own counter, and allocations of @@ -2925,7 +2925,7 @@ pub(crate) struct TypeAlloc { // This is assigned at creation of a `TypeAlloc` and then never changed. // It's used in one entry for all `ResourceId`s contained within. - globally_unique_id: u64, + globally_unique_id: usize, // This is a counter that's incremeneted each time `alloc_resource_id` is // called. @@ -2934,10 +2934,17 @@ pub(crate) struct TypeAlloc { impl Default for TypeAlloc { fn default() -> TypeAlloc { - static NEXT_GLOBAL_ID: AtomicU64 = AtomicU64::new(0); + static NEXT_GLOBAL_ID: AtomicUsize = AtomicUsize::new(0); let mut ret = TypeAlloc { list: TypeList::default(), - globally_unique_id: NEXT_GLOBAL_ID.fetch_add(1, Ordering::Relaxed), + globally_unique_id: { + let id = NEXT_GLOBAL_ID.fetch_add(1, Ordering::Relaxed); + if id > usize::MAX - 10_000 { + NEXT_GLOBAL_ID.store(usize::MAX - 10_000, Ordering::Relaxed); + panic!("overflow on the global id counter"); + } + id + }, next_resource_id: 0, }; ret.list.canonical_rec_groups = Some(Default::default());