Aubin
Aubin

Reputation: 14873

Bus error after mmap success

Context

Code

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>

int main() {
   int fd = open( "/tmp/mapped.bin", O_CREAT|O_RDWR|O_SYNC, 0666 );
   perror("open");
   unsigned char * dst =
      (unsigned char *)mmap(
         NULL, 64*1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
   perror("mmap");
   static unsigned char src[] = { 1, 3, 5, 7 };
   printf("memcpy( %p, {%d,%d,%d,%d}, %zd )\n", dst, src[0], src[1], src[2], src[3], sizeof(src));
   memcpy(dst, src, sizeof(src));
   perror("memcpy");
   return 0;
}

Production

$ gcc -W -Wall -std=c11 src/org/hpms/filemap/BusError.c -o bin/BusError
(no warning, no error)

Execution

$ bin/BusError
open: Success
mmap: Success
memcpy( 0x7f51291fe000, {1,3,5,7}, 4 )
Erreur du bus

gdb

(gdb) run
Starting program: /home/aubin/Dev/Java/2017/org.hpms.filemap/bin/BusError
open: Success
mmap: Success
memcpy( 0x7ffff7fe6000, {1,3,5,7}, 4 )

Program received signal SIGBUS, Bus error.
0x00000000004005ac in main () at src/org/hpms/filemap/BusError.c:15
15     memcpy(dst, src, sizeof(src));
(gdb)

Upvotes: 4

Views: 2579

Answers (1)

Petr Skocik
Petr Skocik

Reputation: 60145

You'll get SIGBUS if you access the mmaped memory outside the file. In other words, if you want to write 4 bytes, the file needs to be at least 4 bytes large.

In your example, you could solve the problem with ftruncate:

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>

int main() {
   int fd = open( "/tmp/mapped.bin", O_CREAT|O_RDWR|O_SYNC, 0666 );
   perror("open");
   unsigned char * dst =
      (unsigned char *)mmap(
         NULL, 64*1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
   perror("mmap");
   static unsigned char src[] = { 1, 3, 5, 7 };
   ftruncate(fd, sizeof src);
   perror("ftruncate");
   printf("memcpy( %p, {%d,%d,%d,%d}, %zd )\n", dst, src[0], src[1], src[2], src[3], sizeof(src));
   memcpy(dst, src, sizeof(src));
   perror("memcpy");
   return 0;
}

Upvotes: 4

Related Questions