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

Misaligned Pointer panic in the Paging Introduction post #1215

Closed
PitchBlackNights opened this issue Apr 26, 2023 · 8 comments
Closed

Misaligned Pointer panic in the Paging Introduction post #1215

PitchBlackNights opened this issue Apr 26, 2023 · 8 comments

Comments

@PitchBlackNights
Copy link

PitchBlackNights commented Apr 26, 2023

I don't know if this is very important, but it's been driving me crazy trying to fix it.

I'm almost at the end of the Introduction to Paging. I'm at the test to see if reading and writing to the code segment address will cause a paging fault.

When I run the binary, it spits out:

panicked at 'misaligned pointer dereference: address must be a multiple of 0x4 but is 0x204f63', src\main.rs:18:22

Here is the code in question (I'm guessing that it's really complaining about line 16, which is the first line included in this snippet):

let ptr = 0x204f63 as *mut u32;

unsafe { let x = *ptr; }
println!("Read Worked");

unsafe { *ptr = 42; }
println!("Write Worked");

I've gotten this error on previous uses of 0xdeadbeaf as a virtual address, so I would always just swap those out for unnecessarily large addresses like 0x4444444444.

I did look to see if anyone else was having this error, and I did find one person.

@TeeJaey
Copy link

TeeJaey commented Apr 30, 2023

I would like to comment that I am also running into this issue.

I so far always swapped 0xdeadbeaf for 0xdeadbea0 since that is a multiple of 0x4.

While that works, I am worried I might run into bigger problems later down the line.

@carloalbertogiordano
Copy link

Hi, maybe the read_unaligned and the write_unaligned could be used in this case. I have not encountered this error so far.
Here are some links:
https://doc.rust-lang.org/std/ptr/fn.read_unaligned.html
https://doc.rust-lang.org/std/ptr/fn.write_unaligned.html

@PitchBlackNights
Copy link
Author

PitchBlackNights commented May 4, 2023

@carloalbertogiordano
Thanks, but those can't be used as the os doesn't contain, or support, the std crate

@bjorn3
Copy link
Contributor

bjorn3 commented May 4, 2023

These functions are re-exports from libcore, which is supported. So you can write core::ptr::read_unaligned and core::ptr::write_unaligned.

@PitchBlackNights
Copy link
Author

Okay, thanks @bjorn3 ! I haven't tried it, but I think it will work. I'll leave this open for now though, as it should be working without read/write_unaligned

@cubed-guy
Copy link

@bjorn3 Can confirm. core::ptr::write_unaligned does work.
But, any idea as to why the standard syntax doesn't work anymore?

@bjorn3
Copy link
Contributor

bjorn3 commented May 9, 2023

Dereferencing unaligned pointers is UB. Previously if you were lucky the compiler wouldn't miscompile it, but to catch this UB, rustc recently added a compiler pass when debug assertions are enabled which adds code to check on every pointer dereference if the pointer is aligned or not and if not aborts. This helps finding this UB.

@PitchBlackNights
Copy link
Author

Okay, so that's just how rustc compiles it now. Thanks!

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

5 participants