3

I tried to help an OP on this question.

I found out that a code like the one below causes segmentation fault randomly even if the stack is set to 2000 Kbytes.

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

As you can see the array is 510000 x 4 bytes = 2040000 bytes.

The stack is set to 2000 Kbytes (2048000 bytes) using ulimit command:

  • ulimit -s 2000
  • ulimit -Ss 2000

Based on those numbers the application has room to store the array, but randomly it return segmentation fault.

Any ideas?

Community
  • 1
  • 1
LPs
  • 16,045
  • 8
  • 30
  • 61
  • 4
    Perhaps the data structures for stdio should be factored in along with the environment etc – Ed Heal Jul 08 '15 at 12:49
  • @EdHeal Could you elaborate? – LPs Jul 08 '15 at 12:53
  • It depends on the implementation, A program does use a library for stdio in your case. The may use the stack to store stuff before main is hit. That may include pointers for `atexit` etc. – Ed Heal Jul 08 '15 at 12:55
  • @EdHeal I understand that, but could be that stuff more that 8000 bytes? – LPs Jul 08 '15 at 13:00
  • How random is randomly ? Does it still segfault if you printf the int #500000 ? What if you allocate only 500000 ints ? – ElderBug Jul 08 '15 at 13:07
  • 1
    @EdHeal: use some inline assembly upon the entry of `main` to print out the value of the `rsp` register. This way you can determine how much of the stack has been used so far. – Blagovest Buyukliev Jul 08 '15 at 13:07
  • stdio does have buffers. That is a bit of memory for starters – Ed Heal Jul 08 '15 at 13:08
  • .. But no mention of platform (@BlagovestBuyukliev) – Ed Heal Jul 08 '15 at 13:09
  • @EdHeal: x86-64 is implied :-) – Blagovest Buyukliev Jul 08 '15 at 13:10
  • Where is that "implied"? And What OS? What compiler? What versions of libraries? – Ed Heal Jul 08 '15 at 13:12
  • I'm on ubuntu linux 14.04 with 8 gigs of ram on a 64bit CPU, I added the missing header file 'stdio.h' compiled/linked using gcc, then ran the program numerous times. No seg fault events. Could it be that your computer does not have enough free memory (I.E. not being used by the OS and other applications) to properly run the program? – user3629249 Jul 08 '15 at 13:14
  • @EdHeal I'm on Linux Debian 8.0. Gcc version 4.9.2-10. x64 paltform. – LPs Jul 08 '15 at 13:16
  • @EdHeal: it's a good hint to the OP even if we are unaware of the exact environment. We are not giving an exact answer but merely directions exactly because this stuff varies a lot on the things you mentioned. – Blagovest Buyukliev Jul 08 '15 at 13:17
  • @user3629249 I have the same on my PC. We are talking about 2000 Kbytes. I think/guess/hope that it is not a matter of available memory. – LPs Jul 08 '15 at 13:18

2 Answers2

6

There's a few reasons why you can't do this. There are things that are already using parts of your stack.

main is not the first thing on your stack. There are functions called by the real entry point, dynamic linker, etc. that are before main and they are all probably using some of the stack.

Additionally, there can be things that are usually put on the top of the stack to set up execution. Many systems I know put all the strings in argv and all environment variables on top of the stack (which is why main is not the entry point, there's usually code that runs before main that sets up environment variables and argv for main).

And to top it off a part of the stack can be deliberately wasted to increase the randomness of ASLR if your system does that.

Run you program in the debugger, add a breakpoint at main, look up the value of the stack register and examine the memory above it (remember that most likely your stack grows down unless you're on a weird architecture). I bet you'll find lots of pointers and strings there. I just did this on a linux system and as I suspected all my environment variables were there.

The purpose of resource limits (ulimit) on Unix has never really been to micromanage things down to a byte/microsecond, they are there just to stop your program from going completely crazy and taking down the whole system with it. See them not as red lights and stop signs on a proper road, see them as run-off areas and crash barriers on a racetrack.

Art
  • 19,807
  • 1
  • 34
  • 60
0

If you still wants to access the int location in the array, try to compile the code with out the main..this will not invoke _start

check this discussion enter link description here

Community
  • 1
  • 1
Mayukh Sarkar
  • 2,289
  • 1
  • 14
  • 38