You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
The text was updated successfully, but these errors were encountered:
lwz23
changed the title
unsoundness in write_f32
unsoundness in write_f32 groups
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.
rustarok/common/src/packets/mod.rs
Line 167 in 7ea7abf
rustarok/common/src/packets/mod.rs
Line 179 in 7ea7abf
Poc:
result:
The text was updated successfully, but these errors were encountered: