Skip to content

Commit

Permalink
fix!: explicitly mark _KGDTENTRY64 and _KIDTENTRY64as opaque type…
Browse files Browse the repository at this point in the history
…s in bindgen

BREAKING CHANGE: defintions for `_KGDTENTRY64` and `_KIDTENTRY64` have been removed
  • Loading branch information
wmmc88 committed Jan 30, 2025
1 parent 4d4f997 commit 15d65ba
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 81 deletions.
4 changes: 2 additions & 2 deletions crates/wdk-build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

//! Build script for the `wdk-build` crate
//!
//! This provides a `nightly_feature` to the `wdk-build` crate, so that it can
//! conditionally enable nightly features.
//! This provides a `nightly_toolchain` feature to the `wdk-build` crate, so
//! that it can conditionally enable unstable features.
fn main() {
println!("cargo::rustc-check-cfg=cfg(nightly_toolchain)");
Expand Down
2 changes: 2 additions & 0 deletions crates/wdk-build/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ impl BuilderExt for Builder {
.blocklist_item("ExAllocatePoolWithQuotaTag") // Deprecated
.blocklist_item("ExAllocatePoolWithTagPriority") // Deprecated
.blocklist_item("ExAllocatePool") // Deprecated
.opaque_type("_KGDTENTRY64") // No definition in WDK
.opaque_type("_KIDTENTRY64") // No definition in WDK
// FIXME: bitfield generated with non-1byte alignment in _MCG_CAP
.blocklist_item(".*MCG_CAP(?:__bindgen.*)?")
.blocklist_item(".*WHEA_XPF_MCA_SECTION")
Expand Down
144 changes: 65 additions & 79 deletions crates/wdk-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ impl Config {
}

/// Returns a [`String`] containing the contents of a header file designed
/// for [`bindgen`](https://docs.rs/bindgen) to processs
/// for [`bindgen`](https://docs.rs/bindgen) to process
///
/// The contents contain `#include`'ed headers based off the [`ApiSubset`]
/// and [`Config`], as well as any additional definitions required for the
Expand All @@ -686,85 +686,11 @@ impl Config {
) -> String {
api_subsets
.into_iter()
.fold(String::new(), |mut acc, api_subset| {
acc.push_str(
self.headers(api_subset)
.fold(String::new(), |mut acc, header| {
acc.push_str(r#"#include ""#);
acc.push_str(&header);
acc.push_str("\"\n");
acc
})
.as_str(),
);

if api_subset == ApiSubset::Base
&& matches!(
self.driver_config,
DriverConfig::Wdm | DriverConfig::Kmdf(_)
)
{
// TODO: Why is there no definition for this struct? Maybe blocklist this struct
// in bindgen.
acc.push_str(
r"
typedef union _KGDTENTRY64
{
struct
{
unsigned short LimitLow;
unsigned short BaseLow;
union
{
struct
{
unsigned char BaseMiddle;
unsigned char Flags1;
unsigned char Flags2;
unsigned char BaseHigh;
} Bytes;
struct
{
unsigned long BaseMiddle : 8;
unsigned long Type : 5;
unsigned long Dpl : 2;
unsigned long Present : 1;
unsigned long LimitHigh : 4;
unsigned long System : 1;
unsigned long LongMode : 1;
unsigned long DefaultBig : 1;
unsigned long Granularity : 1;
unsigned long BaseHigh : 8;
} Bits;
};
unsigned long BaseUpper;
unsigned long MustBeZero;
};
unsigned __int64 Alignment;
} KGDTENTRY64, *PKGDTENTRY64;
typedef union _KIDTENTRY64
{
struct
{
unsigned short OffsetLow;
unsigned short Selector;
unsigned short IstIndex : 3;
unsigned short Reserved0 : 5;
unsigned short Type : 5;
unsigned short Dpl : 2;
unsigned short Present : 1;
unsigned short OffsetMiddle;
unsigned long OffsetHigh;
unsigned long Reserved1;
};
unsigned __int64 Alignment;
} KIDTENTRY64, *PKIDTENTRY64;
",
);
}
acc
.flat_map(|api_subset| {
self.headers(api_subset)
.map(|header| format!("#include \"{header}\"\n"))
})
.collect::<String>()
}

/// Configure a Cargo build of a library that depends on the WDK. This
Expand Down Expand Up @@ -1335,6 +1261,66 @@ mod tests {
assert_eq!(CpuArchitecture::try_from_cargo_str("arm"), None);
}

mod bindgen_header_contents {
use super::*;
use crate::{KmdfConfig, UmdfConfig};

#[test]
fn wdm() {
let config = with_env(&[("CARGO_CFG_TARGET_ARCH", "x86_64")], || Config {
driver_config: DriverConfig::Wdm,
..Default::default()
});

assert_eq!(
config.bindgen_header_contents([ApiSubset::Base]),
r#"#include "ntifs.h"
#include "ntddk.h"
#include "ntstrsafe.h"
"#,
);
}

#[test]
fn kmdf() {
let config = with_env(&[("CARGO_CFG_TARGET_ARCH", "x86_64")], || Config {
driver_config: DriverConfig::Kmdf(KmdfConfig {
kmdf_version_major: 1,
target_kmdf_version_minor: 33,
minimum_kmdf_version_minor: None,
}),
..Default::default()
});

assert_eq!(
config.bindgen_header_contents([ApiSubset::Base, ApiSubset::Wdf]),
r#"#include "ntifs.h"
#include "ntddk.h"
#include "ntstrsafe.h"
#include "wdf.h"
"#,
);
}

#[test]
fn umdf() {
let config = with_env(&[("CARGO_CFG_TARGET_ARCH", "aarch64")], || Config {
driver_config: DriverConfig::Umdf(UmdfConfig {
umdf_version_major: 2,
target_umdf_version_minor: 15,
minimum_umdf_version_minor: None,
}),
..Default::default()
});

assert_eq!(
config.bindgen_header_contents([ApiSubset::Base, ApiSubset::Wdf]),
r#"#include "windows.h"
#include "wdf.h"
"#,
);
}
}
mod compute_wdffunctions_symbol_name {
use super::*;
use crate::{KmdfConfig, UmdfConfig};
Expand Down

0 comments on commit 15d65ba

Please sign in to comment.