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

Subexprs #4

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/restrictions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ max_args(Args) ->
true ->
true;
false ->
sandbox:restricted()
sandbox:restricted_msg()
end.

max_heap_size() ->
Expand All @@ -156,5 +156,5 @@ max_heap_size() ->
true ->
true;
false ->
sandbox:restricted()
sandbox:restricted_msg()
end.
56 changes: 51 additions & 5 deletions src/sandbox.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

-define(MAX_HEAP_SIZE, 10000).
-define(MAX_ARGS_SIZE, 200).
-define(MAX_SIZE_QUALIFIER_DIMENSION, 500).
-define(MAX_SPACE_DIMENSION,1000).

-define(ATOM_PREFIX, "axwlefhubay_").

Expand All @@ -15,8 +17,9 @@ eval(E) ->
eval(E, Bs) ->
{ok, Tokens, _} = erl_scan:string(E),
{ok, Exprs} = erl_parse:parse_exprs(Tokens),
put(bindings,Bs),
SafeExprs = safe_exprs(Exprs),
{value, Value, NBs} = erl_eval:exprs(SafeExprs, Bs, {eval, fun lh/3}, {value, fun nlh/2}),
{value, Value, NBs} = erl_eval:exprs(SafeExprs, get(bindings), {eval, fun lh/3}, {value, fun nlh/2}),
{erl_syntax:concrete(restore_exprs(erl_syntax:abstract(Value))),
erl_syntax:concrete(restore_exprs(erl_syntax:abstract(NBs)))}.

Expand All @@ -27,9 +30,9 @@ lh(f, [{var,_,Name}], Bs) ->
lh(F, Args, Bs) ->
Arity = length(Args),
case erlang:function_exported(user_default, F, Arity) of
true ->
true ->
{eval, erlang:make_fun(user_default, F, Arity), Args, Bs};
false ->
false ->
{value, sandbox:restricted_msg(), Bs}
end.

Expand Down Expand Up @@ -85,11 +88,54 @@ safe_application(Node) ->
fun_expr ->
sandbox:restricted_msg();
size_qualifier ->
sandbox:restricted_msg();
_ ->
SubTree = erl_syntax:size_qualifier_argument(Node),
{value, Value, NBs} = erl_eval:exprs([revert(SubTree)], get(bindings), {eval, fun lh/3}, {value, fun nlh/2}),
if
Value < ?MAX_SIZE_QUALIFIER_DIMENSION, is_integer(Value) ->
[First, _] = erl_syntax:subtrees(Node),
put(bindings,NBs),
erl_syntax:update_tree(Node,[First,[{integer,1,Value}]]);
true ->
sandbox:restricted_msg()
end;
list_comp ->
ListCompBody = erl_syntax:list_comp_body(Node),
SpaceDimension = calculateSpaceDimension(ListCompBody),
case SpaceDimension =< ?MAX_SPACE_DIMENSION of
true ->
Node;
false ->
sandbox:restricted_msg()
end;
binary_comp ->
BinaryCompBody = erl_syntax:binary_comp_body(Node),
SpaceDimension = calculateSpaceDimension(BinaryCompBody),
case SpaceDimension =< ?MAX_SPACE_DIMENSION of
true ->
Node;
false ->
sandbox:restricted_msg()
end;
_Else ->
Node
end.

calculateSpaceDimension(ListCompBody)->
calculateSpaceDimension(ListCompBody,1).

calculateSpaceDimension([],Acc)->
Acc;
calculateSpaceDimension([H|T],Acc)->
H1 = restore_expr(H),
case erl_syntax:type(H1) of
generator ->
[_Var,List] = erl_syntax:subtrees(H1),
{value, Value, _NBs} = erl_eval:exprs(revert(List), get(bindings), {eval, fun lh/3}, {value, fun nlh/2}),
calculateSpaceDimension(T,Acc*length(Value));
_ ->
calculateSpaceDimension(T,Acc)
end.

replace_atoms(Node) ->
case erl_syntax:type(Node) of
atom ->
Expand Down