| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from mypyc.analysis.dataflow import ( |
| 4 |
CFG, |
| 5 |
MAYBE_ANALYSIS, |
| 6 |
AnalysisResult, |
| 7 |
GenAndKill as _DataflowGenAndKill, |
| 8 |
run_analysis, |
| 9 |
) |
| 10 |
from mypyc.ir.ops import ( |
| 11 |
Assign, |
| 12 |
AssignMulti, |
| 13 |
BasicBlock, |
| 14 |
Box, |
| 15 |
Branch, |
| 16 |
Call, |
| 17 |
CallC, |
| 18 |
Cast, |
| 19 |
ComparisonOp, |
| 20 |
DecRef, |
| 21 |
Extend, |
| 22 |
FloatComparisonOp, |
| 23 |
FloatNeg, |
| 24 |
FloatOp, |
| 25 |
GetAttr, |
| 26 |
GetElement, |
| 27 |
GetElementPtr, |
| 28 |
Goto, |
| 29 |
IncRef, |
| 30 |
InitStatic, |
| 31 |
IntOp, |
| 32 |
KeepAlive, |
| 33 |
LoadAddress, |
| 34 |
LoadErrorValue, |
| 35 |
LoadGlobal, |
| 36 |
LoadLiteral, |
| 37 |
LoadMem, |
| 38 |
LoadStatic, |
| 39 |
MethodCall, |
| 40 |
OpVisitor, |
| 41 |
PrimitiveOp, |
| 42 |
RaiseStandardError, |
| 43 |
Register, |
| 44 |
RegisterOp, |
| 45 |
Return, |
| 46 |
SetAttr, |
| 47 |
SetElement, |
| 48 |
SetMem, |
| 49 |
Truncate, |
| 50 |
TupleGet, |
| 51 |
TupleSet, |
| 52 |
Unborrow, |
| 53 |
Unbox, |
| 54 |
Unreachable, |
| 55 |
) |
| 56 |
from mypyc.ir.rtypes import RInstance |
| 57 |
|
| 58 |
GenAndKill = _DataflowGenAndKill[None] |
| 59 |
|
| 60 |
CLEAN: GenAndKill = (set(), set()) |
| 61 |
DIRTY: GenAndKill = ({None}, {None}) |
| 62 |
|
| 63 |
|
| 64 |
class SelfLeakedVisitor(OpVisitor[GenAndKill]): |
| 65 |
"""Analyze whether 'self' may be seen by arbitrary code in '__init__'. |
| 66 |
|
| 67 |
More formally, the set is not empty if along some path from IR entry point |
| 68 |
arbitrary code could have been executed that has access to 'self'. |
| 69 |
|
| 70 |
(We don't consider access via 'gc.get_objects()'.) |
| 71 |
""" |
| 72 |
|
| 73 |
def __init__(self, self_reg: Register) -> None: |
| 74 |
self.self_reg = self_reg |
| 75 |
|
| 76 |
def visit_goto(self, op: Goto) -> GenAndKill: |
| 77 |
return CLEAN |
| 78 |
|
| 79 |
def visit_branch(self, op: Branch) -> GenAndKill: |
| 80 |
return CLEAN |
| 81 |
|
| 82 |
def visit_return(self, op: Return) -> GenAndKill: |
| 83 |
# Consider all exits from the function 'dirty' since they implicitly |
| 84 |
# cause 'self' to be returned. |
| 85 |
return DIRTY |
| 86 |
|
| 87 |
def visit_unreachable(self, op: Unreachable) -> GenAndKill: |
| 88 |
return CLEAN |
| 89 |
|
| 90 |
def visit_assign(self, op: Assign) -> GenAndKill: |
| 91 |
if op.src is self.self_reg or op.dest is self.self_reg: |
| 92 |
return DIRTY |
| 93 |
return CLEAN |
| 94 |
|
| 95 |
def visit_assign_multi(self, op: AssignMulti) -> GenAndKill: |
| 96 |
return CLEAN |
| 97 |
|
| 98 |
def visit_set_mem(self, op: SetMem) -> GenAndKill: |
| 99 |
return CLEAN |
| 100 |
|
| 101 |
def visit_inc_ref(self, op: IncRef) -> GenAndKill: |
| 102 |
return CLEAN |
| 103 |
|
| 104 |
def visit_dec_ref(self, op: DecRef) -> GenAndKill: |
| 105 |
return CLEAN |
| 106 |
|
| 107 |
def visit_call(self, op: Call) -> GenAndKill: |
| 108 |
fn = op.fn |
| 109 |
if fn.class_name and fn.name == "__init__": |
| 110 |
self_type = op.fn.sig.args[0].type |
| 111 |
assert isinstance(self_type, RInstance), self_type |
| 112 |
cl = self_type.class_ir |
| 113 |
if not cl.init_self_leak: |
| 114 |
return CLEAN |
| 115 |
return self.check_register_op(op) |
| 116 |
|
| 117 |
def visit_method_call(self, op: MethodCall) -> GenAndKill: |
| 118 |
return self.check_register_op(op) |
| 119 |
|
| 120 |
def visit_load_error_value(self, op: LoadErrorValue) -> GenAndKill: |
| 121 |
return CLEAN |
| 122 |
|
| 123 |
def visit_load_literal(self, op: LoadLiteral) -> GenAndKill: |
| 124 |
return CLEAN |
| 125 |
|
| 126 |
def visit_get_attr(self, op: GetAttr) -> GenAndKill: |
| 127 |
cl = op.class_type.class_ir |
| 128 |
if cl.get_method(op.attr): |
| 129 |
# Property -- calls a function |
| 130 |
return self.check_register_op(op) |
| 131 |
return CLEAN |
| 132 |
|
| 133 |
def visit_set_attr(self, op: SetAttr) -> GenAndKill: |
| 134 |
cl = op.class_type.class_ir |
| 135 |
if cl.get_method(op.attr): |
| 136 |
# Property - calls a function |
| 137 |
return self.check_register_op(op) |
| 138 |
return CLEAN |
| 139 |
|
| 140 |
def visit_load_static(self, op: LoadStatic) -> GenAndKill: |
| 141 |
return CLEAN |
| 142 |
|
| 143 |
def visit_init_static(self, op: InitStatic) -> GenAndKill: |
| 144 |
return self.check_register_op(op) |
| 145 |
|
| 146 |
def visit_tuple_get(self, op: TupleGet) -> GenAndKill: |
| 147 |
return CLEAN |
| 148 |
|
| 149 |
def visit_tuple_set(self, op: TupleSet) -> GenAndKill: |
| 150 |
return self.check_register_op(op) |
| 151 |
|
| 152 |
def visit_box(self, op: Box) -> GenAndKill: |
| 153 |
return self.check_register_op(op) |
| 154 |
|
| 155 |
def visit_unbox(self, op: Unbox) -> GenAndKill: |
| 156 |
return self.check_register_op(op) |
| 157 |
|
| 158 |
def visit_cast(self, op: Cast) -> GenAndKill: |
| 159 |
return self.check_register_op(op) |
| 160 |
|
| 161 |
def visit_raise_standard_error(self, op: RaiseStandardError) -> GenAndKill: |
| 162 |
return CLEAN |
| 163 |
|
| 164 |
def visit_call_c(self, op: CallC) -> GenAndKill: |
| 165 |
return self.check_register_op(op) |
| 166 |
|
| 167 |
def visit_primitive_op(self, op: PrimitiveOp) -> GenAndKill: |
| 168 |
return self.check_register_op(op) |
| 169 |
|
| 170 |
def visit_truncate(self, op: Truncate) -> GenAndKill: |
| 171 |
return CLEAN |
| 172 |
|
| 173 |
def visit_extend(self, op: Extend) -> GenAndKill: |
| 174 |
return CLEAN |
| 175 |
|
| 176 |
def visit_load_global(self, op: LoadGlobal) -> GenAndKill: |
| 177 |
return CLEAN |
| 178 |
|
| 179 |
def visit_int_op(self, op: IntOp) -> GenAndKill: |
| 180 |
return CLEAN |
| 181 |
|
| 182 |
def visit_comparison_op(self, op: ComparisonOp) -> GenAndKill: |
| 183 |
return CLEAN |
| 184 |
|
| 185 |
def visit_float_op(self, op: FloatOp) -> GenAndKill: |
| 186 |
return CLEAN |
| 187 |
|
| 188 |
def visit_float_neg(self, op: FloatNeg) -> GenAndKill: |
| 189 |
return CLEAN |
| 190 |
|
| 191 |
def visit_float_comparison_op(self, op: FloatComparisonOp) -> GenAndKill: |
| 192 |
return CLEAN |
| 193 |
|
| 194 |
def visit_load_mem(self, op: LoadMem) -> GenAndKill: |
| 195 |
return CLEAN |
| 196 |
|
| 197 |
def visit_get_element(self, op: GetElement) -> GenAndKill: |
| 198 |
return CLEAN |
| 199 |
|
| 200 |
def visit_get_element_ptr(self, op: GetElementPtr) -> GenAndKill: |
| 201 |
return CLEAN |
| 202 |
|
| 203 |
def visit_set_element(self, op: SetElement) -> GenAndKill: |
| 204 |
return CLEAN |
| 205 |
|
| 206 |
def visit_load_address(self, op: LoadAddress) -> GenAndKill: |
| 207 |
return CLEAN |
| 208 |
|
| 209 |
def visit_keep_alive(self, op: KeepAlive) -> GenAndKill: |
| 210 |
return CLEAN |
| 211 |
|
| 212 |
def visit_unborrow(self, op: Unborrow) -> GenAndKill: |
| 213 |
return CLEAN |
| 214 |
|
| 215 |
def check_register_op(self, op: RegisterOp) -> GenAndKill: |
| 216 |
if any(src is self.self_reg for src in op.sources()): |
| 217 |
return DIRTY |
| 218 |
return CLEAN |
| 219 |
|
| 220 |
|
| 221 |
def analyze_self_leaks( |
| 222 |
blocks: list[BasicBlock], self_reg: Register, cfg: CFG |
| 223 |
) -> AnalysisResult[None]: |
| 224 |
return run_analysis( |
| 225 |
blocks=blocks, |
| 226 |
cfg=cfg, |
| 227 |
gen_and_kill=SelfLeakedVisitor(self_reg), |
| 228 |
initial=set(), |
| 229 |
backward=False, |
| 230 |
kind=MAYBE_ANALYSIS, |
| 231 |
) |