/* layout.c - where things live in a 32-bit AIX process. C89. */ #include #include #include #include #include #include #include #include #include #include #include int g_init = 42; int g_bss[1024]; static jmp_buf jb; static void onsegv(int s) { longjmp(jb, 1); } int main(int argc, char **argv) { int local = 0; char *m1, *m2; void *p; int i, n; int ids[32]; volatile int *zero = (volatile int *)0; setvbuf(stdout, 0, _IONBF, 0); printf("pagesize %ld\n", sysconf(_SC_PAGESIZE)); printf("main %p\n", (void *)main); printf("g_init %p\n", (void *)&g_init); printf("g_bss %p\n", (void *)g_bss); m1 = malloc(100); m2 = malloc(1 << 20); printf("malloc(100) %p\n", (void *)m1); printf("malloc(1M) %p\n", (void *)m2); printf("sbrk(0) %p\n", sbrk(0)); printf("local %p\n", (void *)&local); printf("argv %p\n", (void *)argv); printf("argv[0] %p\n", (void *)argv[0]); printf("printf %p\n", (void *)printf); printf("malloc %p\n", (void *)malloc); printf("errno %p\n", (void *)&errno); printf("environ %p\n", (void *)environ); /* reading and writing address 0 */ { struct sigaction sa; memset(&sa, 0, sizeof sa); sa.sa_handler = onsegv; sigaction(SIGSEGV, &sa, 0); } if (setjmp(jb) == 0) { int v = *zero; printf("read *0 ok, value %d\n", v); } else printf("read *0 SIGSEGV\n"); if (setjmp(jb) == 0) { *zero = 1; printf("write *0 ok\n"); } else printf("write *0 SIGSEGV\n"); if (setjmp(jb) == 0) { int v = *(volatile int *)0x1000; printf("read 0x1000 ok, value %d\n", v); } else printf("read 0x1000 SIGSEGV\n"); if (setjmp(jb) == 0) { int v = *(volatile int *)0x100000; printf("read 0x100000 ok, value %d\n", v); } else printf("read 0x100000 SIGSEGV\n"); signal(SIGSEGV, SIG_DFL); /* anonymous mmap */ p = mmap(0, 1 << 20, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); printf("mmap anon 1M %p%s\n", p, p == MAP_FAILED ? strerror(errno) : ""); p = mmap(0, 1 << 20, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); printf("mmap anon 1M %p%s\n", p, p == MAP_FAILED ? strerror(errno) : ""); /* shmat until it fails */ n = argc > 1 ? atoi(argv[1]) : 16; for (i = 0; i < n; i++) { ids[i] = shmget(IPC_PRIVATE, 1 << 20, IPC_CREAT | 0600); if (ids[i] < 0) { printf("shmget %d failed: %s\n", i + 1, strerror(errno)); break; } p = shmat(ids[i], 0, 0); if (p == (void *)-1) { printf("shmat %2d failed: %s\n", i + 1, strerror(errno)); shmctl(ids[i], IPC_RMID, 0); break; } printf("shmat %2d %p\n", i + 1, p); shmctl(ids[i], IPC_RMID, 0); /* removed when detached at exit */ } return 0; }