Stack overflow это ошибка

What is stack overflow?

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. The call stack, also referred to as the stack segment, is a fixed-sized buffer that stores local function variables and return address data during program execution.

The call stack adheres to a last-in, first-out (LIFO) memory architecture. Each function gets its own stack frame for storing variable and address data. When a function is called, the function’s stack frame is added to the top of the call stack. The stack frame will remain in memory until the function is finished executing. The stack frame is then dropped from the stack, freeing up memory for other stack frames.

The size of a call stack is usually defined at the start of a program. Its size depends on multiple factors, such as the architecture of the host computer, the programming language being used and the amount of available memory in the system. If a program demands more memory than is available in the call stack, a stack overflow occurs, which can cause the program — or even the entire computer — to crash.

What causes stack overflow?

One of the most common causes of a stack overflow is the recursive function, a type of function that repeatedly calls itself in an attempt to carry out specific logic. Each time the function calls itself, it uses up more of the stack memory. If the function runs too many times, it can eat up all the available memory, resulting in a stack overflow.

Stack overflow errors can also occur if too much data is assigned to the variables in the stack frame. Array variables are particularly susceptible to stack overflow errors, especially if no logic has been implemented to prevent excess data from being written to the array.

stack buffer overflow attack

Exploiting buffer overflows lets attackers control or crash a process or modify its internal variables.

What happens during a stack overflow?

When a stack overflow occurs, the excess data can corrupt other variables and address data, effectively changing variable values and overwriting return addresses. In some cases, this will cause the program to crash. At other times, the program will continue to run, making it more difficult to troubleshoot the problem once the error is discovered. The longer the program runs, the harder this becomes.

A program susceptible to stack overflows can expose security vulnerabilities that hackers can exploit. By overwriting the call stack, they can insert their own executable code, which could have a significant impact on how the program works or how it is accessed. For example, a hacker might be able to use a stack overflow vulnerability to alter a password or delete a configuration file.

What is a heap overflow?

Another type of buffer overflow error is the heap overflow. Unlike the call stack, the heap (or heap segment) is a memory space that’s allocated dynamically and that stores global variables. The heap is just as susceptible to buffer overflow errors as the call stack, even if the memory is allocated dynamically. With heaps, program developers are responsible for deallocating memory. If they fail to do this properly, heap overflow can occur, resulting in critical data being overwritten. Heap overflow can also occur when the stored variables contain more data than the amount of allocated memory.

See also: memory allocation, memory management, swap file

This was last updated in July 2022


Continue Reading About stack overflow

  • Memory management techniques improve system performance
  • 5 application security threats and how to prevent them
  • Medical devices at risk from Siemens Nucleus vulnerabilities
  • Cache vs. RAM: Differences between the two memory types
  • How did a Microsoft Equation Editor flaw put systems at risk?

В мире программистов ошибка «stack overflow» очень известна благодаря тому, что этот вид ошибки довольно распространен. А сам термин «stack overflow» известен еще больше, чем ошибка, благодаря одноименному англоязычному ресурсу «StackOverflow». Это сообщество программистов международного масштаба, и еще куча всего интересного. Поэтому не нужно путать ошибку «stack overflow» и веб-ресурс с таким же названием. В нашей статье речь пойдет об ошибке.

Ошибка «stack overflow» связана с переполнением стека. Она появляется в том случае, когда в стеке должно сохраниться больше информации, чем он может уместить. Объем памяти стека задает программист при запуске программы. Если в процессе выполнения программы стек переполняется, тогда возникает ошибка «stack overflow» и программа аварийно завершает работу. Причин возникновения подобной ошибки может быть несколько.

Ошибка «stack overflow»

Нужно отметить, что ошибка «stack overflow» не связана с конкретным языком программирования, то есть она может возникнуть в программах на Java, C++, C, C# и других компилируемых языках.

Причин ее возникновения может быть несколько. К самым распространенным причинам относятся:

  • бесконечная рекурсия;

  • глубокая рекурсия;

  • проблемы с переменными в стеке.

Бесконечная рекурсия и ошибка «stack overflow» 

Бесконечная рекурсия редко возникает самостоятельно и по неизвестным причинам. Обычно программист:

  • забывает прописывать условие для выхода из рекурсии;

  • пишет неосознанную косвенную рекурсию.

Самая частая причина из категории «бесконечной рекурсии» программист забывает прописывать условия выхода или же прописывает, но условия выхода не срабатывают.

Вот как это выглядит на С:

int factorial (int number)

{

  if (number == 0)

    return 1;

  return number * factorial(number — 1);

}

В описанном примере прописаны условия выхода из рекурсии, однако они никогда не сработают, если «number» будет отрицательным. Поэтому через несколько миллионов вызовов стек будет переполнен и возникнет ошибка «stack overflow». В некоторых языках программирования предусмотрена «защита» от таких рекурсий. В них рекурсия из конца функции конвертируется в цикл, что не будет расходовать стековую память. Но подобная «оптимизация» вызовет другую, менее опасную проблему «зацикливание».

Неосознанная бесконечная рекурсия возникает в том случае, если программист по невнимательности распределяет один и тот же функционал программы между разными нагруженными функциями, а потом делает так, что они друг друга вызывают.

В коде это выглядит так:

 int Object::getNumber(int index, bool& isChangeable)

{

  isChangeable = true;

  return getNumber(index);

}

int Object::getNumber(int index)

{

  bool noValue;

  return getNumber(index, noValue);

}

Глубокая рекурсия и ошибка «stack overflow»

Глубокая рекурсия — это такая рекурсия, которая имеет свое окончание через определенное время, поэтому она не бесконечная. Однако памяти стека не хватит для завершения такой рекурсии, поэтому ошибка «stack overflow» обеспечена. Обычно такая ситуация заранее просчитывается программистом,поэтому ее можно решить. Например, можно:

  • отыскать другой программный алгоритм для решения поставленной задачи, чтобы избежать применения рекурсии;

  • «вынести» рекурсию за пределы аппаратного стека в динамический;

  • и другое.

Глубокая рекурсия выглядит так:

void eliminateList(struct Item* that)

{

    if (that == NULL)

        return;

    eliminateList(that->next);

    free(that);

}

Проблемы с переменными в стеке и ошибка «stack overflow»

Если взглянуть на популярность возникновения «stack overflow error», то причина с проблемными переменными в стеке стоит на первом месте. Кроется она в том, что программист изначально выделяет слишком много памяти локальной переменной.

Например:

int function() {

     double b[1000000]

}

В данном случае может возникнуть такая ситуация, что массиву потребуется объем памяти, который стек не способен будет обеспечить, а значит, возникнет ошибка «stack overflow». 

Заключение

Ошибка «stack overflow» возникает довольно часто. Каждый конкретный случай ее возникновения требует собственного решения. Одна причина объединяет возникновение такой ошибки — невнимательность программиста. Если «stack overflow error» возникла, значит, программист где-то что-то упустил или не доглядел.

StackOverflowError is an error which Java doesn’t allow to catch, for instance, stack running out of space, as it’s one of the most common runtime errors one can encounter.

The main cause of the StackOverflowError is that we haven’t provided the proper terminating condition to our recursive function or template, which means it will turn into an infinite loop.

When does StackOverflowError encountered?

When we invoke a method, a new stack frame is created on the call stack or on the thread stack size. This stack frame holds parameters of the invoked method, mostly the local variables and the return address of the method. The creation of these stack frames will be iterative and will be stopped only when the end of the method invokes is found in the nested methods. In amidst of this process, if JVM runs out of space for the new stack frames which are required to be created, it will throw a StackOverflowError.

For example: Lack of proper or no termination condition. This is mostly the cause of this situation termed as unterminated or infinite recursion.

Given below is the implementation of infinite recursion:

public class StackOverflowErrorClass {

    static int i = 0;

    public static int printNumber(int x)

    {

        i = i + 2;

        System.out.println(i);

        return i + printNumber(i + 2);

    }

    public static void main(String[] args)

    {

        StackOverflowErrorClass.printNumber(i);

    }

}

Runtime Error:

RunTime Error in java code :- Exception in thread “main” java.lang.StackOverflowError
at java.io.PrintStream.write(PrintStream.java:526)
at java.io.PrintStream.print(PrintStream.java:597)
at java.io.PrintStream.println(PrintStream.java:736)
at StackOverflowErrorClass.printNumber(StackOverflowErrorClass.java:13)
at StackOverflowErrorClass.printNumber(StackOverflowErrorClass.java:14)
at StackOverflowErrorClass.printNumber(StackOverflowErrorClass.java:14)
.
.
.

Note: Please run this on your system to see an error thrown, due to the stack size, this may not show error on online IDE.

How to fix StackOverflowError?

  1. Avoiding repetitive calls: Try to introduce a proper terminating condition or some condition for the recursive calls to ensure that it terminates.

    Given below is the implementation with proper terminating condition:

    public class stackOverflow {

        static int i = 0;

        public static int printNumber(int x)

        {

            i = i + 2;

            System.out.println(i);

            if (i == 10)

                return i;

            return i + printNumber(i + 2);

        }

        public static void main(String[] args)

        {

            stackOverflow.printNumber(i);

        }

    }

  2. Increasing the Stack Size: The second method could be, if you notice that it’s implemented correctly still we see an error, then we can avoid that only by increasing the Stack Size in order to store the required number of recursive calls. This is achieved by changing the settings of the compiler.

    Cyclic Relationships between classes is the relationship caused when two different classes instantiate each other inside their constructors.

    StackOverflowError is encountered because the constructor of Class A1 is instantiating Class A2, and the constructor of Class A2 is again instantiating Class A1, and it occurs repeatedly until we see StackOverflow. This error is mainly due to the bad calling of constructors, that is, calling each other, which is not even required, and also it doesn’t hold any significance, so we can just avoid introducing them in the codes.

    Given below is the implementation of Cyclic Relationships Between Classes:

    public class A1 {

        public A2 type2;

        public A1()

        {

            type2 = new A2();

        }

        public static void main(String[] args)

        {

            A1 type1 = new A1();

        }

    }

    class A2 {

        public A1 type1;

        public A2()

        {

            type1 = new A1();

        }

    }

    Runtime Error:

    RunTime Error in java code :- Exception in thread “main” java.lang.StackOverflowError
    at A2.(A1.java:32)
    at A1.(A1.java:13)
    at A2.(A1.java:32)
    .
    .
    .

    Note: This will keep repeating, Infinite Recursion is seen by these infinite cyclic calls.

