Skip to content

Commit 4d5a33f

Browse files
authored
webapp: Add API for macro block query (#2171)
Signed-off-by: Arthur Chan <[email protected]>
1 parent 56df0f1 commit 4d5a33f

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

tools/web-fuzzing-introspection/app/static/assets/db/oss_fuzz.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def get_introspector_report_url_typedef(project_name, datestr):
5151
return get_introspector_report_url_base(project_name,
5252
datestr) + "full_type_defs.json"
5353

54+
def get_introspector_report_url_macro_block(project_name, datestr):
55+
return get_introspector_report_url_base(project_name,
56+
datestr) + "macro_block_info.json"
57+
5458

5559
def get_fuzzer_stats_fuzz_count_url(project_name, date_str):
5660
base_url = 'https://storage.googleapis.com/oss-fuzz-coverage/{0}/fuzzer_stats/{1}/coverage_targets.txt'
@@ -249,6 +253,17 @@ def extract_local_introspector_typedef(project_name, oss_fuzz_folder):
249253
return json_list
250254

251255

256+
def extract_local_introspector_macro_block(project_name, oss_fuzz_folder):
257+
summary_json = os.path.join(oss_fuzz_folder, 'build', 'out', project_name,
258+
'inspector', 'macro_block_info.json')
259+
if not os.path.isfile(summary_json):
260+
return {}
261+
262+
with open(summary_json, 'r') as f:
263+
json_list = json.load(f)
264+
return json_list
265+
266+
252267
def get_local_code_coverage_summary(project_name, oss_fuzz_folder):
253268
summary_json = os.path.join(oss_fuzz_folder, 'build', 'out', project_name,
254269
'report', 'linux', 'summary.json')
@@ -400,6 +415,22 @@ def extract_introspector_typedef(project_name, date_str):
400415
return typedef_list
401416

402417

418+
def extract_introspector_macro_block(project_name, date_str):
419+
introspector_test_url = get_introspector_report_url_macro_block(
420+
project_name, date_str.replace("-", ""))
421+
422+
# Read the introspector atifact
423+
try:
424+
raw_introspector_json_request = requests.get(introspector_test_url,
425+
timeout=10)
426+
except:
427+
return []
428+
try:
429+
return json.loads(raw_introspector_json_request.text)
430+
except:
431+
return []
432+
433+
403434
def extract_introspector_report(project_name, date_str):
404435
introspector_summary_url = get_introspector_report_url_summary(
405436
project_name, date_str.replace("-", ""))

tools/web-fuzzing-introspection/app/static/assets/db/web_db_creator_from_summary.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ def extract_local_project_data(project_name, oss_fuzz_path,
453453
typedef_list = oss_fuzz.extract_local_introspector_typedef(
454454
project_name, oss_fuzz_path)
455455

456+
# Extract macro block information
457+
macro_block = oss_fuzz.extract_local_introspector_macro_block(
458+
project_name, oss_fuzz_path)
459+
456460
introspector_data_dict = {
457461
"introspector_report_url":
458462
'introspector_url',
@@ -475,7 +479,9 @@ def extract_local_project_data(project_name, oss_fuzz_path,
475479
'project_name':
476480
project_name,
477481
'typedef_list':
478-
typedef_list
482+
typedef_list,
483+
'macro_block':
484+
macro_block
479485
}
480486

481487
code_coverage_data_dict = prepare_code_coverage_dict(
@@ -731,6 +737,10 @@ def extract_project_data(project_name, date_str, should_include_details,
731737
typedef_list = oss_fuzz.extract_introspector_typedef(
732738
project_name, date_str)
733739

740+
# Extract macro block information
741+
macro_block = oss_fuzz.extract_introspector_macro_block(
742+
project_name, date_str)
743+
734744
introspector_data_dict = {
735745
"introspector_report_url": introspector_report_url,
736746
"coverage_lines":
@@ -745,7 +755,8 @@ def extract_project_data(project_name, date_str, should_include_details,
745755
'annotated_cfg': annotated_cfg,
746756
'optimal_targets': optimal_targets,
747757
'project_name': project_name,
748-
'typedef_list': typedef_list
758+
'typedef_list': typedef_list,
759+
'macro_block': macro_block
749760
}
750761

751762
code_coverage_data_dict = prepare_code_coverage_dict(

tools/web-fuzzing-introspection/app/webapp/routes.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,11 +1349,65 @@ def api_full_type_definition(args):
13491349
return {'result': 'error', 'msg': 'Found no introspector data.'}
13501350

13511351

1352+
@blueprint.route('/api/check_macro')
1353+
def api_check_macro():
1354+
"""API that returns all macro block wrapper for a line range in a
1355+
specific source file of a project."""
1356+
source_file = request.args.get('source', '')
1357+
if not source_file:
1358+
return {'result': 'error', 'msg': 'Please provide source file path'}
1359+
1360+
project_name = request.args.get('project', '')
1361+
if not project_name:
1362+
return {'result': 'error', 'msg': 'Please provide project name'}
1363+
1364+
target_project = get_project_with_name(project_name)
1365+
if target_project is None:
1366+
return {'result': 'error', 'msg': 'Project not in the database'}
1367+
1368+
line_start = int(request.args.get('start', 0))
1369+
line_end = int(request.args.get('end', 999999))
1370+
1371+
if target_project.introspector_data is None:
1372+
return {'result': 'error', 'msg': 'Found no introspector data.'}
1373+
1374+
try:
1375+
result_list = []
1376+
macro_list = target_project.introspector_data.get('macro_block')
1377+
1378+
if not macro_list:
1379+
return {'result': 'error', 'msg': 'Found no introspector data.'}
1380+
1381+
for macro in macro_list:
1382+
pos = macro.get('pos')
1383+
if not pos:
1384+
continue
1385+
1386+
macro_source = pos.get('source_file')
1387+
macro_start = pos.get('line_start', -1)
1388+
macro_end = pos.get('line_end', -1)
1389+
1390+
if source_file != macro_source:
1391+
continue
1392+
1393+
if line_start <= macro_end and macro_start <= line_end:
1394+
result_list.append(macro)
1395+
return {
1396+
'result': 'success',
1397+
'project': {
1398+
'name': project_name,
1399+
'macro_block_info': result_list
1400+
}
1401+
}
1402+
except TypeError:
1403+
return {'result': 'error', 'msg': 'Found no introspector data.'}
1404+
1405+
13521406
@api_blueprint.route('/api/project-summary')
13531407
@api_blueprint.arguments(ProjectSchema, location='query')
13541408
def api_project_summary(args):
13551409
"""Returns high-level fuzzing stats of a given project.
1356-
1410+
13571411
# Examples
13581412
13591413
## Example 1

0 commit comments

Comments
 (0)