Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

MemoryMap fails on ARM #4

Open
geraldstanje opened this issue May 17, 2015 · 10 comments
Open

MemoryMap fails on ARM #4

geraldstanje opened this issue May 17, 2015 · 10 comments

Comments

@geraldstanje
Copy link

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

@emberian
Copy link

@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).

@geraldstanje
Copy link
Author

ok. and the 0x30 in the unsafe part?

@geraldstanje
Copy link
Author

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!?

@emberian
Copy link

No, the 0x30 is fine.

@geraldstanje
Copy link
Author

did you see both strace logs?

@geraldstanje
Copy link
Author

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?

@navaati
Copy link

navaati commented May 17, 2015

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…

@geraldstanje
Copy link
Author

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?

@navaati
Copy link

navaati commented May 18, 2015

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

@geraldstanje
Copy link
Author

@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);
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants