1

When I tried to access the address mmap returned, a Bus error is occured.

My code is below:

ftruncate(fd, shared_size);
addr = mmap(shared_start, shared_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0);

shared_size == 256*1024*1024

shared_start == 401000000000 (I used flag MAP_FIXED)

ftruncate the file to 256M.

-rw-r--r--    1 root     0         256.0M Mar  4 03:47 mem.alloc

There is nothing wrong when calling mmap, and not all of address range is not allowed to access. From the gdb information below, we can see, the address 0x40100f11ff00 is not allowed, but address 0x40100fe00000 is allowed:

(gdb) p *((char *)addr+0xf11ff00)
Cannot access memory at address 0x40100f11ff00
(gdb) p *((char *)addr+0xfe1ff00)
Cannot access memory at address 0x40100fe1ff00
(gdb) p *((char *)addr+0xfe00000)
$17 = 0 '\000'

From maps information below, we can see the addresses I accessed above are all within the range of mmap address:

 0x401000000000     0x401010000000 0x10000000        0x0 /dev/mem.alloc

However, when writing these inaccessible addresses, a bus error occurs: Program received signal SIGBUS, Bus error.

PS.When reducing shared_size from 256M to 128M, there is no issue.

Suri2020
  • 21
  • 3
  • You may have tried these already: (1) _SIGBUS_ suggests there is something funky about the file... though I don't know what that could be. I'd try creating a new `mem.alloc1`, making sure that it is exactly 256MiB bytes (eg: `dd count=256K bs=1K if=/dev/zero of=/dev/mem.alloc1`), and trying without the `ftruncate()`. (2) If that doesn't help, I'd try without `MAP_FIXED` -- just in case. – Chris Hall Mar 04 '20 at 11:02
  • Why do you _need_ `MAP_FIXED`? 0x40100f11ff00 is _not_ page aligned. – Craig Estey Mar 04 '20 at 15:20
  • 0x401010000000 is page aligned, this is my memory bundary, thanks~~ I have worked out it, because the space for dev mount is too small.... – Suri2020 Mar 05 '20 at 01:14

1 Answers1

1

I have fixed it. This is a problem that can be easily overlooked. The space mount for dev is too small...so....you known....

Suri2020
  • 21
  • 3