ckb-script-ipc-common 1.1.0

Common utilities for CKB Script IPC.
Documentation
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
extern crate std;

use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use std::sync::Mutex;

use ckb_vm::cost_model::estimate_cycles;
use ckb_vm::registers::{A0, A1, A2, A7};
use ckb_vm::{Bytes, DefaultMachineRunner, Memory, Register, SupportMachine, Syscalls};

use crate::io::{Error, ErrorKind, Read, Write};

pub const SPAWN: i32 = 2601;
pub const WAIT: i32 = 2602;
pub const PROCESS_ID: i32 = 2603;
pub const PIPE: i32 = 2604;
pub const WRITE: i32 = 2605;
pub const READ: i32 = 2606;
pub const INHERITED_FD: i32 = 2607;
pub const CLOSE: i32 = 2608;
pub const DEBUG_PRINT_SYSCALL_NUMBER: i32 = 2177;

pub const SPAWN_YIELD_CYCLES_BASE: u64 = 800;

pub const FIRST_FD_SLOT: u64 = 2;

struct DebugSyscall {}

impl<Mac: SupportMachine> Syscalls<Mac> for DebugSyscall {
    fn initialize(&mut self, _machine: &mut Mac) -> Result<(), ckb_vm::error::Error> {
        Ok(())
    }

    fn ecall(&mut self, machine: &mut Mac) -> Result<bool, ckb_vm::error::Error> {
        let code = &machine.registers()[A7];
        let code = code.to_i32();

        // in native implementation, we don't support these syscalls
        if code == SPAWN || code == WAIT || code == PROCESS_ID || code == PIPE {
            return Err(ckb_vm::error::Error::IO {
                kind: std::io::ErrorKind::Other,
                data: "unsupported syscalls: spawn, wait, process_id and pipe".into(),
            });
        }
        if code != DEBUG_PRINT_SYSCALL_NUMBER {
            return Ok(false);
        }

        let mut addr = machine.registers()[A0].to_u64();
        let mut buffer = Vec::new();

        loop {
            let byte = machine
                .memory_mut()
                .load8(&Mac::REG::from_u64(addr))?
                .to_u8();
            if byte == 0 {
                break;
            }
            buffer.push(byte);
            addr += 1;
        }

        let s = String::from_utf8_lossy(&buffer);
        std::println!("{:?}", s);
        machine.set_register(A0, Mac::REG::from_u8(0));
        Ok(true)
    }
}
struct ReadSyscall {
    pipe: Pipe,
}

impl ReadSyscall {
    pub fn new(pipe: Pipe) -> Self {
        Self { pipe }
    }
}

impl<Mac: SupportMachine> Syscalls<Mac> for ReadSyscall {
    fn initialize(&mut self, _machine: &mut Mac) -> Result<(), ckb_vm::error::Error> {
        Ok(())
    }

    fn ecall(&mut self, machine: &mut Mac) -> Result<bool, ckb_vm::error::Error> {
        let code = &machine.registers()[A7];
        if code.to_i32() != READ {
            return Ok(false);
        }
        let fd = machine.registers()[A0].to_u64();
        if fd != FIRST_FD_SLOT {
            return Err(ckb_vm::error::Error::IO {
                kind: std::io::ErrorKind::Other,
                data: "can only read on pipe 2".into(),
            });
        }
        let buffer_addr = machine.registers()[A1].clone();
        let length_addr = machine.registers()[A2].clone();
        let length = machine.memory_mut().load64(&length_addr)?.to_u64() as usize;
        let mut buf = vec![0; length];
        let real_len = self
            .pipe
            .read(&mut buf)
            .map_err(|_| ckb_vm::error::Error::IO {
                kind: std::io::ErrorKind::Other,
                data: "READ error".into(),
            })?;
        machine
            .memory_mut()
            .store_bytes(buffer_addr.to_u64(), &buf[..real_len])?;
        machine
            .memory_mut()
            .store64(&length_addr, &Mac::REG::from_u64(real_len as u64))?;
        #[cfg(feature = "enable-logging")]
        log::info!("Syscall Read: read {} bytes", real_len);
        machine.add_cycles_no_checking(SPAWN_YIELD_CYCLES_BASE)?;
        machine.set_register(A0, Mac::REG::from_u8(0));
        Ok(true)
    }
}