Last Updated :
07 Apr, 2020

Like Article

Save Article

В мире программистов ошибка «stack overflow» очень известна благодаря тому, что этот вид ошибки довольно распространен. А сам термин «stack overflow» известен еще больше, чем ошибка, благодаря одноименному англоязычному ресурсу «StackOverflow». Это сообщество программистов международного масштаба, и еще куча всего интересного. Поэтому не нужно путать ошибку «stack overflow» и веб-ресурс с таким же названием. В нашей статье речь пойдет об ошибке.

Ошибка «stack overflow» связана с переполнением стека. Она появляется в том случае, когда в стеке должно сохраниться больше информации, чем он может уместить. Объем памяти стека задает программист при запуске программы. Если в процессе выполнения программы стек переполняется, тогда возникает ошибка «stack overflow» и программа аварийно завершает работу. Причин возникновения подобной ошибки может быть несколько.

Нужно отметить, что ошибка «stack overflow» не связана с конкретным языком программирования, то есть она может возникнуть в программах на Java, C++, C, C# и других компилируемых языках.

Причин ее возникновения может быть несколько. К самым распространенным причинам относятся:

  • бесконечная рекурсия;

  • глубокая рекурсия;

  • проблемы с переменными в стеке.

Бесконечная рекурсия и ошибка «stack overflow» 

Бесконечная рекурсия редко возникает самостоятельно и по неизвестным причинам. Обычно программист:

  • забывает прописывать условие для выхода из рекурсии;

  • пишет неосознанную косвенную рекурсию.

Самая частая причина из категории «бесконечной рекурсии» программист забывает прописывать условия выхода или же прописывает, но условия выхода не срабатывают.

Вот как это выглядит на С:

int factorial (int number)

{

  if (number == 0)

    return 1;

  return number * factorial(number — 1);

}

В описанном примере прописаны условия выхода из рекурсии, однако они никогда не сработают, если «number» будет отрицательным. Поэтому через несколько миллионов вызовов стек будет переполнен и возникнет ошибка «stack overflow». В некоторых языках программирования предусмотрена «защита» от таких рекурсий. В них рекурсия из конца функции конвертируется в цикл, что не будет расходовать стековую память. Но подобная «оптимизация» вызовет другую, менее опасную проблему «зацикливание».

Неосознанная бесконечная рекурсия возникает в том случае, если программист по невнимательности распределяет один и тот же функционал программы между разными нагруженными функциями, а потом делает так, что они друг друга вызывают.

В коде это выглядит так:

 int Object::getNumber(int index, bool& isChangeable)

{

  isChangeable = true;

  return getNumber(index);

}

int Object::getNumber(int index)

{

  bool noValue;

  return getNumber(index, noValue);

}

Глубокая рекурсия и ошибка «stack overflow»

Глубокая рекурсия — это такая рекурсия, которая имеет свое окончание через определенное время, поэтому она не бесконечная. Однако памяти стека не хватит для завершения такой рекурсии, поэтому ошибка «stack overflow» обеспечена. Обычно такая ситуация заранее просчитывается программистом,поэтому ее можно решить. Например, можно:

  • отыскать другой программный алгоритм для решения поставленной задачи, чтобы избежать применения рекурсии;

  • «вынести» рекурсию за пределы аппаратного стека в динамический;

  • и другое.

Глубокая рекурсия выглядит так:

void eliminateList(struct Item* that)

{

    if (that == NULL)

        return;

    eliminateList(that->next);

    free(that);

}

Проблемы с переменными в стеке и ошибка «stack overflow»

Если взглянуть на популярность возникновения «stack overflow error», то причина с проблемными переменными в стеке стоит на первом месте. Кроется она в том, что программист изначально выделяет слишком много памяти локальной переменной.

Например:

int function() {

     double b[1000000]

}

В данном случае может возникнуть такая ситуация, что массиву потребуется объем памяти, который стек не способен будет обеспечить, а значит, возникнет ошибка «stack overflow». 

Заключение

Ошибка «stack overflow» возникает довольно часто. Каждый конкретный случай ее возникновения требует собственного решения. Одна причина объединяет возникновение такой ошибки — невнимательность программиста. Если «stack overflow error» возникла, значит, программист где-то что-то упустил или не доглядел.

StackOverflowError в Java

1. обзор

StackOverflowError может раздражать разработчиков Java, поскольку это одна из самых распространенных ошибок времени выполнения, с которыми мы можем столкнуться.

В этой статье мы увидим, как может возникать эта ошибка, на различных примерах кода и узнаем, как с ней бороться.

2. Фреймы стека и как происходитStackOverflowError

Давайте начнем с основ. When a method is called, a new stack frame gets created on the call stack. Этот кадр стека содержит параметры вызванного метода, его локальные переменные и адрес возврата метода, т.е. точка, с которой выполнение метода должно продолжаться после возврата вызванного метода.

Создание кадров стека будет продолжаться до тех пор, пока не будет достигнут конец вызовов методов, найденных во вложенных методах.

Во время этого процесса, если JVM обнаруживает ситуацию, когда нет места для создания нового кадра стека, она выдастStackOverflowError.

Наиболее частая причина, по которой JVM может столкнуться с этой ситуацией, —unterminated/infinite recursion — в описании Javadoc дляStackOverflowError упоминается, что ошибка возникает в результате слишком глубокой рекурсии в конкретном фрагменте кода.

Однако рекурсия не является единственной причиной этой ошибки. Это также может произойти в ситуации, когда приложение хранитcalling methods from within methods until the stack is exhausted. Это редкий случай, так как ни один разработчик не будет намеренно следовать плохой практике кодирования Другая редкая причина —having a vast number of local variables inside a method.

StackOverflowError также может быть выброшено, если приложение разработано для использованияcyclic relationships between classes. В этой ситуации конструкторы друг друга вызывают неоднократно, что приводит к возникновению этой ошибки. Это также можно рассматривать как форму рекурсии.

Другой интересный сценарий, который вызывает эту ошибку, — этоclass is being instantiated within the same class as an instance variable of that class. Это приведет к тому, что конструктор одного и того же класса будет вызываться снова и снова (рекурсивно), что в конечном итоге приведет кStackOverflowError.

В следующем разделе мы рассмотрим несколько примеров кода, демонстрирующих эти сценарии.

3. StackOverflowError в действии

В примере, показанном ниже,StackOverflowError будет выброшено из-за непреднамеренной рекурсии, когда разработчик забыл указать условие завершения для рекурсивного поведения:

public class UnintendedInfiniteRecursion {
    public int calculateFactorial(int number) {
        return number * calculateFactorial(number - 1);
    }
}

Здесь ошибка выдается во всех случаях для любого значения, переданного в метод:

public class UnintendedInfiniteRecursionManualTest {
    @Test(expected = StackOverflowError.class)
    public void givenPositiveIntNoOne_whenCalFact_thenThrowsException() {
        int numToCalcFactorial= 1;
        UnintendedInfiniteRecursion uir
          = new UnintendedInfiniteRecursion();

        uir.calculateFactorial(numToCalcFactorial);
    }

    @Test(expected = StackOverflowError.class)
    public void givenPositiveIntGtOne_whenCalcFact_thenThrowsException() {
        int numToCalcFactorial= 2;
        UnintendedInfiniteRecursion uir
          = new UnintendedInfiniteRecursion();

        uir.calculateFactorial(numToCalcFactorial);
    }

    @Test(expected = StackOverflowError.class)
    public void givenNegativeInt_whenCalcFact_thenThrowsException() {
        int numToCalcFactorial= -1;
        UnintendedInfiniteRecursion uir
          = new UnintendedInfiniteRecursion();

        uir.calculateFactorial(numToCalcFactorial);
    }
}

Однако в следующем примере указано условие завершения, но оно никогда не выполняется, если значение-1 передается методуcalculateFactorial(), что вызывает незавершенную / бесконечную рекурсию:

public class InfiniteRecursionWithTerminationCondition {
    public int calculateFactorial(int number) {
       return number == 1 ? 1 : number * calculateFactorial(number - 1);
    }
}

Этот набор тестов демонстрирует этот сценарий:

public class InfiniteRecursionWithTerminationConditionManualTest {
    @Test
    public void givenPositiveIntNoOne_whenCalcFact_thenCorrectlyCalc() {
        int numToCalcFactorial = 1;
        InfiniteRecursionWithTerminationCondition irtc
          = new InfiniteRecursionWithTerminationCondition();

        assertEquals(1, irtc.calculateFactorial(numToCalcFactorial));
    }

    @Test
    public void givenPositiveIntGtOne_whenCalcFact_thenCorrectlyCalc() {
        int numToCalcFactorial = 5;
        InfiniteRecursionWithTerminationCondition irtc
          = new InfiniteRecursionWithTerminationCondition();

        assertEquals(120, irtc.calculateFactorial(numToCalcFactorial));
    }

    @Test(expected = StackOverflowError.class)
    public void givenNegativeInt_whenCalcFact_thenThrowsException() {
        int numToCalcFactorial = -1;
        InfiniteRecursionWithTerminationCondition irtc
          = new InfiniteRecursionWithTerminationCondition();

        irtc.calculateFactorial(numToCalcFactorial);
    }
}

В этом конкретном случае ошибки можно было бы полностью избежать, если бы условие завершения было просто сформулировано как:

public class RecursionWithCorrectTerminationCondition {
    public int calculateFactorial(int number) {
        return number <= 1 ? 1 : number * calculateFactorial(number - 1);
    }
}

Вот тест, который демонстрирует этот сценарий на практике:

public class RecursionWithCorrectTerminationConditionManualTest {
    @Test
    public void givenNegativeInt_whenCalcFact_thenCorrectlyCalc() {
        int numToCalcFactorial = -1;
        RecursionWithCorrectTerminationCondition rctc
          = new RecursionWithCorrectTerminationCondition();

        assertEquals(1, rctc.calculateFactorial(numToCalcFactorial));
    }
}

Теперь давайте рассмотрим сценарий, в которомStackOverflowError возникает в результате циклических отношений между классами. Давайте рассмотримClassOne иClassTwo, которые создают экземпляры друг друга внутри своих конструкторов, вызывая циклическую связь:

public class ClassOne {
    private int oneValue;
    private ClassTwo clsTwoInstance = null;

    public ClassOne() {
        oneValue = 0;
        clsTwoInstance = new ClassTwo();
    }

    public ClassOne(int oneValue, ClassTwo clsTwoInstance) {
        this.oneValue = oneValue;
        this.clsTwoInstance = clsTwoInstance;
    }
}
public class ClassTwo {
    private int twoValue;
    private ClassOne clsOneInstance = null;

    public ClassTwo() {
        twoValue = 10;
        clsOneInstance = new ClassOne();
    }

    public ClassTwo(int twoValue, ClassOne clsOneInstance) {
        this.twoValue = twoValue;
        this.clsOneInstance = clsOneInstance;
    }
}

Теперь предположим, что мы пытаемся создать экземплярClassOne, как показано в этом тесте:

public class CyclicDependancyManualTest {
    @Test(expected = StackOverflowError.class)
    public void whenInstanciatingClassOne_thenThrowsException() {
        ClassOne obj = new ClassOne();
    }
}

В итоге получаетсяStackOverflowError, поскольку конструкторClassOne создает экземплярClassTwo,, а конструкторClassTwo снова создает экземплярClassOne.. И это происходит неоднократно, пока он не переполнится. стек.

Далее мы рассмотрим, что происходит, когда создается экземпляр класса в том же классе, что и переменная экземпляра этого класса.

Как видно в следующем примере,AccountHolder создает экземпляр переменной экземпляраjointAccountHolder:

public class AccountHolder {
    private String firstName;
    private String lastName;

    AccountHolder jointAccountHolder = new AccountHolder();
}

Когда создается экземпляр классаAccountHolder,, вызываетсяStackOverflowError из-за рекурсивного вызова конструктора, как показано в этом тесте:

public class AccountHolderManualTest {
    @Test(expected = StackOverflowError.class)
    public void whenInstanciatingAccountHolder_thenThrowsException() {
        AccountHolder holder = new AccountHolder();
    }
}

4. Работа сStackOverflowError

Лучшее, что можно сделать при обнаруженииStackOverflowError, — это осторожно изучить трассировку стека, чтобы определить повторяющийся шаблон номеров строк. Это позволит нам найти код с проблемной рекурсией.

Давайте рассмотрим несколько трассировок стека, вызванных примерами кода, которые мы видели ранее.

Эта трассировка стека создаетсяInfiniteRecursionWithTerminationConditionManualTest, если мы опускаем объявление исключенияexpected:

java.lang.StackOverflowError

 at c.b.s.InfiniteRecursionWithTerminationCondition
  .calculateFactorial(InfiniteRecursionWithTerminationCondition.java:5)
 at c.b.s.InfiniteRecursionWithTerminationCondition
  .calculateFactorial(InfiniteRecursionWithTerminationCondition.java:5)
 at c.b.s.InfiniteRecursionWithTerminationCondition
  .calculateFactorial(InfiniteRecursionWithTerminationCondition.java:5)
 at c.b.s.InfiniteRecursionWithTerminationCondition
  .calculateFactorial(InfiniteRecursionWithTerminationCondition.java:5)

Здесь строка № 5 видна повторяющейся. Это где рекурсивный вызов делается. Теперь остается просто изучить код, чтобы убедиться, что рекурсия выполнена правильно.

Вот трассировка стека, которую мы получаем, выполняяCyclicDependancyManualTest (опять же, без исключенияexpected):

java.lang.StackOverflowError
  at c.b.s.ClassTwo.(ClassTwo.java:9)
  at c.b.s.ClassOne.(ClassOne.java:9)
  at c.b.s.ClassTwo.(ClassTwo.java:9)
  at c.b.s.ClassOne.(ClassOne.java:9)

Эта трассировка стека показывает номера строк, которые вызывают проблему в двух классах, которые находятся в циклическом отношении. Строка номер 9ClassTwo и строка номер 9ClassOne указывают на место внутри конструктора, где он пытается создать экземпляр другого класса.

Как только код тщательно проверен, и если ни одно из следующего (или любая другая логическая ошибка кода) не является причиной ошибки:

  • Неправильно реализованная рекурсия (т.е. без условия прекращения)

  • Циклическая зависимость между классами

  • Создание класса внутри того же класса, что и переменная экземпляра этого класса

Было бы неплохо попробовать увеличить размер стека. В зависимости от установленной JVM размер стека по умолчанию может варьироваться.

Флаг-Xss можно использовать для увеличения размера стека либо из конфигурации проекта, либо из командной строки.

5. Заключение

В этой статье мы более подробно рассмотрелиStackOverflowError, включая то, как код Java может вызывать это, и как мы можем диагностировать и исправить это.

Исходный код, относящийся к этой статье, можно найтиover on GitHub.

  • Definition
  • Run-time Stack
  • Consequences of the Bug
  • Reasons for the Bug
  • Examples
  • Conclusion
  • References

Definition

A stack overflow is a run-time software bug when a program attempts to use more space than is available on the run-time stack, which typically results in a program crash.

Run-time Stack

A run-time stack is a special area of computer memory that works on the LIFO principle (Last in, first out: the last element added to a structure must be the first one to be removed). The word «stack» refers to the manner in which several plates are stacked: you form a stack by putting the plates on top of each other (this way of adding an object into the stack is called «push») and then remove them starting with the top plate (this way of removing an object from the stack is known as «pop»). Run-time stack is also known as call stack, execution stack and machine stack (these terms are used in order not to mix it up with the «stack» as an abstract data structure).

The purpose of a stack is to allow the programmer to conveniently arrange calls of subroutines. A stack can be used to store arguments to be passed to a function being called and its local variables. If another function is called by the first one, it can pop the arguments from the stack and use them, as well as store its own variables in a memory area allocated for this function. As it returns control, it also clears and frees the stack memory. High-level language programmers usually don’t bother about such things, for the task of generating the necessary routine code is performed solely by the compiler.

Consequences of the Bug

We have finally got close to the subject of our discussion. As an abstraction, a stack is an infinite storage you can endlessly add new items into. Unfortunately, everything is finite in the real world — stack memory is no exception. What will happen when it runs out as new arguments are being pushed into it or when a function allocates memory to store its variables?

An error known as a stack overflow will occur. Since a stack is used to arrange calls of user subroutines (and most programs written in contemporary programming languages — including object-oriented ones — actively employ functions one way or another), the program won’t be able to call any function after the error occurs. When that happens, the operating system takes control back, clears the stack and terminates the program. Here lies the difference between the buffer overflow and the stack overflow. The former occurs when the program attempts to access a memory area outside the buffer’s boundary and remains unnoticed if there is no protection against that; the program goes on to run correctly if lucky enough. It’s only when there is memory protection that a segmentation fault occurs. But when a stack overflow occurs, the program inevitably crashes.

To be most precise, this scenario is only true for native languages. The virtual machine in managed languages has its own stack for managed programs, which is easier to monitor, so that you can even afford throwing an exception when a stack overflow occurs. But C and C++ cannot afford such a «luxury».

Reasons for the Bug

What are the reasons for this unpleasant error? Keeping in mind the above described mechanism, we can name one: too many embedded function calls. This scenario is especially probable when using recursion: it is actually this error that infinite recursion terminates with (when there is no lazy evaluation mechanism), unlike an infinite loop which may be useful at times. However, when there is a very small area of memory allocated on the stack (which is, for instance, typical of microcontrollers), just a short sequence of calls will do the job.

Another reason is local variables requiring too much stack memory. It’s a bad idea to create a local array of a million of items or a million local variables (just in case). Just one call of such a «greedy» function may easily trigger a stack overflow. If you want to get large data amounts, you’d better use dynamic memory to be able to process an error in case it runs out.

Dynamic memory, however, is quite slow to allocate and free (because it is managed by the operating system). Besides, you have to manually allocate and release it when provided with a direct access. Conversely, stack memory is allocated very quickly (in fact you just need to change the value of one register); moreover, objects allocated on the stack are automatically destroyed when the function returns control and clears the stack. You naturally can’t help the urge to exploit it. Therefore, the third reason for the bug is manual allocation of stack memory by the programmer. The C library provides the special function alloca for this purpose. An interesting thing is that while the malloc function (intended to allocate dynamic memory) has a «sibling» that frees it (the free function), the alloca function has none: memory is freed automatically once the function returns control. This thing is likely to complicate the issue, for you can’t free memory before leaving the function. Though the man page for the alloca function clearly reads that it «is machine- and compiler-dependent; on many systems it cannot be used properly and may cause errors; its use is discouraged», programmers still use it.

Examples

As an example, let’s study a code fragment performing recursive file search (taken from MSDN):

void DirSearch(String* sDir)
 {
     try
     {
         // Find the subfolders in the folder that is passed in.
         String* d[] = Directory::GetDirectories(sDir);
         int numDirs = d->get_Length();
         
         for (int i=0; i < numDirs; i++)
         {
             // Find all the files in the subfolder.
             String* f[] = Directory::GetFiles(d[i],textBox1->Text);
             int numFiles = f->get_Length();

             for (int j=0; j < numFiles; j++)
             {
                 listBox1->Items->Add(f[j]);
             }
             DirSearch(d[i]);
         }
     }
     catch (System::Exception* e)
     {
         MessageBox::Show(e->Message);
     }
 }

