Skip to content

Commit

Permalink
Merge pull request #20 from hjmallon/_hm/update
Browse files Browse the repository at this point in the history
Update bindgen, use libc for symbols available in it and filter the symbols in the generated bindings
  • Loading branch information
mach-kernel authored Nov 17, 2023
2 parents 0f6348e + 6d70cad commit 33b8bed
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 101 deletions.
98 changes: 21 additions & 77 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion xpc-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ bitflags = "2.4.0"
libc = "0.2.147"

[build-dependencies]
bindgen = "0.60.1"
bindgen = "0.69.1"
xcrun = "1.0.4"
13 changes: 8 additions & 5 deletions xpc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ fn main() {

let xpc_path = format!("{}{}/xpc/xpc.h", sdk_path, MACOS_INCLUDE_PATH);
let bootstrap_path = format!("{}{}/bootstrap.h", sdk_path, MACOS_INCLUDE_PATH);
let sys_types = format!("{}{}/sys/types.h", sdk_path, MACOS_INCLUDE_PATH);
let sysctl = format!("{}{}/sys/sysctl.h", sdk_path, MACOS_INCLUDE_PATH);

// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
Expand All @@ -27,11 +25,16 @@ fn main() {
// bindings for.
.header(xpc_path)
.header(bootstrap_path)
.header(sys_types)
.header(sysctl)
// Filter the results to only relevant symbols
.allowlist_function("^xpc_.*")
.allowlist_var("^_xpc_.*")
.allowlist_var("^bootstrap_port")
// The following symbols should probably be in libc or mach2, but are not
.allowlist_function("^mach_port.*")
.allowlist_function("^vm_allocate")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
Expand Down
10 changes: 6 additions & 4 deletions xpc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern crate lazy_static;
#[macro_use]
extern crate bitflags;

pub use libc::MAP_SHARED;
use libc::{geteuid, mach_task_self_, strerror, sysctlbyname, KERN_SUCCESS, MACH_PORT_NULL};
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int, c_long, c_void};
use std::ptr::null_mut;
Expand Down Expand Up @@ -68,8 +70,8 @@ pub struct _os_alloc_once_s {

#[repr(C)]
pub struct xpc_global_data {
pub a: u_int64_t,
pub xpc_flags: u_int64_t,
pub a: u64,
pub xpc_flags: u64,
pub task_bootstrap_port: mach_port_t,
pub xpc_bootstrap_pipe: xpc_pipe_t,
}
Expand All @@ -88,7 +90,7 @@ pub fn rs_strerror(err: i32) -> String {

/// Attempt to yield existing bootstrap_port if not MACH_PORT_NULL
pub unsafe fn get_bootstrap_port() -> mach_port_t {
if bootstrap_port == MACH_PORT_NULL {
if bootstrap_port == MACH_PORT_NULL as mach_port_t {
log::debug!("Bootstrap port is null! Querying for port");
lookup_bootstrap_port()
} else {
Expand Down Expand Up @@ -155,7 +157,7 @@ pub unsafe fn read_xpc_global_data() -> Option<&'static xpc_global_data> {
pub unsafe fn rs_sysctlbyname(name: &str) -> Result<String, String> {
let name = CString::new(name).unwrap();
let mut ret_buf: [c_char; 256] = [0; 256];
let mut size = ret_buf.len() as u64;
let mut size = ret_buf.len();

let err = sysctlbyname(
name.as_ptr(),
Expand Down
7 changes: 4 additions & 3 deletions xpc-sys/src/objects/xpc_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use libc::c_int;

use crate::objects::xpc_type::XPCType;
use crate::{
mach_port_t, xpc_array_append_value, xpc_array_create, xpc_bool_create, xpc_copy,
xpc_copy_description, xpc_double_create, xpc_fd_create, xpc_int64_create, xpc_mach_recv_create,
xpc_mach_send_create, xpc_object_t, xpc_release, xpc_string_create, xpc_uint64_create,
xpc_array_append_value, xpc_array_create, xpc_bool_create, xpc_copy, xpc_copy_description,
xpc_double_create, xpc_fd_create, xpc_int64_create, xpc_mach_recv_create, xpc_mach_send_create,
xpc_object_t, xpc_release, xpc_string_create, xpc_uint64_create,
};
use libc::mach_port_t;
use std::ffi::{CStr, CString};
use std::os::unix::prelude::RawFd;
use std::ptr::null_mut;
Expand Down
8 changes: 3 additions & 5 deletions xpc-sys/src/objects/xpc_shmem.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::objects::xpc_error::XPCError;
use crate::objects::xpc_object::XPCObject;
use crate::{
mach_port_t, mach_task_self_, rs_strerror, vm_address_t, vm_allocate, vm_deallocate, vm_size_t,
xpc_shmem_create,
};
use crate::{rs_strerror, vm_allocate, xpc_shmem_create};
use libc::{mach_port_t, mach_task_self_, vm_address_t, vm_deallocate, vm_size_t};
use std::ffi::c_void;
use std::os::raw::c_int;
use std::ptr::null_mut;
Expand Down Expand Up @@ -39,7 +37,7 @@ impl XPCShmem {
Err(XPCError::IOError(rs_strerror(err)))
} else {
let xpc_object: XPCObject =
unsafe { xpc_shmem_create(region as *mut c_void, size as u64).into() };
unsafe { xpc_shmem_create(region as *mut c_void, size).into() };

log::info!(
"XPCShmem new (region: {:p}, xpc_object_t {:p})",
Expand Down
3 changes: 2 additions & 1 deletion xpc-sys/src/traits/query_builder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::enums::{DomainType, SessionType};
use crate::get_bootstrap_port;
use crate::objects::xpc_dictionary::XPCDictionary;
use crate::objects::xpc_object::MachPortType;
use crate::objects::xpc_object::XPCObject;
use crate::{get_bootstrap_port, mach_port_t};
use libc::mach_port_t;

/// Builder methods for XPCDictionary to make querying easier
pub trait QueryBuilder {
Expand Down
11 changes: 6 additions & 5 deletions xpc-sys/src/traits/xpc_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use std::rc::Rc;
use crate::objects::xpc_object::{MachPortType, XPCObject};
use crate::objects::xpc_type;
use crate::{
mach_port_t, size_t, xpc_array_apply, xpc_bool_get_value, xpc_double_get_value,
xpc_int64_get_value, xpc_mach_send_get_right, xpc_object_t, xpc_string_get_string_ptr,
xpc_type_get_name, xpc_uint64_get_value,
xpc_array_apply, xpc_bool_get_value, xpc_double_get_value, xpc_int64_get_value,
xpc_mach_send_get_right, xpc_object_t, xpc_string_get_string_ptr, xpc_type_get_name,
xpc_uint64_get_value,
};
use libc::mach_port_t;

use crate::objects::xpc_error::XPCError;
use crate::objects::xpc_error::XPCError::ValueError;
Expand Down Expand Up @@ -95,7 +96,7 @@ impl TryXPCValue<Vec<Arc<XPCObject>>> for XPCObject {
let vec: Rc<RefCell<Vec<Arc<XPCObject>>>> = Rc::new(RefCell::new(vec![]));
let vec_rc_clone = vec.clone();

let block = ConcreteBlock::new(move |_: size_t, obj: xpc_object_t| {
let block = ConcreteBlock::new(move |_: usize, obj: xpc_object_t| {
let xpc_object: XPCObject = XPCObject::xpc_copy(obj);
vec_rc_clone.borrow_mut().push(xpc_object.into());
true
Expand All @@ -121,12 +122,12 @@ impl TryXPCValue<Vec<Arc<XPCObject>>> for XPCObject {
#[cfg(test)]
mod tests {
use crate::get_bootstrap_port;
use crate::mach_port_t;
use crate::objects::xpc_error::XPCError;
use crate::objects::xpc_error::XPCError::ValueError;
use crate::objects::xpc_object::MachPortType;
use crate::objects::xpc_object::XPCObject;
use crate::traits::xpc_value::TryXPCValue;
use libc::mach_port_t;
use std::sync::Arc;

#[test]
Expand Down

0 comments on commit 33b8bed

Please sign in to comment.