Skip to content

Commit

Permalink
fix: jna os info on linux/macos
Browse files Browse the repository at this point in the history
- fix: jna/oshi reflection and proxy metadata
- fix: no stacktrace for host-side error causes in native mode
- chore: cpu test script
- chore: update `graalvm` module pin / detekt baseline

Fixes and closes #990

Signed-off-by: Sam Gammon <sam@elide.ventures>
  • Loading branch information
sgammon committed Jun 8, 2024
1 parent bcb7c8b commit 55c83e3
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ public abstract class AbstractStaticNativeLibraryFeature : NativeLibraryFeature
if (it.registerJni) (access as BeforeAnalysisAccessImpl).nativeLibraries.let { nativeLibraries ->
when (it.type) {
STATIC -> nativeLibraries.addStaticJniLibrary(it.name)
SHARED -> error("Dynamic native libraries not supported yet: $it")
SHARED -> if (it.registerJni) { /* Dynamic JNI libraries use JNI to load. */ } else {
nativeLibraries.addDynamicNonJniLibrary(it.name)
}
}
}
if (it.eager) {
Expand Down
9 changes: 8 additions & 1 deletion packages/runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ val commonNativeArgs = listOfNotNull(
"--install-exit-handlers",
"--enable-url-protocols=jar",
"--macro:truffle-svm",
"--enable-native-access=ALL-UNNAMED",
"--enable-native-access=com.sun.jna,ALL-UNNAMED",
"-J--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED",
"-J--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.hosted=ALL-UNNAMED",
"-J--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.hosted.c=ALL-UNNAMED",
Expand All @@ -668,6 +668,10 @@ val commonNativeArgs = listOfNotNull(
"-J-Delide.staticJni=$enableStaticJni",
"-Delide.natives=$nativesPath",
"-J-Delide.natives=$nativesPath",
"-Djna.library.path=$nativesPath",
"-J-Djna.library.path=$nativesPath",
"-Djna.boot.library.path=$nativesPath",
"-J-Djna.boot.library.path=$nativesPath",
"-Dorg.sqlite.lib.path=$nativesPath",
"-J-Dorg.sqlite.lib.path=$nativesPath",
"-Dorg.sqlite.lib.exportPath=$nativesPath",
Expand Down Expand Up @@ -799,6 +803,8 @@ val releaseFlags: List<String> = listOf(

val jvmDefs = mapOf(
"elide.natives" to nativesPath,
"jna.library.path" to nativesPath,
"jna.boot.library.path" to nativesPath,
"elide.nativeTransport.v2" to enableNativeTransportV2.toString(),
"io.netty.allocator.type" to "unpooled",
"io.netty.native.deleteLibAfterLoading" to "false",
Expand Down Expand Up @@ -857,6 +863,7 @@ val initializeAtRuntime: List<String> = listOfNotNull(

// --- JNA -----

"com.sun.jna.Native",
"com.sun.jna.Structure${'$'}FFIType",
"com.sun.jna.platform.mac.IOKit",
"com.sun.jna.platform.mac.IOKitUtil",
Expand Down
5 changes: 1 addition & 4 deletions packages/runtime/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ID>CyclomaticComplexMethod:ToolShellCommand.kt$ToolShellCommand$override fun PolyglotEngineConfiguration.configureEngine()</ID>
<ID>CyclomaticComplexMethod:ToolShellCommand.kt$ToolShellCommand$override suspend fun CommandContext.invoke(state: ToolContext&lt;ToolState>): CommandResult</ID>
<ID>CyclomaticComplexMethod:ToolShellCommand.kt$ToolShellCommand$private fun beginInteractiveSession( languages: EnumSet&lt;GuestLanguage>, primaryLanguage: GuestLanguage, engine: PolyglotEngine, ctx: PolyglotContext, )</ID>
<ID>CyclomaticComplexMethod:ToolShellCommand.kt$ToolShellCommand$private fun displayFormattedError( exc: Throwable, message: String, advice: String? = null, internal: Boolean = false, stacktrace: Boolean = internal, )</ID>
<ID>CyclomaticComplexMethod:ToolShellCommand.kt$ToolShellCommand$private fun displayFormattedError( exc: Throwable, message: String, advice: String? = null, internal: Boolean = false, stacktrace: Boolean = internal, withCause: Boolean = true, )</ID>
<ID>FunctionParameterNaming:Statics.kt$Statics$`in`: InputStream</ID>
<ID>LargeClass:ToolShellCommand.kt$ToolShellCommand : AbstractSubcommand</ID>
<ID>LoopWithTooManyJumpStatements:ToolShellCommand.kt$ToolShellCommand$while</ID>
Expand All @@ -29,12 +29,9 @@
<ID>NestedBlockDepth:SelfTestCommand.kt$SelfTestCommand$override suspend fun CommandContext.invoke(state: ToolContext&lt;ToolState>): CommandResult</ID>
<ID>ObjectPropertyNaming:Statics.kt$Statics$val `in`: InputStream get() = delegatedInStream.get() ?: System.`in`</ID>
<ID>ReturnCount:ExecutionController.kt$ExecutionController$private fun toHost(polyglotException: PolyglotException): Throwable</ID>
<ID>ReturnCount:NativeSQLiteFeature.kt$NativeSQLiteFeature$override fun unpackNatives(access: BeforeAnalysisAccess): List&lt;UnpackedNative></ID>
<ID>ReturnCount:NativeUtil.kt$NativeUtil$@JvmStatic internal fun loadOrCopy( workdir: File, path: String, libName: String, loader: ClassLoader, forceCopy: Boolean = false, forceLoad: Boolean = false, loadFromPath: Boolean = true, ): Pair&lt;Boolean, Boolean></ID>
<ID>ReturnCount:RuntimeWorkdirManager.kt$RuntimeWorkdirManager$private fun nearestDirectoryWithAnyOfTheseFiles( files: SortedSet&lt;String>, base: File? = null, depth: Int? = null, ): File?</ID>
<ID>SpreadOperator:Elide.kt$Elide.Companion$(*args)</ID>
<ID>SpreadOperator:NativeSQLiteFeature.kt$NativeSQLiteFeature$(*fields( NativeDB::class.java, "pointer", "busyHandler", "commitListener", "updateListener", "progressHandler", ))</ID>
<ID>SpreadOperator:NativeSQLiteFeature.kt$NativeSQLiteFeature$(*this.fields(Function::class.java, "context", "value", "args"))</ID>
<ID>SwallowedException:DefaultProjectManager.kt$DefaultProjectManager.Companion$ioe: IOException</ID>
<ID>SwallowedException:DefaultProjectManager.kt$DefaultProjectManager.Companion$thr: Throwable</ID>
<ID>SwallowedException:Elide.kt$Elide.Companion$uoe: UnsupportedOperationException</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1107,10 +1107,11 @@ import elide.tool.project.ProjectManager
advice: String? = null,
internal: Boolean = false,
stacktrace: Boolean = internal,
withCause: Boolean = true,
) {
if (exc !is PolyglotException) return
val term = terminal.get()
val reader = lineReader.get()
if (exc !is PolyglotException) return

// begin calculating with source context
val middlePrefix = ""
Expand Down Expand Up @@ -1146,6 +1147,16 @@ import elide.tool.project.ProjectManager
val stackString = StringWriter()
val stackPrinter = PrintWriter(stackString)
exc.printStackTrace(stackPrinter)
when (val cause = exc.cause ?: if (exc.isHostException) exc.asHostException() else null) {
null -> {}
else -> if (withCause) {
stackString.append("\nCause stacktrace: ")
cause.printStackTrace(stackPrinter)
} else if (exc.isHostException) {
stackString.append("\nCause: ${cause.message}")
stackString.append(" Failed to gather stacktrace for host exception of type ${cause::class.simpleName}.")
}
}
stackPrinter.flush()
stackString.toString()
} else {
Expand Down Expand Up @@ -1275,7 +1286,7 @@ import elide.tool.project.ProjectManager

exc.isHostException || exc.message?.contains("HostException: ") == true -> displayFormattedError(
exc,
exc.message ?: "An runtime error was thrown",
exc.message ?: "A runtime error was thrown",
advice = "This is an error in Elide. Please report this to the Elide Team with `elide bug`",
stacktrace = true,
internal = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true,
"methods":[{"name":"dispose","parameterTypes":[] }, {"name":"fromNative","parameterTypes":["com.sun.jna.FromNativeConverter","java.lang.Object","java.lang.reflect.Method"] }, {"name":"fromNative","parameterTypes":["java.lang.Class","java.lang.Object"] }, {"name":"fromNative","parameterTypes":["java.lang.reflect.Method","java.lang.Object"] }, {"name":"nativeType","parameterTypes":["java.lang.Class"] }, {"name":"toNative","parameterTypes":["com.sun.jna.ToNativeConverter","java.lang.Object"] }]
"methods":[
{"name":"dispose","parameterTypes":[] },
{"name":"fromNative","parameterTypes":["com.sun.jna.FromNativeConverter","java.lang.Object","java.lang.reflect.Method"] },
{"name":"fromNative","parameterTypes":["java.lang.Class","java.lang.Object"] },
{"name":"fromNative","parameterTypes":["java.lang.reflect.Method","java.lang.Object"] },
{"name":"nativeType","parameterTypes":["java.lang.Class"] },
{"name":"toNative","parameterTypes":["com.sun.jna.ToNativeConverter","java.lang.Object"]},
{"name":"open","parameterTypes":["java.lang.String","java.lang.Integer"]},
{"name":"close","parameterTypes":["java.lang.Long"]},
{"name":"findSymbol","parameterTypes":["java.lang.Long","java.lang.String"]}
]
},
{
"name":"com.sun.jna.Native$ffi_callback",
Expand Down Expand Up @@ -159,6 +169,13 @@
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.jna.platform.mac.CoreFoundation$CFStringRef",
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.management.internal.DiagnosticCommandArgumentInfo",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String","boolean","boolean","boolean","int"] }]
Expand Down Expand Up @@ -561,4 +578,4 @@
"name":"sun.nio.ch.FileChannelImpl",
"fields":[{"name":"fd"}]
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
},
{
"interfaces":["oshi.jna.platform.linux.LinuxLibc"]
},
{
"interfaces":["com.sun.jna.platform.mac.SystemB"]
},
{
"interfaces":["oshi.jna.platform.mac.SystemB"]
},
{
"interfaces":["com.sun.jna.platform.mac.IOKit"]
},
{
"interfaces":["com.sun.jna.platform.mac.CoreFoundation"]
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -7420,5 +7420,117 @@
{
"name":"sun.security.x509.SubjectKeyIdentifierExtension",
"methods":[{"name":"<init>","parameterTypes":["java.lang.Boolean","java.lang.Object"] }]
},
{
"name":"oshi.jna.ByRef$CloseableSizeTByReference",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"oshi.jna.ByRef$CloseableIntByReference",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"oshi.jna.ByRef$CloseableLongByReference",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"oshi.jna.ByRef$CloseableNativeLongByReference",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.jna.platform.unix.LibCAPI$size_t",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.jna.platform.mac.CoreFoundation$CFMutableDictionaryRef",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.jna.platform.mac.IOKit$IOIterator",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.jna.platform.mac.IOKit$IORegistryEntry",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.jna.platform.mac.CoreFoundation$CFStringRef",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.jna.platform.mac.CoreFoundation$CFTypeID",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.jna.platform.mac.CoreFoundation$CFIndex",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.jna.platform.mac.CoreFoundation$CFAllocatorRef",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.jna.platform.mac.CoreFoundation$CFTypeRef",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
},
{
"name":"com.sun.jna.platform.mac.CoreFoundation$CFDataRef",
"methods":[{"name":"<init>","parameterTypes":[] }],
"allDeclaredMethods":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true
}
]
]
3 changes: 3 additions & 0 deletions tools/scripts/cpu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const os = require("node:os");
const cpus = os.cpus();
console.log("CPU info: " + JSON.stringify(cpus, null, 2));

0 comments on commit 55c83e3

Please sign in to comment.