Skip to content

Commit

Permalink
add script to generate wrapper automatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed Jul 27, 2024
1 parent 4870923 commit b90face
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 78 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
- Run the following commands to build Fray

```bash
./gradlew build -x test
./gradlew jlink
./gradlew shadowJar
./gradlew build
```

## Run Fray

### Push-button Testing




### Application Configuration

Expand Down
53 changes: 40 additions & 13 deletions bin/sfuzz
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
#!/bin/python3
#!/usr/bin/python3

import os
import sys
import subprocess
import argparse

BASE = os.path.dirname(os.path.realpath('__file__'))
JAVA_PATH = os.path.join(BASE, "jdk", "build", "java-inst", "bin", "java")
JVMTI_PATH = os.path.join(BASE, "jvmti", "build", "cmake", "native_release", "linux-amd64", "cpp", "libjvmti.so")
AGENT_PATH = os.path.join(BASE, "instrumentation", "build", "libs", "instrumentation-1.0-SNAPSHOT-all.jar")
CORE_PATH = os.path.join(BASE, "core", "build", "libs", "core-1.0-SNAPSHOT-all.jar")
JVMTI_PATH = "/Users/aoli/repos/sfuzz/jvmti/build/cmake/native_release/mac-aarch64/cpp/libjvmti.dylib"
AGENT_PATH = "/Users/aoli/repos/sfuzz/instrumentation/build/libs/instrumentation-1.0-SNAPSHOT-all.jar"
CORE_PATH = "/Users/aoli/repos/sfuzz/core/build/libs/core-1.0-SNAPSHOT-all.jar"

command = [
JAVA_PATH,
"-cp", CORE_PATH + ":" + sys.argv[1],
"-agentpath:" + JVMTI_PATH,
"-javaagent:" + AGENT_PATH,
"cmu.pasta.fray.core.MainKt",
*sys.argv[2:]
]
def main():
parser = argparse.ArgumentParser(description="Fray Fuzzer")
parser.add_argument("-cp", "--class-path",
help="class search path of directories and zip/jar files. A : separated list of directories, JAR archives, and ZIP archives to search for class files.",
default="")
parser.add_argument("clazz", help="Main class to run")
parser.add_argument("args", nargs=argparse.REMAINDER,
help="Arguments to pass to the target")
args = parser.parse_args()

subprocess.run(command)
class_path = CORE_PATH
if args.class_path:
class_path = class_path + ":" + args.class_path
command = [
JAVA_PATH,
"-cp", class_path,
"-agentpath:" + JVMTI_PATH,
"-javaagent:" + AGENT_PATH,
"cmu.pasta.fray.core.MainKt",
"--run-config",
"cli",
"--clazz",
args.clazz,
"--method",
"main",
]
if args.args:
command.extend([
"--args",
":".join(args.args)
])
subprocess.run(command)


if __name__ == "__main__":
main()
9 changes: 7 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.regex.Pattern

plugins {
kotlin("jvm") version "1.9.22"
id("com.ncorti.ktfmt.gradle") version "0.17.0"
Expand Down Expand Up @@ -25,13 +27,16 @@ kotlin {
}

val jvmti = project(":jvmti")
extra["agentPath"] = if (System.getProperty("os.name").toLowerCase().contains("mac")) {
extra["agentPath"] = if (System.getProperty("os.name").lowercase().contains("mac")) {
"${jvmti.layout.buildDirectory.get().asFile}/cmake/native_release/mac-aarch64/cpp/lib${jvmti.name}.dylib"
} else {
"${jvmti.layout.buildDirectory.get().asFile}/cmake/native_release/linux-amd64/cpp/lib${jvmti.name}.so"
}


configure(allprojects - project(":jvmti")) {
plugins.apply("com.ncorti.ktfmt.gradle")
}




62 changes: 62 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import java.util.regex.Pattern

plugins {
kotlin("jvm")
Expand Down Expand Up @@ -29,6 +30,67 @@ tasks.named<ShadowJar>("shadowJar") {
}
}


tasks.named("build") {
dependsOn("shadowJar")
}

tasks.withType<JavaExec> {
dependsOn(":jvmti:build")
dependsOn(":jdk:build")
val instrumentationTask = evaluationDependsOn(":instrumentation").tasks.named("shadowJar").get()
val agentPath: String by rootProject.extra
val jdk = project(":jdk")
val instrumentation = instrumentationTask.outputs.files.first().absolutePath
classpath = tasks.named("shadowJar").get().outputs.files
executable("${jdk.layout.buildDirectory.get().asFile}/java-inst/bin/java")
mainClass = "cmu.pasta.fray.core.MainKt"
jvmArgs("-agentpath:$agentPath")
jvmArgs("-javaagent:$instrumentation")
jvmArgs("-ea")
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/java.util.concurrent.atomic=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/java.util=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/java.io=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/sun.nio.ch=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/java.lang.reflect=ALL-UNNAMED")
doFirst {
// Printing the full command
println("Executing command: ${executable} ${jvmArgs!!.joinToString(" ")} -cp ${classpath.asPath} ${mainClass.get()} ${args!!.joinToString(" ")}")
}
}

tasks.register<JavaExec>("runFray") {
var configPath = properties["configPath"] as String? ?: ""
val extraArgs = when (val extraArgs = properties["extraArgs"]) {
is String -> {
val pattern = Pattern.compile("""("[^"]+"|\S+)""")
val matcher = pattern.matcher(extraArgs)
val result = mutableListOf<String>()
while (matcher.find()) {
result.add(matcher.group(1).replace("\"", ""))
}
result
}
else -> emptyList()
}
if (!File(configPath).isAbsolute) {
configPath = System.getProperty("user.dir") + "/" + configPath
}
args = listOf("--run-config", "json", "--config-path", configPath) + extraArgs
}

tasks.create("genRunner") {
val instrumentationTask = evaluationDependsOn(":instrumentation").tasks.named("shadowJar").get()
val instrumentation = instrumentationTask.outputs.files.first().absolutePath
val core = tasks.named("shadowJar").get().outputs.files.first().absolutePath
val agentPath: String by rootProject.extra
val binDir = "${rootProject.projectDir.absolutePath}/bin"
var runner = file("${binDir}/sfuzz.template").readText()
runner = runner.replace("#JVM_TI_PATH#", agentPath)
runner = runner.replace("#AGENT_PATH#", instrumentation)
runner = runner.replace("#CORE_PATH#", core)
val file = File("${binDir}/sfuzz")
file.writeText(runner)
file.setExecutable(true)
}
7 changes: 3 additions & 4 deletions core/src/main/kotlin/cmu/pasta/fray/core/TestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ class TestRunner(val config: Configuration) {
config.executionInfo.executor.execute()
} else {
setup()
val outputFile = Paths.get(config.report, "output.txt").toFile()
val timeSource = TimeSource.Monotonic
val start = timeSource.markNow()
var i = 0
while (i != config.iter) {
outputFile.appendText("Starting iteration $i\n")
println("Starting iteration $i")
try {
Runtime.DELEGATE = RuntimeDelegate()
Runtime.start()
Expand All @@ -49,8 +48,8 @@ class TestRunner(val config: Configuration) {
Runtime.onMainExit()
}
if (GlobalContext.bugFound) {
outputFile.appendText(
"Error found at iter: $i, Elapsed time: ${(timeSource.markNow() - start).inWholeMilliseconds}ms\n")
println(
"Error found at iter: $i, Elapsed time: ${(timeSource.markNow() - start).inWholeMilliseconds}ms")
if (!config.exploreMode) {
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class PCT : ScheduleAlgorithm("pct") {

class MainCommand : CliktCommand() {
val report by option("-o", "--output", help = "Report output directory.").default("/tmp/report")
val iter by option("-i", "--iter", help = "Number of iterations.").int().default(1)
val iter by option("-i", "--iter", help = "Number of iterations.").int().default(1000)
val fullSchedule by
option(
"-f",
Expand All @@ -153,6 +153,7 @@ class MainCommand : CliktCommand() {
val logger by
option("-l", "--logger", help = "Logger type.")
.groupChoice("json" to JsonLoggerOption(), "csv" to CsvLoggerOption())
.defaultByName("json")
val scheduler by
option(help = "Scheduling algorithm.")
.groupChoice(
Expand All @@ -161,6 +162,7 @@ class MainCommand : CliktCommand() {
"pos" to POS(),
"random" to Rand(),
"pct" to PCT())
.defaultByName("random")
val noFray by option("--no-fray", help = "Runnning in no-Fray mode.").flag()
val exploreMode by
option(
Expand All @@ -171,7 +173,7 @@ class MainCommand : CliktCommand() {
option("--no-exit-on-bug", help = "Fray will not immediately exit when a failure is found.")
.flag()
val runConfig by
option(help = "Run configuration for the application.")
option("--run-config", help = "Run configuration for the application.")
.groupChoice(
"cli" to CliExecutionConfig(),
"json" to JsonExecutionConfig(),
Expand Down
1 change: 0 additions & 1 deletion instrumentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ tasks.test {
tasks.named("build") {
dependsOn("shadowJar")
}

4 changes: 2 additions & 2 deletions integration-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ tasks.test {
useJUnitPlatform()
val agentPath: String by rootProject.extra
val jdk = project(":jdk")
val instrumentation = project(":instrumentation")
val instrumentation = project(":instrumentation").tasks.named("shadowJar").get().outputs.files.first().absolutePath
executable("${jdk.layout.buildDirectory.get().asFile}/java-inst/bin/java")
jvmArgs("-agentpath:$agentPath")
jvmArgs("-javaagent:${instrumentation.layout.buildDirectory.get().asFile}/libs/${instrumentation.name}-${instrumentation.version}-all.jar")
jvmArgs("-javaagent:$instrumentation")
dependsOn(":jdk:build")
dependsOn(":jvmti:build")
}
Expand Down
2 changes: 2 additions & 0 deletions integration-tests/src/main/java/example/FrayExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public void run() {
b = x;
}
public static void main(String[] args) throws Exception {
a = new AtomicInteger();
b = 0;
FrayExample[] threads = {new FrayExample(), new FrayExample()};
for (var thread : threads) thread.start();
for (var thread : threads) thread.join();
Expand Down
55 changes: 10 additions & 45 deletions junit-runner/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import java.util.regex.Pattern
plugins {
id("java")
kotlin("jvm")
id("com.github.johnrengelman.shadow") version "8.1.1"
}

repositories {
Expand All @@ -11,61 +14,23 @@ repositories {
dependencies {
implementation("org.junit.vintage:junit-vintage-engine:5.10.2")
implementation("org.junit.platform:junit-platform-launcher:1.10.3")
implementation(project(":core"))
}

tasks.test {
useJUnitPlatform()
}

tasks.withType<JavaExec> {
val agentPath: String by rootProject.extra
val jdk = project(":jdk")
val instrumentation = project(":instrumentation")
classpath = sourceSets["main"].runtimeClasspath
executable("${jdk.layout.buildDirectory.get().asFile}/java-inst/bin/java")
mainClass = "cmu.pasta.fray.core.MainKt"
jvmArgs("-agentpath:$agentPath")
jvmArgs("-javaagent:${instrumentation.layout.buildDirectory.get().asFile}/libs/${instrumentation.name}-${instrumentation.version}-all.jar")
jvmArgs("-ea")
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/java.util.concurrent.atomic=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/java.util=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/java.io=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/sun.nio.ch=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/java.lang.reflect=ALL-UNNAMED")
doFirst {
// Printing the full command
println("Executing command: ${executable} ${jvmArgs!!.joinToString(" ")} -cp ${classpath.asPath} ${mainClass.get()} ${args!!.joinToString(" ")}")
}
tasks.compileJava {
options.compilerArgs.addAll(listOf("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED"))
}

tasks.register<JavaExec>("runFray") {
var configPath = properties["configPath"] as String? ?: ""
val extraArgs = when (val extraArgs = properties["extraArgs"]) {
is String -> {
val pattern = Pattern.compile("""("[^"]+"|\S+)""")
val matcher = pattern.matcher(extraArgs)
val result = mutableListOf<String>()
while (matcher.find()) {
result.add(matcher.group(1).replace("\"", ""))
}
result
}
else -> emptyList()
}
if (!File(configPath).isAbsolute) {
configPath = System.getProperty("user.dir") + "/" + configPath
tasks.named<ShadowJar>("shadowJar") {
manifest {
attributes(mapOf("Main-Class" to "cmu.pasta.fray.core.MainKt"))
}
args = listOf("--run-config", "json", "--config-path", configPath) + extraArgs
}

tasks.compileJava {
options.compilerArgs.addAll(listOf("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED"))
}

tasks.register<Copy>("copyDependencies") {
from(configurations.runtimeClasspath)
into("${layout.buildDirectory.get().asFile}/dependency")
tasks.named("build") {
dependsOn("shadowJar")
}

7 changes: 1 addition & 6 deletions jvmti/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ plugins {

cmake {
targets {
register("native_release") {
cmakeLists.set(file("src/CMakeLists.txt"))
register("native_release") { cmakeLists.set(file("src/CMakeLists.txt"))
cmakeArgs.add("-DCMAKE_BUILD_TYPE=Release")
}
register("native_debug") {
cmakeLists.set(file("src/CMakeLists.txt"))
cmakeArgs.add("-DCMAKE_BUILD_TYPE=Debug")
}
}
}

Expand Down

0 comments on commit b90face

Please sign in to comment.