exiting programs

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

exiting programs

Post 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?
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: exiting programs

Post by Love4Boobies »

yemista wrote:lets say someone builds a program and the main function never calls exit, just returns
Here's your hint :wink:
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: exiting programs

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: exiting programs

Post 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.
Last edited by JohnnyTheDon on Thu Jan 29, 2009 5:44 pm, edited 1 time in total.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: exiting programs

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: exiting programs

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: exiting programs

Post 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.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: exiting programs

Post 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?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: exiting programs

Post by Firestryke31 »

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?
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: exiting programs

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: exiting programs

Post 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.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: exiting programs

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: exiting programs

Post 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.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: exiting programs

Post 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...
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: exiting programs

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Post Reply