1 | /* |
2 | * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
3 | * Copyright (c) 2018, SAP SE. All rights reserved. |
4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
5 | * |
6 | * This code is free software; you can redistribute it and/or modify it |
7 | * under the terms of the GNU General Public License version 2 only, as |
8 | * published by the Free Software Foundation. |
9 | * |
10 | * This code is distributed in the hope that it will be useful, but WITHOUT |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
13 | * version 2 for more details (a copy is included in the LICENSE file that |
14 | * accompanied this code). |
15 | * |
16 | * You should have received a copy of the GNU General Public License version |
17 | * 2 along with this work; if not, write to the Free Software Foundation, |
18 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
19 | * |
20 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 | * or visit www.oracle.com if you need additional information or have any |
22 | * questions. |
23 | * |
24 | */ |
25 | |
26 | #include "precompiled.hpp" |
27 | #include "classfile/systemDictionary.hpp" |
28 | #include "memory/resourceArea.hpp" |
29 | #include "oops/constantPool.hpp" |
30 | #include "oops/reflectionAccessorImplKlassHelper.hpp" |
31 | #include "utilities/constantTag.hpp" |
32 | #include "utilities/debug.hpp" |
33 | #include "utilities/globalDefinitions.hpp" |
34 | |
35 | // This code extracts name of target class, method and signature from the constant pool of a class |
36 | // assumed to be of type jdk/internal/reflect/Generated{SerializationConstructor|Constructor|Method}AccessorXXX. |
37 | // Since this may be affected by bitrot if these classes change, extra care is taken to make the |
38 | // release build of this coding robust. |
39 | |
40 | // We extract target class name, method name and sig from the constant pool of the Accessor class. |
41 | // This is an excerpt of the Constant pool (see jdk/internal/reflect/MethodAccessorGenerator.java:) |
42 | |
43 | // (^ = Only present if generating SerializationConstructorAccessor) |
44 | // 1 [UTF-8] [This class's name] |
45 | // 2 [CONSTANT_Class_info] for above |
46 | // 3 [UTF-8] "jdk/internal/reflect/{MethodAccessorImpl,ConstructorAccessorImpl,SerializationConstructorAccessorImpl}" |
47 | // 4 [CONSTANT_Class_info] for above |
48 | // 5 [UTF-8] [Target class's name] |
49 | // 6 [CONSTANT_Class_info] for above |
50 | // 7^ [UTF-8] [Serialization: Class's name in which to invoke constructor] |
51 | // 8^ [CONSTANT_Class_info] for above |
52 | // 9 [UTF-8] target method or constructor name |
53 | // 10 [UTF-8] target method or constructor signature |
54 | |
55 | // Note that these strings are found at slightly different slots depending on the class type: |
56 | // - MethodAccessorImpl, ConstructoreAccessorImpl: slots 5, 7 and 8. |
57 | // - SerializationConstructorAccessorImpl: slots 5, 9 and 10. |
58 | // Unfortunately SerializationConstructorAccessorImpl is a child of ConstructoreAccessorImpl and there |
59 | // is no easy way to tell them apart. So we examine parent class name. |
60 | |
61 | enum cpi_slots { |
62 | cpi_slot_parent_class_name = 3, |
63 | cpi_slot_target_class_name = 5, |
64 | cpi_slot_target_method_name = 7, |
65 | cpi_slot_target_method_name_sca = 9, // SerializationConstructorAccessor case, see above |
66 | cpi_slot_target_method_sig = 8, |
67 | cpi_slot_target_method_sig_sca = 10 // SerializationConstructorAccessor case, see above |
68 | }; |
69 | |
70 | // Returns a string, resource-area allocated, from an UTF8 slot in the constant pool in the |
71 | // given Klass*. |
72 | static const char* get_string_from_cp_with_checks(const InstanceKlass* k, int cpi) { |
73 | const char* s = NULL; |
74 | const ConstantPool* const cp = k->constants(); |
75 | |
76 | assert(cp != NULL, "No cp?" ); |
77 | assert(cp->is_within_bounds(cpi), "Unexpected constant pool layout for \"%s\", child class of Generated{Method|Constructor}AccessorImplXXX" |
78 | " (cpi %d out of bounds for [0..%d))." , k->external_name(), cpi, cp->length()); |
79 | assert(cp->tag_at(cpi).is_utf8(), "Unexpected constant pool layout for \"%s\", child class of Generated{Method|Constructor}AccessorImplXXX" |
80 | " (no UTF8 at cpi %d (%u))." , k->external_name(), cpi, cp->tag_at(cpi).value()); |
81 | |
82 | // Be nice in release: lets not crash, just return NULL. |
83 | if (cp != NULL && cp->is_within_bounds(cpi) && cp->tag_at(cpi).is_utf8()) { |
84 | s = cp->symbol_at(cpi)->as_C_string(); |
85 | } |
86 | |
87 | return s; |
88 | } |
89 | |
90 | // helper, returns true if class name of given class matches a given prefix |
91 | static bool classname_matches_prefix(const Klass* k, const char* prefix) { |
92 | const char* classname = k->external_name(); |
93 | if (classname != NULL) { |
94 | if (::strncmp(classname, prefix, strlen(prefix)) == 0) { |
95 | return true; |
96 | } |
97 | } |
98 | return false; |
99 | } |
100 | |
101 | // Returns true if k is of type jdk/internal/reflect/GeneratedMethodAccessorXXX. |
102 | bool ReflectionAccessorImplKlassHelper::is_generated_method_accessor(const InstanceKlass* k) { |
103 | return k->super() == SystemDictionary::reflect_MethodAccessorImpl_klass() && |
104 | classname_matches_prefix(k, "jdk.internal.reflect.GeneratedMethodAccessor" ); |
105 | } |
106 | |
107 | // Returns true if k is of type jdk/internal/reflect/GeneratedConstructorAccessorXXX. |
108 | bool ReflectionAccessorImplKlassHelper::is_generated_constructor_accessor(const InstanceKlass* k) { |
109 | return k->super() == SystemDictionary::reflect_ConstructorAccessorImpl_klass() && |
110 | classname_matches_prefix(k, "jdk.internal.reflect.GeneratedConstructorAccessor" ); |
111 | } |
112 | |
113 | // Returns true if k is of type jdk/internal/reflect/GeneratedSerializationConstructorAccessorXXX. |
114 | bool ReflectionAccessorImplKlassHelper::is_generated_method_serialization_constructor_accessor(const InstanceKlass* k) { |
115 | // GeneratedSerializationConstructorAccessor is not a direct subclass of ConstructorAccessorImpl |
116 | const Klass* sk = k->super(); |
117 | if (sk != NULL && sk->super() == SystemDictionary::reflect_ConstructorAccessorImpl_klass() && |
118 | classname_matches_prefix(k, "jdk.internal.reflect.GeneratedSerializationConstructorAccessor" )) { |
119 | return true; |
120 | } |
121 | return false; |
122 | } |
123 | |
124 | const char* ReflectionAccessorImplKlassHelper::get_target_class_name(const InstanceKlass* k) { |
125 | return get_string_from_cp_with_checks(k, cpi_slot_target_class_name); |
126 | } |
127 | |
128 | const char* ReflectionAccessorImplKlassHelper::get_target_method_name(const InstanceKlass* k) { |
129 | const int target_method_name_cpi = |
130 | is_generated_method_serialization_constructor_accessor(k) ? cpi_slot_target_method_name_sca : cpi_slot_target_method_name; |
131 | return get_string_from_cp_with_checks(k, target_method_name_cpi); |
132 | } |
133 | |
134 | const char* ReflectionAccessorImplKlassHelper::get_target_method_signature(const InstanceKlass* k) { |
135 | const int target_method_name_cpi = |
136 | is_generated_method_serialization_constructor_accessor(k) ? cpi_slot_target_method_sig_sca : cpi_slot_target_method_sig; |
137 | return get_string_from_cp_with_checks(k, target_method_name_cpi); |
138 | } |
139 | |
140 | // Returns true if this is either one of jdk/internal/reflect/Generated{SerializationConstructor|Constructor|Method}AccessorXXX |
141 | // and it is safe to call print_invocation_target(k) |
142 | bool ReflectionAccessorImplKlassHelper::is_generated_accessor(const Klass* k) { |
143 | if (k != NULL && k->is_instance_klass()) { |
144 | const InstanceKlass* ik = InstanceKlass::cast(k); |
145 | if (ik->is_initialized()) { |
146 | return is_generated_method_accessor(ik) || |
147 | is_generated_constructor_accessor(ik) || |
148 | is_generated_method_serialization_constructor_accessor(ik); |
149 | } |
150 | } |
151 | return false; |
152 | } |
153 | void ReflectionAccessorImplKlassHelper::print_invocation_target(outputStream* out, Klass* k) { |
154 | assert(ReflectionAccessorImplKlassHelper::is_generated_accessor(k), "Invariant" ); |
155 | InstanceKlass* ik = InstanceKlass::cast(k); |
156 | ResourceMark rm; |
157 | const char* target_class_name = ReflectionAccessorImplKlassHelper::get_target_class_name(ik); |
158 | const char* target_method_name = ReflectionAccessorImplKlassHelper::get_target_method_name(ik); |
159 | const char* target_method_signature = ReflectionAccessorImplKlassHelper::get_target_method_signature(ik); |
160 | out->print("%s::%s %s" , |
161 | target_class_name != NULL ? target_class_name : "?" , |
162 | target_method_name != NULL ? target_method_name : "?" , |
163 | target_method_signature != NULL ? target_method_signature : "?" ); |
164 | } |
165 | |