فهرست منبع

* Fixed checks for exceeding limit of locals space. Prevent range and overflow errors during the checks. Introduced the MaxLocalsSize constant which provides the maximum possible size of locals space (stack frame) depending of bitness of a cpu.

git-svn-id: trunk@39916 -
yury 6 سال پیش
والد
کامیت
d0b6f427d3
2فایلهای تغییر یافته به همراه23 افزوده شده و 9 حذف شده
  1. 8 0
      compiler/globtype.pas
  2. 15 9
      compiler/tgobj.pas

+ 8 - 0
compiler/globtype.pas

@@ -87,6 +87,14 @@ interface
        AIntBits = 8;
        AIntBits = 8;
 {$endif cpu8bitalu}
 {$endif cpu8bitalu}
 
 
+     { Maximum possible size of locals space (stack frame) }
+     Const
+{$if defined(cpu8bitalu) or defined(cpu16bitalu)}
+       MaxLocalsSize = High(AWord);
+{$else}
+       MaxLocalsSize = High(longint) - 15;
+{$endif}
+
      Type
      Type
        PAWord = ^AWord;
        PAWord = ^AWord;
        PAInt = ^AInt;
        PAInt = ^AInt;

+ 15 - 9
compiler/tgobj.pas

@@ -274,6 +274,11 @@ implementation
          freetype:=Used2Free[temptype];
          freetype:=Used2Free[temptype];
          if freetype=tt_none then
          if freetype=tt_none then
            internalerror(200208201);
            internalerror(200208201);
+         if size>MaxLocalsSize then
+            begin
+              CGMessage(cg_e_localsize_too_big);
+              size:=0;  // Prevent further range check errors
+            end;
          size:=align(size,alignment);
          size:=align(size,alignment);
          { First check the tmpfreelist, but not when
          { First check the tmpfreelist, but not when
            we don't want to reuse an already allocated block }
            we don't want to reuse an already allocated block }
@@ -417,29 +422,30 @@ implementation
             tl^.temptype:=temptype;
             tl^.temptype:=temptype;
             tl^.def:=def;
             tl^.def:=def;
 
 
-{$push}
-{$r-}
-{$warn 6018 off}
-{$warn 4044 off}
             { Extend the temp }
             { Extend the temp }
             if direction=-1 then
             if direction=-1 then
               begin
               begin
-                if qword(align(-lasttemp-alignmismatch,alignment))+size+alignmismatch>high(tl^.pos) then
-                  CGMessage(cg_e_localsize_too_big);
+                if qword(align(-lasttemp-alignmismatch,alignment))+size+alignmismatch>MaxLocalsSize then
+                  begin
+                    CGMessage(cg_e_localsize_too_big);
+                    size:=0;  // Prevent further range check errors
+                  end;
                 lasttemp:=(-align(-lasttemp-alignmismatch,alignment))-size-alignmismatch;
                 lasttemp:=(-align(-lasttemp-alignmismatch,alignment))-size-alignmismatch;
                 tl^.pos:=lasttemp;
                 tl^.pos:=lasttemp;
               end
               end
             else
             else
               begin
               begin
                 tl^.pos:=align(lasttemp+alignmismatch,alignment)-alignmismatch;
                 tl^.pos:=align(lasttemp+alignmismatch,alignment)-alignmismatch;
-                if qword(tl^.pos)+size>high(tl^.pos) then
-                  CGMessage(cg_e_localsize_too_big);
+                if qword(tl^.pos)+size>MaxLocalsSize then
+                  begin
+                    CGMessage(cg_e_localsize_too_big);
+                    size:=0;  // Prevent further range check errors
+                  end;
                 lasttemp:=tl^.pos+size;
                 lasttemp:=tl^.pos+size;
               end;
               end;
 {$ifdef EXTDEBUG}
 {$ifdef EXTDEBUG}
          Comment(V_Note,'tgobj: (AllocTemp) lasttemp set to '+tostr(lasttemp));
          Comment(V_Note,'tgobj: (AllocTemp) lasttemp set to '+tostr(lasttemp));
 {$endif}
 {$endif}
-{$pop}
             tl^.fini:=fini;
             tl^.fini:=fini;
             tl^.alignment:=alignment;
             tl^.alignment:=alignment;
             tl^.size:=size;
             tl^.size:=size;