diff --git a/fuzzer/Fuzzer.java b/fuzzer/Fuzzer.java index cdd356bc30cf8ca0f05ac247397ae01253992965..d86fe94f9ef74194e9bcefdd3f2d7b663815b5b2 100644 --- a/fuzzer/Fuzzer.java +++ b/fuzzer/Fuzzer.java @@ -40,6 +40,11 @@ public class Fuzzer { private static ArrayList<String> vars = new ArrayList<>(); + // Instruction Count Array + private static int[] counts = {0,0,0,0,0,0,0,0,0,0,0,0}; + // Instruction Added Probability Array + private static int[] addProb = {0,0,0,0,0,0,0,0,0,0,0,0}; + public static void main(String[] args) throws IOException { System.out.println(Instruction.getBNF()); FileOutputStream out = null; @@ -120,6 +125,17 @@ public class Fuzzer { return generateInput(true, INSTRUCTION_MAX, randomRange(0, MAX_STACK_SIZE)); } + /* + * Given an instruction, adds the instruction count to the global array + * and adds probability to the other instructions + */ + private static void addCountProb(Instruction instruction) { + int index = instruction.ordinal(); + counts[index] += 1; + addProb[index] += instruction.getProbability(); + } + + /*** * Generates a list of different inputs for the program * @@ -165,20 +181,23 @@ public class Fuzzer { if (correct) { while (counter < numInstructions) { - Instruction newInstr = Instruction.getRandomInstruction(stackSize); + Instruction newInstr = Instruction.getRandomInstruction(stackSize, addProb); stackSize = stackSize + newInstr.getStackChange(); result.append(completeInstruction(true, newInstr)); + addCountProb(newInstr); counter += 1; } } else { while (counter < numInstructions) { Instruction newInstr; if (rand.nextInt(100) < LINE_ERROR_PERCENTAGE){ - newInstr = Instruction.getRandomInstruction(2); + newInstr = Instruction.getRandomInstruction(2, addProb); result.append(completeInstruction(false, newInstr)); + addCountProb(newInstr); } else { - newInstr = Instruction.getRandomInstruction(stackSize); + newInstr = Instruction.getRandomInstruction(stackSize, addProb); result.append(completeInstruction(true, newInstr)); + addCountProb(newInstr); } stackSize = stackSize + newInstr.getStackChange(); if (stackSize < 0){ diff --git a/fuzzer/Instruction.java b/fuzzer/Instruction.java index 197299adaa81bd7cb737afc0c463aab3e5ab745a..f4be61a027e6d723d66ab79510f9ddf43bf4d56d 100644 --- a/fuzzer/Instruction.java +++ b/fuzzer/Instruction.java @@ -82,9 +82,9 @@ public enum Instruction { // Returns a random instruction based on the stack size to prevent repeated // requests - public static Instruction getRandomInstruction(int max) { + public static Instruction getRandomInstruction(int max, int[] addProb) { // Check if cumlative probabilities have been calculated - checkProbability(); + checkProbability(addProb); ArrayList<Instruction> instructions; ArrayList<Integer> instCumlProbs; @@ -120,26 +120,26 @@ public enum Instruction { // Called before doing probability calculations to check if the clumulative // probabilities have been calculated - private static void checkProbability() { + private static void checkProbability(int[] addProb) { // Check if cumlative probabilities have been calculated. If not, do so. if (Instruction.ALL_MAX_CUML_PROB == 0) { Integer cumProb = 0; for (Instruction instruction : ALL_INSTRUCTIONS) { - cumProb += instruction.probability; + cumProb += instruction.probability + addProb[instruction.ordinal()]; ALL_CUML_PROB.add(cumProb); } ALL_MAX_CUML_PROB = cumProb; cumProb = 0; for (Instruction instruction : MAX_ZERO_INSTRUCTIONS) { - cumProb += instruction.probability; + cumProb += instruction.probability + addProb[instruction.ordinal()]; ZERO_CUML_PROB.add(cumProb); } ZERO_MAX_CUML_PROB = cumProb; cumProb = 0; for (Instruction instruction : MAX_ONE_INSTRUCTIONS) { - cumProb += instruction.probability; + cumProb += instruction.probability + addProb[instruction.ordinal()]; ONE_CUML_PROB.add(cumProb); } ONE_MAX_CUML_PROB = cumProb; @@ -169,4 +169,8 @@ public enum Instruction { } return "\"" + opcode + "\"" + operandsString; } + + public Integer getProbability() { + return this.probability; + } }