-
Notifications
You must be signed in to change notification settings - Fork 14k
Remove initialized-bytes tracking from BorrowedBuf and BorrowedCursor
#148937
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
base: main
Are you sure you want to change the base?
Remove initialized-bytes tracking from BorrowedBuf and BorrowedCursor
#148937
Conversation
…sor` As discussed extensively in libs-api, the initialized-bytes tracking primarily benefits calls to `read_buf` that end up initializing the buffer and calling `read`, at the expense of calls to `read_buf` that *don't* need to initialize the buffer. Essentially, this optimizes for the past at the expense of the future. If people observe performance issues using `read_buf` (or something that calls it) with a given `Read` impl, they can fix those performance issues by implementing `read_buf` for that `Read`. Update the documentation to stop talking about initialized-but-unfilled bytes. Remove all functions that just deal with those bytes and their tracking, and remove usage of those methods. Make `BorrowedCursor::as_mut` safe, as it's no longer a safety problem if the caller uninitializes bytes. Remove `BorrowedCursor::advance` as there's no longer a safe case for advancing within initialized-but-unfilled bytes. Rename `BorrowedCursor::advance_unchecked` to `advance`. Update tests.
7de0924 to
d131ff2
Compare
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
As-is, this is unsound with the combination of the existing reproducer with Miri output#![feature(core_io_borrowed_buf)]
#![forbid(unsafe_code)]
use core::io::BorrowedBuf;
use std::mem::MaybeUninit;
fn main() {
let mut bytes = [0; 32];
let mut buf = BorrowedBuf::from(&mut bytes[..]);
buf.unfilled().as_mut()[1] = MaybeUninit::uninit();
dbg!(bytes);
}$ cargo +dev-compiler-2-stage1 miri run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
Running `/home/zachary/opt_mount/zachary/Programming/rust-compiler-2/build/x86_64-unknown-linux-gnu/stage1/bin/cargo-miri runner /home/zachary/opt_mount/zachary/cargo-target/miri/x86_64-unknown-linux-gnu/debug/borrowedbuf-thing`
error: Undefined Behavior: constructing invalid value at [1]: encountered uninitialized memory, but expected an integer
--> src/main.rs:10:5
|
10 | dbg!(bytes);
| ^^^^^^^^^^^ Undefined Behavior occurred here
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
error: aborting due to 1 previous errorIf we allow making a |
|
@zachs18 Nice catch: you are correct that we need to ensure that we never de-initialize the buffer, which means that we can't expose
|
As discussed extensively in libs-api, the initialized-bytes tracking primarily benefits calls to
read_bufthat end up initializing the buffer and callingread, at the expense of calls toread_bufthat don't need to initialize the buffer. Essentially, this optimizes for the past at the expense of the future. If people observe performance issues usingread_buf(or something that calls it) with a givenReadimpl, they can fix those performance issues by implementingread_buffor thatRead.Update the documentation to stop talking about initialized-but-unfilled bytes.
Remove all functions that just deal with those bytes and their tracking, and remove usage of those methods.
Make
BorrowedCursor::as_mutsafe, as it's no longer a safety problem if the caller uninitializes bytes.Remove
BorrowedCursor::advanceas there's no longer a safe case for advancing within initialized-but-unfilled bytes. RenameBorrowedCursor::advance_uncheckedtoadvance.Update tests.
r? @Amanieu