Path: blob/master/test/hotspot/jtreg/runtime/LocalLong/LocalLongHelper.java
40942 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.reflect.Field;24import java.lang.reflect.Method;25import java.util.List;26import java.util.Set;27import java.util.stream.Collectors;28import java.lang.StackWalker.StackFrame;2930public class LocalLongHelper {31static StackWalker sw;32static Method longValue;33static Method getLocals;34static Class<?> primitiveValueClass;35static Method primitiveSize;36static Method getMethodType;37static Field memberName;38static Field offset;3940public static void main(String[] args) throws Throwable {41setupReflectionStatics();42new LocalLongHelper().longArg(0xC0FFEE, 0x1234567890ABCDEFL);43}4445// locals[2] contains the unused slot of the long argument.46public long longArg(int i, long l) throws Throwable {47List<StackFrame> frames = sw.walk(s -> s.collect(Collectors.toList()));48Object[] locals = (Object[]) getLocals.invoke(frames.get(0));4950if (8 == (int) primitiveSize.invoke(locals[2])) { // Only test 64-bit51long locals_2 = (long) longValue.invoke(locals[2]);52if (locals_2 != 0){53throw new RuntimeException("Expected locals_2 == 0");54}55}56return l; // Don't want l to become a dead var57}5859private static void setupReflectionStatics() throws Throwable {60Class<?> liveStackFrameClass = Class.forName("java.lang.LiveStackFrame");61primitiveValueClass = Class.forName("java.lang.LiveStackFrame$PrimitiveSlot");6263getLocals = liveStackFrameClass.getDeclaredMethod("getLocals");64getLocals.setAccessible(true);6566longValue = primitiveValueClass.getDeclaredMethod("longValue");67longValue.setAccessible(true);6869Class<?> stackFrameInfoClass = Class.forName("java.lang.StackFrameInfo");70memberName = stackFrameInfoClass.getDeclaredField("memberName");71memberName.setAccessible(true);72offset = stackFrameInfoClass.getDeclaredField("bci");73offset.setAccessible(true);74getMethodType = Class.forName("java.lang.invoke.MemberName").getDeclaredMethod("getMethodType");75getMethodType.setAccessible(true);7677Class<?> extendedOptionClass = Class.forName("java.lang.StackWalker$ExtendedOption");78Method ewsNI = StackWalker.class.getDeclaredMethod("newInstance", Set.class, extendedOptionClass);79ewsNI.setAccessible(true);80Field f = extendedOptionClass.getDeclaredField("LOCALS_AND_OPERANDS");81f.setAccessible(true);82Object localsAndOperandsOption = f.get(null);8384primitiveSize = primitiveValueClass.getDeclaredMethod("size");85primitiveSize.setAccessible(true);86sw = (StackWalker) ewsNI.invoke(null, java.util.Collections.emptySet(), localsAndOperandsOption);87}88}899091