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

Remove panics in Deref impl of QueueWriteBufferView and BufferViewMut #3336

Merged
merged 13 commits into from
Jan 12, 2023

Conversation

botahamec
Copy link
Contributor

@botahamec botahamec commented Dec 27, 2022

Checklist

  • Run cargo clippy.
  • Run RUSTFLAGS=--cfg=web_sys_unstable_apis cargo clippy --target wasm32-unknown-unknown if applicable.
  • Add change to CHANGELOG.md. See simple instructions inside file.

Connections
closes #3134

Description

According to the documentation for Queue::write_buffer_with:

The returned value can be dereferenced to a &mut [u8]; dereferencing it to a &[u8] panics!

According to the documentation for Deref:

this trait should never fail. Failure during dereferencing can be extremely confusing when Deref is invoked implicitly.

A similar problem appears in BufferViewMut.

The way I solved this problem was to get rid of the implementations of Deref and DerefMut for each, implement AsMut for QueueWriteBufferView, and add the read method to BufferViewMut

Testing
I ran cargo nextest run.

@nical
Copy link
Contributor

nical commented Jan 3, 2023

Sorry I'm chiming in a bit late, didn't see #3134.

Considering we can read from the &mut deref, I think that we should just keep things simple, remove the panic and simply make sure to document the behavior well (reading the bytes may be slow and won't yield the real content of the buffer).
I'm not fond of the panic in the first place since it solves at best half of the problem it tries to solve.

Thoughts @teoxoy @cwfitzgerald ?

@teoxoy
Copy link
Member

teoxoy commented Jan 4, 2023

Considering we can read from the &mut deref, I think that we should just keep things simple, remove the panic and simply make sure to document the behavior well (reading the bytes may be slow and won't yield the real content of the buffer).

I'd say that this is quite difficult to document since Deref/DerefMut happen implicitly.
This PR looks good to me, makes things more idiomatic.

@nical are you concerned about the ergonomics being slightly worse?

@cwfitzgerald
Copy link
Member

I am having trouble coming to a conclusion of which option I actually like more. Deref* makes it really easy to use - lets it slot into various apis that take anything that impls Deref (notably Encase). And it's not like we don't have other giant performance footguns that you can easily write. At the end of the day, as long as the panic goes away (as that is notably unidomatic. I think we'll be fine.

@codecov-commenter
Copy link

codecov-commenter commented Jan 4, 2023

Codecov Report

Merging #3336 (003d998) into master (48fbb92) will decrease coverage by 0.20%.
The diff coverage is 8.33%.

@@            Coverage Diff             @@
##           master    #3336      +/-   ##
==========================================
- Coverage   64.70%   64.49%   -0.21%     
==========================================
  Files          66       86      +20     
  Lines       37336    42721    +5385     
==========================================
+ Hits        24159    27554    +3395     
- Misses      13177    15167    +1990     
Impacted Files Coverage Δ
wgpu/src/lib.rs 51.01% <8.33%> (-0.33%) ⬇️
wgpu-core/src/hub.rs 60.67% <0.00%> (-3.91%) ⬇️
wgpu-core/src/instance.rs 65.67% <0.00%> (-2.04%) ⬇️
wgpu-hal/src/dx12/conv.rs 84.22% <0.00%> (ø)
wgpu-hal/src/dx12/mod.rs 27.27% <0.00%> (ø)
wgpu-hal/src/dx12/command.rs 73.46% <0.00%> (ø)
wgpu-hal/src/dx11/adapter.rs 0.00% <0.00%> (ø)
wgpu-hal/src/dx12/adapter.rs 81.38% <0.00%> (ø)
wgpu-hal/src/dx11/command.rs 0.00% <0.00%> (ø)
... and 23 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@nical
Copy link
Contributor

nical commented Jan 5, 2023

In my opinion it's irrelevant whether someone ends up calling Deref or DerefMut (explicitly or implicitly), what matters is that they don't read form the (& or &mut) reference. AsMut has the same problem, it doesn't prevent or discourage reading, it just forces you to type "as_mut()" before doing so. As a result we still need to document that reading has likely undesired effects (potentially slow uncached memory reads and not providing access to the real buffer data).

Deref could be useful, here. We should not panic because it is not idiomatic, but we can log a performance warning. If we remove that hook, it will be as easy for accidental reads to happen but we won'be able to put a red flag on some of them.

@nical are you concerned about the ergonomics being slightly worse?

Yes-ish. My primary concern is that I don't think the way we are trying to paper over the issue helps (I don't think Rust can let us express something that would be helpful for this). The ergonomics cost is not something I'll lose sleep over but at the same time since I'm unconvinced that it helps in a meaningful way, my preference would be to keep things simple.

The problem isn't important enough to press further so if my arguments have not swayed you, don't feel bad about merging this as is.

@teoxoy
Copy link
Member

teoxoy commented Jan 5, 2023

I'm in the same boat as @cwfitzgerald (don't think there is a satisfactory solution).

We should not panic because it is not idiomatic, but we can log a performance warning.

This sounds better to me if we keep the Deref/DerefMut; log a warning instead of panicking.

@botahamec how do you feel about keeping the current API and logging a warning instead of panicking?

@botahamec
Copy link
Contributor Author

@teoxoy That works for me. I assume that's referring to QueueWriteBufferView. Should BufferViewMut stay the way I currently have it in this PR?

@nical
Copy link
Contributor

nical commented Jan 6, 2023

I'd say let them both have the familiar Deref/DerefMut API.

@botahamec
Copy link
Contributor Author

@nical Does that make sense for BufferViewMut? Currently Deref panics only if self.readable is false. Is it similar to QueueWriteBufferView where it will just give nonsense data if self.readable is false?

@cwfitzgerald
Copy link
Member

I believe it will give something resembling the correct data, just may be horrendously slow to read from.

@nical
Copy link
Contributor

nical commented Jan 9, 2023

Yep, like @cwfitzgerald said. It makes sense for BufferViewMut for the same reason it makes sense for QueueWriteBufferView (For some definition of "makes sense" since it's hard to express what we really want to express in rust).

@botahamec
Copy link
Contributor Author

I added back Deref and DerefMut. Let me know if anyone has any other comments

@botahamec
Copy link
Contributor Author

botahamec commented Jan 12, 2023

Oh I guess readable isn't being used anymore on BufferViewMut. Should I remove it? I could also just add a method that uses it.

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

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

One nit, then g2g

wgpu/src/lib.rs Outdated Show resolved Hide resolved
@teoxoy
Copy link
Member

teoxoy commented Jan 12, 2023

What about logging out warnings? We don't seem to do that yet but was what we previously talked about.

@botahamec
Copy link
Contributor Author

@teoxoy Oh yes! You're right, I completely forgot. I'll get to that.

Copy link
Member

@teoxoy teoxoy left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@teoxoy
Copy link
Member

teoxoy commented Jan 12, 2023

@botahamec looks like CI is unhappy

@botahamec
Copy link
Contributor Author

Hmm, it's weird that my editor didn't show any problems. Maybe that's a sign to put less faith in rust-analyzer. Hopefully it works now.

@botahamec
Copy link
Contributor Author

Ok, coincidentally, cargo deny generated an error because of a recent vulnerability in tokio #3368

@teoxoy teoxoy changed the title Updated the API for QueueWriteBufferView and BufferViewMut Remove panics in Deref impl of QueueWriteBufferView and BufferViewMut Jan 12, 2023
@teoxoy teoxoy merged commit f2d2a0c into gfx-rs:master Jan 12, 2023
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.

QueueWriteBufferView is strange.
5 participants