This document provides a guide to running the basic examples with SWAT. The examples in this directory serve as the first introduction to using SWAT's capabilities.
The basic-1 directory contains:
Example1.java: A simple Java class demonstrating symbolic execution with integersExample2.java: A Java class demonstrating symbolic execution with stringsbuild.sh: Script to compile the examplesrun.sh: Script to execute the examples using SWATREADME.md: Contains an example command
This example demonstrates basic symbolic execution with integers:
- It uses a
@Symbolicannotation on an integer variable - Contains a simple conditional statement that creates two distinct execution paths
- Returns different strings based on the condition (path 1 or path 2)
This example demonstrates symbolic execution with strings:
- It uses a
@Symbolicannotation on a string variable - Checks if the string equals "Hello world"
- Returns different strings based on the condition (path 1 or path 2)
The build.sh script compiles the Java examples with the SWAT annotations:
./build.sh [target_file]Parameters:
target_file: The Java file to compile (default:Example1.java) As this example uses@Symbolicannotations provided by SWAT, theannotationsJAR file is required on the classpath to compile the examples.
Actions:
- Compiles the specified Java file with the SWAT annotations JAR in the classpath
- The annotations JAR is located at
../../../annotations/build/libs/annotations-1.0.jarIn this example the class is compiled into the directory, i.e.Example1.class. For more complex examples such as gradle built jars see the next examples.
The run.sh script executes the compiled examples using SWAT:
./run.sh [target] [mode]Parameters:
- target: The Java class to run (default: Example1)
- mode: Execution mode (options: local or explorer, default: local)
- Local Mode (default):
- Directly executes the program with the symbolic executor as a Java agent
java \
-Djava.library.path=../../../libs/java-library-path \
-javaagent:../../../symbolic-executor/lib/symbolic-executor.jar \
[target]- Explorer Mode:
- Runs the program using the symbolic explorer Python script
- Command used:
When running Example1:
- SWAT will symbolically execute the code, exploring both paths
- It will identify that the condition
i == 42creates two execution paths - Path 1: When
i == 42, returns"Path 1" - Path 2: When
i != 42, returns"Path 2" - The concolic engine will systematically explore both paths and generate test cases for each
When running Example2:
- SWAT will symbolically execute the code using a symbolic string
- It will identify that the condition
s.equals("Hello world")creates two execution paths - Path 1: When
sequals"Hello world", returns"Path 1" - Path 2: When
sdoes not equal"Hello world", returns"Path 2" - The engine will generate appropriate string inputs to cover both paths
Similarly to the explorer, you can also directly control assignments of symbolic values using system properties:
java \
-Djava.library.path=../../../libs/java-library-path \
-Dswat.assignment.java/lang/String_0="Hello world" \
-javaagent:../../../symbolic-executor/lib/symbolic-executor.jar \
Example1The -Dswat.assignment.java/lang/String_0="Hello world" property assigns the value "Hello world" to the first symbolic String in the program.
- Ensure all paths to JARs and libraries are correct
- The Z3 solver must be in the java.library.path
- If you encounter errors with the symbolic executor, check that the annotations JAR is correctly built
- Different symbolic annotations can be applied to various data types
- Multiple symbolic variables can be used in the same program
- The explorer mode offers more advanced analysis capabilities for complex programs
This documentation provides the essential information to get started with SWAT using these basic examples. As you become more familiar with the system, you can explore more complex examples and applications in the other directories.