struct WriteSyscall {
    pipe: Pipe,
}

impl WriteSyscall {
    pub fn new(pipe: Pipe) -> Self {
        Self { pipe }
    }
}

impl<Mac: SupportMachine> Syscalls<Mac> for WriteSyscall {
    fn initialize(&mut self, _machine: &mut Mac) -> Result<(), ckb_vm::error::Error> {
        Ok(())
    }

    fn ecall(&mut self, machine: &mut Mac) -> Result<bool, ckb_vm::error::Error> {
        let code = &machine.registers()[A7];
        if code.to_i32() != WRITE {
            return Ok(false);
        }
        let fd = machine.registers()[A0].to_u64();
        if fd != (FIRST_FD_SLOT + 1) {
            return Err(ckb_vm::error::Error::IO {
                kind: std::io::ErrorKind::Other,
                data: "can only write on pipe 3".into(),
            });
        }
        let buffer_addr = machine.registers()[A1].clone();
        let length_addr = machine.registers()[A2].clone();
        let length = machine.memory_mut().load64(&length_addr)?.to_u64();
        // Skip zero-length writes to prevent false EOF signals
        // The ckb-script scheduler allows zero-length data transfers, which could be
        // misinterpreted as EOF by higher-level APIs
        if length == 0 {
            machine.set_register(A0, Mac::REG::from_u8(0));
            return Ok(true);
        }
        let bytes = machine
            .memory_mut()
            .load_bytes(buffer_addr.to_u64(), length)?;
        // the pipe write can't write partial data so we don't need to check result length.
        self.pipe
            .write(&bytes)
            .map_err(|_| ckb_vm::error::Error::IO {
                kind: std::io::ErrorKind::Other,
                data: "WRITE error".into(),
            })?;

        machine
            .memory_mut()
            .store64(&length_addr, &Mac::REG::from_u64(bytes.len() as u64))?;
        #[cfg(feature = "enable-logging")]
        log::info!("Syscall Write: write {} bytes", bytes.len());
        machine.add_cycles_no_checking(SPAWN_YIELD_CYCLES_BASE)?;
        machine.set_register(A0, Mac::REG::from_u8(0));
        Ok(true)
    }
}

struct InheritedFdSyscall {}

impl<Mac: SupportMachine> Syscalls<Mac> for InheritedFdSyscall {
    fn initialize(&mut self, _machine: &mut Mac) -> Result<(), ckb_vm::error::Error> {
        Ok(())
    }

    fn ecall(&mut self, machine: &mut Mac) -> Result<bool, ckb_vm::error::Error> {
        let code = &machine.registers()[A7];
        if code.to_i32() != INHERITED_FD {
            return Ok(false);
        }
        let buffer_addr = machine.registers()[A0].clone();
        let length_addr = machine.registers()[A1].clone();

        let length = machine.memory_mut().load64(&length_addr)?;
        if length.to_u64() < 2 {
            return Err(ckb_vm::error::Error::IO {
                kind: std::io::ErrorKind::Other,
                data: "length of inherited fd is less than 2".into(),
            });
        }
        let mut inherited_fd = [0u8; 16];
        inherited_fd[0..8].copy_from_slice(&FIRST_FD_SLOT.to_le_bytes());
        inherited_fd[8..16].copy_from_slice(&(FIRST_FD_SLOT + 1).to_le_bytes());
        machine
            .memory_mut()
            .store_bytes(buffer_addr.to_u64(), &inherited_fd[..])?;
        machine
            .memory_mut()
            .store64(&length_addr, &Mac::REG::from_u64(2))?;

        machine.set_register(A0, Mac::REG::from_u8(0));
        machine.add_cycles_no_checking(SPAWN_YIELD_CYCLES_BASE)?;
        Ok(true)
    }
}

struct CloseSyscall {}

impl<Mac: SupportMachine> Syscalls<Mac> for CloseSyscall {
    fn initialize(&mut self, _machine: &mut Mac) -> Result<(), ckb_vm::error::Error> {
        Ok(())
    }

    fn ecall(&mut self, machine: &mut Mac) -> Result<bool, ckb_vm::error::Error> {
        let code = &machine.registers()[A7];
        if code.to_i32() != CLOSE {
            return Ok(false);
        }
        machine.set_register(A0, Mac::REG::from_u8(0));
        Ok(true)
    }
}

