-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathPluginRegistry.cpp
363 lines (306 loc) · 11 KB
/
PluginRegistry.cpp
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
//===--- PluginRegistry.cpp -----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/AST/PluginRegistry.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/LoadDynamicLibrary.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Program.h"
#include "swift/Basic/Sandbox.h"
#include "swift/Basic/StringExtras.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Config/config.h"
#include <signal.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#elif defined(_WIN32)
#include <io.h>
#endif
using namespace swift;
PluginRegistry::PluginRegistry() {
dumpMessaging = ::getenv("SWIFT_DUMP_PLUGIN_MESSAGING") != nullptr;
}
CompilerPlugin::~CompilerPlugin() {
// Let ASTGen do cleanup things.
if (this->cleanup)
this->cleanup();
}
llvm::Expected<std::unique_ptr<InProcessPlugins>>
InProcessPlugins::create(const char *serverPath) {
std::string err;
auto server = loadLibrary(serverPath, &err);
if (!server) {
return llvm::createStringError(llvm::inconvertibleErrorCode(), err);
}
auto funcPtr =
getAddressOfSymbol(server, "swift_inproc_plugins_handle_message");
if (!funcPtr) {
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"entry point not found in '%s'", serverPath);
}
return std::unique_ptr<InProcessPlugins>(new InProcessPlugins(
serverPath, reinterpret_cast<HandleMessageFunction>(funcPtr)));
}
llvm::Error InProcessPlugins::sendMessage(llvm::StringRef message) {
assert(receivedResponse.empty() &&
"sendMessage() called before consuming previous response?");
if (shouldDumpMessaging()) {
llvm::dbgs() << "->(plugin:0) " << message << '\n';
}
char *responseData = nullptr;
size_t responseLength = 0;
bool hadError = handleMessageFn(message.data(), message.size(), &responseData,
&responseLength);
// 'responseData' now holds a response message or error message depending on
// 'hadError'. Either way, it's our responsibility to deallocate it.
SWIFT_DEFER { free(responseData); };
if (hadError) {
return llvm::createStringError(llvm::inconvertibleErrorCode(),
StringRef(responseData, responseLength));
}
// Store the response and wait for 'waitForNextMessage()' call.
receivedResponse = std::string(responseData, responseLength);
if (shouldDumpMessaging()) {
llvm::dbgs() << "<-(plugin:0) " << receivedResponse << "\n";
}
assert(!receivedResponse.empty() && "received empty response");
return llvm::Error::success();
}
llvm::Expected<std::string> InProcessPlugins::waitForNextMessage() {
assert(!receivedResponse.empty() &&
"waitForNextMessage() called without response data.");
SWIFT_DEFER { receivedResponse = ""; };
return std::move(receivedResponse);
}
llvm::Expected<CompilerPlugin *>
PluginRegistry::getInProcessPlugins(llvm::StringRef serverPath) {
std::lock_guard<std::mutex> lock(mtx);
if (!inProcessPlugins) {
auto server = InProcessPlugins::create(serverPath.str().c_str());
if (!server) {
return llvm::handleErrors(
server.takeError(), [&](const llvm::ErrorInfoBase &err) {
return llvm::createStringError(
err.convertToErrorCode(),
"failed to load in-process plugin server: " + serverPath +
"; " + err.message());
});
}
inProcessPlugins = std::move(server.get());
} else if (inProcessPlugins->getPath() != serverPath) {
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"loading multiple in-process servers are not supported: '%s' and '%s'",
inProcessPlugins->getPath().data(), serverPath.str().c_str());
}
inProcessPlugins->setDumpMessaging(dumpMessaging);
return inProcessPlugins.get();
}
llvm::Expected<CompilerPlugin *>
PluginRegistry::loadExecutablePlugin(StringRef path, bool disableSandbox) {
llvm::sys::fs::file_status stat;
if (auto err = llvm::sys::fs::status(path, stat)) {
return llvm::errorCodeToError(err);
}
std::lock_guard<std::mutex> lock(mtx);
// See if the plugin is already loaded.
auto &storage = LoadedPluginExecutables[path];
if (storage) {
// See if the loaded one is still usable.
if (storage->getLastModificationTime() == stat.getLastModificationTime())
return storage.get();
// The plugin is updated. Close the previously opened plugin.
storage = nullptr;
}
if (!llvm::sys::fs::exists(stat)) {
return llvm::createStringError(std::errc::no_such_file_or_directory,
"not found");
}
if (!llvm::sys::fs::can_execute(path)) {
return llvm::createStringError(std::errc::permission_denied,
"not executable");
}
auto plugin = std::make_unique<LoadedExecutablePlugin>(
path, stat.getLastModificationTime(), disableSandbox);
plugin->setDumpMessaging(dumpMessaging);
// Launch here to see if it's actually executable, and diagnose (by returning
// an error) if necessary.
if (auto error = plugin->spawnIfNeeded()) {
return std::move(error);
}
storage = std::move(plugin);
return storage.get();
}
llvm::Error LoadedExecutablePlugin::spawnIfNeeded() {
if (Process) {
// NOTE: We don't check the mtime here because 'stat(2)' call is too heavy.
// PluginRegistry::loadExecutablePlugin() checks it and replace this object
// itself if the plugin is updated.
return llvm::Error::success();
}
// Create command line arguments.
SmallVector<StringRef, 4> command{getPath()};
// Apply sandboxing.
llvm::BumpPtrAllocator Allocator;
if (!disableSandbox) {
Sandbox::apply(command, Allocator);
}
// Launch.
auto childInfo = ExecuteWithPipe(command[0], command);
if (!childInfo) {
return llvm::errorCodeToError(childInfo.getError());
}
Process = std::make_unique<PluginProcess>(childInfo->ProcessInfo,
childInfo->Read, childInfo->Write);
// Call "on reconnect" callbacks.
for (auto *callback : getOnReconnectCallbacks()) {
(*callback)();
}
return llvm::Error::success();
}
LoadedExecutablePlugin::PluginProcess::~PluginProcess() {
#if defined(_WIN32)
_close(input);
_close(output);
#else
close(input);
close(output);
#endif
llvm::sys::Wait(process, /*SecondsToWait=*/0);
}
ssize_t LoadedExecutablePlugin::PluginProcess::read(void *buf,
size_t nbyte) const {
#if defined(_WIN32)
size_t nread = 0;
while (nread < nbyte) {
int n = _read(input, static_cast<char*>(buf) + nread,
std::min(static_cast<size_t>(UINT32_MAX), nbyte - nread));
if (n <= 0)
break;
nread += n;
}
return nread;
#else
ssize_t bytesToRead = nbyte;
void *ptr = buf;
#if defined(SIGPIPE)
/// Ignore SIGPIPE while reading.
auto *old_handler = signal(SIGPIPE, SIG_IGN);
SWIFT_DEFER { signal(SIGPIPE, old_handler); };
#endif
while (bytesToRead > 0) {
ssize_t readingSize = std::min(ssize_t(INT32_MAX), bytesToRead);
ssize_t readSize = ::read(input, ptr, readingSize);
if (readSize <= 0) {
// 0: EOF (the plugin exited?), -1: error (e.g. broken pipe.)
// FIXME: Mark the plugin 'stale' and relaunch later.
break;
}
ptr = static_cast<char *>(ptr) + readSize;
bytesToRead -= readSize;
}
return nbyte - bytesToRead;
#endif
}
ssize_t LoadedExecutablePlugin::PluginProcess::write(const void *buf,
size_t nbyte) const {
#if defined(_WIN32)
size_t nwritten = 0;
while (nwritten < nbyte) {
int n = _write(output, static_cast<const char *>(buf) + nwritten,
std::min(static_cast<size_t>(UINT32_MAX), nbyte - nwritten));
if (n <= 0)
break;
nwritten += n;
}
return nwritten;
#else
ssize_t bytesToWrite = nbyte;
const void *ptr = buf;
#if defined(SIGPIPE)
/// Ignore SIGPIPE while writing.
auto *old_handler = signal(SIGPIPE, SIG_IGN);
SWIFT_DEFER { signal(SIGPIPE, old_handler); };
#endif
while (bytesToWrite > 0) {
ssize_t writingSize = std::min(ssize_t(INT32_MAX), bytesToWrite);
ssize_t writtenSize = ::write(output, ptr, writingSize);
if (writtenSize <= 0) {
// -1: error (e.g. broken pipe,)
// FIXME: Mark the plugin 'stale' and relaunch later.
break;
}
ptr = static_cast<const char *>(ptr) + writtenSize;
bytesToWrite -= writtenSize;
}
return nbyte - bytesToWrite;
#endif
}
llvm::Error LoadedExecutablePlugin::sendMessage(llvm::StringRef message) {
ssize_t writtenSize = 0;
if (shouldDumpMessaging()) {
llvm::dbgs() << "->(plugin:" << Process->process.Pid << ") " << message << '\n';
}
const char *data = message.data();
size_t size = message.size();
// Write header (message size).
uint64_t header = llvm::support::endian::byte_swap(uint64_t(size),
llvm::endianness::little);
writtenSize = Process->write(&header, sizeof(header));
if (writtenSize != sizeof(header)) {
setStale();
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"failed to write plugin message header");
}
// Write message.
writtenSize = Process->write(data, size);
if (writtenSize != ssize_t(size)) {
setStale();
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"failed to write plugin message data");
}
return llvm::Error::success();
}
llvm::Expected<std::string> LoadedExecutablePlugin::waitForNextMessage() {
ssize_t readSize = 0;
// Read header (message size).
uint64_t header;
readSize = Process->read(&header, sizeof(header));
if (readSize != sizeof(header)) {
setStale();
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"failed to read plugin message header");
}
size_t size =
llvm::support::endian::read<uint64_t>(&header, llvm::endianness::little);
// Read message.
std::string message;
message.reserve(size);
auto sizeToRead = size;
while (sizeToRead > 0) {
char buffer[4096];
readSize = Process->read(buffer, std::min(sizeof(buffer), sizeToRead));
if (readSize == 0) {
setStale();
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"failed to read plugin message data");
}
sizeToRead -= readSize;
message.append(buffer, readSize);
}
if (shouldDumpMessaging()) {
llvm::dbgs() << "<-(plugin:" << Process->process.Pid << ") " << message << "\n";
}
return message;
}