binaryninja/
ffi.rs

1// Copyright 2021-2026 Vector 35 Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::time::{Duration, SystemTime, UNIX_EPOCH};
16
17pub(crate) const INVALID_REGISTER: u32 = 0xffff_ffff;
18
19macro_rules! ffi_wrap {
20    ($n:expr, $b:expr) => {{
21        use std::panic;
22        use std::process;
23
24        panic::catch_unwind(|| $b).unwrap_or_else(|_| {
25            ::tracing::error!("ffi callback caught panic: {}", $n);
26            process::abort()
27        })
28    }};
29}
30
31pub(crate) fn time_from_bn(timestamp: u64) -> SystemTime {
32    let m = Duration::from_secs(timestamp);
33    UNIX_EPOCH + m
34}
35
36pub(crate) unsafe fn slice_from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
37    if len == 0 {
38        // C can and will pass null pointers for data in the case of zero length arrays.
39        // According to the documentation of std::slice::from_raw_parts, data must be
40        // non-null and properly aligned. To avoid creating unsound slices, return an
41        // empty slice directly on any zero-length array, avoiding the unsound call
42        // to std::slice::from_raw_parts.
43        &[]
44    } else {
45        unsafe { std::slice::from_raw_parts(data, len) }
46    }
47}
48
49#[macro_export]
50macro_rules! ffi_span {
51    ($name:expr, $bv:expr) => {{
52        #[allow(unused_imports)]
53        use $crate::file_metadata::FileMetadata;
54        ::tracing::info_span!($name, session_id = $bv.file().session_id().0).entered()
55    }};
56    ($name:expr) => {
57        ::tracing::info_span!($name).entered()
58    };
59}
60
61macro_rules! new_id_type {
62    ($name:ident, $inner_type:ty) => {
63        #[derive(std::fmt::Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
64        pub struct $name(pub $inner_type);
65
66        impl From<$inner_type> for $name {
67            fn from(value: $inner_type) -> Self {
68                Self(value)
69            }
70        }
71
72        impl From<$name> for $inner_type {
73            fn from(value: $name) -> Self {
74                value.0
75            }
76        }
77
78        impl std::fmt::Display for $name {
79            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80                write!(f, "{}", self.0)
81            }
82        }
83    };
84}