#mach #macos #kernel #bindings #darwin #api-bindings #system-interface

sys no-std mach-sys

forked from original mach, and merge from mach2/machx. A Rust interface to the user-space API of the Mach 3.0 kernel that underlies OSX.

7 releases

0.5.4 Mar 1, 2024
0.5.3 Mar 1, 2024
0.5.1 Feb 29, 2024
0.4.4 Feb 26, 2024

#231 in Operating systems

Download history 37/week @ 2024-06-16 27/week @ 2024-06-23 49/week @ 2024-06-30 57/week @ 2024-07-07 21/week @ 2024-07-14 10/week @ 2024-07-21 121/week @ 2024-07-28 15/week @ 2024-08-04 2/week @ 2024-08-11 4/week @ 2024-08-25 24/week @ 2024-09-01 2/week @ 2024-09-08 27/week @ 2024-09-15 636/week @ 2024-09-22 554/week @ 2024-09-29

1,220 downloads per month
Used in 3 crates (2 directly)

GPL-3.0 OR Apache-2.0

190KB
4K SLoC

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:

Target Min. Rust XCode build ctest run
x86_64-apple-darwin 1.33.0 10.3.0 - 13.1.0
aarch64-apple-darwin nightly 13.1.0 - -
aarch64-apple-ios nightly 13.1.0 - -
aarch64-apple-ios-sim nightly 13.1.0 - -
x86_64-apple-ios nightly 13.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.).

Dependencies

~0–5.5MB
~21K SLoC