Skip to content

Commit 4de028d

Browse files
authored
Merge pull request #367 from wedsonaf/cleanup-object
Small refactor in binder.
2 parents 5bb76a6 + 8df748e commit 4de028d

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

drivers/android/allocation.rs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -112,33 +112,6 @@ impl<'a> Allocation<'a> {
112112
pub(crate) fn set_info(&mut self, info: AllocationInfo) {
113113
self.allocation_info = Some(info);
114114
}
115-
116-
fn cleanup_object(&self, index_offset: usize, view: &AllocationView) -> Result {
117-
let offset = self.read(index_offset)?;
118-
let header = view.read::<bindings::binder_object_header>(offset)?;
119-
// TODO: Handle other types.
120-
match header.type_ {
121-
BINDER_TYPE_WEAK_BINDER | BINDER_TYPE_BINDER => {
122-
let obj = view.read::<bindings::flat_binder_object>(offset)?;
123-
let strong = header.type_ == BINDER_TYPE_BINDER;
124-
// SAFETY: The type is `BINDER_TYPE_{WEAK_}BINDER`, so the `binder` field is
125-
// populated.
126-
let ptr = unsafe { obj.__bindgen_anon_1.binder } as usize;
127-
let cookie = obj.cookie as usize;
128-
self.process.update_node(ptr, cookie, strong, false);
129-
Ok(())
130-
}
131-
BINDER_TYPE_WEAK_HANDLE | BINDER_TYPE_HANDLE => {
132-
let obj = view.read::<bindings::flat_binder_object>(offset)?;
133-
let strong = header.type_ == BINDER_TYPE_HANDLE;
134-
// SAFETY: The type is `BINDER_TYPE_{WEAK_}HANDLE`, so the `handle` field is
135-
// populated.
136-
let handle = unsafe { obj.__bindgen_anon_1.handle } as _;
137-
self.process.update_ref(handle, false, strong)
138-
}
139-
_ => Ok(()),
140-
}
141-
}
142115
}
143116

144117
impl Drop for Allocation<'_> {
@@ -148,9 +121,10 @@ impl Drop for Allocation<'_> {
148121
}
149122

150123
if let Some(info) = &self.allocation_info {
151-
let view = AllocationView::new(self, info.offsets.start);
152-
for i in info.offsets.clone().step_by(size_of::<usize>()) {
153-
if self.cleanup_object(i, &view).is_err() {
124+
let offsets = info.offsets.clone();
125+
let view = AllocationView::new(self, offsets.start);
126+
for i in offsets.step_by(size_of::<usize>()) {
127+
if view.cleanup_object(i).is_err() {
154128
pr_warn!("Error cleaning up object at offset {}\n", i)
155129
}
156130
}
@@ -160,13 +134,13 @@ impl Drop for Allocation<'_> {
160134
}
161135
}
162136

163-
pub(crate) struct AllocationView<'a> {
164-
alloc: &'a Allocation<'a>,
137+
pub(crate) struct AllocationView<'a, 'b> {
138+
pub(crate) alloc: &'a mut Allocation<'b>,
165139
limit: usize,
166140
}
167141

168-
impl<'a> AllocationView<'a> {
169-
pub(crate) fn new(alloc: &'a Allocation, limit: usize) -> Self {
142+
impl<'a, 'b> AllocationView<'a, 'b> {
143+
pub(crate) fn new(alloc: &'a mut Allocation<'b>, limit: usize) -> Self {
170144
AllocationView { alloc, limit }
171145
}
172146

@@ -250,4 +224,31 @@ impl<'a> AllocationView<'a> {
250224
}
251225
Ok(())
252226
}
227+
228+
fn cleanup_object(&self, index_offset: usize) -> Result {
229+
let offset = self.alloc.read(index_offset)?;
230+
let header = self.read::<bindings::binder_object_header>(offset)?;
231+
// TODO: Handle other types.
232+
match header.type_ {
233+
BINDER_TYPE_WEAK_BINDER | BINDER_TYPE_BINDER => {
234+
let obj = self.read::<bindings::flat_binder_object>(offset)?;
235+
let strong = header.type_ == BINDER_TYPE_BINDER;
236+
// SAFETY: The type is `BINDER_TYPE_{WEAK_}BINDER`, so the `binder` field is
237+
// populated.
238+
let ptr = unsafe { obj.__bindgen_anon_1.binder } as usize;
239+
let cookie = obj.cookie as usize;
240+
self.alloc.process.update_node(ptr, cookie, strong, false);
241+
Ok(())
242+
}
243+
BINDER_TYPE_WEAK_HANDLE | BINDER_TYPE_HANDLE => {
244+
let obj = self.read::<bindings::flat_binder_object>(offset)?;
245+
let strong = header.type_ == BINDER_TYPE_HANDLE;
246+
// SAFETY: The type is `BINDER_TYPE_{WEAK_}HANDLE`, so the `handle` field is
247+
// populated.
248+
let handle = unsafe { obj.__bindgen_anon_1.handle } as _;
249+
self.alloc.process.update_ref(handle, false, strong)
250+
}
251+
_ => Ok(()),
252+
}
253+
}
253254
}

drivers/android/thread.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,10 @@ impl Thread {
376376
fn translate_object(
377377
&self,
378378
index_offset: usize,
379-
alloc: &Allocation,
380379
view: &AllocationView,
381380
allow_fds: bool,
382381
) -> BinderResult {
383-
let offset = alloc.read(index_offset)?;
382+
let offset = view.alloc.read(index_offset)?;
384383
let header = view.read::<bindings::binder_object_header>(offset)?;
385384
// TODO: Handle other types.
386385
match header.type_ {
@@ -421,9 +420,9 @@ impl Thread {
421420
end: usize,
422421
allow_fds: bool,
423422
) -> BinderResult {
424-
let view = AllocationView::new(&alloc, start);
423+
let view = AllocationView::new(alloc, start);
425424
for i in (start..end).step_by(size_of::<usize>()) {
426-
if let Err(err) = self.translate_object(i, alloc, &view, allow_fds) {
425+
if let Err(err) = self.translate_object(i, &view, allow_fds) {
427426
alloc.set_info(AllocationInfo { offsets: start..i });
428427
return Err(err);
429428
}

0 commit comments

Comments
 (0)