Create custom cxx binding to Utf8CStr

This commit is contained in:
topjohnwu
2023-12-26 23:08:06 +08:00
parent 062e498bdd
commit 65207f96c8
18 changed files with 142 additions and 68 deletions

View File

@@ -9,7 +9,7 @@ use base::{
Utf8CStrBufArr, Utf8CStrBufRef, WalkResult,
};
use crate::ffi::{CxxMagiskD, RequestCode};
use crate::ffi::{get_magisk_tmp, CxxMagiskD, RequestCode};
use crate::logging::magisk_logging;
use crate::{get_prop, MAIN_CONFIG};
@@ -97,10 +97,6 @@ impl MagiskD {
}
}
pub fn get_magisk_tmp() -> &'static Utf8CStr {
unsafe { Utf8CStr::from_ptr(super::ffi::get_magisk_tmp()).unwrap_unchecked() }
}
pub fn daemon_entry() {
let mut qemu = get_prop(cstr!("ro.kernel.qemu"), false);
if qemu.is_empty() {
@@ -170,13 +166,9 @@ pub fn get_magiskd() -> &'static MagiskD {
unsafe { MAGISKD.get().unwrap_unchecked() }
}
pub fn find_apk_path(pkg: &[u8], data: &mut [u8]) -> usize {
pub fn find_apk_path(pkg: &Utf8CStr, data: &mut [u8]) -> usize {
use WalkResult::*;
fn inner(pkg: &[u8], buf: &mut dyn Utf8CStrBuf) -> io::Result<usize> {
let pkg = match Utf8CStr::from_bytes(pkg) {
Ok(pkg) => pkg,
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
};
fn inner(pkg: &Utf8CStr, buf: &mut dyn Utf8CStrBuf) -> io::Result<usize> {
Directory::open(cstr!("/data/app"))?.pre_order_walk(|e| {
if !e.is_dir() {
return Ok(Skip);

View File

@@ -1,5 +1,7 @@
#pragma once
#include <base.hpp>
namespace rust {
struct MagiskD;
}
@@ -24,3 +26,4 @@ private:
};
const char *get_magisk_tmp();
static inline rust::Utf8CStr get_magisk_tmp_rs() { return get_magisk_tmp(); }

View File

@@ -58,9 +58,14 @@ pub mod ffi {
}
unsafe extern "C++" {
#[namespace = "rust"]
#[cxx_name = "Utf8CStr"]
type Utf8CStrRef<'a> = base::ffi::Utf8CStrRef<'a>;
include!("include/daemon.hpp");
fn get_magisk_tmp() -> *const c_char;
#[cxx_name = "get_magisk_tmp_rs"]
fn get_magisk_tmp() -> Utf8CStrRef<'static>;
#[cxx_name = "MagiskD"]
type CxxMagiskD;
@@ -76,12 +81,12 @@ pub mod ffi {
fn zygisk_logging();
fn zygisk_close_logd();
fn zygisk_get_logd() -> i32;
fn find_apk_path(pkg: &[u8], data: &mut [u8]) -> usize;
fn find_apk_path(pkg: Utf8CStrRef, data: &mut [u8]) -> usize;
fn read_certificate(fd: i32, version: i32) -> Vec<u8>;
unsafe fn persist_get_prop(name: *const c_char, prop_cb: Pin<&mut PropCb>);
unsafe fn persist_get_prop(name: Utf8CStrRef, prop_cb: Pin<&mut PropCb>);
unsafe fn persist_get_props(prop_cb: Pin<&mut PropCb>);
unsafe fn persist_delete_prop(name: *const c_char) -> bool;
unsafe fn persist_set_prop(name: *const c_char, value: *const c_char) -> bool;
unsafe fn persist_delete_prop(name: Utf8CStrRef) -> bool;
unsafe fn persist_set_prop(name: Utf8CStrRef, value: Utf8CStrRef) -> bool;
}
#[namespace = "rust"]

View File

@@ -19,7 +19,8 @@ use base::libc::{
};
use base::*;
use crate::daemon::{get_magisk_tmp, MagiskD, MAGISKD};
use crate::daemon::{MagiskD, MAGISKD};
use crate::ffi::get_magisk_tmp;
use crate::logging::LogFile::{Actual, Buffer};
use crate::{LOGFILE, LOG_PIPE};

View File

@@ -176,7 +176,7 @@ int get_manager(int user_id, string *pkg, bool install) {
int app_id = to_app_id(st.st_uid);
byte_array<PATH_MAX> apk;
find_apk_path(byte_view(str[SU_MANAGER]), apk);
find_apk_path(str[SU_MANAGER], apk);
int fd = xopen((const char *) apk.buf(), O_RDONLY | O_CLOEXEC);
auto cert = read_certificate(fd, -1);
close(fd);
@@ -230,7 +230,7 @@ int get_manager(int user_id, string *pkg, bool install) {
if (stat(app_path, &st) == 0) {
#if ENFORCE_SIGNATURE
byte_array<PATH_MAX> apk;
find_apk_path(byte_view(JAVA_PACKAGE_NAME), apk);
find_apk_path(JAVA_PACKAGE_NAME, apk);
int fd = xopen((const char *) apk.buf(), O_RDONLY | O_CLOEXEC);
auto cert = read_certificate(fd, MAGISK_VER_CODE);
close(fd);

View File

@@ -1,4 +1,3 @@
use core::ffi::c_char;
use std::io::Read;
use std::{
fs::File,
@@ -142,9 +141,8 @@ fn proto_write_props(props: &PersistentProperties) -> LoggedResult<()> {
Ok(())
}
pub unsafe fn persist_get_prop(name: *const c_char, prop_cb: Pin<&mut PropCb>) {
fn inner(name: *const c_char, mut prop_cb: Pin<&mut PropCb>) -> LoggedResult<()> {
let name = unsafe { Utf8CStr::from_ptr(name)? };
pub unsafe fn persist_get_prop(name: &Utf8CStr, prop_cb: Pin<&mut PropCb>) {
fn inner(name: &Utf8CStr, mut prop_cb: Pin<&mut PropCb>) -> LoggedResult<()> {
if check_proto() {
let mut props = proto_read_props()?;
let prop = props.find(name)?;
@@ -197,9 +195,8 @@ pub unsafe fn persist_get_props(prop_cb: Pin<&mut PropCb>) {
inner(prop_cb).ok();
}
pub unsafe fn persist_delete_prop(name: *const c_char) -> bool {
fn inner(name: *const c_char) -> LoggedResult<()> {
let name = unsafe { Utf8CStr::from_ptr(name)? };
pub unsafe fn persist_delete_prop(name: &Utf8CStr) -> bool {
fn inner(name: &Utf8CStr) -> LoggedResult<()> {
if check_proto() {
let mut props = proto_read_props()?;
let idx = props.find_index(name).no_log()?;
@@ -212,10 +209,8 @@ pub unsafe fn persist_delete_prop(name: *const c_char) -> bool {
inner(name).is_ok()
}
pub unsafe fn persist_set_prop(name: *const c_char, value: *const c_char) -> bool {
unsafe fn inner(name: *const c_char, value: *const c_char) -> LoggedResult<()> {
let name = Utf8CStr::from_ptr(name)?;
let value = Utf8CStr::from_ptr(value)?;
pub unsafe fn persist_set_prop(name: &Utf8CStr, value: &Utf8CStr) -> bool {
unsafe fn inner(name: &Utf8CStr, value: &Utf8CStr) -> LoggedResult<()> {
if check_proto() {
let mut props = proto_read_props()?;
match props.find_index(name) {