1 /*
   2  * Copyright (c) 1997, 2023, 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 #include "precompiled.hpp"
  26 #include "cds/cdsConfig.hpp"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/moduleEntry.hpp"
  29 #include "classfile/packageEntry.hpp"
  30 #include "classfile/stringTable.hpp"
  31 #include "classfile/verifier.hpp"
  32 #include "classfile/vmClasses.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "interpreter/linkResolver.hpp"
  35 #include "jvm.h"
  36 #include "logging/log.hpp"
  37 #include "memory/oopFactory.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "memory/universe.hpp"
  40 #include "oops/inlineKlass.inline.hpp"
  41 #include "oops/instanceKlass.inline.hpp"
  42 #include "oops/klass.inline.hpp"
  43 #include "oops/objArrayKlass.hpp"
  44 #include "oops/objArrayOop.inline.hpp"
  45 #include "oops/oop.inline.hpp"
  46 #include "oops/typeArrayOop.inline.hpp"
  47 #include "prims/jvmtiExport.hpp"
  48 #include "runtime/fieldDescriptor.inline.hpp"
  49 #include "runtime/handles.inline.hpp"
  50 #include "runtime/javaCalls.hpp"
  51 #include "runtime/javaThread.hpp"
  52 #include "runtime/reflection.hpp"
  53 #include "runtime/reflectionUtils.hpp"
  54 #include "runtime/signature.hpp"
  55 #include "runtime/vframe.inline.hpp"
  56 #include "utilities/formatBuffer.hpp"
  57 #include "utilities/globalDefinitions.hpp"
  58 
  59 static void trace_class_resolution(oop mirror) {
  60   if (mirror == nullptr || java_lang_Class::is_primitive(mirror)) {
  61     return;
  62   }
  63   Klass* to_class = java_lang_Class::as_Klass(mirror);
  64   ResourceMark rm;
  65   int line_number = -1;
  66   const char * source_file = nullptr;
  67   Klass* caller = nullptr;
  68   JavaThread* jthread = JavaThread::current();
  69   if (jthread->has_last_Java_frame()) {
  70     vframeStream vfst(jthread);
  71     // skip over any frames belonging to java.lang.Class
  72     while (!vfst.at_end() &&
  73            vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class()) {
  74       vfst.next();
  75     }
  76     if (!vfst.at_end()) {
  77       // this frame is a likely suspect
  78       caller = vfst.method()->method_holder();
  79       line_number = vfst.method()->line_number_from_bci(vfst.bci());
  80       Symbol* s = vfst.method()->method_holder()->source_file_name();
  81       if (s != nullptr) {
  82         source_file = s->as_C_string();
  83       }
  84     }
  85   }
  86   if (caller != nullptr) {
  87     const char * from = caller->external_name();
  88     const char * to = to_class->external_name();
  89     // print in a single call to reduce interleaving between threads
  90     if (source_file != nullptr) {
  91       log_debug(class, resolve)("%s %s %s:%d (reflection)", from, to, source_file, line_number);
  92     } else {
  93       log_debug(class, resolve)("%s %s (reflection)", from, to);
  94     }
  95   }
  96 }
  97 
  98 
  99 oop Reflection::box(jvalue* value, BasicType type, TRAPS) {
 100   if (type == T_VOID) {
 101     return nullptr;
 102   }
 103   if (is_reference_type(type)) {
 104     // regular objects are not boxed
 105     return cast_to_oop(value->l);
 106   }
 107   oop result = java_lang_boxing_object::create(type, value, CHECK_NULL);
 108   if (result == nullptr) {
 109     THROW_(vmSymbols::java_lang_IllegalArgumentException(), result);
 110   }
 111   return result;
 112 }
 113 
 114 
 115 BasicType Reflection::unbox_for_primitive(oop box, jvalue* value, TRAPS) {
 116   if (box == nullptr) {
 117     THROW_(vmSymbols::java_lang_IllegalArgumentException(), T_ILLEGAL);
 118   }
 119   return java_lang_boxing_object::get_value(box, value);
 120 }
 121 
 122 BasicType Reflection::unbox_for_regular_object(oop box, jvalue* value) {
 123   // Note:  box is really the unboxed oop.  It might even be a Short, etc.!
 124   value->l = cast_from_oop<jobject>(box);
 125   return T_OBJECT;
 126 }
 127 
 128 
 129 void Reflection::widen(jvalue* value, BasicType current_type, BasicType wide_type, TRAPS) {
 130   assert(wide_type != current_type, "widen should not be called with identical types");
 131   switch (wide_type) {
 132     case T_BOOLEAN:
 133     case T_BYTE:
 134     case T_CHAR:
 135       break;  // fail
 136     case T_SHORT:
 137       switch (current_type) {
 138         case T_BYTE:
 139           value->s = (jshort) value->b;
 140           return;
 141         default:
 142           break;
 143       }
 144       break;  // fail
 145     case T_INT:
 146       switch (current_type) {
 147         case T_BYTE:
 148           value->i = (jint) value->b;
 149           return;
 150         case T_CHAR:
 151           value->i = (jint) value->c;
 152           return;
 153         case T_SHORT:
 154           value->i = (jint) value->s;
 155           return;
 156         default:
 157           break;
 158       }
 159       break;  // fail
 160     case T_LONG:
 161       switch (current_type) {
 162         case T_BYTE:
 163           value->j = (jlong) value->b;
 164           return;
 165         case T_CHAR:
 166           value->j = (jlong) value->c;
 167           return;
 168         case T_SHORT:
 169           value->j = (jlong) value->s;
 170           return;
 171         case T_INT:
 172           value->j = (jlong) value->i;
 173           return;
 174         default:
 175           break;
 176       }
 177       break;  // fail
 178     case T_FLOAT:
 179       switch (current_type) {
 180         case T_BYTE:
 181           value->f = (jfloat) value->b;
 182           return;
 183         case T_CHAR:
 184           value->f = (jfloat) value->c;
 185           return;
 186         case T_SHORT:
 187           value->f = (jfloat) value->s;
 188           return;
 189         case T_INT:
 190           value->f = (jfloat) value->i;
 191           return;
 192         case T_LONG:
 193           value->f = (jfloat) value->j;
 194           return;
 195         default:
 196           break;
 197       }
 198       break;  // fail
 199     case T_DOUBLE:
 200       switch (current_type) {
 201         case T_BYTE:
 202           value->d = (jdouble) value->b;
 203           return;
 204         case T_CHAR:
 205           value->d = (jdouble) value->c;
 206           return;
 207         case T_SHORT:
 208           value->d = (jdouble) value->s;
 209           return;
 210         case T_INT:
 211           value->d = (jdouble) value->i;
 212           return;
 213         case T_FLOAT:
 214           value->d = (jdouble) value->f;
 215           return;
 216         case T_LONG:
 217           value->d = (jdouble) value->j;
 218           return;
 219         default:
 220           break;
 221       }
 222       break;  // fail
 223     default:
 224       break;  // fail
 225   }
 226   THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
 227 }
 228 
 229 
 230 BasicType Reflection::array_get(jvalue* value, arrayOop a, int index, TRAPS) {
 231   if (!a->is_within_bounds(index)) {
 232     THROW_(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), T_ILLEGAL);
 233   }
 234   if (a->is_objArray()) {
 235     value->l = cast_from_oop<jobject>(objArrayOop(a)->obj_at(index));
 236     return T_OBJECT;
 237   } else {
 238     assert(a->is_typeArray(), "just checking");
 239     BasicType type = TypeArrayKlass::cast(a->klass())->element_type();
 240     switch (type) {
 241       case T_BOOLEAN:
 242         value->z = typeArrayOop(a)->bool_at(index);
 243         break;
 244       case T_CHAR:
 245         value->c = typeArrayOop(a)->char_at(index);
 246         break;
 247       case T_FLOAT:
 248         value->f = typeArrayOop(a)->float_at(index);
 249         break;
 250       case T_DOUBLE:
 251         value->d = typeArrayOop(a)->double_at(index);
 252         break;
 253       case T_BYTE:
 254         value->b = typeArrayOop(a)->byte_at(index);
 255         break;
 256       case T_SHORT:
 257         value->s = typeArrayOop(a)->short_at(index);
 258         break;
 259       case T_INT:
 260         value->i = typeArrayOop(a)->int_at(index);
 261         break;
 262       case T_LONG:
 263         value->j = typeArrayOop(a)->long_at(index);
 264         break;
 265       default:
 266         return T_ILLEGAL;
 267     }
 268     return type;
 269   }
 270 }
 271 
 272 
 273 void Reflection::array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS) {
 274   if (!a->is_within_bounds(index)) {
 275     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 276   }
 277 
 278   if (a->is_objArray()) {
 279     if (value_type == T_OBJECT) {
 280       oop obj = cast_to_oop(value->l);
 281       if (a->is_null_free_array() && obj == nullptr) {
 282          THROW_MSG(vmSymbols::java_lang_NullPointerException(), "null-restricted array");
 283       }
 284 
 285       if (obj != nullptr) {
 286         Klass* element_klass = ObjArrayKlass::cast(a->klass())->element_klass();
 287         if (!obj->is_a(element_klass)) {
 288           THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "array element type mismatch");
 289         }
 290       }
 291       objArrayOop(a)->obj_at_put(index, obj);
 292     }
 293   } else {
 294     assert(a->is_typeArray(), "just checking");
 295     BasicType array_type = TypeArrayKlass::cast(a->klass())->element_type();
 296     if (array_type != value_type) {
 297       // The widen operation can potentially throw an exception, but cannot block,
 298       // so typeArrayOop a is safe if the call succeeds.
 299       widen(value, value_type, array_type, CHECK);
 300     }
 301     switch (array_type) {
 302       case T_BOOLEAN:
 303         typeArrayOop(a)->bool_at_put(index, value->z);
 304         break;
 305       case T_CHAR:
 306         typeArrayOop(a)->char_at_put(index, value->c);
 307         break;
 308       case T_FLOAT:
 309         typeArrayOop(a)->float_at_put(index, value->f);
 310         break;
 311       case T_DOUBLE:
 312         typeArrayOop(a)->double_at_put(index, value->d);
 313         break;
 314       case T_BYTE:
 315         typeArrayOop(a)->byte_at_put(index, value->b);
 316         break;
 317       case T_SHORT:
 318         typeArrayOop(a)->short_at_put(index, value->s);
 319         break;
 320       case T_INT:
 321         typeArrayOop(a)->int_at_put(index, value->i);
 322         break;
 323       case T_LONG:
 324         typeArrayOop(a)->long_at_put(index, value->j);
 325         break;
 326       default:
 327         THROW(vmSymbols::java_lang_IllegalArgumentException());
 328     }
 329   }
 330 }
 331 
 332 static Klass* basic_type_mirror_to_arrayklass(oop basic_type_mirror, TRAPS) {
 333   assert(java_lang_Class::is_primitive(basic_type_mirror), "just checking");
 334   BasicType type = java_lang_Class::primitive_type(basic_type_mirror);
 335   if (type == T_VOID) {
 336     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 337   }
 338   else {
 339     return Universe::typeArrayKlassObj(type);
 340   }
 341 }
 342 
 343 arrayOop Reflection::reflect_new_array(oop element_mirror, jint length, TRAPS) {
 344   if (element_mirror == nullptr) {
 345     THROW_0(vmSymbols::java_lang_NullPointerException());
 346   }
 347   if (length < 0) {
 348     THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length));
 349   }
 350   if (java_lang_Class::is_primitive(element_mirror)) {
 351     Klass* tak = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL);
 352     return TypeArrayKlass::cast(tak)->allocate(length, THREAD);
 353   } else {
 354     Klass* k = java_lang_Class::as_Klass(element_mirror);
 355     if (k->is_array_klass() && ArrayKlass::cast(k)->dimension() >= MAX_DIM) {
 356       THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 357     }
 358     return oopFactory::new_objArray(k, length, THREAD);
 359   }
 360 }
 361 
 362 
 363 arrayOop Reflection::reflect_new_multi_array(oop element_mirror, typeArrayOop dim_array, TRAPS) {
 364   assert(dim_array->is_typeArray(), "just checking");
 365   assert(TypeArrayKlass::cast(dim_array->klass())->element_type() == T_INT, "just checking");
 366 
 367   if (element_mirror == nullptr) {
 368     THROW_0(vmSymbols::java_lang_NullPointerException());
 369   }
 370 
 371   int len = dim_array->length();
 372   if (len <= 0 || len > MAX_DIM) {
 373     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 374   }
 375 
 376   jint dimensions[MAX_DIM];   // C array copy of intArrayOop
 377   for (int i = 0; i < len; i++) {
 378     int d = dim_array->int_at(i);
 379     if (d < 0) {
 380       THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", d));
 381     }
 382     dimensions[i] = d;
 383   }
 384 
 385   Klass* klass;
 386   int dim = len;
 387   if (java_lang_Class::is_primitive(element_mirror)) {
 388     klass = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL);
 389   } else {
 390     klass = java_lang_Class::as_Klass(element_mirror);
 391     if (klass->is_array_klass()) {
 392       int k_dim = ArrayKlass::cast(klass)->dimension();
 393       if (k_dim + len > MAX_DIM) {
 394         THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 395       }
 396       dim += k_dim;
 397     }
 398   }
 399   klass = klass->array_klass(dim, CHECK_NULL);
 400   oop obj = ArrayKlass::cast(klass)->multi_allocate(len, dimensions, CHECK_NULL);
 401   assert(obj->is_array(), "just checking");
 402   return arrayOop(obj);
 403 }
 404 
 405 
 406 static bool can_relax_access_check_for(const Klass* accessor,
 407                                        const Klass* accessee,
 408                                        bool classloader_only) {
 409 
 410   const InstanceKlass* accessor_ik = InstanceKlass::cast(accessor);
 411   const InstanceKlass* accessee_ik = InstanceKlass::cast(accessee);
 412 
 413   if (RelaxAccessControlCheck &&
 414     accessor_ik->major_version() < Verifier::NO_RELAX_ACCESS_CTRL_CHECK_VERSION &&
 415     accessee_ik->major_version() < Verifier::NO_RELAX_ACCESS_CTRL_CHECK_VERSION) {
 416     return classloader_only &&
 417       Verifier::relax_access_for(accessor_ik->class_loader()) &&
 418       accessor_ik->protection_domain() == accessee_ik->protection_domain() &&
 419       accessor_ik->class_loader() == accessee_ik->class_loader();
 420   }
 421 
 422   return false;
 423 }
 424 
 425 /*
 426     Type Accessibility check for public types: Callee Type T is accessible to Caller Type S if:
 427 
 428                         Callee T in             Callee T in package PT,
 429                         unnamed module          runtime module MT
 430  ------------------------------------------------------------------------------------------------
 431 
 432  Caller S in package     If MS is loose: YES      If same classloader/package (PS == PT): YES
 433  PS, runtime module MS   If MS can read T's       If same runtime module: (MS == MT): YES
 434                          unnamed module: YES
 435                                                   Else if (MS can read MT (establish readability) &&
 436                                                     ((MT exports PT to MS or to all modules) ||
 437                                                      (MT is open))): YES
 438 
 439  ------------------------------------------------------------------------------------------------
 440  Caller S in unnamed         YES                  Readability exists because unnamed module
 441  module UM                                            "reads" all modules
 442                                                   if (MT exports PT to UM or to all modules): YES
 443 
 444  ------------------------------------------------------------------------------------------------
 445 
 446  Note: a loose module is a module that can read all current and future unnamed modules.
 447 */
 448 Reflection::VerifyClassAccessResults Reflection::verify_class_access(
 449   const Klass* current_class, const InstanceKlass* new_class, bool classloader_only) {
 450 
 451   // Verify that current_class can access new_class.  If the classloader_only
 452   // flag is set, we automatically allow any accesses in which current_class
 453   // doesn't have a classloader.
 454   if ((current_class == nullptr) ||
 455       (current_class == new_class) ||
 456       is_same_class_package(current_class, new_class)) {
 457     return ACCESS_OK;
 458   }
 459   // Allow all accesses from jdk/internal/reflect/SerializationConstructorAccessorImpl subclasses to
 460   // succeed trivially.
 461   if (vmClasses::reflect_SerializationConstructorAccessorImpl_klass_is_loaded() &&
 462       current_class->is_subclass_of(vmClasses::reflect_SerializationConstructorAccessorImpl_klass())) {
 463     return ACCESS_OK;
 464   }
 465 
 466   // module boundaries
 467   if (new_class->is_public()) {
 468     // Ignore modules for -Xshare:dump because we do not have any package
 469     // or module information for modules other than java.base.
 470     if (CDSConfig::is_dumping_static_archive()) {
 471       return ACCESS_OK;
 472     }
 473 
 474     // Find the module entry for current_class, the accessor
 475     ModuleEntry* module_from = current_class->module();
 476     // Find the module entry for new_class, the accessee
 477     ModuleEntry* module_to = new_class->module();
 478 
 479     // both in same (possibly unnamed) module
 480     if (module_from == module_to) {
 481       return ACCESS_OK;
 482     }
 483 
 484     // Acceptable access to a type in an unnamed module. Note that since
 485     // unnamed modules can read all unnamed modules, this also handles the
 486     // case where module_from is also unnamed but in a different class loader.
 487     if (!module_to->is_named() &&
 488         (module_from->can_read_all_unnamed() || module_from->can_read(module_to))) {
 489       return ACCESS_OK;
 490     }
 491 
 492     // Establish readability, check if module_from is allowed to read module_to.
 493     if (!module_from->can_read(module_to)) {
 494       return MODULE_NOT_READABLE;
 495     }
 496 
 497     // Access is allowed if module_to is open, i.e. all its packages are unqualifiedly exported
 498     if (module_to->is_open()) {
 499       return ACCESS_OK;
 500     }
 501 
 502     PackageEntry* package_to = new_class->package();
 503     assert(package_to != nullptr, "can not obtain new_class' package");
 504 
 505     {
 506       MutexLocker m1(Module_lock);
 507 
 508       // Once readability is established, if module_to exports T unqualifiedly,
 509       // (to all modules), than whether module_from is in the unnamed module
 510       // or not does not matter, access is allowed.
 511       if (package_to->is_unqual_exported()) {
 512         return ACCESS_OK;
 513       }
 514 
 515       // Access is allowed if both 1 & 2 hold:
 516       //   1. Readability, module_from can read module_to (established above).
 517       //   2. Either module_to exports T to module_from qualifiedly.
 518       //      or
 519       //      module_to exports T to all unnamed modules and module_from is unnamed.
 520       //      or
 521       //      module_to exports T unqualifiedly to all modules (checked above).
 522       if (!package_to->is_qexported_to(module_from)) {
 523         return TYPE_NOT_EXPORTED;
 524       }
 525     }
 526     return ACCESS_OK;
 527   }
 528 
 529   if (can_relax_access_check_for(current_class, new_class, classloader_only)) {
 530     return ACCESS_OK;
 531   }
 532   return OTHER_PROBLEM;
 533 }
 534 
 535 // Return an error message specific to the specified Klass*'s and result.
 536 // This function must be called from within a block containing a ResourceMark.
 537 char* Reflection::verify_class_access_msg(const Klass* current_class,
 538                                           const InstanceKlass* new_class,
 539                                           const VerifyClassAccessResults result) {
 540   assert(result != ACCESS_OK, "must be failure result");
 541   char * msg = nullptr;
 542   if (result != OTHER_PROBLEM && new_class != nullptr && current_class != nullptr) {
 543     // Find the module entry for current_class, the accessor
 544     ModuleEntry* module_from = current_class->module();
 545     const char * module_from_name = module_from->is_named() ? module_from->name()->as_C_string() : UNNAMED_MODULE;
 546     const char * current_class_name = current_class->external_name();
 547 
 548     // Find the module entry for new_class, the accessee
 549     ModuleEntry* module_to = nullptr;
 550     module_to = new_class->module();
 551     const char * module_to_name = module_to->is_named() ? module_to->name()->as_C_string() : UNNAMED_MODULE;
 552     const char * new_class_name = new_class->external_name();
 553 
 554     if (result == MODULE_NOT_READABLE) {
 555       assert(module_from->is_named(), "Unnamed modules can read all modules");
 556       if (module_to->is_named()) {
 557         size_t len = 100 + strlen(current_class_name) + 2*strlen(module_from_name) +
 558           strlen(new_class_name) + 2*strlen(module_to_name);
 559         msg = NEW_RESOURCE_ARRAY(char, len);
 560         jio_snprintf(msg, len - 1,
 561           "class %s (in module %s) cannot access class %s (in module %s) because module %s does not read module %s",
 562           current_class_name, module_from_name, new_class_name,
 563           module_to_name, module_from_name, module_to_name);
 564       } else {
 565         oop jlm = module_to->module();
 566         assert(jlm != nullptr, "Null jlm in module_to ModuleEntry");
 567         intptr_t identity_hash = jlm->identity_hash();
 568         size_t len = 160 + strlen(current_class_name) + 2*strlen(module_from_name) +
 569           strlen(new_class_name) + 2*sizeof(uintx);
 570         msg = NEW_RESOURCE_ARRAY(char, len);
 571         jio_snprintf(msg, len - 1,
 572           "class %s (in module %s) cannot access class %s (in unnamed module @" SIZE_FORMAT_X ") because module %s does not read unnamed module @" SIZE_FORMAT_X,
 573           current_class_name, module_from_name, new_class_name, uintx(identity_hash),
 574           module_from_name, uintx(identity_hash));
 575       }
 576 
 577     } else if (result == TYPE_NOT_EXPORTED) {
 578       assert(new_class->package() != nullptr,
 579              "Unnamed packages are always exported");
 580       const char * package_name =
 581         new_class->package()->name()->as_klass_external_name();
 582       assert(module_to->is_named(), "Unnamed modules export all packages");
 583       if (module_from->is_named()) {
 584         size_t len = 118 + strlen(current_class_name) + 2*strlen(module_from_name) +
 585           strlen(new_class_name) + 2*strlen(module_to_name) + strlen(package_name);
 586         msg = NEW_RESOURCE_ARRAY(char, len);
 587         jio_snprintf(msg, len - 1,
 588           "class %s (in module %s) cannot access class %s (in module %s) because module %s does not export %s to module %s",
 589           current_class_name, module_from_name, new_class_name,
 590           module_to_name, module_to_name, package_name, module_from_name);
 591       } else {
 592         oop jlm = module_from->module();
 593         assert(jlm != nullptr, "Null jlm in module_from ModuleEntry");
 594         intptr_t identity_hash = jlm->identity_hash();
 595         size_t len = 170 + strlen(current_class_name) + strlen(new_class_name) +
 596           2*strlen(module_to_name) + strlen(package_name) + 2*sizeof(uintx);
 597         msg = NEW_RESOURCE_ARRAY(char, len);
 598         jio_snprintf(msg, len - 1,
 599           "class %s (in unnamed module @" SIZE_FORMAT_X ") cannot access class %s (in module %s) because module %s does not export %s to unnamed module @" SIZE_FORMAT_X,
 600           current_class_name, uintx(identity_hash), new_class_name, module_to_name,
 601           module_to_name, package_name, uintx(identity_hash));
 602       }
 603     } else {
 604         ShouldNotReachHere();
 605     }
 606   }  // result != OTHER_PROBLEM...
 607   return msg;
 608 }
 609 
 610 bool Reflection::verify_member_access(const Klass* current_class,
 611                                       const Klass* resolved_class,
 612                                       const Klass* member_class,
 613                                       AccessFlags access,
 614                                       bool classloader_only,
 615                                       bool protected_restriction,
 616                                       TRAPS) {
 617   // Verify that current_class can access a member of member_class, where that
 618   // field's access bits are "access".  We assume that we've already verified
 619   // that current_class can access member_class.
 620   //
 621   // If the classloader_only flag is set, we automatically allow any accesses
 622   // in which current_class doesn't have a classloader.
 623   //
 624   // "resolved_class" is the runtime type of "member_class". Sometimes we don't
 625   // need this distinction (e.g. if all we have is the runtime type, or during
 626   // class file parsing when we only care about the static type); in that case
 627   // callers should ensure that resolved_class == member_class.
 628   //
 629   if ((current_class == nullptr) ||
 630       (current_class == member_class) ||
 631       access.is_public()) {
 632     return true;
 633   }
 634 
 635   if (current_class == member_class) {
 636     return true;
 637   }
 638 
 639   if (access.is_protected()) {
 640     if (!protected_restriction) {
 641       // See if current_class (or outermost host class) is a subclass of member_class
 642       // An interface may not access protected members of j.l.Object
 643       if (!current_class->is_interface() && current_class->is_subclass_of(member_class)) {
 644         if (access.is_static() || // static fields are ok, see 6622385
 645             current_class == resolved_class ||
 646             member_class == resolved_class ||
 647             current_class->is_subclass_of(resolved_class) ||
 648             resolved_class->is_subclass_of(current_class)) {
 649           return true;
 650         }
 651       }
 652     }
 653   }
 654 
 655   // package access
 656   if (!access.is_private() && is_same_class_package(current_class, member_class)) {
 657     return true;
 658   }
 659 
 660   // private access between different classes needs a nestmate check.
 661   if (access.is_private()) {
 662     if (current_class->is_instance_klass() && member_class->is_instance_klass() ) {
 663       InstanceKlass* cur_ik = const_cast<InstanceKlass*>(InstanceKlass::cast(current_class));
 664       InstanceKlass* field_ik = const_cast<InstanceKlass*>(InstanceKlass::cast(member_class));
 665       // Nestmate access checks may require resolution and validation of the nest-host.
 666       // It is up to the caller to check for pending exceptions and handle appropriately.
 667       bool access = cur_ik->has_nestmate_access_to(field_ik, CHECK_false);
 668       if (access) {
 669         guarantee(resolved_class->is_subclass_of(member_class), "must be!");
 670         return true;
 671       }
 672     }
 673   }
 674 
 675   // Allow all accesses from jdk/internal/reflect/SerializationConstructorAccessorImpl subclasses to
 676   // succeed trivially.
 677   if (current_class->is_subclass_of(vmClasses::reflect_SerializationConstructorAccessorImpl_klass())) {
 678     return true;
 679   }
 680 
 681   // Check for special relaxations
 682   return can_relax_access_check_for(current_class, member_class, classloader_only);
 683 }
 684 
 685 bool Reflection::is_same_class_package(const Klass* class1, const Klass* class2) {
 686   return InstanceKlass::cast(class1)->is_same_class_package(class2);
 687 }
 688 
 689 // Checks that the 'outer' klass has declared 'inner' as being an inner klass. If not,
 690 // throw an incompatible class change exception
 691 // If inner_is_member, require the inner to be a member of the outer.
 692 // If !inner_is_member, require the inner to be hidden (non-member).
 693 // Caller is responsible for figuring out in advance which case must be true.
 694 void Reflection::check_for_inner_class(const InstanceKlass* outer, const InstanceKlass* inner,
 695                                        bool inner_is_member, TRAPS) {
 696   InnerClassesIterator iter(outer);
 697   constantPoolHandle cp   (THREAD, outer->constants());
 698   for (; !iter.done(); iter.next()) {
 699     int ioff = iter.inner_class_info_index();
 700     int ooff = iter.outer_class_info_index();
 701 
 702     if (inner_is_member && ioff != 0 && ooff != 0) {
 703       if (cp->klass_name_at_matches(outer, ooff) &&
 704           cp->klass_name_at_matches(inner, ioff)) {
 705         Klass* o = cp->klass_at(ooff, CHECK);
 706         if (o == outer) {
 707           Klass* i = cp->klass_at(ioff, CHECK);
 708           if (i == inner) {
 709             return;
 710           }
 711         }
 712       }
 713     }
 714 
 715     if (!inner_is_member && ioff != 0 && ooff == 0 &&
 716         cp->klass_name_at_matches(inner, ioff)) {
 717       Klass* i = cp->klass_at(ioff, CHECK);
 718       if (i == inner) {
 719         return;
 720       }
 721     }
 722   }
 723 
 724   // 'inner' not declared as an inner klass in outer
 725   ResourceMark rm(THREAD);
 726   Exceptions::fthrow(
 727     THREAD_AND_LOCATION,
 728     vmSymbols::java_lang_IncompatibleClassChangeError(),
 729     "%s and %s disagree on InnerClasses attribute",
 730     outer->external_name(),
 731     inner->external_name()
 732   );
 733 }
 734 
 735 static objArrayHandle get_parameter_types(const methodHandle& method,
 736                                           int parameter_count,
 737                                           oop* return_type,
 738                                           TRAPS) {
 739   objArrayOop m;
 740   if (parameter_count == 0) {
 741     // Avoid allocating an array for the empty case
 742     // Still need to parse the signature for the return type below
 743     m = Universe::the_empty_class_array();
 744   } else {
 745     // Allocate array holding parameter types (java.lang.Class instances)
 746     m = oopFactory::new_objArray(vmClasses::Class_klass(), parameter_count, CHECK_(objArrayHandle()));
 747   }
 748   objArrayHandle mirrors(THREAD, m);
 749   int index = 0;
 750   // Collect parameter types
 751   ResourceMark rm(THREAD);
 752   for (ResolvingSignatureStream ss(method()); !ss.is_done(); ss.next()) {
 753     oop mirror = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_(objArrayHandle()));
 754     if (log_is_enabled(Debug, class, resolve)) {
 755       trace_class_resolution(mirror);
 756     }
 757     if (!ss.at_return_type()) {
 758       mirrors->obj_at_put(index++, mirror);
 759     } else if (return_type != nullptr) {
 760       // Collect return type as well
 761       assert(ss.at_return_type(), "return type should be present");
 762       *return_type = mirror;
 763     }
 764   }
 765   assert(index == parameter_count, "invalid parameter count");
 766   return mirrors;
 767 }
 768 
 769 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
 770   return method->resolved_checked_exceptions(THREAD);
 771 }
 772 
 773 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
 774   ResolvingSignatureStream ss(signature, k, false);
 775   oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);
 776   return Handle(THREAD, nt);
 777 }
 778 
 779 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
 780   // Allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
 781   assert(!method()->name()->starts_with('<') || for_constant_pool_access,
 782          "should call new_constructor instead");
 783   InstanceKlass* holder = method->method_holder();
 784   int slot = method->method_idnum();
 785 
 786   Symbol*  signature  = method->signature();
 787   int parameter_count = ArgumentCount(signature).size();
 788   oop return_type_oop = nullptr;
 789   objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
 790   if (parameter_types.is_null() || return_type_oop == nullptr) return nullptr;
 791 
 792   Handle return_type(THREAD, return_type_oop);
 793 
 794   objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
 795   assert(!exception_types.is_null(), "cannot return null");
 796 
 797   Symbol*  method_name = method->name();
 798   oop name_oop = StringTable::intern(method_name, CHECK_NULL);
 799   Handle name = Handle(THREAD, name_oop);
 800   if (name == nullptr) return nullptr;
 801 
 802   const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
 803 
 804   Handle mh = java_lang_reflect_Method::create(CHECK_NULL);
 805 
 806   java_lang_reflect_Method::set_clazz(mh(), holder->java_mirror());
 807   java_lang_reflect_Method::set_slot(mh(), slot);
 808   java_lang_reflect_Method::set_name(mh(), name());
 809   java_lang_reflect_Method::set_return_type(mh(), return_type());
 810   java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
 811   java_lang_reflect_Method::set_exception_types(mh(), exception_types());
 812   java_lang_reflect_Method::set_modifiers(mh(), modifiers);
 813   java_lang_reflect_Method::set_override(mh(), false);
 814   if (method->generic_signature() != nullptr) {
 815     Symbol*  gs = method->generic_signature();
 816     Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
 817     java_lang_reflect_Method::set_signature(mh(), sig());
 818   }
 819   typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
 820   java_lang_reflect_Method::set_annotations(mh(), an_oop);
 821   an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
 822   java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
 823   an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
 824   java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
 825   return mh();
 826 }
 827 
 828 
 829 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
 830   assert(method()->is_object_constructor(),
 831          "should call new_method instead");
 832 
 833   InstanceKlass* holder = method->method_holder();
 834   int slot = method->method_idnum();
 835 
 836   Symbol*  signature  = method->signature();
 837   int parameter_count = ArgumentCount(signature).size();
 838   objArrayHandle parameter_types = get_parameter_types(method, parameter_count, nullptr, CHECK_NULL);
 839   if (parameter_types.is_null()) return nullptr;
 840 
 841   objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
 842   assert(!exception_types.is_null(), "cannot return null");
 843 
 844   const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
 845 
 846   Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
 847 
 848   java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
 849   java_lang_reflect_Constructor::set_slot(ch(), slot);
 850   java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
 851   java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());
 852   java_lang_reflect_Constructor::set_modifiers(ch(), modifiers);
 853   java_lang_reflect_Constructor::set_override(ch(), false);
 854   if (method->generic_signature() != nullptr) {
 855     Symbol*  gs = method->generic_signature();
 856     Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
 857     java_lang_reflect_Constructor::set_signature(ch(), sig());
 858   }
 859   typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
 860   java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
 861   an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
 862   java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
 863   return ch();
 864 }
 865 
 866 
 867 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
 868   Symbol*  field_name = fd->name();
 869   oop name_oop = StringTable::intern(field_name, CHECK_NULL);
 870   Handle name = Handle(THREAD, name_oop);
 871   Symbol*  signature  = fd->signature();
 872   InstanceKlass* holder = fd->field_holder();
 873   Handle type = new_type(signature, holder, CHECK_NULL);
 874   Handle rh  = java_lang_reflect_Field::create(CHECK_NULL);
 875 
 876   java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
 877   java_lang_reflect_Field::set_slot(rh(), fd->index());
 878   java_lang_reflect_Field::set_name(rh(), name());
 879   java_lang_reflect_Field::set_type(rh(), type());
 880 
 881   int flags = 0;
 882   if (fd->is_trusted_final()) {
 883     flags |= TRUSTED_FINAL;
 884   }
 885   if (fd->is_null_free_inline_type()) {
 886     flags |= NULL_RESTRICTED;
 887   }
 888   java_lang_reflect_Field::set_flags(rh(), flags);
 889 
 890   // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
 891   int modifiers = fd->access_flags().as_int();
 892   java_lang_reflect_Field::set_modifiers(rh(), modifiers);
 893   java_lang_reflect_Field::set_override(rh(), false);
 894   if (fd->has_generic_signature()) {
 895     Symbol*  gs = fd->generic_signature();
 896     Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
 897     java_lang_reflect_Field::set_signature(rh(), sig());
 898   }
 899   typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
 900   java_lang_reflect_Field::set_annotations(rh(), an_oop);
 901   return rh();
 902 }
 903 
 904 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
 905                               int flags, TRAPS) {
 906 
 907   Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
 908 
 909   if(nullptr != sym) {
 910     Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
 911     java_lang_reflect_Parameter::set_name(rh(), name());
 912   } else {
 913     java_lang_reflect_Parameter::set_name(rh(), nullptr);
 914   }
 915 
 916   java_lang_reflect_Parameter::set_modifiers(rh(), flags);
 917   java_lang_reflect_Parameter::set_executable(rh(), method());
 918   java_lang_reflect_Parameter::set_index(rh(), index);
 919   return rh();
 920 }
 921 
 922 
 923 static methodHandle resolve_interface_call(InstanceKlass* klass,
 924                                            const methodHandle& method,
 925                                            Klass* recv_klass,
 926                                            Handle receiver,
 927                                            TRAPS) {
 928 
 929   assert(!method.is_null() , "method should not be null");
 930 
 931   CallInfo info;
 932   Symbol*  signature  = method->signature();
 933   Symbol*  name       = method->name();
 934   LinkResolver::resolve_interface_call(info, receiver, recv_klass,
 935                                        LinkInfo(klass, name, signature),
 936                                        true,
 937                                        CHECK_(methodHandle()));
 938   return methodHandle(THREAD, info.selected_method());
 939 }
 940 
 941 // Conversion
 942 static BasicType basic_type_mirror_to_basic_type(oop basic_type_mirror) {
 943   assert(java_lang_Class::is_primitive(basic_type_mirror),
 944     "just checking");
 945   return java_lang_Class::primitive_type(basic_type_mirror);
 946 }
 947 
 948 // Narrowing of basic types. Used to create correct jvalues for
 949 // boolean, byte, char and short return return values from interpreter
 950 // which are returned as ints. Throws IllegalArgumentException.
 951 static void narrow(jvalue* value, BasicType narrow_type, TRAPS) {
 952   switch (narrow_type) {
 953   case T_BOOLEAN:
 954     value->z = (jboolean) (value->i & 1);
 955     return;
 956   case T_BYTE:
 957     value->b = (jbyte)value->i;
 958     return;
 959   case T_CHAR:
 960     value->c = (jchar)value->i;
 961     return;
 962   case T_SHORT:
 963     value->s = (jshort)value->i;
 964     return;
 965   default:
 966     break; // fail
 967   }
 968   THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
 969 }
 970 
 971 
 972 // Method call (shared by invoke_method and invoke_constructor)
 973 static oop invoke(InstanceKlass* klass,
 974                   const methodHandle& reflected_method,
 975                   Handle receiver,
 976                   bool override,
 977                   objArrayHandle ptypes,
 978                   BasicType rtype,
 979                   objArrayHandle args,
 980                   bool is_method_invoke,
 981                   TRAPS) {
 982 
 983   ResourceMark rm(THREAD);
 984 
 985   methodHandle method;      // actual method to invoke
 986   Klass* target_klass;      // target klass, receiver's klass for non-static
 987 
 988   // Ensure klass is initialized
 989   klass->initialize(CHECK_NULL);
 990 
 991   bool is_static = reflected_method->is_static();
 992   if (is_static) {
 993     // ignore receiver argument
 994     method = reflected_method;
 995     target_klass = klass;
 996   } else {
 997     // check for null receiver
 998     if (receiver.is_null()) {
 999       THROW_0(vmSymbols::java_lang_NullPointerException());
1000     }
1001     // Check class of receiver against class declaring method
1002     if (!receiver->is_a(klass)) {
1003       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
1004     }
1005     // target klass is receiver's klass
1006     target_klass = receiver->klass();
1007     // no need to resolve if method is private or <init>
1008     if (reflected_method->is_private() ||
1009         reflected_method->name() == vmSymbols::object_initializer_name()) {
1010       method = reflected_method;
1011     } else {
1012       // resolve based on the receiver
1013       if (reflected_method->method_holder()->is_interface()) {
1014         // resolve interface call
1015         //
1016         // Match resolution errors with those thrown due to reflection inlining
1017         // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
1018         method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
1019         if (HAS_PENDING_EXCEPTION) {
1020           // Method resolution threw an exception; wrap it in an InvocationTargetException
1021           oop resolution_exception = PENDING_EXCEPTION;
1022           CLEAR_PENDING_EXCEPTION;
1023           // JVMTI has already reported the pending exception
1024           // JVMTI internal flag reset is needed in order to report InvocationTargetException
1025           JvmtiExport::clear_detected_exception(THREAD);
1026           JavaCallArguments args(Handle(THREAD, resolution_exception));
1027           THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1028                       vmSymbols::throwable_void_signature(),
1029                       &args);
1030         }
1031       }  else {
1032         // if the method can be overridden, we resolve using the vtable index.
1033         assert(!reflected_method->has_itable_index(), "");
1034         int index = reflected_method->vtable_index();
1035         method = reflected_method;
1036         if (index != Method::nonvirtual_vtable_index) {
1037           method = methodHandle(THREAD, target_klass->method_at_vtable(index));
1038         }
1039         if (!method.is_null()) {
1040           // Check for abstract methods as well
1041           if (method->is_abstract()) {
1042             // new default: 6531596
1043             ResourceMark rm(THREAD);
1044             stringStream ss;
1045             ss.print("'");
1046             Method::print_external_name(&ss, target_klass, method->name(), method->signature());
1047             ss.print("'");
1048             Handle h_origexception = Exceptions::new_exception(THREAD,
1049               vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1050             JavaCallArguments args(h_origexception);
1051             THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1052               vmSymbols::throwable_void_signature(),
1053               &args);
1054           }
1055         }
1056       }
1057     }
1058   }
1059 
1060   // I believe this is a ShouldNotGetHere case which requires
1061   // an internal vtable bug. If you ever get this please let Karen know.
1062   if (method.is_null()) {
1063     ResourceMark rm(THREAD);
1064     stringStream ss;
1065     ss.print("'");
1066     Method::print_external_name(&ss, klass,
1067                                      reflected_method->name(),
1068                                      reflected_method->signature());
1069     ss.print("'");
1070     THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
1071   }
1072 
1073   assert(ptypes->is_objArray(), "just checking");
1074   int args_len = args.is_null() ? 0 : args->length();
1075   // Check number of arguments
1076   if (ptypes->length() != args_len) {
1077     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1078                 "wrong number of arguments");
1079   }
1080 
1081   // Create object to contain parameters for the JavaCall
1082   JavaCallArguments java_args(method->size_of_parameters());
1083 
1084   if (!is_static) {
1085     java_args.push_oop(receiver);
1086   }
1087 
1088   for (int i = 0; i < args_len; i++) {
1089     oop type_mirror = ptypes->obj_at(i);
1090     oop arg = args->obj_at(i);
1091     if (java_lang_Class::is_primitive(type_mirror)) {
1092       jvalue value;
1093       BasicType ptype = basic_type_mirror_to_basic_type(type_mirror);
1094       BasicType atype = Reflection::unbox_for_primitive(arg, &value, CHECK_NULL);
1095       if (ptype != atype) {
1096         Reflection::widen(&value, atype, ptype, CHECK_NULL);
1097       }
1098       switch (ptype) {
1099         case T_BOOLEAN:     java_args.push_int(value.z);    break;
1100         case T_CHAR:        java_args.push_int(value.c);    break;
1101         case T_BYTE:        java_args.push_int(value.b);    break;
1102         case T_SHORT:       java_args.push_int(value.s);    break;
1103         case T_INT:         java_args.push_int(value.i);    break;
1104         case T_LONG:        java_args.push_long(value.j);   break;
1105         case T_FLOAT:       java_args.push_float(value.f);  break;
1106         case T_DOUBLE:      java_args.push_double(value.d); break;
1107         default:
1108           THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
1109       }
1110     } else {
1111       if (arg != nullptr) {
1112         Klass* k = java_lang_Class::as_Klass(type_mirror);
1113         if (!arg->is_a(k)) {
1114           THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1115                       "argument type mismatch");
1116         }
1117       }
1118       Handle arg_handle(THREAD, arg);         // Create handle for argument
1119       java_args.push_oop(arg_handle); // Push handle
1120     }
1121   }
1122 
1123   assert(java_args.size_of_parameters() == method->size_of_parameters(),
1124     "just checking");
1125 
1126   // All oops (including receiver) is passed in as Handles. An potential oop is returned as an
1127   // oop (i.e., NOT as an handle)
1128   JavaValue result(rtype);
1129   JavaCalls::call(&result, method, &java_args, THREAD);
1130 
1131   if (HAS_PENDING_EXCEPTION) {
1132     // Method threw an exception; wrap it in an InvocationTargetException
1133     oop target_exception = PENDING_EXCEPTION;
1134     CLEAR_PENDING_EXCEPTION;
1135     // JVMTI has already reported the pending exception
1136     // JVMTI internal flag reset is needed in order to report InvocationTargetException
1137     JvmtiExport::clear_detected_exception(THREAD);
1138 
1139     JavaCallArguments args(Handle(THREAD, target_exception));
1140     THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1141                 vmSymbols::throwable_void_signature(),
1142                 &args);
1143   } else {
1144     if (rtype == T_BOOLEAN || rtype == T_BYTE || rtype == T_CHAR || rtype == T_SHORT) {
1145       narrow((jvalue*)result.get_value_addr(), rtype, CHECK_NULL);
1146     }
1147     return Reflection::box((jvalue*)result.get_value_addr(), rtype, THREAD);
1148   }
1149 }
1150 
1151 // This would be nicer if, say, java.lang.reflect.Method was a subclass
1152 // of java.lang.reflect.Constructor
1153 
1154 oop Reflection::invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS) {
1155   oop mirror             = java_lang_reflect_Method::clazz(method_mirror);
1156   int slot               = java_lang_reflect_Method::slot(method_mirror);
1157   bool override          = java_lang_reflect_Method::override(method_mirror) != 0;
1158   objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Method::parameter_types(method_mirror)));
1159 
1160   oop return_type_mirror = java_lang_reflect_Method::return_type(method_mirror);
1161   BasicType rtype;
1162   if (java_lang_Class::is_primitive(return_type_mirror)) {
1163     rtype = basic_type_mirror_to_basic_type(return_type_mirror);
1164   } else {
1165     rtype = T_OBJECT;
1166   }
1167 
1168   InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1169   Method* m = klass->method_with_idnum(slot);
1170   if (m == nullptr) {
1171     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1172   }
1173   methodHandle method(THREAD, m);
1174 
1175   return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1176 }
1177 
1178 
1179 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1180   oop mirror             = java_lang_reflect_Constructor::clazz(constructor_mirror);
1181   int slot               = java_lang_reflect_Constructor::slot(constructor_mirror);
1182   bool override          = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1183   objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1184 
1185   InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1186   Method* m = klass->method_with_idnum(slot);
1187   if (m == nullptr) {
1188     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1189   }
1190   methodHandle method(THREAD, m);
1191 
1192   // Make sure klass gets initialize
1193   klass->initialize(CHECK_NULL);
1194 
1195   // Create new instance (the receiver)
1196   klass->check_valid_for_instantiation(false, CHECK_NULL);
1197   Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1198 
1199   // Ignore result from call and return receiver
1200   invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1201   return receiver();
1202 }