|
| 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 | +} |
0 commit comments