Skip to content

Commit 42220b3

Browse files
authored
Add FreeBSD support (#215)
* fix(gum): add freebsd as target_os * fix(gum): add freebsd for gum_*_parse_ucontext * fix(gum): add freebsd for frida_g_get_*_dir functions
1 parent f39dcc5 commit 42220b3

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

frida-gum/src/backtracer.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ use {core::mem::MaybeUninit, frida_gum_sys as gum_sys};
1212

1313
// The following function is not exposed through the `frida-gum.h` header, so we don't have an
1414
// auto-generated binding for it. This may change in a future version.
15-
#[cfg(not(target_os = "windows"))]
1615
extern "C" {
1716
// On some platforms `ucontext` contains a u128 which does not have a defined ABI. In this case,
1817
// we disable the error as we assume the behaviour is correct (all other platforms are unaffected).
18+
#[cfg(target_os = "linux")]
1919
#[allow(improper_ctypes)]
2020
fn gum_linux_parse_ucontext(
2121
context: *const libc::ucontext_t,
2222
cpu_context: *mut gum_sys::GumCpuContext,
2323
);
24+
25+
#[cfg(target_os = "freebsd")]
26+
#[allow(improper_ctypes)]
27+
fn gum_freebsd_parse_ucontext(
28+
context: *const libc::ucontext_t,
29+
cpu_context: *mut gum_sys::GumCpuContext,
30+
);
2431
}
2532

2633
pub struct Backtracer;
@@ -84,24 +91,36 @@ impl Backtracer {
8491

8592
/// Generate an accurate backtrace as a list of return addresses for the supplied signal
8693
/// context.
87-
#[cfg(not(target_os = "windows"))]
94+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
8895
pub fn accurate_with_signal_context(context: &libc::ucontext_t) -> Vec<usize> {
8996
let mut cpu_context = MaybeUninit::<gum_sys::GumCpuContext>::uninit();
9097

9198
unsafe {
99+
#[cfg(target_os = "linux")]
92100
gum_linux_parse_ucontext(context as *const libc::ucontext_t, cpu_context.as_mut_ptr());
101+
#[cfg(target_os = "freebsd")]
102+
gum_freebsd_parse_ucontext(
103+
context as *const libc::ucontext_t,
104+
cpu_context.as_mut_ptr(),
105+
);
93106
Self::accurate_with_context(&cpu_context.assume_init())
94107
}
95108
}
96109

97110
/// Generate a fuzzy backtrace as a list of return addresses for the supplied signal
98111
/// context.
99-
#[cfg(not(target_os = "windows"))]
112+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
100113
pub fn fuzzy_with_signal_context(context: &libc::ucontext_t) -> Vec<usize> {
101114
let mut cpu_context = MaybeUninit::<gum_sys::GumCpuContext>::uninit();
102115

103116
unsafe {
117+
#[cfg(target_os = "linux")]
104118
gum_linux_parse_ucontext(context as *const libc::ucontext_t, cpu_context.as_mut_ptr());
119+
#[cfg(target_os = "freebsd")]
120+
gum_freebsd_parse_ucontext(
121+
context as *const libc::ucontext_t,
122+
cpu_context.as_mut_ptr(),
123+
);
105124
Self::fuzzy_with_context(&cpu_context.assume_init())
106125
}
107126
}

frida-gum/src/process.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ use alloc::{string::String, string::ToString, vec::Vec};
2020
use cstr_core::CString;
2121
use frida_gum_sys::GumThreadFlags_GUM_THREAD_FLAGS_ALL;
2222

23-
#[cfg(target_os = "linux")]
23+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
2424
extern "C" {
2525
pub fn _frida_g_get_home_dir() -> *const c_char;
2626
pub fn _frida_g_get_current_dir() -> *const c_char;
2727
pub fn _frida_g_get_tmp_dir() -> *const c_char;
2828
}
2929

30-
#[cfg(not(target_os = "linux"))]
30+
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
3131
extern "C" {
3232
pub fn g_get_home_dir() -> *const c_char;
3333
pub fn g_get_current_dir() -> *const c_char;
@@ -130,9 +130,9 @@ impl<'a> Process<'a> {
130130
/// Returns a string specifying the filesystem path to the current working directory
131131
pub fn current_dir(&self) -> String {
132132
unsafe {
133-
#[cfg(target_os = "linux")]
133+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
134134
let dir = _frida_g_get_current_dir();
135-
#[cfg(not(target_os = "linux"))]
135+
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
136136
let dir = g_get_current_dir();
137137

138138
CStr::from_ptr(dir).to_string_lossy().to_string()
@@ -142,9 +142,9 @@ impl<'a> Process<'a> {
142142
/// Returns a string specifying the filesystem path to the directory to use for temporary files
143143
pub fn tmp_dir(&self) -> String {
144144
unsafe {
145-
#[cfg(target_os = "linux")]
145+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
146146
let dir = _frida_g_get_tmp_dir();
147-
#[cfg(not(target_os = "linux"))]
147+
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
148148
let dir = g_get_tmp_dir();
149149

150150
CStr::from_ptr(dir).to_string_lossy().to_string()
@@ -154,9 +154,9 @@ impl<'a> Process<'a> {
154154
/// Returns a string specifying the filesystem path to the current user’s home directory
155155
pub fn home_dir(&self) -> String {
156156
unsafe {
157-
#[cfg(target_os = "linux")]
157+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
158158
let dir = _frida_g_get_home_dir();
159-
#[cfg(not(target_os = "linux"))]
159+
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
160160
let dir = g_get_home_dir();
161161

162162
CStr::from_ptr(dir).to_string_lossy().to_string()

frida-gum/src/script/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use {
33
frida_gum_sys::{GMainContext, GMainLoop},
44
};
55

6-
#[cfg(any(target_os = "linux", target_os = "android"))]
6+
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
77
use frida_gum_sys::{
88
_frida_g_main_context_iteration as g_main_context_iteration,
99
_frida_g_main_context_pending as g_main_context_pending,
1010
_frida_g_main_context_push_thread_default as g_main_context_push_thread_default,
1111
_frida_g_main_loop_new as g_main_loop_new,
1212
};
1313

14-
#[cfg(not(any(target_os = "linux", target_os = "android")))]
14+
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "freebsd")))]
1515
use frida_gum_sys::{
1616
g_main_context_iteration, g_main_context_pending, g_main_context_push_thread_default,
1717
g_main_loop_new,

frida-gum/src/script/data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use {
1313
};
1414

1515
/* glib exports are aliased in frida devkit for Linux and Android */
16-
#[cfg(any(target_os = "linux", target_os = "android"))]
16+
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
1717
use frida_gum_sys::_frida_g_bytes_get_data as g_bytes_get_data;
1818

19-
#[cfg(not(any(target_os = "linux", target_os = "android")))]
19+
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "freebsd")))]
2020
use frida_gum_sys::g_bytes_get_data;
2121

2222
pub(crate) struct ScriptData<F>

0 commit comments

Comments
 (0)