c - mmap allocation returns 0xfffffffffffffff4 (not MAP_FAILED) -
i trying allocate memory using mmap, here code:
long long *copy; copy = (long long*)mmap(null, (size_t)1024, prot_read | prot_write | prot_exec, map_private | map_anon, -1, 0); if (copy == map_failed) { fprintf(stderr, "memory allocation failed (process aborted)\n"); exit(1); } printf("pointer: %p\n", copy);
obviously, check if allocation fails. when happens should -1 gather man pages. thing -12, 0xfffffffffffffff4
, error isn't caught , program goes on. thought maybe because of (long long*)
cast, cast shouldn't change pointer value. curious why happens , how prevent it.
more strange behavior:
i tried print errno
. if use printf("%d\n", errno);
prints 0 , pointer still set 0xfffffffffffffff4
. if use err(errno, "%p", copy);
prints:
program.exe: 0x7f8130981000: success
and pointer valid, can't use because err
terminated execution.
when compiling warnings (-wmissing-prototypes -wstrict-prototypes -werror -wextra
), suggested in comments, realized <err.h>
wasn't included. when included it, "strange behavior" described @ end of question disappeared; program behaved still bad pointer, didn't know @ point program have worked if didn't check errno
.
in other parts of code, have assembly-written functions have been working great (that's why thought mistake wasn't related them). remember of them use %rbx
pass information between them, happens %rbx
register used errno
in gcc.
so looking @ generated assembly code function, realized %rbx
wasn't "protected" compiler. adding headers <errno.h>
fixed that, , working fine.
even though seems work without proper includes, definetly lesson more careful , use maximum warning options.
Comments
Post a Comment