BeRo1985 / flre

FLRE - Fast Light Regular Expressions - A fast light regular expression library
GNU Lesser General Public License v2.1
94 stars 23 forks source link

Stack alignment #58

Open benibela opened 4 years ago

benibela commented 4 years ago

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 4 years ago

Perhaps lea esp, [esp - 12] is better? https://github.com/benibela/flre/commit/fb1038d2a808cb93c494ccaa82a8e444938ddbde

BeRo1985 commented 4 years ago

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 4 years ago

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?