Commit Graph

35 Commits

Author SHA1 Message Date
Rintaro Ishizaki
8474d27b2f Revert "[Macros] Mitigate plugin process 'wait' failure"
This reverts commit be25ea2584.
2025-05-15 16:15:25 -07:00
Rintaro Ishizaki
4a62ef86d4 [Macros] Terminate macro plugin
Instead of expecting the plugin read `EOF` and exit, proactively
terminate the plugin and `wait` to reap it.
2025-05-14 13:09:43 -07:00
Rintaro Ishizaki
be25ea2584 [Macros] Mitigate plugin process 'wait' failure
Previously, the compiler waited the plugin process with
`llvm::sys::Wait(pid, /*SecondsToWait=*/1)`. But in the `Wait`
implementation, it sets the `alarm(SecondsToWait)`, then
`wait4(pid, ..)` so if the alarm fires before the `wait4` call,
it may miss the timeout and can wait indefinitely. To mitigate that
risk,use `10` for the timeout value.

rdar://150474701
2025-05-14 12:38:28 -07:00
Rintaro Ishizaki
6e4580a862 [Macros] Ensure to kill and reap plugin process
`llvm::sys::Wait(process, /*SecondsToWait=*/0)` perform a non-blocking
`wait`. That means the plugin goes a zombie if it hasn't exited.
Set `SecondsToWait` 1  so it wait for 1 second and kill it on the time
out. Usually, when the pipe is closed, the plugins detect the EOF in
stdin and exits immediately, fo the parent process usually don't wait
for the timeout.

rdar://148110944
2025-03-31 13:10:45 -07:00
Alex Hoppen
40b91aa97a Share logic to load dynamic libraries between SourceKit plugins and macro plugins 2025-01-03 16:23:16 +01:00
swift-ci
ad721b21c9 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-14 15:33:31 -07:00
Rintaro Ishizaki
e4dc04d564 [Macros] Reap plugin process as soon as it ends 2024-10-14 09:44:38 -07:00
swift-ci
40aee60b24 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-08 19:09:12 -07:00
Allan Shortlidge
c9d1ac07eb AST: Remove unused server field from InProcessPlugins. 2024-10-08 14:32:56 -07:00
swift-ci
474b11c0ba Merge remote-tracking branch 'origin/main' into rebranch 2024-08-15 16:15:10 -07:00
Rintaro Ishizaki
2ec4dc8d1c [Macros] Don't dlclose InProcessPluginServer shared library
llvm::DynamicLibrary does dlclose on all the libraries when global
destructor runs. Which causes dangling pointers if the library contains
Swift code.

rdar://131048379
2024-08-14 12:31:55 -07:00
Ben Barham
b7954411ec Merge remote-tracking branch 'origin/main' into manually-merge-main-to-rebranch
Conflicts:
  - `include/swift/AST/PluginRegistry.h`
2024-06-27 14:56:11 -07:00
Rintaro Ishizaki
5ebd715a2a Merge pull request #73725 from rintaro/macros-inproc-plugin
[Macros] In-process plugin server
2024-06-26 06:33:38 -07:00
Xi Ge
736ccef626 Merge remote-tracking branch 'apple/main' into rebranch 2024-06-20 15:16:55 -07:00
Rintaro Ishizaki
2f7aa428db [Macros] In-process plugin server
Separate swift-syntax libs for the compiler and for the library plugins.
Compiler communicates with library plugins using serialized messages
just like executable plugins.

* `lib/swift/host/compiler/lib_Compiler*.dylib`(`lib/CompilerSwiftSyntax`):
  swift-syntax libraries for compiler. Library evolution is disabled.
* Compiler (`ASTGen` and `swiftIDEUtilsBridging`) only depends on
  `lib/swift/host/compiler` libraries.
* `SwiftInProcPluginServer`: In-process plugin server shared library.
  This has one `swift_inproc_plugins_handle_message` entry point that
  receives a message and return the response.
* In the compiler
  * Add `-in-process-plugin-server-path` front-end option, which specifies
    the `SwiftInProcPluginServer` shared library path.
  * Remove `LoadedLibraryPlugin`, because all library plugins are managed
    by `SwiftInProcPluginServer`
  * Introduce abstract `CompilerPlugin` class that has 2 subclasses:
    * `LoadedExecutablePlugin` existing class that represents an
      executable plugin
    * `InProcessPlugins` wraps `dlopen`ed `SwiftInProcPluginServer`
  * Unified the code path in `TypeCheckMacros.cpp` and `ASTGen`, the
    difference between executable plugins and library plugins are now
    abstracted by `CompilerPlugin`
