Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swen90006-a2-2020
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Arman Arethna
swen90006-a2-2020
Commits
ac72a6ad
Commit
ac72a6ad
authored
Oct 23, 2020
by
ehuang32
Browse files
Options
Downloads
Patches
Plain Diff
dynamic prob w/ map
parent
b6103849
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
fuzzer/Fuzzer.java
+50
-10
50 additions, 10 deletions
fuzzer/Fuzzer.java
fuzzer/Instruction.java
+16
-6
16 additions, 6 deletions
fuzzer/Instruction.java
with
66 additions
and
16 deletions
fuzzer/Fuzzer.java
+
50
−
10
View file @
ac72a6ad
...
...
@@ -4,6 +4,9 @@ import java.io.FileOutputStream;
import
java.io.PrintWriter
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Random
;
import
java.util.Scanner
;
...
...
@@ -44,6 +47,12 @@ public class Fuzzer {
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
};
// Pathway Prob Map, E.g. key = [PUSH, POP, STORE] - value = 2, this value is added to base prob
private
static
HashMap
<
List
<
Instruction
>,
Integer
>
pathwayProb
=
new
HashMap
<
List
<
Instruction
>,
Integer
>();
// Current stack of instructions
private
static
ArrayList
<
Instruction
>
instructionStack
=
new
ArrayList
<
Instruction
>();
// Max stack of instructions before resetting stack
private
static
final
int
MAX_STACK
=
2
;
public
static
void
main
(
String
[]
args
)
throws
IOException
{
System
.
out
.
println
(
Instruction
.
getBNF
());
...
...
@@ -113,13 +122,13 @@ public class Fuzzer {
switch
(
runCount
){
case
0
:
// Test with stack full
return
generateInput
(
true
,
INSTRUCTION_MAX
,
MAX_STACK_SIZE
,
fals
e
,
false
);
return
generateInput
(
true
,
INSTRUCTION_MAX
,
MAX_STACK_SIZE
,
tru
e
,
false
);
case
1
:
// Test with stack full
return
generateInput
(
true
,
INSTRUCTION_MAX
,
MAX_STACK_SIZE
-
1
,
fals
e
,
false
);
return
generateInput
(
true
,
INSTRUCTION_MAX
,
MAX_STACK_SIZE
-
1
,
tru
e
,
false
);
case
3
:
// Run static tests and empty stack
return
getStaticTests
()
+
generateInput
(
true
,
INSTRUCTION_MAX
,
0
,
fals
e
,
false
);
return
getStaticTests
()
+
generateInput
(
true
,
INSTRUCTION_MAX
,
0
,
tru
e
,
false
);
case
4
:
// Test with dynamic probability
return
generateInput
(
true
,
INSTRUCTION_MAX
,
0
,
true
,
false
);
...
...
@@ -128,7 +137,7 @@ public class Fuzzer {
return
generateInput
(
true
,
INSTRUCTION_MAX
,
0
,
true
,
true
);
}
// Run from random stack
return
generateInput
(
true
,
INSTRUCTION_MAX
,
0
,
fals
e
,
false
);
return
generateInput
(
true
,
INSTRUCTION_MAX
,
0
,
tru
e
,
false
);
}
/*
...
...
@@ -167,6 +176,37 @@ public class Fuzzer {
return
result
.
toString
();
}
private
static
void
incrementStackAndCount
(
Instruction
newInstruction
)
{
// Instruction values
Instruction
allInstructions
[]
=
Instruction
.
values
();
// Add base prob to stack with every other instruction
for
(
Instruction
inst:
allInstructions
)
{
if
(!
inst
.
equals
(
newInstruction
))
{
List
<
Instruction
>
tempStack
=
new
ArrayList
<
Instruction
>(
instructionStack
);
tempStack
.
add
(
inst
);
if
(
pathwayProb
.
containsKey
(
tempStack
))
{
int
oldProb
=
pathwayProb
.
get
(
tempStack
);
pathwayProb
.
replace
(
tempStack
,
oldProb
+
inst
.
getProbability
());
}
else
{
pathwayProb
.
put
(
tempStack
,
inst
.
getProbability
());
}
}
}
// Add instruction to stack
instructionStack
.
add
(
newInstruction
);
// Check if size is bigger than max stack, then reset stack
if
(
instructionStack
.
size
()
>=
MAX_STACK
)
{
instructionStack
.
clear
();
}
System
.
out
.
println
(
"stack: "
+
Arrays
.
toString
(
instructionStack
.
toArray
()));
for
(
List
<
Instruction
>
list:
pathwayProb
.
keySet
())
{
String
key
=
list
.
toString
();
String
value
=
pathwayProb
.
get
(
list
).
toString
();
System
.
out
.
println
(
"map: "
+
key
+
" "
+
value
);
}
}
/***
* Generates a line of input for the program
*
...
...
@@ -187,11 +227,11 @@ public class Fuzzer {
if
(
correct
)
{
while
(
counter
<
numInstructions
)
{
Instruction
newInstr
=
Instruction
.
getRandomInstruction
(
stackSize
,
add
Prob
);
Instruction
newInstr
=
Instruction
.
getRandomInstruction
(
stackSize
,
instructionStack
,
pathway
Prob
);
stackSize
=
stackSize
+
newInstr
.
getStackChange
();
result
.
append
(
completeInstruction
(
true
,
newInstr
,
false
));
if
(
dynamicProb
)
{
ad
dCount
Prob
(
newInstr
);
incrementStackAn
dCount
(
newInstr
);
}
counter
+=
1
;
}
...
...
@@ -199,16 +239,16 @@ public class Fuzzer {
while
(
counter
<
numInstructions
)
{
Instruction
newInstr
;
if
(
rand
.
nextInt
(
100
)
<
LINE_ERROR_PERCENTAGE
){
newInstr
=
Instruction
.
getRandomInstruction
(
2
,
add
Prob
);
newInstr
=
Instruction
.
getRandomInstruction
(
2
,
instructionStack
,
pathway
Prob
);
result
.
append
(
completeInstruction
(
false
,
newInstr
,
longVarNames
));
if
(
dynamicProb
)
{
ad
dCount
Prob
(
newInstr
);
incrementStackAn
dCount
(
newInstr
);
}
}
else
{
newInstr
=
Instruction
.
getRandomInstruction
(
stackSize
,
add
Prob
);
newInstr
=
Instruction
.
getRandomInstruction
(
stackSize
,
instructionStack
,
pathway
Prob
);
result
.
append
(
completeInstruction
(
true
,
newInstr
,
false
));
if
(
dynamicProb
)
{
ad
dCount
Prob
(
newInstr
);
incrementStackAn
dCount
(
newInstr
);
}
}
stackSize
=
stackSize
+
newInstr
.
getStackChange
();
...
...
This diff is collapsed.
Click to expand it.
fuzzer/Instruction.java
+
16
−
6
View file @
ac72a6ad
...
...
@@ -82,9 +82,10 @@ public enum Instruction {
// Returns a random instruction based on the stack size to prevent repeated
// requests
public
static
Instruction
getRandomInstruction
(
int
max
,
int
[]
addProb
)
{
public
static
Instruction
getRandomInstruction
(
int
max
,
List
<
Instruction
>
instructionStack
,
Map
<
List
<
Instruction
>,
Integer
>
pathwayProb
)
{
// Check if cumlative probabilities have been calculated
checkProbability
(
add
Prob
);
checkProbability
(
instructionStack
,
pathway
Prob
);
ArrayList
<
Instruction
>
instructions
;
ArrayList
<
Integer
>
instCumlProbs
;
...
...
@@ -120,26 +121,35 @@ public enum Instruction {
// Called before doing probability calculations to check if the clumulative
// probabilities have been calculated
private
static
void
checkProbability
(
int
[]
addProb
)
{
private
static
void
checkProbability
(
List
<
Instruction
>
instructionStack
,
Map
<
List
<
Instruction
>,
Integer
>
pathwayProb
)
{
// 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
+
addProb
[
instruction
.
ordinal
()];
// Temporary stack where we add this loop's instruction
List
<
Instruction
>
tempStack
=
new
ArrayList
<
Instruction
>(
instructionStack
);
tempStack
.
add
(
instruction
);
// If the probability has increased, find it and add to base prob, otherwise 0
int
probToAdd
=
0
;
if
(
pathwayProb
.
containsKey
(
tempStack
))
{
probToAdd
=
pathwayProb
.
get
(
tempStack
);
}
cumProb
+=
instruction
.
probability
+
probToAdd
;
ALL_CUML_PROB
.
add
(
cumProb
);
}
ALL_MAX_CUML_PROB
=
cumProb
;
cumProb
=
0
;
for
(
Instruction
instruction
:
MAX_ZERO_INSTRUCTIONS
)
{
cumProb
+=
instruction
.
probability
+
addProb
[
instruction
.
ordinal
()]
;
cumProb
+=
instruction
.
probability
;
ZERO_CUML_PROB
.
add
(
cumProb
);
}
ZERO_MAX_CUML_PROB
=
cumProb
;
cumProb
=
0
;
for
(
Instruction
instruction
:
MAX_ONE_INSTRUCTIONS
)
{
cumProb
+=
instruction
.
probability
+
addProb
[
instruction
.
ordinal
()]
;
cumProb
+=
instruction
.
probability
;
ONE_CUML_PROB
.
add
(
cumProb
);
}
ONE_MAX_CUML_PROB
=
cumProb
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment