Skip to content
Snippets Groups Projects
Commit 71687166 authored by Ewen Smith's avatar Ewen Smith
Browse files

Merge branch 'master' of gitlab.eng.unimelb.edu.au:AARETHNA/swen90006-a2-2020

Merging new input
parents 08e608c6 5c50a02c
No related branches found
No related tags found
No related merge requests found
Pipeline #58346 passed
...@@ -40,6 +40,11 @@ public class Fuzzer { ...@@ -40,6 +40,11 @@ public class Fuzzer {
private static ArrayList<String> vars = new ArrayList<>(); 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 { public static void main(String[] args) throws IOException {
System.out.println(Instruction.getBNF()); System.out.println(Instruction.getBNF());
FileOutputStream out = null; FileOutputStream out = null;
...@@ -120,6 +125,17 @@ public class Fuzzer { ...@@ -120,6 +125,17 @@ public class Fuzzer {
return generateInput(true, INSTRUCTION_MAX, randomRange(0, MAX_STACK_SIZE)); 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 * Generates a list of different inputs for the program
* *
...@@ -165,20 +181,23 @@ public class Fuzzer { ...@@ -165,20 +181,23 @@ public class Fuzzer {
if (correct) { if (correct) {
while (counter < numInstructions) { while (counter < numInstructions) {
Instruction newInstr = Instruction.getRandomInstruction(stackSize); Instruction newInstr = Instruction.getRandomInstruction(stackSize, addProb);
stackSize = stackSize + newInstr.getStackChange(); stackSize = stackSize + newInstr.getStackChange();
result.append(completeInstruction(true, newInstr)); result.append(completeInstruction(true, newInstr));
addCountProb(newInstr);
counter += 1; counter += 1;
} }
} else { } else {
while (counter < numInstructions) { while (counter < numInstructions) {
Instruction newInstr; Instruction newInstr;
if (rand.nextInt(100) < LINE_ERROR_PERCENTAGE){ if (rand.nextInt(100) < LINE_ERROR_PERCENTAGE){
newInstr = Instruction.getRandomInstruction(2); newInstr = Instruction.getRandomInstruction(2, addProb);
result.append(completeInstruction(false, newInstr)); result.append(completeInstruction(false, newInstr));
addCountProb(newInstr);
} else { } else {
newInstr = Instruction.getRandomInstruction(stackSize); newInstr = Instruction.getRandomInstruction(stackSize, addProb);
result.append(completeInstruction(true, newInstr)); result.append(completeInstruction(true, newInstr));
addCountProb(newInstr);
} }
stackSize = stackSize + newInstr.getStackChange(); stackSize = stackSize + newInstr.getStackChange();
if (stackSize < 0){ if (stackSize < 0){
......
...@@ -82,9 +82,9 @@ public enum Instruction { ...@@ -82,9 +82,9 @@ public enum Instruction {
// Returns a random instruction based on the stack size to prevent repeated // Returns a random instruction based on the stack size to prevent repeated
// requests // requests
public static Instruction getRandomInstruction(int max) { public static Instruction getRandomInstruction(int max, int[] addProb) {
// Check if cumlative probabilities have been calculated // Check if cumlative probabilities have been calculated
checkProbability(); checkProbability(addProb);
ArrayList<Instruction> instructions; ArrayList<Instruction> instructions;
ArrayList<Integer> instCumlProbs; ArrayList<Integer> instCumlProbs;
...@@ -120,26 +120,26 @@ public enum Instruction { ...@@ -120,26 +120,26 @@ public enum Instruction {
// Called before doing probability calculations to check if the clumulative // Called before doing probability calculations to check if the clumulative
// probabilities have been calculated // 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. // Check if cumlative probabilities have been calculated. If not, do so.
if (Instruction.ALL_MAX_CUML_PROB == 0) { if (Instruction.ALL_MAX_CUML_PROB == 0) {
Integer cumProb = 0; Integer cumProb = 0;
for (Instruction instruction : ALL_INSTRUCTIONS) { for (Instruction instruction : ALL_INSTRUCTIONS) {
cumProb += instruction.probability; cumProb += instruction.probability + addProb[instruction.ordinal()];
ALL_CUML_PROB.add(cumProb); ALL_CUML_PROB.add(cumProb);
} }
ALL_MAX_CUML_PROB = cumProb; ALL_MAX_CUML_PROB = cumProb;
cumProb = 0; cumProb = 0;
for (Instruction instruction : MAX_ZERO_INSTRUCTIONS) { for (Instruction instruction : MAX_ZERO_INSTRUCTIONS) {
cumProb += instruction.probability; cumProb += instruction.probability + addProb[instruction.ordinal()];
ZERO_CUML_PROB.add(cumProb); ZERO_CUML_PROB.add(cumProb);
} }
ZERO_MAX_CUML_PROB = cumProb; ZERO_MAX_CUML_PROB = cumProb;
cumProb = 0; cumProb = 0;
for (Instruction instruction : MAX_ONE_INSTRUCTIONS) { for (Instruction instruction : MAX_ONE_INSTRUCTIONS) {
cumProb += instruction.probability; cumProb += instruction.probability + addProb[instruction.ordinal()];
ONE_CUML_PROB.add(cumProb); ONE_CUML_PROB.add(cumProb);
} }
ONE_MAX_CUML_PROB = cumProb; ONE_MAX_CUML_PROB = cumProb;
...@@ -169,4 +169,8 @@ public enum Instruction { ...@@ -169,4 +169,8 @@ public enum Instruction {
} }
return "\"" + opcode + "\"" + operandsString; return "\"" + opcode + "\"" + operandsString;
} }
public Integer getProbability() {
return this.probability;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment