ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Basically what I want to do is to alert the program itself when stack corruption happens, either through a signal or any other method and want to terminate it. I dont want the execution to continue and end up failing somewhere else in the code.
For this I want to know some things like this,
In C, how stack, heap etc are allocated when I am creating an executable?
Who determines how much memory my exe should use w.r.t heap , stack or any other segments?
Suppose I want to alert my program regarding a possible when stack gets corrupted, what is the best way to do it?
I know these are some random questions not much related to each other, but if you can give me some pointers regarding this, I may be able to explain my question better..
Basically what I want to do is to alert the program itself when stack corruption happens, either through a signal or any other method and want to terminate it. I dont want the execution to continue and end up failing somewhere else in the code.
I don't know what the background of your plans is, but such a feature is already (vaguely) implemented, at least in GCC. Check out the option -fstack-protector. This feature is a bit different from what you want to achieve, because it basically recognize the stack corruption on function exit, but maybe this helps you to get further pointers.
I personally think that it is not really easy to detect this in real-time, meaning on the first write attempt, but as another hint, you could check out how hardware watchpoints (e.g. in GDB) are implemented.
Quote:
Originally Posted by sree_ec
In C, how stack, heap etc are allocated when I am creating an executable?
Initially, when the program starts, the heap (data segment) and stack are allocated by the operating system and then enhanced by the program on purpose via malloc(3) calls or function calls (increasing/decreasing the stack). When you create/compile an executable, you don't yet have a stack. The stack is finally created/allocated at runtime.
Quote:
Originally Posted by sree_ec
Who determines how much memory my exe should use w.r.t heap , stack or any other segments?
This might not be generally true for all platforms/operating systems out there, but this could be determined by:
shell/system settings (e.g. ulimit)
address space limits (e.g. heap would run into the stack or vice-versa)
With regard to the segments and the initial sizes: check out the size(1) command on Linux, like
Code:
# size test.exe
text data bss dec hex filename
1332 528 16 1876 754 test.exe
Quote:
Originally Posted by sree_ec
Suppose I want to alert my program regarding a possible when stack gets corrupted, what is the best way to do it?
The question is where do you want to detect this? In the operating system kernel, in your user application code? The usual UNIX like alert method would be to use signals, but this greatly depends on your implementation.
I hope that these answers help you to find further pointers, if not feel free to ask,
Andi
I don't know what the background of your plans is, but such a feature is already (vaguely) implemented, at least in GCC. Check out the option -fstack-protector. This feature is a bit different from what you want to achieve, because it basically recognize the stack corruption on function exit, but maybe this helps you to get further pointers.
I personally think that it is not really easy to detect this in real-time, meaning on the first write attempt, but as another hint, you could check out how hardware watchpoints (e.g. in GDB) are implemented.
Initially, when the program starts, the heap (data segment) and stack are allocated by the operating system and then enhanced by the program on purpose via malloc(3) calls or function calls (increasing/decreasing the stack). When you create/compile an executable, you don't yet have a stack. The stack is finally created/allocated at runtime.
This might not be generally true for all platforms/operating systems out there, but this could be determined by:
shell/system settings (e.g. ulimit)
address space limits (e.g. heap would run into the stack or vice-versa)
With regard to the segments and the initial sizes: check out the size(1) command on Linux, like
Code:
# size test.exe
text data bss dec hex filename
1332 528 16 1876 754 test.exe
The question is where do you want to detect this? In the operating system kernel, in your user application code? The usual UNIX like alert method would be to use signals, but this greatly depends on your implementation.
I hope that these answers help you to find further pointers, if not feel free to ask,
Andi
You said stack is allocated at run time,but whether linker scripts play any role in this? Atleast in determining the starting address of these memory segments?
Is there any mechanism where I can know the pages where stack is allocated or not allocated and then try to alert my program when it writes into wrong page?[ I heard this somewhere but I never got what it really meant, thats why I am asking here]
Or Is there any way atleast I can know from which address to other, the stack will grow? [In a multithreaded application]?
You said stack is allocated at run time,but whether linker scripts play any role in this? Atleast in determining the starting address of these memory segments?
That's true. By using a linker script, you are change segment start addresses. Here's also a document about howto set the stack address for e.g. embedded systems. Not sure if that helps you: http://sunsite.ualberta.ca/Documenta...e/ldint_5.html
Quote:
Originally Posted by sree_ec
Is there any mechanism where I can know the pages where stack is allocated or not allocated and then try to alert my program when it writes into wrong page?[ I heard this somewhere but I never got what it really meant, thats why I am asking here]
Mhhh, well the kernel obviously knows this. And from userspace, at least in GNU/Linux systems, you could use the /proc-filesystem, e.g. /proc/<pid>/maps
I don't know what the background of your plans is, but such a feature is already (vaguely) implemented, at least in GCC. Check out the option -fstack-protector. This feature is a bit different from what you want to achieve, because it basically recognize the stack corruption on function exit, but maybe this helps you to get further pointers.
The program executed without any error..I expected some error as you said after executing call_func1().
But did not get anything. I had assigned my thread a stack size of 1256 and declared an array of 8000bytes. This should have caused a stack overflow right? Or am I missing something here?
Last edited by sree_ec; 12-15-2010 at 03:08 PM.
Reason: corrected the program
The pthread_attr_setstacksize() function sets the stack size attribute of the thread attributes object referred to
by attr to the value specified in stacksize.
The stack size attribute determines the minimum size (in bytes) that will be allocated for threads created using
the thread attributes object attr.
The compiler is smart enough to recognize that you need more stack than the minimum defined. So, when you invoke that thread, you get enough stack.
Stack overflows are a bad thing and the system will try to stop that from happening.
If you want to overflow your stack, start writing to that array after you define it on the stack, and don't stop writing when you reach the end of the array.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.