flat assembler
Message board for the users of flat assembler.
Index
> Windows > SEH + RaiseException: Accessing register values |
Author |
|
revolution 19 Aug 2015, 12:06
If you want to restore all the registers to some known state then you can do that in an exception handler. And the natural way to do that is to save the values somewhere in RAM for recovery later. There are no other places you could store such values.
Or maybe I misunderstand your problem? However I think that a better approach than using exceptions is to make a small macro that wraps the allocate function with the 'test' and 'jz' instructions all in one. In the long term it will save you much hair pulling when you need to debug a problem. Exceptions after the fact can be tricky to diagnose when debugging. |
|||
19 Aug 2015, 12:06 |
|
DimonSoft 19 Aug 2015, 14:37
revolution wrote: If you want to restore all the registers to some known state then you can do that in an exception handler. And the natural way to do that is to save the values somewhere in RAM for recovery later. There are no other places you could store such values. I guess, you understood it correctly. I just needed a confirmation Thanks. revolution wrote: However I think that a better approach than using exceptions is to make a small macro that wraps the allocate function with the 'test' and 'jz' instructions all in one. In the long term it will save you much hair pulling when you need to debug a problem. Exceptions after the fact can be tricky to diagnose when debugging. Yep, debugging them is quite tricky |
|||
19 Aug 2015, 14:37 |
|
l_inc 19 Aug 2015, 14:50
DimonSoft
Quote: I’ve written a small test program, and saw that the CONTEXT structure Your code is not referring to the CONTEXT structure. It incorrectly refers to the establisher frame structure. Quote: values that the registers had inside somewhere inside RaiseException I'm not sure what particular guarantees does RaiseException give at the moment of exception generation, but when calling a function you should be ready that it corrupts eax, ecx and edx. If you want to generate an exception and preserve register values, then you should do int3 or ud2 or whatever other single instruction generating an exception. If you want to process exceptions generated by HeapAlloc, then you should store at least the return pointer (corresponding to the C __except block) in your establisher frame, so that you can put it into the CONTEXT structure before returning ExceptionContinueExecution. Whether you store ebp, ebx, esi and edi depends on the implementation of your function that calls HeapAlloc. But I'd actually agree with the revolution's remark regarding a different approach. Quote:
This is not how endp is meant to be used and may result in unwanted effects. There's no support for nested functions in fasm macros. _________________ Faith is a superposition of knowledge and fallacy |
|||
19 Aug 2015, 14:50 |
|
AsmGuru62 19 Aug 2015, 15:26
May I suggest something?.. it is slightly of topic, however...
I noticed that you have a few allocations in a row with a constant sizes passed. You can improve your code by allocation one block of memory as a sum of sizes and then cutting the block into pieces later on. This way you would need only one TEST EAX,EAX - something like that: Code: invoke HeapAlloc, [heap], 0, (sizeOfInstance + sizeOfFld1 + sizeOfFld2) test eax, eax jz .failed_object_allocation mov [ptrObject], eax mov edi, eax add eax, sizeOfInstance mov [edi + Fld1], eax add eax, sizeOfFld1 mov [edi + Fld2], eax ... successful allocation ... .failed_object_allocation: ... error handling here ... Also, one HeapAlloc call is shorter than three with all parameters - a bonus! |
|||
19 Aug 2015, 15:26 |
|
typedef 19 Aug 2015, 18:48
^I like that, but the trouble comes when you have to free any one of the higher bound pointers. You can't, since the headers aren't there. So, you'll have to keep the whole block until you are done with at least the lower bound pointer at mem_block+$00000000
|
|||
19 Aug 2015, 18:48 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.