Skip to main content

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    ($(#[$meta:meta])* $name:ident, $inner_type:ty $(, $ffi_type:ty, $ffi_field:ident)?) => {
63        $(#[$meta])*
64        #[derive(std::fmt::Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
65        pub struct $name(pub $inner_type);
66
67        impl From<$inner_type> for $name {
68            fn from(value: $inner_type) -> Self {
69                Self(value)
70            }
71        }
72
73        impl From<$name> for $inner_type {
74            fn from(value: $name) -> Self {
75                value.0
76            }
77        }
78
79        impl std::fmt::Display for $name {
80            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81                write!(f, "{}", self.0)
82            }
83        }
84
85        $(
86            impl From<$ffi_type> for $name {
87                fn from(value: $ffi_type) -> Self {
88                    Self(value.$ffi_field)
89                }
90            }
91
92            impl From<$name> for $ffi_type {
93                fn from(value: $name) -> Self {
94                    Self { $ffi_field: value.0 }
95                }
96            }
97        )?
98    };
99}