Skip to content

target-independent bindings #3

@Irate-Walrus

Description

@Irate-Walrus

Currently bindings are restricted to windows and x86_64/x86. This is reflected in the src/ffi/generated.rs:

compile_error!("These bindings can only be used on `x86_64` architectures. To generate bindings for your target architecture, consider using the `regenerate` feature.");

There are two reasons I found for this:

  • The cty crate depends on the target to create ctype definitions
  • extern blocks rely on linked symbols and ABIs such as stdcall may not be possible on other targets.

My use case is such that I wish to use structures such as PPEB on non-native targets such as i686-unknown-linux-gnu.

This being the case I forked this repository, which can be found here: https://github.com/Irate-Walrus/phnt-rs

In this fork I introduced a pre-generated x86_64_bindgen.rs and x86_bindgen.rs as well as two feature flags externs and fn_types.

externs allows externally linked symbols to be enabled/disabled:

#[cfg(feature="externs")]
extern "C" {
   pub static GUID_NULL: GUID;
   pub static mut NlsAnsiCodePage: USHORT;
   ...

While fn_types defines the externally linked functions as types:

#[cfg(feature="fn_types")]
mod fn_types {
    pub type NtCallbackReturnFn = unsafe extern "C" fn(OutputBuffer: PVOID, OutputLength: ULONG, Status: NTSTATUS) -> NTSTATUS;
    pub type NtFlushProcessWriteBuffersFn = unsafe extern "C" fn() -> NTSTATUS;
    pub type NtQueryDebugFilterStateFn = unsafe extern "C" fn(ComponentId: ULONG, Level: ULONG) -> NTSTATUS;
    ...

This is helpful if you are trying to call these functions directly in memory:

let rtl_allocate_heap: RtlAllocateHeapFn = core::mem::transmute(rtl_allocate_heap_addr);
let ptr = rtl_allocate_heap(self.handle(), HEAP_ZERO_MEMORY, layout.size() as _)

I've also added to the GitHub workflows to auto-regenerate both x86 and x86_64 bindings and commit them to the repo.

There are a few issues that I've seen so far:

  • Bad regexes in my build.rs lead to failure with the latest bindgen version 0.71.1
  • Dropping the use of cty for internally defined ctypes may break compatibility with pre-existing users.
  • Custom rustfmt to allow for the use of regex when feature-gating externs and function types.

This issue is to gauge your interest in these changes, although it's likely I may continue in the fork to add further definitions from other header files. Thanks for you hard work, it made these changes relatively straightforward.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions