summaryrefslogtreecommitdiff
path: root/crates/windows-kernel-rs/src/io.rs
blob: 7d783cec1e3c4b0029cba7799195c25cda2f25a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use windows_kernel_sys::{base::ANSI_STRING, ntoskrnl::DbgPrint};

#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
}

#[macro_export]
macro_rules! println {
    () => ($crate::print!("\n"));
    ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}

#[doc(hidden)]
pub fn _print(args: core::fmt::Arguments) {
  // Format the string using the `alloc::format!` as this is guaranteed to return
  // a `String` instead of a `Result` that we would have to `unwrap`. This
  // ensures that this code stays panic-free.
  let s = alloc::format!("{}", args);

  // Print the string. We must make sure to not pass this user-supplied string as
  // the format string, as `DbgPrint` may then format any format specifiers it
  // contains. This could potentially be an attack vector.
  let s = ANSI_STRING {
    Length:        s.len() as u16,
    MaximumLength: s.len() as u16,
    Buffer:        s.as_ptr() as _,
  };

  unsafe { DbgPrint("%Z\0".as_ptr() as _, &s) };
}