rust-mmap icon indicating copy to clipboard operation
rust-mmap copied to clipboard

MemoryMap fails on ARM

Open geraldstanje opened this issue 10 years ago • 10 comments

Hi,

I ported this c app (http://ideone.com/AbImAr) to rust but face an issue with MemoryMap.

The rust app is cross compiled on my ubuntu linux for the ARM:

# file /sbin/init
/sbin/init: ELF 32-bit LSB  shared object, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=289a0061854bb8d2f7ed8286c0a18aa1207141a5, stripped

Rust was built from source according to https://github.com/japaric/ruststrap/blob/master/1-how-to-cross-compile.md

The rust app is built using cargo:

$ rustc --version
rustc 1.0.0-dev (a59de37e9 2015-05-13) (built 2015-5-14)

$ cargo build --target=arm-unkown-linux-gnuabihf

The rust app crashes without any error and cuts off my ssh connection to the device... what happens could be: in best case the kernel crashes, in worst case a disk buffer is overwritten and face a silent filesystem corruption...

Here the code code of the rust app (which crashes when executing the unsafe part): http://is.gd/PT0cwM

Gdb output: why does it report "Cannot access memory"? Can i trust this info to be correct?

let mmap = MemoryMap::new(size, mmap_opts).unwrap();
(gdb) next
42      let data = mmap.data();
(gdb) print mmap
$1 = {data = 0xb5c00000 <error: Cannot access memory at address 0xb5c00000>, len = 8388608, kind = {{RUST$ENUM$DISR = MapFile, __0 = 0x0}, {
      RUST$ENUM$DISR = MapFile}}}

Strace for the rust app (to get the strace, the unsafe part of the code was disabled): http://ideone.com/hNIYMw

geraldstanje avatar May 17 '15 15:05 geraldstanje

@geraldstanje It seems that the libc isn't dividing the offset by 4096 before calling mmap2. Try specifying 0x40000 as the offset (just a divide by 0x1000).

emberian avatar May 17 '15 17:05 emberian

ok. and the 0x30 in the unsafe part?

geraldstanje avatar May 17 '15 17:05 geraldstanje

strace for the c app: http://ideone.com/jiKlfK strace for the modified rust app (with offset 0x40000): http://ideone.com/wGOkN0

when i look at mmap2 in the c version of the app... i can see that 0x40000000 is correct!?

geraldstanje avatar May 17 '15 17:05 geraldstanje

No, the 0x30 is fine.

emberian avatar May 17 '15 18:05 emberian

did you see both strace logs?

geraldstanje avatar May 17 '15 18:05 geraldstanje

i checked the addresses written to for both in C and rust:

the address i write to in C is: 0xb6e3f030

void toggle_led(const uint32_t led_pin) {
    printf("%p", g_hk_fpga_LED_mem);
    *g_hk_fpga_LED_mem ^= 1 << led_pin;
}

the address i write to in rust is: 0xb5c00030

fn toggle(data: *mut u8, led_pin: u32) {
    unsafe {
        let led_mem: *mut u32 = (data.offset(0x30) as *mut u32);
        println!("{:?}", led_mem);
    }
}

when you look at both strace logs (offset: 0x40000000, offset: 0x40000) ... mmap2 maps in both cases to address 0xb5c00000 ... why?

geraldstanje avatar May 17 '15 23:05 geraldstanje

does *mut T::offset() gives you the address plus the offset, or just the offset (the difference itself, regardless of the base pointer). You might want to check that.

Also, as someone on IRC said, try using the libc's mmap (using the ffi, extern {}, etc) to see what happens. I cannot really say anything else at the moment…

navaati avatar May 17 '15 23:05 navaati

where do you see a function *mut T::offset() within rust-mmap? if you mean the address of data:

fn toggle(data: *mut u8, led_pin: u32) {
        unsafe {
                let led_mem: *mut u8 = data;
                println!("{:?}", led_mem);
        }
}

output: 0xb5c00000

@cmr any idea to the latest infos?

geraldstanje avatar May 18 '15 19:05 geraldstanje

No, not within rst-mmap, within the standard lib, I'm talking about the one you use here : data.offset(0x30).

navaati avatar May 18 '15 19:05 navaati

@navaati ... thats the output:

output: 0xb5c00030

fn toggle(data: *mut u8, led_pin: u32) {
    unsafe {
        let led_mem: *mut u8 = data.offset(0x30);
        println!("{:?}", led_mem);
    }
}

geraldstanje avatar May 18 '15 21:05 geraldstanje