1 /*
2 * Copyright (c) 2011, 2019, 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 package jdk.vm.ci.hotspot;
24
25 import static jdk.vm.ci.hotspot.CompilerToVM.compilerToVM;
26 import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
27
28 import jdk.vm.ci.code.InstalledCode;
29 import jdk.vm.ci.code.InvalidInstalledCodeException;
30 import jdk.vm.ci.common.JVMCIError;
31 import jdk.vm.ci.meta.JavaKind;
32 import jdk.vm.ci.meta.JavaType;
33 import jdk.vm.ci.meta.ResolvedJavaMethod;
34
35 /**
36 * Implementation of {@link InstalledCode} for code installed as an {@code nmethod}. The address of
37 * the {@code nmethod} is stored in {@link InstalledCode#address} and the value of
38 * {@code nmethod::verified_entry_point()} is in {@link InstalledCode#entryPoint}.
39 */
40 public class HotSpotNmethod extends HotSpotInstalledCode {
41
42 /**
43 * This (indirect) {@code Method*} reference is safe since class redefinition preserves all
44 * methods associated with nmethods in the code cache.
45 */
46 private final HotSpotResolvedJavaMethodImpl method;
47
48 /**
49 * Specifies whether the {@code nmethod} associated with this object is the code executed by
50 * default HotSpot linkage when a normal Java call to {@link #method} is made. That is, does
51 * {@code this.method.metadataHandle->_code} == {@code this.address}. If not, then the
52 * {@code nmethod} can only be invoked via a reference to this object. An example of this is the
53 * trampoline mechanism used by Truffle: https://goo.gl/LX88rZ.
54 */
55 private final boolean isDefault;
56
57 /**
58 * Determines whether this object is in the oops table of the nmethod.
59 * <p>
60 * If this object is in the oops table, the VM uses the oops table entry to update this object's
61 * {@link #address} and {@link #entryPoint} fields when the state of the nmethod changes. The
62 * nmethod will be unloadable when this object dies.
63 * <p>
64 * Otherwise, the nmethod's unloadability is not changed when this object dies.
65 */
66 boolean inOopsTable() {
67 return compileIdSnapshot != 0;
68 }
69
70 /**
71 * If this field is 0, this object is in the oops table of the nmethod. Otherwise, the value of
72 * the field records the nmethod's compile identifier. This value is used to confirm if an entry
73 * in the code cache retrieved by {@link #address} is indeed the nmethod represented by this
74 * object.
75 *
76 * @see #inOopsTable
77 */
78 private final long compileIdSnapshot;
79
80 /**
81 * Identify the reason that caused this nmethod to be invalidated.
82 * A value of -1 means that the nmethod was not invalidated.
83 */
84 private int invalidationReason;
85
86 HotSpotNmethod(HotSpotResolvedJavaMethodImpl method, String name, boolean isDefault, long compileId) {
87 super(name);
88 this.method = method;
89 this.isDefault = isDefault;
90 boolean inOopsTable = !IS_IN_NATIVE_IMAGE && !isDefault;
91 this.compileIdSnapshot = inOopsTable ? 0L : compileId;
92 this.invalidationReason = -1;
93 assert inOopsTable || compileId != 0L : this;
94 }
95
96 /**
97 * Attaches {@code log} to this object. If {@code log.managesFailedSpeculations() == true}, this
98 * ensures the failed speculation list lives at least as long as this object.
99 */
100 public void setSpeculationLog(HotSpotSpeculationLog log) {
101 this.speculationLog = log;
102 }
103
104 /**
105 * The speculation log containing speculations embedded in the nmethod.
106 *
107 * If {@code speculationLog.managesFailedSpeculations() == true}, this field ensures the failed
108 * speculation list lives at least as long as this object. This prevents deoptimization from
109 * appending to an already freed list.
110 */
111 @SuppressWarnings("unused") private HotSpotSpeculationLog speculationLog;
112
113 /**
114 * Determines if the nmethod associated with this object is the compiled entry point for
115 * {@link #getMethod()}.
116 */
117 public boolean isDefault() {
118 return isDefault;
119 }
120
121 @Override
122 public boolean isValid() {
123 if (compileIdSnapshot != 0L) {
124 compilerToVM().updateHotSpotNmethod(this);
125 }
126 return super.isValid();
127 }
128
129 public ResolvedJavaMethod getMethod() {
130 return method;
131 }
132
133 /**
134 * Invalidate this nmethod using the reason specified in {@code invalidationReason} and
135 * optionally deoptimize the method if {@code deoptimize} is set.
136 * @param deoptimize whether or not to deoptimize the method.
137 * @param invalidationReason invalidation reason code.
138 */
139 public void invalidate(boolean deoptimize, int invalidationReason) {
140 compilerToVM().invalidateHotSpotNmethod(this, deoptimize, invalidationReason);
141 }
142
143 @Override
144 public void invalidate(boolean deoptimize) {
145 invalidate(deoptimize, jvmciInvalidationReason());
146 }
147
148 @Override
149 public long getAddress() {
150 if (compileIdSnapshot != 0L) {
151 compilerToVM().updateHotSpotNmethod(this);
152 }
153 return super.getAddress();
154 }
155
156 @Override
157 public long getEntryPoint() {
158 if (compileIdSnapshot != 0L) {
159 return 0;
160 }
161 return super.getEntryPoint();
162 }
163
164 @Override
165 public String toString() {
166 return String.format("HotSpotNmethod[method=%s, codeBlob=0x%x, isDefault=%b, name=%s, inOopsTable=%s]",
167 method, getAddress(), isDefault, name, inOopsTable());
168 }
169
170 private boolean checkArgs(Object... args) {
171 JavaType[] sig = method.toParameterTypes();
172 assert args.length == sig.length : method.format("%H.%n(%p): expected ") + sig.length + " args, got " + args.length;
173 for (int i = 0; i < sig.length; i++) {
174 Object arg = args[i];
175 if (arg == null) {
176 assert sig[i].getJavaKind() == JavaKind.Object : method.format("%H.%n(%p): expected arg ") + i + " to be Object, not " + sig[i];
177 } else if (sig[i].getJavaKind() != JavaKind.Object) {
178 assert sig[i].getJavaKind().toBoxedJavaClass() == arg.getClass() : method.format("%H.%n(%p): expected arg ") + i + " to be " + sig[i] + ", not " + arg.getClass();
179 }
180 }
181 return true;
182 }
183
184 /**
185 * {@inheritDoc}
186 *
187 * It's possible for the HotSpot runtime to sweep nmethods at any point in time. As a result,
188 * there is no guarantee that calling this method will execute the wrapped nmethod. Instead, it
189 * may end up executing the bytecode of the associated {@link #getMethod() Java method}. Only if
190 * {@link #isValid()} is {@code true} after returning can the caller be sure that the nmethod
191 * was executed. If {@link #isValid()} is {@code false}, then the only way to determine if the
192 * nmethod was executed is to test for some side-effect specific to the nmethod (e.g., update to
193 * a field) that is not performed by the bytecode of the associated {@link #getMethod() Java
194 * method}.
195 */
196 @Override
197 public Object executeVarargs(Object... args) throws InvalidInstalledCodeException {
198 if (IS_IN_NATIVE_IMAGE) {
199 throw new HotSpotJVMCIUnsupportedOperationError("Cannot execute nmethod via mirror in native image");
200 }
201 assert checkArgs(args);
202 return compilerToVM().executeHotSpotNmethod(args, this);
203 }
204
205 @Override
206 public long getStart() {
207 return isValid() ? super.getStart() : 0;
208 }
209
210 /**
211 * @return an integer representing the reason why this nmethod was invalidated.
212 */
213 public int getInvalidationReason() {
214 return invalidationReason;
215 }
216
217 /**
218 * @return a String describing the reason why this nmethod was invalidated.
219 */
220 public String getInvalidationReasonDescription() {
221 return compilerToVM().getInvalidationReasonDescription(this.getInvalidationReason());
222 }
223
224 private static int jvmciInvalidationReason() {
225 return HotSpotJVMCIRuntime.runtime().config.getConstant("nmethod::InvalidationReason::JVMCI_INVALIDATE", Integer.class);
226 }
227 }