Skip to content
Snippets Groups Projects
Commit 5c50a02c authored by ehuang32's avatar ehuang32
Browse files
parents 2f086c36 9ea667df
Branches
No related tags found
No related merge requests found
Pipeline #58340 passed
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/* a stub for your team's fuzzer */
public class Fuzzer {
// Blah - Test 3
private static final String OUTPUT_FILE = "fuzz.txt";
......@@ -22,7 +24,7 @@ public class Fuzzer {
private static final int NUMBER_TO_GENERATE = 10;
// Instruction number range for each input
private static final int INSTRUCTION_MIN = 0;
private static final int INSTRUCTION_MAX = 100;
private static final int INSTRUCTION_MAX = 1000;
// Maximum variable name length
private static final int VAR_NAME_LENGTH_MAX = 100;
private static final int VAR_ASCII_MIN = 33;
......@@ -47,6 +49,7 @@ public class Fuzzer {
System.out.println(Instruction.getBNF());
FileOutputStream out = null;
PrintWriter pw = null;
int runCount = getRun();
try {
out = new FileOutputStream(OUTPUT_FILE);
......@@ -55,8 +58,8 @@ public class Fuzzer {
/* We just print one instruction.
Hint: you might want to make use of the instruction
grammar which is effectively encoded in Instruction.java */
pw.print(getStaticTests());
pw.println(generateMultipleInputs());
// pw.print(getStaticTests());
pw.println(generateRunInputs(runCount));
}catch (Exception e){
e.printStackTrace(System.err);
......@@ -69,8 +72,51 @@ public class Fuzzer {
out.close();
}
}
writeRun(runCount);
}
private static int getRun(){
int runCount = 0;
try {
Scanner input = new Scanner(new File("runCount.txt"));
runCount = input.nextInt() + 1;
} catch (Exception e){
// Carry on
}
return runCount;
}
private static void writeRun(int runCount) throws IOException {
FileOutputStream out = null;
PrintWriter pw = null;
try {
out = new FileOutputStream("runCount.txt");
pw = new PrintWriter(out);
pw.print(runCount);
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(1);
}finally{
if (pw != null){
pw.flush();
}
if (out != null){
out.close();
}
}
}
private static String generateRunInputs(int runCount){
switch (runCount){
case 0:
return generateInput(false, INSTRUCTION_MAX, true);
case 1:
return generateInput(true, INSTRUCTION_MAX, true);
}
return generateInput(true, INSTRUCTION_MAX, false);
}
/*
......@@ -99,8 +145,10 @@ public class Fuzzer {
// Loop to create list of inputs
while (generated < NUMBER_TO_GENERATE) {
// Determine if the line will be correct or not
boolean stackFull = rand.nextInt(100) < STACK_FULL_PERCENTAGE;
result.append(generateInput(false,
INSTRUCTION_MAX));
INSTRUCTION_MAX, stackFull));
// Increment generated
generated += 1;
}
......@@ -114,13 +162,13 @@ public class Fuzzer {
* @param numInstructions for the line
* @return the concatenated input for the program as a string
*/
private static String generateInput(boolean correct, long numInstructions){
private static String generateInput(boolean correct, long numInstructions, boolean stackFull ){
int stackSize = 0;
int counter = 0;
StringBuilder result = new StringBuilder();
if (rand.nextInt(100) < STACK_FULL_PERCENTAGE) {
if (stackFull) {
for ( int i = 0 ; i < MAX_STACK_SIZE ; i++ ) {
result.append(completeInstruction(true, Instruction.PUSH));
stackSize++;
......@@ -196,9 +244,18 @@ public class Fuzzer {
}
break;
case STORE:
// If not correct, make up a name not in the list
if (correct || vars.size() == 0){
// Make up a name
name = name + generateName();
name = generateName();
vars.add(name);
}
// If no variables, return empty string
else {
name = vars.get(randomRange(0,vars.size()-1));
}
name = " " + name;
break;
case SAVE:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment