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

unsoundness in write_f32 groups #25

Open
lwz23 opened this issue Jan 6, 2025 · 1 comment
Open

unsoundness in write_f32 groups #25

lwz23 opened this issue Jan 6, 2025 · 1 comment

Comments

@lwz23
Copy link

lwz23 commented Jan 6, 2025

I think there exist a unaligned problem in this project, because they are pub function, I assume other mod can control them directly.

pub fn write_f32(&mut self, value: f32) {

pub fn write_i32(&mut self, value: i32) {

Poc:

pub struct SocketBuffer {
    buf: [u8; 2048],
    /// pointer at which the OS writes/reads the data during send/recv
    os_pointer: usize,
    /// pointer at which the application writes/reads the data during send/recv
    user_pointer: usize,
}

impl SocketBuffer {
    pub fn write_f32(&mut self, value: f32) {
        unsafe {
            *(self.buf.as_mut_ptr().offset(self.user_pointer as isize) as *mut f32) = value;
        }
        self.user_pointer += 4;
    }
}

fn main() {

    let mut buffer = SocketBuffer {
        buf: [0; 2048],
        os_pointer: 0,
        user_pointer: 1, // unaligned
    };

    buffer.write_f32(42.0);


}

result:

PS E:\Github\lwz> cargo +nightly miri run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `C:\Users\ROG\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\bin\cargo-miri.exe runner target\miri\x86_64-pc-windows-msvc\debug\lwz.exe`
warning: field `os_pointer` is never read
 --> src\main.rs:4:5
  |
1 | pub struct SocketBuffer {
  |            ------------ field in this struct
...
4 |     os_pointer: usize,
  |     ^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

error: Undefined Behavior: accessing memory based on pointer with alignment 1, but alignment 4 is required
  --> src\main.rs:12:13
   |
12 | ...   *(self.buf.as_mut_ptr().offset(self.user_pointer as isize) as *mut f32) = val...
   |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ accessing memory based on pointer with alignment 1, but alignment 4 is required
   |
   = 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: BACKTRACE:
   = note: inside `SocketBuffer::write_f32` at src\main.rs:12:13: 12:92
note: inside `main`
  --> src\main.rs:27:5
   |
27 |     buffer.write_f32(42.0);
   |     ^^^^^^^^^^^^^^^^^^^^^^

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error; 1 warning emitted

error: process didn't exit successfully: `C:\Users\ROG\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\bin\cargo-miri.exe runner target\miri\x86_64-pc-windows-msvc\debug\lwz.exe` (exit code: 1)
PS E:\Github\lwz> cargo +nightly miri run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `C:\Users\ROG\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\bin\cargo-miri.exe runner target\miri\x86_64-pc-windows-msvc\debug\lwz.exe`
warning: field `os_pointer` is never read
 --> src\main.rs:4:5
  |
1 | pub struct SocketBuffer {
  |            ------------ field in this struct
...
4 |     os_pointer: usize,
  |     ^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

error: Undefined Behavior: accessing memory based on pointer with alignment 1, but alignment 4 is required
  --> src\main.rs:12:13
   |
12 | ...   *(self.buf.as_mut_ptr().offset(self.user_pointer as isize) as *mut f32) = val...
   |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ accessing memory based on pointer with alignment 1, but alignment 4 is required
   |
   = 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: BACKTRACE:
   = note: inside `SocketBuffer::write_f32` at src\main.rs:12:13: 12:92
note: inside `main`
  --> src\main.rs:26:5
   |
26 |     buffer.write_f32(42.0);
   |     ^^^^^^^^^^^^^^^^^^^^^^

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error; 1 warning emitted

error: process didn't exit successfully: `C:\Users\ROG\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\bin\cargo-miri.exe runner target\miri\x86_64-pc-windows-msvc\debug\lwz.exe` (exit code: 1)
PS E:\Github\lwz> 
@lwz23 lwz23 changed the title unsoundness in write_f32 unsoundness in write_f32 groups Jan 6, 2025
@lwz23
Copy link
Author

lwz23 commented Jan 6, 2025

same for

*(self.buf.as_mut_ptr().offset(self.user_pointer as isize) as *mut i32) = value;

*(self.buf.as_mut_ptr().offset(self.user_pointer as isize) as *mut u32) = value;

*(self.buf.as_mut_ptr().offset(self.user_pointer as isize) as *mut u64) = value;

*(self.buf.as_mut_ptr().offset(self.user_pointer as isize) as *mut u16) = value;

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

No branches or pull requests

1 participant