2024-06-17 11:36:52 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
swift-ci
389f10daf1 Merge remote-tracking branch 'origin/main' into rebranch 2024-05-09 20:35:20 -07:00
Rintaro Ishizaki
0a0e665c22 [Macros] Ensure reaping executable plugin process
rdar://126489446
2024-05-09 16:27:01 -07:00
Ben Barham
cacfd3e3ae Rename llvm::support::endianness to llvm::endianness
LLVM is gearing up to move to `std::endianness` and as part of that has
moved `llvm::support::endianness` to `llvm::endianness`
(bbdbcd83e6702f314d147a680247058a899ba261). Rename our uses.
2024-04-08 08:58:58 -07:00
Rintaro Ishizaki
0c9f099d88 [Macros] Add option to disable sandbox for exectuable plugins
`-disable-sandbox` to disable sandboxing when invoking subprocess from
from the frontend. Since `sandbox(7)` in macOS doesn't support nested
sandbox, complation used to fail when the parent build process is sandboxed.
2023-11-27 13:44:40 -08:00
Rintaro Ishizaki
bdd4c005e5 [Macros] Diagnose how ExternalMacroDefinition request was failed
Return the failure reason as the result.
2023-10-12 16:08:48 -07:00
Saleem Abdulrasool
0000534799 Update PluginRegistry.cpp 2023-09-03 16:49:02 -07:00
Saleem Abdulrasool
06ad3a75b6 AST,Basic: update support for process execution on Windows
Implement process launching on Windows to support macros.  Prefer to use
the LLVM types wherever possible.  The pipes are converted into file
descriptors as the types are internal to the process.  This allows us to
have similar paths on both sides and avoid having to drag in `Windows.h`
for the definition of `HANDLE`.  This is the core missing functionality
for Windows to support macros.
2023-09-03 10:45:03 -07:00
Rintaro Ishizaki
e93e4ba2cd [Macro] Handle error during initializing executable plugins
Previously, the compiler fails to read the result from 'getCapability'
IPC message, it used to just emit a fatalError and crashed.
Instead, propagate the error status and diagnose it.

rdar://108022847
2023-05-04 09:03:21 -07:00
Rintaro Ishizaki
90e1558341 Use make_unique 2023-04-11 15:01:53 -07:00
Rintaro Ishizaki
f99d70d181 [Macros] PluginRegistry vends 'LoadedLibraryPlugin' instead of 'void *'
`LoadedLibraryPlugin` is currently just a wrapper of `void` pointer from
`dlopen`. This will be convenient for abstracting platform specific
dynamic linrary handling.
2023-04-11 14:59:22 -07:00
Rintaro Ishizaki
a4d1fc6abe [SourceKit] Use a single PluginRegistry in multiple ASTContexts
Make a single 'PluginRegistry' and share it between SwiftASTManager,
IDEInspectionInstance, and CompileInstance. And inject the plugin
registry to ASTContext right after 'CompilerInstance.setup()'

That way, all sema-capable ASTContext in SourceKit share a single
PluginRegistry.
2023-03-27 14:59:08 -07:00
Rintaro Ishizaki
bd9af096d8 [Macro] Add env variable option to dump exectuable plugin messagings
Set 'SWIFT_DUMP_PLUGIN_MESSAGING' env variable to enable it.
2023-03-24 16:39:30 -07:00
Rintaro Ishizaki
a49ab25ae8 [Macros] Recovery after executable plugin crash
When executable plugins crashed or somehow decided to exit, the compiler
should relaunch the plugin executable before sending another message.
2023-03-23 22:26:42 -07:00
Richard Wei
c6139f2f78 [Macros] Emit loaded module trace for plugins loaded from search paths
When loading plugins from `-plugin-path`, use the global `PluginRegistry` to keep a record of what's loaded. Emit these dependencies to the loaded module trace.
2023-03-06 02:19:56 -08:00
Rintaro Ishizaki
a6e0191bc0 [Macros] Improve error handling for executable macro plugins
* Handle broken pipe
* Handle invalid response (malformed message, unexpected message kind)
2023-03-02 17:40:55 -08:00
Rintaro Ishizaki
138934ed64 [PluginRegistry] Error handling tweaks 2023-02-23 10:32:21 -08:00
Rintaro Ishizaki
623043ef6a [AST] Fix Windows compile error 2023-02-22 19:17:01 -08:00
Rintaro Ishizaki
c5a737f9ba [AST] PluginRegistry returns Error/Expected instead of errorMsg out 2023-02-22 17:35:56 -08:00
Rintaro Ishizaki
0e31393024 [Macros] Add executable plugin support
Executable compiler plugins are programs invoked by the host compiler
and communicate with the host with IPC via standard IO (stdin/stdout.)
Each message is serialized in JSON, prefixed with a header which is a
64bit little-endian integer indicating the size of the message.

* Basic/ExecuteWithPipe: External program invocation. Lik
  llvm::sys::ExecuteNoWait() but establishing pipes to the child's
  stdin/stdout
* Basic/Sandbox: Sandboxed execution helper. Create command line
  arguments to be executed in sandbox environment (similar to SwiftPM's
  pluging sandbox)
* AST/PluginRepository: ASTContext independent plugin manager
* ASTGen/PluginHost: Communication with the plugin. Messages are
  serialized by ASTGen/LLVMJSON

rdar://101508815
2023-02-22 10:22:14 -08:00