Framework Registers/Stack restoration on exceptions

J

Jack

What does the framework for stack/registers restoration when an
exception is thrown ?
Is there any way to act on the stack before/after framework does it's
stack restoration ?

I currently know that .NET use the default exception handler with an
array of callback for catch blocks (at least 1.0 and 1.1), but if we
play with stack before an exception is thrown, Framework fails to
restore stack gracefully... Does anyone has an idea ?
 
P

Peter Duniho

What does the framework for stack/registers restoration when an
exception is thrown ?
Is there any way to act on the stack before/after framework does it's
stack restoration ?

I currently know that .NET use the default exception handler with an
array of callback for catch blocks (at least 1.0 and 1.1), but if we
play with stack before an exception is thrown, Framework fails to
restore stack gracefully... Does anyone has an idea ?

Why are you "playing with the stack"? What is it you're actually trying
to do?

Your question seems pretty vague. What goal is it that you're trying to
accomplish and why is that you think it's useful to know how .NET deals
with exceptions in order to address that?

Pete
 
J

Jack

Peter said:
Why are you "playing with the stack"? What is it you're actually trying
to do?

Your question seems pretty vague. What goal is it that you're trying to
accomplish and why is that you think it's useful to know how .NET deals
with exceptions in order to address that?

Pete

So much questions :)

- go inside generated asm code
- adding some stuff on stack
- calling functions
- removing data from stack
- continue

I know we can change .Net opcode and ask to rejit functions but it's not
my goal.

The only thing which make my code crash is if exception thrwon inside a
function isn't catch by itself that means I got no trouble for functions
like
f()
{
try
{
// throw error
}
catch
{
}
}

but for

g1()
{
// throw error
}
g()
{
try
{
g1()
}
catch
{
}
}
Framework use a sucking algorithm (probably based on stack size needed
by function parameters) and badly restore the stack.

That's why I need informations on it...
So from now, you may understand my question better, my have you some
documentation ?
 
P

Peter Duniho

[...]
Framework use a sucking algorithm (probably based on stack size needed
by function parameters) and badly restore the stack.

I'm not sure what you mean by "sucking algorithm", but if you mean that
..NET itself is using a poor algorithm, I'd say you've got a long way to go
before you demonstrate that.
That's why I need informations on it...
So from now, you may understand my question better, my have you some
documentation ?

I don't see that you've really managed to describe what it is you're doing
in a precise enough way. Not that I'm personally likely to have the
answer in any case, but I doubt that even the person who does could
provide it given the information you've only provided so far.

From what you _have_ described, I'd say that if you are auto-generating
assembly code and calling that from .NET and that an exception thrown from
within the ASM code causes problems, then it's your ASM that has
problems. You need to figure out the right way to handle exceptions in
your ASM code so that you don't corrupt the stack.

Pete
 
J

Jack

Peter said:
[...]
Framework use a sucking algorithm (probably based on stack size needed
by function parameters) and badly restore the stack.

I'm not sure what you mean by "sucking algorithm", but if you mean that
.NET itself is using a poor algorithm, I'd say you've got a long way to
go before you demonstrate that.
That's why I need informations on it...
So from now, you may understand my question better, my have you some
documentation ?

I don't see that you've really managed to describe what it is you're
doing in a precise enough way. Not that I'm personally likely to have
the answer in any case, but I doubt that even the person who does could
provide it given the information you've only provided so far.

From what you _have_ described, I'd say that if you are auto-generating
assembly code and calling that from .NET and that an exception thrown
from within the ASM code causes problems, then it's your ASM that has
problems. You need to figure out the right way to handle exceptions in
your ASM code so that you don't corrupt the stack.

Pete

..NET use a "poor algorithm" yes really, instead of saving registers as
c++, c and all other compiled language do, and use the
unexpected_handler as unique exception handler. Anyway

My asm is not generating exception, the flow is the following

The think I want to do is hooking of .NET in win32 (I know
EnterLeaveFunction of ICorprofiler exits)

Entering .NET asm generated code of my .Net function f_NET()
execution of my asm code part1
execution of .Net asm generated code --> my .Net code design to throw an
error (software exception with throw, or an hadware one)
execution of my asm code part2


- if no .Net error is thrown all is ok
- if .Net error is thrown and catch is inside the same .Net function
(f_NET) there is no error, as .Net stack retrieval algo is not used
(stack has not to be restored as exception handler is in the same function)
- if .Net error is catch on function calling f_NET(), stack has to be
restored by .NET framework, and has it seems they don't save esp/ebp but
try to restore them software crash
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top