1#!/usr/bin/env python 2# -*- coding:utf-8 -*- 3# 4# Copyright (c) 2021 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18import re 19import os 20import stat 21 22FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL 23MODES = stat.S_IWUSR | stat.S_IRUSR 24 25 26class RunResult(): 27 SUCCESS = 0 28 29 RUN_ENV_ERROR = -1 30 RUN_NO_DEVICE_ERROR = -2 31 RUN_CONFIG_FORMAT_ERROR = -3 32 RUN_CONFIG_FORMAT_NOCOLON_ERROR = -4 33 RUN_FUZZER_BIN_NOT_FOUND_ERROR = -5 34 RUN_CONNECT_ERROR = -6 35 RUN_EXEC_ERROR = -7 36 RUN_CONFIG_DICT_ERROR = -8 37 RUN_LINK_ERROR = -9 38 39 40 def __init__(self, code, data): 41 self.code = code 42 self.data = data 43 self.payload = {} 44 45 self.crash_info = { 46 "run_times": 0, 47 "log": "", 48 "project": "", 49 "speed": 0, 50 "summary": "No vulnerable", 51 "command_log": "", 52 "vulnerable": False, 53 "backtrace": "", 54 "cov": 0, 55 "libscov": {}, 56 "report_progress": 0 57 } 58 59 60 def get_log(self): 61 return "code :{}, msg: {}".format(self.code, self.data) 62 63 @staticmethod 64 def filter_log(log_str): 65 ansi_escape = re.compile(r''' 66 \x1B 67 (?: 68 [@-Z\\-_] 69 | 70 \[ 71 [0-?]* 72 [ -/]* 73 [@-~] 74 ) 75 ''', re.VERBOSE) 76 result = ansi_escape.sub('', log_str) 77 return result 78 79 def analysis(self, result, outdir): 80 pass 81 82 def write_analysis_result(self, analysis_ressult_path, html_format=True): 83 if os.path.exists(analysis_ressult_path): 84 os.remove(analysis_ressult_path) 85 with os.fdopen(os.open(analysis_ressult_path, FLAGS, MODES), 'wb') as f: 86 if html_format: 87 f.write(RunResult.filter_log(render_detail(self.crash_info))) 88 else: 89 f.write(RunResult.filter_log(self.crash_info["backtrace"])) 90 91 92if __name__ == "__main__": 93 cmd_log = "" 94 res_obj = RunResult(0, "OK") 95 res_obj.analysis(cmd_log, "../../../out/") 96 print(res_obj.crash_info) 97 98