This function receives the list of items in the specified folder and then recursively calls itself over those items which are folders. If the file tree is deep enough, the result of this is quite obvious.

Here is an example to illustrate the second reason taken from the question «What might be the reason for a stack overflow exception?» asked at Stack Overflow (this is a question-and-answer website dealing with all programming-related topics, not only stack overflow, as one may conclude from its name):

#define W 1000
#define H 1000
#define MAX 100000 
//...
int main()
{
    int image[W*H];
    float dtr[W*H];
    initImg(image,dtr);
    return 0;
}

As you can see, the main function asks for some stack memory to be allocated for an int-array and a float-array, one million items each, which gives just a bit less than 8 Mbytes in total. If we recall that Visual C++ by default reserves only 1 Mbyte for the stack, we can easily answer that question.

And, finally, here is an example taken from the GitHub-repository of the Flash-player Lightspark project:

DefineSoundTag::DefineSoundTag(/* ... */)
{
    // ...
    unsigned int soundDataLength = h.getLength()-7;
    unsigned char *tmp = (unsigned char *)alloca(soundDataLength);
    // ...
}

One may hope that h.getLength()-7 won’t grow too big and no overflow will occur in the next line. But is the time saved on memory allocation worth a potential crash?

Conclusion

Stack Overflow is a fatal error which is most often found in programs containing recursive functions. It can also be caused by pushing too many local variables into the stack or manual allocation of stack memory. Stick to the old good rules: when you have a choice, prefer iteration to recursion, and avoid manual interference in what the compiler does better.

References

  • A. Tanenbaum. Structured Computer Organization.
  • Wikipedia. Stack Overflow.
  • man 3 alloca.
  • MSDN. How to recursively search folders by using Visual C++.
  • Stack Overflow. Stack Overflow C++.
  • GitHub. Lightspark — «tags.cpp».

We can email you a selection of our best articles once a month

From Wikipedia, the free encyclopedia

In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. 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.[1]

Causes[edit]

Infinite recursion[edit]

The most-common cause of stack overflow is excessively deep or infinite recursion, in which a function calls itself so many times that the space needed to store the variables and information associated with each call is more than can fit on the stack.[2]

An example of infinite recursion in C.

int foo() 
{
     return foo();
}

The function foo, when it is invoked, continues to invoke itself, allocating additional space on the stack each time, until the stack overflows resulting in a segmentation fault.[2] However, some compilers implement tail-call optimization, allowing infinite recursion of a specific sort—tail recursion—to occur without stack overflow. This works because tail-recursion calls do not take up additional stack space.[3]

Some C compiler options will effectively enable tail-call optimization; for example, compiling the above simple program using gcc with -O1 will result in a segmentation fault, but not when using -O2 or -O3, since these optimization levels imply the -foptimize-sibling-calls compiler option.[4] Other languages, such as Scheme, require all implementations to include tail-recursion as part of the language standard.[5]

Very deep recursion[edit]

A recursive function that terminates in theory but causes a call stack buffer overflow in practice can be fixed by transforming the recursion into a loop and storing the function arguments in an explicit stack (rather than the implicit use of the call stack). This is always possible because the class of primitive recursive functions is equivalent to the class of LOOP computable functions. Consider this example in C++-like pseudocode:

void function (argument) 
{
  if (condition)
    function (argument.next);

}
stack.push(argument);
while (!stack.empty())
{
  argument = stack.pop();
  if (condition)
    stack.push(argument.next);
}

A primitive recursive function like the one on the left side can always be transformed into a loop like on the right side.

A function like the example above on the left would not be a problem in an environment supporting tail-call optimization; however, it is still possible to create a recursive function that may result in a stack overflow in these languages. Consider the example below of two simple integer exponentiation functions.

int pow(int base, int exp) {
    if (exp > 0)
        return base * pow(base, exp - 1);
    else
        return 1;
}
int pow(int base, int exp) {
    return pow_accum(base, exp, 1);
}

int pow_accum(int base, int exp, int accum) {
    if (exp > 0)
        return pow_accum(base, exp - 1, accum * base);
    else
        return accum;
}

Both pow(base, exp) functions above compute an equivalent result, however, the one on the left is prone to causing a stack overflow because tail-call optimization is not possible for this function. During execution, the stack for these functions will look like this:

pow(5, 4)
5 * pow(5, 3)
5 * (5 * pow(5, 2))
5 * (5 * (5 * pow(5, 1)))
5 * (5 * (5 * (5 * pow(5, 0))))
5 * (5 * (5 * (5 * 1)))
625
pow(5, 4)
pow_accum(5, 4, 1)
pow_accum(5, 3, 5)
pow_accum(5, 2, 25)
pow_accum(5, 1, 125)
pow_accum(5, 0, 625)
625

Notice that the function on the left must store in its stack exp number of integers, which will be multiplied when the recursion terminates and the function returns 1. In contrast, the function at the right must only store 3 integers at any time, and computes an intermediary result which is passed to its following invocation. As no other information outside of the current function invocation must be stored, a tail-recursion optimizer can «drop» the prior stack frames, eliminating the possibility of a stack overflow.

