Update cstr macro

This commit is contained in:
topjohnwu 2023-05-25 01:03:04 -07:00
parent 18d0cedbe2
commit 533aeadd38
3 changed files with 20 additions and 58 deletions

View File

@ -58,59 +58,21 @@ macro_rules! bfmt_cstr {
}}; }};
} }
// The cstr! macro is inspired by https://github.com/Nugine/const-str // The cstr! macro is copied from https://github.com/bytecodealliance/rustix/blob/main/src/cstr.rs
macro_rules! const_assert {
($s: expr) => {
assert!($s)
};
}
pub struct ToCStr<'a>(pub &'a str);
impl ToCStr<'_> {
const fn assert_no_nul(&self) {
let bytes = self.0.as_bytes();
let mut i = 0;
while i < bytes.len() {
const_assert!(bytes[i] != 0);
i += 1;
}
}
pub const fn eval_len(&self) -> usize {
self.assert_no_nul();
self.0.as_bytes().len() + 1
}
pub const fn eval_bytes<const N: usize>(&self) -> [u8; N] {
let mut buf = [0; N];
let mut pos = 0;
let bytes = self.0.as_bytes();
let mut i = 0;
while i < bytes.len() {
const_assert!(bytes[i] != 0);
buf[pos] = bytes[i];
pos += 1;
i += 1;
}
pos += 1;
const_assert!(pos == N);
buf
}
}
#[macro_export] #[macro_export]
macro_rules! cstr { macro_rules! cstr {
($s:literal) => {{ ($str:literal) => {{
const LEN: usize = $crate::ToCStr($s).eval_len(); assert!(
const BUF: [u8; LEN] = $crate::ToCStr($s).eval_bytes(); !$str.bytes().any(|b| b == b'\0'),
unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(&BUF) } "cstr argument contains embedded NUL bytes",
);
unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(concat!($str, "\0").as_bytes()) }
}}; }};
} }
#[macro_export] #[macro_export]
macro_rules! str_ptr { macro_rules! raw_cstr {
($s:literal) => {{ ($s:literal) => {{
cstr!($s).as_ptr() cstr!($s).as_ptr()
}}; }};

View File

@ -62,12 +62,12 @@ pub fn android_logging() {
let mut buf: [u8; 4096] = [0; 4096]; let mut buf: [u8; 4096] = [0; 4096];
fmt_to_buf(&mut buf, args); fmt_to_buf(&mut buf, args);
unsafe { unsafe {
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), buf.as_ptr()); __android_log_write(level_to_prio(level), raw_cstr!("Magisk"), buf.as_ptr());
} }
} }
fn android_log_write(level: LogLevel, msg: &[u8]) { fn android_log_write(level: LogLevel, msg: &[u8]) {
unsafe { unsafe {
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), msg.as_ptr()); __android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
} }
} }
@ -87,13 +87,13 @@ pub fn magisk_logging() {
let mut buf: [u8; 4096] = [0; 4096]; let mut buf: [u8; 4096] = [0; 4096];
let len = fmt_to_buf(&mut buf, args); let len = fmt_to_buf(&mut buf, args);
unsafe { unsafe {
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), buf.as_ptr()); __android_log_write(level_to_prio(level), raw_cstr!("Magisk"), buf.as_ptr());
} }
magisk_log_write(level_to_prio(level), &buf[..len]); magisk_log_write(level_to_prio(level), &buf[..len]);
} }
fn magisk_write(level: LogLevel, msg: &[u8]) { fn magisk_write(level: LogLevel, msg: &[u8]) {
unsafe { unsafe {
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), msg.as_ptr()); __android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
} }
magisk_log_write(level_to_prio(level), &msg); magisk_log_write(level_to_prio(level), &msg);
} }
@ -114,13 +114,13 @@ pub fn zygisk_logging() {
let mut buf: [u8; 4096] = [0; 4096]; let mut buf: [u8; 4096] = [0; 4096];
let len = fmt_to_buf(&mut buf, args); let len = fmt_to_buf(&mut buf, args);
unsafe { unsafe {
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), buf.as_ptr()); __android_log_write(level_to_prio(level), raw_cstr!("Magisk"), buf.as_ptr());
} }
zygisk_log_write(level_to_prio(level), &buf[..len]); zygisk_log_write(level_to_prio(level), &buf[..len]);
} }
fn zygisk_write(level: LogLevel, msg: &[u8]) { fn zygisk_write(level: LogLevel, msg: &[u8]) {
unsafe { unsafe {
__android_log_write(level_to_prio(level), str_ptr!("Magisk"), msg.as_ptr()); __android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
} }
zygisk_log_write(level_to_prio(level), &msg); zygisk_log_write(level_to_prio(level), &msg);
} }
@ -312,7 +312,7 @@ extern "C" fn logfile_writer(arg: *mut c_void) -> *mut c_void {
aux_len = strftime( aux_len = strftime(
aux.as_mut_ptr().cast(), aux.as_mut_ptr().cast(),
aux.len(), aux.len(),
str_ptr!("%m-%d %T"), raw_cstr!("%m-%d %T"),
&tm, &tm,
); );
let ms = ts.tv_nsec / 1000000; let ms = ts.tv_nsec / 1000000;

View File

@ -16,10 +16,10 @@ static KMSG: OnceLock<File> = OnceLock::new();
pub fn setup_klog() { pub fn setup_klog() {
// Shut down first 3 fds // Shut down first 3 fds
unsafe { unsafe {
let mut fd = open(str_ptr!("/dev/null"), O_RDWR | O_CLOEXEC); let mut fd = open(raw_cstr!("/dev/null"), O_RDWR | O_CLOEXEC);
if fd < 0 { if fd < 0 {
mknod(str_ptr!("/null"), S_IFCHR | 0666, makedev(1, 3)); mknod(raw_cstr!("/null"), S_IFCHR | 0666, makedev(1, 3));
fd = open(str_ptr!("/null"), O_RDWR | O_CLOEXEC); fd = open(raw_cstr!("/null"), O_RDWR | O_CLOEXEC);
fs::remove_file("/null").ok(); fs::remove_file("/null").ok();
} }
@ -35,10 +35,10 @@ pub fn setup_klog() {
KMSG.set(kmsg).ok(); KMSG.set(kmsg).ok();
} else { } else {
unsafe { unsafe {
mknod(str_ptr!("/kmsg"), S_IFCHR | 0666, makedev(1, 11)); mknod(raw_cstr!("/kmsg"), S_IFCHR | 0666, makedev(1, 11));
KMSG.set(File::options().write(true).open("/kmsg").unwrap()) KMSG.set(File::options().write(true).open("/kmsg").unwrap())
.ok(); .ok();
unlink(str_ptr!("/kmsg")); unlink(raw_cstr!("/kmsg"));
} }
} }