/
RequireNode.java
388 lines (334 loc) · 16.5 KB
/
RequireNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 2.0, or
* GNU General Public License version 2, or
* GNU Lesser General Public License version 2.1.
*/
package org.truffleruby.language.loader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.source.Source;
import org.graalvm.collections.Pair;
import org.truffleruby.RubyLanguage;
import org.truffleruby.core.array.ArrayUtils;
import org.truffleruby.core.cast.BooleanCastNode;
import org.truffleruby.core.rope.Rope;
import org.truffleruby.interop.InteropNodes;
import org.truffleruby.interop.TranslateInteropExceptionNode;
import org.truffleruby.language.RubyConstant;
import org.truffleruby.language.RubyContextNode;
import org.truffleruby.language.WarningNode;
import org.truffleruby.language.constants.GetConstantNode;
import org.truffleruby.language.control.RaiseException;
import org.truffleruby.language.dispatch.DispatchNode;
import org.truffleruby.language.library.RubyStringLibrary;
import org.truffleruby.language.methods.DeclarationContext;
import org.truffleruby.language.methods.TranslateExceptionNode;
import org.truffleruby.parser.ParserContext;
import org.truffleruby.shared.Metrics;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.nodes.IndirectCallNode;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.SourceSection;
public abstract class RequireNode extends RubyContextNode {
@Child private IndirectCallNode callNode = IndirectCallNode.create();
@Child private DispatchNode isInLoadedFeatures = DispatchNode.create();
@Child private BooleanCastNode booleanCastNode = BooleanCastNode.create();
@Child private DispatchNode addToLoadedFeatures = DispatchNode.create();
@Child private WarningNode warningNode;
public abstract boolean executeRequire(String feature, Object expandedPath);
@Specialization(guards = "libExpandedPathString.isRubyString(expandedPathString)")
protected boolean require(String feature, Object expandedPathString,
@CachedLibrary(limit = "2") RubyStringLibrary libExpandedPathString) {
final String expandedPath = libExpandedPathString.getJavaString(expandedPathString);
return requireWithMetrics(feature, expandedPath, expandedPathString);
}
@TruffleBoundary
private boolean requireWithMetrics(String feature, String expandedPathRaw, Object pathString) {
requireMetric("before-require-" + feature);
try {
//intern() to improve footprint
return getContext().getMetricsProfiler().callWithMetrics(
"require",
feature,
() -> requireConsideringAutoload(feature, expandedPathRaw.intern(), pathString));
} finally {
requireMetric("after-require-" + feature);
}
}
/** During the require operation we need to load constants marked as autoloaded for the expandedPath (see
* {@code FeatureLoader#registeredAutoloads}) and mark them as started loading (via locks). After require we
* re-select autoload constants (because their list can be supplemented with constants that are loaded themselves
* (i.e. Object.autoload(:C, __FILE__))) and remove them from autoload registry. More details here:
* https://github.com/oracle/truffleruby/pull/2060#issuecomment-668627142 **/
private boolean requireConsideringAutoload(String feature, String expandedPath, Object pathString) {
final FeatureLoader featureLoader = getContext().getFeatureLoader();
final List<RubyConstant> constantsUnfiltered = featureLoader.getAutoloadConstants(expandedPath);
final List<RubyConstant> alreadyAutoloading = new ArrayList<>();
if (!constantsUnfiltered.isEmpty()) {
final List<RubyConstant> toAutoload = new ArrayList<>();
for (RubyConstant constant : constantsUnfiltered) {
// Do not autoload recursively from the #require call in GetConstantNode
if (constant.getAutoloadConstant().isAutoloading()) {
alreadyAutoloading.add(constant);
} else {
toAutoload.add(constant);
}
}
if (getContext().getOptions().LOG_AUTOLOAD && !toAutoload.isEmpty()) {
String info = toAutoload
.stream()
.filter(c -> !c.getAutoloadConstant().isAutoloading())
.map(c -> c + " with " + c.getAutoloadConstant().getAutoloadPath())
.collect(Collectors.joining(" and "));
RubyLanguage.LOGGER
.info(() -> String.format(
"%s: requiring %s which is registered as an autoload for %s",
RubyLanguage.fileLine(getContext().getCallStack().getTopMostUserSourceSection()),
feature,
info));
}
for (RubyConstant constant : toAutoload) {
GetConstantNode.autoloadConstantStart(getContext(), constant, this);
}
}
try {
return doRequire(feature, expandedPath, pathString);
} finally {
final List<RubyConstant> releasedConstants = featureLoader.getAutoloadConstants(expandedPath);
for (RubyConstant constant : releasedConstants) {
if (constant.getAutoloadConstant().isAutoloadingThread() && !alreadyAutoloading.contains(constant)) {
final boolean undefined = GetConstantNode
.autoloadUndefineConstantIfStillAutoload(constant);
GetConstantNode.logAutoloadResult(getContext(), constant, undefined);
GetConstantNode.autoloadConstantStop(constant);
featureLoader.removeAutoload(constant);
}
}
}
}
private boolean doRequire(String originalFeature, String expandedPath, Object pathString) {
final ReentrantLockFreeingMap<String> fileLocks = getContext().getFeatureLoader().getFileLocks();
final ConcurrentMap<String, Boolean> patchFiles = getContext().getCoreLibrary().getPatchFiles();
String relativeFeature = originalFeature;
if (new File(originalFeature).isAbsolute()) {
int i = originalFeature.lastIndexOf("/lib/");
if (i != -1) {
relativeFeature = originalFeature.substring(i + "/lib/".length());
}
if (relativeFeature.endsWith(".rb")) {
relativeFeature = relativeFeature.substring(0, relativeFeature.length() - ".rb".length());
}
}
Boolean patchLoaded = patchFiles.get(relativeFeature);
final boolean isPatched = patchLoaded != null;
while (true) {
final ReentrantLock lock = fileLocks.get(expandedPath);
if (lock.isHeldByCurrentThread()) {
if (isPatched && !patchLoaded) {
// it is loading the original of the patched file for the first time
// it has to allow this one case of circular require where the first require was the patch
patchLoaded = true;
patchFiles.put(relativeFeature, true);
} else {
warnCircularRequire(expandedPath);
return false;
}
}
if (!fileLocks.lock(getContext(), expandedPath, lock, this)) {
continue;
}
try {
if (isPatched && !patchLoaded) {
String expandedPatchPath = getContext().getRubyHome() + "/lib/patches/" + relativeFeature + ".rb";
RubyLanguage.LOGGER.config("patch file used: " + expandedPatchPath);
final boolean loaded = parseAndCall(expandedPatchPath, expandedPatchPath);
assert loaded;
final boolean originalLoaded = patchFiles.get(relativeFeature);
if (!originalLoaded) {
addToLoadedFeatures(pathString);
// if original is not loaded make sure we set the patch to loaded
patchFiles.put(relativeFeature, true);
}
return true;
}
if (isFeatureLoaded(pathString)) {
return false;
}
if (!parseAndCall(originalFeature, expandedPath)) {
return false;
}
addToLoadedFeatures(pathString);
return true;
} finally {
fileLocks.unlock(expandedPath, lock);
}
}
}
private boolean parseAndCall(String feature, String expandedPath) {
if (isCExtension(expandedPath)) {
requireCExtension(feature, expandedPath, this);
} else {
// All other files are assumed to be Ruby, the file type detection is not enough
final Pair<Source, Rope> sourceRopePair;
try {
final FileLoader fileLoader = new FileLoader(getContext(), getLanguage());
sourceRopePair = fileLoader.loadFile(expandedPath);
} catch (IOException e) {
return false;
}
final RootCallTarget callTarget = getContext().getCodeLoader().parseTopLevelWithCache(sourceRopePair, this);
final CodeLoader.DeferredCall deferredCall = getContext().getCodeLoader().prepareExecute(
callTarget,
ParserContext.TOP_LEVEL,
DeclarationContext.topLevel(getContext()),
null,
coreLibrary().mainObject);
requireMetric("before-execute-" + feature);
try {
getContext().getMetricsProfiler().callWithMetrics(
"execute",
feature,
() -> deferredCall.call(callNode));
} finally {
requireMetric("after-execute-" + feature);
}
}
return true;
}
private boolean isCExtension(String path) {
return path.toLowerCase(Locale.ENGLISH).endsWith(RubyLanguage.CEXT_EXTENSION);
}
@TruffleBoundary
private void requireCExtension(String feature, String expandedPath, Node currentNode) {
final FeatureLoader featureLoader = getContext().getFeatureLoader();
final Object library;
try {
featureLoader.ensureCExtImplementationLoaded(feature, this);
if (getContext().getOptions().CEXTS_LOG_LOAD) {
RubyLanguage.LOGGER
.info(String.format("loading cext module %s (requested as %s)", expandedPath, feature));
}
library = featureLoader.loadCExtLibrary(feature, expandedPath, currentNode);
} catch (Exception e) {
handleCExtensionException(feature, e);
throw e;
}
final String initFunctionName = "Init_" + getBaseName(expandedPath);
final Object initFunction = featureLoader.findFunctionInLibrary(library, initFunctionName, expandedPath);
final InteropLibrary initFunctionInteropLibrary = InteropLibrary.getFactory().getUncached(initFunction);
if (!initFunctionInteropLibrary.isExecutable(initFunction)) {
throw new RaiseException(
getContext(),
coreExceptions().loadError(initFunctionName + "() is not executable", expandedPath, currentNode));
}
requireMetric("before-execute-" + feature);
try {
InteropNodes
.execute(
initFunction,
ArrayUtils.EMPTY_ARRAY,
initFunctionInteropLibrary,
TranslateInteropExceptionNode.getUncached());
} finally {
requireMetric("after-execute-" + feature);
}
}
@TruffleBoundary
private void handleCExtensionException(String feature, Exception e) {
TranslateExceptionNode.logJavaException(getContext(), this, e);
final Throwable linkErrorException = searchForException("NFIUnsatisfiedLinkError", e);
if (linkErrorException != null) {
final String linkError = linkErrorException.getMessage();
if (getContext().getOptions().CEXTS_LOG_LOAD) {
RubyLanguage.LOGGER.info("unsatisfied link error " + linkError);
}
final String message;
if (feature.equals("openssl.so")) {
message = String.format(
"%s (%s)",
"you may need to install the system OpenSSL library libssl - see https://github.com/oracle/truffleruby/blob/master/doc/user/installing-libssl.md",
linkError);
} else {
message = linkError;
}
throw new RaiseException(getContext(), getContext().getCoreExceptions().runtimeError(message, this));
}
final Throwable linkerException = searchForException("LLVMLinkerException", e);
if (linkerException != null) {
final String linkError = linkerException.getMessage();
final String message;
final String home = getContext().getRubyHome();
final String postInstallHook = (home != null ? home + "/" : "") + "lib/truffle/post_install_hook.sh";
// Mismatches between the libssl compiled against and the libssl used at runtime (typically on a different machine)
if (feature.contains("openssl")) {
message = String.format(
"%s (%s)",
"the OpenSSL C extension was compiled against a different libssl than the one used on this system - recompile by running " +
postInstallHook,
linkError);
} else {
message = linkError;
}
throw new RaiseException(getContext(), getContext().getCoreExceptions().runtimeError(message, this));
}
}
private Throwable searchForException(String exceptionClass, Throwable exception) {
while (exception != null) {
if (exception.getClass().getSimpleName().equals(exceptionClass)) {
return exception;
}
exception = exception.getCause();
}
return null;
}
@TruffleBoundary
private String getBaseName(String path) {
final String name = new File(path).getName();
final int firstDot = name.indexOf('.');
if (firstDot == -1) {
return name;
} else {
return name.substring(0, firstDot);
}
}
public boolean isFeatureLoaded(Object feature) {
final Object included = isInLoadedFeatures
.call(coreLibrary().truffleFeatureLoaderModule, "feature_provided?", feature, true);
return booleanCastNode.executeToBoolean(included);
}
private void addToLoadedFeatures(Object feature) {
addToLoadedFeatures.call(coreLibrary().truffleFeatureLoaderModule, "provide_feature", feature);
}
private void warnCircularRequire(String path) {
if (warningNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
warningNode = insert(new WarningNode());
}
if (warningNode.shouldWarn()) {
final SourceSection sourceSection = getContext().getCallStack().getTopMostUserSourceSection();
warningNode.warningMessage(
sourceSection,
"loading in progress, circular require considered harmful - " + path);
}
}
private void requireMetric(String id) {
if (Metrics.getMetricsTime() && getContext().getOptions().METRICS_TIME_REQUIRE) {
Metrics.printTime(id);
}
}
}