Skip to content

Commit b64d8f4

Browse files
authored
fix!: explicitly mark _KGDTENTRY64 and _KIDTENTRY64 as opaque types in bindgen (#277)
1 parent 74e1087 commit b64d8f4

File tree

3 files changed

+67
-81
lines changed

3 files changed

+67
-81
lines changed

crates/wdk-build/build.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
//! Build script for the `wdk-build` crate
55
//!
6-
//! This provides a `nightly_feature` to the `wdk-build` crate, so that it can
7-
//! conditionally enable nightly features.
6+
//! This provides a `nightly_toolchain` feature to the `wdk-build` crate, so
7+
//! that it can conditionally enable unstable features.
88
99
fn main() {
1010
println!("cargo::rustc-check-cfg=cfg(nightly_toolchain)");

crates/wdk-build/src/bindgen.rs

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ impl BuilderExt for Builder {
7171
.blocklist_item("ExAllocatePoolWithQuotaTag") // Deprecated
7272
.blocklist_item("ExAllocatePoolWithTagPriority") // Deprecated
7373
.blocklist_item("ExAllocatePool") // Deprecated
74+
.opaque_type("_KGDTENTRY64") // No definition in WDK
75+
.opaque_type("_KIDTENTRY64") // No definition in WDK
7476
// FIXME: bitfield generated with non-1byte alignment in _MCG_CAP
7577
.blocklist_item(".*MCG_CAP(?:__bindgen.*)?")
7678
.blocklist_item(".*WHEA_XPF_MCA_SECTION")

crates/wdk-build/src/lib.rs

+63-79
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ impl Config {
675675
}
676676

677677
/// Returns a [`String`] containing the contents of a header file designed
678-
/// for [`bindgen`](https://docs.rs/bindgen) to processs
678+
/// for [`bindgen`](https://docs.rs/bindgen) to process
679679
///
680680
/// The contents contain `#include`'ed headers based off the [`ApiSubset`]
681681
/// and [`Config`], as well as any additional definitions required for the
@@ -686,85 +686,11 @@ impl Config {
686686
) -> String {
687687
api_subsets
688688
.into_iter()
689-
.fold(String::new(), |mut acc, api_subset| {
690-
acc.push_str(
691-
self.headers(api_subset)
692-
.fold(String::new(), |mut acc, header| {
693-
acc.push_str(r#"#include ""#);
694-
acc.push_str(&header);
695-
acc.push_str("\"\n");
696-
acc
697-
})
698-
.as_str(),
699-
);
700-
701-
if api_subset == ApiSubset::Base
702-
&& matches!(
703-
self.driver_config,
704-
DriverConfig::Wdm | DriverConfig::Kmdf(_)
705-
)
706-
{
707-
// TODO: Why is there no definition for this struct? Maybe blocklist this struct
708-
// in bindgen.
709-
acc.push_str(
710-
r"
711-
typedef union _KGDTENTRY64
712-
{
713-
struct
714-
{
715-
unsigned short LimitLow;
716-
unsigned short BaseLow;
717-
union
718-
{
719-
struct
720-
{
721-
unsigned char BaseMiddle;
722-
unsigned char Flags1;
723-
unsigned char Flags2;
724-
unsigned char BaseHigh;
725-
} Bytes;
726-
struct
727-
{
728-
unsigned long BaseMiddle : 8;
729-
unsigned long Type : 5;
730-
unsigned long Dpl : 2;
731-
unsigned long Present : 1;
732-
unsigned long LimitHigh : 4;
733-
unsigned long System : 1;
734-
unsigned long LongMode : 1;
735-
unsigned long DefaultBig : 1;
736-
unsigned long Granularity : 1;
737-
unsigned long BaseHigh : 8;
738-
} Bits;
739-
};
740-
unsigned long BaseUpper;
741-
unsigned long MustBeZero;
742-
};
743-
unsigned __int64 Alignment;
744-
} KGDTENTRY64, *PKGDTENTRY64;
745-
746-
typedef union _KIDTENTRY64
747-
{
748-
struct
749-
{
750-
unsigned short OffsetLow;
751-
unsigned short Selector;
752-
unsigned short IstIndex : 3;
753-
unsigned short Reserved0 : 5;
754-
unsigned short Type : 5;
755-
unsigned short Dpl : 2;
756-
unsigned short Present : 1;
757-
unsigned short OffsetMiddle;
758-
unsigned long OffsetHigh;
759-
unsigned long Reserved1;
760-
};
761-
unsigned __int64 Alignment;
762-
} KIDTENTRY64, *PKIDTENTRY64;
763-
",
764-
);
765-
}
766-
acc
689+
.flat_map(|api_subset| {
690+
self.headers(api_subset)
691+
.map(|header| format!("#include \"{header}\"\n"))
767692
})
693+
.collect::<String>()
768694
}
769695

770696
/// Configure a Cargo build of a library that depends on the WDK. This
@@ -1332,6 +1258,64 @@ mod tests {
13321258
assert_eq!(CpuArchitecture::try_from_cargo_str("arm"), None);
13331259
}
13341260

1261+
mod bindgen_header_contents {
1262+
use super::*;
1263+
use crate::{KmdfConfig, UmdfConfig};
1264+
1265+
#[test]
1266+
fn wdm() {
1267+
let config = with_env(&[("CARGO_CFG_TARGET_ARCH", "x86_64")], || Config {
1268+
driver_config: DriverConfig::Wdm,
1269+
..Default::default()
1270+
});
1271+
1272+
assert_eq!(
1273+
config.bindgen_header_contents([ApiSubset::Base]),
1274+
r#"#include "ntifs.h"
1275+
#include "ntddk.h"
1276+
"#,
1277+
);
1278+
}
1279+
1280+
#[test]
1281+
fn kmdf() {
1282+
let config = with_env(&[("CARGO_CFG_TARGET_ARCH", "x86_64")], || Config {
1283+
driver_config: DriverConfig::Kmdf(KmdfConfig {
1284+
kmdf_version_major: 1,
1285+
target_kmdf_version_minor: 33,
1286+
minimum_kmdf_version_minor: None,
1287+
}),
1288+
..Default::default()
1289+
});
1290+
1291+
assert_eq!(
1292+
config.bindgen_header_contents([ApiSubset::Base, ApiSubset::Wdf]),
1293+
r#"#include "ntifs.h"
1294+
#include "ntddk.h"
1295+
#include "wdf.h"
1296+
"#,
1297+
);
1298+
}
1299+
1300+
#[test]
1301+
fn umdf() {
1302+
let config = with_env(&[("CARGO_CFG_TARGET_ARCH", "aarch64")], || Config {
1303+
driver_config: DriverConfig::Umdf(UmdfConfig {
1304+
umdf_version_major: 2,
1305+
target_umdf_version_minor: 15,
1306+
minimum_umdf_version_minor: None,
1307+
}),
1308+
..Default::default()
1309+
});
1310+
1311+
assert_eq!(
1312+
config.bindgen_header_contents([ApiSubset::Base, ApiSubset::Wdf]),
1313+
r#"#include "windows.h"
1314+
#include "wdf.h"
1315+
"#,
1316+
);
1317+
}
1318+
}
13351319
mod compute_wdffunctions_symbol_name {
13361320
use super::*;
13371321
use crate::{KmdfConfig, UmdfConfig};

0 commit comments

Comments
 (0)