Skip to content

Commit 0467deb

Browse files
committed
more tests for protecting access to destroyed objects
1 parent 5984bb2 commit 0467deb

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

Samples/SwiftKitSampleApp/src/test/java/org/swift/swiftkit/SwiftArenaTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.swift.swiftkit;
1616

1717
import com.example.swift.MySwiftClass;
18+
import com.example.swift.MySwiftStruct;
1819
import org.junit.jupiter.api.BeforeAll;
1920
import org.junit.jupiter.api.Test;
2021
import org.junit.jupiter.api.condition.DisabledIf;
@@ -47,8 +48,44 @@ public void arena_releaseClassOnClose_class_ok() {
4748
release(obj.$memorySegment());
4849
assertEquals(1, retainCount(obj.$memorySegment()));
4950
}
51+
}
52+
53+
// FIXME: The destroy witness table call hangs on x86_64 platforms during the destroy witness table call
54+
// See: https://github.com/swiftlang/swift-java/issues/97
55+
@Test
56+
public void arena_markAsDestroyed_preventUseAfterFree_class() {
57+
MySwiftClass unsafelyEscapedOutsideArenaScope = null;
58+
59+
try (var arena = SwiftArena.ofConfined()) {
60+
var obj = new MySwiftClass(arena,1, 2);
61+
unsafelyEscapedOutsideArenaScope = obj;
62+
}
63+
64+
try {
65+
unsafelyEscapedOutsideArenaScope.echoIntMethod(1);
66+
fail("Expected exception to be thrown! Object was suposed to be dead.");
67+
} catch (IllegalStateException ex) {
68+
return;
69+
}
70+
}
71+
72+
// FIXME: The destroy witness table call hangs on x86_64 platforms during the destroy witness table call
73+
// See: https://github.com/swiftlang/swift-java/issues/97
74+
@Test
75+
public void arena_markAsDestroyed_preventUseAfterFree_struct() {
76+
MySwiftStruct unsafelyEscapedOutsideArenaScope = null;
5077

51-
// TODO: should we zero out the $memorySegment perhaps?
78+
try (var arena = SwiftArena.ofConfined()) {
79+
var s = new MySwiftStruct(arena,1, 2);
80+
unsafelyEscapedOutsideArenaScope = s;
81+
}
82+
83+
try {
84+
unsafelyEscapedOutsideArenaScope.echoIntMethod(1);
85+
fail("Expected exception to be thrown! Object was suposed to be dead.");
86+
} catch (IllegalStateException ex) {
87+
return;
88+
}
5289
}
5390

5491
@Test

0 commit comments

Comments
 (0)