libloong 0.7.0

Rust bindings for the libloong LoongArch emulator - a high-performance 64-bit LoongArch virtual machine
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
//! Low-level FFI bindings to the libloong C wrapper
#![allow(dead_code)]
#![allow(non_camel_case_types)]

use std::fmt;

#[repr(C)]
pub struct LibLoongMachine {
    _private: [u8; 0],
}

#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct LibLoongMachineOptions {
    pub memory_max: usize,
    pub stack_size: usize,
    pub brk_size: usize,
    pub verbose_loader: i32,
    pub verbose_syscalls: i32,
    pub use_shared_execute_segments: i32,
}

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LibLoongError {
    LIBLOONG_OK = 0,
    LIBLOONG_ERROR_INVALID_ELF = 1,
    LIBLOONG_ERROR_EXECUTION = 2,
    LIBLOONG_ERROR_TIMEOUT = 3,
    LIBLOONG_ERROR_INVALID_ADDRESS = 4,
    LIBLOONG_ERROR_SYMBOL_NOT_FOUND = 5,
    LIBLOONG_ERROR_OUT_OF_MEMORY = 6,
    LIBLOONG_ERROR_UNKNOWN = 99,
}

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LibLoongExceptionType {
    LIBLOONG_EXCEPTION_NONE = 0,
    LIBLOONG_EXCEPTION_ILLEGAL_OPCODE,
    LIBLOONG_EXCEPTION_ILLEGAL_OPERATION,
    LIBLOONG_EXCEPTION_PROTECTION_FAULT,
    LIBLOONG_EXCEPTION_EXECUTION_SPACE_PROTECTION_FAULT,
    LIBLOONG_EXCEPTION_MISALIGNED_INSTRUCTION,
    LIBLOONG_EXCEPTION_UNIMPLEMENTED_INSTRUCTION,
    LIBLOONG_EXCEPTION_MACHINE_TIMEOUT,
    LIBLOONG_EXCEPTION_OUT_OF_MEMORY,
    LIBLOONG_EXCEPTION_INVALID_PROGRAM,
    LIBLOONG_EXCEPTION_FEATURE_DISABLED,
    LIBLOONG_EXCEPTION_UNIMPLEMENTED_SYSCALL,
    LIBLOONG_EXCEPTION_GUEST_ABORT,
}

#[repr(C)]
#[derive(Debug, Clone)]
pub struct LibLoongErrorInfo {
    pub error_code: LibLoongError,
    pub exception_type: LibLoongExceptionType,
    pub data: u64,
    pub message: [i8; 256],
}

/// Exception details from the guest
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExceptionType {
    IllegalOpcode,
    IllegalOperation,
    ProtectionFault,
    ExecutionSpaceProtectionFault,
    MisalignedInstruction,
    UnimplementedInstruction,
    MachineTimeout,
    OutOfMemory,
    InvalidProgram,
    FeatureDisabled,
    UnimplementedSyscall,
    GuestAbort,
}

impl From<LibLoongExceptionType> for ExceptionType {
    fn from(ex: LibLoongExceptionType) -> Self {
        match ex {
            LibLoongExceptionType::LIBLOONG_EXCEPTION_ILLEGAL_OPCODE => {
                ExceptionType::IllegalOpcode
            }
            LibLoongExceptionType::LIBLOONG_EXCEPTION_ILLEGAL_OPERATION => {
                ExceptionType::IllegalOperation
            }
            LibLoongExceptionType::LIBLOONG_EXCEPTION_PROTECTION_FAULT => {
                ExceptionType::ProtectionFault
            }
            LibLoongExceptionType::LIBLOONG_EXCEPTION_EXECUTION_SPACE_PROTECTION_FAULT => {
                ExceptionType::ExecutionSpaceProtectionFault
            }
            LibLoongExceptionType::LIBLOONG_EXCEPTION_MISALIGNED_INSTRUCTION => {
                ExceptionType::MisalignedInstruction
            }
            LibLoongExceptionType::LIBLOONG_EXCEPTION_UNIMPLEMENTED_INSTRUCTION => {
                ExceptionType::UnimplementedInstruction
            }
            LibLoongExceptionType::LIBLOONG_EXCEPTION_MACHINE_TIMEOUT => {
                ExceptionType::MachineTimeout
            }
            LibLoongExceptionType::LIBLOONG_EXCEPTION_OUT_OF_MEMORY => ExceptionType::OutOfMemory,
            LibLoongExceptionType::LIBLOONG_EXCEPTION_INVALID_PROGRAM => {
                ExceptionType::InvalidProgram
            }
            LibLoongExceptionType::LIBLOONG_EXCEPTION_FEATURE_DISABLED => {
                ExceptionType::FeatureDisabled
            }
            LibLoongExceptionType::LIBLOONG_EXCEPTION_UNIMPLEMENTED_SYSCALL => {
                ExceptionType::UnimplementedSyscall
            }
            LibLoongExceptionType::LIBLOONG_EXCEPTION_GUEST_ABORT => ExceptionType::GuestAbort,
            _ => ExceptionType::IllegalOperation,
        }
    }
}

