rustpython-host_env 0.6.0

Host OS API abstractions for RustPython
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
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
#![allow(
    clippy::missing_safety_doc,
    reason = "These wrappers expose low-level fault handler hooks with raw OS ABI semantics."
)]
#![allow(
    clippy::result_unit_err,
    reason = "These helpers preserve the existing fault-handler error surface."
)]
#![allow(static_mut_refs)]

#[cfg(unix)]
use alloc::vec::Vec;
#[cfg(unix)]
use parking_lot::Mutex;

#[cfg(unix)]
pub use libc::{SA_NODEFER, c_int};
pub use libc::{SIGFPE, SIGSEGV};
#[cfg(windows)]
use windows_sys::Win32::System::{
    Diagnostics::Debug::{
        AddVectoredExceptionHandler, EXCEPTION_POINTERS, PVECTORED_EXCEPTION_HANDLER,
        RaiseException, RemoveVectoredExceptionHandler, SEM_NOGPFAULTERRORBOX, SetErrorMode,
    },
    Threading::GetCurrentThreadId,
};

#[cfg(windows)]
pub type ExceptionPointers = EXCEPTION_POINTERS;

#[cfg(unix)]
struct FatalSignalHandler {
    signum: libc::c_int,
    enabled: bool,
    name: &'static str,
    previous: libc::sigaction,
}

#[cfg(windows)]
struct FatalSignalHandler {
    signum: libc::c_int,
    enabled: bool,
    name: &'static str,
    previous: libc::sighandler_t,
}

#[cfg(unix)]
impl FatalSignalHandler {
    const fn new(signum: libc::c_int, name: &'static str) -> Self {
        Self {
            signum,
            enabled: false,
            name,
            previous: unsafe { core::mem::zeroed() },
        }
    }
}

#[cfg(windows)]
impl FatalSignalHandler {
    const fn new(signum: libc::c_int, name: &'static str) -> Self {
        Self {
            signum,
            enabled: false,
            name,
            previous: 0,
        }
    }
}

#[cfg(unix)]
const FATAL_SIGNAL_COUNT: usize = 5;
#[cfg(windows)]
const FATAL_SIGNAL_COUNT: usize = 4;

#[cfg(unix)]
static mut FATAL_SIGNAL_HANDLERS: [FatalSignalHandler; FATAL_SIGNAL_COUNT] = [
    FatalSignalHandler::new(libc::SIGBUS, "Bus error"),
    FatalSignalHandler::new(libc::SIGILL, "Illegal instruction"),
    FatalSignalHandler::new(libc::SIGFPE, "Floating-point exception"),
    FatalSignalHandler::new(libc::SIGABRT, "Aborted"),
    FatalSignalHandler::new(libc::SIGSEGV, "Segmentation fault"),
];

#[cfg(windows)]
static mut FATAL_SIGNAL_HANDLERS: [FatalSignalHandler; FATAL_SIGNAL_COUNT] = [
    FatalSignalHandler::new(libc::SIGILL, "Illegal instruction"),
    FatalSignalHandler::new(libc::SIGFPE, "Floating-point exception"),
    FatalSignalHandler::new(libc::SIGABRT, "Aborted"),
    FatalSignalHandler::new(libc::SIGSEGV, "Segmentation fault"),
];

#[cfg(unix)]
const USER_SIGNAL_CAPACITY: usize = 64;

#[cfg(unix)]
#[derive(Clone, Copy)]
pub struct UserSignal {
    pub fd: i32,
    pub all_threads: bool,
    pub chain: bool,
}

#[cfg(unix)]
#[derive(Clone, Copy)]
struct RegisteredUserSignal {
    enabled: bool,
    fd: i32,
    all_threads: bool,
    chain: bool,
    previous: libc::sigaction,
}

#[cfg(unix)]
impl Default for RegisteredUserSignal {
    fn default() -> Self {
        Self {
            enabled: false,
            fd: 2,
            all_threads: true,
            chain: false,
            previous: unsafe { core::mem::zeroed() },
        }
    }
}

#[cfg(unix)]
static USER_SIGNALS: Mutex<Option<Vec<RegisteredUserSignal>>> = Mutex::new(None);

pub fn write_fd(fd: i32, buf: &[u8]) {
    let _ = unsafe { libc::write(fd, buf.as_ptr() as *const libc::c_void, buf.len() as _) };
}

#[cfg(any(unix, windows))]
pub fn is_fatal_signal(signum: libc::c_int) -> bool {
    unsafe {
        FATAL_SIGNAL_HANDLERS
            .iter()
            .any(|handler| handler.signum == signum)
    }
}

