Skip to content

Commit

Permalink
Only enable JS runtime limits during execution
Browse files Browse the repository at this point in the history
  • Loading branch information
achamayou committed Oct 13, 2023
1 parent f012149 commit da384f0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/apps/js_generic/js_generic_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ namespace ccfapp
// Update the top of the stack for the current thread, used by the stack
// guard Note this is only active outside SGX
JS_UpdateStackTop(ctx.runtime());
// Make the heap and stack limits safe while we init the runtime
ctx.runtime().reset_runtime_options();

ctx.runtime().set_runtime_options(&endpoint_ctx.tx);
JS_SetModuleLoaderFunc(
ctx.runtime(), nullptr, js::js_app_module_loader, &endpoint_ctx.tx);

Expand Down Expand Up @@ -353,7 +354,11 @@ namespace ccfapp

// Call exported function
auto request = create_request_obj(endpoint, endpoint_ctx, ctx);

// Enable runtime limits for the duration of the call
ctx.runtime().set_runtime_options(&endpoint_ctx.tx);
auto val = ctx.call(export_func, {request});
ctx.runtime().reset_runtime_options();

if (JS_IsException(val))
{
Expand Down
7 changes: 7 additions & 0 deletions src/js/wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,13 @@ namespace ccf::js
}
}

void Runtime::reset_runtime_options()
{
JS_SetMaxStackSize(rt, 0);
JS_SetMemoryLimit(rt, -1);
JS_SetInterruptHandler(rt, NULL, NULL);
}

void Runtime::set_runtime_options(kv::Tx* tx)
{
size_t stack_size = default_stack_size;
Expand Down
1 change: 1 addition & 0 deletions src/js/wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ namespace ccf::js
return rt;
}

void reset_runtime_options();
void set_runtime_options(kv::Tx* tx);

std::chrono::milliseconds get_max_exec_time() const
Expand Down
13 changes: 9 additions & 4 deletions src/node/gov/handlers/proposals.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ namespace ccf::gov::endpoints
for (const auto& [mid, mb] : proposal_info.ballots)
{
js::Context js_context(js::TxAccess::GOV_RO);
js_context.runtime().set_runtime_options(&tx);
js::TxContext txctx{&tx};
js::populate_global_ccf_kv(&txctx, js_context);
auto ballot_func = js_context.function(
Expand All @@ -156,7 +155,10 @@ namespace ccf::gov::endpoints
proposal_info.proposer_id.data(),
proposal_info.proposer_id.size())};

js_context.runtime().set_runtime_options(&tx);
auto val = js_context.call(ballot_func, argv);
js_context.runtime().reset_runtime_options();

if (!JS_IsException(val))
{
votes.emplace_back(mid, JS_ToBool(js_context, val));
Expand All @@ -182,7 +184,6 @@ namespace ccf::gov::endpoints
{
{
js::Context js_context(js::TxAccess::GOV_RO);
js_context.runtime().set_runtime_options(&tx);
js::TxContext txctx{&tx};
js::populate_global_ccf_kv(&txctx, js_context);
auto resolve_func = js_context.function(
Expand Down Expand Up @@ -213,7 +214,9 @@ namespace ccf::gov::endpoints
}
argv.push_back(vs);

js_context.runtime().set_runtime_options(&tx);
auto val = js_context.call(resolve_func, argv);
js_context.runtime().reset_runtime_options();

if (JS_IsException(val))
{
Expand Down Expand Up @@ -267,7 +270,6 @@ namespace ccf::gov::endpoints
{
// Evaluate apply function
js::Context js_context(js::TxAccess::GOV_RW);
js_context.runtime().set_runtime_options(&tx);
js::TxContext txctx{&tx};

auto gov_effects =
Expand All @@ -292,7 +294,9 @@ namespace ccf::gov::endpoints
js_context.new_string_len(
proposal_id.c_str(), proposal_id.size())};

js_context.runtime().set_runtime_options(&tx);
auto val = js_context.call(apply_func, argv);
js_context.runtime().reset_runtime_options();

if (JS_IsException(val))
{
Expand Down Expand Up @@ -438,7 +442,6 @@ namespace ccf::gov::endpoints
}

js::Context context(js::TxAccess::GOV_RO);
context.runtime().set_runtime_options(&ctx.tx);
js::TxContext txctx{&ctx.tx};
js::populate_global_ccf_kv(&txctx, context);

Expand All @@ -450,7 +453,9 @@ namespace ccf::gov::endpoints
proposal_body = cose_ident.content;
auto proposal_arg = context.new_string_len(
(const char*)proposal_body.data(), proposal_body.size());
context.runtime().set_runtime_options(&ctx.tx);
auto validate_result = context.call(validate_func, {proposal_arg});
context.runtime().reset_runtime_options();

// Handle error cases of validation
{
Expand Down
13 changes: 9 additions & 4 deletions src/node/rpc/member_frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ namespace ccf
for (const auto& [mid, mb] : pi_->ballots)
{
js::Context context(js::TxAccess::GOV_RO);
context.runtime().set_runtime_options(&tx);
js::TxContext txctx{&tx};
js::populate_global_ccf_kv(&txctx, context);
auto ballot_func = context.function(
Expand All @@ -163,7 +162,10 @@ namespace ccf
context.new_string_len(
pi_->proposer_id.data(), pi_->proposer_id.size())};

context.runtime().set_runtime_options(&tx);
auto val = context.call(ballot_func, argv);
context.runtime().reset_runtime_options();

if (!JS_IsException(val))
{
votes.emplace_back(mid, JS_ToBool(context, val));
Expand All @@ -187,7 +189,6 @@ namespace ccf

{
js::Context js_context(js::TxAccess::GOV_RO);
js_context.runtime().set_runtime_options(&tx);
js::TxContext txctx{&tx};
js::populate_global_ccf_kv(&txctx, js_context);
auto resolve_func = js_context.function(
Expand Down Expand Up @@ -216,7 +217,9 @@ namespace ccf
}
argv.push_back(vs);

js_context.runtime().set_runtime_options(&tx);
auto val = js_context.call(resolve_func, argv);
js_context.runtime().reset_runtime_options();

std::optional<jsgov::Failure> failure = std::nullopt;
if (JS_IsException(val))
Expand Down Expand Up @@ -284,7 +287,6 @@ namespace ccf
if (pi_.value().state == ProposalState::ACCEPTED)
{
js::Context apply_js_context(js::TxAccess::GOV_RW);
apply_js_context.runtime().set_runtime_options(&tx);

js::TxContext apply_txctx{&tx};

Expand All @@ -310,7 +312,9 @@ namespace ccf
apply_js_context.new_string_len(
proposal_id.c_str(), proposal_id.size())};

apply_js_context.runtime().set_runtime_options(&tx);
auto apply_val = apply_js_context.call(apply_func, apply_argv);
apply_js_context.runtime().reset_runtime_options();

if (JS_IsException(apply_val))
{
Expand Down Expand Up @@ -1151,7 +1155,6 @@ namespace ccf
auto validate_script = constitution.value();

js::Context context(js::TxAccess::GOV_RO);
context.runtime().set_runtime_options(&ctx.tx);
js::TxContext txctx{&ctx.tx};
js::populate_global_ccf_kv(&txctx, context);

Expand All @@ -1166,7 +1169,9 @@ namespace ccf
auto body_len = proposal_body.size();

auto proposal = context.new_string_len(body, body_len);
context.runtime().set_runtime_options(&ctx.tx);
auto val = context.call(validate_func, {proposal});
context.runtime().reset_runtime_options();

if (JS_IsException(val))
{
Expand Down

0 comments on commit da384f0

Please sign in to comment.