Tuesday 15 March 2011

c++ - What exception is raised in C by GCC -fstack-check option -



c++ - What exception is raised in C by GCC -fstack-check option -

as per gcc documentation

-fstack-check

generate code verify not go beyond boundary of stack. note switch not cause checking done; operating scheme must that. switch causes generation of code ensure operating scheme sees stack beingness extended.

my assumption code generate exception allow os know. when using c language need know exception beingness generated code.

google not helping much. close came know generates storage_error exception in case of ada language (reference).

i working on sort of little os/scheduler need grab exception. using c/c++.

my gcc version 3.4.4

it doesn't generate exception directly. generates code which, when stack enlarged more 1 page, generates read-write access each page in newly allocated region. that's all does. example:

extern void bar(char *); void foo(void) { char buf[4096 * 8]; bar(buf); }

compiles (with gcc 4.9, on x86-64, @ -o2) to:

foo: pushq %rbp movq $-32768, %r11 movq %rsp, %rbp subq $4128, %rsp addq %rsp, %r11 .lpsrl0: cmpq %r11, %rsp je .lpsre0 subq $4096, %rsp orq $0, (%rsp) jmp .lpsrl0 .lpsre0: addq $4128, %rsp leaq -32768(%rbp), %rdi phone call bar leave ret

orq $0, (%rsp) has no effect on contents of memory @ (%rsp), cpu treats read-write access address anyway. (i don't know why gcc offsets %rsp 4128 bytes during loop, or why thinks frame pointer necessary.) theory os can notice these accesses , appropriate if stack has become large. posix-compliant operating system, delivery of sigsegv signal.

you may wondering how os can notice such thing. hardware allows os designate pages of address space inaccessible; effort read or write memory in pages triggers hardware fault os can process sees fit (again, posix-compliant os, delivery of sigsegv). can used place "guard area" past end of space reserved stack. that's why 1 access per page sufficient.

what -fstack-check meant protect from, clear, situation "guard area" little - perhaps 1 page - allocating big buffer on stack moves stack pointer past area , part of accessible ram. if programme happens never touch memory within guard area, won't prompt crash, scribble on whatever other part is, causing delayed-action malfunction.

c++ c exception gcc

No comments:

Post a Comment