#[cfg(any(unix, windows))]
pub fn fatal_signal_name(signum: libc::c_int) -> Option<&'static str> {
    unsafe {
        FATAL_SIGNAL_HANDLERS
            .iter()
            .find(|handler| handler.signum == signum)
            .map(|handler| handler.name)
    }
}

#[cfg(any(unix, windows))]
pub fn abort_process() -> ! {
    unsafe { libc::abort() }
}

#[cfg(any(unix, windows))]
pub fn raise_signal(signum: libc::c_int) {
    unsafe {
        libc::raise(signum);
    }
}

#[cfg(unix)]
#[inline]
pub fn current_thread_id() -> u64 {
    unsafe { libc::pthread_self() as u64 }
}

#[cfg(windows)]
#[inline]
pub fn current_thread_id() -> u64 {
    unsafe { GetCurrentThreadId() as u64 }
}

#[cfg(unix)]
pub fn install_sigaction(
    signum: libc::c_int,
    handler: extern "C" fn(libc::c_int),
    flags: libc::c_int,
    previous: &mut libc::sigaction,
) -> bool {
    let mut action: libc::sigaction = unsafe { core::mem::zeroed() };
    action.sa_sigaction = handler as *const () as libc::sighandler_t;
    action.sa_flags = flags;
    unsafe { libc::sigaction(signum, &action, previous) == 0 }
}

#[cfg(unix)]
unsafe fn disable_fatal_signal_handler(handler: &mut FatalSignalHandler) {
    if !handler.enabled {
        return;
    }
    handler.enabled = false;
    restore_sigaction(handler.signum, &handler.previous);
}

#[cfg(unix)]
pub fn enable_fatal_handlers(handler: extern "C" fn(libc::c_int), flags: libc::c_int) -> bool {
    unsafe {
        let mut installed = Vec::new();
        for entry in &mut FATAL_SIGNAL_HANDLERS {
            if entry.enabled {
                continue;
            }

            if !install_sigaction(entry.signum, handler, flags, &mut entry.previous) {
                for signum in installed {
                    disable_fatal_signal(signum);
                }
                return false;
            }
            entry.enabled = true;
            installed.push(entry.signum);
        }
    }
    true
}

#[cfg(unix)]
pub fn disable_fatal_signal(signum: libc::c_int) {
    unsafe {
        if let Some(handler) = FATAL_SIGNAL_HANDLERS
            .iter_mut()
            .find(|handler| handler.signum == signum)
        {
            disable_fatal_signal_handler(handler);
        }
    }
}

#[cfg(unix)]
pub fn disable_fatal_handlers() {
    unsafe {
        for handler in &mut FATAL_SIGNAL_HANDLERS {
            disable_fatal_signal_handler(handler);
        }
    }
}

#[cfg(unix)]
pub fn restore_sigaction(signum: libc::c_int, previous: &libc::sigaction) {
    unsafe {
        libc::sigaction(signum, previous, core::ptr::null_mut());
    }
}

#[cfg(unix)]
pub fn signal_default_and_raise(signum: libc::c_int) {
    unsafe {
        libc::signal(signum, libc::SIG_DFL);
        libc::raise(signum);
    }
}

#[cfg(unix)]
pub fn exit_immediately(code: libc::c_int) -> ! {
    unsafe { libc::_exit(code) }
}

#[cfg(unix)]
pub fn get_user_signal(signum: usize) -> Option<UserSignal> {
    let guard = USER_SIGNALS.lock();
    guard
        .as_ref()
        .and_then(|signals| signals.get(signum))
        .and_then(|signal| {
            signal.enabled.then_some(UserSignal {
                fd: signal.fd,
                all_threads: signal.all_threads,
                chain: signal.chain,
            })
        })
}

#[cfg(unix)]
pub fn register_user_signal(
    signum: libc::c_int,
    fd: i32,
    all_threads: bool,
    chain: bool,
    handler: extern "C" fn(libc::c_int),
) -> std::io::Result<()> {
    if signum < 0 || signum as usize >= USER_SIGNAL_CAPACITY {
        return Err(std::io::Error::from_raw_os_error(libc::EINVAL));
    }
    let signum = signum as usize;
    let mut guard = USER_SIGNALS.lock();
    if guard.is_none() {
        *guard = Some(vec![RegisteredUserSignal::default(); USER_SIGNAL_CAPACITY]);
    }
    let signals = guard
        .as_mut()
        .expect("user signal table must be initialized");
    let entry = &mut signals[signum];

    if !entry.enabled {
        let mut previous = unsafe { core::mem::zeroed() };
        if !install_sigaction(
            signum as libc::c_int,
            handler,
            if chain {
                libc::SA_NODEFER
            } else {
                libc::SA_RESTART
            },
            &mut previous,
        ) {
            return Err(std::io::Error::last_os_error());
        }
        entry.previous = previous;
    }

    entry.enabled = true;
    entry.fd = fd;
    entry.all_threads = all_threads;
    entry.chain = chain;
    Ok(())
}

