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    @staticmethod
60    def filter_log(log_str):
61        ansi_escape = re.compile(r'''
62            \x1B
63            (?:
64                [@-Z\\-_]
65            |
66                \[
67                [0-?]*
68                [ -/]*
69                [@-~]
70            )
71        ''', re.VERBOSE)
72        result = ansi_escape.sub('', log_str)
73        return result
74
75    # 获取日志
76    def get_log(self):
77        return "code :{}, msg: {}".format(self.code, self.data)
78
79    # 分析结果
80    def analysis(self, result, outdir):
81        pass
82
83    def write_analysis_result(self, analysis_ressult_path, html_format=True):
84        if os.path.exists(analysis_ressult_path):
85            os.remove(analysis_ressult_path)
86        with os.fdopen(os.open(analysis_ressult_path, FLAGS, MODES), 'wb') as f:
87            if html_format:
88                f.write(RunResult.filter_log(render_detail(self.crash_info)))
89            else:
90                f.write(RunResult.filter_log(self.crash_info["backtrace"]))
91
92
93if __name__ == "__main__":
94    cmd_log = ""
95    res_obj = RunResult(0, "OK")
96    res_obj.analysis(cmd_log, "../../../out/")
97    print(res_obj.crash_info)
98
99