From f78f590dca8de4944bc06349a935a82c0e44747d Mon Sep 17 00:00:00 2001 From: Wedson Almeida Filho Date: Sat, 29 May 2021 19:03:38 +0100 Subject: [PATCH 1/2] binder: Move `cleanup_object` from `Allocation` to `AllocationView`. Signed-off-by: Wedson Almeida Filho --- drivers/android/allocation.rs | 56 +++++++++++++++++------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/drivers/android/allocation.rs b/drivers/android/allocation.rs index 61ce23d7218046..6b336b57bd8661 100644 --- a/drivers/android/allocation.rs +++ b/drivers/android/allocation.rs @@ -112,33 +112,6 @@ impl<'a> Allocation<'a> { pub(crate) fn set_info(&mut self, info: AllocationInfo) { self.allocation_info = Some(info); } - - fn cleanup_object(&self, index_offset: usize, view: &AllocationView) -> Result { - let offset = self.read(index_offset)?; - let header = view.read::(offset)?; - // TODO: Handle other types. - match header.type_ { - BINDER_TYPE_WEAK_BINDER | BINDER_TYPE_BINDER => { - let obj = view.read::(offset)?; - let strong = header.type_ == BINDER_TYPE_BINDER; - // SAFETY: The type is `BINDER_TYPE_{WEAK_}BINDER`, so the `binder` field is - // populated. - let ptr = unsafe { obj.__bindgen_anon_1.binder } as usize; - let cookie = obj.cookie as usize; - self.process.update_node(ptr, cookie, strong, false); - Ok(()) - } - BINDER_TYPE_WEAK_HANDLE | BINDER_TYPE_HANDLE => { - let obj = view.read::(offset)?; - let strong = header.type_ == BINDER_TYPE_HANDLE; - // SAFETY: The type is `BINDER_TYPE_{WEAK_}HANDLE`, so the `handle` field is - // populated. - let handle = unsafe { obj.__bindgen_anon_1.handle } as _; - self.process.update_ref(handle, false, strong) - } - _ => Ok(()), - } - } } impl Drop for Allocation<'_> { @@ -150,7 +123,7 @@ impl Drop for Allocation<'_> { if let Some(info) = &self.allocation_info { let view = AllocationView::new(self, info.offsets.start); for i in info.offsets.clone().step_by(size_of::()) { - if self.cleanup_object(i, &view).is_err() { + if view.cleanup_object(i).is_err() { pr_warn!("Error cleaning up object at offset {}\n", i) } } @@ -250,4 +223,31 @@ impl<'a> AllocationView<'a> { } Ok(()) } + + fn cleanup_object(&self, index_offset: usize) -> Result { + let offset = self.alloc.read(index_offset)?; + let header = self.read::(offset)?; + // TODO: Handle other types. + match header.type_ { + BINDER_TYPE_WEAK_BINDER | BINDER_TYPE_BINDER => { + let obj = self.read::(offset)?; + let strong = header.type_ == BINDER_TYPE_BINDER; + // SAFETY: The type is `BINDER_TYPE_{WEAK_}BINDER`, so the `binder` field is + // populated. + let ptr = unsafe { obj.__bindgen_anon_1.binder } as usize; + let cookie = obj.cookie as usize; + self.alloc.process.update_node(ptr, cookie, strong, false); + Ok(()) + } + BINDER_TYPE_WEAK_HANDLE | BINDER_TYPE_HANDLE => { + let obj = self.read::(offset)?; + let strong = header.type_ == BINDER_TYPE_HANDLE; + // SAFETY: The type is `BINDER_TYPE_{WEAK_}HANDLE`, so the `handle` field is + // populated. + let handle = unsafe { obj.__bindgen_anon_1.handle } as _; + self.alloc.process.update_ref(handle, false, strong) + } + _ => Ok(()), + } + } } From 8df748eb3b6699212202ee69e9bf810dfd3fa4db Mon Sep 17 00:00:00 2001 From: Wedson Almeida Filho Date: Sat, 29 May 2021 19:20:07 +0100 Subject: [PATCH 2/2] binder: No need to pass `alloc` explicitly to `translate_object`. Signed-off-by: Wedson Almeida Filho --- drivers/android/allocation.rs | 13 +++++++------ drivers/android/thread.rs | 7 +++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/drivers/android/allocation.rs b/drivers/android/allocation.rs index 6b336b57bd8661..3e10ae4be6687e 100644 --- a/drivers/android/allocation.rs +++ b/drivers/android/allocation.rs @@ -121,8 +121,9 @@ impl Drop for Allocation<'_> { } if let Some(info) = &self.allocation_info { - let view = AllocationView::new(self, info.offsets.start); - for i in info.offsets.clone().step_by(size_of::()) { + let offsets = info.offsets.clone(); + let view = AllocationView::new(self, offsets.start); + for i in offsets.step_by(size_of::()) { if view.cleanup_object(i).is_err() { pr_warn!("Error cleaning up object at offset {}\n", i) } @@ -133,13 +134,13 @@ impl Drop for Allocation<'_> { } } -pub(crate) struct AllocationView<'a> { - alloc: &'a Allocation<'a>, +pub(crate) struct AllocationView<'a, 'b> { + pub(crate) alloc: &'a mut Allocation<'b>, limit: usize, } -impl<'a> AllocationView<'a> { - pub(crate) fn new(alloc: &'a Allocation, limit: usize) -> Self { +impl<'a, 'b> AllocationView<'a, 'b> { + pub(crate) fn new(alloc: &'a mut Allocation<'b>, limit: usize) -> Self { AllocationView { alloc, limit } } diff --git a/drivers/android/thread.rs b/drivers/android/thread.rs index 9a1376c8d48caf..94c2d70fa33ba4 100644 --- a/drivers/android/thread.rs +++ b/drivers/android/thread.rs @@ -376,11 +376,10 @@ impl Thread { fn translate_object( &self, index_offset: usize, - alloc: &Allocation, view: &AllocationView, allow_fds: bool, ) -> BinderResult { - let offset = alloc.read(index_offset)?; + let offset = view.alloc.read(index_offset)?; let header = view.read::(offset)?; // TODO: Handle other types. match header.type_ { @@ -421,9 +420,9 @@ impl Thread { end: usize, allow_fds: bool, ) -> BinderResult { - let view = AllocationView::new(&alloc, start); + let view = AllocationView::new(alloc, start); for i in (start..end).step_by(size_of::()) { - if let Err(err) = self.translate_object(i, alloc, &view, allow_fds) { + if let Err(err) = self.translate_object(i, &view, allow_fds) { alloc.set_info(AllocationInfo { offsets: start..i }); return Err(err); }