Stack alignment #58

Open
opened 2020-08-02 16:06:41 +00:00 by benibela · 3 comments
benibela commented 2020-08-02 16:06:41 +00:00 (Migrated from github.com)

FPC 3.3.1 (perhaps 3.2 as well) assumes the stack is 16-byte aligned

Without the alignment, it throws an runtime error "Stack overflow" when compiled with stack checking -Ct

For example:


var f: TFLRE;
  mc: TFLREMultiCaptures;
begin
  mc := nil;
  f := TFLRE.Create('.g.', []);
  f.UTF8MatchAll('regex', mc);
  writeln(mc[0][0].Length);

$ /tmp/ftest
An unhandled exception occurred at $0806204E:
EStackOverflow: Stack overflow
  $0806204E
  $080972FC  SEARCHMATCHFAST
  $080C1437  SEARCHMATCH,  line 19860 of ../home/benito/components/pascal/import/flre/src/FLRE.pas
  $080C1D3E  PTRMATCHALL,  line 20036 of ../home/benito/components/pascal/import/flre/src/FLRE.pas
  $080C44A9  UTF8MATCHALL,  line 20752 of ../home/benito/components/pascal/import/flre/src/FLRE.pas
  $0804920A  main,  line 17 of ftest.pas


This would fix that:

diff --git a/src/FLRE.pas b/src/FLRE.pas
index 326372a..591cecc 100644
--- a/src/FLRE.pas
+++ b/src/FLRE.pas
@@ -12290,12 +12290,18 @@ asm
 
    @HaveNoNextState:
     push ecx
+    push edx
+    push edx
+    push edx
     push edx
      mov ecx,eax // Char
      mov eax,self
      mov edx,ebx // State
      call FastProcessNextState
-    pop edx
+     pop edx
+     pop edx
+     pop edx
+     pop edx
     pop ecx
     mov edi,eax
     test edi,edi
@@ -13325,6 +13331,7 @@ begin
 
    @HaveNoNextState:
     push ecx
+    push edx
     push edx
      push eax // Char
      lea ecx,[esi+1]
@@ -13332,7 +13339,8 @@ begin
      mov eax,self
      mov edx,ebx // State
      call RunStateOnByte
-    pop edx
+     pop edx
+     pop edx
     pop ecx
     mov edi,eax
     test edi,edi

but it is probably too slow, and only changes two of the calls

FPC 3.3.1 (perhaps 3.2 as well) assumes the stack is 16-byte aligned Without the alignment, it throws an runtime error "Stack overflow" when compiled with stack checking `-Ct` For example: ``` var f: TFLRE; mc: TFLREMultiCaptures; begin mc := nil; f := TFLRE.Create('.g.', []); f.UTF8MatchAll('regex', mc); writeln(mc[0][0].Length); $ /tmp/ftest An unhandled exception occurred at $0806204E: EStackOverflow: Stack overflow $0806204E $080972FC SEARCHMATCHFAST $080C1437 SEARCHMATCH, line 19860 of ../home/benito/components/pascal/import/flre/src/FLRE.pas $080C1D3E PTRMATCHALL, line 20036 of ../home/benito/components/pascal/import/flre/src/FLRE.pas $080C44A9 UTF8MATCHALL, line 20752 of ../home/benito/components/pascal/import/flre/src/FLRE.pas $0804920A main, line 17 of ftest.pas ``` This would fix that: ``` diff --git a/src/FLRE.pas b/src/FLRE.pas index 326372a..591cecc 100644 --- a/src/FLRE.pas +++ b/src/FLRE.pas @@ -12290,12 +12290,18 @@ asm @HaveNoNextState: push ecx + push edx + push edx + push edx push edx mov ecx,eax // Char mov eax,self mov edx,ebx // State call FastProcessNextState - pop edx + pop edx + pop edx + pop edx + pop edx pop ecx mov edi,eax test edi,edi @@ -13325,6 +13331,7 @@ begin @HaveNoNextState: push ecx + push edx push edx push eax // Char lea ecx,[esi+1] @@ -13332,7 +13339,8 @@ begin mov eax,self mov edx,ebx // State call RunStateOnByte - pop edx + pop edx + pop edx pop ecx mov edi,eax test edi,edi ``` but it is probably too slow, and only changes two of the calls
benibela commented 2020-08-02 16:28:03 +00:00 (Migrated from github.com)

Perhaps lea esp, [esp - 12] is better? github.com/benibela/flre@fb1038d2a8

Perhaps `lea esp, [esp - 12] ` is better? https://github.com/benibela/flre/commit/fb1038d2a808cb93c494ccaa82a8e444938ddbde
BeRo1985 commented 2020-08-02 19:26:00 +00:00 (Migrated from github.com)

or for better compatibly to older compilers:

push ebp
mov ebp,esp
and esp,$fffffff0  // Align stack
...

sub esp,12
...
add esp,12

...
mov esp,ebp
pop ebp

or somewhat in this direction.

or for better compatibly to older compilers: ``` push ebp mov ebp,esp and esp,$fffffff0 // Align stack ... sub esp,12 ... add esp,12 ... mov esp,ebp pop ebp ``` or somewhat in this direction.
benibela commented 2020-08-12 21:20:26 +00:00 (Migrated from github.com)

Which older compilers?

Compilers that do not know lea? I just tried it in Delphi 4 and it knows it

Or compilers that some other kind of alignment?

Which older compilers? Compilers that do not know `lea`? I just tried it in Delphi 4 and it knows it Or compilers that some other kind of alignment?
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
BeRo1985/flre#58
No description provided.