Skip to content
Snippets Groups Projects
Commit 2f086c36 authored by ehuang32's avatar ehuang32
Browse files

addDynamicProb

parent fa00d99c
No related branches found
No related tags found
No related merge requests found
......@@ -38,6 +38,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;
......@@ -65,8 +70,20 @@ public class Fuzzer {
}
}
}
/*
* 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
*
......@@ -112,20 +129,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){
......
......@@ -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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment