forked from PurCL/RepoAudit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintra_dataflow_analyzer.py
More file actions
250 lines (221 loc) · 9.5 KB
/
intra_dataflow_analyzer.py
File metadata and controls
250 lines (221 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from os import path
import json
import time
from typing import List, Set, Optional, Dict, Union
from llmtool.LLM_utils import *
from llmtool.LLM_tool import *
from memory.syntactic.function import *
from memory.syntactic.value import *
from memory.syntactic.api import *
BASE_PATH = Path(__file__).resolve().parent.parent.parent
class IntraDataFlowAnalyzerInput(LLMToolInput):
def __init__(
self,
function: Function,
summary_start: Value,
sink_values: List[Tuple[str, int]],
call_statements: List[Tuple[str, int]],
ret_values: List[Tuple[str, int]],
non_locals: List[Tuple[str, int]],
) -> None:
self.function = function
self.summary_start = summary_start
self.sink_values = sink_values
self.call_statements = call_statements
self.ret_values = ret_values
self.non_locals = non_locals
return
def __hash__(self) -> int:
return hash((self.function.function_id, str(self.summary_start)))
class IntraDataFlowAnalyzerOutput(LLMToolOutput):
def __init__(self, reachable_values: List[Set[Value]]) -> None:
self.reachable_values = reachable_values
return
def __str__(self):
output_str = ""
for i, reachable_values_per_path in enumerate(self.reachable_values):
output_str += f"Path {i}:\n"
for value in reachable_values_per_path:
output_str += f"- {value}\n"
return output_str
class IntraDataFlowAnalyzer(LLMTool):
def __init__(
self,
model_name: str,
temperature: float,
language: str,
max_query_num: int,
logger: Logger,
) -> None:
"""
:param model_name: the model name
:param temperature: the temperature
:param language: the programming language
:param max_query_num: the maximum number of queries if the model fails
:param logger: the logger
"""
super().__init__(model_name, temperature, language, max_query_num, logger)
self.prompt_file = (
f"{BASE_PATH}/prompt/{language}/dfbscan/intra_dataflow_analyzer.json"
)
return
def _get_prompt(self, input: LLMToolInput) -> str:
if not isinstance(input, IntraDataFlowAnalyzerInput):
raise TypeError("Expect IntraDataFlowAnalyzerInput")
with open(self.prompt_file, "r") as f:
prompt_template_dict = json.load(f)
prompt = prompt_template_dict["task"]
prompt += "\n" + "\n".join(prompt_template_dict["analysis_rules"])
prompt += "\n" + "\n".join(prompt_template_dict["analysis_examples"])
prompt += "\n" + "".join(prompt_template_dict["meta_prompts"])
prompt = prompt.replace(
"<ANSWER>", "\n".join(prompt_template_dict["answer_format_cot"])
)
prompt = prompt.replace("<QUESTION>", prompt_template_dict["question_template"])
prompt = (
prompt.replace("<FUNCTION>", input.function.lined_code)
.replace("<SRC_NAME>", input.summary_start.name)
.replace(
"<SRC_LINE>",
str(
input.summary_start.line_number
- input.function.start_line_number
+ 1
),
)
)
sinks_str = "Sink values in this function:\n"
for sink_value in input.sink_values:
sinks_str += f"- {sink_value[0]} at line {sink_value[1]}\n"
prompt = prompt.replace("<SINK_VALUES>", sinks_str)
calls_str = "Call statements in this function:\n"
for call_statement in input.call_statements:
calls_str += f"- {call_statement[0]} at line {call_statement[1]}\n"
prompt = prompt.replace("<CALL_STATEMENTS>", calls_str)
rets_str = "Return values in this function:\n"
for ret_val in input.ret_values:
rets_str += f"- {ret_val[0]} at line {ret_val[1]}\n"
prompt = prompt.replace("<RETURN_VALUES>", rets_str)
if input.non_locals:
non_local_str = "Non local variables relevant to this function:\n"
for non_local in input.non_locals:
non_local_str += f"- {non_local[0]} at line {non_local[1]}\n"
prompt = prompt.replace("<NONLOCAL_VALUES>", non_local_str)
else:
prompt = prompt.replace("<NONLOCAL_VALUES>", "")
return prompt
def _parse_response(
self, response: str, input: Optional[LLMToolInput] = None
) -> Optional[LLMToolOutput]:
"""
Parse the LLM response to extract all execution paths and their propagation details.
Args:
response (str): The response string from the LLM.
input (IntraDataFlowAnalyzerInput): The input object containing function details.
Returns:
IntraDataFlowAnalyzerOutput: The output containing reachable values for each path.
"""
paths: List[Dict] = []
# Regex to match a path header line, e.g., "Path 1: Lines 2 -> 3"
path_header_re = re.compile(r"Path\s*(\d+):\s*([^;]+);?$")
# Regex to match a propagation detail line, e.g.,
# " - Type: Return; Name: getNullObject(); Function: None; Index: 0; Line: 3; Dependency: ..."
detail_re = re.compile(
r"Type:\s*([^;]+);\s*"
r"Name:\s*([^;]+);\s*"
r"Function:\s*([^;]+);\s*"
r"Index:\s*([^;]+);\s*"
r"Line:\s*([^;]+);"
)
current_path: Optional[Dict[str, Union[str, list]]] = None
for line in response.splitlines():
line = line.strip().lstrip("-").strip()
if not line:
continue
# Check for path header
header_match = path_header_re.match(line)
if header_match:
if current_path:
paths.append(current_path)
current_path = {
"path_number": header_match.group(1).strip(),
"execution_path": header_match.group(2).strip(),
"propagation_details": [],
}
else:
# Check for propagation detail line
detail_match = detail_re.match(line)
if detail_match and current_path is not None:
detail = {
"type": detail_match.group(1).strip(),
"name": detail_match.group(2).strip(),
"function": detail_match.group(3).strip(),
"index": detail_match.group(4).strip(),
"line": detail_match.group(5).strip(),
}
if isinstance(current_path["propagation_details"], list):
current_path["propagation_details"].append(detail)
elif current_path is not None:
paths.append(current_path)
current_path = None
if current_path:
paths.append(current_path)
assert input is not None, "input cannot be none"
if not isinstance(input, IntraDataFlowAnalyzerInput):
raise TypeError("Expect IntraDataFlowAnalyzerInput")
# Process paths to extract reachable values
reachable_values = []
file_path = input.function.file_path
start_line_number = input.function.start_line_number
for single_path in paths:
reachable_values_per_path = set()
for detail in single_path["propagation_details"]:
if not detail["line"].isdigit():
continue
line_number = int(detail["line"]) + start_line_number - 1
if detail["type"] == "Argument":
reachable_values_per_path.add(
Value(
detail["name"],
line_number,
ValueLabel.ARG,
file_path,
int(detail["index"]),
)
)
elif detail["type"] == "Parameter":
reachable_values_per_path.add(
Value(
detail["name"],
line_number,
ValueLabel.PARA,
file_path,
int(detail["index"]),
)
)
elif detail["type"] == "Return":
reachable_values_per_path.add(
Value(
detail["name"],
line_number,
ValueLabel.RET,
file_path,
int(detail["index"]),
)
)
elif detail["type"] == "Sink":
reachable_values_per_path.add(
Value(detail["name"], line_number, ValueLabel.SINK, file_path)
)
elif detail["type"] == "Nonlocal":
reachable_values_per_path.add(
Value(
detail["name"], line_number, ValueLabel.NONLOCAL, file_path
)
)
reachable_values.append(reachable_values_per_path)
output = IntraDataFlowAnalyzerOutput(reachable_values)
self.logger.print_log(
"Output of intra-procedural data-flow analyzer:", output.reachable_values
)
return output