1 /*
  2  * Copyright (c) 2008, 2020, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang.invoke;
 27 
 28 import jdk.internal.misc.Unsafe;
 29 import jdk.internal.vm.annotation.ForceInline;
 30 import jdk.internal.vm.annotation.Stable;
 31 import sun.invoke.util.ValueConversions;
 32 import sun.invoke.util.VerifyAccess;
 33 import sun.invoke.util.VerifyType;
 34 import sun.invoke.util.Wrapper;
 35 
 36 import java.util.Arrays;
 37 import java.util.Objects;
 38 import java.util.function.Function;
 39 
 40 import static java.lang.invoke.LambdaForm.*;
 41 import static java.lang.invoke.LambdaForm.Kind.*;
 42 import static java.lang.invoke.MethodHandleNatives.Constants.*;
 43 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
 44 import static java.lang.invoke.MethodHandleStatics.newInternalError;
 45 import static java.lang.invoke.MethodTypeForm.*;
 46 
 47 /**
 48  * The flavor of method handle which implements a constant reference
 49  * to a class member.
 50  * @author jrose
 51  */
 52 class DirectMethodHandle extends MethodHandle {
 53     final MemberName member;
 54     final boolean crackable;
 55 
 56     // Constructors and factory methods in this class *must* be package scoped or private.
 57     private DirectMethodHandle(MethodType mtype, LambdaForm form, MemberName member, boolean crackable) {
 58         super(mtype, form);
 59         if (!member.isResolved())  throw new InternalError();
 60 
 61         if (member.getDeclaringClass().isInterface() &&
 62             member.getReferenceKind() == REF_invokeInterface &&
 63             member.isMethod() && !member.isAbstract()) {
 64             // Check for corner case: invokeinterface of Object method
 65             MemberName m = new MemberName(Object.class, member.getName(), member.getMethodType(), member.getReferenceKind());
 66             m = MemberName.getFactory().resolveOrNull(m.getReferenceKind(), m, null, LM_TRUSTED);
 67             if (m != null && m.isPublic()) {
 68                 assert(member.getReferenceKind() == m.getReferenceKind());  // else this.form is wrong
 69                 member = m;
 70             }
 71         }
 72 
 73         this.member = member;
 74         this.crackable = crackable;
 75     }
 76 
 77     // Factory methods:
 78     static DirectMethodHandle make(byte refKind, Class<?> refc, MemberName member, Class<?> callerClass) {
 79         MethodType mtype = member.getMethodOrFieldType();
 80         if (!member.isStatic()) {
 81             if (!member.getDeclaringClass().isAssignableFrom(refc) || member.isConstructor())
 82                 throw new InternalError(member.toString());
 83             mtype = mtype.insertParameterTypes(0, refc);
 84         }
 85         if (!member.isField()) {
 86             // refKind reflects the original type of lookup via findSpecial or
 87             // findVirtual etc.
 88             return switch (refKind) {
 89                 case REF_invokeSpecial -> {
 90                     member = member.asSpecial();
 91                     // if caller is an interface we need to adapt to get the
 92                     // receiver check inserted
 93                     if (callerClass == null) {
 94                         throw new InternalError("callerClass must not be null for REF_invokeSpecial");
 95                     }
 96                     LambdaForm lform = preparedLambdaForm(member, callerClass.isInterface());
 97                     yield new Special(mtype, lform, member, true, callerClass);
 98                 }
 99                 case REF_invokeInterface -> {
100                     // for interfaces we always need the receiver typecheck,
101                     // so we always pass 'true' to ensure we adapt if needed
102                     // to include the REF_invokeSpecial case
103                     LambdaForm lform = preparedLambdaForm(member, true);
104                     yield new Interface(mtype, lform, member, true, refc);
105                 }
106                 default -> {
107                     LambdaForm lform = preparedLambdaForm(member);
108                     yield new DirectMethodHandle(mtype, lform, member, true);
109                 }
110             };
111         } else {
112             LambdaForm lform = preparedFieldLambdaForm(member);
113             if (member.isStatic()) {
114                 long offset = MethodHandleNatives.staticFieldOffset(member);
115                 Object base = MethodHandleNatives.staticFieldBase(member);
116                 return new StaticAccessor(mtype, lform, member, true, base, offset);
117             } else {
118                 long offset = MethodHandleNatives.objectFieldOffset(member);
119                 assert(offset == (int)offset);
120                 return new Accessor(mtype, lform, member, true, (int)offset);
121             }
122         }
123     }
124     static DirectMethodHandle make(Class<?> refc, MemberName member) {
125         byte refKind = member.getReferenceKind();
126         if (refKind == REF_invokeSpecial)
127             refKind =  REF_invokeVirtual;
128         return make(refKind, refc, member, null /* no callerClass context */);
129     }
130     static DirectMethodHandle make(MemberName member) {
131         if (member.isConstructor())
132             return makeAllocator(member);
133         return make(member.getDeclaringClass(), member);
134     }
135     private static DirectMethodHandle makeAllocator(MemberName ctor) {
136         assert(ctor.isConstructor() && ctor.getName().equals("<init>"));
137         Class<?> instanceClass = ctor.getDeclaringClass();
138         ctor = ctor.asConstructor();
139         assert(ctor.isConstructor() && ctor.getReferenceKind() == REF_newInvokeSpecial) : ctor;
140         MethodType mtype = ctor.getMethodType().changeReturnType(instanceClass);
141         LambdaForm lform = preparedLambdaForm(ctor);
142         MemberName init = ctor.asSpecial();
143         assert(init.getMethodType().returnType() == void.class);
144         return new Constructor(mtype, lform, ctor, true, init, instanceClass);
145     }
146 
147     @Override
148     BoundMethodHandle rebind() {
149         return BoundMethodHandle.makeReinvoker(this);
150     }
151 
152     @Override
153     MethodHandle copyWith(MethodType mt, LambdaForm lf) {
154         assert(this.getClass() == DirectMethodHandle.class);  // must override in subclasses
155         return new DirectMethodHandle(mt, lf, member, crackable);
156     }
157 
158     @Override
159     MethodHandle viewAsType(MethodType newType, boolean strict) {
160         // No actual conversions, just a new view of the same method.
161         // However, we must not expose a DMH that is crackable into a
162         // MethodHandleInfo, so we return a cloned, uncrackable DMH
163         assert(viewAsTypeChecks(newType, strict));
164         assert(this.getClass() == DirectMethodHandle.class);  // must override in subclasses
165         return new DirectMethodHandle(newType, form, member, false);
166     }
167 
168     @Override
169     boolean isCrackable() {
170         return crackable;
171     }
172 
173     @Override
174     String internalProperties() {
175         return "\n& DMH.MN="+internalMemberName();
176     }
177 
178     //// Implementation methods.
179     @Override
180     @ForceInline
181     MemberName internalMemberName() {
182         return member;
183     }
184 
185     private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory();
186 
187     /**
188      * Create a LF which can invoke the given method.
189      * Cache and share this structure among all methods with
190      * the same basicType and refKind.
191      */
192     private static LambdaForm preparedLambdaForm(MemberName m, boolean adaptToSpecialIfc) {
193         assert(m.isInvocable()) : m;  // call preparedFieldLambdaForm instead
194         MethodType mtype = m.getInvocationType().basicType();
195         assert(!m.isMethodHandleInvoke()) : m;
196         // MemberName.getReferenceKind represents the JVM optimized form of the call
197         // as distinct from the "kind" passed to DMH.make which represents the original
198         // bytecode-equivalent request. Specifically private/final methods that use a direct
199         // call have getReferenceKind adapted to REF_invokeSpecial, even though the actual
200         // invocation mode may be invokevirtual or invokeinterface.
201         int which = switch (m.getReferenceKind()) {
202             case REF_invokeVirtual    -> LF_INVVIRTUAL;
203             case REF_invokeStatic     -> LF_INVSTATIC;
204             case REF_invokeSpecial    -> LF_INVSPECIAL;
205             case REF_invokeInterface  -> LF_INVINTERFACE;
206             case REF_newInvokeSpecial -> LF_NEWINVSPECIAL;
207             default -> throw new InternalError(m.toString());
208         };
209         if (which == LF_INVSTATIC && shouldBeInitialized(m)) {
210             // precompute the barrier-free version:
211             preparedLambdaForm(mtype, which);
212             which = LF_INVSTATIC_INIT;
213         }
214         if (which == LF_INVSPECIAL && adaptToSpecialIfc) {
215             which = LF_INVSPECIAL_IFC;
216         }
217         LambdaForm lform = preparedLambdaForm(mtype, which);
218         maybeCompile(lform, m);
219         assert(lform.methodType().dropParameterTypes(0, 1)
220                 .equals(m.getInvocationType().basicType()))
221                 : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType());
222         return lform;
223     }
224 
225     private static LambdaForm preparedLambdaForm(MemberName m) {
226         return preparedLambdaForm(m, false);
227     }
228 
229     private static LambdaForm preparedLambdaForm(MethodType mtype, int which) {
230         LambdaForm lform = mtype.form().cachedLambdaForm(which);
231         if (lform != null)  return lform;
232         lform = makePreparedLambdaForm(mtype, which);
233         return mtype.form().setCachedLambdaForm(which, lform);
234     }
235 
236     static LambdaForm makePreparedLambdaForm(MethodType mtype, int which) {
237         boolean needsInit = (which == LF_INVSTATIC_INIT);
238         boolean doesAlloc = (which == LF_NEWINVSPECIAL);
239         boolean needsReceiverCheck = (which == LF_INVINTERFACE ||
240                                       which == LF_INVSPECIAL_IFC);
241 
242         String linkerName;
243         LambdaForm.Kind kind;
244         switch (which) {
245         case LF_INVVIRTUAL:    linkerName = "linkToVirtual";   kind = DIRECT_INVOKE_VIRTUAL;     break;
246         case LF_INVSTATIC:     linkerName = "linkToStatic";    kind = DIRECT_INVOKE_STATIC;      break;
247         case LF_INVSTATIC_INIT:linkerName = "linkToStatic";    kind = DIRECT_INVOKE_STATIC_INIT; break;
248         case LF_INVSPECIAL_IFC:linkerName = "linkToSpecial";   kind = DIRECT_INVOKE_SPECIAL_IFC; break;
249         case LF_INVSPECIAL:    linkerName = "linkToSpecial";   kind = DIRECT_INVOKE_SPECIAL;     break;
250         case LF_INVINTERFACE:  linkerName = "linkToInterface"; kind = DIRECT_INVOKE_INTERFACE;   break;
251         case LF_NEWINVSPECIAL: linkerName = "linkToSpecial";   kind = DIRECT_NEW_INVOKE_SPECIAL; break;
252         default:  throw new InternalError("which="+which);
253         }
254 
255         MethodType mtypeWithArg = mtype.appendParameterTypes(MemberName.class);
256         if (doesAlloc)
257             mtypeWithArg = mtypeWithArg
258                     .insertParameterTypes(0, Object.class)  // insert newly allocated obj
259                     .changeReturnType(void.class);          // <init> returns void
260         MemberName linker = new MemberName(MethodHandle.class, linkerName, mtypeWithArg, REF_invokeStatic);
261         try {
262             linker = IMPL_NAMES.resolveOrFail(REF_invokeStatic, linker, null, LM_TRUSTED,
263                                               NoSuchMethodException.class);
264         } catch (ReflectiveOperationException ex) {
265             throw newInternalError(ex);
266         }
267         final int DMH_THIS    = 0;
268         final int ARG_BASE    = 1;
269         final int ARG_LIMIT   = ARG_BASE + mtype.parameterCount();
270         int nameCursor = ARG_LIMIT;
271         final int NEW_OBJ     = (doesAlloc ? nameCursor++ : -1);
272         final int GET_MEMBER  = nameCursor++;
273         final int CHECK_RECEIVER = (needsReceiverCheck ? nameCursor++ : -1);
274         final int LINKER_CALL = nameCursor++;
275         Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
276         assert(names.length == nameCursor);
277         if (doesAlloc) {
278             // names = { argx,y,z,... new C, init method }
279             names[NEW_OBJ] = new Name(getFunction(NF_allocateInstance), names[DMH_THIS]);
280             names[GET_MEMBER] = new Name(getFunction(NF_constructorMethod), names[DMH_THIS]);
281         } else if (needsInit) {
282             names[GET_MEMBER] = new Name(getFunction(NF_internalMemberNameEnsureInit), names[DMH_THIS]);
283         } else {
284             names[GET_MEMBER] = new Name(getFunction(NF_internalMemberName), names[DMH_THIS]);
285         }
286         assert(findDirectMethodHandle(names[GET_MEMBER]) == names[DMH_THIS]);
287         Object[] outArgs = Arrays.copyOfRange(names, ARG_BASE, GET_MEMBER+1, Object[].class);
288         if (needsReceiverCheck) {
289             names[CHECK_RECEIVER] = new Name(getFunction(NF_checkReceiver), names[DMH_THIS], names[ARG_BASE]);
290             outArgs[0] = names[CHECK_RECEIVER];
291         }
292         assert(outArgs[outArgs.length-1] == names[GET_MEMBER]);  // look, shifted args!
293         int result = LAST_RESULT;
294         if (doesAlloc) {
295             assert(outArgs[outArgs.length-2] == names[NEW_OBJ]);  // got to move this one
296             System.arraycopy(outArgs, 0, outArgs, 1, outArgs.length-2);
297             outArgs[0] = names[NEW_OBJ];
298             result = NEW_OBJ;
299         }
300         names[LINKER_CALL] = new Name(linker, outArgs);
301         LambdaForm lform = new LambdaForm(ARG_LIMIT, names, result, kind);
302 
303         // This is a tricky bit of code.  Don't send it through the LF interpreter.
304         lform.compileToBytecode();
305         return lform;
306     }
307 
308     /* assert */ static Object findDirectMethodHandle(Name name) {
309         if (name.function.equals(getFunction(NF_internalMemberName)) ||
310             name.function.equals(getFunction(NF_internalMemberNameEnsureInit)) ||
311             name.function.equals(getFunction(NF_constructorMethod))) {
312             assert(name.arguments.length == 1);
313             return name.arguments[0];
314         }
315         return null;
316     }
317 
318     private static void maybeCompile(LambdaForm lform, MemberName m) {
319         if (lform.vmentry == null && VerifyAccess.isSamePackage(m.getDeclaringClass(), MethodHandle.class))
320             // Help along bootstrapping...
321             lform.compileToBytecode();
322     }
323 
324     /** Static wrapper for DirectMethodHandle.internalMemberName. */
325     @ForceInline
326     /*non-public*/
327     static Object internalMemberName(Object mh) {
328         return ((DirectMethodHandle)mh).member;
329     }
330 
331     /** Static wrapper for DirectMethodHandle.internalMemberName.
332      * This one also forces initialization.
333      */
334     /*non-public*/
335     static Object internalMemberNameEnsureInit(Object mh) {
336         DirectMethodHandle dmh = (DirectMethodHandle)mh;
337         dmh.ensureInitialized();
338         return dmh.member;
339     }
340 
341     /*non-public*/
342     static boolean shouldBeInitialized(MemberName member) {
343         switch (member.getReferenceKind()) {
344         case REF_invokeStatic:
345         case REF_getStatic:
346         case REF_putStatic:
347         case REF_newInvokeSpecial:
348             break;
349         default:
350             // No need to initialize the class on this kind of member.
351             return false;
352         }
353         Class<?> cls = member.getDeclaringClass();
354         if (cls == ValueConversions.class ||
355             cls == MethodHandleImpl.class ||
356             cls == Invokers.class) {
357             // These guys have lots of <clinit> DMH creation but we know
358             // the MHs will not be used until the system is booted.
359             return false;
360         }
361         if (VerifyAccess.isSamePackage(MethodHandle.class, cls) ||
362             VerifyAccess.isSamePackage(ValueConversions.class, cls)) {
363             // It is a system class.  It is probably in the process of
364             // being initialized, but we will help it along just to be safe.
365             UNSAFE.ensureClassInitialized(cls);
366             return false;
367         }
368         return UNSAFE.shouldBeInitialized(cls);
369     }
370 
371     private void ensureInitialized() {
372         if (checkInitialized(member)) {
373             // The coast is clear.  Delete the <clinit> barrier.
374             updateForm(new Function<>() {
375                 public LambdaForm apply(LambdaForm oldForm) {
376                     return (member.isField() ? preparedFieldLambdaForm(member)
377                                              : preparedLambdaForm(member));
378                 }
379             });
380         }
381     }
382     private static boolean checkInitialized(MemberName member) {
383         Class<?> defc = member.getDeclaringClass();
384         UNSAFE.ensureClassInitialized(defc);
385         // Once we get here either defc was fully initialized by another thread, or
386         // defc was already being initialized by the current thread. In the latter case
387         // the barrier must remain. We can detect this simply by checking if initialization
388         // is still needed.
389         return !UNSAFE.shouldBeInitialized(defc);
390     }
391 
392     /*non-public*/
393     static void ensureInitialized(Object mh) {
394         ((DirectMethodHandle)mh).ensureInitialized();
395     }
396 
397     /** This subclass represents invokespecial instructions. */
398     static class Special extends DirectMethodHandle {
399         private final Class<?> caller;
400         private Special(MethodType mtype, LambdaForm form, MemberName member, boolean crackable, Class<?> caller) {
401             super(mtype, form, member, crackable);
402             this.caller = caller;
403         }
404         @Override
405         boolean isInvokeSpecial() {
406             return true;
407         }
408         @Override
409         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
410             return new Special(mt, lf, member, crackable, caller);
411         }
412         @Override
413         MethodHandle viewAsType(MethodType newType, boolean strict) {
414             assert(viewAsTypeChecks(newType, strict));
415             return new Special(newType, form, member, false, caller);
416         }
417         Object checkReceiver(Object recv) {
418             if (!caller.isInstance(recv)) {
419                 String msg = String.format("Receiver class %s is not a subclass of caller class %s",
420                                            recv.getClass().getName(), caller.getName());
421                 throw new IncompatibleClassChangeError(msg);
422             }
423             return recv;
424         }
425     }
426 
427     /** This subclass represents invokeinterface instructions. */
428     static class Interface extends DirectMethodHandle {
429         private final Class<?> refc;
430         private Interface(MethodType mtype, LambdaForm form, MemberName member, boolean crackable, Class<?> refc) {
431             super(mtype, form, member, crackable);
432             assert(refc.isInterface()) : refc;
433             this.refc = refc;
434         }
435         @Override
436         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
437             return new Interface(mt, lf, member, crackable, refc);
438         }
439         @Override
440         MethodHandle viewAsType(MethodType newType, boolean strict) {
441             assert(viewAsTypeChecks(newType, strict));
442             return new Interface(newType, form, member, false, refc);
443         }
444         @Override
445         Object checkReceiver(Object recv) {
446             if (!refc.isInstance(recv)) {
447                 String msg = String.format("Receiver class %s does not implement the requested interface %s",
448                                            recv.getClass().getName(), refc.getName());
449                 throw new IncompatibleClassChangeError(msg);
450             }
451             return recv;
452         }
453     }
454 
455     /** Used for interface receiver type checks, by Interface and Special modes. */
456     Object checkReceiver(Object recv) {
457         throw new InternalError("Should only be invoked on a subclass");
458     }
459 
460     /** This subclass handles constructor references. */
461     static class Constructor extends DirectMethodHandle {
462         final MemberName initMethod;
463         final Class<?>   instanceClass;
464 
465         private Constructor(MethodType mtype, LambdaForm form, MemberName constructor,
466                             boolean crackable, MemberName initMethod, Class<?> instanceClass) {
467             super(mtype, form, constructor, crackable);
468             this.initMethod = initMethod;
469             this.instanceClass = instanceClass;
470             assert(initMethod.isResolved());
471         }
472         @Override
473         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
474             return new Constructor(mt, lf, member, crackable, initMethod, instanceClass);
475         }
476         @Override
477         MethodHandle viewAsType(MethodType newType, boolean strict) {
478             assert(viewAsTypeChecks(newType, strict));
479             return new Constructor(newType, form, member, false, initMethod, instanceClass);
480         }
481     }
482 
483     /*non-public*/
484     static Object constructorMethod(Object mh) {
485         Constructor dmh = (Constructor)mh;
486         return dmh.initMethod;
487     }
488 
489     /*non-public*/
490     static Object allocateInstance(Object mh) throws InstantiationException {
491         Constructor dmh = (Constructor)mh;
492         return UNSAFE.allocateInstance(dmh.instanceClass);
493     }
494 
495     /** This subclass handles non-static field references. */
496     static class Accessor extends DirectMethodHandle {
497         final Class<?> fieldType;
498         final int      fieldOffset;
499         private Accessor(MethodType mtype, LambdaForm form, MemberName member,
500                          boolean crackable, int fieldOffset) {
501             super(mtype, form, member, crackable);
502             this.fieldType   = member.getFieldType();
503             this.fieldOffset = fieldOffset;
504         }
505 
506         @Override Object checkCast(Object obj) {
507             return fieldType.cast(obj);
508         }
509         @Override
510         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
511             return new Accessor(mt, lf, member, crackable, fieldOffset);
512         }
513         @Override
514         MethodHandle viewAsType(MethodType newType, boolean strict) {
515             assert(viewAsTypeChecks(newType, strict));
516             return new Accessor(newType, form, member, false, fieldOffset);
517         }
518     }
519 
520     @ForceInline
521     /*non-public*/
522     static long fieldOffset(Object accessorObj) {
523         // Note: We return a long because that is what Unsafe.getObject likes.
524         // We store a plain int because it is more compact.
525         return ((Accessor)accessorObj).fieldOffset;
526     }
527 
528     @ForceInline
529     /*non-public*/
530     static Object checkBase(Object obj) {
531         // Note that the object's class has already been verified,
532         // since the parameter type of the Accessor method handle
533         // is either member.getDeclaringClass or a subclass.
534         // This was verified in DirectMethodHandle.make.
535         // Therefore, the only remaining check is for null.
536         // Since this check is *not* guaranteed by Unsafe.getInt
537         // and its siblings, we need to make an explicit one here.
538         return Objects.requireNonNull(obj);
539     }
540 
541     /** This subclass handles static field references. */
542     static class StaticAccessor extends DirectMethodHandle {
543         private final Class<?> fieldType;
544         private final Object   staticBase;
545         private final long     staticOffset;
546 
547         private StaticAccessor(MethodType mtype, LambdaForm form, MemberName member,
548                                boolean crackable, Object staticBase, long staticOffset) {
549             super(mtype, form, member, crackable);
550             this.fieldType    = member.getFieldType();
551             this.staticBase   = staticBase;
552             this.staticOffset = staticOffset;
553         }
554 
555         @Override Object checkCast(Object obj) {
556             return fieldType.cast(obj);
557         }
558         @Override
559         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
560             return new StaticAccessor(mt, lf, member, crackable, staticBase, staticOffset);
561         }
562         @Override
563         MethodHandle viewAsType(MethodType newType, boolean strict) {
564             assert(viewAsTypeChecks(newType, strict));
565             return new StaticAccessor(newType, form, member, false, staticBase, staticOffset);
566         }
567     }
568 
569     @ForceInline
570     /*non-public*/
571     static Object nullCheck(Object obj) {
572         return Objects.requireNonNull(obj);
573     }
574 
575     @ForceInline
576     /*non-public*/
577     static Object staticBase(Object accessorObj) {
578         return ((StaticAccessor)accessorObj).staticBase;
579     }
580 
581     @ForceInline
582     /*non-public*/
583     static long staticOffset(Object accessorObj) {
584         return ((StaticAccessor)accessorObj).staticOffset;
585     }
586 
587     @ForceInline
588     /*non-public*/
589     static Object checkCast(Object mh, Object obj) {
590         return ((DirectMethodHandle) mh).checkCast(obj);
591     }
592 
593     Object checkCast(Object obj) {
594         return member.getReturnType().cast(obj);
595     }
596 
597     // Caching machinery for field accessors:
598     static final byte
599             AF_GETFIELD        = 0,
600             AF_PUTFIELD        = 1,
601             AF_GETSTATIC       = 2,
602             AF_PUTSTATIC       = 3,
603             AF_GETSTATIC_INIT  = 4,
604             AF_PUTSTATIC_INIT  = 5,
605             AF_LIMIT           = 6;
606     // Enumerate the different field kinds using Wrapper,
607     // with an extra case added for checked references.
608     static final int
609             FT_LAST_WRAPPER    = Wrapper.COUNT-1,
610             FT_UNCHECKED_REF   = Wrapper.OBJECT.ordinal(),
611             FT_CHECKED_REF     = FT_LAST_WRAPPER+1,
612             FT_LIMIT           = FT_LAST_WRAPPER+2;
613     private static int afIndex(byte formOp, boolean isVolatile, int ftypeKind) {
614         return ((formOp * FT_LIMIT * 2)
615                 + (isVolatile ? FT_LIMIT : 0)
616                 + ftypeKind);
617     }
618     @Stable
619     private static final LambdaForm[] ACCESSOR_FORMS
620             = new LambdaForm[afIndex(AF_LIMIT, false, 0)];
621     static int ftypeKind(Class<?> ftype) {
622         if (ftype.isPrimitive())
623             return Wrapper.forPrimitiveType(ftype).ordinal();
624         else if (VerifyType.isNullReferenceConversion(Object.class, ftype))
625             return FT_UNCHECKED_REF;
626         else
627             return FT_CHECKED_REF;
628     }
629 
630     /**
631      * Create a LF which can access the given field.
632      * Cache and share this structure among all fields with
633      * the same basicType and refKind.
634      */
635     private static LambdaForm preparedFieldLambdaForm(MemberName m) {
636         Class<?> ftype = m.getFieldType();
637         boolean isVolatile = m.isVolatile();
638         byte formOp = switch (m.getReferenceKind()) {
639             case REF_getField  -> AF_GETFIELD;
640             case REF_putField  -> AF_PUTFIELD;
641             case REF_getStatic -> AF_GETSTATIC;
642             case REF_putStatic -> AF_PUTSTATIC;
643             default -> throw new InternalError(m.toString());
644         };
645         if (shouldBeInitialized(m)) {
646             // precompute the barrier-free version:
647             preparedFieldLambdaForm(formOp, isVolatile, ftype);
648             assert((AF_GETSTATIC_INIT - AF_GETSTATIC) ==
649                    (AF_PUTSTATIC_INIT - AF_PUTSTATIC));
650             formOp += (AF_GETSTATIC_INIT - AF_GETSTATIC);
651         }
652         LambdaForm lform = preparedFieldLambdaForm(formOp, isVolatile, ftype);
653         maybeCompile(lform, m);
654         assert(lform.methodType().dropParameterTypes(0, 1)
655                 .equals(m.getInvocationType().basicType()))
656                 : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType());
657         return lform;
658     }
659     private static LambdaForm preparedFieldLambdaForm(byte formOp, boolean isVolatile, Class<?> ftype) {
660         int ftypeKind = ftypeKind(ftype);
661         int afIndex = afIndex(formOp, isVolatile, ftypeKind);
662         LambdaForm lform = ACCESSOR_FORMS[afIndex];
663         if (lform != null)  return lform;
664         lform = makePreparedFieldLambdaForm(formOp, isVolatile, ftypeKind);
665         ACCESSOR_FORMS[afIndex] = lform;  // don't bother with a CAS
666         return lform;
667     }
668 
669     private static final Wrapper[] ALL_WRAPPERS = Wrapper.values();
670 
671     private static Kind getFieldKind(boolean isGetter, boolean isVolatile, Wrapper wrapper) {
672         if (isGetter) {
673             if (isVolatile) {
674                 switch (wrapper) {
675                     case BOOLEAN: return GET_BOOLEAN_VOLATILE;
676                     case BYTE:    return GET_BYTE_VOLATILE;
677                     case SHORT:   return GET_SHORT_VOLATILE;
678                     case CHAR:    return GET_CHAR_VOLATILE;
679                     case INT:     return GET_INT_VOLATILE;
680                     case LONG:    return GET_LONG_VOLATILE;
681                     case FLOAT:   return GET_FLOAT_VOLATILE;
682                     case DOUBLE:  return GET_DOUBLE_VOLATILE;
683                     case OBJECT:  return GET_REFERENCE_VOLATILE;
684                 }
685             } else {
686                 switch (wrapper) {
687                     case BOOLEAN: return GET_BOOLEAN;
688                     case BYTE:    return GET_BYTE;
689                     case SHORT:   return GET_SHORT;
690                     case CHAR:    return GET_CHAR;
691                     case INT:     return GET_INT;
692                     case LONG:    return GET_LONG;
693                     case FLOAT:   return GET_FLOAT;
694                     case DOUBLE:  return GET_DOUBLE;
695                     case OBJECT:  return GET_REFERENCE;
696                 }
697             }
698         } else {
699             if (isVolatile) {
700                 switch (wrapper) {
701                     case BOOLEAN: return PUT_BOOLEAN_VOLATILE;
702                     case BYTE:    return PUT_BYTE_VOLATILE;
703                     case SHORT:   return PUT_SHORT_VOLATILE;
704                     case CHAR:    return PUT_CHAR_VOLATILE;
705                     case INT:     return PUT_INT_VOLATILE;
706                     case LONG:    return PUT_LONG_VOLATILE;
707                     case FLOAT:   return PUT_FLOAT_VOLATILE;
708                     case DOUBLE:  return PUT_DOUBLE_VOLATILE;
709                     case OBJECT:  return PUT_REFERENCE_VOLATILE;
710                 }
711             } else {
712                 switch (wrapper) {
713                     case BOOLEAN: return PUT_BOOLEAN;
714                     case BYTE:    return PUT_BYTE;
715                     case SHORT:   return PUT_SHORT;
716                     case CHAR:    return PUT_CHAR;
717                     case INT:     return PUT_INT;
718                     case LONG:    return PUT_LONG;
719                     case FLOAT:   return PUT_FLOAT;
720                     case DOUBLE:  return PUT_DOUBLE;
721                     case OBJECT:  return PUT_REFERENCE;
722                 }
723             }
724         }
725         throw new AssertionError("Invalid arguments");
726     }
727 
728     static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, int ftypeKind) {
729         boolean isGetter  = (formOp & 1) == (AF_GETFIELD & 1);
730         boolean isStatic  = (formOp >= AF_GETSTATIC);
731         boolean needsInit = (formOp >= AF_GETSTATIC_INIT);
732         boolean needsCast = (ftypeKind == FT_CHECKED_REF);
733         Wrapper fw = (needsCast ? Wrapper.OBJECT : ALL_WRAPPERS[ftypeKind]);
734         Class<?> ft = fw.primitiveType();
735         assert(ftypeKind(needsCast ? String.class : ft) == ftypeKind);
736 
737         // getObject, putIntVolatile, etc.
738         Kind kind = getFieldKind(isGetter, isVolatile, fw);
739 
740         MethodType linkerType;
741         if (isGetter)
742             linkerType = MethodType.methodType(ft, Object.class, long.class);
743         else
744             linkerType = MethodType.methodType(void.class, Object.class, long.class, ft);
745         MemberName linker = new MemberName(Unsafe.class, kind.methodName, linkerType, REF_invokeVirtual);
746         try {
747             linker = IMPL_NAMES.resolveOrFail(REF_invokeVirtual, linker, null, LM_TRUSTED,
748                                               NoSuchMethodException.class);
749         } catch (ReflectiveOperationException ex) {
750             throw newInternalError(ex);
751         }
752 
753         // What is the external type of the lambda form?
754         MethodType mtype;
755         if (isGetter)
756             mtype = MethodType.methodType(ft);
757         else
758             mtype = MethodType.methodType(void.class, ft);
759         mtype = mtype.basicType();  // erase short to int, etc.
760         if (!isStatic)
761             mtype = mtype.insertParameterTypes(0, Object.class);
762         final int DMH_THIS  = 0;
763         final int ARG_BASE  = 1;
764         final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
765         // if this is for non-static access, the base pointer is stored at this index:
766         final int OBJ_BASE  = isStatic ? -1 : ARG_BASE;
767         // if this is for write access, the value to be written is stored at this index:
768         final int SET_VALUE  = isGetter ? -1 : ARG_LIMIT - 1;
769         int nameCursor = ARG_LIMIT;
770         final int F_HOLDER  = (isStatic ? nameCursor++ : -1);  // static base if any
771         final int F_OFFSET  = nameCursor++;  // Either static offset or field offset.
772         final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1);
773         final int U_HOLDER  = nameCursor++;  // UNSAFE holder
774         final int INIT_BAR  = (needsInit ? nameCursor++ : -1);
775         final int PRE_CAST  = (needsCast && !isGetter ? nameCursor++ : -1);
776         final int LINKER_CALL = nameCursor++;
777         final int POST_CAST = (needsCast && isGetter ? nameCursor++ : -1);
778         final int RESULT    = nameCursor-1;  // either the call or the cast
779         Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
780         if (needsInit)
781             names[INIT_BAR] = new Name(getFunction(NF_ensureInitialized), names[DMH_THIS]);
782         if (needsCast && !isGetter)
783             names[PRE_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[SET_VALUE]);
784         Object[] outArgs = new Object[1 + linkerType.parameterCount()];
785         assert(outArgs.length == (isGetter ? 3 : 4));
786         outArgs[0] = names[U_HOLDER] = new Name(getFunction(NF_UNSAFE));
787         if (isStatic) {
788             outArgs[1] = names[F_HOLDER]  = new Name(getFunction(NF_staticBase), names[DMH_THIS]);
789             outArgs[2] = names[F_OFFSET]  = new Name(getFunction(NF_staticOffset), names[DMH_THIS]);
790         } else {
791             outArgs[1] = names[OBJ_CHECK] = new Name(getFunction(NF_checkBase), names[OBJ_BASE]);
792             outArgs[2] = names[F_OFFSET]  = new Name(getFunction(NF_fieldOffset), names[DMH_THIS]);
793         }
794         if (!isGetter) {
795             outArgs[3] = (needsCast ? names[PRE_CAST] : names[SET_VALUE]);
796         }
797         for (Object a : outArgs)  assert(a != null);
798         names[LINKER_CALL] = new Name(linker, outArgs);
799         if (needsCast && isGetter)
800             names[POST_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[LINKER_CALL]);
801         for (Name n : names)  assert(n != null);
802 
803         LambdaForm form;
804         if (needsCast || needsInit) {
805             // can't use the pre-generated form when casting and/or initializing
806             form = new LambdaForm(ARG_LIMIT, names, RESULT);
807         } else {
808             form = new LambdaForm(ARG_LIMIT, names, RESULT, kind);
809         }
810 
811         if (LambdaForm.debugNames()) {
812             // add some detail to the lambdaForm debugname,
813             // significant only for debugging
814             StringBuilder nameBuilder = new StringBuilder(kind.methodName);
815             if (isStatic) {
816                 nameBuilder.append("Static");
817             } else {
818                 nameBuilder.append("Field");
819             }
820             if (needsCast) {
821                 nameBuilder.append("Cast");
822             }
823             if (needsInit) {
824                 nameBuilder.append("Init");
825             }
826             LambdaForm.associateWithDebugName(form, nameBuilder.toString());
827         }
828         return form;
829     }
830 
831     /**
832      * Pre-initialized NamedFunctions for bootstrapping purposes.
833      */
834     static final byte NF_internalMemberName = 0,
835             NF_internalMemberNameEnsureInit = 1,
836             NF_ensureInitialized = 2,
837             NF_fieldOffset = 3,
838             NF_checkBase = 4,
839             NF_staticBase = 5,
840             NF_staticOffset = 6,
841             NF_checkCast = 7,
842             NF_allocateInstance = 8,
843             NF_constructorMethod = 9,
844             NF_UNSAFE = 10,
845             NF_checkReceiver = 11,
846             NF_LIMIT = 12;
847 
848     private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT];
849 
850     private static NamedFunction getFunction(byte func) {
851         NamedFunction nf = NFS[func];
852         if (nf != null) {
853             return nf;
854         }
855         // Each nf must be statically invocable or we get tied up in our bootstraps.
856         nf = NFS[func] = createFunction(func);
857         assert(InvokerBytecodeGenerator.isStaticallyInvocable(nf));
858         return nf;
859     }
860 
861     private static final MethodType OBJ_OBJ_TYPE = MethodType.methodType(Object.class, Object.class);
862 
863     private static final MethodType LONG_OBJ_TYPE = MethodType.methodType(long.class, Object.class);
864 
865     private static NamedFunction createFunction(byte func) {
866         try {
867             switch (func) {
868                 case NF_internalMemberName:
869                     return getNamedFunction("internalMemberName", OBJ_OBJ_TYPE);
870                 case NF_internalMemberNameEnsureInit:
871                     return getNamedFunction("internalMemberNameEnsureInit", OBJ_OBJ_TYPE);
872                 case NF_ensureInitialized:
873                     return getNamedFunction("ensureInitialized", MethodType.methodType(void.class, Object.class));
874                 case NF_fieldOffset:
875                     return getNamedFunction("fieldOffset", LONG_OBJ_TYPE);
876                 case NF_checkBase:
877                     return getNamedFunction("checkBase", OBJ_OBJ_TYPE);
878                 case NF_staticBase:
879                     return getNamedFunction("staticBase", OBJ_OBJ_TYPE);
880                 case NF_staticOffset:
881                     return getNamedFunction("staticOffset", LONG_OBJ_TYPE);
882                 case NF_checkCast:
883                     return getNamedFunction("checkCast", MethodType.methodType(Object.class, Object.class, Object.class));
884                 case NF_allocateInstance:
885                     return getNamedFunction("allocateInstance", OBJ_OBJ_TYPE);
886                 case NF_constructorMethod:
887                     return getNamedFunction("constructorMethod", OBJ_OBJ_TYPE);
888                 case NF_UNSAFE:
889                     MemberName member = new MemberName(MethodHandleStatics.class, "UNSAFE", Unsafe.class, REF_getField);
890                     return new NamedFunction(
891                             MemberName.getFactory().resolveOrFail(REF_getField, member,
892                                                                   DirectMethodHandle.class, LM_TRUSTED,
893                                                                   NoSuchMethodException.class));
894                 case NF_checkReceiver:
895                     member = new MemberName(DirectMethodHandle.class, "checkReceiver", OBJ_OBJ_TYPE, REF_invokeVirtual);
896                     return new NamedFunction(
897                             MemberName.getFactory().resolveOrFail(REF_invokeVirtual, member,
898                                                                   DirectMethodHandle.class, LM_TRUSTED,
899                                                                   NoSuchMethodException.class));
900                 default:
901                     throw newInternalError("Unknown function: " + func);
902             }
903         } catch (ReflectiveOperationException ex) {
904             throw newInternalError(ex);
905         }
906     }
907 
908     private static NamedFunction getNamedFunction(String name, MethodType type)
909         throws ReflectiveOperationException
910     {
911         MemberName member = new MemberName(DirectMethodHandle.class, name, type, REF_invokeStatic);
912         return new NamedFunction(
913                 MemberName.getFactory().resolveOrFail(REF_invokeStatic, member,
914                                                       DirectMethodHandle.class, LM_TRUSTED,
915                                                       NoSuchMethodException.class));
916     }
917 
918     static {
919         // The Holder class will contain pre-generated DirectMethodHandles resolved
920         // speculatively using MemberName.getFactory().resolveOrNull. However, that
921         // doesn't initialize the class, which subtly breaks inlining etc. By forcing
922         // initialization of the Holder class we avoid these issues.
923         UNSAFE.ensureClassInitialized(Holder.class);
924     }
925 
926     /* Placeholder class for DirectMethodHandles generated ahead of time */
927     final class Holder {}
928 }