diff --git a/sapp/pipeline/mariana_trench_parser_objects.py b/sapp/pipeline/mariana_trench_parser_objects.py index 5ee9d9b..39f2b25 100644 --- a/sapp/pipeline/mariana_trench_parser_objects.py +++ b/sapp/pipeline/mariana_trench_parser_objects.py @@ -18,6 +18,20 @@ UNKNOWN_LINE: int = -1 +def _parse_kind_name(kind: Union[str, Dict[str, Any]]) -> str: + if type(kind) is str: + return kind + # Parse the name in case this is a TransformKind + name = "" + if local_transform := kind.get("local"): + name += f"{local_transform}@" + if global_transform := kind.get("global"): + name += f"{global_transform}:" + name += kind["base"] + + return name + + class Method(NamedTuple): name: str @@ -268,7 +282,7 @@ def from_json( ) -> "ExtraTrace": frame_type = extra_trace["frame_type"] return ExtraTrace( - kind=extra_trace["kind"], + kind=_parse_kind_name(extra_trace["kind"]), callee=CallInfo.from_json( extra_trace["call_info"], frame_type, caller_position ), @@ -349,7 +363,7 @@ def from_json( for extra_trace in kind.get("extra_traces", []): extra_traces.append(ExtraTrace.from_json(extra_trace, caller_position)) return Kind( - name=kind["kind"], + name=_parse_kind_name(kind["kind"]), distance=kind.get("distance", 0), origins=origins, extra_traces=extra_traces, diff --git a/sapp/pipeline/tests/test_mariana_trench_parser.py b/sapp/pipeline/tests/test_mariana_trench_parser.py index 10f9e35..949cdc7 100644 --- a/sapp/pipeline/tests/test_mariana_trench_parser.py +++ b/sapp/pipeline/tests/test_mariana_trench_parser.py @@ -2295,10 +2295,19 @@ def testModelPropagations(self) -> None: } }, "frame_type": "sink", - "kind" : "T2:LocalReturn" + "kind" : + { + "base" : "LocalReturn", + "global" : "T2" + } } ], - "kind" : "T2@T1:LocalReturn", + "kind" : + { + "base" : "LocalReturn", + "global" : "T1", + "local" : "T2" + }, "origins" : [ { @@ -2792,3 +2801,389 @@ def testClassIntervals(self) -> None: ) ], ) + + def testTransformKind(self) -> None: + # Only local kinds + self.assertParsed( + """ + { + "method" : "LClass;.hopPropagation2:(I)I", + "position" : { "line" : 43, "path" : "ExtraTraces.java" }, + "propagation" : + [ + { + "input" : "Argument(0)", + "output" : + [ + { + "call_info" : + { + "call_kind" : "PropagationWithTrace:CallSite", + "port" : "Argument(0)", + "position" : { "line" : 45, "path" : "ExtraTraces.java" }, + "resolves_to" : "LClass;.hopPropagation3:(I)I" + }, + "kinds" : + [ + { + "call_kind" : "PropagationWithTrace:CallSite", + "distance" : 2, + "extra_traces" : + [ + { + "call_info" : + { + "call_kind" : "PropagationWithTrace:Origin", + "port" : "Argument(0)", + "position" : { + "line" : 44, + "path" : "ExtraTraces.java" + } + }, + "frame_type": "sink", + "kind" : + { + "base" : "LocalReturn", + "global" : "T2" + } + } + ], + "kind" : + { + "base" : "LocalReturn", + "local" : "T2" + }, + "origins" : + [ + { + "method" : "LClass;.transformT1:(I)I", + "port" : "Argument(0)" + } + ], + "output_paths" : { "" : 0 } + } + ] + } + ] + } + ] + } + """, + [ + ParseConditionTuple( + type=ParseType.PRECONDITION, + caller="LClass;.hopPropagation2:(I)I", + callee="LClass;.hopPropagation3:(I)I", + callee_location=SourceLocation( + line_no=45, + begin_column=1, + end_column=1, + ), + filename="ExtraTraces.java", + titos=[], + leaves=[("T2@LocalReturn", 2)], + caller_port="argument(0)", + callee_port="argument(0)", + type_interval=None, + features=[], + annotations=[ + ParseTraceAnnotation( + location=SourceLocation( + line_no=44, + begin_column=1, + end_column=1, + ), + kind="sink", + msg="Propagation through T2:LocalReturn", + leaf_kind="T2:LocalReturn", + leaf_depth=0, + type_interval=None, + link=None, + trace_key=None, + titos=[], + subtraces=[], + ) + ], + ) + ], + ) + + # Only global kinds + self.assertParsed( + """ + { + "method" : "LClass;.hopPropagation2:(I)I", + "position" : { "line" : 43, "path" : "ExtraTraces.java" }, + "propagation" : + [ + { + "input" : "Argument(0)", + "output" : + [ + { + "call_info" : + { + "call_kind" : "PropagationWithTrace:CallSite", + "port" : "Argument(0)", + "position" : { "line" : 45, "path" : "ExtraTraces.java" }, + "resolves_to" : "LClass;.hopPropagation3:(I)I" + }, + "kinds" : + [ + { + "call_kind" : "PropagationWithTrace:CallSite", + "distance" : 2, + "kind" : + { + "base" : "LocalReturn", + "global" : "T2" + }, + "origins" : + [ + { + "method" : "LClass;.transformT1:(I)I", + "port" : "Argument(0)" + } + ], + "output_paths" : { "" : 0 } + } + ] + } + ] + } + ] + } + """, + [ + ParseConditionTuple( + type=ParseType.PRECONDITION, + caller="LClass;.hopPropagation2:(I)I", + callee="LClass;.hopPropagation3:(I)I", + callee_location=SourceLocation( + line_no=45, + begin_column=1, + end_column=1, + ), + filename="ExtraTraces.java", + titos=[], + leaves=[("T2:LocalReturn", 2)], + caller_port="argument(0)", + callee_port="argument(0)", + type_interval=None, + features=[], + annotations=[], + ) + ], + ) + + # Both global and local kinds + self.assertParsed( + """ + { + "method" : "LClass;.hopPropagation2:(I)I", + "position" : { "line" : 43, "path" : "ExtraTraces.java" }, + "propagation" : + [ + { + "input" : "Argument(0)", + "output" : + [ + { + "call_info" : + { + "call_kind" : "PropagationWithTrace:CallSite", + "port" : "Argument(0)", + "position" : { "line" : 45, "path" : "ExtraTraces.java" }, + "resolves_to" : "LClass;.hopPropagation3:(I)I" + }, + "kinds" : + [ + { + "call_kind" : "PropagationWithTrace:CallSite", + "distance" : 2, + "extra_traces" : + [ + { + "call_info" : + { + "call_kind" : "PropagationWithTrace:Origin", + "port" : "Argument(0)", + "position" : { + "line" : 44, + "path" : "ExtraTraces.java" + } + }, + "frame_type": "sink", + "kind" : + { + "base" : "LocalReturn", + "global" : "T2" + } + } + ], + "kind" : + { + "base" : "LocalReturn", + "global" : "T1", + "local" : "T2" + }, + "origins" : + [ + { + "method" : "LClass;.transformT1:(I)I", + "port" : "Argument(0)" + } + ], + "output_paths" : { "" : 0 } + } + ] + } + ] + } + ] + } + """, + [ + ParseConditionTuple( + type=ParseType.PRECONDITION, + caller="LClass;.hopPropagation2:(I)I", + callee="LClass;.hopPropagation3:(I)I", + callee_location=SourceLocation( + line_no=45, + begin_column=1, + end_column=1, + ), + filename="ExtraTraces.java", + titos=[], + leaves=[("T2@T1:LocalReturn", 2)], + caller_port="argument(0)", + callee_port="argument(0)", + type_interval=None, + features=[], + annotations=[ + ParseTraceAnnotation( + location=SourceLocation( + line_no=44, + begin_column=1, + end_column=1, + ), + kind="sink", + msg="Propagation through T2:LocalReturn", + leaf_kind="T2:LocalReturn", + leaf_depth=0, + type_interval=None, + link=None, + trace_key=None, + titos=[], + subtraces=[], + ) + ], + ) + ], + ) + + # Multiple global and local kinds + self.assertParsed( + """ + { + "method" : "LClass;.hopPropagation2:(I)I", + "position" : { "line" : 43, "path" : "ExtraTraces.java" }, + "propagation" : + [ + { + "input" : "Argument(0)", + "output" : + [ + { + "call_info" : + { + "call_kind" : "PropagationWithTrace:CallSite", + "port" : "Argument(0)", + "position" : { "line" : 45, "path" : "ExtraTraces.java" }, + "resolves_to" : "LClass;.hopPropagation3:(I)I" + }, + "kinds" : + [ + { + "call_kind" : "PropagationWithTrace:CallSite", + "distance" : 2, + "extra_traces" : + [ + { + "call_info" : + { + "call_kind" : "PropagationWithTrace:Origin", + "port" : "Argument(0)", + "position" : { + "line" : 44, + "path" : "ExtraTraces.java" + } + }, + "frame_type": "sink", + "kind" : + { + "base" : "LocalReturn", + "global" : "T1:T2" + } + } + ], + "kind" : + { + "base" : "LocalReturn", + "global" : "T3:T4", + "local" : "T1:T2" + }, + "origins" : + [ + { + "method" : "LClass;.transformT1:(I)I", + "port" : "Argument(0)" + } + ], + "output_paths" : { "" : 0 } + } + ] + } + ] + } + ] + } + """, + [ + ParseConditionTuple( + type=ParseType.PRECONDITION, + caller="LClass;.hopPropagation2:(I)I", + callee="LClass;.hopPropagation3:(I)I", + callee_location=SourceLocation( + line_no=45, + begin_column=1, + end_column=1, + ), + filename="ExtraTraces.java", + titos=[], + leaves=[("T1:T2@T3:T4:LocalReturn", 2)], + caller_port="argument(0)", + callee_port="argument(0)", + type_interval=None, + features=[], + annotations=[ + ParseTraceAnnotation( + location=SourceLocation( + line_no=44, + begin_column=1, + end_column=1, + ), + kind="sink", + msg="Propagation through T1:T2:LocalReturn", + leaf_kind="T1:T2:LocalReturn", + leaf_depth=0, + type_interval=None, + link=None, + trace_key=None, + titos=[], + subtraces=[], + ) + ], + ) + ], + )