-
Notifications
You must be signed in to change notification settings - Fork 10
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
Document missing safe abstractions #34
Comments
I think part of this is that you're supposed to reuse a buffer, not just make it, read to it, and throw it away. perhaps their code could be made to reuse buffers more often and average out the cost of the initial zero initialization? |
If initialization is done only once it comes without any overhead because the allocator can request already-zeroed memory from the OS. It's the subsequent initializations that require |
That only holds if no one else has allocated and deallocated before you create your vec. With how box happy a lot of Rust code sadly is, you cannot assume that. |
This is not always the case. Not every single fresh allocation comes directly from the OS (that’s exactly what allocators are supposed to avoid, as it’s very slow). |
There are several crates of this nature. |
Oh, another one! There are also: |
Another use case for fixed-capacity Vec-like memory view - flate2 needs it to become 100% safe. See #32 |
Regarding the "fixed-capacity Vec" approach: |
Being able to write uninit mem is must have for any performance oriented code |
@DoumanAsh the problem does not lie in writing to a
|
I do not see a particular problem with current Read and it would be hard to remove something that is 'broken' due to backward compatibility Well you can extend Read to have something like |
The problem (IIUC) is that a caller to a generic Read implementation cannot pass uninitialised memory to be filled in, as all the relevant methods are not marked unsafe (or don’t accept a structure capable of preventing uninit mem accesses like Shnatsel is describing). So to be safe everyone has to pass initialised memory which wastes some cycles. |
Yeah, what we want is |
As someone who has raised the topic of an improved |
I'm posting here only under the "You do not have to be an
(I imagine too that, if one person does this who cares enough about safe code to warn about it and to provide a safe alternative, several more people do this who don't.) |
The line https://github.com/gamozolabs/fzero_fuzzer/blob/6fe91bcd87af1db71472f4b549e66ea273811576/src/main.rs#L302 can wrap and overflow, especially on |
the element count of a slice can't actually exceed isize::MAX in practice, because llvm's IR operation for indexing uses signed values. though this is not well documented at the rust level. |
I don't know if this is still an issue, but |
Some crates, e.g. reqwest (see #5) clearly indicate the need for better safe abstractions, as their logic cannot be expressed in terms of the existing ones.
The worst offender by far is the
Read
trait which requires an initialized slice to write to, but initializing a slice is costly, so people just throw uninitialized slices at it and hope for the best. This unsafety could be encapsulated by appending to a Vec-like fixed-size structure.Another such case is rust-lang/rfcs#2714
I'm thinking of filing issues on https://github.com/rust-lang/rust/ and then linking to them from some markdown file or from this issue. Thoughts?
The text was updated successfully, but these errors were encountered: