使用libFuzzer 的时候,编译得时候,需要一个命令来编译
这个python 脚本,可以递归搜集当前目录下所有C、C++源文件,
然后输出一条编译命令,到一个批处理文件中,叫做build.bat,
然后用VS直接执行这条批处理命令,即可直接编译,
由于VS有接管输出的功能,所以也可以看到编译过程,
VS命令只需要加如下两句即可:
python3 enumfile.py
$(ProjectDir)build.bat
1 import os 2 import sys 3 4 # Clang 路径 5 CLANG_PATH = "L:\LLVM\llvm-11.0.0.src\project\RelWithDebInfo\bin\clang.exe" 6 # Clang Fuzz 参数 7 FUZZER_PARAM = "-g -fsanitize=fuzzer -fsanitize=address" 8 # 输出目录 9 OUT_DIR = "out" 10 # 输出文件名 11 OUT_FILE = "main.exe" 12 13 # 要处理的文件 14 extend = [".c", ".cpp"] 15 16 17 def WalkDir(dirname): 18 global extend 19 dirlist = [] 20 filelist = [] 21 try: 22 ls = os.listdir(dirname) 23 except: 24 print("Access Deny.") 25 else: 26 for fn in ls: 27 temp = os.path.join(dirname, fn) 28 if os.path.isdir(temp): 29 dirlist.append(temp) 30 d, f = WalkDir(temp) 31 dirlist.extend(d) 32 filelist.extend(f) 33 else: 34 for ext in extend: 35 if temp.endswith(ext): 36 filelist.append(temp) 37 return dirlist, filelist 38 39 40 def GetAllSourceList(dirname): 41 dirlist, filelist = WalkDir(dirname) 42 str_file = "" 43 for file in filelist: 44 str_file = str_file + """ + file + "" " 45 return str_file 46 47 48 def WriteBuild(dirname, cmd): 49 print(cmd) 50 51 f = open(dirname + "/" + 'build.bat', 'w') 52 f.write(cmd) 53 f.close() 54 55 56 def Main(): 57 global CLANG_PATH 58 global FUZZER_PARAM 59 global OUT_FILE 60 61 argc = len(sys.argv) 62 63 # os.path.abspath('.') 64 # os.getcwd() 65 # 初值给一个当前文件所在的同级目录 66 dirname = os.path.dirname(os.path.abspath(sys.argv[0])) 67 if argc == 2: 68 dirname = sys.argv[1] 69 70 outfile = "" 71 if argc == 3: 72 outfile = sys.argv[2] 73 74 if outfile != "": 75 OUT_FILE = outfile 76 77 if dirname == "": 78 return 79 else: 80 str_file = GetAllSourceList(dirname) 81 82 os.makedirs(OUT_DIR) 83 cmd = CLANG_PATH + " " + FUZZER_PARAM + " " + "-o " + OUT_DIR + "/" + OUT_FILE + " " + str_file 84 85 WriteBuild(dirname, cmd) 86 87 88 if __name__ == "__main__": 89 Main()