Linux gcc stack size

Change stack size for a C++ application in Linux during compilation with GNU compiler

but this is not nice as not always can a single user do that. How can I increase the stack size in Linux with GCC for a single application?

Attempting to set rlimit_stack after Stack Clash remediations may result in failure or related problems. Also see Red Hat Issue 1463241

5 Answers 5

You can set the stack size programmatically with setrlimit, e.g.

#include 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; > 

Note: even when using this method to increase stack size you should not declare large local variables in main() itself, since you may well get a stack overflow as soon as you enter main() , before the getrlimit / setrlimit code has had a chance to change the stack size. Any large local variables should therefore be defined only in functions which are subsequently called from main() , after the stack size has successfully been increased.

I tried it on SLES 11. Even the value is set I cannot define a local variable according to the new limit —> segmentation fault . Only when I run in the command line ulimit -s 16000 then I can define a local variable like char x [14000000] . Apparently the stack size is not set through this code. Any idea?

@AlBundy: all I can tell you is that the above code works for me on both OS X and various flavours of Linux. One thought occurs to me though: are you trying to declare a large local variable in main() itself ? That won’t work of course, because you’ll get a stack overflow before you even get to the getrlimit / setrlimit code. Put the large local variable in another function and then call this function from main (after the getrlimit / setrlimit code).

@PaulR: OK. I tried it in main() but after I set the new limit. Apparently this does not work. When I call from main() a function which has a huge local variable then it works. I checked it now with different limits and all works perfectly. Conclusion: The stack limit must be set in main() in order to be active for the rest of the program. I had also to close the ssh session because I was playing around with ulimit -s and I got always -1 as return code.

Читайте также:  Kaspersky antivirus для linux

Thanks for confirming that — I’ve added a note to the answer now in case anyone else runs into the same problem.

Источник

How does the gcc determine stack size the function based on C will use?

I write program in C programming language, and I use objdump to translate the executable file to asm file. I have no idea how gcc determine the stack size the function will use?

It’s just for problem explanation, ignore that it’s naïve. Will the gcc allocate really 1024 * 1024 bytes space for function a? If the function is a little complicated, sometimes a lot of local variables, how does the compiler determine the stack size?

if you have problems with stacksize, I would use «-Wlarger-than-xxx» Compiler option to Warn if stack is larger than xxx, and switch to malloc().

2 Answers 2

First, at least without optimizations, GCC will emit code that allocates 1024*1024 int -s (not bytes; often an int is 4 bytes because sizeof(int)==4 ) on the call stack (i.e. 4Mbytes!). This might be too much, and you might get a segmentation fault because of the stack overflow. See also the setrlimit(2) and execve(2) syscalls.

Some versions of GCC are able to optimize your code. On Linux/Debian/Sid/x86-64 the gcc-4.8 -O3 -fverbose-asm -S stonestrong.c command (actually using GCC 4.8.2) is able to optimize your code to:

 .globl a .type a, @function a: .LFB0: .cfi_startproc movl $1, %eax #, ret .cfi_endproc .LFE0: .size a, .-a 

So in your particular case, no stack frame at all is needed when optimizing with -O3 .

The compiler determines the stack size and layout using quite complex optimization algorithms. Each function usually has its own stack frame. When the compiler is optimizing, a given slot of the call frame might be used for several source code variables, and a given source variable might not need any stack slot (because it could be kept in a register), or maybe use several of them (one slot for a block, another for another, etc. ).

You may want to explore the various internal representations (notably Gimple) used by GCC by passing -fdump-tree-all (which will dump hundreds of files!) to your gcc command. You may want to use MELT to extend GCC (by adding your new passes) or inspect the internal representations.

Читайте также:  What hacking can you do with kali linux

Some variables or some intermediate values are not even kept on the stack, but only in register. The compiler works hard (when optimizing) on register allocation (which is a difficult question having its own experts). See also this.

A general rule of thumb when coding in C (or in C++) is to avoid too large call frames; often, you want your local variables to consume no more than a few kilobytes at most.

