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

refactor!: Remove lazy static instances #250

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
passsed all pipeline checks
  • Loading branch information
Leon Durrenberger committed Dec 12, 2024
commit a8280a3d99b7c10d5cf9960b212d31d05c4323f1
1 change: 0 additions & 1 deletion Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -45,9 +45,6 @@ cfg-if = "1.0.0"
clap = "4.5.9"
clap-cargo = "0.14.1"
itertools = "0.13.0"
once_cell = { version = "1.20.2", default-features = false, features = [
"alloc",
] }
paste = "1.0.15"
pretty_assertions = "1.4.0"
proc-macro2 = "1.0.86"
11 changes: 6 additions & 5 deletions crates/wdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -230,7 +230,8 @@ impl DerivedASTFragments {
// function table index and the correct function pointer type.
unsafe {
let wdf_function_table = wdk_sys::WdfFunctions;

let wdf_function_count = wdk_sys::wdf::get_wdf_function_count();

// SAFETY: This is safe because:
// 1. `WdfFunctions` is valid for reads for `{NUM_WDF_FUNCTIONS_PLACEHOLDER}` * `core::mem::size_of::<WDFFUNC>()`
// bytes, and is guaranteed to be aligned and it must be properly aligned.
@@ -239,10 +240,10 @@ impl DerivedASTFragments {
// 3. WDF does not mutate the memory referenced by the returned slice for for its entire `'static' lifetime.
// 4. The total size, `{NUM_WDF_FUNCTIONS_PLACEHOLDER}` * `core::mem::size_of::<WDFFUNC>()`, of the slice must be no
// larger than `isize::MAX`. This is proven by the below `const_assert!`.
debug_assert!(isize::try_from(wdk_sys::wdf::WDF_FUNCTION_COUNT * core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok());
let wdf_function_table = core::slice::from_raw_parts(wdf_function_table, wdk_sys::wdf::WDF_FUNCTION_COUNT);

debug_assert!(isize::try_from(wdf_function_count * core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok());
let wdf_function_table = core::slice::from_raw_parts(wdf_function_table, wdf_function_count);

core::mem::transmute(
// FIXME: investigate why _WDFFUNCENUM does not have a generated type alias without the underscore prefix
wdf_function_table[wdk_sys::_WDFFUNCENUM::#function_table_index as usize],
5 changes: 0 additions & 5 deletions crates/wdk-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -21,15 +21,13 @@ anyhow.workspace = true
bindgen.workspace = true
cargo_metadata.workspace = true
cc.workspace = true
once_cell.workspace = true
serde_json.workspace = true
thiserror.workspace = true
tracing.workspace = true
tracing-subscriber = { workspace = true, features = ["env-filter"] }
wdk-build.workspace = true

[dependencies]
once_cell.workspace = true
rustversion.workspace = true
wdk-macros.workspace = true

@@ -72,6 +70,3 @@ missing_crate_level_docs = "warn"
private_intra_doc_links = "warn"
redundant_explicit_links = "warn"
unescaped_backticks = "warn"

[package.metadata.cargo-machete]
ignored = ["once_cell"]
36 changes: 23 additions & 13 deletions crates/wdk-sys/build.rs
Original file line number Diff line number Diff line change
@@ -35,16 +35,23 @@ const OUT_DIR_PLACEHOLDER: &str =
"<PLACEHOLDER FOR LITERAL VALUE CONTAINING OUT_DIR OF wdk-sys CRATE>";
const WDFFUNCTIONS_SYMBOL_NAME_PLACEHOLDER: &str =
"<PLACEHOLDER FOR LITERAL VALUE CONTAINING WDFFUNCTIONS SYMBOL NAME>";
const WDF_FUNCTION_COUNT_PLACEHOLDER: &str =
"<PLACEHOLDER FOR EVALUATION CONTAINING WDF_FUNCTION_COUNT VALUE";
leon-xd marked this conversation as resolved.
Show resolved Hide resolved

const WDF_FUNCTION_COUNT_DECLARATION_EXTERNAL_SYMBOL: &str = "
// SAFETY: `crate::WdfFunctionCount` is generated as a mutable static, but is not supposed \
to be ever mutated by WDF.
/// Number of functions in the WDF function table
pub static WDF_FUNCTION_COUNT: usize = unsafe { crate::WdfFunctionCount } as usize;";
const WDF_FUNCTION_COUNT_DECLARATION_EXTERNAL_SYMBOL: &str =
leon-xd marked this conversation as resolved.
Show resolved Hide resolved
"(unsafe { crate::WdfFunctionCount }) as usize";

const WDF_FUNCTION_COUNT_DECLARATION_TABLE_INDEX: &str = "
/// Number of functions in the WDF function table
pub static WDF_FUNCTION_COUNT: usize = crate::_WDFFUNCENUM::WdfFunctionTableNumEntries as usize;";
const WDF_FUNCTION_COUNT_DECLARATION_TABLE_INDEX: &str =
"crate::_WDFFUNCENUM::WdfFunctionTableNumEntries as usize";

const WDF_FUNCTION_COUNT_FUNCTION_TEMPLATE: LazyLock<String> = LazyLock::new(|| {
format!(
r#"/// function to access the value of the number of functions in the WDF function table.
pub fn get_wdf_function_count() -> usize {{
{WDF_FUNCTION_COUNT_PLACEHOLDER}
}}"#
)
});

static CALL_UNSAFE_WDF_BINDING_TEMPLATE: LazyLock<String> = LazyLock::new(|| {
format!(
@@ -277,11 +284,14 @@ fn generate_wdf_function_table(out_path: &Path, config: &Config) -> std::io::Res
}
};

let wdf_function_table_count_snippet = if is_wdf_function_count_generated {
WDF_FUNCTION_COUNT_DECLARATION_EXTERNAL_SYMBOL
} else {
WDF_FUNCTION_COUNT_DECLARATION_TABLE_INDEX
};
let wdf_function_table_count_snippet = WDF_FUNCTION_COUNT_FUNCTION_TEMPLATE.replace(
WDF_FUNCTION_COUNT_PLACEHOLDER,
if is_wdf_function_count_generated {
WDF_FUNCTION_COUNT_DECLARATION_EXTERNAL_SYMBOL
} else {
WDF_FUNCTION_COUNT_DECLARATION_TABLE_INDEX
},
);

generated_file.write_all(wdf_function_table_count_snippet.as_bytes())?;
Ok(())
1 change: 0 additions & 1 deletion crates/wdk-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@

#![no_std]


#[cfg(any(
driver_model__driver_type = "WDM",
driver_model__driver_type = "KMDF",
2 changes: 1 addition & 1 deletion crates/wdk-sys/src/wdf.rs
Original file line number Diff line number Diff line change
@@ -20,4 +20,4 @@ mod bindings {
include!(concat!(env!("OUT_DIR"), "/wdf.rs"));
}

include!(concat!(env!("OUT_DIR"), "/wdf_function_table.rs"));
include!(concat!(env!("OUT_DIR"), "/wdf_function_count.rs"));
leon-xd marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion examples/sample-kmdf-driver/Cargo.lock

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

1 change: 0 additions & 1 deletion examples/sample-umdf-driver/Cargo.lock

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

1 change: 0 additions & 1 deletion examples/sample-wdm-driver/Cargo.lock

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

1 change: 0 additions & 1 deletion tests/config-kmdf/Cargo.lock

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

1 change: 0 additions & 1 deletion tests/config-umdf/Cargo.lock

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

1 change: 0 additions & 1 deletion tests/mixed-package-kmdf-workspace/Cargo.lock

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

1 change: 0 additions & 1 deletion tests/umdf-driver-workspace/Cargo.lock

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

22 changes: 13 additions & 9 deletions tests/wdk-macros-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
// License: MIT OR Apache-2.0

use std::{path::PathBuf, sync::LazyLock};

use fs4::FileExt;
pub use macrotest::{expand, expand_args};
pub use owo_colors::OwoColorize;
@@ -17,17 +18,20 @@ const TOOLCHAIN_CHANNEL_NAME: &str = "beta";
#[rustversion::nightly]
const TOOLCHAIN_CHANNEL_NAME: &str = "nightly";

static TESTS_FOLDER_PATH: LazyLock<PathBuf> = LazyLock::new(|| {[env!("CARGO_MANIFEST_DIR"), "tests"].iter().collect()});
static INPUTS_FOLDER_PATH: LazyLock<PathBuf> = LazyLock::new(|| {TESTS_FOLDER_PATH.join("inputs")});
pub static MACROTEST_INPUT_FOLDER_PATH: LazyLock<PathBuf> = LazyLock::new(|| {INPUTS_FOLDER_PATH.join("macrotest")});
pub static TRYBUILD_INPUT_FOLDER_PATH: LazyLock<PathBuf> = LazyLock::new(|| {INPUTS_FOLDER_PATH.join("trybuild")});
static OUTPUTS_FOLDER_PATH: LazyLock<PathBuf> = LazyLock::new(|| {TESTS_FOLDER_PATH.join("outputs")});
static TOOLCHAIN_SPECIFIC_OUTPUTS_FOLDER_PATH: LazyLock<PathBuf> = LazyLock::new(|| {OUTPUTS_FOLDER_PATH.join(TOOLCHAIN_CHANNEL_NAME)});
static TESTS_FOLDER_PATH: LazyLock<PathBuf> =
LazyLock::new(|| [env!("CARGO_MANIFEST_DIR"), "tests"].iter().collect());
static INPUTS_FOLDER_PATH: LazyLock<PathBuf> = LazyLock::new(|| TESTS_FOLDER_PATH.join("inputs"));
pub static MACROTEST_INPUT_FOLDER_PATH: LazyLock<PathBuf> =
LazyLock::new(|| INPUTS_FOLDER_PATH.join("macrotest"));
pub static TRYBUILD_INPUT_FOLDER_PATH: LazyLock<PathBuf> =
LazyLock::new(|| INPUTS_FOLDER_PATH.join("trybuild"));
static OUTPUTS_FOLDER_PATH: LazyLock<PathBuf> = LazyLock::new(|| TESTS_FOLDER_PATH.join("outputs"));
static TOOLCHAIN_SPECIFIC_OUTPUTS_FOLDER_PATH: LazyLock<PathBuf> =
LazyLock::new(|| OUTPUTS_FOLDER_PATH.join(TOOLCHAIN_CHANNEL_NAME));
pub static MACROTEST_OUTPUT_FOLDER_PATH: LazyLock<PathBuf> =
LazyLock::new(|| {TOOLCHAIN_SPECIFIC_OUTPUTS_FOLDER_PATH.join("macrotest")});
LazyLock::new(|| TOOLCHAIN_SPECIFIC_OUTPUTS_FOLDER_PATH.join("macrotest"));
pub static TRYBUILD_OUTPUT_FOLDER_PATH: LazyLock<PathBuf> =
LazyLock::new(|| {TOOLCHAIN_SPECIFIC_OUTPUTS_FOLDER_PATH.join("trybuild")});

LazyLock::new(|| TOOLCHAIN_SPECIFIC_OUTPUTS_FOLDER_PATH.join("trybuild"));

/// Given a filename `f` which contains code utilizing
/// [`wdk_sys::call_unsafe_wdf_function_binding`], generates a pair of tests to
Original file line number Diff line number Diff line change
@@ -40,21 +40,22 @@ fn foo(
) {
let wdf_function: wdk_sys::PFN_WDFDEVICEINITSETPNPPOWEREVENTCALLBACKS = Some(unsafe {
let wdf_function_table = wdk_sys::WdfFunctions;
let wdf_function_count = wdk_sys::wdf::get_wdf_function_count();
if true {
if !isize::try_from(
wdk_sys::wdf::WDF_FUNCTION_COUNT
wdf_function_count
* core::mem::size_of::<wdk_sys::WDFFUNC>(),
)
.is_ok()
{
::core::panicking::panic(
"assertion failed: isize::try_from(wdk_sys::wdf::WDF_FUNCTION_COUNT *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
"assertion failed: isize::try_from(wdf_function_count *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
)
}
}
let wdf_function_table = core::slice::from_raw_parts(
wdf_function_table,
wdk_sys::wdf::WDF_FUNCTION_COUNT,
wdf_function_count,
);
core::mem::transmute(
wdf_function_table[wdk_sys::_WDFFUNCENUM::WdfDeviceInitSetPnpPowerEventCallbacksTableIndex
Original file line number Diff line number Diff line change
@@ -48,21 +48,22 @@ pub extern "system" fn driver_entry(
) -> NTSTATUS {
let wdf_function: wdk_sys::PFN_WDFDRIVERCREATE = Some(unsafe {
let wdf_function_table = wdk_sys::WdfFunctions;
let wdf_function_count = wdk_sys::wdf::get_wdf_function_count();
if true {
if !isize::try_from(
wdk_sys::wdf::WDF_FUNCTION_COUNT
wdf_function_count
* core::mem::size_of::<wdk_sys::WDFFUNC>(),
)
.is_ok()
{
::core::panicking::panic(
"assertion failed: isize::try_from(wdk_sys::wdf::WDF_FUNCTION_COUNT *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
"assertion failed: isize::try_from(wdf_function_count *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
)
}
}
let wdf_function_table = core::slice::from_raw_parts(
wdf_function_table,
wdk_sys::wdf::WDF_FUNCTION_COUNT,
wdf_function_count,
);
core::mem::transmute(
wdf_function_table[wdk_sys::_WDFFUNCENUM::WdfDriverCreateTableIndex
Original file line number Diff line number Diff line change
@@ -18,21 +18,22 @@ extern "C" fn evt_driver_device_add(
) -> NTSTATUS {
let wdf_function: wdk_sys::PFN_WDFDEVICECREATE = Some(unsafe {
let wdf_function_table = wdk_sys::WdfFunctions;
let wdf_function_count = wdk_sys::wdf::get_wdf_function_count();
if true {
if !isize::try_from(
wdk_sys::wdf::WDF_FUNCTION_COUNT
wdf_function_count
* core::mem::size_of::<wdk_sys::WDFFUNC>(),
)
.is_ok()
{
::core::panicking::panic(
"assertion failed: isize::try_from(wdk_sys::wdf::WDF_FUNCTION_COUNT *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
"assertion failed: isize::try_from(wdf_function_count *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
)
}
}
let wdf_function_table = core::slice::from_raw_parts(
wdf_function_table,
wdk_sys::wdf::WDF_FUNCTION_COUNT,
wdf_function_count,
);
core::mem::transmute(
wdf_function_table[wdk_sys::_WDFFUNCENUM::WdfDeviceCreateTableIndex
Original file line number Diff line number Diff line change
@@ -20,21 +20,22 @@ fn create_device_interface(wdf_device: wdk_sys::WDFDEVICE) -> wdk_sys::NTSTATUS
) -> NTSTATUS {
let wdf_function: wdk_sys::PFN_WDFDEVICECREATEDEVICEINTERFACE = Some(unsafe {
let wdf_function_table = wdk_sys::WdfFunctions;
let wdf_function_count = wdk_sys::wdf::get_wdf_function_count();
if true {
if !isize::try_from(
wdk_sys::wdf::WDF_FUNCTION_COUNT
wdf_function_count
* core::mem::size_of::<wdk_sys::WDFFUNC>(),
)
.is_ok()
{
::core::panicking::panic(
"assertion failed: isize::try_from(wdk_sys::wdf::WDF_FUNCTION_COUNT *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
"assertion failed: isize::try_from(wdf_function_count *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
)
}
}
let wdf_function_table = core::slice::from_raw_parts(
wdf_function_table,
wdk_sys::wdf::WDF_FUNCTION_COUNT,
wdf_function_count,
);
core::mem::transmute(
wdf_function_table[wdk_sys::_WDFFUNCENUM::WdfDeviceCreateDeviceInterfaceTableIndex
Original file line number Diff line number Diff line change
@@ -25,21 +25,22 @@ pub extern "system" fn driver_entry(
) -> NTSTATUS {
let wdf_function: wdk_sys::PFN_WDFDRIVERCREATE = Some(unsafe {
let wdf_function_table = wdk_sys::WdfFunctions;
let wdf_function_count = wdk_sys::wdf::get_wdf_function_count();
if true {
if !isize::try_from(
wdk_sys::wdf::WDF_FUNCTION_COUNT
wdf_function_count
* core::mem::size_of::<wdk_sys::WDFFUNC>(),
)
.is_ok()
{
::core::panicking::panic(
"assertion failed: isize::try_from(wdk_sys::wdf::WDF_FUNCTION_COUNT *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
"assertion failed: isize::try_from(wdf_function_count *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
)
}
}
let wdf_function_table = core::slice::from_raw_parts(
wdf_function_table,
wdk_sys::wdf::WDF_FUNCTION_COUNT,
wdf_function_count,
);
core::mem::transmute(
wdf_function_table[wdk_sys::_WDFFUNCENUM::WdfDriverCreateTableIndex
Original file line number Diff line number Diff line change
@@ -17,21 +17,22 @@ fn process_wdf_request(request: wdk_sys::WDFREQUEST) {
) -> NTSTATUS {
let wdf_function: wdk_sys::PFN_WDFREQUESTRETRIEVEOUTPUTBUFFER = Some(unsafe {
let wdf_function_table = wdk_sys::WdfFunctions;
let wdf_function_count = wdk_sys::wdf::get_wdf_function_count();
if true {
if !isize::try_from(
wdk_sys::wdf::WDF_FUNCTION_COUNT
wdf_function_count
* core::mem::size_of::<wdk_sys::WDFFUNC>(),
)
.is_ok()
{
::core::panicking::panic(
"assertion failed: isize::try_from(wdk_sys::wdf::WDF_FUNCTION_COUNT *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
"assertion failed: isize::try_from(wdf_function_count *\n core::mem::size_of::<wdk_sys::WDFFUNC>()).is_ok()",
)
}
}
let wdf_function_table = core::slice::from_raw_parts(
wdf_function_table,
wdk_sys::wdf::WDF_FUNCTION_COUNT,
wdf_function_count,
);
core::mem::transmute(
wdf_function_table[wdk_sys::_WDFFUNCENUM::WdfRequestRetrieveOutputBufferTableIndex
Loading
Loading