Page 1 of 2
exiting programs
Posted: Thu Jan 29, 2009 5:18 pm
by yemista
How do you guys handle finished programs. I know how to do it with the system call exit, but lets say someone builds a program and the main function never calls exit, just returns, how do you detect that?
Re: exiting programs
Posted: Thu Jan 29, 2009 5:30 pm
by Love4Boobies
yemista wrote:lets say someone builds a program and the main function never calls exit, just returns
Here's your hint
Re: exiting programs
Posted: Thu Jan 29, 2009 5:42 pm
by piranha
Most programs get linked with an assembly file that defines _start, which the OS calls. Simple _start functions call main, and then also call exit.
So when main returns, exit is still called.
Either that or the kernel adds an exit call to the program at the end of its code itself.
-JL
Re: exiting programs
Posted: Thu Jan 29, 2009 5:43 pm
by JohnnyTheDon
The main function isn't usually called directly. When a program is run execution starts in CRT, which sets up stuff like malloc and free and then calls main. When main returns, CRT calls exit() or whatever system call you have set up. If you define main as the entry point and it returns, the program will terminate due to a page fault (ret will try to pop EIP off of part of the stack that doesn't exist) if you're using paging, otherwise it will terminate with a segment fault for the same reason.
EDIT: CRT is just one example, other languages typically have a similar mechanism.
Re: exiting programs
Posted: Thu Jan 29, 2009 5:44 pm
by Love4Boobies
piranha wrote:Either that or the kernel adds an exit call to the program at the end of its code itself.
That won't work, main() might return before the end of its code.
Re: exiting programs
Posted: Thu Jan 29, 2009 5:48 pm
by piranha
Love4Boobies wrote:piranha wrote:Either that or the kernel adds an exit call to the program at the end of its code itself.
That won't work, main() might return before the end of its code.
Yeah, thats true, but I've seen some OSs do that.
But yeah, your right, scratch that off my post.
-JL
Re: exiting programs
Posted: Thu Jan 29, 2009 6:07 pm
by Hangin10
Love4Boobies wrote:piranha wrote:Either that or the kernel adds an exit call to the program at the end of its code itself.
That won't work, main() might return before the end of its code.
The address of said call to exit could be pushed on the stack before calling main.
Re: exiting programs
Posted: Thu Jan 29, 2009 6:43 pm
by Love4Boobies
Hangin10 wrote:Love4Boobies wrote:piranha wrote:Either that or the kernel adds an exit call to the program at the end of its code itself.
That won't work, main() might return before the end of its code.
The address of said call to exit could be pushed on the stack before calling main.
How would that help?
Re: exiting programs
Posted: Thu Jan 29, 2009 6:59 pm
by Firestryke31
When main returns, it would 'return' to the beginning of the exit function.
Re: exiting programs
Posted: Thu Jan 29, 2009 7:15 pm
by Love4Boobies
Not necessarily, the stack might have been corrupted in the meantime... Or not even corrupted, a simple PUSH without a POP is enough to return a bogus address.
Re: exiting programs
Posted: Thu Jan 29, 2009 7:21 pm
by Hangin10
Love4Boobies wrote:Not necessarily, the stack might have been corrupted in the meantime... Or not even corrupted, a simple PUSH without a POP is enough to return a bogus address.
Then if the code doesn't fail soon, it'll hopefully fail later. It becomes, I think, a if-and-how-soon-should-bad-code-fail problem.
Re: exiting programs
Posted: Thu Jan 29, 2009 7:30 pm
by Love4Boobies
Hangin10 wrote:Love4Boobies wrote:Not necessarily, the stack might have been corrupted in the meantime... Or not even corrupted, a simple PUSH without a POP is enough to return a bogus address.
Then if the code doesn't fail soon, it'll hopefully fail later. It becomes, I think, a if-and-how-soon-should-bad-code-fail problem.
Take the case where the address on the stack points to the current program's code section. You might be able to force a page fault or a general protection fault if not, but in this case, the results could be disatrous as random code would be able to run.
However, if you mean that the process manager should be the one handling the stack, then that would probably be the best way to go.
Re: exiting programs
Posted: Thu Jan 29, 2009 8:16 pm
by JohnnyTheDon
If stack corruption happens ever, you can return to random (or selected) code. Corruption of the return from main is just as bad as corrupting the return from any other function. It can excecute random code, but any code excecuted will be in user mode.
Re: exiting programs
Posted: Thu Jan 29, 2009 8:19 pm
by Love4Boobies
User mode code can't trash other app's memory, but in most OSes, it can trash files and other stuff. I admit that the probability for this to happen is very low for most applications. You do have a point about other procedures not returning to the right place, but at least taking care of this will give us some comfort...
Re: exiting programs
Posted: Thu Jan 29, 2009 9:33 pm
by neon
yemista wrote:How do you guys handle finished programs. I know how to do it with the system call exit, but lets say someone builds a program and the main function never calls exit, just returns, how do you detect that?
The runtime C library calls the programs entry point. Because all programs are linked with this library, when the entry point returns it simply returns back to the CRT which in turn terminates the program via the system API.