Crate mach_sys

source ·
Expand description

§mach-sys: forked from original “mach”, and merge from “mach2” & “machx”.

github docs.rs crates.io

A Rust interface to the user-space API of the Mach 3.0 kernel exposed in /usr/include/mach that underlies macOS and is linked via libSystem (and libsystem_kernel).

This library does not expose the kernel-space API of the Mach 3.0 kernel exposed in SDK/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/mach.

That is, if you are writing a kernel-resident device drivers or some other kernel extensions you have to use something else. The user-space kernel API is often API-incompatible with the kernel space one, and even in the cases where they match, they are sometimes ABI incompatible such that using this library would have undefined behavior.

(notes from machx developers:)
1. due to the lack of maintenance of `mach2` and `mach`, a lot of APIs are missing, so I maintain this version for my own use.
2. via bindgen as well as manual fixes.

§Usage

Add the following to your Cargo.toml to conditionally include mach on those platforms that support it.

[target.'cfg(target_vendor="apple")'.dependencies]

# use alias for migration from exists project:
mach = { package = "mach-sys", version = "0.5" }

# or without alias:
mach-sys = "0.5"

Available crate feature:

  • unstable (disabled by default): Exposes newly changed APIs. Enabling this may bring breaking changes (see the breaking change policy).

§Breaking change policy

We do the following steps when an item is changed/removed on latest toolchain:

  1. Flagging an existing one as Deprecated.

  2. Declare a new one (by default).

  3. Forever kept older one.

  4. if two items have equivalents value, then type alias to new one, or internal function calling new one.

  5. otherwise, if two items have different value or behavior, then both will exists together.

  6. or, in cases, if the older one is really sucks, then After a month or more since releasing a new version that contains that remove an older one.

For instance (equivalent), if function FUNC will changes it’s return type from u32 to u64, a new function will be created, then older function just call it and convert it’s return type (u64 as u32). in most of cases, no more changes will be taken, and both of these will be kept forever.

For instance (non-equivalent), if const FOO value is changed from 3 to 4, we expose the newer one, i.e. 4. So the users should notice the change on the first release since deprecating. Or, After a month or more, may or may not, all the users should migration/following this change.

§Examples

Examples can be found in the examples directory of this repository.

Since examples/dump_process_registers.rs makes use of the task_for_pid() function, which requires elevated privileges, it is necessary to disable System Integrity Protection (SIP) and to be part of the admin or _developer group in order to run the example. However, do note that disabling SIP is in no way encouraged, and should only be done for development/debugging purposes.

  1. Reboot macOS in recovery mode.
  2. Click on Options.
  3. Log in to your user.
  4. In the menu click on Utilities and then Terminal.
  5. In the terminal type the following command to disable SIP: csrutil disable (csrutil enable to re-enable SIP).
  6. Reboot your machine.

To run the example, build it as follows:

cargo b --example dump_process_registers

Then run it using sudo:

sudo ./target/debug/examples/dump_process_registers

§Platform support

The following table describes the current CI set-up:

TargetMin. RustXCodebuildctestrun
x86_64-apple-darwin1.33.010.3.0 - 13.1.0
aarch64-apple-darwinnightly13.1.0--
aarch64-apple-iosnightly13.1.0--
aarch64-apple-ios-simnightly13.1.0--
x86_64-apple-iosnightly13.1.0--

§License

This project is licensed under either of

at your option.

§Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in mach-sys by you, as defined in the dual licensing under GPL-3.0 OR Apache-2.0, shall be both licensed as above, without any additional terms or conditions.

To locally test the library, run:

$ ./tomake.sh nostd --target=x86_64-apple-darwin --any-args-accept-by-rustc
$ ./tomake.sh test --target=<your_platform> --any-args-accept-by-cargo
$ echo "for other args please see the bash script named tomake.sh"

where you can replace the --target with the target you want to test (e.g. aarch64-apple-darwin) and use Rustup if you need to use different Rust version, (e.g. stable, 1.33.0, etc.).

Modules§

  • This module corresponds to mach/i386/boolean.h.
  • This module corresponds to bootstrap.h
  • This module roughly corresponds to mach/clock.h.
  • This module roughly corresponds to mach/clock_priv.h.
  • This module roughly corresponds to mach/clock_reply.h.
  • This module roughly corresponds to mach/clock_types.h.
  • This module roughly corresponds to dlfcn.h.
  • This module roughly corresponds to mach-o/dyld.h.
  • This module roughly corresponds to mach-o/dyld_images.h.
  • This module roughly corresponds to mach/dyld_kernel.h.
  • This module roughly corresponds to mach/mach_error.h.
  • This module roughly corresponds to mach/exc.h.
  • This module roughly corresponds to mach/exception_types.h.
  • imports C types from core::ffi
  • This module corresponds to mach/kern_return.h.
  • This module roughly corresponds to libproc.h.
  • This module roughly corresponds to mach-o/loader.h.
  • This module corresponds to mach/mach_init.h.
  • This module corresponds to mach/mach_port.h
  • This module corresponds to mach/mach_time.h
  • This module corresponds to mach/mach_types.h
  • This module roughly corresponds to mach/memory_object_types.h.
  • This module corresponds to mach/message.h.
  • This module roughly corresponds to mach/ndr.h.
  • This module roughly corresponds to mach-o/nlist.h.
  • This module corresponds to mach/port.h
  • This module corresponds to mach/semaphore.h
  • This module corresponds to mach/i386/_structs.h and mach/arm/_structs.h.
  • This module corresponds to mach/sync_policy.h
  • This module corresponds to mach/task.h.
  • This module roughly corresponds to mach/task_info.h.
  • This module corresponds to mach/thread_act.defs.
  • This module corresponds to mach/thread_policy.h.
  • This module corresponds to mach/thread_status.h.
  • This module corresponds to mach/mach_traps.h.
  • This module roughly corresponds to mach/mach_vm.defs.
  • This module corresponds to mach/vm_attributes.h.
  • This module corresponds to mach/vm_behavior.h.
  • This module corresponds to mach/vm_inherit.h.
  • This module roughly corresponds to mach/vm_page_size.h
  • This module corresponds to mach/vm_prot.h.
  • This module corresponds to mach/vm_purgable.h.
  • This module roughly corresponds to mach/vm_region.h.
  • This module roughly corresponds to mach/vm_statistics.h
  • This module corresponds to mach/vm_sync.h.
  • This module roughly corresponds to mach/i386/vm_types.h (on amd64) and mach/arm/vm_types.h (on aarch64).

Constants§