AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein In Asm-Prozedur eine Exception auslösen
Thema durchsuchen
Ansicht
Themen-Optionen

In Asm-Prozedur eine Exception auslösen

Ein Thema von Amateurprofi · begonnen am 2. Nov 2023 · letzter Beitrag vom 11. Nov 2023
Antwort Antwort
Kas Ob.

Registriert seit: 3. Sep 2023
412 Beiträge
 
#1

AW: In Asm-Prozedur eine Exception auslösen

  Alt 4. Nov 2023, 08:29
@Kas Ob.
Thanks for your detailed comments in #8.
Unfortunately not was I am looking for, because I dont want to call procedures outside of my Asm-Procedure (except system-routines).

What I am currently using is the following:

Delphi-Quellcode:
PROCEDURE RaiseException(const S:String);
begin
   raise Exception.Create(S);
end;
Delphi-Quellcode:
PROCEDURE Test;
const S:String='My Message';
asm
   {$IFDEF CPUX86}
   mov eax,S
   {$ELSE}
   mov rcx,S
   {$ENDIF}
   jmp RaiseException
end;
Thank you for clarifying and sorry if i wasn't clear enough.
Delphi assembler has very limited capability, eg. that is why you have to declare constant strings to be used with the assembler with typed strings.

My function above "ExceptionAsm32" is the closest thing you can do with assembly to create and raise exception without writing very complex and unreliable assembly code for Delphi assembler, because it is inconsistent not allow you to get or access specific types, VMT, offsets..etc

beantwortet, also:
Code:
  mov  eax,????
That i had answered , if you don't need to declare your own (or will not multiple ) then replace TMyException with the default Exception.
Code:
const
  pMyExClass: ExceptClass = (TMyException);
  ..
asm
  ..
  mov   eax, pMyExClass; // <-----
That way you can use multiple exception from asm block, only i use create because there is no way or at least no way i know to access the default constructor with compiler help, your assembly can't have the default constructor index to call Create directly for specific class.

ps: about this
Delphi-Quellcode:
PROCEDURE Test;
const S:String='My Message';
asm
   {$IFDEF CPUX86}
   mov eax,S
   {$ELSE}
   mov rcx,S
   {$ENDIF}
   jmp RaiseException
end;
I am against using jmp to raise exception, this will prevent one push on the stack which might leads to whole different story and confusion, just use call, the more code addresses on the stack the safer for OS to rewind correctly, see, when the system will unwind it might not see your that Test procedure at all and will report the caller to Test as the exception raiser.
Kas
  Mit Zitat antworten Zitat
Benutzerbild von paule32.jk
paule32.jk

Registriert seit: 24. Sep 2022
Ort: Planet Erde
356 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: In Asm-Prozedur eine Exception auslösen

  Alt 4. Nov 2023, 08:48