/// Error type for libloong operations
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    /// Invalid ELF binary
    InvalidElf(String),
    /// Execution error with exception details
    Execution {
        exception_type: Option<ExceptionType>,
        data: u64,
        message: String,
    },
    /// Instruction limit exceeded (timeout)
    Timeout { data: u64, message: String },
    /// Invalid memory address
    InvalidAddress { address: u64, message: String },
    /// Symbol not found
    SymbolNotFound(String),
    /// Out of memory
    OutOfMemory(String),
    /// Unknown error
    Unknown(String),
}

impl From<LibLoongError> for Error {
    fn from(err: LibLoongError) -> Self {
        match err {
            LibLoongError::LIBLOONG_OK => unreachable!("Cannot convert OK to Error"),
            LibLoongError::LIBLOONG_ERROR_INVALID_ELF => {
                Error::InvalidElf("Invalid ELF binary".to_string())
            }
            LibLoongError::LIBLOONG_ERROR_EXECUTION => Error::Execution {
                exception_type: None,
                data: 0,
                message: "Execution error".to_string(),
            },
            LibLoongError::LIBLOONG_ERROR_TIMEOUT => Error::Timeout {
                data: 0,
                message: "Instruction limit exceeded".to_string(),
            },
            LibLoongError::LIBLOONG_ERROR_INVALID_ADDRESS => Error::InvalidAddress {
                address: 0,
                message: "Invalid memory address".to_string(),
            },
            LibLoongError::LIBLOONG_ERROR_SYMBOL_NOT_FOUND => {
                Error::SymbolNotFound("Symbol not found".to_string())
            }
            LibLoongError::LIBLOONG_ERROR_OUT_OF_MEMORY => {
                Error::OutOfMemory("Out of memory".to_string())
            }
            LibLoongError::LIBLOONG_ERROR_UNKNOWN => Error::Unknown("Unknown error".to_string()),
        }
    }
}

impl From<LibLoongErrorInfo> for Error {
    fn from(info: LibLoongErrorInfo) -> Self {
        // Convert C string to Rust String
        let message = unsafe {
            let bytes = std::slice::from_raw_parts(info.message.as_ptr() as *const u8, 256);
            let len = bytes.iter().position(|&b| b == 0).unwrap_or(256);
            String::from_utf8_lossy(&bytes[..len]).to_string()
        };

        match info.error_code {
            LibLoongError::LIBLOONG_OK => unreachable!("Cannot convert OK to Error"),
            LibLoongError::LIBLOONG_ERROR_INVALID_ELF => Error::InvalidElf(message),
            LibLoongError::LIBLOONG_ERROR_EXECUTION => Error::Execution {
                exception_type: if info.exception_type
                    != LibLoongExceptionType::LIBLOONG_EXCEPTION_NONE
                {
                    Some(info.exception_type.into())
                } else {
                    None
                },
                data: info.data,
                message,
            },
            LibLoongError::LIBLOONG_ERROR_TIMEOUT => Error::Timeout {
                data: info.data,
                message,
            },
            LibLoongError::LIBLOONG_ERROR_INVALID_ADDRESS => Error::InvalidAddress {
                address: info.data,
                message,
            },
            LibLoongError::LIBLOONG_ERROR_SYMBOL_NOT_FOUND => Error::SymbolNotFound(message),
            LibLoongError::LIBLOONG_ERROR_OUT_OF_MEMORY => Error::OutOfMemory(message),
            LibLoongError::LIBLOONG_ERROR_UNKNOWN => Error::Unknown(message),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::InvalidElf(msg) => write!(f, "Invalid ELF binary: {}", msg),
            Error::Execution {
                exception_type,
                data,
                message,
            } => {
                write!(f, "Execution error")?;
                if let Some(ex) = exception_type {
                    write!(f, " ({:?})", ex)?;
                }
                write!(f, ": {} (data: 0x{:x})", message, data)
            }
            Error::Timeout { data, message } => write!(
                f,
                "Instruction limit exceeded: {} (at PC: 0x{:x})",
                message, data
            ),
            Error::InvalidAddress { address, message } => {
                write!(f, "Invalid memory address 0x{:x}: {}", address, message)
            }
            Error::SymbolNotFound(msg) => write!(f, "Symbol not found: {}", msg),
            Error::OutOfMemory(msg) => write!(f, "Out of memory: {}", msg),
            Error::Unknown(msg) => write!(f, "Unknown error: {}", msg),
        }
    }
}

impl std::error::Error for Error {}

pub type LibLoongSyscallHandler = extern "C" fn(*mut LibLoongMachine);
pub type LibLoongStdoutCallback = extern "C" fn(*const i8, usize);

#[link(name = "loong_wrapper", kind = "static")]
extern "C" {
    pub fn libloong_machine_create(
        binary_data: *const u8,
        binary_size: usize,
        options: *const LibLoongMachineOptions,
        error_info: *mut LibLoongErrorInfo,
    ) -> *mut LibLoongMachine;

    pub fn libloong_machine_destroy(machine: *mut LibLoongMachine);

    pub fn libloong_machine_setup_linux(
        machine: *mut LibLoongMachine,
        args: *const *const i8,
        argc: usize,
        env: *const *const i8,
        envc: usize,
    ) -> LibLoongError;

    pub fn libloong_machine_setup_minimal_syscalls();
    pub fn libloong_machine_setup_linux_syscalls();

    pub fn libloong_machine_setup_accelerated_syscalls(
        machine: *mut LibLoongMachine,
    ) -> LibLoongError;

    pub fn libloong_machine_setup_accelerated_heap(
        machine: *mut LibLoongMachine,
        arena_base: u64,
        arena_size: usize,
    ) -> LibLoongError;

    pub fn libloong_machine_simulate(
        machine: *mut LibLoongMachine,
        max_instructions: u64,
        counter: u64,
        error_info: *mut LibLoongErrorInfo,
    ) -> LibLoongError;

    pub fn libloong_machine_stop(machine: *mut LibLoongMachine);
    pub fn libloong_machine_stopped(machine: *const LibLoongMachine) -> i32;
    pub fn libloong_machine_instruction_limit_reached(machine: *const LibLoongMachine) -> i32;

    pub fn libloong_machine_instruction_counter(machine: *const LibLoongMachine) -> u64;
    pub fn libloong_machine_set_instruction_counter(machine: *mut LibLoongMachine, val: u64);
    pub fn libloong_machine_increment_counter(machine: *mut LibLoongMachine, val: u64);
    pub fn libloong_machine_max_instructions(machine: *const LibLoongMachine) -> u64;
    pub fn libloong_machine_set_max_instructions(machine: *mut LibLoongMachine, val: u64);

    pub fn libloong_install_syscall_handler(sysnum: u32, handler: LibLoongSyscallHandler);
    pub fn libloong_machine_system_call(machine: *mut LibLoongMachine, sysnum: u32);

    pub fn libloong_machine_sysarg(machine: *const LibLoongMachine, idx: i32) -> u64;
    pub fn libloong_machine_set_result(machine: *mut LibLoongMachine, value: u64);
    pub fn libloong_machine_return_value(machine: *const LibLoongMachine) -> u64;

    pub fn libloong_machine_vmcall(
        machine: *mut LibLoongMachine,
        func_addr: u64,
        max_instructions: u64,
        args: *const u64,
        arg_count: usize,
        return_value: *mut u64,
        error_info: *mut LibLoongErrorInfo,
    ) -> LibLoongError;

    pub fn libloong_machine_vmcall_by_name(
        machine: *mut LibLoongMachine,
        func_name: *const i8,
        max_instructions: u64,
        args: *const u64,
        arg_count: usize,
        return_value: *mut u64,
        error_info: *mut LibLoongErrorInfo,
    ) -> LibLoongError;

    pub fn libloong_machine_vmcall_float(
        machine: *mut LibLoongMachine,
        func_addr: u64,
        max_instructions: u64,
        args: *const u64,
        arg_count: usize,
        return_value: *mut f32,
        error_info: *mut LibLoongErrorInfo,
    ) -> LibLoongError;

    pub fn libloong_machine_vmcall_double(
        machine: *mut LibLoongMachine,
        func_addr: u64,
        max_instructions: u64,
        args: *const u64,
        arg_count: usize,
        return_value: *mut f64,
        error_info: *mut LibLoongErrorInfo,
    ) -> LibLoongError;

    pub fn libloong_machine_address_of(machine: *const LibLoongMachine, name: *const i8) -> u64;
    pub fn libloong_machine_has_symbol(machine: *const LibLoongMachine, name: *const i8) -> i32;

    pub fn libloong_machine_read_memory(
        machine: *const LibLoongMachine,
        addr: u64,
        data: *mut u8,
        size: usize,
    ) -> LibLoongError;

    pub fn libloong_machine_write_memory(
        machine: *mut LibLoongMachine,
        addr: u64,
        data: *const u8,
        size: usize,
    ) -> LibLoongError;

    pub fn libloong_machine_read_string(
        machine: *const LibLoongMachine,
        addr: u64,
        buffer: *mut i8,
        max_len: usize,
        actual_len: *mut usize,
    ) -> LibLoongError;

    pub fn libloong_machine_get_register(machine: *const LibLoongMachine, reg_num: u32) -> u64;
    pub fn libloong_machine_set_register(machine: *mut LibLoongMachine, reg_num: u32, value: u64);
    pub fn libloong_machine_get_pc(machine: *const LibLoongMachine) -> u64;
    pub fn libloong_machine_set_pc(machine: *mut LibLoongMachine, pc: u64);

    pub fn libloong_machine_get_float_register(
        machine: *const LibLoongMachine,
        reg_num: u32,
    ) -> f32;
    pub fn libloong_machine_set_float_register(
        machine: *mut LibLoongMachine,
        reg_num: u32,
        value: f32,
    );
    pub fn libloong_machine_get_double_register(
        machine: *const LibLoongMachine,
        reg_num: u32,
    ) -> f64;
    pub fn libloong_machine_set_double_register(
        machine: *mut LibLoongMachine,
        reg_num: u32,
        value: f64,
    );

    pub fn libloong_machine_copy_to_guest(
        machine: *mut LibLoongMachine,
        dest: u64,
        src: *const u8,
        len: usize,
    ) -> LibLoongError;
    pub fn libloong_machine_copy_from_guest(
        machine: *const LibLoongMachine,
        dest: *mut u8,
        src: u64,
        len: usize,
    ) -> LibLoongError;
    pub fn libloong_machine_mmap_allocate(machine: *mut LibLoongMachine, size: usize) -> u64;

    pub fn libloong_machine_arena_malloc(machine: *mut LibLoongMachine, size: usize) -> u64;
    pub fn libloong_machine_arena_free(machine: *mut LibLoongMachine, ptr: u64) -> i32;
    pub fn libloong_machine_has_arena(machine: *const LibLoongMachine) -> i32;

    pub fn libloong_machine_set_stdout_callback(callback: Option<LibLoongStdoutCallback>);

    pub fn libloong_machine_set_userdata(machine: *mut LibLoongMachine, userdata: *mut ());
    pub fn libloong_machine_get_userdata(machine: *const LibLoongMachine) -> *mut ();

    pub fn libloong_error_string(error: LibLoongError) -> *const i8;
    pub fn libloong_default_options() -> LibLoongMachineOptions;
}