/* * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
// The idea of this test is that we pass a 'dirty' int value down to native code, and then receive it back // as the argument to an upcall, as well as the result of the downcall, but with a sub-int type (boolean, byte, short, char). // When we do either of those, argument normalization should take place, so that the resulting value is sane (1). // After that we convert the value back to int again, the JVM can/will skip value normalization here. // We then check the high order bits of the resulting int. If argument normalization took place at (1), they should all be 0.
@Test(dataProvider = "cases") publicvoid testNormalize(ValueLayout layout, int testValue, int hobMask, MethodHandle toInt, MethodHandle saver) throws Throwable { // use actual type as parameter type to test upcall arg normalization
FunctionDescriptor upcallDesc = FunctionDescriptor.ofVoid(layout); // use actual type as return type to test downcall return normalization
FunctionDescriptor downcallDesc = FunctionDescriptor.of(layout, ADDRESS, JAVA_INT);
try (Arena arena = Arena.openConfined()) { int[] box = newint[1];
saver = MethodHandles.insertArguments(saver, 1, box);
MemorySegment upcallStub = LINKER.upcallStub(saver, upcallDesc, arena.scope()); int dirtyValue = testValue | hobMask; // set all bits that should not be set
// test after JIT as well for (int i = 0; i < 20_000; i++) {
doCall(downcallHandle, upcallStub, box, dirtyValue, hobMask);
}
}
}
privatestaticvoid doCall(MethodHandle downcallHandle, MemorySegment upcallStub, int[] box, int dirtyValue, int hobMask) throws Throwable { int result = (int) downcallHandle.invokeExact(upcallStub, dirtyValue);
assertEquals(box[0] & hobMask, 0); // check normalized upcall arg
assertEquals(result & hobMask, 0); // check normalized downcall return value
}
// test which int values are considered true and false // we currently convert any int with a non-zero first byte to true, otherwise false.
@Test(dataProvider = "bools") publicvoid testBool(int testValue, boolean expected) throws Throwable {
MemorySegment addr = findNativeOrThrow("test");
MethodHandle target = LINKER.downcallHandle(addr, FunctionDescriptor.of(JAVA_BOOLEAN, ADDRESS, JAVA_INT));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.