Skip to content

Commit

Permalink
Handle stack overflow in Attributes
Browse files Browse the repository at this point in the history
The main purpose of this patch is to fix stack-depth-operation.g.
Rather than handle that specially, move where we handle stack
depth, to consistently handle all problems
  • Loading branch information
ChrisJefferson committed Jan 23, 2019
1 parent e868e29 commit 2d1c2a1
Show file tree
Hide file tree
Showing 12 changed files with 2,004 additions and 18 deletions.
81 changes: 65 additions & 16 deletions src/calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#ifndef GAP_CALLS_H
#define GAP_CALLS_H

#include "funcs.h"
#include "gaputils.h"
#include "objects.h"

Expand Down Expand Up @@ -328,42 +329,66 @@ EXPORT_INLINE int IS_FUNC(Obj obj)
*/
EXPORT_INLINE Obj CALL_0ARGS(Obj f)
{
return HDLR_0ARGS(f)(f);
CheckRecursionBefore();
Obj o = HDLR_0ARGS(f)(f);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_1ARGS(Obj f, Obj a1)
{
return HDLR_1ARGS(f)(f, a1);
CheckRecursionBefore();
Obj o = HDLR_1ARGS(f)(f, a1);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_2ARGS(Obj f, Obj a1, Obj a2)
{
return HDLR_2ARGS(f)(f, a1, a2);
CheckRecursionBefore();
Obj o = HDLR_2ARGS(f)(f, a1, a2);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_3ARGS(Obj f, Obj a1, Obj a2, Obj a3)
{
return HDLR_3ARGS(f)(f, a1, a2, a3);
CheckRecursionBefore();
Obj o = HDLR_3ARGS(f)(f, a1, a2, a3);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_4ARGS(Obj f, Obj a1, Obj a2, Obj a3, Obj a4)
{
return HDLR_4ARGS(f)(f, a1, a2, a3, a4);
CheckRecursionBefore();
Obj o = HDLR_4ARGS(f)(f, a1, a2, a3, a4);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_5ARGS(Obj f, Obj a1, Obj a2, Obj a3, Obj a4, Obj a5)
{
return HDLR_5ARGS(f)(f, a1, a2, a3, a4, a5);
CheckRecursionBefore();
Obj o = HDLR_5ARGS(f)(f, a1, a2, a3, a4, a5);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_6ARGS(Obj f, Obj a1, Obj a2, Obj a3, Obj a4, Obj a5, Obj a6)
{
return HDLR_6ARGS(f)(f, a1, a2, a3, a4, a5, a6);
CheckRecursionBefore();
Obj o = HDLR_6ARGS(f)(f, a1, a2, a3, a4, a5, a6);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_XARGS(Obj f, Obj as)
{
return HDLR_XARGS(f)(f, as);
CheckRecursionBefore();
Obj o = HDLR_XARGS(f)(f, as);
DecRecursionDepth();
return o;
}


Expand All @@ -384,42 +409,66 @@ EXPORT_INLINE Obj CALL_XARGS(Obj f, Obj as)
*/
EXPORT_INLINE Obj CALL_0ARGS_PROF(Obj f)
{
return HDLR_0ARGS(PROF_FUNC(f))(f);
CheckRecursionBefore();
Obj o = HDLR_0ARGS(PROF_FUNC(f))(f);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_1ARGS_PROF(Obj f, Obj a1)
{
return HDLR_1ARGS(PROF_FUNC(f))(f, a1);
CheckRecursionBefore();
Obj o = HDLR_1ARGS(PROF_FUNC(f))(f, a1);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_2ARGS_PROF(Obj f, Obj a1, Obj a2)
{
return HDLR_2ARGS(PROF_FUNC(f))(f, a1, a2);
CheckRecursionBefore();
Obj o = HDLR_2ARGS(PROF_FUNC(f))(f, a1, a2);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_3ARGS_PROF(Obj f, Obj a1, Obj a2, Obj a3)
{
return HDLR_3ARGS(PROF_FUNC(f))(f, a1, a2, a3);
CheckRecursionBefore();
Obj o = HDLR_3ARGS(PROF_FUNC(f))(f, a1, a2, a3);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_4ARGS_PROF(Obj f, Obj a1, Obj a2, Obj a3, Obj a4)
{
return HDLR_4ARGS(PROF_FUNC(f))(f, a1, a2, a3, a4);
CheckRecursionBefore();
Obj o = HDLR_4ARGS(PROF_FUNC(f))(f, a1, a2, a3, a4);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_5ARGS_PROF(Obj f, Obj a1, Obj a2, Obj a3, Obj a4, Obj a5)
{
return HDLR_5ARGS(PROF_FUNC(f))(f, a1, a2, a3, a4, a5);
CheckRecursionBefore();
Obj o = HDLR_5ARGS(PROF_FUNC(f))(f, a1, a2, a3, a4, a5);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_6ARGS_PROF(Obj f, Obj a1, Obj a2, Obj a3, Obj a4, Obj a5, Obj a6)
{
return HDLR_6ARGS(PROF_FUNC(f))(f, a1, a2, a3, a4, a5, a6);
CheckRecursionBefore();
Obj o = HDLR_6ARGS(PROF_FUNC(f))(f, a1, a2, a3, a4, a5, a6);
DecRecursionDepth();
return o;
}

EXPORT_INLINE Obj CALL_XARGS_PROF(Obj f, Obj as)
{
return HDLR_XARGS(PROF_FUNC(f))(f, as);
CheckRecursionBefore();
Obj o = HDLR_XARGS(PROF_FUNC(f))(f, as);
DecRecursionDepth();
return o;
}


Expand Down
2 changes: 0 additions & 2 deletions src/funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,8 @@ void RecursionDepthTrap( void )

#define CHECK_RECURSION_BEFORE \
HookedLineIntoFunction(func); \
CheckRecursionBefore();

#define CHECK_RECURSION_AFTER \
DecRecursionDepth(); \
HookedLineOutFunction(func);

#ifdef HPCGAP
Expand Down
2 changes: 2 additions & 0 deletions tst/testspecial/stack-depth-func.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
f := function() f(); end;
f();
13 changes: 13 additions & 0 deletions tst/testspecial/stack-depth-func.g.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
gap> f := function() f(); end;
function( ) ... end
gap> f();
Error, recursion depth trap (5000) in
f( ); at *stdin*:2 called from
f( ); at *stdin*:2 called from
f( ); at *stdin*:2 called from
f( ); at *stdin*:2 called from
f( ); at *stdin*:2 called from
f( ); at *stdin*:2 called from
... at *stdin*:3
you may 'return;'
brk> QUIT;
2 changes: 2 additions & 0 deletions tst/testspecial/stack-depth-func2.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
f := function() local x; x := f(); return x; end;
y := f();
13 changes: 13 additions & 0 deletions tst/testspecial/stack-depth-func2.g.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
gap> f := function() local x; x := f(); return x; end;
function( ) ... end
gap> y := f();
Error, recursion depth trap (5000) in
f( ) at *stdin*:2 called from
f( ) at *stdin*:2 called from
f( ) at *stdin*:2 called from
f( ) at *stdin*:2 called from
f( ) at *stdin*:2 called from
f( ) at *stdin*:2 called from
... at *stdin*:3
you may 'return;'
brk> QUIT;
3 changes: 3 additions & 0 deletions tst/testspecial/stack-depth-list.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
l := []; for i in [1..10000] do l := [l]; od;
Print(l);
String(l);
23 changes: 23 additions & 0 deletions tst/testspecial/stack-depth-list.g.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
gap> l := []; for i in [1..10000] do l := [l]; od;
[ ]
gap> Print(l);
[ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [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] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ]\
] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ]
gap> String(l);
Error, recursion depth trap (5000) in
IsString( obj ) at GAPROOT/lib/string.g:292 called from
IsEmptyString( list ) at GAPROOT/lib/list.gi:306 called from
String( list[i] ) at GAPROOT/lib/list.gi:319 called from
String( list[i] ) at GAPROOT/lib/list.gi:319 called from
String( list[i] ) at GAPROOT/lib/list.gi:319 called from
String( list[i] ) at GAPROOT/lib/list.gi:319 called from
... at *stdin*:4
you may 'return;'
brk> QUIT;
3 changes: 3 additions & 0 deletions tst/testspecial/stack-depth-operation.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DeclareAttribute("XXX",IsGroup);
InstallMethod(XXX,[IsGroup],XXX);
XXX(SymmetricGroup(5));
7 changes: 7 additions & 0 deletions tst/testspecial/stack-depth-operation.g.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
gap> DeclareAttribute("XXX",IsGroup);
gap> InstallMethod(XXX,[IsGroup],XXX);
gap> XXX(SymmetricGroup(5));
Error, recursion depth trap (5000)
not in any function at *stdin*:4
you may 'return;'
brk> QUIT;
3 changes: 3 additions & 0 deletions tst/testspecial/stack-depth-rec.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
r := rec(); for i in [1..10000] do r := rec(a := r); od;
Print(r);
String(r);
Loading

0 comments on commit 2d1c2a1

Please sign in to comment.