Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate the need for call_unsafe_wdf_function_binding via helper lib #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

8 changes: 3 additions & 5 deletions crates/sample-kmdf-driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use static_assertions::const_assert;
use wdk::println;
#[cfg(not(test))]
use wdk_alloc::WDKAllocator;
use wdk_macros::call_unsafe_wdf_function_binding;
use wdk_sys::{
ntddk::DbgPrint,
wdf::*,
DRIVER_OBJECT,
NTSTATUS,
PCUNICODE_STRING,
Expand Down Expand Up @@ -82,8 +82,7 @@ pub unsafe extern "system" fn driver_entry(
let driver_handle_output = WDF_NO_HANDLE.cast::<*mut wdk_sys::WDFDRIVER__>();

let wdf_driver_create_ntstatus = unsafe {
call_unsafe_wdf_function_binding!(
WdfDriverCreate,
WdfDriverCreate(
driver as wdk_sys::PDRIVER_OBJECT,
registry_path,
driver_attributes,
Expand Down Expand Up @@ -118,8 +117,7 @@ extern "C" fn evt_driver_device_add(
let mut device_handle_output: WDFDEVICE = WDF_NO_HANDLE.cast();

let ntstatus = unsafe {
wdk_macros::call_unsafe_wdf_function_binding!(
WdfDeviceCreate,
WdfDeviceCreate(
&mut device_init,
WDF_NO_OBJECT_ATTRIBUTES,
&mut device_handle_output,
Expand Down
1 change: 1 addition & 0 deletions crates/wdk-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bindgen.workspace = true
wdk-build.workspace = true
thiserror = "1.0.48"
tracing-subscriber = "0.3.17"
cc = "1.0.83"

[dependencies]
wdk-macros.workspace = true
Expand Down
82 changes: 81 additions & 1 deletion crates/wdk-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
};

use bindgen::CodegenConfig;
use wdk_build::{BuilderExt, Config, ConfigError, DriverConfig, KMDFConfig};
use wdk_build::{BuilderExt, CPUArchitecture, Config, ConfigError, DriverConfig, KMDFConfig};

// FIXME: feature gate the WDF version
// FIXME: check that the features are exclusive
Expand Down Expand Up @@ -55,6 +55,8 @@ fn generate_wdf(out_path: &Path, config: Config) -> Result<(), ConfigError> {
// WDKs may introduce non-inlined functions.
Ok(
bindgen::Builder::wdk_default(vec!["src/wdf-input.h"], config)?
.clang_arg("-fkeep-inline-functions")
.generate_inline_functions(true)
.with_codegen_config((CodegenConfig::TYPES | CodegenConfig::VARS).complement())
.allowlist_file("(?i).*wdf.*") // Only generate for files that are prefixed with (case-insensitive) wdf (ie.
// /some/path/WdfSomeHeader.h), to prevent duplication of code in ntddk.rs
Expand Down Expand Up @@ -94,6 +96,84 @@ fn main() -> Result<(), ConfigError> {
generate_wdf(&out_path, config.clone())?;
}

// FIXME: This is mostly duplicated from wdk-build/src/bindgen.rs.
let args = config
.get_include_paths()?
.iter()
.map(|include_path| {
format!(
"--include-directory={}",
include_path
.to_str()
.expect("Non Unicode paths are not supported")
)
})
.chain([format!(
"--define-macro={}",
match config.cpu_architecture {
CPUArchitecture::AMD64 => "_AMD64_",
CPUArchitecture::ARM64 => "_ARM64EC_",
}
)])
.chain(
match config.driver_config {
// FIXME: Add support for KMDF_MINIMUM_VERSION_REQUIRED and
// UMDF_MINIMUM_VERSION_REQUIRED
DriverConfig::WDM() => {
vec![]
}
DriverConfig::KMDF(kmdf_config) => {
vec![
format!("KMDF_VERSION_MAJOR={}", kmdf_config.kmdf_version_major),
format!("KMDF_VERSION_MINOR={}", kmdf_config.kmdf_version_minor),
]
}
DriverConfig::UMDF(umdf_config) => {
let mut umdf_definitions = vec![
format!("UMDF_VERSION_MAJOR={}", umdf_config.umdf_version_major),
format!("UMDF_VERSION_MINOR={}", umdf_config.umdf_version_minor),
];

if umdf_config.umdf_version_major >= 2 {
umdf_definitions.push("UMDF_USING_NTSTATUS".to_string());
umdf_definitions.push("_UNICODE".to_string());
umdf_definitions.push("UNICODE".to_string());
}

umdf_definitions
}
}
.iter()
.map(|preprocessor_definition| format!("--define-macro={preprocessor_definition}")),
)
// Windows SDK & DDK have non-portable paths (ex. #include "DriverSpecs.h" but the file
// is actually driverspecs.h)
.chain(["--warn-=no-nonportable-include-path".to_string()])
// Windows SDK & DDK use pshpack and poppack headers to change packing
.chain(["--warn-=no-pragma-pack".to_string()])
.chain(["--warn-=no-ignored-attributes".to_string()])
.chain(["--warn-=no-ignored-pragma-intrinsic".to_string()])
.chain(["--warn-=no-visibility".to_string()])
.chain(["--warn-=no-microsoft-anon-tag".to_string()])
.chain(["--warn-=no-microsoft-enum-forward-reference".to_string()])
// Don't warn for deprecated declarations. deprecated items are already blocklisted
// below and if there are any non-blocklisted function definitions, it will throw a
// -WDeprecated warning
.chain(["--warn-=no-deprecated-declarations".to_string()])
.chain(["-fms-extensions".to_string()])
.collect::<Vec<_>>();

// Generate the inline `Wdf*` functions for linking.
let mut cc = cc::Build::new();
for flag in args {
cc.flag(&flag);
}

cc.compiler("clang")
.warnings(false)
.file("src/wdf.c")
.compile("wdf");

config.configure_library_build()?;
Ok(config.export_config()?)
}
48 changes: 42 additions & 6 deletions crates/wdk-sys/generated_bindings/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,7 @@ pub const PF_AVX512F_INSTRUCTIONS_AVAILABLE: u32 = 41;
pub const PF_ERMS_AVAILABLE: u32 = 42;
pub const PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE: u32 = 43;
pub const PF_ARM_V83_JSCVT_INSTRUCTIONS_AVAILABLE: u32 = 44;
pub const PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE: u32 = 45;
pub const IsNEC_98: u32 = 0;
pub const IsNotNEC_98: u32 = 1;
pub const PROCESSOR_FEATURE_MAX: u32 = 64;
Expand Down Expand Up @@ -2457,8 +2458,9 @@ pub const DMA_IOMMU_INTERFACE_VERSION: u32 = 1;
pub const DMA_IOMMU_INTERFACE_EX_VERSION_1: u32 = 1;
pub const DMA_IOMMU_INTERFACE_EX_VERSION_2: u32 = 2;
pub const DMA_IOMMU_INTERFACE_EX_VERSION_MIN: u32 = 1;
pub const DMA_IOMMU_INTERFACE_EX_VERSION_MAX: u32 = 2;
pub const DMA_IOMMU_INTERFACE_EX_VERSION: u32 = 1;
pub const DMA_IOMMU_INTERFACE_EX_VERSION_3: u32 = 3;
pub const DMA_IOMMU_INTERFACE_EX_VERSION_MAX: u32 = 3;
pub const DMA_IOMMU_INTERFACE_EX_VERSION: u32 = 3;
pub const PO_MEM_PRESERVE: u32 = 1;
pub const PO_MEM_CLONE: u32 = 2;
pub const PO_MEM_CL_OR_NCHK: u32 = 4;
Expand Down Expand Up @@ -3685,6 +3687,7 @@ pub const TOKEN_READ: u32 = 131080;
pub const TOKEN_WRITE: u32 = 131296;
pub const TOKEN_EXECUTE: u32 = 131072;
pub const TOKEN_TRUST_CONSTRAINT_MASK: u32 = 131096;
pub const TOKEN_TRUST_ALLOWED_MASK: u32 = 131102;
pub const TOKEN_ACCESS_PSEUDO_HANDLE_WIN8: u32 = 24;
pub const TOKEN_ACCESS_PSEUDO_HANDLE: u32 = 24;
pub const TOKEN_MANDATORY_POLICY_OFF: u32 = 0;
Expand Down Expand Up @@ -4229,6 +4232,8 @@ pub const PERSISTENT_VOLUME_STATE_REALLOCATE_ALL_DATA_WRITES: u32 = 512;
pub const PERSISTENT_VOLUME_STATE_CHKDSK_RAN_ONCE: u32 = 1024;
pub const PERSISTENT_VOLUME_STATE_MODIFIED_BY_CHKDSK: u32 = 2048;
pub const PERSISTENT_VOLUME_STATE_DAX_FORMATTED: u32 = 4096;
pub const PERSISTENT_VOLUME_STATE_DEV_VOLUME: u32 = 8192;
pub const PERSISTENT_VOLUME_STATE_TRUSTED_VOLUME: u32 = 16384;
pub const OPLOCK_LEVEL_CACHE_READ: u32 = 1;
pub const OPLOCK_LEVEL_CACHE_HANDLE: u32 = 2;
pub const OPLOCK_LEVEL_CACHE_WRITE: u32 = 4;
Expand All @@ -4238,6 +4243,7 @@ pub const REQUEST_OPLOCK_INPUT_FLAG_COMPLETE_ACK_ON_CLOSE: u32 = 4;
pub const REQUEST_OPLOCK_CURRENT_VERSION: u32 = 1;
pub const REQUEST_OPLOCK_OUTPUT_FLAG_ACK_REQUIRED: u32 = 1;
pub const REQUEST_OPLOCK_OUTPUT_FLAG_MODES_PROVIDED: u32 = 2;
pub const REQUEST_OPLOCK_OUTPUT_FLAG_WRITABLE_SECTION_PRESENT: u32 = 4;
pub const QUERY_DEPENDENT_VOLUME_REQUEST_FLAG_HOST_VOLUMES: u32 = 1;
pub const QUERY_DEPENDENT_VOLUME_REQUEST_FLAG_GUEST_VOLUMES: u32 = 2;
pub const SD_GLOBAL_CHANGE_TYPE_MACHINE_SID: u32 = 1;
Expand Down Expand Up @@ -4285,12 +4291,12 @@ pub const STREAM_LAYOUT_ENTRY_NO_CLUSTERS_ALLOCATED: u32 = 8;
pub const STREAM_LAYOUT_ENTRY_HAS_INFORMATION: u32 = 16;
pub const STREAM_EXTENT_ENTRY_AS_RETRIEVAL_POINTERS: u32 = 1;
pub const STREAM_EXTENT_ENTRY_ALL_EXTENTS: u32 = 2;
pub const CHECKSUM_TYPE_UNCHANGED: i32 = -1;
pub const CHECKSUM_TYPE_NONE: u32 = 0;
pub const CHECKSUM_TYPE_CRC32: u32 = 1;
pub const CHECKSUM_TYPE_CRC64: u32 = 2;
pub const CHECKSUM_TYPE_ECC: u32 = 3;
pub const CHECKSUM_TYPE_FIRST_UNUSED_TYPE: u32 = 4;
pub const CHECKSUM_TYPE_SHA256: u32 = 4;
pub const CHECKSUM_TYPE_FIRST_UNUSED_TYPE: u32 = 5;
pub const FSCTL_INTEGRITY_FLAG_CHECKSUM_ENFORCEMENT_OFF: u32 = 1;
pub const OFFLOAD_READ_FLAG_ALL_ZERO_BEYOND_CURRENT_RANGE: u32 = 1;
pub const SET_PURGE_FAILURE_MODE_ENABLED: u32 = 1;
Expand Down Expand Up @@ -4366,6 +4372,7 @@ pub const IO_REPARSE_TAG_RESERVED_ONE: u32 = 1;
pub const IO_REPARSE_TAG_RESERVED_TWO: u32 = 2;
pub const IO_REPARSE_TAG_RESERVED_RANGE: u32 = 2;
pub const IO_REPARSE_TAG_VALID_VALUES: u32 = 4026597375;
pub const IO_REPARSE_TAG_RESERVED_INVALID: u32 = 3221258240;
pub const IO_REPARSE_TAG_MOUNT_POINT: u32 = 2684354563;
pub const IO_REPARSE_TAG_HSM: u32 = 3221225476;
pub const IO_REPARSE_TAG_DRIVE_EXTENDER: u32 = 2147483653;
Expand Down Expand Up @@ -4540,13 +4547,22 @@ pub const FILE_PIPE_SYMLINK_FLAG_RELATIVE: u32 = 2;
pub const FILE_PIPE_SYMLINK_VALID_FLAGS: u32 = 3;
pub const QUERY_DIRECT_ACCESS_IMAGE_EXTENTS: u32 = 1;
pub const QUERY_DIRECT_ACCESS_DATA_EXTENTS: u32 = 2;
pub const REFS_VOLUME_DEDUP_INFO_INPUT_BUFFER_VERSION: u32 = 1;
pub const REFS_VOLUME_DEDUP_INFO_OUTPUT_BUFFER_VERSION: u32 = 1;
pub const REFS_QUERY_VOLUME_TOTAL_SHARED_LCNS_OUTPUT_BUFFER_VERSION: u32 = 1;
pub const REFS_SET_VOLUME_COMPRESSION_INFO_INPUT_BUFFER_VERSION: u32 = 1;
pub const REFS_QUERY_VOLUME_COMPRESSION_INFO_OUTPUT_BUFFER_VERSION: u32 = 1;
pub const REFS_SET_VOLUME_IO_METRICS_INFO_INPUT_BUFFER_VERSION: u32 = 1;
pub const REFS_QUERY_VOLUME_IO_METRICS_INFO_INPUT_BUFFER_VERSION: u32 = 1;
pub const REFS_QUERY_VOLUME_IO_METRICS_INFO_OUTPUT_BUFFER_VERSION: u32 = 1;
pub const IO_QOS_MAX_RESERVATION: u32 = 1000000000;
pub const SMB_CCF_APP_INSTANCE_EA_NAME: &[u8; 29] = b"ClusteredApplicationInstance\0";
pub const NETWORK_APP_INSTANCE_CSV_FLAGS_VALID_ONLY_IF_CSV_COORDINATOR: u32 = 1;
pub const LX_FILE_METADATA_UID_EA_NAME: &[u8; 7] = b"$LXUID\0";
pub const LX_FILE_METADATA_GID_EA_NAME: &[u8; 7] = b"$LXGID\0";
pub const LX_FILE_METADATA_MODE_EA_NAME: &[u8; 7] = b"$LXMOD\0";
pub const LX_FILE_METADATA_DEVICE_ID_EA_NAME: &[u8; 7] = b"$LXDEV\0";
pub const VALID_COPY_FILE_CHUNK_FLAGS: u32 = 0;
pub const SYSTEM_PAGE_PRIORITY_BITS: u32 = 3;
pub const SYSTEM_PAGE_PRIORITY_LEVELS: u32 = 8;
pub const TOKEN_HAS_TRAVERSE_PRIVILEGE: u32 = 1;
Expand Down Expand Up @@ -4576,6 +4592,7 @@ pub const TOKEN_AUDIT_REDIRECTION_TRUST: u32 = 8388608;
pub const TOKEN_LEARNING_MODE_LOGGING: u32 = 16777216;
pub const TOKEN_PERMISSIVE_LEARNING_MODE: u32 = 50331648;
pub const TOKEN_INHERIT_SECURITY_FLAGS: u32 = 3670016;
pub const SECURITY_DESCRIPTOR_DO_NOT_FREE: u32 = 67108864;
pub const IO_OPEN_PAGING_FILE: u32 = 2;
pub const IO_OPEN_TARGET_DIRECTORY: u32 = 4;
pub const IO_STOP_ON_SYMLINK: u32 = 8;
Expand Down Expand Up @@ -4743,6 +4760,7 @@ pub const ECP_OPEN_PARAMETERS_FLAG_OPEN_FOR_WRITE: u32 = 2;
pub const ECP_OPEN_PARAMETERS_FLAG_OPEN_FOR_DELETE: u32 = 4;
pub const ECP_OPEN_PARAMETERS_FLAG_IGNORE_DIR_CASE_SENSITIVITY: u32 = 8;
pub const ECP_OPEN_PARAMETERS_FLAG_FAIL_ON_CASE_SENSITIVE_DIR: u32 = 16;
pub const OPLOCK_FS_FILTER_FLAGS_MASK: u32 = 8;
pub const QoCFileStatInformation: u32 = 1;
pub const QoCFileLxInformation: u32 = 2;
pub const QoCFileEaInformation: u32 = 4;
Expand Down Expand Up @@ -4847,10 +4865,22 @@ pub const SECBUFFER_SUBSCRIBE_GENERIC_TLS_EXTENSION: u32 = 26;
pub const SECBUFFER_FLAGS: u32 = 27;
pub const SECBUFFER_TRAFFIC_SECRETS: u32 = 28;
pub const SECBUFFER_CERTIFICATE_REQUEST_CONTEXT: u32 = 29;
pub const SECBUFFER_CHANNEL_BINDINGS_RESULT: u32 = 30;
pub const SECBUFFER_ATTRMASK: u32 = 4026531840;
pub const SECBUFFER_READONLY: u32 = 2147483648;
pub const SECBUFFER_READONLY_WITH_CHECKSUM: u32 = 268435456;
pub const SECBUFFER_RESERVED: u32 = 1610612736;
pub const SEC_CHANNEL_BINDINGS_AUDIT_BINDINGS: u32 = 1;
pub const SEC_CHANNEL_BINDINGS_VALID_FLAGS: u32 = 1;
pub const SEC_CHANNEL_BINDINGS_RESULT_CLIENT_SUPPORT: u32 = 1;
pub const SEC_CHANNEL_BINDINGS_RESULT_ABSENT: u32 = 2;
pub const SEC_CHANNEL_BINDINGS_RESULT_NOTVALID_MISMATCH: u32 = 4;
pub const SEC_CHANNEL_BINDINGS_RESULT_NOTVALID_MISSING: u32 = 8;
pub const SEC_CHANNEL_BINDINGS_RESULT_VALID_MATCHED: u32 = 16;
pub const SEC_CHANNEL_BINDINGS_RESULT_VALID_PROXY: u32 = 32;
pub const SEC_CHANNEL_BINDINGS_RESULT_VALID_MISSING: u32 = 64;
pub const SEC_CHANNEL_BINDINGS_RESULT_VALID: u32 = 112;
pub const SEC_CHANNEL_BINDINGS_RESULT_NOTVALID: u32 = 12;
pub const SZ_ALG_MAX_SIZE: u32 = 64;
pub const SECURITY_NATIVE_DREP: u32 = 16;
pub const SECURITY_NETWORK_DREP: u32 = 0;
Expand Down Expand Up @@ -5290,10 +5320,16 @@ extern "C" {
pub static GUID_STANDBY_RESET_PERCENT: GUID;
}
extern "C" {
pub static GUID_HUPR_ADAPTIVE_DISPLAY_TIMEOUT: GUID;
pub static GUID_HUPR_ADAPTIVE_AWAY_DISPLAY_TIMEOUT: GUID;
}
extern "C" {
pub static GUID_HUPR_ADAPTIVE_DIM_TIMEOUT: GUID;
pub static GUID_HUPR_ADAPTIVE_INATTENTIVE_DIM_TIMEOUT: GUID;
}
extern "C" {
pub static GUID_HUPR_ADAPTIVE_INATTENTIVE_DISPLAY_TIMEOUT: GUID;
}
extern "C" {
pub static GUID_HUPR_ADAPTIVE_AWAY_DIM_TIMEOUT: GUID;
}
extern "C" {
pub static GUID_ALLOW_STANDBY_STATES: GUID;
Expand Down
23 changes: 23 additions & 0 deletions crates/wdk-sys/generated_bindings/ntddk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10512,6 +10512,21 @@ extern "C" {
IoStatusBlock: PIO_STATUS_BLOCK,
) -> NTSTATUS;
}
extern "C" {
#[must_use]
pub fn NtCopyFileChunk(
SourceHandle: HANDLE,
DestHandle: HANDLE,
Event: HANDLE,
IoStatusBlock: PIO_STATUS_BLOCK,
Length: ULONG,
SourceOffset: PLARGE_INTEGER,
DestOffset: PLARGE_INTEGER,
SourceKey: PULONG,
DestKey: PULONG,
Flags: ULONG,
) -> NTSTATUS;
}
extern "C" {
#[must_use]
pub fn NtQueryObject(
Expand Down Expand Up @@ -12874,6 +12889,14 @@ extern "C" {
NotifyRoutine: POPLOCK_NOTIFY_ROUTINE,
) -> NTSTATUS;
}
extern "C" {
#[must_use]
pub fn FsRtlCheckOplockForFsFilterCallback(
Oplock: POPLOCK,
CallbackData: PVOID,
Flags: ULONG,
) -> NTSTATUS;
}
extern "C" {
pub fn FsRtlGetCurrentProcessLoaderList() -> PLIST_ENTRY;
}
Expand Down
Loading
Loading