Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Four different outcomes when overflowing main stack

Tags:

stack

memory

rust

Out of curiosity, I was playing with overflowing the the stack with this code:

fn main() {
    let my_array: [i32; 3000000000] = [3; 3000000000];
    println!("{}", my_array[0]);
}

And to my surprise I ended with three different outcomes:

1) This is what I expected:

thread '<main>' has overflowed its stack
    Illegal instruction (core dumped)

2) Surprisingly vague:

Illegal instruction (core dumped)

3) Totally puzzling:

208333333

In order for stochastic nature to show up I had to restart the shell, otherwise results were deterministic ( I would get the same error message over and over).

I compiled with just:

rustc my_file.rs

and excuted with:

./my_file

My rustc version:

rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)

My ubuntu version:

Distributor ID: Ubuntu
Description:    Ubuntu 14.04 LTS
Release:    14.04
Codename:   trusty

Also the size of the array I am trying to create is 12 gigs, I am on a tiny laptop that does not have that amount of RAM.

Any ideas what could be going on here?

Edit:

I was playing with the size of array (which I think might be the reason for different errors, but why?), and got one more:

4) Makes perfect sense.

error: the type `[i32; 300000000000000]` is too big for the current architecture

and my system architecture is x86_64.

like image 499
Akavall Avatar asked Jul 04 '15 03:07

Akavall


People also ask

What happens when stack overflow?

When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a buffer overflow), the stack is said to overflow, typically resulting in a program crash.

What is overflow condition in stack?

A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use more memory space in the call stack than has been allocated to that stack.

How can we minimize the stack overflow?

One method to prevent stack overflow is to track the stack pointer with test and measurement methods. Use timer interrupts that periodically check the location of the stack pointer, record the largest value, and watch that it does not grow beyond that value.

Why is it called stack overflow?

Named for a common computing error (in which memory used in a section of the computer's storage known as the “call stack” exceeds its allocated capacity) (no need to thank me for explaining), Stack Overflow is a Q&A forum aimed primarily at coders.


1 Answers

It seems that above randomness is related to my machine.

I checked the same code on another machine, that has the same rustc version, ubuntu version and the same architecture. And my results a much more predictable:

If size of the array 536870871 or greater (without getting to case 4) I get:

Illegal instruction (core dumped)

If size of array is 536870870 or smaller (without being small enough to actually work) I get:

thread '<main>' has overflowed its stack
Illegal instruction (core dumped)

Not a single time have I gotten a case 3) where I had garbage returned.

like image 198
Akavall Avatar answered Sep 17 '22 12:09

Akavall