Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle infinite recursion in attribute methods #3221

Merged
merged 1 commit into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 6 additions & 14 deletions src/funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,6 @@ void RecursionDepthTrap( void )
}
}

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

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

#ifdef HPCGAP

#define REMEMBER_LOCKSTACK() \
Expand Down Expand Up @@ -465,7 +457,7 @@ static ALWAYS_INLINE Obj DoExecFunc(Obj func, Int narg, const Obj *arg)
{
Bag oldLvars; /* old values bag */
Obj result;
CHECK_RECURSION_BEFORE
HookedLineIntoFunction(func);

#ifdef HPCGAP
REMEMBER_LOCKSTACK();
Expand All @@ -489,7 +481,7 @@ static ALWAYS_INLINE Obj DoExecFunc(Obj func, Int narg, const Obj *arg)
/* switch back to the old values bag */
SWITCH_TO_OLD_LVARS_AND_FREE( oldLvars );

CHECK_RECURSION_AFTER
HookedLineOutFunction(func);

/* return the result */
return result;
Expand Down Expand Up @@ -543,7 +535,7 @@ static Obj DoExecFuncXargs(Obj func, Obj args)
UInt i; /* loop variable */
Obj result;

CHECK_RECURSION_BEFORE
HookedLineIntoFunction(func);

/* check the number of arguments */
len = NARG_FUNC( func );
Expand Down Expand Up @@ -574,7 +566,7 @@ static Obj DoExecFuncXargs(Obj func, Obj args)
/* switch back to the old values bag */
SWITCH_TO_OLD_LVARS_AND_FREE( oldLvars );

CHECK_RECURSION_AFTER
HookedLineOutFunction(func);

/* return the result */
return result;
Expand All @@ -589,7 +581,7 @@ static Obj DoPartialUnWrapFunc(Obj func, Obj args)
UInt len;
Obj argx, result;

CHECK_RECURSION_BEFORE
HookedLineIntoFunction(func);

named = ((UInt)-NARG_FUNC(func))-1;
len = LEN_PLIST(args);
Expand Down Expand Up @@ -627,7 +619,7 @@ static Obj DoPartialUnWrapFunc(Obj func, Obj args)
/* switch back to the old values bag */
SWITCH_TO_OLD_LVARS_AND_FREE( oldLvars );

CHECK_RECURSION_AFTER
HookedLineOutFunction(func);

/* return the result */
return result;
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;
Loading