Skip to content

Commit

Permalink
Added debug runner
Browse files Browse the repository at this point in the history
  • Loading branch information
tochilinak committed Nov 1, 2023
1 parent 6bcb46b commit 14bfe7b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.usvm.runner

class DebugRunner(config: USVMPythonConfig): USVMPythonRunner(config) {
fun runProcessAndPrintInfo(runConfig: USVMPythonRunConfig) {
val builder = setupEnvironment(runConfig)
builder.redirectError(ProcessBuilder.Redirect.INHERIT)
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT)
val process = builder.start()
process.waitFor()
println("Exit status: ${process.exitValue()}")
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package org.usvm.runner

import mu.KLogging
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
import java.net.InetSocketAddress
import java.nio.channels.Channels
import java.nio.channels.ClosedChannelException
import java.nio.channels.InterruptibleChannel
import java.nio.channels.ServerSocketChannel
import java.nio.channels.SocketChannel
import java.util.concurrent.TimeUnit
Expand All @@ -20,14 +13,8 @@ interface PythonSymbolicAnalysisRunner: AutoCloseable {
}

class PythonSymbolicAnalysisRunnerImpl(
private val vacantPort: Int,
private val config: USVMPythonConfig
): PythonSymbolicAnalysisRunner {
private val serverSocketChannel = ServerSocketChannel.open()

init {
serverSocketChannel.socket().bind(InetSocketAddress("localhost", vacantPort))
}
config: USVMPythonConfig
): USVMPythonRunner(config), PythonSymbolicAnalysisRunner {

override fun analyze(
runConfig: USVMPythonRunConfig,
Expand All @@ -37,6 +24,7 @@ class PythonSymbolicAnalysisRunnerImpl(
val processBuilder = setupEnvironment(runConfig)
val client = ClientResources(serverSocketChannel, processBuilder)
client.use {
println("Here!")
// println(BufferedReader(InputStreamReader(client.process.errorStream)).readLines())
val channel = it.clientSocketChannel
if (channel == null) {
Expand Down Expand Up @@ -91,37 +79,6 @@ class PythonSymbolicAnalysisRunnerImpl(
}
}

private fun setupEnvironment(runConfig: USVMPythonRunConfig): ProcessBuilder {
val layout = config.distributionLayout
val functionConfig = when (runConfig.callableConfig) {
is USVMPythonFunctionConfig -> runConfig.callableConfig
}
val args = listOf(
config.javaCmd,
"-Xss50m",
"-Xmx2g",
"-Dapproximations.path=${layout.approximationsPath.canonicalPath}",
"-Djava.library.path=${layout.nativeLibPath.canonicalPath}",
"-jar",
layout.jarPath.canonicalPath,
config.mypyBuildDir,
vacantPort.toString(),
functionConfig.module,
functionConfig.name,
runConfig.timeoutPerRunMs.toString(),
runConfig.timeoutMs.toString()
) + config.roots.toList()

val processBuilder = ProcessBuilder(args)

val env = processBuilder.environment()
env["LD_LIBRARY_PATH"] = "${File(layout.cpythonPath, "lib").canonicalPath}:${layout.cpythonPath.canonicalPath}"
env["LD_PRELOAD"] = File(layout.cpythonPath, "lib/libpython3.so").canonicalPath
env["PYTHONHOME"] = layout.cpythonPath.canonicalPath

return processBuilder
}

override fun close() {
serverSocketChannel.close()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.usvm.runner

import java.io.File
import java.net.InetSocketAddress
import java.nio.channels.ServerSocketChannel

open class USVMPythonRunner(private val config: USVMPythonConfig): AutoCloseable {
protected val serverSocketChannel: ServerSocketChannel = ServerSocketChannel.open()
private val port: InetSocketAddress

init {
serverSocketChannel.socket().bind(InetSocketAddress(0))
port = serverSocketChannel.localAddress as? InetSocketAddress
?: error("Couldn't cast SocketAddress ${serverSocketChannel.localAddress} to InetSocketAddress")
println("Port: ${port.port}")
}

override fun close() {
serverSocketChannel.close()
}

protected fun setupEnvironment(runConfig: USVMPythonRunConfig): ProcessBuilder {
val layout = config.distributionLayout
val functionConfig = when (runConfig.callableConfig) {
is USVMPythonFunctionConfig -> runConfig.callableConfig
}
val args = listOf(
config.javaCmd,
"-Xss50m",
"-Xmx2g",
"-Dapproximations.path=${layout.approximationsPath.canonicalPath}",
"-Djava.library.path=${layout.nativeLibPath.canonicalPath}",
"-jar",
layout.jarPath.canonicalPath,
config.mypyBuildDir,
port.port.toString(),
functionConfig.module,
functionConfig.name,
runConfig.timeoutPerRunMs.toString(),
runConfig.timeoutMs.toString()
) + config.roots.toList()

val processBuilder = ProcessBuilder(args)

val env = processBuilder.environment()
env["LD_LIBRARY_PATH"] = "${File(layout.cpythonPath, "lib").canonicalPath}:${layout.cpythonPath.canonicalPath}"
env["LD_PRELOAD"] = File(layout.cpythonPath, "lib/libpython3.so").canonicalPath
env["PYTHONHOME"] = layout.cpythonPath.canonicalPath

return processBuilder
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ fun main() {
20_000,
3_000
)
val receiver = PrintingResultReceiver()
val runner = PythonSymbolicAnalysisRunnerImpl(8888, config)
// val receiver = PrintingResultReceiver()
val debugRunner = DebugRunner(config)
debugRunner.use {
it.runProcessAndPrintInfo(runConfig)
}
/*val runner = PythonSymbolicAnalysisRunnerImpl(config)
runner.use {
val start = System.currentTimeMillis()
it.analyze(runConfig, receiver) { System.currentTimeMillis() - start >= 5_000 }
}
}*/
}

0 comments on commit 14bfe7b

Please sign in to comment.