Program optimization can be very hard; however current compilers are quite good at optimization like the one above. With GCC you need to enable optimizations (e.g. with -O2 or -O3 and many other flags) explicitly. GCC has more than ten millions lines of source code, and half of them are middle-end optimizations (not depending on the source language or the target processor).

Источник

Linux gcc stack size

Hi all. I’m following a course with coursera (algorithm 1) and my program for the assignment crashes inexorably (many are experimenting the same thing there, the assignment asks for a recursive program for solving a big graph of 500000 nodes). I’m using a version of g++ from MinGW with the editor Geany and windows7. The current setup is:

How can I set the stack ‘unlimited’?

(where is in bytes)
to set the stack size.

(From doing some tests, it seems the default is 2 MB)

Having a program crash is about as generic as an error can get. What makes you suspect that the stack size is the issue? Do you get a run time error code? What does event viewer tell you about the crash?

There technically are ways of making an arbitrarily large stack, if you’re willing to mess around with Assembly. It’s a really ugly solution, though, and I don’t recommend it ever.

IMO, if you have to process an arbitrarily large data structure recursively, you should put the recursion somewhere that isn’t the call stack. Hell, even making a dynamically allocated pseudo-stack+switch is better.

Thank you all. I guess Computergeek01 is right. The fact is I’m not able (for now) to make my implementation of Kosaraju’s algorithm work. I have read on the forum of coursera that some have solved their problems by enlarging the stack on their computers, so I was trying this way «blindly», so to say, but I have changed my mind now; after all I’m there to learn. It’s a really hard assignment — compared to my current skills. For some days now I have been trying to write a iterative version of that algorithm, I hope to succeed. Thank you all again.

Читайте также:  Linux no usb keyboard

well, just a little update for those who may need the same piece of information in the future. I succeeded in doing the program work; it was correct and simply needed more memory, as many suggested in the coursera forum, because the graph had 500000 nodes and 5 million edges. In windows7 the default stack size is 1MB. As «long double main» wrote, the command is
-Wl,—stack,
In the GCC Options for Linking (http://gcc.gnu.org/onlinedocs/gcc-3.0/gcc_3.html#SEC16) I read that
-Wl\,option Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas.
In the Geany editor I found by trials that the instruction may be:
g++ -std=c++11 -Wall -Wl,—stack -Wl, -o «%e» «%f»

I set to a very large number, just to solve my problem; then I set again the default:
g++ -std=c++11 -Wall -o «%e» «%f»

Источник

Setting the default stack size on Linux globally for the program

So I’ve noticed that the default stack size for threads on linux is 8MB (if I’m wrong, PLEASE correct me), and, incidentally, 1MB on Windows. This is quite bad for my application, as on a 4-core processor that means 64 MB is space is used JUST for threads! The worst part is, I’m never using more than 100kb of stack per thread (I abuse the heap a LOT ;)). My solution right now is to limit the stack size of threads. However, I have no idea how to do this portably. Just for context, I’m using Boost.Thread for my threading needs. I’m okay with a little bit of #ifdef hell, but I’d like to know how to do it easily first. Basically, I want something like this (where windows_* is linked on windows builds, and posix_* is linked under linux builds)

// windows_stack_limiter.c int limit_stack_size() < // Windows impl. return 0; >// posix_stack_limiter.c int limit_stack_size() < // Linux impl. return 0; >// stack_limiter.cpp int limit_stack_size(); static volatile int placeholder = limit_stack_size(); 

How do I flesh out those functions? Or, alternatively, am I just doing this entirely wrong? Remember I have no control over the actual thread creation (no new params to CreateThread on Windows), as I’m using Boost.Thread.

It’s not as bad as you think. only address space is reserved, not actual RAM. So e.g. if you spawn 8 threads, and each thread actually uses 100kb of stack space, then you’ve only used up 800kb of RAM, not 8MB.

Источник

Оцените статью
Adblock
detector