From 86bb404b0a6990c3247fe21872be61e52e687ba4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 21 Aug 2025 21:05:35 -0700 Subject: [PATCH] Make log_err directly return LoggedResult --- native/src/base/result.rs | 5 ++++- native/src/boot/cli.rs | 4 ++-- native/src/boot/compress.rs | 4 ++-- native/src/boot/cpio.rs | 36 ++++++++++++++++++-------------- native/src/boot/sign.rs | 10 ++++----- native/src/core/zygisk/daemon.rs | 6 +++--- native/src/sepolicy/cli.rs | 10 ++++----- 7 files changed, 41 insertions(+), 34 deletions(-) diff --git a/native/src/base/result.rs b/native/src/base/result.rs index 0fd3a458b..24867352b 100644 --- a/native/src/base/result.rs +++ b/native/src/base/result.rs @@ -23,9 +23,12 @@ pub type LoggedResult = Result; #[macro_export] macro_rules! log_err { + () => {{ + Err($crate::LoggedError::default()) + }}; ($($args:tt)+) => {{ $crate::error!($($args)+); - $crate::LoggedError::default() + Err($crate::LoggedError::default()) }}; } diff --git a/native/src/boot/cli.rs b/native/src/boot/cli.rs index 5f4723468..10f23af62 100644 --- a/native/src/boot/cli.rs +++ b/native/src/boot/cli.rs @@ -363,7 +363,7 @@ pub extern "C" fn main(argc: i32, argv: *const *const c_char, _envp: *const *con ref args, }) => { if args.len() > 2 { - Err(log_err!("Too many arguments"))?; + log_err!("Too many arguments")?; } extract_boot_from_payload( payload, @@ -382,7 +382,7 @@ pub extern "C" fn main(argc: i32, argv: *const *const c_char, _envp: *const *con Utf8CStr::from_string(src), Utf8CStr::from_string(dest), ) { - Err(log_err!("Failed to patch"))?; + log_err!("Failed to patch")?; } } Action::Cpio(Cpio { diff --git a/native/src/boot/compress.rs b/native/src/boot/compress.rs index 6699a0cf4..321af2c73 100644 --- a/native/src/boot/compress.rs +++ b/native/src/boot/compress.rs @@ -462,7 +462,7 @@ pub(crate) fn decompress(infile: &mut String, outfile: Option<&mut String>) -> L eprintln!("Detected format: {format}"); if !format.is_compressed() { - return Err(log_err!("Input file is not a supported type!")); + return log_err!("Input file is not a supported type!"); } let raw_out = if let Some(outfile) = outfile { @@ -478,7 +478,7 @@ pub(crate) fn decompress(infile: &mut String, outfile: Option<&mut String>) -> L rm_in = true; let mut outfile = if let Some((outfile, ext)) = infile.rsplit_once('.') { if ext != format.ext() { - Err(log_err!("Input file is not a supported type!"))?; + log_err!("Input file is not a supported type!")?; } outfile.to_owned() } else { diff --git a/native/src/boot/cpio.rs b/native/src/boot/cpio.rs index d84c7d07c..4079c2ac2 100644 --- a/native/src/boot/cpio.rs +++ b/native/src/boot/cpio.rs @@ -20,8 +20,8 @@ use base::libc::{ dev_t, gid_t, major, makedev, minor, mknod, mode_t, uid_t, }; use base::{ - BytesExt, EarlyExitExt, LoggedResult, MappedFile, ResultExt, Utf8CStr, Utf8CStrBuf, WriteExt, - cstr, log_err, + BytesExt, EarlyExitExt, LoggedError, LoggedResult, MappedFile, ResultExt, Utf8CStr, + Utf8CStrBuf, WriteExt, cstr, error, log_err, }; use crate::check_env; @@ -227,7 +227,7 @@ impl Cpio { let hdr_sz = size_of::(); let hdr = from_bytes::(&data[pos..(pos + hdr_sz)]); if &hdr.magic != b"070701" { - return Err(log_err!("invalid cpio magic")); + return log_err!("invalid cpio magic"); } pos += hdr_sz; let name_sz = x8u(&hdr.namesize)? as usize; @@ -328,10 +328,10 @@ impl Cpio { } fn extract_entry(&self, path: &str, out: &mut String) -> LoggedResult<()> { - let entry = self - .entries - .get(path) - .ok_or_else(|| log_err!("No such file"))?; + let entry = self.entries.get(path).ok_or_else(|| { + error!("No such file"); + LoggedError::default() + })?; eprintln!("Extracting entry [{path}] to [{out}]"); let out = Utf8CStr::from_string(out); @@ -362,7 +362,7 @@ impl Cpio { unsafe { mknod(out.as_ptr().cast(), entry.mode, dev) }; } _ => { - return Err(log_err!("unknown entry type")); + return log_err!("unknown entry type"); } } Ok(()) @@ -389,7 +389,7 @@ impl Cpio { fn add(&mut self, mode: mode_t, path: &str, file: &mut String) -> LoggedResult<()> { if path.ends_with('/') { - return Err(log_err!("path cannot end with / for add")); + return log_err!("path cannot end with / for add"); } let file = Utf8CStr::from_string(file); let attr = file.get_attr()?; @@ -412,7 +412,7 @@ impl Cpio { } else if attr.is_char_device() { mode | S_IFCHR } else { - return Err(log_err!("unsupported file type")); + return log_err!("unsupported file type"); } }; @@ -462,10 +462,10 @@ impl Cpio { } fn mv(&mut self, from: &str, to: &str) -> LoggedResult<()> { - let entry = self - .entries - .remove(&norm_path(from)) - .ok_or_else(|| log_err!("no such entry {}", from))?; + let entry = self.entries.remove(&norm_path(from)).ok_or_else(|| { + error!("no such entry {}", from); + LoggedError::default() + })?; self.entries.insert(norm_path(to), entry); eprintln!("Move [{from}] -> [{to}]"); Ok(()) @@ -792,7 +792,7 @@ pub(crate) fn cpio_commands(file: &mut String, cmds: &mut Vec) -> Logged CpioAction::Add(Add { mode, path, file }) => cpio.add(*mode, path, file)?, CpioAction::Extract(Extract { paths }) => { if !paths.is_empty() && paths.len() != 2 { - Err(log_err!("invalid arguments"))?; + log_err!("invalid arguments")?; } let mut it = paths.iter_mut(); cpio.extract(it.next(), it.next())?; @@ -812,7 +812,11 @@ fn x8u(x: &[u8; 8]) -> LoggedResult { let mut ret = 0u32; let s = str::from_utf8(x).log_with_msg(|w| w.write_str("bad cpio header"))?; for c in s.chars() { - ret = ret * 16 + c.to_digit(16).ok_or_else(|| log_err!("bad cpio header"))?; + ret = ret * 16 + + c.to_digit(16).ok_or_else(|| { + error!("bad cpio header"); + LoggedError::default() + })?; } Ok(ret) } diff --git a/native/src/boot/sign.rs b/native/src/boot/sign.rs index 5fab4bcac..4dd1c03ab 100644 --- a/native/src/boot/sign.rs +++ b/native/src/boot/sign.rs @@ -25,7 +25,7 @@ use x509_cert::der::Any; use x509_cert::der::asn1::{OctetString, PrintableString}; use x509_cert::spki::AlgorithmIdentifier; -use base::{LoggedError, LoggedResult, MappedFile, ResultExt, Utf8CStr, cstr, log_err}; +use base::{LoggedResult, MappedFile, ResultExt, Utf8CStr, cstr, log_err}; use crate::ffi::BootImage; @@ -116,7 +116,7 @@ impl Verifier { digest = Box::::default(); VerifyingKey::SHA521withECDSA(ec) } else { - return Err(log_err!("Unsupported private key")); + return log_err!("Unsupported private key"); }; Ok(Verifier { digest, key }) } @@ -177,7 +177,7 @@ impl Signer { SigningKey::SHA521withECDSA(ec) } _ => { - return Err(log_err!("Unsupported private key")); + return log_err!("Unsupported private key"); } }, }, @@ -248,7 +248,7 @@ struct BootSignature { impl BootSignature { fn verify(self, payload: &[u8]) -> LoggedResult<()> { if self.authenticated_attributes.length as usize != payload.len() { - return Err(log_err!("Invalid image size")); + return log_err!("Invalid image size"); } let mut verifier = Verifier::from_public_key( self.certificate @@ -268,7 +268,7 @@ impl BootImage { pub fn verify(&self, cert: Option<&Utf8CStr>) -> LoggedResult<()> { let tail = self.tail(); if tail.starts_with(b"AVB0") { - return Err(LoggedError::default()); + return log_err!(); } // Don't use BootSignature::from_der because tail might have trailing zeros diff --git a/native/src/core/zygisk/daemon.rs b/native/src/core/zygisk/daemon.rs index 8e4eb4dcc..e21548eca 100644 --- a/native/src/core/zygisk/daemon.rs +++ b/native/src/core/zygisk/daemon.rs @@ -6,8 +6,8 @@ use crate::ffi::{ use crate::socket::{IpcRead, UnixSocketExt}; use base::libc::{O_CLOEXEC, O_CREAT, O_RDONLY, STDOUT_FILENO}; use base::{ - Directory, FsPathBuilder, LoggedError, LoggedResult, ResultExt, Utf8CStr, WriteExt, cstr, - fork_dont_care, libc, raw_cstr, warn, + Directory, FsPathBuilder, LoggedResult, ResultExt, Utf8CStr, WriteExt, cstr, fork_dont_care, + libc, log_err, raw_cstr, warn, }; use std::fmt::Write; use std::os::fd::{AsRawFd, FromRawFd, RawFd}; @@ -101,7 +101,7 @@ impl ZygiskState { local.send_fds(&module_fds)?; } if local.read_decodable::()? != 0 { - Err(LoggedError::default())?; + return log_err!(); } local }; diff --git a/native/src/sepolicy/cli.rs b/native/src/sepolicy/cli.rs index bf82b9cf2..65da5ef06 100644 --- a/native/src/sepolicy/cli.rs +++ b/native/src/sepolicy/cli.rs @@ -92,10 +92,10 @@ pub unsafe extern "C" fn main( (None, true, false) => SePolicy::from_split(), (None, false, true) => SePolicy::compile_split(), (None, false, false) => SePolicy::from_file(cstr!("/sys/fs/selinux/policy")), - _ => Err(log_err!("Multiple load source supplied"))?, + _ => log_err!("Multiple load source supplied")?, }; if sepol._impl.is_null() { - Err(log_err!("Cannot load policy"))?; + log_err!("Cannot load policy")?; } if cli.print_rules { @@ -105,7 +105,7 @@ pub unsafe extern "C" fn main( || cli.live || cli.save.is_some() { - Err(log_err!("Cannot print rules with other options"))?; + log_err!("Cannot print rules with other options")?; } sepol.print_rules(); return 0; @@ -124,12 +124,12 @@ pub unsafe extern "C" fn main( } if cli.live && !sepol.to_file(cstr!("/sys/fs/selinux/load")) { - Err(log_err!("Cannot apply policy"))?; + log_err!("Cannot apply policy")?; } if let Some(file) = &mut cli.save { if !sepol.to_file(Utf8CStr::from_string(file)) { - Err(log_err!("Cannot dump policy to {}", file))?; + log_err!("Cannot dump policy to {}", file)?; } } };