-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathffi.rs
More file actions
633 lines (577 loc) · 21.1 KB
/
Copy pathffi.rs
File metadata and controls
633 lines (577 loc) · 21.1 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! In-process FFI transport: hosts the Copilot runtime by loading its native
//! library and speaking JSON-RPC over its C ABI,
//! instead of spawning a CLI child process and communicating over stdio/TCP.
//!
//! The runtime's `host_start` export spawns the residual TypeScript worker
//! itself — the packaged single-file CLI (`copilot --embedded-host`) or, for
//! dev, `node dist-cli/index.js --embedded-host`. JSON-RPC frames are pumped
//! across the ABI: writes go to `connection_write`; inbound frames arrive on a
//! native callback that feeds an async reader. The framing is unchanged — the
//! same LSP `Content-Length:` frames the stdio transport uses.
use std::collections::HashMap;
use std::ffi::c_void;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicU32, AtomicUsize, Ordering};
use std::sync::{Arc, OnceLock};
use std::task::{Context, Poll};
use libloading::Library;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::sync::mpsc;
use tracing::debug;
use crate::{Error, ErrorKind};
type OutboundCallback = unsafe extern "C" fn(*mut c_void, *const u8, usize);
type HostStartFn = unsafe extern "C" fn(*const u8, usize, *const u8, usize) -> u32;
type HostShutdownFn = unsafe extern "C" fn(u32) -> bool;
#[allow(clippy::type_complexity)]
type ConnectionOpenFn = unsafe extern "C" fn(
u32,
OutboundCallback,
*mut c_void,
*const u8,
usize,
*const u8,
usize,
*const u8,
usize,
) -> u32;
type ConnectionWriteFn = unsafe extern "C" fn(u32, *const u8, usize) -> bool;
type ConnectionCloseFn = unsafe extern "C" fn(u32) -> bool;
/// State handed to the native side as `user_data` so the outbound callback can
/// route inbound frames back to the reader.
struct CallbackState {
tx: mpsc::UnboundedSender<Vec<u8>>,
active_callbacks: AtomicUsize,
closing: AtomicBool,
}
extern "C" fn on_outbound(user_data: *mut c_void, bytes: *const u8, len: usize) {
if user_data.is_null() || bytes.is_null() || len == 0 {
return;
}
let state = unsafe { &*(user_data as *const CallbackState) };
state.active_callbacks.fetch_add(1, Ordering::SeqCst);
if state.closing.load(Ordering::SeqCst) {
state.active_callbacks.fetch_sub(1, Ordering::SeqCst);
return;
}
let slice = unsafe { std::slice::from_raw_parts(bytes, len) };
let _ = state.tx.send(slice.to_vec());
state.active_callbacks.fetch_sub(1, Ordering::SeqCst);
}
/// Bound exports and connection lifecycle state, shared between the
/// [`FfiWriter`] and the owning [`Client`]. The cdylib itself is loaded
/// process-globally and never unloaded (see [`load_library`]), so this holds
/// only the bound fn pointers and connection state.
pub(crate) struct FfiShared {
host_shutdown: HostShutdownFn,
connection_write: ConnectionWriteFn,
connection_close: ConnectionCloseFn,
server_id: AtomicU32,
connection_id: AtomicU32,
callback_state: AtomicPtr<CallbackState>,
closed: AtomicBool,
operation_lock: parking_lot::Mutex<()>,
library_path: PathBuf,
}
// The raw fn pointers and the boxed callback state are safe to move across
// threads: the native side copies buffers synchronously and the callback only
// forwards to a thread-safe channel sender.
unsafe impl Send for FfiShared {}
unsafe impl Sync for FfiShared {}
impl FfiShared {
/// Close the connection, shut the host down, and free the callback state.
/// Idempotent; called from [`Client::stop`], drop, and on startup failure.
pub(crate) fn close(&self) {
let _operation = self.operation_lock.lock();
if self.closed.swap(true, Ordering::SeqCst) {
return;
}
let state = self.callback_state.load(Ordering::SeqCst);
if !state.is_null() {
unsafe { &*state }.closing.store(true, Ordering::SeqCst);
}
let conn = self.connection_id.swap(0, Ordering::SeqCst);
if conn != 0 {
unsafe { (self.connection_close)(conn) };
}
let server = self.server_id.swap(0, Ordering::SeqCst);
if server != 0 {
unsafe { (self.host_shutdown)(server) };
}
// Free the callback state only after the connection is closed and the
// host is shut down, so native can no longer invoke the callback.
let state = self
.callback_state
.swap(std::ptr::null_mut(), Ordering::SeqCst);
if !state.is_null() {
while unsafe { &*state }.active_callbacks.load(Ordering::SeqCst) != 0 {
std::thread::yield_now();
}
drop(unsafe { Box::from_raw(state) });
}
debug!(library = %self.library_path.display(), "FFI runtime connection closed");
}
fn write_frame(&self, frame: &[u8]) -> bool {
let _operation = self.operation_lock.lock();
if self.closed.load(Ordering::SeqCst) {
return false;
}
let conn = self.connection_id.load(Ordering::SeqCst);
if conn == 0 {
return false;
}
unsafe { (self.connection_write)(conn, frame.as_ptr(), frame.len()) }
}
}
impl Drop for FfiShared {
fn drop(&mut self) {
self.close();
}
}
/// Read side of the FFI transport, fed by the native outbound callback via an
/// unbounded channel. Implements [`AsyncRead`] for the JSON-RPC read loop.
pub(crate) struct FfiReader {
rx: mpsc::UnboundedReceiver<Vec<u8>>,
leftover: Vec<u8>,
pos: usize,
}
impl AsyncRead for FfiReader {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
if self.pos >= self.leftover.len() {
match self.rx.poll_recv(cx) {
Poll::Ready(Some(chunk)) => {
self.leftover = chunk;
self.pos = 0;
}
Poll::Ready(None) => return Poll::Ready(Ok(())),
Poll::Pending => return Poll::Pending,
}
}
let available = self.leftover.len() - self.pos;
let n = available.min(buf.remaining());
let start = self.pos;
buf.put_slice(&self.leftover[start..start + n]);
self.pos += n;
Poll::Ready(Ok(()))
}
}
/// Write side of the FFI transport. Each frame is forwarded synchronously to
/// the native `connection_write` export (native copies before returning).
pub(crate) struct FfiWriter {
shared: Arc<FfiShared>,
}
impl AsyncWrite for FfiWriter {
fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
if self.shared.write_frame(buf) {
Poll::Ready(Ok(buf.len()))
} else {
Poll::Ready(Err(std::io::Error::new(
std::io::ErrorKind::BrokenPipe,
"failed to write a frame to the in-process runtime connection",
)))
}
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Poll::Ready(Ok(()))
}
}
/// Prepared FFI host: the bound cdylib exports plus the spawn arguments needed
/// to start the runtime worker. The cdylib is loaded process-globally and never
/// unloaded (see [`load_library`]).
pub(crate) struct FfiHost {
library_path: PathBuf,
entrypoint: PathBuf,
environment: Vec<(String, String)>,
args: Vec<String>,
host_start: HostStartFn,
host_shutdown: HostShutdownFn,
connection_open: ConnectionOpenFn,
connection_write: ConnectionWriteFn,
connection_close: ConnectionCloseFn,
}
// SAFETY: as for `FfiShared` — the bound exports are plain fn pointers, safe to
// move to the blocking thread that starts the host.
unsafe impl Send for FfiHost {}
impl FfiHost {
/// Load the cdylib next to `entrypoint` and bind its exports.
///
/// `entrypoint` is the packaged single-file CLI binary or, for dev, a
/// `.js` file launched via `node`. The native library is resolved relative
/// to the entrypoint directory, supporting both packaged and development
/// layouts.
pub(crate) fn create(
entrypoint: &Path,
environment: Vec<(String, String)>,
args: Vec<String>,
) -> Result<Self, Error> {
let entrypoint = std::fs::canonicalize(entrypoint)
.map(path_for_child_process)
.map_err(|e| {
Error::with_message(
ErrorKind::InvalidConfig,
format!(
"failed to resolve in-process CLI entrypoint '{}': {e}",
entrypoint.display()
),
)
})?;
let library_path =
std::fs::canonicalize(resolve_library_path(&entrypoint)?).map_err(|e| {
Error::with_message(
ErrorKind::InvalidConfig,
format!("failed to resolve in-process runtime library: {e}"),
)
})?;
let lib = load_library(&library_path)?;
let host_start = *bind::<HostStartFn>(lib, b"copilot_runtime_host_start\0", &library_path)?;
let host_shutdown =
*bind::<HostShutdownFn>(lib, b"copilot_runtime_host_shutdown\0", &library_path)?;
let connection_open =
*bind::<ConnectionOpenFn>(lib, b"copilot_runtime_connection_open\0", &library_path)?;
let connection_write =
*bind::<ConnectionWriteFn>(lib, b"copilot_runtime_connection_write\0", &library_path)?;
let connection_close =
*bind::<ConnectionCloseFn>(lib, b"copilot_runtime_connection_close\0", &library_path)?;
Ok(Self {
library_path,
entrypoint,
environment,
args,
host_start,
host_shutdown,
connection_open,
connection_write,
connection_close,
})
}
/// Start the runtime worker and open the FFI JSON-RPC connection.
///
/// `host_start` blocks until the worker connects back and signals
/// readiness (up to ~30s), and must not run on an async executor thread, so
/// the blocking handshake is offloaded to [`tokio::task::spawn_blocking`].
pub(crate) async fn start(self) -> Result<(FfiReader, FfiWriter, Arc<FfiShared>), Error> {
tokio::task::spawn_blocking(move || self.start_blocking())
.await
.map_err(|e| {
Error::with_message(
ErrorKind::InvalidConfig,
format!("in-process runtime startup task failed: {e}"),
)
})?
}
fn start_blocking(self) -> Result<(FfiReader, FfiWriter, Arc<FfiShared>), Error> {
let argv = build_argv_json(&self.entrypoint, &self.args);
let env = build_env_json(&self.environment);
let (env_ptr, env_len) = match &env {
Some(bytes) => (bytes.as_ptr(), bytes.len()),
None => (std::ptr::null(), 0),
};
let server_id = unsafe { (self.host_start)(argv.as_ptr(), argv.len(), env_ptr, env_len) };
if server_id == 0 {
return Err(Error::with_message(
ErrorKind::InvalidConfig,
format!(
"copilot_runtime_host_start failed (library '{}', entrypoint '{}')",
self.library_path.display(),
self.entrypoint.display()
),
));
}
let (tx, rx) = mpsc::unbounded_channel::<Vec<u8>>();
let state_ptr = Box::into_raw(Box::new(CallbackState {
tx,
active_callbacks: AtomicUsize::new(0),
closing: AtomicBool::new(false),
}));
let connection_id = unsafe {
(self.connection_open)(
server_id,
on_outbound,
state_ptr as *mut c_void,
std::ptr::null(),
0,
std::ptr::null(),
0,
std::ptr::null(),
0,
)
};
if connection_id == 0 {
drop(unsafe { Box::from_raw(state_ptr) });
unsafe { (self.host_shutdown)(server_id) };
return Err(Error::with_message(
ErrorKind::InvalidConfig,
"copilot_runtime_connection_open failed",
));
}
let shared = Arc::new(FfiShared {
host_shutdown: self.host_shutdown,
connection_write: self.connection_write,
connection_close: self.connection_close,
server_id: AtomicU32::new(server_id),
connection_id: AtomicU32::new(connection_id),
callback_state: AtomicPtr::new(state_ptr),
closed: AtomicBool::new(false),
operation_lock: parking_lot::Mutex::new(()),
library_path: self.library_path.clone(),
});
debug!(
library = %self.library_path.display(),
server_id, connection_id, "FFI runtime host started"
);
let reader = FfiReader {
rx,
leftover: Vec::new(),
pos: 0,
};
let writer = FfiWriter {
shared: Arc::clone(&shared),
};
Ok((reader, writer, shared))
}
}
fn bind<'lib, T>(
lib: &'lib Library,
symbol: &[u8],
library_path: &Path,
) -> Result<libloading::Symbol<'lib, T>, Error> {
match unsafe { lib.get::<T>(symbol) } {
Ok(export) => Ok(export),
Err(e) => Err(Error::with_message(
ErrorKind::InvalidConfig,
format!(
"in-process runtime library '{}' is missing an expected export ({}): {e}",
library_path.display(),
String::from_utf8_lossy(symbol.strip_suffix(b"\0").unwrap_or(symbol))
),
)),
}
}
/// Loads the runtime cdylib once per process and never unloads it, returning a
/// `'static` reference. Subsequent loads of the same path reuse the first
/// handle.
///
/// The library stays mapped because native worker threads can outlive an
/// individual connection teardown.
fn load_library(library_path: &Path) -> Result<&'static Library, Error> {
static LIBRARIES: OnceLock<parking_lot::Mutex<HashMap<PathBuf, &'static Library>>> =
OnceLock::new();
let cache = LIBRARIES.get_or_init(|| parking_lot::Mutex::new(HashMap::new()));
let mut guard = cache.lock();
if let Some(lib) = guard.get(library_path) {
return Ok(*lib);
}
let lib = unsafe { Library::new(library_path) }.map_err(|e| {
Error::with_message(
ErrorKind::InvalidConfig,
format!(
"failed to load in-process runtime library '{}': {e}",
library_path.display()
),
)
})?;
// Leak the library so it is never unloaded for the process lifetime.
let leaked: &'static Library = Box::leak(Box::new(lib));
guard.insert(library_path.to_path_buf(), leaked);
Ok(leaked)
}
/// The natural platform shared-library file name for the runtime cdylib — the
/// `.node` file renamed to what the Rust cdylib would be called on this OS.
fn natural_library_name() -> &'static str {
if cfg!(windows) {
"copilot_runtime.dll"
} else if cfg!(target_os = "macos") {
"libcopilot_runtime.dylib"
} else {
"libcopilot_runtime.so"
}
}
/// The package prebuild folder name for the current host.
pub(crate) fn prebuilds_folder() -> Option<String> {
let platform = if cfg!(target_os = "windows") {
"win32"
} else if cfg!(target_os = "macos") {
"darwin"
} else if cfg!(target_os = "linux") {
"linux"
} else {
return None;
};
let arch = if cfg!(target_arch = "x86_64") {
"x64"
} else if cfg!(target_arch = "aarch64") {
"arm64"
} else {
return None;
};
Some(format!("{platform}-{arch}"))
}
fn resolve_library_path(entrypoint: &Path) -> Result<PathBuf, Error> {
let dir = entrypoint.parent().ok_or_else(|| {
Error::with_message(
ErrorKind::InvalidConfig,
format!(
"could not determine directory for CLI entrypoint '{}'",
entrypoint.display()
),
)
})?;
// Bundled/flat layout: natural shared-library name next to the CLI.
let flat = dir.join(natural_library_name());
if flat.is_file() {
return Ok(flat);
}
// Development package layout.
let prebuilds =
prebuilds_folder().map(|folder| dir.join("prebuilds").join(folder).join("runtime.node"));
if let Some(prebuilds_path) = &prebuilds
&& prebuilds_path.is_file()
{
return Ok(prebuilds_path.clone());
}
Err(Error::with_message(
ErrorKind::BinaryNotFound {
name: natural_library_name().into(),
hint: Some(format!(
"native runtime library not found next to '{}'. Enable the \
`bundled-in-process` feature or set COPILOT_CLI_PATH to a compatible CLI package.",
entrypoint.display()
)),
},
"native runtime library not found",
))
}
#[cfg(windows)]
fn path_for_child_process(path: PathBuf) -> PathBuf {
use std::ffi::OsString;
use std::os::windows::ffi::{OsStrExt, OsStringExt};
const VERBATIM_PREFIX: &[u16] = &[b'\\' as u16, b'\\' as u16, b'?' as u16, b'\\' as u16];
const UNC_PREFIX: &[u16] = &[b'U' as u16, b'N' as u16, b'C' as u16, b'\\' as u16];
let encoded: Vec<u16> = path.as_os_str().encode_wide().collect();
let Some(stripped) = encoded.strip_prefix(VERBATIM_PREFIX) else {
return path;
};
let normalized = if let Some(unc_path) = stripped.strip_prefix(UNC_PREFIX) {
let mut result = vec![b'\\' as u16, b'\\' as u16];
result.extend_from_slice(unc_path);
result
} else {
stripped.to_vec()
};
PathBuf::from(OsString::from_wide(&normalized))
}
#[cfg(not(windows))]
fn path_for_child_process(path: PathBuf) -> PathBuf {
path
}
fn build_argv_json(entrypoint: &Path, extra_args: &[String]) -> Vec<u8> {
// A `.js` entrypoint (dev / dist-cli) is launched via node; the packaged
// single-file CLI binary embeds its own Node and is invoked directly.
let entrypoint_str = entrypoint.to_string_lossy().into_owned();
let is_js = entrypoint
.extension()
.and_then(|ext| ext.to_str())
.is_some_and(|ext| ext.eq_ignore_ascii_case("js"));
let mut argv: Vec<String> = if is_js {
vec![
"node".to_string(),
entrypoint_str,
"--embedded-host".to_string(),
"--no-auto-update".to_string(),
]
} else {
vec![
entrypoint_str,
"--embedded-host".to_string(),
"--no-auto-update".to_string(),
]
};
argv.extend_from_slice(extra_args);
serde_json::to_vec(&argv).expect("argv serializes")
}
fn build_env_json(environment: &[(String, String)]) -> Option<Vec<u8>> {
if environment.is_empty() {
return None;
}
let map: serde_json::Map<String, serde_json::Value> = environment
.iter()
.map(|(k, v)| (k.clone(), serde_json::Value::String(v.clone())))
.collect();
Some(serde_json::to_vec(&map).expect("env serializes"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn argv_pins_worker_and_appends_client_options() {
let argv: Vec<String> = serde_json::from_slice(&build_argv_json(
Path::new("copilot"),
&["--log-level".into(), "debug".into()],
))
.unwrap();
assert_eq!(
argv,
[
"copilot",
"--embedded-host",
"--no-auto-update",
"--log-level",
"debug"
]
);
}
#[test]
fn javascript_entrypoint_uses_node() {
let argv: Vec<String> =
serde_json::from_slice(&build_argv_json(Path::new("index.js"), &[])).unwrap();
assert_eq!(
argv,
["node", "index.js", "--embedded-host", "--no-auto-update"]
);
}
#[cfg(windows)]
#[test]
fn child_process_path_removes_windows_verbatim_prefix() {
assert_eq!(
path_for_child_process(PathBuf::from(r"\\?\D:\a\copilot-sdk\index.js")),
PathBuf::from(r"D:\a\copilot-sdk\index.js")
);
assert_eq!(
path_for_child_process(PathBuf::from(r"\\?\UNC\server\share\copilot-sdk\index.js")),
PathBuf::from(r"\\server\share\copilot-sdk\index.js")
);
}
#[test]
fn environment_is_omitted_when_empty() {
assert_eq!(build_env_json(&[]), None);
}
#[test]
fn environment_serializes_worker_overrides() {
let env: serde_json::Value = serde_json::from_slice(
&build_env_json(&[
("COPILOT_HOME".into(), "state".into()),
("COPILOT_DISABLE_KEYTAR".into(), "1".into()),
])
.unwrap(),
)
.unwrap();
assert_eq!(
env,
serde_json::json!({
"COPILOT_HOME": "state",
"COPILOT_DISABLE_KEYTAR": "1",
})
);
}
}