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 os 19import time 20 21GN_ENTRY_TEMPLATE = """\ 22 23group("hydra_fuzz"){ 24 testonly = true 25 if (use_libfuzzer) { 26 deps = ["//test/fuzzing_test/projects"] 27 }else{ 28 deps = [] 29 } 30} 31""" 32 33 34PROJECT_GN_TEMPLATE = """\ 35# Copyright (c) 2021 Huawei Device Co., Ltd. 36# Licensed under the Apache License, Version 2.0 (the "License"); 37# you may not use this file except in compliance with the License. 38# You may obtain a copy of the License at 39# 40# http://www.apache.org/licenses/LICENSE-2.0 41# 42# Unless required by applicable law or agreed to in writing, software 43# distributed under the License is distributed on an "AS IS" BASIS, 44# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 45# See the License for the specific language governing permissions and 46# limitations under the License. 47 48#####################hydra-fuzz################### 49import("//build/config/features.gni") 50import("//build/test.gni") 51 52##############################fuzztest########################################## 53ohos_fuzztest("") { 54 module_out_path = module_output_path 55 56 include_dirs = [ 57 ] 58 cflags = ["-g","-O0","-Wno-unused-variable","-fno-omit-frame-pointer"] 59 sources = [ 60 "%(project_name)s.cpp", 61 ] 62} 63############################################################################### 64group("fuzztest") { 65 testonly = true 66 deps = [] 67 deps += [ 68 # deps file 69 ":", 70 ] 71} 72############################################################################### 73""" 74 75 76PROJECT_DEMO_TEMPLATE = """\ 77/* 78 * Copyright (c) 2021 Huawei Device Co., Ltd. 79 * Licensed under the Apache License, Version 2.0 (the "License"); 80 * you may not use this file except in compliance with the License. 81 * You may obtain a copy of the License at 82 * 83 * http://www.apache.org/licenses/LICENSE-2.0 84 * 85 * Unless required by applicable law or agreed to in writing, software 86 * distributed under the License is distributed on an "AS IS" BASIS, 87 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 88 * See the License for the specific language governing permissions and 89 * limitations under the License. 90 */ 91 92#include "%(project_name)s.h" 93 94#include <stddef.h> 95#include <stdint.h> 96 97const int FUZZ_DATA_LEN = 3; 98const int FUZZ_FST_DATA = 0; 99const int FUZZ_SND_DATA = 1; 100const int FUZZ_TRD_DATA = 2; 101const int FUZZ_FTH_DATA = 3; 102 103namespace OHOS { 104 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) 105 { 106 bool result = false; 107 if (size >= FUZZ_DATA_LEN) { 108 result = data[FUZZ_FST_DATA] == 'F' && 109 data[FUZZ_SND_DATA] == 'U' && 110 data[FUZZ_TRD_DATA] == 'Z' && 111 data[FUZZ_FTH_DATA] == 'Z'; 112 } 113 return result; 114 } 115} 116 117/* Fuzzer entry point */ 118extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 119{ 120 /* Run your code on data */ 121 OHOS::DoSomethingInterestingWithMyAPI(data, size); 122 return 0; 123} 124 125""" 126 127PROJECT_HEADER_TEMPLATE = """\ 128/* 129 * Copyright (c) 2021 Huawei Device Co., Ltd. 130 * Licensed under the Apache License, Version 2.0 (the "License"); 131 * you may not use this file except in compliance with the License. 132 * You may obtain a copy of the License at 133 * 134 * http://www.apache.org/licenses/LICENSE-2.0 135 * 136 * Unless required by applicable law or agreed to in writing, software 137 * distributed under the License is distributed on an "AS IS" BASIS, 138 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139 * See the License for the specific language governing permissions and 140 * limitations under the License. 141 */ 142 143#include <cstdint> 144#include <unistd.h> 145#include <climits> 146#include <cstdio> 147#include <cstdlib> 148#include <fcntl.h> 149 150#define FUZZ_PROJECT_NAME "%(project_name)s" 151 152""" 153 154 155PROJECT_XML_TEMPLATE = """\ 156<?xml version="1.0" encoding="utf-8"?> 157<!-- Copyright (c) 2021 Huawei Device Co., Ltd. 158 159 Licensed under the Apache License, Version 2.0 (the "License"); 160 you may not use this file except in compliance with the License. 161 You may obtain a copy of the License at 162 163 http://www.apache.org/licenses/LICENSE-2.0 164 165 Unless required by applicable law or agreed to in writing, software 166 distributed under the License is distributed on an "AS IS" BASIS, 167 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 168 See the License for the specific language governing permissions and 169 limitations under the License. 170--> 171<fuzz_config> 172 <fuzztest> 173 <!-- maximum length of a test input --> 174 <max_len>1000</max_len> 175 <!-- maximum total time in seconds to run the fuzzer --> 176 <max_total_time>300</max_total_time> 177 <!-- memory usage limit in Mb --> 178 <rss_limit_mb>4096</rss_limit_mb> 179 </fuzztest> 180</fuzz_config> 181""" 182 183 184REPORT_CSS_TEMPLATE = """ 185""" 186 187 188def render_tbody(data): 189 res = "" 190 for row in data: 191 row_line = "<tr>" 192 for row_td in row: 193 row_line = row_line \ 194 + "\n<th scope=\"col\">{}</td>\n".format(row_td) 195 row_line = "%s %s " % (row_line, "</tr>") 196 res = res + row_line 197 return res 198 199 200def render_common(data): 201 return REPORT_COMMON_HTML_TEMPLATE % data 202 203 204def get_format_bt(backtrace): 205 new_backtrack = "" 206 line_tag = ["#0", "#1", "#2", "#3", "#4", "#5", "#6", "#7"] 207 block_file_list = [ 208 "sanitizer_common_interceptors.inc", 209 "FuzzerDriver.cpp", 210 "FuzzerLoop.cpp", 211 "FuzzerMain.cpp", 212 "??" 213 ] 214 215 tmp_flag = False 216 for line in backtrace.split("\n"): 217 tag_check = False 218 for tag in line_tag: 219 if line.strip().startswith(tag) == True \ 220 and "in" in line and "exec/s" not in line: 221 tag_check = True 222 break 223 224 if tag_check: 225 block_flag = False 226 for block_line in block_file_list: 227 if block_line in line: 228 block_flag = True 229 break 230 if block_flag: 231 new_backtrack = " %s %s \n" % (new_backtrack, line) 232 else: 233 234 end_line = '<b target="view_frame" \ 235 style="color: red; font-size:16px;\ 236 " > %s </b>\n' % line 237 new_backtrack = " %s %s " % (new_backtrack, end_line) 238 else: 239 new_backtrack = " %s %s \n" % (new_backtrack, line) 240 return new_backtrack