Skip to contentSkip to conversation

mmap64() behaviour on 64-bit machines ?

DISCUSSION Badari Pulavarty 2 participants 2 messages Nov 22, 2005

lore
Share & tools
lore
#0 lore raw

Hi,

I am confused on the behaviour of mmap64() on 64-bit machines. When I run following simple program, I get SIGSEGV in memset(). But if I replace mmap64() with mmap() - it works fine. I verified this on ppc64, em64t, amd64.

Whats happening here ? Any clues ?

Thanks,
Badari

[root@localhost ~]# ./tst junk
Segmentation fault

strace output:
...

open("junk", O_RDWR|O_CREAT, 0644) = 3
write(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 4096) = 4096
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0) = 0x2aaaaaaac000
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
Process 26736 detached

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

char buf[4096];
int main(int argc, char* argv[])
{
int fd;
char *start;

  if (argc < 2) {
	printf("Usage: %s <filename>\n", argv[0]);
	exit(1);
  }
  fd = open(argv[1], O_RDWR | O_CREAT , 0644);
  if (fd < 0) {
	perror("open");
	exit(1);
  }
  if (write(fd, buf, 4096) < 0) {
	perror("write");
	exit(2);
  } 
  start = (char *)mmap64(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (!start) {
	perror("mmap64");
	exit(2);

}
memset(start, 0, 4096);
exit(0);
}

#1 lore raw
Quoted 8 lines · Badari Pulavarty

Hi,

I am confused on the behaviour of mmap64() on 64-bit machines. When I run following simple program, I get SIGSEGV in memset(). But if I replace mmap64() with mmap() - it works fine. I verified this on ppc64, em64t, amd64.

Whats happening here ? Any clues ?

If I fix all the compiler warnings, it works for me.

Also, mmap/mmap64 return MAP_FAILED on failure, not 0, but the compiler doesn't know to warn about that.

Signature · 1 line
Nicholas Miell <[email protected]>