Skip to content

Commit 03bbd3d

Browse files
committed
add new tests
1 parent 4d2d9d8 commit 03bbd3d

File tree

2 files changed

+253
-0
lines changed

2 files changed

+253
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8357601
27+
* @requires vm.flagless
28+
* @library /test/lib
29+
* @run main/othervm/native TestCharArrayReleasing 0 0
30+
* @run main/othervm/native TestCharArrayReleasing 1 0
31+
* @run main/othervm/native TestCharArrayReleasing 2 0
32+
* @run main/othervm/native TestCharArrayReleasing 3 0
33+
* @run main/othervm/native TestCharArrayReleasing 4 0
34+
* @run main/othervm/native TestCharArrayReleasing 0 1
35+
* @run main/othervm/native TestCharArrayReleasing 1 1
36+
* @run main/othervm/native TestCharArrayReleasing 2 1
37+
* @run main/othervm/native TestCharArrayReleasing 3 1
38+
* @run main/othervm/native TestCharArrayReleasing 4 1
39+
* @run main/othervm/native TestCharArrayReleasing 0 2
40+
* @run main/othervm/native TestCharArrayReleasing 1 2
41+
* @run main/othervm/native TestCharArrayReleasing 2 2
42+
* @run main/othervm/native TestCharArrayReleasing 3 2
43+
* @run main/othervm/native TestCharArrayReleasing 4 2
44+
* @run main/othervm/native TestCharArrayReleasing 0 3
45+
* @run main/othervm/native TestCharArrayReleasing 1 3
46+
* @run main/othervm/native TestCharArrayReleasing 2 3
47+
* @run main/othervm/native TestCharArrayReleasing 3 3
48+
* @run main/othervm/native TestCharArrayReleasing 4 3
49+
*/
50+
51+
import jdk.test.lib.Platform;
52+
import jdk.test.lib.process.ProcessTools;
53+
import jdk.test.lib.process.OutputAnalyzer;
54+
55+
// Test the behaviour of the JNI "char" releasing functions, under Xcheck:jni,
56+
// when they are passed "char" arrays obtained from different sources:
57+
// - source_mode indicates which array to use
58+
// - 0: use a raw malloc'd array
59+
// - 1: use an array from GetCharArrayElements
60+
// - 2: use an array from GetStringChars
61+
// - 3: use an array from GetStringUTFChars
62+
// - 4: use an array from GetPrimitiveArrayCritical
63+
// - release_mode indicates which releasing function to use
64+
// - 0: ReleaseCharArrayElements
65+
// - 1: ReleaseStringChars
66+
// - 2: ReleaseStringUTFChars
67+
// - 3: ReleasePrimitiveArrayCritical
68+
69+
public class TestCharArrayReleasing {
70+
71+
static native void testIt(int srcMode, int releaseMode);
72+
73+
static class Driver {
74+
75+
static {
76+
System.loadLibrary("CharArrayReleasing");
77+
}
78+
79+
public static void main(String[] args) {
80+
int srcMode = Integer.parseInt(args[0]);
81+
int relMode = Integer.parseInt(args[1]);
82+
testIt(srcMode, relMode);
83+
}
84+
}
85+
86+
public static void main(String[] args) throws Throwable {
87+
int ABRT = Platform.isWindows() ? 1 : 134;
88+
int[][] errorCodes = new int[][] {
89+
{ ABRT, 0, ABRT, ABRT, ABRT },
90+
{ ABRT, ABRT, 0, ABRT, ABRT },
91+
{ ABRT, ABRT, ABRT, 0, ABRT },
92+
{ ABRT, ABRT, ABRT, ABRT, 0 },
93+
};
94+
95+
String rcae = "ReleaseCharArrayElements called on something allocated by GetStringChars";
96+
String rcaeUTF = "ReleaseCharArrayElements called on something allocated by GetStringUTFChars";
97+
String rcaeCrit = "ReleaseCharArrayElements called on something allocated by GetPrimitiveArrayCritical";
98+
String rcaeBounds = "ReleaseCharArrayElements: release array failed bounds check";
99+
String rsc = "ReleaseStringChars called on something not allocated by GetStringChars";
100+
String rscBounds = "ReleaseStringChars: release chars failed bounds check";
101+
String rsuc = "ReleaseStringUTFChars called on something not allocated by GetStringUTFChars";
102+
String rsucBounds = "ReleaseStringUTFChars: release chars failed bounds check";
103+
String rpac = "ReleasePrimitiveArrayCritical called on something not allocated by GetPrimitiveArrayCritical";
104+
String rpacBounds = "ReleasePrimitiveArrayCritical: release array failed bounds check";
105+
String rpacStr = "ReleasePrimitiveArrayCritical called on something allocated by GetStringChars";
106+
String rpacStrUTF = "ReleasePrimitiveArrayCritical called on something allocated by GetStringUTFChars";
107+
108+
String[][] errorMsgs = new String[][] {
109+
{ rcaeBounds, "", rcae, rcaeUTF, rcaeCrit },
110+
{ rscBounds, rsc, "", rsc, rsc },
111+
{ rsucBounds, rsuc, rsuc, "", rsuc },
112+
{ rpacBounds, rpac, rpacStr, rpacStrUTF, "" },
113+
};
114+
115+
int srcMode = Integer.parseInt(args[0]);
116+
int relMode = Integer.parseInt(args[1]);
117+
118+
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(
119+
"-Djava.library.path=" + System.getProperty("test.nativepath"),
120+
"--enable-native-access=ALL-UNNAMED",
121+
"-Xcheck:jni",
122+
"TestCharArrayReleasing$Driver",
123+
args[0], args[1]);
124+
OutputAnalyzer output = new OutputAnalyzer(pb.start());
125+
output.shouldHaveExitValue(errorCodes[relMode][srcMode]);
126+
output.shouldContain(errorMsgs[relMode][srcMode]);
127+
}
128+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
#include <stdio.h>
24+
#include <stdlib.h>
25+
#include <string.h>
26+
27+
#include "jni.h"
28+
#include "jni_util.h"
29+
30+
// Test the behaviour of the JNI "char" releasing functions, under Xcheck:jni,
31+
// when they are passed "char" arrays obtained from different sources:
32+
// - source_mode indicates which array to use
33+
// - 0: use a raw malloc'd array
34+
// - 1: use an array from GetCharArrayElements
35+
// - 2: use an array from GetStringChars
36+
// - 3: use an array from GetStringUTFChars
37+
// - 4: use an array from GetPrimitiveArrayCritical
38+
// - release_mode indicates which releasing function to use
39+
// - 0: ReleaseCharArrayElements
40+
// - 1: ReleaseStringChars
41+
// - 2: ReleaseStringUTFChars
42+
// - 3: ReleasePrimitiveArrayCritical
43+
//
44+
45+
static char* source[] = {
46+
"malloc",
47+
"GetCharArrayElements",
48+
"GetStringChars",
49+
"GetStringUTFChars",
50+
"GetPrimitiveArrayCritical"
51+
};
52+
53+
static char* release_func[] = {
54+
"ReleaseCharArrayElements",
55+
"ReleaseStringChars",
56+
"ReleaseStringUTFChars",
57+
"ReleasePrimitiveArrayCritical"
58+
};
59+
60+
JNIEXPORT void JNICALL
61+
Java_TestCharArrayReleasing_testIt(JNIEnv *env, jclass cls, jint source_mode,
62+
jint release_mode) {
63+
64+
// First create some Java objects to be used as the sources for jchar[]
65+
// extraction.
66+
const int len = 10;
67+
jcharArray ca = (*env)->NewCharArray(env, len);
68+
jstring str = (*env)->NewStringUTF(env, "A_String");
69+
70+
jthrowable exc = (*env)->ExceptionOccurred(env);
71+
if (exc != NULL) {
72+
fprintf(stderr, "ERROR: Unexpected exception during test set up:\n");
73+
(*env)->ExceptionDescribe(env);
74+
exit(2);
75+
}
76+
77+
fprintf(stdout, "Testing release function %s with array from %s\n",
78+
release_func[release_mode], source[source_mode]);
79+
fflush(stdout);
80+
81+
jboolean is_copy = JNI_FALSE;
82+
jchar* to_release;
83+
switch(source_mode) {
84+
case 0: {
85+
to_release = malloc(10 * sizeof(jchar));
86+
break;
87+
}
88+
case 1: {
89+
to_release = (*env)->GetCharArrayElements(env, ca, &is_copy);
90+
break;
91+
}
92+
case 2: {
93+
to_release = (jchar*) (*env)->GetStringChars(env, str, &is_copy);
94+
break;
95+
}
96+
case 3: {
97+
to_release = (jchar*) (*env)->GetStringUTFChars(env, str, &is_copy);
98+
break;
99+
}
100+
case 4: {
101+
to_release = (jchar*) (*env)->GetPrimitiveArrayCritical(env, ca, &is_copy);
102+
break;
103+
}
104+
default: fprintf(stderr, "Unexpected source_mode %d\n", source_mode);
105+
exit(1);
106+
}
107+
108+
switch (release_mode) {
109+
case 0:
110+
(*env)->ReleaseCharArrayElements(env, ca, to_release, 0);
111+
break;
112+
case 1:
113+
(*env)->ReleaseStringChars(env, str, to_release);
114+
break;
115+
case 2:
116+
(*env)->ReleaseStringUTFChars(env, str, (const char*)to_release);
117+
break;
118+
case 3:
119+
(*env)->ReleasePrimitiveArrayCritical(env, ca, to_release, 0);
120+
break;
121+
default: fprintf(stderr, "Unexpected release_mode %d\n", source_mode);
122+
exit(1);
123+
}
124+
125+
}

0 commit comments

Comments
 (0)