Skip to content

Commit

Permalink
IBinder sample
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Mar 19, 2024
1 parent 8167b53 commit de58966
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions ndk/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ impl IBinder {
pub fn class(&self) -> Option<&IBinderClass> {
let class = unsafe { ffi::AIBinder_getClass(self.ptr.as_ptr()) };

dbg!(class);
todo!()
// NonNull::new(class).map(IBinderClass::from_ptr)
}
Expand Down
5 changes: 3 additions & 2 deletions sample/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ edition = "2021"

[dependencies]
libloading = "0.8.3"
ndk = { path = "../ndk", all-features = true }
ndk-sys = { path = "../ndk-sys", all-features = true }
ndk = { path = "../ndk", features = ["binder", "api-level-29"] }
ndk-sys = { path = "../ndk-sys" }
rustix = { version = "0.38", features = ["pipe"] }
51 changes: 39 additions & 12 deletions sample/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use std::ffi::{c_char, CString};
use std::{
ffi::{c_char, CString},
ptr::NonNull,
};

#[repr(C)]
struct AIBinder {}
use ndk::binder::IBinder;
use ndk_sys::AIBinder;
use rustix::fd::AsFd;

#[link(name = "binder_ndk")]
extern "C" {
fn AIBinder_isAlive() -> bool;
// Loaded at runtime because it cannot be linked dynamically through the NDK as the bindings are only in LL-NDK:
// https://cs.android.com/android/platform/superproject/main/+/main:frameworks/native/libs/binder/ndk/libbinder_ndk.map.txt;l=96;drc=a7aa4ce8e2471f00f41c0f46495c76af478ddb06
// fn AServiceManager_checkService(instance: *const c_char) -> *mut AIBinder;
}
type AServiceManager_checkService = extern "system" fn(instance: *const c_char) -> *mut AIBinder;

fn main() {
Expand All @@ -19,7 +16,7 @@ fn main() {
.unwrap();

let names = [
"power",
// "power",
"power.stats-vendor",
"powerstats", // https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/core/java/com/android/server/powerstats/PowerStatsService.java, https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/core/java/com/android/server/powerstats/PowerStatsService.java;l=295;drc=15978b577cc30ad2c5b8f5e9d1cd5986ea7f09ae;bpv=1;bpt=1
"android.hardware.power.stats.IPowerStats/default", // https://android.googlesource.com/device/google/pantah/+/3c30a32/powerstats/panther/service.cpp#88
Expand All @@ -32,6 +29,36 @@ fn main() {
let name = CString::new(name).unwrap();
// let service = unsafe { AServiceManager_checkService(name.as_ptr()) };
let service = service_manager_check_service(name.as_ptr());
dbg!(name, service);
let Some(binder) = NonNull::new(service) else {
continue;
};
let binder = unsafe { IBinder::from_ptr(binder) };
// dbg!(binder.class());
dbg!(&name, &binder);
dbg!(binder.is_alive());
dbg!(binder.is_remote());

let (r, w) = rustix::pipe::pipe().unwrap();

let dump = std::thread::spawn(move || {
let mut dump = String::new();
let mut data = vec![0u8; 1024];
loop {
let len = rustix::io::read(&r, &mut data).unwrap();
if len > 0 {
let data = &data[..len];
dump.push_str(std::str::from_utf8(data).unwrap());
} else {
break dump;
}
}
});

if let Err(e) = binder.dump(w.as_fd(), &[]) {
eprintln!("Failed to dump {name:?}: {e:?}")
} else {
drop(w);
println!("{}", dump.join().unwrap());
}
}
}

0 comments on commit de58966

Please sign in to comment.