3

I am working on a program that tends to use a lot of stack memory. Is there a way I can find out the remaining space on the stack ? This is on the linux platform.

Thanks!!

RagHaven
  • 4,156
  • 21
  • 72
  • 113
  • 1
    If you're using a lot of stack memory consider allocating on the heap instead. – Jesus Ramos Apr 15 '12 at 23:27
  • 6
    I agree with @JesusRamos. Stack space is a case of, "If you have to ask, you can't afford it." – Matthew Flaschen Apr 16 '12 at 00:33
  • Related, `rlimit_stack` behavior has changed due to [Stack Clash](http://www.openwall.com/lists/oss-security/2017/06/19/1). Also see Red Hat [Issue 1463241](https://bugzilla.redhat.com/show_bug.cgi?id=1463241) – jww Jun 21 '17 at 16:27

2 Answers2

6

Is there a way I can find out the remaining space on the stack

Yes, there is: for the main thread, you can simply record &argc in main in some global (e.g. int *g_addr_argc), then call getrlimit(RLIMIT_STACK, ...) and compare address of some local to it, e.g.

char a_local;
struct rlimit rlim_stack;

if (getrlimit(RLIMIT_STACK, &rlim_stack) == 0 &&
    rlim_stack.rlim_cur != RLIM_INFINITY &&
    (uintptr_t)g_addr_argc - (uintptr_t)&a_local > rlim_stack.rlim_cur - 8192) {
  fprintf(stderr, "Danger: getting too close to the stack limit\n");
}

This would only work for the main thread. If your application is multi-threaded, you can use pthread_getattr_np to find information about your current thread stack.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
2

You can set the stack size yourself in your code, using setrlimit. Then you don't have to wonder, and you can increase it (within reason) as you see fit.

#include <sys/resource.h>

int main (int argc, char **argv)
{
    const rlim_t kStackSize = 16 * 1024 * 1024;   // min stack size = 16 MB
    struct rlimit rl;
    int result;

    result = getrlimit(RLIMIT_STACK, &rl);
    if (result == 0)
    {
        if (rl.rlim_cur < kStackSize)
        {
            rl.rlim_cur = kStackSize;
            result = setrlimit(RLIMIT_STACK, &rl);
            if (result != 0)
            {
                fprintf(stderr, "setrlimit returned result = %d\n", result);
            }
        }
    }

    // ...

    return 0;
}
Community
  • 1
  • 1
Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • "You can set the stack size yourself" -- this may not be very useful. For example, if you process arbitrary user input in a recursive-descent parser, then *no matter* how large a stack you allocate, the user can provide input that will exceed that stack. It is much more useful to detect that you are about to run out of stack and stop the parse, rather then run out of stack and crash. – Employed Russian Apr 15 '12 at 23:57
  • 1
    @EmployedRussian that's only half the point I was making. The other half is, once you know how big your stack is (because you set it), you can tell how much left you've got. – Mahmoud Al-Qudsi Apr 16 '12 at 00:35
  • @MahmoudAl-Qudsi: But you can't increase the size of the stack. That's the point Matthew was trying to make. – Julie in Austin Apr 16 '12 at 03:37
  • @JulieinAustin Nope. There are 2 different cases here: 1) You can set the stack size and monitor your usage to ensure you don't exceed (nullifying EmployedRussian's objection). 2) You can't set the stack size due to permission issues SOMETIMES, which is MatthewFlaschen's point and is fully valid. – Mahmoud Al-Qudsi Apr 16 '12 at 17:06