c - Segmentation fault with ulimit set correctly -


i tried op on this question.

i found out code 1 below causes segmentation fault randomly if stack set 2000 kbytes.

int main () {    int a[510000];    a[509999] = 1;    printf("%d", a[509999]);    return 0; } 

as can see array 510000 x 4 bytes = 2040000 bytes.

the stack set 2000 kbytes (2048000 bytes) using ulimit command:

  • ulimit -s 2000
  • ulimit -ss 2000

based on numbers application has room store array, randomly return segmentation fault.

any ideas?

there's few reasons why can't this. there things using parts of stack.

main not first thing on stack. there functions called real entry point, dynamic linker, etc. before main , using of stack.

additionally, there can things put on top of stack set execution. many systems know put strings in argv , environment variables on top of stack (which why main not entry point, there's code runs before main sets environment variables , argv main).

and top off part of stack can deliberately wasted increase randomness of aslr if system that.

run program in debugger, add breakpoint @ main, value of stack register , examine memory above (remember stack grows down unless you're on weird architecture). bet you'll find lots of pointers , strings there. did on linux system , suspected environment variables there.

the purpose of resource limits (ulimit) on unix has never been micromanage things down byte/microsecond, there stop program going crazy , taking down whole system it. see them not red lights , stop signs on proper road, see them run-off areas , crash barriers on racetrack.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -