Skip to content

[SwiftKit] Move 'selfPointer' to 'JNISwiftInstance' & downcall trace logs #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

// Import javakit/swiftkit support libraries

import org.swift.swiftkit.core.CallTraces;
import org.swift.swiftkit.core.SwiftLibraries;
import org.swift.swiftkit.ffm.AllocatingSwiftArena;
import org.swift.swiftkit.ffm.SwiftRuntime;
Expand All @@ -43,30 +44,30 @@ static void examples() {

long cnt = MySwiftLibrary.globalWriteString("String from Java");

SwiftRuntime.trace("count = " + cnt);
CallTraces.trace("count = " + cnt);

MySwiftLibrary.globalCallMeRunnable(() -> {
SwiftRuntime.trace("running runnable");
CallTraces.trace("running runnable");
});

SwiftRuntime.trace("getGlobalBuffer().byteSize()=" + MySwiftLibrary.getGlobalBuffer().byteSize());
CallTraces.trace("getGlobalBuffer().byteSize()=" + MySwiftLibrary.getGlobalBuffer().byteSize());

MySwiftLibrary.withBuffer((buf) -> {
SwiftRuntime.trace("withBuffer{$0.byteSize()}=" + buf.byteSize());
CallTraces.trace("withBuffer{$0.byteSize()}=" + buf.byteSize());
});
// Example of using an arena; MyClass.deinit is run at end of scope
try (var arena = AllocatingSwiftArena.ofConfined()) {
MySwiftClass obj = MySwiftClass.init(2222, 7777, arena);

// just checking retains/releases work
SwiftRuntime.trace("retainCount = " + SwiftRuntime.retainCount(obj));
CallTraces.trace("retainCount = " + SwiftRuntime.retainCount(obj));
SwiftRuntime.retain(obj);
SwiftRuntime.trace("retainCount = " + SwiftRuntime.retainCount(obj));
CallTraces.trace("retainCount = " + SwiftRuntime.retainCount(obj));
SwiftRuntime.release(obj);
SwiftRuntime.trace("retainCount = " + SwiftRuntime.retainCount(obj));
CallTraces.trace("retainCount = " + SwiftRuntime.retainCount(obj));

obj.setCounter(12);
SwiftRuntime.trace("obj.counter = " + obj.getCounter());
CallTraces.trace("obj.counter = " + obj.getCounter());

obj.voidMethod();
obj.takeIntMethod(42);
Expand All @@ -75,22 +76,22 @@ static void examples() {
otherObj.voidMethod();

MySwiftStruct swiftValue = MySwiftStruct.init(2222, 1111, arena);
SwiftRuntime.trace("swiftValue.capacity = " + swiftValue.getCapacity());
CallTraces.trace("swiftValue.capacity = " + swiftValue.getCapacity());
swiftValue.withCapLen((cap, len) -> {
SwiftRuntime.trace("withCapLenCallback: cap=" + cap + ", len=" + len);
CallTraces.trace("withCapLenCallback: cap=" + cap + ", len=" + len);
});
}

// Example of using 'Data'.
try (var arena = AllocatingSwiftArena.ofConfined()) {
var origBytes = arena.allocateFrom("foobar");
var origDat = Data.init(origBytes, origBytes.byteSize(), arena);
SwiftRuntime.trace("origDat.count = " + origDat.getCount());
CallTraces.trace("origDat.count = " + origDat.getCount());

var retDat = MySwiftLibrary.globalReceiveReturnData(origDat, arena);
retDat.withUnsafeBytes((retBytes) -> {
var str = retBytes.getString(0);
SwiftRuntime.trace("retStr=" + str);
CallTraces.trace("retStr=" + str);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ extension FFMSwift2JavaGenerator {
"""
public static \(returnTy) call(\(paramsStr)) {
try {
if (SwiftRuntime.TRACE_DOWNCALLS) {
SwiftRuntime.traceDowncall(\(argsStr));
if (CallTraces.TRACE_DOWNCALLS) {
CallTraces.traceDowncall(\(argsStr));
}
\(maybeReturn)HANDLE.invokeExact(\(argsStr));
} catch (Throwable ex$) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ extension FFMSwift2JavaGenerator {
}
printer.print("import \(module)")
}
printer.println()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,15 @@ extension JNISwift2JavaGenerator {
printDeclDocumentation(&printer, decl)
printer.printBraceBlock("public \(renderFunctionSignature(decl))") { printer in
var arguments = translatedDecl.translatedFunctionSignature.parameters.map(\.name)
arguments.append("selfPointer")

let selfVarName = "self$"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we're doing $ in names in bodies is so they don't clash with any parameter name that someone can come up with, so I moved to self$ here -- FYI @madsodgaard

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea!

arguments.append(selfVarName)

let returnKeyword = translatedDecl.translatedFunctionSignature.resultType.isVoid ? "" : "return "

printer.print(
"""
long selfPointer = this.pointer();
long \(selfVarName) = this.$memoryAddress();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are basically unsafe, so I figured we shoudl hide them using our $ pattern.

\(returnKeyword)\(translatedDecl.parentName).$\(translatedDecl.name)(\(arguments.joined(separator: ", ")));
"""
)
Expand All @@ -235,8 +237,8 @@ extension JNISwift2JavaGenerator {
let initArguments = translatedDecl.translatedFunctionSignature.parameters.map(\.name)
printer.print(
"""
long selfPointer = \(type.qualifiedName).allocatingInit(\(initArguments.joined(separator: ", ")));
return new \(type.qualifiedName)(selfPointer, swiftArena$);
long self$ = \(type.qualifiedName).allocatingInit(\(initArguments.joined(separator: ", ")));
return new \(type.qualifiedName)(self$, swiftArena$);
"""
)
}
Expand All @@ -262,15 +264,24 @@ extension JNISwift2JavaGenerator {
private func printDestroyFunction(_ printer: inout CodePrinter, _ type: ImportedNominalType) {
printer.print("private static native void $destroy(long selfPointer);")

let funcName = "$createDestroyFunction"
printer.print("@Override")
printer.printBraceBlock("protected Runnable $createDestroyFunction()") { printer in
printer.printBraceBlock("protected Runnable \(funcName)()") { printer in
printer.print(
"""
long $selfPointer = this.pointer();
long self$ = this.$memoryAddress();
if (CallTraces.TRACE_DOWNCALLS) {
CallTraces.traceDowncall("\(type.swiftNominal.name).\(funcName)",
"this", this,
"self", self$);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit of downtracing helped diagnose issues

return new Runnable() {
@Override
public void run() {
\(type.swiftNominal.name).$destroy($selfPointer);
if (CallTraces.TRACE_DOWNCALLS) {
CallTraces.traceDowncall("\(type.swiftNominal.name).$destroy", "self", self$);
}
\(type.swiftNominal.name).$destroy(self$);
}
};
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ extension JNISwift2JavaGenerator {
// TODO: Throwing initializers
printer.print(
"""
let selfPointer = UnsafeMutablePointer<\(typeName)>.allocate(capacity: 1)
selfPointer.initialize(to: \(typeName)(\(downcallArguments)))
return Int64(Int(bitPattern: selfPointer)).getJNIValue(in: environment)
let self$ = UnsafeMutablePointer<\(typeName)>.allocate(capacity: 1)
self$.initialize(to: \(typeName)(\(downcallArguments)))
return Int64(Int(bitPattern: self$)).getJNIValue(in: environment)
"""
)
}
Expand Down Expand Up @@ -184,22 +184,20 @@ extension JNISwift2JavaGenerator {
let translatedDecl = self.translatedDecl(for: decl)! // We will only call this method if can translate the decl.
let swiftParentName = decl.parentType!.asNominalTypeDeclaration!.qualifiedName

let selfPointerParam = JavaParameter(name: "selfPointer", type: .long)
printCDecl(
&printer,
javaMethodName: "$\(translatedDecl.name)",
parentName: translatedDecl.parentName,
parameters: translatedDecl.translatedFunctionSignature.parameters + [
JavaParameter(name: "selfPointer", type: .long)
selfPointerParam
],
isStatic: true,
resultType: translatedDecl.translatedFunctionSignature.resultType
) { printer in
printer.print(
"""
let self$ = UnsafeMutablePointer<\(swiftParentName)>(bitPattern: Int(Int64(fromJNI: selfPointer, in: environment!)))!
"""
)
self.printFunctionDowncall(&printer, decl, calleeName: "self$.pointee")
let selfVar = self.printSelfJLongToUnsafeMutablePointer(&printer,
swiftParentName: swiftParentName, selfPointerParam)
self.printFunctionDowncall(&printer, decl, calleeName: "\(selfVar).pointee")
}
}

Expand Down Expand Up @@ -320,28 +318,53 @@ extension JNISwift2JavaGenerator {

/// Prints the implementation of the destroy function.
private func printDestroyFunctionThunk(_ printer: inout CodePrinter, _ type: ImportedNominalType) {
let selfPointerParam = JavaParameter(name: "selfPointer", type: .long)
printCDecl(
&printer,
javaMethodName: "$destroy",
parentName: type.swiftNominal.name,
parameters: [
JavaParameter(name: "selfPointer", type: .long)
selfPointerParam
],
isStatic: true,
resultType: .void
) { printer in
let parentName = type.qualifiedName
let selfVar = self.printSelfJLongToUnsafeMutablePointer(&printer, swiftParentName: parentName, selfPointerParam)
// Deinitialize the pointer allocated (which will call the VWT destroy method)
// then deallocate the memory.
printer.print(
"""
let pointer = UnsafeMutablePointer<\(type.qualifiedName)>(bitPattern: Int(Int64(fromJNI: selfPointer, in: environment!)))!
pointer.deinitialize(count: 1)
pointer.deallocate()
\(selfVar).deinitialize(count: 1)
\(selfVar).deallocate()
"""
)
}
}

/// Print the necessary conversion logic to go from a `jlong` to a `UnsafeMutablePointer<Type>`
///
/// - Returns: name of the created "self" variable
private func printSelfJLongToUnsafeMutablePointer(
_ printer: inout CodePrinter,
swiftParentName: String, _ selfPointerParam: JavaParameter) -> String {
let newSelfParamName = "self$"
printer.print(
"""
guard let env$ = environment else {
fatalError("Missing JNIEnv in downcall to \\(#function)")
}
assert(\(selfPointerParam.name) != 0, "\(selfPointerParam.name) memory address was null")
let selfBits$ = Int(Int64(fromJNI: \(selfPointerParam.name), in: env$))
guard let \(newSelfParamName) = UnsafeMutablePointer<\(swiftParentName)>(bitPattern: selfBits$) else {
fatalError("self memory address was null in call to \\(#function)!")
}
"""
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was repeated 2 times, so now it's in 1 place.

The reason to split it up into more lines and not just the single line with multiple ! is so we have better messages when things fail; It took me a while to diagnose an issue without this so I'd suggest to keep this in.

return newSelfParamName
}


/// Renders the arguments for making a downcall
private func renderDowncallArguments(
swiftFunctionSignature: SwiftFunctionSignature,
Expand Down
61 changes: 61 additions & 0 deletions SwiftKitCore/src/main/java/org/swift/swiftkit/core/CallTraces.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

package org.swift.swiftkit.core;

public class CallTraces {
public static final boolean TRACE_DOWNCALLS =
Boolean.getBoolean("jextract.trace.downcalls");

// Used to manually debug with complete backtraces on every traceDowncall
public static final boolean TRACE_DOWNCALLS_FULL = false;

public static void traceDowncall(Object... args) {
RuntimeException ex = new RuntimeException();

String traceArgs = joinArgs(args);
System.err.printf("[java][%s:%d] Downcall: %s.%s(%s)\n",
ex.getStackTrace()[1].getFileName(),
ex.getStackTrace()[1].getLineNumber(),
ex.getStackTrace()[1].getClassName(),
ex.getStackTrace()[1].getMethodName(),
traceArgs);
if (TRACE_DOWNCALLS_FULL) {
ex.printStackTrace();
}
}

public static void trace(Object... args) {
RuntimeException ex = new RuntimeException();

String traceArgs = joinArgs(args);
System.err.printf("[java][%s:%d] %s: %s\n",
ex.getStackTrace()[1].getFileName(),
ex.getStackTrace()[1].getLineNumber(),
ex.getStackTrace()[1].getMethodName(),
traceArgs);
}

private static String joinArgs(Object[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < args.length; i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(args[i].toString());
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void close() {
public void register(SwiftInstance instance) {
checkValid();

SwiftInstanceCleanup cleanup = instance.createCleanupAction();
SwiftInstanceCleanup cleanup = instance.$createCleanup();
this.resources.add(cleanup);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,30 @@

package org.swift.swiftkit.core;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

public abstract class JNISwiftInstance extends SwiftInstance {
// Pointer to the "self".
protected final long selfPointer;

/**
* The designated constructor of any imported Swift types.
*
* @param pointer a pointer to the memory containing the value
* @param selfPointer a pointer to the memory containing the value
* @param arena the arena this object belongs to. When the arena goes out of scope, this value is destroyed.
*/
protected JNISwiftInstance(long pointer, SwiftArena arena) {
super(pointer, arena);
protected JNISwiftInstance(long selfPointer, SwiftArena arena) {
SwiftObjects.requireNonZero(selfPointer, "selfPointer");
this.selfPointer = selfPointer;

// Only register once we have fully initialized the object since this will need the object pointer.
arena.register(this);
}

@Override
public long $memoryAddress() {
return this.selfPointer;
}

/**
Expand All @@ -42,7 +55,7 @@ protected JNISwiftInstance(long pointer, SwiftArena arena) {
protected abstract Runnable $createDestroyFunction();

@Override
public SwiftInstanceCleanup createCleanupAction() {
public SwiftInstanceCleanup $createCleanup() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not a method end users should be invoking explicitly, so changed it while at it.

final AtomicBoolean statusDestroyedFlag = $statusDestroyedFlag();
Runnable markAsDestroyed = new Runnable() {
@Override
Expand Down
Loading
Loading