#[cfg(unix)]
pub fn unregister_user_signal(signum: libc::c_int) -> bool {
    if signum < 0 {
        return false;
    }
    let signum = signum as usize;
    let mut guard = USER_SIGNALS.lock();
    let Some(signals) = guard.as_mut() else {
        return false;
    };
    let Some(entry) = signals.get_mut(signum) else {
        return false;
    };
    if !entry.enabled {
        return false;
    }

    let previous = entry.previous;
    *entry = RegisteredUserSignal::default();
    restore_sigaction(signum as libc::c_int, &previous);
    true
}

#[cfg(unix)]
pub fn reraise_user_signal(signum: libc::c_int, handler: extern "C" fn(libc::c_int)) -> bool {
    if signum < 0 {
        return false;
    }
    let signum_usize = signum as usize;
    let previous = {
        let guard = USER_SIGNALS.lock();
        let Some(signals) = guard.as_ref() else {
            return false;
        };
        let Some(entry) = signals.get(signum_usize) else {
            return false;
        };
        if !entry.enabled || !entry.chain {
            return false;
        }
        entry.previous
    };

    let saved_errno = crate::os::get_errno();
    restore_sigaction(signum, &previous);
    crate::os::set_errno(saved_errno);
    raise_signal(signum);

    let mut ignored_previous = unsafe { core::mem::zeroed() };
    let _ = install_sigaction(signum, handler, libc::SA_NODEFER, &mut ignored_previous);

    crate::os::set_errno(saved_errno);
    true
}

#[cfg(windows)]
pub fn install_signal_handler(
    signum: libc::c_int,
    handler: extern "C" fn(libc::c_int),
) -> Result<libc::sighandler_t, ()> {
    let previous = unsafe { libc::signal(signum, handler as *const () as libc::sighandler_t) };
    if previous == libc::SIG_ERR as libc::sighandler_t {
        Err(())
    } else {
        Ok(previous)
    }
}

#[cfg(windows)]
unsafe fn disable_fatal_signal_handler(handler: &mut FatalSignalHandler) {
    if !handler.enabled {
        return;
    }
    handler.enabled = false;
    restore_signal_handler(handler.signum, handler.previous);
}

#[cfg(windows)]
pub fn enable_fatal_handlers(handler: extern "C" fn(libc::c_int), _flags: libc::c_int) -> bool {
    unsafe {
        for entry in &mut FATAL_SIGNAL_HANDLERS {
            if entry.enabled {
                continue;
            }

            let Ok(previous) = install_signal_handler(entry.signum, handler) else {
                return false;
            };
            entry.previous = previous;
            entry.enabled = true;
        }
    }
    true
}

#[cfg(windows)]
pub fn disable_fatal_signal(signum: libc::c_int) {
    unsafe {
        if let Some(handler) = FATAL_SIGNAL_HANDLERS
            .iter_mut()
            .find(|handler| handler.signum == signum)
        {
            disable_fatal_signal_handler(handler);
        }
    }
}

#[cfg(windows)]
pub fn disable_fatal_handlers() {
    unsafe {
        for handler in &mut FATAL_SIGNAL_HANDLERS {
            disable_fatal_signal_handler(handler);
        }
    }
}

#[cfg(windows)]
pub fn restore_signal_handler(signum: libc::c_int, previous: libc::sighandler_t) {
    unsafe {
        libc::signal(signum, previous);
    }
}

#[cfg(windows)]
pub fn signal_default_and_raise(signum: libc::c_int) {
    unsafe {
        libc::signal(signum, libc::SIG_DFL);
        libc::raise(signum);
    }
}

#[cfg(windows)]
pub fn add_vectored_exception_handler(handler: PVECTORED_EXCEPTION_HANDLER) -> usize {
    unsafe { AddVectoredExceptionHandler(1, handler) as usize }
}

#[cfg(windows)]
pub fn remove_vectored_exception_handler(handle: usize) {
    if handle != 0 {
        unsafe {
            RemoveVectoredExceptionHandler(handle as *mut core::ffi::c_void);
        }
    }
}

#[cfg(windows)]
pub fn suppress_crash_report() {
    unsafe {
        let mode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
        SetErrorMode(mode | SEM_NOGPFAULTERRORBOX);
    }
}

#[cfg(windows)]
pub fn raise_exception(code: u32, flags: u32) {
    unsafe {
        RaiseException(code, flags, 0, core::ptr::null());
    }
}

#[cfg(windows)]
pub fn ignore_exception(code: u32) -> bool {
    if (code & 0x8000_0000) == 0 {
        return true;
    }
    code == 0xE06D7363 || code == 0xE0434352
}