ich weiß gerade nicht, was IHR so an der Fragestellung versteht, was ich nicht verstehe...
Für mich (und was der Fragestellende gepostet hat, ist, das er mittels Einzeiler eine Exception bzw. eine System-Routine aufrufen will, OHNE viel Delphi zu verwenden.

Einfach einen Einzeiler Assembler mittels inline Code zu realisieren geht halt nicht, weil IHR schon geschrieben habt, das eine Instanz zu einer Exception Klasse vorliegen muss.
Klassen werden intern in einer virtuellen Tabelle aufgezeichnet, die dann auch Einsprung-Addressen für die Routinen speichert.

Man kann zwar einen einen Einzeiler Delphi Code schreiben - raise EException.Create('message');
und wie man hier schon erkennen kann, wird "Create" dafür benutzt, eine Instanz zu erstellen.

Aber in Assembler ist das recht aufwendiger und man kann nicht einfach Assembler-Code übernehmen, nur weil man denkt, das der finktionieren würde...
Mit Create werden auch andere Dinge "vorbereitet" Stack-Segment abgebaut, damit es keinen Überlauf-Fehler gibt und und und...

OOP Programmierung, wie man die unter Delphi betreibt, ist halt keine direkte funktionale Programmierung, so dass man nicht einfach wie bei RECORDS, einfach mal so in eine Klassen-Struktur irgendwelche Daten reinpumpen kann...

Aus den genannten und sicherlich aich bekannten Gründen, habe ich ein funktionales Beispiel in Assembler beigefügt, im meinen letzten Posting. Ich weiß jetzt nicht inwiefern IHR Euch das angeschaut habt: aber es soll zeigen, das mehr als nur ein Einzeiler nötig ist, um einen Exception-Handdler aufzurufen.

Wenn IHR das nicht verstehen könnt oder wollt, ist das nicht meine Schuld ...

Und unter uns:
man kann den Code, den ich Euch vorgelegt habe auch dazu nutzen, Einen Exception-Handler aufzurufen.
Dazu kann man ihn in inline Assembler einbetten und dann als Funktion einzeln aufrufen, oder als Objekt-Datei einbinden (dann sind aber wieder Aufrufkonventionen zu beachten, falls Parameter übergeben werden sollen)...

Ich habe jetzt keinen Delphi-Dissambler gefunden, aber hier mal an Hand von C++
C++ Exception per TRY CATCH
Frag doch einfach
Alles was nicht programmiert werden kann, wird gelötet

Geändert von paule32.jk ( 4. Nov 2023 um 09:02 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.960 Beiträge
 
Delphi 12 Athens
 
#3

AW: In Asm-Prozedur eine Exception auslösen

  Alt 4. Nov 2023, 09:41
@Kas Ob.
Thanks for your detailed comments in #8.
Unfortunately not was I am looking for, because I dont want to call procedures outside of my Asm-Procedure (except system-routines).
Hattest du diesen Teil seines Codes übersehen?
Here a simple code that will work for 32bit
Code:
//[..]
procedure ExceptionAsm32;
const
  pMyExClass: ExceptClass = (TMyException);
  LocalExceptionMsg : string = 'Error from Assembly';
asm
  mov    edx, LocalExceptionMsg
  mov    eax, pMyExClass
  Call   GetException
  Call   system.@RaiseExcept
end;
Einfach einen Einzeiler Assembler mittels inline Code zu realisieren geht halt nicht, weil IHR schon geschrieben habt, das eine Instanz zu einer Exception Klasse vorliegen muss.
Klaus hat doch schon in seinem Eingangspost den notwendigen Assemblercode gepostet. Die einzige Frage war, wie er nun EAX korrekt befüllen muss. Die Lösung hatte Kas Ob. ja auch schon gepostet.

Ich habe jetzt keinen Delphi-Dissambler gefunden
Delphi?
DelphiAsm.png
Sebastian Jänicke
AppCentral
  Mit Zitat antworten Zitat
Amateurprofi

Registriert seit: 17. Nov 2005
Ort: Hamburg
1.100 Beiträge
 
Delphi XE2 Professional
 
#4

AW: In Asm-Prozedur eine Exception auslösen

  Alt 5. Nov 2023, 02:37
@jaenicke
Zitat:
Hattest du diesen Teil seines Codes übersehen?
Nein, aber ich habe nicht gleich realisiert, dass das die Lösung sein könnte.
Danke für den Hinweis, werde mich im Laufe des Tages darum kümmern, jetzt erstmal 2-3 Stunden schlafen.
Gruß, Klaus
Die Titanic wurde von Profis gebaut,
die Arche Noah von einem Amateur.
... Und dieser Beitrag vom Amateurprofi....
  Mit Zitat antworten Zitat
Amateurprofi

Registriert seit: 17. Nov 2005
Ort: Hamburg
1.100 Beiträge
 
Delphi XE2 Professional
 
#5

AW: In Asm-Prozedur eine Exception auslösen

  Alt 5. Nov 2023, 08:11
@jaenicke
Zitat:
Hattest du diesen Teil seines Codes übersehen?
Nein, aber ich habe nicht gleich realisiert, dass das die Lösung sein könnte.
Danke für den Hinweis, werde mich im Laufe des Tages darum kümmern, jetzt erstmal 2-3 Stunden schlafen.
@jaenicke
So, bin wieder da.
Hab mir das angeschaut und mir ist auch wieder eingefallen, was mich gestört hatte, nämlich der Aufruf der Pascal-Routine "GetException", die außerhalb des Asm-Blocks liegt, was ich ja gerade vermeiden wollte.
Nach näherer Betrachtung ließ sich das aber leicht beheben.
Die nachstehende Prozedur funktioniert unter 32Bit, nicht aber unter 64Bit (Wie Kas Ob. auch schon deutlich machte).
Wäre schön, wenn da noch einmal jemand, der das durchschaut, reinschauen könnte.
Delphi-Quellcode:
PROCEDURE Test;
const
   pExClass:ExceptClass=(Exception);
   sErr:String='Meine Meldung';
asm
   {$IfDef CPUX86}
   mov ecx,sErr
   mov dl,1
   mov eax,pExClass
   call Exception.Create
   call System.@RaiseExcept
   {$Else}
   mov r8,sErr
   mov dl,$01
   mov rcx,pExClass
   call Exception.Create // Scheint zu funktionieren
   mov rcx,rax
   call System.@RaiseExcept // Hier krachts. "ACCESS VIOLATION"
   {$EndIf}
end;
Gruß, Klaus
Die Titanic wurde von Profis gebaut,
die Arche Noah von einem Amateur.
... Und dieser Beitrag vom Amateurprofi....
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
412 Beiträge
 
#6

AW: In Asm-Prozedur eine Exception auslösen

  Alt 5. Nov 2023, 10:22
I am trying here to explain and language barrier is playing huge role.

This will work on x64 for this exact procedure
Code:
PROCEDURE test;
const
   pExClass:ExceptClass=( Exception );
   sErr:String=' My message ';
asm
    {$IfDef CPUX86}
   mov ecx,sErr
   mov dl ,1
   mov eax,pExClass
   call Exception .Create
   call System.@RaiseExcept
   {$Else}
   push rbp
   sub rsp,$20
   mov rbp,rsp
   mov r8,sErr
   mov dl ,$01
   mov rcx,pExClass
   call Exception .Create // Seems to work
   mov rcx,rax
   call System.@RaiseExcept // Here it crashes. " ACCESS VIOLATION"
   lea rsp,[rbp+$20]
   pop rbp
   pop rbp
   {$EndIf}
end ;
BUT will fail on many other places and different situations or for just little more complex assembly above that system.@RaiseExcept !!!

The reasoning : i put a frame stack for it and so it worked, now if your assembly is using variables or utilizing the stack or even the compiler went ahead and added a frame on its own to build right assembly, we will end up with double frame and whole different mess and that solution become broken again and you should remove that stack i introduced, this is one hand on other hand this can't be guaranteed to be working on future compiler, tested it on XE8 and your test procedure and it is raising stack overflow on my machine not an access violation!
That why i moved the raising call to its own pascal procedure, this guaranteed to have right frame stack and the compiler will handle it right, as i said that in my humble opinion and according to my experience this is the closest for assembly to raise an exception without braking things every time you modify your assembly and guaranteed to be working on different compilers including future ones, also will not send tools like EurekaLog after witch hunting!

I hope that is clear.

ps : this was a good question and might help many who interested.
Kas
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
412 Beiträge
 
#7

AW: In Asm-Prozedur eine Exception auslösen

  Alt 5. Nov 2023, 10:32
OK, there much to say about this but really i don't like write essays

You can force the compiler to not introduce a stack frame by using .noframe in the x64 assembly block.

so the code can be like this
Code:
PROCEDURE test;
const
   pExClass : ExceptClass = (Exception);
   sErr : String = ' My message ';
asm
    {$IfDef CPUX86}
   mov ecx,sErr
   mov dl ,1
   mov eax,pExClass
   call Exception .Create
   call System.@RaiseExcept
   {$Else}
   .noframe            //  crucial here
   push rbp
   sub rsp,$20
   mov rbp,rsp
   mov r8,sErr
   mov dl ,$01
   mov rcx,pExClass
   call Exception .Create
   mov rcx,rax
   call System.@RaiseExcept
   lea rsp,[rbp+$20]
   pop rbp
   {$EndIf}
end ;
Yet again, use that on your risk !

Edit: to fix an extra pop useless in that example but will cause problem in real life code.
Kas
  Mit Zitat antworten Zitat
Amateurprofi

Registriert seit: 17. Nov 2005
Ort: Hamburg
1.100 Beiträge
 
Delphi XE2 Professional
 
#8

AW: In Asm-Prozedur eine Exception auslösen

  Alt 6. Nov 2023, 05:45
I am trying here to explain and language barrier is playing huge role.

This will work on x64 for this exact procedure
Code:
PROCEDURE test;
const
   pExClass:ExceptClass=( Exception );
   sErr:String=' My message ';
asm
    {$IfDef CPUX86}
   mov ecx,sErr
   mov dl ,1
   mov eax,pExClass
   call Exception .Create
   call System.@RaiseExcept
   {$Else}
   push rbp
   sub rsp,$20
   mov rbp,rsp
   mov r8,sErr
   mov dl ,$01
   mov rcx,pExClass
   call Exception .Create // Seems to work
   mov rcx,rax
   call System.@RaiseExcept // Here it crashes. " ACCESS VIOLATION"
   lea rsp,[rbp+$20]
   pop rbp
   pop rbp
   {$EndIf}
end ;
BUT will fail on many other places and different situations or for just little more complex assembly above that system.@RaiseExcept !!!

The reasoning : i put a frame stack for it and so it worked, now if your assembly is using variables or utilizing the stack or even the compiler went ahead and added a frame on its own to build right assembly, we will end up with double frame and whole different mess and that solution become broken again and you should remove that stack i introduced, this is one hand on other hand this can't be guaranteed to be working on future compiler, tested it on XE8 and your test procedure and it is raising stack overflow on my machine not an access violation!
That why i moved the raising call to its own pascal procedure, this guaranteed to have right frame stack and the compiler will handle it right, as i said that in my humble opinion and according to my experience this is the closest for assembly to raise an exception without braking things every time you modify your assembly and guaranteed to be working on different compilers including future ones, also will not send tools like EurekaLog after witch hunting!

I hope that is clear.

ps : this was a good question and might help many who interested.
Thank you, Kas Ob.
Funny thing.
Just before entering the DP I looked, how

Delphi-Quellcode:
PROCEDURE TestRaise;
const sErr:String='Meine Meldung';
begin
   raise Exception.Create(sErr);
end;
is translated to asm, found the following

Code:
FS_Main.pas.5041: begin
00000000005D3070 55               push rbp
00000000005D3071 4883EC20         sub rsp,$20
00000000005D3075 488BEC          mov rbp,rsp
FS_Main.pas.5042: raise Exception.Create(sErr);
00000000005D3078 488B0D4901E5FF  mov rcx,[rel $ffe50149]
00000000005D307F B201             mov dl,$01
00000000005D3081 4C8B0500460300   mov r8,[rel $00034600]
00000000005D3088 E81307E6FF      call Exception.Create
00000000005D308D 4889C1           mov rcx,rax
00000000005D3090 E8EB73E3FF      call @RaiseExcept
FS_Main.pas.5043: end;
00000000005D3095 488D6520         lea rsp,[rbp+$20]
00000000005D3099 5D              pop rbp
00000000005D309A C3               ret
copied it into my test-procedure ... and it worked.
Then i wanted to post it and saw that did the same.
Again thank you
Gruß, Klaus
Die Titanic wurde von Profis gebaut,
die Arche Noah von einem Amateur.
... Und dieser Beitrag vom Amateurprofi....
  Mit Zitat antworten Zitat
Amateurprofi

Registriert seit: 17. Nov 2005
Ort: Hamburg
1.100 Beiträge
 
Delphi XE2 Professional
 
#9

AW: In Asm-Prozedur eine Exception auslösen

  Alt 5. Nov 2023, 07:52
@Paule32.jk
Zitat:
ich weiß gerade nicht, was IHR so an der Fragestellung versteht, was ich nicht verstehe...
Für mich (und was der Fragestellende gepostet hat, ist, das er mittels Einzeiler eine Exception bzw. eine System-Routine aufrufen will, OHNE viel Delphi zu verwenden.
Wo habe ich denn geschrieben, dass ich "mittels Einzeiler eine Exception bzw. eine System-Routine aufrufen will" ?
Gruß, Klaus
Die Titanic wurde von Profis gebaut,
die Arche Noah von einem Amateur.
... Und dieser Beitrag vom Amateurprofi....
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:21 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz