diff --git a/src/restrictions.erl b/src/restrictions.erl index 5bd5866..9bd8982 100644 --- a/src/restrictions.erl +++ b/src/restrictions.erl @@ -147,7 +147,7 @@ max_args(Args) -> true -> true; false -> - sandbox:restricted() + sandbox:restricted_msg() end. max_heap_size() -> @@ -156,5 +156,5 @@ max_heap_size() -> true -> true; false -> - sandbox:restricted() + sandbox:restricted_msg() end. diff --git a/src/sandbox.erl b/src/sandbox.erl index d0a3f68..2c1e61e 100644 --- a/src/sandbox.erl +++ b/src/sandbox.erl @@ -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_"). @@ -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)))}. @@ -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. @@ -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 ->