Skip to content
Open
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
86 changes: 86 additions & 0 deletions maldoca/js/ir/analyses/dataflow_analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,25 @@ class JsirDataFlowAnalysis : public mlir::DataFlowAnalysis,

mlir::LogicalResult initialize(mlir::Operation* op) override;

// Connects a function-like op (function declarations and expressions, arrow
// functions, object and class methods) to its params and body, so that the
// body starts from the state at the point where the function is *defined*.
//
// As a result, variables captured from enclosing scopes (closures) carry
// their state at the definition. For example, in
//
// var a = getImageData();
// el.onclick = function() { leak(a); };
//
// the state of `a` flows into the body, and reaches `leak(a)`.
//
// This is a "state at the definition" approximation:
// - a state assigned to a captured variable *after* the definition does not
// reach the body;
// - a function declaration's definition point is its position in the code
// (not the start of the scope, where JavaScript hoists it).
void EmplaceFunctionDefinitionCfgEdges(mlir::Operation* op);

void VisitOp(mlir::Operation* op, const StateT* input,
JsirStateRef<StateT> output);

Expand Down Expand Up @@ -1589,6 +1608,8 @@ mlir::LogicalResult JsirDataFlowAnalysis<ValueT, StateT, direction>::initialize(
}
}

EmplaceFunctionDefinitionCfgEdges(op);

// Get optional jump targets and label to be used during recursive
// initialization. These variables use RAII.
auto with_jump_targets = WithJumpTargets(maybe_jump_targets);
Expand All @@ -1604,6 +1625,71 @@ mlir::LogicalResult JsirDataFlowAnalysis<ValueT, StateT, direction>::initialize(
return mlir::success();
}

template <typename ValueT, typename StateT, DataflowDirection direction>
void JsirDataFlowAnalysis<ValueT, StateT, direction>::
EmplaceFunctionDefinitionCfgEdges(mlir::Operation* op) {
auto function = llvm::dyn_cast<JsirFunctionOpInterface>(op);
if (function == nullptr) {
return;
}

// A function op that is not in a block (e.g., the root op of the analysis)
// has no program point before it, so there is no state to propagate.
if (op->getBlock() == nullptr) {
return;
}

mlir::Region& params = function.getParams();
mlir::Region& body = function.getBody();
if (body.empty()) {
return;
}

// <state before the function op>
// │
// ├──► params region ──┐
// │ ▼
// └──────────────► body region
//
// The edges into the params region and the body region start from the state
// before the function op, and are owned by the function op, so they are
// visited (and the state is propagated) whenever the function op is visited,
// i.e., whenever the state before the function op changes.
//
// The edge from the params region to the body region (which carries the
// parameter bindings) is owned by the entry block of the body instead, and
// visited whenever the state at the end of the params region changes. If it
// were owned by the function op, then the function op would be visited even
// if it is unreachable, because the params region, as an entry block, is
// always reachable.
//
// There is no edge from the end of the body, since the body is not executed
// at the definition.
MaybeEmplaceCfgEdges({
.from = Before(op),
.to = Before(body),
.owner = op,
});
if (params.empty()) {
return;
}
MaybeEmplaceCfgEdges({
.from = Before(op),
.to = Before(params),
.owner = op,
});
mlir::Block* body_entry = &body.front();
MaybeEmplaceCfgEdges({
.from = After(params),
.to = Before(body),
.owner = body_entry,
});
for (mlir::ProgramPoint* params_end : After(params)) {
GetStateImpl<StateT>(params_end)
.AddDependent(getProgramPointBefore(body_entry));
}
}

template <typename ValueT, typename StateT, DataflowDirection direction>
void JsirDataFlowAnalysis<ValueT, StateT, direction>::InitializeBlock(
mlir::Block* block) {
Expand Down
Loading