Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-13 Thread FeepingCreature
Note: I worked out this method for my own language, Neat, but the basic approach should be portable to D's exceptions as well. I've seen it argued a lot over the years (even argued it myself) that it's impossible to throw from Linux signal handlers. This is basically correct, because they const

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-13 Thread deadalnix
Le 13/03/2012 11:09, FeepingCreature a écrit : Note: I worked out this method for my own language, Neat, but the basic approach should be portable to D's exceptions as well. I've seen it argued a lot over the years (even argued it myself) that it's impossible to throw from Linux signal handler

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-13 Thread FeepingCreature
On 03/13/12 11:23, deadalnix wrote: > Le 13/03/2012 11:09, FeepingCreature a écrit : >> Note: I worked out this method for my own language, Neat, but the basic >> approach should be portable to D's exceptions as well. >> >> I've seen it argued a lot over the years (even argued it myself) that it's

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-13 Thread Vladimir Panteleev
On Tuesday, 13 March 2012 at 10:09:55 UTC, FeepingCreature wrote: However, there is a method to turn a signal handler into a regular function call that you can throw from. Very nice! The only similarity with a buffer overflow exploit is that we're overriding the continuation address. There is

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-13 Thread H. S. Teoh
On Tue, Mar 13, 2012 at 11:09:54AM +0100, FeepingCreature wrote: [...] > I've seen it argued a lot over the years (even argued it myself) that > it's impossible to throw from Linux signal handlers. This is basically > correct, because they constitute an interruption in the stack that > breaks excep

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread FeepingCreature
On 03/13/12 23:24, Vladimir Panteleev wrote: > On Tuesday, 13 March 2012 at 10:09:55 UTC, FeepingCreature wrote: >> However, there is a method to turn a signal handler into a regular function >> call that you can throw from. > > Very nice! > > The only similarity with a buffer overflow exploit i

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread deadalnix
Le 13/03/2012 23:24, Vladimir Panteleev a écrit : On Tuesday, 13 March 2012 at 10:09:55 UTC, FeepingCreature wrote: However, there is a method to turn a signal handler into a regular function call that you can throw from. Very nice! The only similarity with a buffer overflow exploit is that w

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread FeepingCreature
On 03/14/12 12:13, deadalnix wrote: > Le 13/03/2012 23:24, Vladimir Panteleev a écrit : >> I think something like this needs to end up in Druntime, at least for >> Linux x86 and x64. > > You are loosing EAX in the process. It's somewhat unavoidable. One way or another, you need to find _some_ thr

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Vladimir Panteleev
On Wednesday, 14 March 2012 at 11:11:54 UTC, deadalnix wrote: You are loosing EAX in the process. When would this matter? EAX is a scratch register per ABIs, no?

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Vladimir Panteleev
On Wednesday, 14 March 2012 at 07:35:50 UTC, FeepingCreature wrote: Sweet. Yeah, I think you need to use naked and reconstruct the stackframe. Not sure how it'd look; I'm not familiar with the x86_64 ABI. I think it might be safe to just reconstruct the stack frame in the signal handler, and

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread deadalnix
Le 14/03/2012 17:08, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 11:11:54 UTC, deadalnix wrote: You are loosing EAX in the process. When would this matter? EAX is a scratch register per ABIs, no? You may want to return from the function the standard way an resume operations.

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread deadalnix
Le 14/03/2012 17:34, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 07:35:50 UTC, FeepingCreature wrote: Sweet. Yeah, I think you need to use naked and reconstruct the stackframe. Not sure how it'd look; I'm not familiar with the x86_64 ABI. I think it might be safe to just recons

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread deadalnix
Le 14/03/2012 14:43, FeepingCreature a écrit : On 03/14/12 12:13, deadalnix wrote: Le 13/03/2012 23:24, Vladimir Panteleev a écrit : I think something like this needs to end up in Druntime, at least for Linux x86 and x64. You are loosing EAX in the process. It's somewhat unavoidable. One way

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Vladimir Panteleev
On Wednesday, 14 March 2012 at 16:39:29 UTC, deadalnix wrote: Le 14/03/2012 17:34, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 07:35:50 UTC, FeepingCreature wrote: Sweet. Yeah, I think you need to use naked and reconstruct the stackframe. Not sure how it'd look; I'm not familiar

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Vladimir Panteleev
On Wednesday, 14 March 2012 at 16:37:45 UTC, deadalnix wrote: Le 14/03/2012 17:08, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 11:11:54 UTC, deadalnix wrote: You are loosing EAX in the process. When would this matter? EAX is a scratch register per ABIs, no? You may want to r

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread H. S. Teoh
On Wed, Mar 14, 2012 at 05:39:38PM +0100, deadalnix wrote: > Le 14/03/2012 17:08, Vladimir Panteleev a écrit : > >On Wednesday, 14 March 2012 at 11:11:54 UTC, deadalnix wrote: > >>You are loosing EAX in the process. > > > >When would this matter? EAX is a scratch register per ABIs, no? > > You may

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread deadalnix
Le 14/03/2012 18:00, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 16:37:45 UTC, deadalnix wrote: Le 14/03/2012 17:08, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 11:11:54 UTC, deadalnix wrote: You are loosing EAX in the process. When would this matter? EAX is a

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Vladimir Panteleev
On Wednesday, 14 March 2012 at 17:18:06 UTC, deadalnix wrote: Le 14/03/2012 18:00, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 16:37:45 UTC, deadalnix wrote: Le 14/03/2012 17:08, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 11:11:54 UTC, deadalnix wrote: You are

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread deadalnix
Le 14/03/2012 18:28, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 17:18:06 UTC, deadalnix wrote: Le 14/03/2012 18:00, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 16:37:45 UTC, deadalnix wrote: Le 14/03/2012 17:08, Vladimir Panteleev a écrit : On Wednesday, 14 Ma

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread deadalnix
Le 14/03/2012 18:01, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 16:39:29 UTC, deadalnix wrote: Le 14/03/2012 17:34, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 07:35:50 UTC, FeepingCreature wrote: Sweet. Yeah, I think you need to use naked and reconstruct the s

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Vladimir Panteleev
On Wednesday, 14 March 2012 at 19:48:28 UTC, deadalnix wrote: Le 14/03/2012 18:28, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 17:18:06 UTC, deadalnix wrote: Le 14/03/2012 18:00, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 16:37:45 UTC, deadalnix wrote: Le 14/0

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Don Clugston
On 13/03/12 11:09, FeepingCreature wrote: Note: I worked out this method for my own language, Neat, but the basic approach should be portable to D's exceptions as well. I've seen it argued a lot over the years (even argued it myself) that it's impossible to throw from Linux signal handlers. Th

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread deadalnix
t turning SIGSEGV into an exception that 1) you can catch 2) will print a stack trace when uncaught. You've brought in stack overflows, moving garbage collectors, etc. I assure you, we are well-aware of the problems when using this exact code for other purposes. The topic is *Turning a SIGSEG

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Vladimir Panteleev
On Wednesday, 14 March 2012 at 20:20:05 UTC, deadalnix wrote: The topic is *Turning a SIGSEGV into a regular function call under Linux, allowing throw*, not only Exception. I don't understand what is the problem here ? Can't we talk about how we could keep trash register clean in cas

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Steven Schveighoffer
On Wed, 14 Mar 2012 16:08:29 -0400, Don Clugston wrote: Now, your user space handler will cause another segfault when it does the mov [ESP], 0. I think that gives you an infinite loop. SEGFAULT inside a SEGV signal handler aborts the program (no way to turn this off IIRC). -Steve

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Don Clugston
On 14/03/12 21:31, Steven Schveighoffer wrote: On Wed, 14 Mar 2012 16:08:29 -0400, Don Clugston wrote: Now, your user space handler will cause another segfault when it does the mov [ESP], 0. I think that gives you an infinite loop. SEGFAULT inside a SEGV signal handler aborts the program (no

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread FeepingCreature
On 03/14/12 21:08, Don Clugston wrote: > > I didn't realize that was possible. Very interesting. > As it stands, though, that's got some pretty serious issues. > > You are on the stack of the function that was called, but you don't know for > sure that it is a valid stack. > > asm { > push

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Steven Schveighoffer
On Wed, 14 Mar 2012 16:45:49 -0400, Don Clugston wrote: On 14/03/12 21:31, Steven Schveighoffer wrote: On Wed, 14 Mar 2012 16:08:29 -0400, Don Clugston wrote: Now, your user space handler will cause another segfault when it does the mov [ESP], 0. I think that gives you an infinite loop. S

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Sean Kelly
On Mar 14, 2012, at 1:54 PM, FeepingCreature wrote: > > I think that case is sufficiently rare that it'd have to count somewhere > between "act of god" and "outright developer malice". The assumption that the > stack frame is valid is, I'd say, safe to make in the vast majority of cases. > You

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread deadalnix
Le 14/03/2012 21:28, Vladimir Panteleev a écrit : On Wednesday, 14 March 2012 at 20:20:05 UTC, deadalnix wrote: The topic is *Turning a SIGSEGV into a regular function call under Linux, allowing throw*, not only Exception. I don't understand what is the problem here ? Can't we talk ab

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread deadalnix
Le 14/03/2012 21:53, Steven Schveighoffer a écrit : On Wed, 14 Mar 2012 16:45:49 -0400, Don Clugston wrote: On 14/03/12 21:31, Steven Schveighoffer wrote: On Wed, 14 Mar 2012 16:08:29 -0400, Don Clugston wrote: Now, your user space handler will cause another segfault when it does the mov [

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread deadalnix
Le 14/03/2012 21:59, Sean Kelly a écrit : On Mar 14, 2012, at 1:54 PM, FeepingCreature wrote: I think that case is sufficiently rare that it'd have to count somewhere between "act of god" and "outright developer malice". The assumption that the stack frame is valid is, I'd say, safe to make i

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Steven Schveighoffer
On Wed, 14 Mar 2012 17:25:28 -0400, deadalnix wrote: Le 14/03/2012 21:53, Steven Schveighoffer a écrit : On Wed, 14 Mar 2012 16:45:49 -0400, Don Clugston wrote: On 14/03/12 21:31, Steven Schveighoffer wrote: On Wed, 14 Mar 2012 16:08:29 -0400, Don Clugston wrote: Now, your user space

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread Don Clugston
On 14/03/12 21:59, Sean Kelly wrote: On Mar 14, 2012, at 1:54 PM, FeepingCreature wrote: I think that case is sufficiently rare that it'd have to count somewhere between "act of god" and "outright developer malice". The assumption that the stack frame is valid is, I'd say, safe to make in the

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-14 Thread H. S. Teoh
On Wed, Mar 14, 2012 at 05:35:04PM -0400, Steven Schveighoffer wrote: [...] > I get that. What I was saying is, I thought even the signal handler > uses the stack (thereby it would abort if invalid). And even if it > doesn't, simply accessing the stack by loading it into a register > should be su

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-15 Thread deadalnix
Here is a proof of concept of how we can recover from segfault. This isn't perfect as it doesn't protect everything (like floating point registers). This is mostly because I can't find the precise documentation about what must be saved or not. The handler call a naked function that will set u

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-15 Thread Kagamin
Does it recover from void function() f=null; f();

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-15 Thread FeepingCreature
On 03/15/12 16:16, Kagamin wrote: > Does it recover from > > void function() f=null; > f(); Not as currently written, no. It should be possible to detect this case and get a proper stackframe back, though.

Re: Turning a SIGSEGV into a regular function call under Linux, allowing throw

2012-03-17 Thread deadalnix
Le 15/03/2012 21:20, FeepingCreature a écrit : On 03/15/12 16:16, Kagamin wrote: Does it recover from void function() f=null; f(); Not as currently written, no. It should be possible to detect this case and get a proper stackframe back, though. It is supported as written in my sample code.