#[cfg(windows)]
pub fn exception_description(code: u32) -> Option<&'static str> {
    match code {
        EXCEPTION_ACCESS_VIOLATION => Some("access violation"),
        EXCEPTION_FLT_DIVIDE_BY_ZERO => Some("float divide by zero"),
        EXCEPTION_FLT_OVERFLOW => Some("float overflow"),
        EXCEPTION_INT_DIVIDE_BY_ZERO => Some("int divide by zero"),
        EXCEPTION_INT_OVERFLOW => Some("integer overflow"),
        EXCEPTION_IN_PAGE_ERROR => Some("page error"),
        EXCEPTION_STACK_OVERFLOW => Some("stack overflow"),
        EXCEPTION_ILLEGAL_INSTRUCTION => Some("illegal instruction"),
        _ => None,
    }
}

/// `EXCEPTION_CONTINUE_SEARCH`: this handler only reports; later
/// handlers decide the process fate.
#[cfg(windows)]
pub const EXCEPTION_CONTINUE_SEARCH: i32 =
    windows_sys::Win32::System::Diagnostics::Debug::EXCEPTION_CONTINUE_SEARCH;

#[cfg(windows)]
pub const EXCEPTION_ACCESS_VIOLATION: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_ACCESS_VIOLATION as u32;
#[cfg(windows)]
pub const EXCEPTION_ARRAY_BOUNDS_EXCEEDED: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_ARRAY_BOUNDS_EXCEEDED as u32;
#[cfg(windows)]
pub const EXCEPTION_BREAKPOINT: u32 = windows_sys::Win32::Foundation::EXCEPTION_BREAKPOINT as u32;
#[cfg(windows)]
pub const EXCEPTION_DATATYPE_MISALIGNMENT: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_DATATYPE_MISALIGNMENT as u32;
#[cfg(windows)]
pub const EXCEPTION_FLT_DENORMAL_OPERAND: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_FLT_DENORMAL_OPERAND as u32;
#[cfg(windows)]
pub const EXCEPTION_FLT_DIVIDE_BY_ZERO: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_FLT_DIVIDE_BY_ZERO as u32;
#[cfg(windows)]
pub const EXCEPTION_FLT_INEXACT_RESULT: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_FLT_INEXACT_RESULT as u32;
#[cfg(windows)]
pub const EXCEPTION_FLT_INVALID_OPERATION: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_FLT_INVALID_OPERATION as u32;
#[cfg(windows)]
pub const EXCEPTION_FLT_OVERFLOW: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_FLT_OVERFLOW as u32;
#[cfg(windows)]
pub const EXCEPTION_FLT_STACK_CHECK: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_FLT_STACK_CHECK as u32;
#[cfg(windows)]
pub const EXCEPTION_FLT_UNDERFLOW: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_FLT_UNDERFLOW as u32;
#[cfg(windows)]
pub const EXCEPTION_INT_DIVIDE_BY_ZERO: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_INT_DIVIDE_BY_ZERO as u32;
#[cfg(windows)]
pub const EXCEPTION_INT_OVERFLOW: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_INT_OVERFLOW as u32;
#[cfg(windows)]
pub const EXCEPTION_IN_PAGE_ERROR: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_IN_PAGE_ERROR as u32;
#[cfg(windows)]
pub const EXCEPTION_PRIV_INSTRUCTION: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_PRIV_INSTRUCTION as u32;
#[cfg(windows)]
pub const EXCEPTION_SINGLE_STEP: u32 = windows_sys::Win32::Foundation::EXCEPTION_SINGLE_STEP as u32;
#[cfg(windows)]
pub const EXCEPTION_STACK_OVERFLOW: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_STACK_OVERFLOW as u32;
#[cfg(windows)]
pub const EXCEPTION_ILLEGAL_INSTRUCTION: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_ILLEGAL_INSTRUCTION as u32;
#[cfg(windows)]
pub const EXCEPTION_NONCONTINUABLE: u32 =
    windows_sys::Win32::System::SystemServices::EXCEPTION_NONCONTINUABLE;
#[cfg(windows)]
pub const EXCEPTION_NONCONTINUABLE_EXCEPTION: u32 =
    windows_sys::Win32::Foundation::EXCEPTION_NONCONTINUABLE_EXCEPTION as u32;

#[cfg(windows)]
pub unsafe fn exception_code(exc_info: *mut EXCEPTION_POINTERS) -> u32 {
    let record = unsafe { &*(*exc_info).ExceptionRecord };
    record.ExceptionCode as u32
}

#[cfg(windows)]
#[inline]
pub fn is_access_violation(code: u32) -> bool {
    code == EXCEPTION_ACCESS_VIOLATION
}