Skip to content

Commit

Permalink
Added support for custom max source size.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustaf Sjöberg committed Aug 24, 2015
1 parent 2b8fc12 commit 1c69055
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
28 changes: 20 additions & 8 deletions src/erlang_v8_vm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
-define(EXECUTABLE, "erlang_v8").
-define(SPAWN_OPTS, [{packet, 4}, binary]).
-define(DEFAULT_TIMEOUT, 5000).
-define(MAX_SOURCE_SIZE, 5 * 1024 * 1024).
-define(MAX_SOURCE_SIZE, 16#FFFFFFFF).

-define(OP_EVAL, 1).
-define(OP_CALL, 2).
Expand All @@ -33,6 +33,7 @@

-record(state, {
initial_source = [],
max_source_size = 5 * 1024 * 1024,
port,
monitor_pid
}).
Expand Down Expand Up @@ -77,12 +78,15 @@ init([Opts]) ->
{ok, State}.

handle_call({call, FunctionName, Args, Timeout}, _From,
#state{port = Port} = State) ->
#state{port = Port, max_source_size = MaxSourceSize} = State) ->
Source = jsx:encode([{function, FunctionName}, {args, Args}]),
handle_response(send_to_port(Port, ?OP_CALL, Source, Timeout), State);
handle_response(send_to_port(Port, ?OP_CALL, Source, Timeout,
MaxSourceSize), State);

handle_call({eval, Source, Timeout}, _From, #state{port = Port} = State) ->
handle_response(send_to_port(Port, ?OP_EVAL, Source, Timeout), State);
handle_call({eval, Source, Timeout}, _From,
#state{port = Port, max_source_size = MaxSourceSize} = State) ->
handle_response(send_to_port(Port, ?OP_EVAL, Source, Timeout,
MaxSourceSize), State);

handle_call(set, _From, #state{port = Port} = State) ->
Port ! {self(), {command, <<?OP_SET:8>>}},
Expand Down Expand Up @@ -170,10 +174,10 @@ os_kill(OSPid) ->
os:cmd(io_lib:format("kill -9 ~p", [OSPid])).

%% @doc Send source to port and wait for response
send_to_port(_Port, _Op, Source, _Timeout)
when size(Source) > ?MAX_SOURCE_SIZE ->
send_to_port(_Port, _Op, Source, _Timeout, MaxSourceSize)
when size(Source) > MaxSourceSize ->
{error, invalid_source_size};
send_to_port(Port, Op, Source, Timeout) ->
send_to_port(Port, Op, Source, Timeout, _MaxSourceSize) ->
Port ! {self(), {command, <<Op:8, Source/binary>>}},
receive
{Port, {data, <<_:8, "undefined">>}} ->
Expand Down Expand Up @@ -211,5 +215,13 @@ parse_opt({file, F}, #state{initial_source = InitialSource} = State) ->
{ok, S} = file:read_file(F),
State#state{initial_source = [S|InitialSource]};

%% @doc Invalid max source size
parse_opt({max_source_size, N}, _State) when N > ?MAX_SOURCE_SIZE ->
error(invalid_max_source_size_value);

%% @doc Set max source size for this vm
parse_opt({max_source_size, N}, State) ->
State#state{max_source_size = N};

%% @doc Ignore unknown options.
parse_opt(_, State) -> State.
6 changes: 3 additions & 3 deletions test/port_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ multiple_vms(_Config) ->
ok.

big_input(_Config) ->
{ok, VM} = erlang_v8:start_vm(),
{ok, VM} = erlang_v8:start_vm([{max_source_size, 1000}]),
{ok, undefined} = erlang_v8:eval(VM, <<"function call(arg) {
return arg;
}">>),

{error, invalid_source_size} = erlang_v8:call(VM, <<"call">>,
[random_bytes(5 * 1024 * 1024)]),
[random_bytes(1000)]),

{ok, _} = erlang_v8:call(VM, <<"call">>, [random_bytes(30)]),
{ok, _} = erlang_v8:call(VM, <<"call">>, [random_bytes(500)]),

ok = erlang_v8:stop_vm(VM),
ok.
Expand Down

0 comments on commit 1c69055

Please sign in to comment.