/// A bidirectional communication channel for transferring data between native code and CKB-VM.
///
/// `Pipe` implements a buffered channel that can be used for either reading or writing, but not both
/// simultaneously. It uses a synchronous channel internally to ensure proper flow control.
///
/// # Structure
/// * `tx` - Optional sender end of the channel
/// * `rx` - Optional receiver end of the channel, wrapped in a mutex for thread safety
/// * `buf` - Internal buffer for storing partially read data
///
/// # Examples
/// ```ignore
/// // Create a pipe pair for bidirectional communication
/// let (pipe1, pipe2) = Pipe::new_pair();
///
/// // Write to pipe2
/// pipe2.write(b"hello")?;
///
/// // Read from pipe1
/// let mut buf = vec![0; 5];
/// let n = pipe1.read(&mut buf)?;
/// assert_eq!(&buf[..n], b"hello");
/// ```
///
/// # Implementation Details
/// - The pipe uses a zero-capacity channel (`sync_channel(0)`), making all write operations
///   synchronous
/// - Reading is buffered: data is read from the channel into an internal buffer and then
///   served from there
/// - Either `tx` or `rx` will be `Some`, but never both, determining whether the pipe is
///   for reading or writing
///
/// # Thread Safety
/// - The receiver is wrapped in a `Mutex` to ensure thread-safe access
/// - The sender is naturally thread-safe through `SyncSender`
///
/// # Resource Management
/// The pipe automatically closes when dropped, ensuring proper cleanup of system resources.
///
pub struct Pipe {
    tx: Option<SyncSender<Vec<u8>>>,
    rx: Option<Mutex<Receiver<Vec<u8>>>>,
    buf: Vec<u8>,
}

impl Pipe {
    pub fn new_pair() -> (Self, Self) {
        let (tx, rx) = sync_channel(0);
        (
            Self {
                tx: None,
                rx: Some(Mutex::new(rx)),
                buf: vec![],
            },
            Self {
                tx: Some(tx),
                rx: None,
                buf: vec![],
            },
        )
    }
    pub fn close(&mut self) {
        if self.tx.is_some() {
            drop(self.tx.take());
        }
        if self.rx.is_some() {
            drop(self.rx.take());
        }
    }
}

impl Read for Pipe {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
        if self.buf.is_empty() {
            match self.rx.as_ref().unwrap().lock().unwrap().recv() {
                Ok(data) => self.buf = data,
                Err(_) => {
                    #[cfg(feature = "enable-logging")]
                    log::info!("Pipe Read: channel is closed");
                    // if channel is closed, return EOF
                    return Ok(0);
                }
            }
        }
        let len = self.buf.len().min(buf.len());
        buf[..len].copy_from_slice(&self.buf[..len]);
        self.buf = self.buf.split_off(len);
        #[cfg(feature = "enable-logging")]
        log::info!("Pipe Read: read {} bytes", len);
        Ok(len)
    }
}

impl Write for Pipe {
    fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
        // short-circuit zero-length writes
        if buf.is_empty() {
            return Ok(0);
        }
        match self.tx.as_mut().unwrap().send(buf.to_vec()) {
            Ok(_) => {
                #[cfg(feature = "enable-logging")]
                log::info!("Pipe Write: write {} bytes", buf.len());
                Ok(buf.len())
            }
            Err(e) => {
                #[cfg(feature = "enable-logging")]
                log::error!("Pipe Write: channel is closed {:?}", e);
                drop(e);
                Err(Error::new(ErrorKind::Other, "channel is closed"))
            }
        }
    }

    fn flush(&mut self) -> Result<(), Error> {
        Ok(())
    }
}

