Skip to content

Add support for map type#12216

Open
yordis wants to merge 19 commits intobytecodealliance:mainfrom
yordis:yordis/feat-map-support
Open

Add support for map type#12216
yordis wants to merge 19 commits intobytecodealliance:mainfrom
yordis:yordis/feat-map-support

Conversation

@yordis
Copy link

@yordis yordis commented Dec 23, 2025

@yordis yordis force-pushed the yordis/feat-map-support branch 3 times, most recently from 6b6f675 to cd2282b Compare December 23, 2025 21:53
@github-actions github-actions bot added wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:c-api Issues pertaining to the C API. wasmtime:config Issues related to the configuration of Wasmtime wizer Issues related to Wizer snapshotting, pre-initialization, and the `wasmtime wizer` subcommand labels Dec 23, 2025
@github-actions
Copy link

Subscribe to Label Action

cc @fitzgen

Details This issue or pull request has been labeled: "wasmtime:api", "wasmtime:c-api", "wasmtime:config", "wizer"

Thus the following users have been cc'd because of the following labels:

  • fitzgen: wizer

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@github-actions
Copy link

Label Messager: wasmtime:config

It looks like you are changing Wasmtime's configuration options. Make sure to
complete this check list:

  • If you added a new Config method, you wrote extensive documentation for
    it.

    Details

    Our documentation should be of the following form:

    Short, simple summary sentence.
    
    More details. These details can be multiple paragraphs. There should be
    information about not just the method, but its parameters and results as
    well.
    
    Is this method fallible? If so, when can it return an error?
    
    Can this method panic? If so, when does it panic?
    
    # Example
    
    Optional example here.
    
  • If you added a new Config method, or modified an existing one, you
    ensured that this configuration is exercised by the fuzz targets.

    Details

    For example, if you expose a new strategy for allocating the next instance
    slot inside the pooling allocator, you should ensure that at least one of our
    fuzz targets exercises that new strategy.

    Often, all that is required of you is to ensure that there is a knob for this
    configuration option in wasmtime_fuzzing::Config (or one
    of its nested structs).

    Rarely, this may require authoring a new fuzz target to specifically test this
    configuration. See our docs on fuzzing for more details.

  • If you are enabling a configuration option by default, make sure that it
    has been fuzzed for at least two weeks before turning it on by default.


Details

To modify this label's message, edit the .github/label-messager/wasmtime-config.md file.

To add new label messages or remove existing label messages, edit the
.github/label-messager.json configuration file.

Learn more.

@alexcrichton
Copy link
Member

On a skim this looks like it's all in the right direction, thanks! As as a heads up the wasm-tools deps will be updated in #12254 which'll avoid the need for git deps. I'll take a closer look once this is further along in CI passing tests

@yordis yordis force-pushed the yordis/feat-map-support branch 5 times, most recently from db45a3a to 83dd7a7 Compare January 8, 2026 10:17
@github-actions github-actions bot added the fuzzing Issues related to our fuzzing infrastructure label Jan 8, 2026
@github-actions
Copy link

github-actions bot commented Jan 8, 2026

Subscribe to Label Action

cc @fitzgen

Details This issue or pull request has been labeled: "fuzzing"

Thus the following users have been cc'd because of the following labels:

  • fitzgen: fuzzing

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@yordis yordis force-pushed the yordis/feat-map-support branch from 83dd7a7 to b9ca740 Compare January 8, 2026 19:19
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking quite good to me, thanks for the thorough tests!

I haven't scrutinized the trampoline generation nor the lifting/lowering yet, but I can do that once the tests added here are passing (the #[ignore] ones at least).

If you can one thing I'd also recommend is modeling as many tests as possible as a *.wast test since that's generally the easiest to run and share (albeit difficult to write and debug)

