Skip to content

Commit

Permalink
Add traces support to SAPP
Browse files Browse the repository at this point in the history
  • Loading branch information
the-storm committed Jul 20, 2023
1 parent fc5695c commit 4a4a7c0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 40 deletions.
36 changes: 12 additions & 24 deletions sapp/sarif.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,28 @@ def __init__(
"pysa": (5000, 6000),
}
driver_json = {}
self.tool = tool
if self.tool == "pysa":
if tool == "pysa":
driver_json["name"] = "Pysa"
driver_json["informationUri"] = "https://github.com/facebook/pyre-check/"

tool_warning_messages = get_warning_message_range(
session,
self._tool_warning_code_ranges[tool][0],
self._tool_warning_code_ranges[tool][1],
)
rules_json = []
for rule in tool_warning_messages:
rules_json.append({"id": str(rule.code), "name": rule.message})
driver_json["rules"] = rules_json
elif self.tool == "mariana-trench":
elif tool == "mariana-trench":
driver_json["name"] = "Mariana Trench"
driver_json[
"informationUri"
] = "https://github.com/facebook/mariana-trench/"

tool_warning_messages = get_warning_message_range(
session,
self._tool_warning_code_ranges[self.tool][0],
self._tool_warning_code_ranges[self.tool][1],
)
rules_json = []
for rule in tool_warning_messages:
rules_json.append({"id": str(rule.code), "name": rule.message})
driver_json["rules"] = rules_json
else:
raise NotImplementedError

tool_warning_messages = get_warning_message_range(
session,
self._tool_warning_code_ranges[tool][0],
self._tool_warning_code_ranges[tool][1],
)
rules_json = []
for rule in tool_warning_messages:
rules_json.append({"id": str(rule.code), "name": rule.message})
driver_json["rules"] = rules_json
self.driver = driver_json
self.results = [issue.to_sarif(session, self.tool) for issue in filtered_issues]
self.results = [issue.to_sarif(session, tool) for issue in filtered_issues]

def to_json(self, indent: int = 2) -> str:
return json.dumps(self, cls=SARIFEncoder, indent=indent)
Expand Down
31 changes: 15 additions & 16 deletions sapp/ui/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from ..sarif_types import (
SARIFCodeflowLocationObject,
SARIFCodeflowsObject,
SARIFPhyicalLocationObject,
)
from . import run
from .issues import IssueQueryResult
Expand Down Expand Up @@ -121,20 +120,22 @@ def is_leaf(self) -> bool:
return self.callee_port in LEAF_NAMES

def get_clean_caller(self, tool: str) -> str:
return self._clean_callable_name(self.caller, tool)
return self._human_readable_name(self.caller, tool)

def get_clean_callee(self, tool: str) -> str:
return self._clean_callable_name(self.callee, tool)
return self._human_readable_name(self.callee, tool)

def _clean_callable_name(self, name: str, tool: str, depth: int = 1) -> str:
def _human_readable_name(self, name: str, tool: str, depth: int = 1) -> str:
if name in LEAF_NAMES:
return name
if tool == "mariana-trench":
package_and_class, method_name = name.split(".")
method_name = method_name.split(":")[0] # parse
package_and_class = package_and_class.strip(";").split("/")[
depth * -1
] # parse Lpackage1/package2/class;.
# convert name from dalvik byte code e.g., Lpackage1/package2/class;.Func1 to human readable name class.Func1
package_class_and_method_name = name.split(".")
if len(package_class_and_method_name) != 2:
return name
package_and_class, method_name = package_class_and_method_name
method_name = method_name.split(":")[0]
package_and_class = package_and_class.strip(";").split("/")[depth * -1]
return f"{package_and_class}.{method_name}"
else:
return name
Expand Down Expand Up @@ -468,16 +469,15 @@ def to_sarif(
trace_tuples = _create_trace_tuples(
reversed(postcondition_navigation)
) + _create_trace_tuples(precondition_navigation)
codeflows = [{"threadFlows": [{"locations": []}]}]
codeflows = []
nesting_level = 0
for t in trace_tuples:
location = _sarif_codeflow_location_from_trace_tuple(
t.trace_frame, tool, nesting_level, True
t.trace_frame, tool, nesting_level, output_features=True
)
codeflows[0]["threadFlows"][0]["locations"].append(location)
codeflows.append(location)
nesting_level += 1

return codeflows
return [{"threadFlows": [{"locations": codeflows}]}]


def _create_trace_tuples(
Expand Down Expand Up @@ -513,7 +513,7 @@ def _sarif_codeflow_location_from_trace_tuple(
trace_region = {}
if trace_frame.callee_location:
trace_region = trace_frame.callee_location.to_sarif()
location = {
return {
"location": {
"physicalLocation": {
"artifactLocation": {
Expand All @@ -528,4 +528,3 @@ def _sarif_codeflow_location_from_trace_tuple(
},
"nestingLevel": nesting_level,
}
return location

0 comments on commit 4a4a7c0

Please sign in to comment.