How to create an exception in an asm block?

I want to throw an exception in an asm X64 block.

Suppose I have a function like this:

function Example(Values: array of integer): integer; asm or rcx,rcx jz @error .... 

I know that I can just read the pointer and get AV, however I want to raise a more descriptive error.

I could make an extra function and call it:

 asm or rcx,rcx jz @error .... @error: mov ecx, 1 mov rdx, ErrorString jmp RaiseError .... function RaiseError(code: integer; const Msg: string); begin case code of 1: raise EEmptyArrayError.Create(Msg); 

However, an error will occur outside the function in which it was called. How to get an exception from (it seems) from inside the Example function.

Note that this is X64, so all SEH responses that are related to X86 are not suitable, because X64 uses VEH.

+5
source share
1 answer

Full raise syntax:

 raise Exception at address 

All you have to do is pass the current IP address as a parameter, and proc proc can pass this exception.

You can get RIP with lea rax, [rip] .

Thus, the code becomes:

 asm or rcx,rcx jz @error .... @error: mov ecx, 1 mov rdx, ErrorString lea r8,[rip] jmp RaiseError .... function RaiseError(code: integer; const Msg: string; address: pointer); begin case code of 1: raise EEmptyArrayError.Create(Msg) at address; 

Of course, in this case it’s easier to use

 function RaiseError(code: integer; const Msg: string); begin case code of 1: raise EEmptyArrayError.Create(Msg) at ReturnAddress; 

Note
In this case, if you save jmp, the error seems to come from the calling routine, in which case it is really correct. If you want the exception to point to the guilty finger on your asm code, then use the call.

+4
source

All Articles