1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dexfuzz; 18 19 import dexfuzz.fuzzers.Fuzzer; 20 import dexfuzz.fuzzers.FuzzerMultipleExecute; 21 import dexfuzz.fuzzers.FuzzerMultipleNoExecute; 22 import dexfuzz.fuzzers.FuzzerSingleExecute; 23 import dexfuzz.fuzzers.FuzzerSingleNoExecute; 24 import dexfuzz.listeners.BisectionSearchListener; 25 import dexfuzz.listeners.ConsoleLoggerListener; 26 import dexfuzz.listeners.FinalStatusListener; 27 import dexfuzz.listeners.LogFileListener; 28 import dexfuzz.listeners.MultiplexerListener; 29 import dexfuzz.listeners.UniqueProgramTrackerListener; 30 import dexfuzz.listeners.UpdatingConsoleListener; 31 32 /** 33 * Entrypoint class for dexfuzz. 34 */ 35 public class DexFuzz { 36 private static int majorVersion = 1; 37 private static int minorVersion = 1; 38 private static int seedChangeVersion = 0; 39 40 /** 41 * Entrypoint to dexfuzz. 42 */ main(String[] args)43 public static void main(String[] args) { 44 // Report the version number, which should be incremented every time something will cause 45 // the same input seed to produce a different result than before. 46 Log.always(String.format("DexFuzz v%d.%d.%d", 47 majorVersion, minorVersion, seedChangeVersion)); 48 Log.always(""); 49 50 if (!Options.readOptions(args)) { 51 Log.error("Failed to validate options."); 52 Options.usage(); 53 } 54 55 56 // Create a Listener that is responsible for multiple Listeners. 57 MultiplexerListener multipleListener = new MultiplexerListener(); 58 multipleListener.setup(); 59 60 FinalStatusListener statusListener = new FinalStatusListener(); 61 multipleListener.addListener(statusListener); 62 63 if (Options.repeat > 1 && Options.execute) { 64 // Add the live updating listener, but only if we're not printing out lots of logs. 65 if (!Log.likelyToLog()) { 66 multipleListener.addListener(new UpdatingConsoleListener()); 67 } else { 68 // If we are dumping out lots of logs, then use the ConsoleLogger instead. 69 multipleListener.addListener(new ConsoleLoggerListener()); 70 } 71 // Add the file logging listener. 72 multipleListener.addListener(new LogFileListener(Options.reportLogFile)); 73 if (Options.runBisectionSearch) { 74 // Add the bisection search listener. 75 multipleListener.addListener(new BisectionSearchListener()); 76 } 77 // Add the unique program tracker. 78 multipleListener.addListener(new UniqueProgramTrackerListener(Options.uniqueDatabaseFile)); 79 } else { 80 // Just use the basic listener. 81 multipleListener.addListener(new ConsoleLoggerListener()); 82 } 83 84 // Create the Fuzzer that uses a particular strategy for fuzzing. 85 Fuzzer fuzzer = null; 86 if ((Options.repeat > 1) && Options.execute) { 87 fuzzer = new FuzzerMultipleExecute(multipleListener); 88 } else if ((Options.repeat > 1) && !Options.execute) { 89 fuzzer = new FuzzerMultipleNoExecute(multipleListener); 90 } else if ((Options.repeat == 1) && Options.execute) { 91 fuzzer = new FuzzerSingleExecute(multipleListener); 92 } else if ((Options.repeat == 1) && !Options.execute) { 93 fuzzer = new FuzzerSingleNoExecute(multipleListener); 94 } else { 95 Log.errorAndQuit("Invalid options provided, desired fuzzer unknown."); 96 } 97 // TODO: Implement FuzzerFindMinimalMutations. 98 // TODO: Implement FuzzerGenerational. 99 100 // Actually run the Fuzzer. 101 fuzzer.run(); 102 fuzzer.printTimingInfo(); 103 fuzzer.shutdown(); 104 105 // Cleanup the Listener. 106 multipleListener.shutdown(); 107 108 if (!statusListener.isSuccessful()) { 109 System.exit(1); 110 } 111 } 112 } 113