Skip to content

Warn against clippy::cast_possible_truncation in Wasmtime#9209

Merged
alexcrichton merged 2 commits intobytecodealliance:mainfrom
alexcrichton:cast_possible_truncation
Sep 6, 2024
Merged

Warn against clippy::cast_possible_truncation in Wasmtime#9209
alexcrichton merged 2 commits intobytecodealliance:mainfrom
alexcrichton:cast_possible_truncation

Conversation

@alexcrichton
Copy link
Member

This commit explicitly enables the clippy::cast_possible_truncation lint in Clippy for just the wasmtime::runtime module. This does not enable it for the entire workspace since it's a very noisy lint and in general has a low signal value. For the domain that wasmtime::runtime is working in, however, this is a much more useful lint. We in general want to be very careful about casting between usize, u32, and u64 and the purpose of this module-targeted lint is to help with just that. I was inspired to do this after reading over #9206 where especially when refactoring code and changing types I think it would be useful to have locations flagged as "truncation may happen here" which previously weren't truncating.

The failure mode for this lint is that panics might be introduced where truncation is explicitly intended. Most of the time though this isn't actually desired so the more practical consequence of this lint is to probably slow down wasmtime ever so slightly and bloat it ever so slightly by having a few more checks in a few places. This is likely best addressed in a more comprehensive manner, however, rather than specifically for just this one case. This problem isn't unique to just casts, but to many other forms of .unwrap() for example.

This commit explicitly enables the `clippy::cast_possible_truncation`
lint in Clippy for just the `wasmtime::runtime` module. This does not
enable it for the entire workspace since it's a very noisy lint and in
general has a low signal value. For the domain that `wasmtime::runtime`
is working in, however, this is a much more useful lint. We in general
want to be very careful about casting between `usize`, `u32`, and `u64`
and the purpose of this module-targeted lint is to help with just that.
I was inspired to do this after reading over bytecodealliance#9206 where especially when
refactoring code and changing types I think it would be useful to have
locations flagged as "truncation may happen here" which previously
weren't truncating.

The failure mode for this lint is that panics might be introduced where
truncation is explicitly intended. Most of the time though this isn't
actually desired so the more practical consequence of this lint is to
probably slow down wasmtime ever so slightly and bloat it ever so
slightly by having a few more checks in a few places. This is likely
best addressed in a more comprehensive manner, however, rather than
specifically for just this one case. This problem isn't unique to just
casts, but to many other forms of `.unwrap()` for example.
@alexcrichton alexcrichton requested a review from a team as a code owner September 6, 2024 19:09
@alexcrichton alexcrichton requested review from elliottt and removed request for a team September 6, 2024 19:09
Copy link
Member

@elliottt elliottt left a comment

Choose a reason for hiding this comment

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

Looks great!

Comment on lines +67 to +70
.try_into()
.ok()
.and_then(|offset| object::from_bytes_mut::<U64Bytes<NE>>(&mut bytes[offset..]).ok())
.ok_or_else(|| anyhow!("invalid dwarf relocations"))?;
Copy link
Member

Choose a reason for hiding this comment

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

Does this indirect through Option because the error types are different between line 67 and 69? Otherwise, wouldn't a combination of and_then and unwrap_or_else work here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah this had to do with different error types across the two functions and I figured options was the easiest way to go here. This is code is on the older side in Wasmtime and is probably ripe for some improvement.

@alexcrichton alexcrichton added this pull request to the merge queue Sep 6, 2024
Merged via the queue into bytecodealliance:main with commit 0bce096 Sep 6, 2024
@alexcrichton alexcrichton deleted the cast_possible_truncation branch September 6, 2024 22:40
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.

2 participants