When accessing mmap adress, signal SIGBUS was received


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.

asked on Stack Overflow Mar 4, 2020 by Suri2020

1 Answer


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

answered on Stack Overflow Mar 5, 2020 by Suri2020

User contributions licensed under CC BY-SA 3.0