#[cfg(has_asm)]
fn ckb_vm_entry(
    code: Bytes,
    args: Vec<Bytes>,
    read_pipe: Pipe,
    write_pipe: Pipe,
) -> Result<(), Box<dyn std::error::Error>> {
    let asm_core = <Box<ckb_vm::machine::asm::AsmCoreMachine> as SupportMachine>::new(
        ckb_vm::ISA_IMC | ckb_vm::ISA_B | ckb_vm::ISA_MOP,
        ckb_vm::machine::VERSION2,
        u64::MAX,
    );
    let core = ckb_vm::DefaultMachineBuilder::new(asm_core)
        .instruction_cycle_func(Box::new(estimate_cycles))
        .syscall(Box::new(DebugSyscall {}))
        .syscall(Box::new(ReadSyscall::new(read_pipe)))
        .syscall(Box::new(WriteSyscall::new(write_pipe)))
        .syscall(Box::new(InheritedFdSyscall {}))
        .syscall(Box::new(CloseSyscall {}))
        .syscall(Box::new(DebugSyscall {}))
        .build();
    let mut machine = ckb_vm::machine::asm::AsmMachine::new(core);
    let args_iter = args.into_iter().map(Ok);
    machine.load_program(&code, args_iter)?;
    let _exit = machine.run();
    let _cycles = machine.machine.cycles();
    Ok(())
}

#[cfg(not(has_asm))]
fn ckb_vm_entry(
    code: Bytes,
    args: Vec<Bytes>,
    read_pipe: Pipe,
    write_pipe: Pipe,
) -> Result<(), Box<dyn std::error::Error>> {
    let core_machine = ckb_vm::DefaultCoreMachine::<u64, ckb_vm::SparseMemory<u64>>::new(
        ckb_vm::ISA_IMC | ckb_vm::ISA_B | ckb_vm::ISA_MOP,
        ckb_vm::machine::VERSION2,
        u64::MAX,
    );
    let machine_builder = ckb_vm::DefaultMachineBuilder::new(core_machine)
        .instruction_cycle_func(Box::new(estimate_cycles));
    let mut machine = machine_builder
        .instruction_cycle_func(Box::new(estimate_cycles))
        .syscall(Box::new(DebugSyscall {}))
        .syscall(Box::new(ReadSyscall::new(read_pipe)))
        .syscall(Box::new(WriteSyscall::new(write_pipe)))
        .syscall(Box::new(InheritedFdSyscall {}))
        .syscall(Box::new(CloseSyscall {}))
        .build();
    let args_iter = args.into_iter().map(Ok);
    machine.load_program(&code, args_iter)?;
    let _exit = machine.run();
    let _cycles = machine.cycles();
    Ok(())
}
/// Spawns a new CKB-VM instance running the provided script binary in a
/// separate thread with bidirectional communication channels.
///
/// This function creates two pipes for bidirectional communication between the
/// native code and the CKB-VM instance:
/// - One pipe for sending data from native code to CKB-VM
/// - One pipe for receiving data from CKB-VM to native code
///
/// # Arguments
///
/// * `script_binary` - A byte slice containing the RISC-V binary to be executed
///   in CKB-VM
/// * `args` - A slice of string arguments to be passed to the script binary
///
/// # Returns
///
/// Returns a `Result` containing a tuple of two `Pipe`s on success:
/// - The first `Pipe` is for reading data from the CKB-VM instance
/// - The second `Pipe` is for writing data to the CKB-VM instance
///
/// # Errors
///
/// Returns a boxed error if the thread creation or VM initialization fails.
///
/// # Example
///
/// ```rust,ignore
/// let binary = include_bytes!("path/to/binary");
/// let args = &["arg1", "arg2"];
///
/// let (read_pipe, write_pipe) = spawn_server(binary, args)?;
/// // Now you can use read_pipe to receive data from the VM
/// // and write_pipe to send data to the VM
/// ```
/// # Note
/// The VM thread will be terminated when either:
/// - Both `read_pipe` and `write_pipe` are dropped, causing the communication channels to close
/// - The VM execution completes naturally
/// - An unrecoverable error occurs in the VM
pub fn spawn_server(
    script_binary: &[u8],
    args: &[&str],
) -> Result<(Pipe, Pipe), Box<dyn std::error::Error>> {
    // channel: ckb-vm -> native
    let (read_pipe1, write_pipe1) = Pipe::new_pair();
    // channel: native -> ckb-vm
    let (read_pipe2, write_pipe2) = Pipe::new_pair();

    let code = Bytes::copy_from_slice(script_binary);
    let args = args
        .iter()
        .map(|s| Bytes::copy_from_slice(s.as_bytes()))
        .collect();
    std::thread::spawn(move || {
        let _ = ckb_vm_entry(code, args, read_pipe2, write_pipe1);
    });
    Ok((read_pipe1, write_pipe2))
}