@yordis yordis force-pushed the yordis/feat-map-support branch from 301e19b to d291adf Compare February 6, 2026 06:26
@yordis yordis requested a review from alexcrichton February 10, 2026 20:12
@yordis yordis force-pushed the yordis/feat-map-support branch from ddac63a to b6376cb Compare February 16, 2026 23:06
@yordis yordis marked this pull request as ready for review February 16, 2026 23:07
@yordis yordis requested review from a team as code owners February 16, 2026 23:07
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly some thoughts about deduplication/sharing of the gnarliest bits below --

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
…model

This commit introduces the Map and MapEntry classes, enabling the representation of map values in the component model. The Map class allows for the creation and iteration of key/value pairs, enhancing the functionality of the wasmtime component API. Additionally, the .gitignore file is updated to exclude build artifacts from the crates/c-api directory.
This commit introduces support for HashMap<K, V> in the component model, allowing maps to be represented as list<tuple<K, V>> in the canonical ABI. It includes implementations for the ComponentType, Lower, and Lift traits for HashMap, enabling type checking, lowering to flat representations, and lifting from memory. Additionally, the maximum depth for type generation in the fuzzing utility is updated to accommodate the new map type.
This commit removes the previous wasm features configuration and adds new functions for creating a map-configured engine. The `map_config` and `map_engine` functions are introduced to facilitate the use of the component model with maps in tests, ensuring that the engine is properly configured for map types in the component model.
…existing tests

This commit introduces a new WAST test file specifically for testing various map types in the component model. Additionally, it removes the redundant map type definitions from the existing types.wast file to streamline the test suite.
…ved iteration and memory management. Introduce new implementations for ComponentType, Lower, and Lift traits for std::collections::HashMap, enhancing support for map types in the component model.
@yordis yordis force-pushed the yordis/feat-map-support branch from b6376cb to e644b8b Compare February 27, 2026 01:21
The translate_map function had two categories of bugs preventing map
adapter trampolines from working:

1. Wasm stack discipline: local_set_new_tmp emits LocalSet which pops
   from the stack, but was called when the stack was empty (to
   "pre-allocate" locals). Fixed by computing values first, then
   calling local_set_new_tmp to consume them—matching translate_list's
   pattern. Also removed an erroneous LocalTee that left an orphan
   value on the stack. Affected: src_byte_len, dst_byte_len,
   cur_src_ptr, cur_dst_ptr.

2. Pointer advancement: after value translation, the pointer still
   points at the value start. The code only advanced by trailing
   padding instead of value_size + trailing_padding, causing every
   loop iteration to re-read the same memory.

Also fixes entry layout to use proper record alignment rules (entry
align = max(key_align, value_align), value at aligned offset).
Val::Map already holds Vec<(Val, Val)> which derefs to &[(Val, Val)],
matching lower_map's signature directly. The intermediate Vec allocation
and deep clone of every key/value pair was redundant.
@yordis
Copy link
Author

yordis commented Feb 27, 2026

@alexcrichton addressed the comments, about the sharing stuff, feedback welcome there, I am not sure if it is the best way or not; not sure what you had in mind

@yordis yordis requested a review from alexcrichton February 27, 2026 06:05
let tuple_alignment = key_abi.align32.max(value_abi.align32);
let tuple_size = usize::try_from(offset).unwrap();
let tuple_size = (tuple_size + usize::try_from(tuple_alignment)? - 1)
& !(usize::try_from(tuple_alignment)? - 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading over crates/environ/src/component/types.rs again, I think it would be best to store the "tuple ABI" inside of the TypeMap structure. That way it wouldn't need to be caluclated at runtime either here or in the typed.rs case and it would only be calculated in one location.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might also be a good location to store a value_offset: u32 value which is the offset from the start of the tuple to the value, so it also wouldn't need to be recomputed here for example.

Comment on lines +2384 to +2428
#[cfg(feature = "std")]
unsafe impl<K, V> Lift for std::collections::HashMap<K, V>
where
K: Lift + Eq + Hash,
V: Lift,
{
fn linear_lift_from_flat(
cx: &mut LiftContext<'_>,
ty: InterfaceType,
src: &Self::Lower,
) -> Result<Self> {
let (key_ty, value_ty) = match ty {
InterfaceType::Map(i) => {
let m = &cx.types[i];
(m.key, m.value)
}
_ => bad_type_info(),
};
// FIXME(#4311): needs memory64 treatment
let ptr = src[0].get_u32();
let len = src[1].get_u32();
let (ptr, len) = (usize::try_from(ptr)?, usize::try_from(len)?);
lift_std_map(cx, key_ty, value_ty, ptr, len)
}

fn linear_lift_from_memory(
cx: &mut LiftContext<'_>,
ty: InterfaceType,
bytes: &[u8],
) -> Result<Self> {
let (key_ty, value_ty) = match ty {
InterfaceType::Map(i) => {
let m = &cx.types[i];
(m.key, m.value)
}
_ => bad_type_info(),
};
debug_assert!((bytes.as_ptr() as usize) % (Self::ALIGN32 as usize) == 0);
// FIXME(#4311): needs memory64 treatment
let ptr = u32::from_le_bytes(bytes[..4].try_into().unwrap());
let len = u32::from_le_bytes(bytes[4..].try_into().unwrap());
let (ptr, len) = (usize::try_from(ptr)?, usize::try_from(len)?);
lift_std_map(cx, key_ty, value_ty, ptr, len)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For lifting here what I'd recommend is that since this is implemented in terms of the fallible hash map this can delegate to the fallible version, then convert the fallible version to the std version.

Fore xample linear_lift_from_flat here would call HashMap::linear_lift_form_flat(...), and if successful, would convert that to std::collections::HashMap

Comment on lines +2323 to +2334
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
match ty {
InterfaceType::Map(t) => {
let map_ty = &types.types[*t];
K::typecheck(&map_ty.key, types)?;
V::typecheck(&map_ty.value, types)?;
Ok(())
}
other => bail!("expected `map` found `{}`", desc(other)),
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a good place to delegate to HashMap::typecheck(...) as well for example as opposed to duplicating the method body here.

Comment on lines +2342 to +2381
fn linear_lower_to_flat<U>(
&self,
cx: &mut LowerContext<'_, U>,
ty: InterfaceType,
dst: &mut MaybeUninit<[ValRaw; 2]>,
) -> Result<()> {
let (key_ty, value_ty) = match ty {
InterfaceType::Map(i) => {
let m = &cx.types[i];
(m.key, m.value)
}
_ => bad_type_info(),
};
let (ptr, len) = lower_map_iter(cx, key_ty, value_ty, self.len(), self.iter())?;
// See "WRITEPTR64" above for why this is always storing a 64-bit
// integer.
map_maybe_uninit!(dst[0]).write(ValRaw::i64(ptr as i64));
map_maybe_uninit!(dst[1]).write(ValRaw::i64(len as i64));
Ok(())
}

fn linear_lower_to_memory<U>(
&self,
cx: &mut LowerContext<'_, U>,
ty: InterfaceType,
offset: usize,
) -> Result<()> {
let (key_ty, value_ty) = match ty {
InterfaceType::Map(i) => {
let m = &cx.types[i];
(m.key, m.value)
}
_ => bad_type_info(),
};
debug_assert!(offset % (Self::ALIGN32 as usize) == 0);
let (ptr, len) = lower_map_iter(cx, key_ty, value_ty, self.len(), self.iter())?;
*cx.get(offset + 0) = u32::try_from(ptr).unwrap().to_le_bytes();
*cx.get(offset + 4) = u32::try_from(len).unwrap().to_le_bytes();
Ok(())
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I might recommend for deduplicating this with the above HashMap implementation is to have two helpers on top of lower_map_iter, specifically linear_lower_map_to_memory and linear_lower_map_to_flat. That would have the boilerplate here (plus a generic iterator) and that way the hash map lower implementations would be one-liners to the shared functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fuzzing Issues related to our fuzzing infrastructure wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:c-api Issues pertaining to the C API. wasmtime:config Issues related to the configuration of Wasmtime wizer Issues related to Wizer snapshotting, pre-initialization, and the `wasmtime wizer` subcommand

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants