diff --git a/fuzzer/Fuzzer.java b/fuzzer/Fuzzer.java index 8b4cacafbe9028fe237d1aa549803f32c470353a..88e5fd31d4d6d934f1fc5f5b3688ab35dca862ec 100644 --- a/fuzzer/Fuzzer.java +++ b/fuzzer/Fuzzer.java @@ -13,7 +13,7 @@ public class Fuzzer { private static final String OUTPUT_FILE = "fuzz.txt"; // The percentage of outputs that will start with a full stack - private static final int STACK_FULL_PERCENTAGE = 20; + private static final int STACK_FULL_PERCENTAGE = 10; // The percentage of inputs that will be incorrect private static final int INPUT_ERROR_PERCENTAGE = 5; // The percentage of instructions that will be incorrect @@ -28,7 +28,7 @@ public class Fuzzer { private static final int VAR_ASCII_MIN = 33; private static final int VAR_ASCII_MAX = 127; // Variable range - private static final int VAR_MIN = -2147483647; + private static final int VAR_MIN = -2147483648; private static final int VAR_MAX = 2147483647; // Maximum number of items in stack private static final int MAX_STACK_SIZE = 512; @@ -50,6 +50,7 @@ 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(generateInput(false, INSTRUCTION_MAX)); }catch (Exception e){ @@ -226,5 +227,95 @@ public class Fuzzer { return min + rand.nextInt(max - min); } + /*** + * Get the desired static boundary tests + * + * @return a string containing all of the boundary tests' instructions + */ + private static String getStaticTests() { + // Note. Some operations will crash dc if performed, so they cannot be tested + // Examples include *, /, -, +, pop, store operations on empty stack + + StringBuilder result = new StringBuilder(); + + // Test opreations that need variables on no variables defined + result.append(Instruction.LIST.getOpcode() + "\n"); + result.append(Instruction.LOAD.getOpcode() + " VAR1\n"); + result.append(Instruction.REM.getOpcode() + " VAR1\n"); + + // Test operations that use stack on empty stack + result.append(Instruction.PRINT.getOpcode() + "\n"); + + // Prep for next tests + result.append(Instruction.PUSH.getOpcode() + " 5\n"); + result.append(Instruction.STORE.getOpcode() + " VAR1\n"); + result.append(Instruction.PUSH.getOpcode() + " 10\n"); + result.append(Instruction.STORE.getOpcode() + " VAR2\n"); + result.append(Instruction.SAVE.getOpcode() + "\n"); + + // Test incorrect arguements for operation + result.append(Instruction.LOAD.getOpcode() + " VAR3\n"); + + // Test Variables stored and used corectly + result.append(Instruction.LIST.getOpcode() + "\n"); + result.append(Instruction.PRINT.getOpcode() + "\n"); + result.append(Instruction.LOAD.getOpcode() + " VAR2\n"); + result.append(Instruction.LOAD.getOpcode() + " VAR1\n"); + result.append(Instruction.PRINT.getOpcode() + "\n"); + result.append(Instruction.POP.getOpcode() + "\n"); + result.append(Instruction.PRINT.getOpcode() + "\n"); + result.append(Instruction.REM.getOpcode() + " VAR2\n"); + result.append(Instruction.LIST.getOpcode() + "\n"); + result.append(Instruction.LOAD.getOpcode() + " VAR2\n"); + result.append(Instruction.PRINT.getOpcode() + "\n"); + result.append(Instruction.STORE.getOpcode() + " VAR1\n"); + result.append(Instruction.LIST.getOpcode() + "\n"); + result.append(Instruction.LOAD.getOpcode() + " VAR1\n"); + result.append(Instruction.STORE.getOpcode() + " VAR1\n"); + result.append(Instruction.LIST.getOpcode() + "\n"); + result.append(Instruction.REM.getOpcode() + " VAR1\n"); + + // Test Plus operation + result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n"); + result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MIN + "\n"); + result.append(Instruction.PLUS.getOpcode() + "\n"); + result.append(Instruction.POP.getOpcode() + "\n"); + result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n"); + result.append(Instruction.PUSH.getOpcode() + " 1\n"); + result.append(Instruction.PLUS.getOpcode() + "\n"); + result.append(Instruction.POP.getOpcode() + "\n"); + + // Test Subtract operation + result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n"); + result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n"); + result.append(Instruction.SUB.getOpcode() + "\n"); + result.append(Instruction.POP.getOpcode() + "\n"); + result.append(Instruction.PUSH.getOpcode() + " 1\n"); + result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MIN + "\n"); + result.append(Instruction.SUB.getOpcode() + "\n"); + result.append(Instruction.POP.getOpcode() + "\n"); + + // Test Divide operation + result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n"); + result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n"); + result.append(Instruction.DIV.getOpcode() + "\n"); + result.append(Instruction.POP.getOpcode() + "\n"); + result.append(Instruction.PUSH.getOpcode() + " 1\n"); + result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MIN + "\n"); + result.append(Instruction.DIV.getOpcode() + "\n"); + result.append(Instruction.POP.getOpcode() + "\n"); + + // Test Multiply operation + result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n"); + result.append(Instruction.PUSH.getOpcode() + " 2\n"); + result.append(Instruction.MULT.getOpcode() + "\n"); + result.append(Instruction.POP.getOpcode() + "\n"); + result.append(Instruction.PUSH.getOpcode() + " 2\n"); + result.append(Instruction.PUSH.getOpcode() + " "+ Math.floorDiv(VAR_MIN, 2) + "\n"); + result.append(Instruction.MULT.getOpcode() + "\n"); + result.append(Instruction.POP.getOpcode() + "\n"); + + return result.toString(); + } } diff --git a/fuzzer/Instruction.java b/fuzzer/Instruction.java index c9b509ee9e00c59dd80cc5ce3e74e7040f4b5b64..197299adaa81bd7cb737afc0c463aab3e5ab745a 100644 --- a/fuzzer/Instruction.java +++ b/fuzzer/Instruction.java @@ -15,7 +15,7 @@ public enum Instruction { LOAD("load",new OperandType[]{OperandType.STRING}, 0, 1, 1), //Note. Check variable existence REM("remove",new OperandType[]{OperandType.STRING}, 0, 0, 1), //Note. Check variable existence STORE("store",new OperandType[]{OperandType.STRING}, 1, -1, 1), - SAVE("save",new OperandType[]{OperandType.STRING}, 1, 0, 1), + SAVE("save",new OperandType[]{OperandType.STRING}, 0, 0, 1), LIST("list",new OperandType[]{}, 0, 0, 1), PRINT("print",new OperandType[]{}, 0, 0, 1);