diff --git a/maldoca/js/ir/analyses/dataflow_analysis.h b/maldoca/js/ir/analyses/dataflow_analysis.h index b08aa49..5a8576d 100644 --- a/maldoca/js/ir/analyses/dataflow_analysis.h +++ b/maldoca/js/ir/analyses/dataflow_analysis.h @@ -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 output); @@ -1589,6 +1608,8 @@ mlir::LogicalResult JsirDataFlowAnalysis::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); @@ -1604,6 +1625,71 @@ mlir::LogicalResult JsirDataFlowAnalysis::initialize( return mlir::success(); } +template +void JsirDataFlowAnalysis:: + EmplaceFunctionDefinitionCfgEdges(mlir::Operation* op) { + auto function = llvm::dyn_cast(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; + } + + // + // │ + // ├──► 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(params_end) + .AddDependent(getProgramPointBefore(body_entry)); + } +} + template void JsirDataFlowAnalysis::InitializeBlock( mlir::Block* block) {