exiting programs
exiting programs
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?
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: exiting programs
Here's your hintyemista wrote:lets say someone builds a program and the main function never calls exit, just returns
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
- piranha
- Member
- Posts: 1391
- Joined: Thu Dec 21, 2006 7:42 pm
- Location: Unknown. Momentum is pretty certain, however.
- Contact:
Re: exiting programs
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
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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
-
- Member
- Posts: 524
- Joined: Sun Nov 09, 2008 2:55 am
- Location: Pennsylvania, USA
Re: exiting programs
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.
EDIT: CRT is just one example, other languages typically have a similar mechanism.
Last edited by JohnnyTheDon on Thu Jan 29, 2009 5:44 pm, edited 1 time in total.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: exiting programs
That won't work, main() might return before the end of its code.piranha wrote:Either that or the kernel adds an exit call to the program at the end of its code itself.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
- piranha
- Member
- Posts: 1391
- Joined: Thu Dec 21, 2006 7:42 pm
- Location: Unknown. Momentum is pretty certain, however.
- Contact:
Re: exiting programs
Yeah, thats true, but I've seen some OSs do that.Love4Boobies wrote:That won't work, main() might return before the end of its code.piranha wrote:Either that or the kernel adds an exit call to the program at the end of its code itself.
But yeah, your right, scratch that off my post.
-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
Re: exiting programs
The address of said call to exit could be pushed on the stack before calling main.Love4Boobies wrote:That won't work, main() might return before the end of its code.piranha wrote:Either that or the kernel adds an exit call to the program at the end of its code itself.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: exiting programs
How would that help?Hangin10 wrote:The address of said call to exit could be pushed on the stack before calling main.Love4Boobies wrote:That won't work, main() might return before the end of its code.piranha wrote:Either that or the kernel adds an exit call to the program at the end of its code itself.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
- Firestryke31
- Member
- Posts: 550
- Joined: Sat Nov 29, 2008 1:07 pm
- Location: Throw a dart at central Texas
- Contact:
Re: exiting programs
When main returns, it would 'return' to the beginning of the exit function.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: exiting programs
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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: exiting programs
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.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.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: exiting programs
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.Hangin10 wrote: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.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.
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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
-
- Member
- Posts: 524
- Joined: Sun Nov 09, 2008 2:55 am
- Location: Pennsylvania, USA
Re: exiting programs
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.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: exiting programs
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...
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: exiting programs
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.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?
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}