diff --git a/usvm-python/usvm-python-runner/src/main/kotlin/org/usvm/runner/DebugRunner.kt b/usvm-python/usvm-python-runner/src/main/kotlin/org/usvm/runner/DebugRunner.kt new file mode 100644 index 0000000000..468b0c7638 --- /dev/null +++ b/usvm-python/usvm-python-runner/src/main/kotlin/org/usvm/runner/DebugRunner.kt @@ -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()}") + } +} \ No newline at end of file diff --git a/usvm-python/usvm-python-runner/src/main/kotlin/org/usvm/runner/PythonSymbolicAnalysisRunner.kt b/usvm-python/usvm-python-runner/src/main/kotlin/org/usvm/runner/PythonSymbolicAnalysisRunner.kt index 1409156d0d..f709969bcd 100644 --- a/usvm-python/usvm-python-runner/src/main/kotlin/org/usvm/runner/PythonSymbolicAnalysisRunner.kt +++ b/usvm-python/usvm-python-runner/src/main/kotlin/org/usvm/runner/PythonSymbolicAnalysisRunner.kt @@ -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 @@ -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, @@ -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) { @@ -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() } diff --git a/usvm-python/usvm-python-runner/src/main/kotlin/org/usvm/runner/USVMPythonRunner.kt b/usvm-python/usvm-python-runner/src/main/kotlin/org/usvm/runner/USVMPythonRunner.kt new file mode 100644 index 0000000000..e02d0c3286 --- /dev/null +++ b/usvm-python/usvm-python-runner/src/main/kotlin/org/usvm/runner/USVMPythonRunner.kt @@ -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 + } +} \ No newline at end of file diff --git a/usvm-python/usvm-python-runner/src/test/kotlin/org/usvm/runner/manualTest.kt b/usvm-python/usvm-python-runner/src/test/kotlin/org/usvm/runner/manualTest.kt index 1d43c1e0d1..64f1a530ca 100644 --- a/usvm-python/usvm-python-runner/src/test/kotlin/org/usvm/runner/manualTest.kt +++ b/usvm-python/usvm-python-runner/src/test/kotlin/org/usvm/runner/manualTest.kt @@ -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 } - } + }*/ } \ No newline at end of file