Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use &'static [PReg] instead of Vec<PReg> #208

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

JonasKruckenberg
Copy link
Contributor

@JonasKruckenberg JonasKruckenberg commented Jan 13, 2025

This PR makes MachineEnv use &'static [PReg] instead of Vec<PReg> to make it possible to place MachineEnv into statics as part of bytecodealliance/wasmtime#8489.

Edit: as it turns out serialization makes this not quite as trivial, serde can't deserialize into slices (had forgotten that) so I introduced a new SerializableMachineEnv type in the spirit of SerializableFunction to let consumers still serialize a machine env if required.

related: #177.

env.preferred_regs_by_class[RegClass::Int as usize].clone(),
env.preferred_regs_by_class[RegClass::Float as usize].clone(),
env.preferred_regs_by_class[RegClass::Vector as usize].clone(),
Vec::from(env.preferred_regs_by_class[RegClass::Int as usize]),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

don't love this, open for better ideas

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe use .to_owned() instead of Vec::from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm, yeah doesn't really change anything about it but might look nicer

src/lib.rs Outdated Show resolved Hide resolved
@JonasKruckenberg
Copy link
Contributor Author

Hmm seems libfuzz doesn't like Vec::leak?

@JonasKruckenberg
Copy link
Contributor Author

Hmm seems libfuzz doesn't like Vec::leak?

interestingly I locally can't replicate the CI fail

Copy link
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

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

A few comments below -- thanks for working on this! I think the general shape is right, I just have some thoughts on the way the fuzzing testcase generation is working (we need to avoid leaks).

/// will have very few of and generally want to keep around for their entire lifetime.
/// In order to make static initialization easier the registers lists are static slices instead
/// of `Vec`s. If your use case depends on dynamically creating the registers lists, consider
/// [`Vec::leak`].
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer not to give this suggestion in the doc-comment: since it's parameterized on a lifetime (rather than hardcoding 'static), it's possible to build a MachineEnv dynamically that is a borrowed view on owned Vecs, basically giving the same capabilities as before. This is fairly idiomatic in Rust, so I don't think we need a special note or recommendation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yeah good catch, that was from before I allowed any lifetimes. This notice doesn't make much sense anymore

]
})
.collect();
let fixed_stack_slots = Vec::leak(
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to avoid Vec::leak here: fuzzing will continually generate new functions with machine envs, and we cannot allocate indefinitely. Can you build a static initializer, since the range (32..63) is static?

.collect(),
vec![],
vec![],
Vec::leak(
Copy link
Member

Choose a reason for hiding this comment

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

We definitely can't leak while fuzzing -- can we instead have a few static MachineEnvs in scope, and pick one of them? We don't necessarily need to fuzz with arbitrary no_of_regs, just with a few values that are interesting -- the two extremes of its range and a few in the middle, for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants