1 /* 2 * Copyright (c) 2016, 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 8144212 27 * @summary Check for correct memory flow with the String compress/inflate intrinsics. 28 * @modules java.base/jdk.internal.misc 29 * @library /test/lib 30 * 31 * @run main compiler.intrinsics.string.TestStringIntrinsicMemoryFlow 32 */ 33 34 package compiler.intrinsics.string; 35 36 import jdk.test.lib.Asserts; 37 38 public class TestStringIntrinsicMemoryFlow { 39 main(String[] args)40 public static void main(String[] args) { 41 for (int i = 0; i < 100_000; ++i) { 42 String s = "MyString"; 43 char[] c = {'M'}; 44 char res = testInflate1(s); 45 Asserts.assertEquals(res, 'M', "testInflate1 failed"); 46 res = testInflate2(s); 47 Asserts.assertEquals(res, (char)42, "testInflate2 failed"); 48 res = testCompress1(c); 49 Asserts.assertEquals(res, 'M', "testCompress1 failed"); 50 byte resB = testCompress2(c); 51 Asserts.assertEquals(resB, (byte)42, "testCompress2 failed"); 52 } 53 } 54 testInflate1(String s)55 private static char testInflate1(String s) { 56 char c[] = new char[1]; 57 // Inflate String from byte[] to char[] 58 s.getChars(0, 1, c, 0); 59 // Read char[] memory written by inflate intrinsic 60 return c[0]; 61 } 62 testInflate2(String s)63 private static char testInflate2(String s) { 64 char c1[] = new char[1]; 65 char c2[] = new char[1]; 66 c2[0] = 42; 67 // Inflate String from byte[] to char[] 68 s.getChars(0, 1, c1, 0); 69 // Read char[] memory written before inflation 70 return c2[0]; 71 } 72 testCompress1(char[] c)73 private static char testCompress1(char[] c) { 74 // Compress String from char[] to byte[] 75 String s = new String(c); 76 // Read the memory written by compress intrinsic 77 return s.charAt(0); 78 } 79 testCompress2(char[] c)80 private static byte testCompress2(char[] c) { 81 byte b1[] = new byte[1]; 82 b1[0] = 42; 83 // Compress String from char[] to byte[] 84 new String(c); 85 // Read byte[] memory written before compression 86 return b1[0]; 87 } 88 } 89