Very large stack variables[edit]

The other major cause of a stack overflow results from an attempt to allocate more memory on the stack than will fit, for example by creating local array variables that are too large. For this reason some authors recommend that arrays larger than a few kilobytes should be allocated dynamically instead of as a local variable.[6]

An example of a very large stack variable in C:

int foo() 
{
     double x[1048576];
}

On a C implementation with 8 byte double-precision floats, the declared array consumes 8 megabytes of data; if this is more memory than is available on the stack (as set by thread creation parameters or operating system limits), a stack overflow will occur.

Stack overflows are made worse by anything that reduces the effective stack size of a given program. For example, the same program being run without multiple threads might work fine, but as soon as multi-threading is enabled the program will crash. This is because most programs with threads have less stack space per thread than a program with no threading support. Because kernels are generally multi-threaded, people new to kernel development are usually discouraged from using recursive algorithms or large stack buffers.[7]

See also[edit]

  • Buffer overflow
  • Call stack
  • Heap overflow
  • Stack buffer overflow
  • Double fault

References[edit]

  1. ^ Burley, James Craig (1991-06-01). «Using and Porting GNU Fortran». Archived from the original on 2012-02-06.
  2. ^ a b What is the difference between a segmentation fault and a stack overflow? at StackOverflow
  3. ^ «An Introduction to Scheme and its Implementation». 1997-02-19. Archived from the original on 2007-08-10.
  4. ^ «Using the GNU Compiler Collection (GCC): Optimize Options». Retrieved 2017-08-20.
  5. ^ Richard Kelsey; William Clinger; Jonathan Rees; et al. (August 1998). «Revised5 Report on the Algorithmic Language Scheme». Higher-Order and Symbolic Computation. 11 (1): 7–105. doi:10.1023/A:1010051815785. S2CID 14069423. Retrieved 2012-08-09.
  6. ^ Feldman, Howard (2005-11-23). «Modern Memory Management, Part 2».
  7. ^ «Kernel Programming Guide: Performance and Stability Tips». Apple Inc. 2014-05-02.

External links[edit]

  • The reasons why 64-bit programs require more stack memory Archived 2017-11-04 at the Wayback Machine

Stack overflow is an error in programming that a user-mode thread encounters when attempting to write more data but the memory block is running out of space to hold it.

There are two types of overflow errors: the first one can immediately cause the program to crash. The second remains undetected allowing the program to run after the error, which is harder to trace and more challenging to debug.

What are the symptoms of a stack overflow? 

A stack overflow might appear as a segmentation fault, leaving without a clue where it occurs and how it happened. This is often the case in C++. The signs of an overflow usually depend on the language or the mechanism for error reporting. In Java, however, the virtual machine could crash, as it handles memory inside it, leaving an error file that might further baffle users. The root cause of the overflow is harder to find.  

Once an overflow is detected, you can correct it right away by identifying the source and by debugging the error. Programming languages that allow explicit memory management can prevent these overflows by adjusting the program’s memory allocation to keep the usage to a minimum. Computer languages with implicit memory management encounter difficulty in safeguarding against stack overflow, causing the machine to crash, potentially halting the entire program.

What causes overflow errors in a program?

There are three possible causes of a stack overflow: 

  1. Infinite recursion that causes the thread to use the entire reserved memory stack.
  2. A maxed-out page file is unable to add more pages, which results in the thread failing to extend the stack.
  3. The system is within the time or in the process of extending the page file when the thread attempts to extend the stack.

There should be ample stack space allocated to local variables when a function runs on a thread. Reduced space risks the stack overflowing.

How is stack overflow different from heap overflow?

There’s an area of the memory where local variables used in the function are stored. It is called a stack, which has a Last In-First Out data structure. New local variables are pushed to the stack, and the variables associated with the function get deleted after running to free up memory space. When the stack runs out of space due to a program using more memory space than the stack size, an error or an overflow occurs.

Heap overflow happens when a leak occurs with the memory that stores dynamic variables. Memory on heap is manually allocated and resized using malloc(), calloc(), and realloc() functions. A failure to free up memory space after use results in a memory error.

How to fix a stack overflow?

Microsoft documentation identifies the steps to take in correcting a stack overflow:

  1. Establish the event that causes the error by investigating the target process’ stack usage.
  2. Identify the thread in the stack that causes the overflow.
  3. Calculate the stack size and start debugging

Kelvene Requiroso

Kelvene Requiroso

Kelvene Requiroso is a writer and an enthusiast interested in the interplay between technology and everyday life. He writes for TechnologyAdvice, Baseline, eSecurity Planet, and Webopedia. Also a lover of science fiction and fantasy, he publishes an ongoing web novel series. He has previously worked with non-profits and non-government organizations in Manila, Philippines.

Понравилась статья? Поделить с друзьями:
  • Stack overflow ошибка java
  • Stable diffusion ошибка
  • Srs ошибка на митсубиси каризма
  • Srv008021 ошибка гис жкх
  • Stabili track cadillac ошибка