cloudfox-coreshift-core 2.32.0

Low-level Linux and Android systems primitives for CoreShift (CloudFox)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/

//! NDK binder primitives for querying Android system services.
//!
//! Uses `dlopen` on `libbinder_ndk.so` to avoid hard-linking against a library
//! absent from older NDK toolchains or non-Android targets.
//!
//! ## Transaction code resolution
//!
//! Transaction codes are resolved fresh from `framework.jar` DEX on each
//! startup; no persistent cache.
//!
//! ## Observer mode
//!
//! `ActivityManager::open_with_observer` registers this process as an
//! `IProcessObserver` with ActivityManager. Callbacks fire when foreground
//! activities change and signal an `eventfd` that callers can poll via epoll.
//! After the eventfd fires, call `get_focused_package` to read the new value.

// ─────────────────────────────────────────────────────────────────────────────
// Host-runnable wire model (compiled on Android — where the handoff write path
// runs — and in `cargo test` on host, so the bundle byte math is exercised on
// every host commit; finding 28).
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(any(target_os = "android", test))]
mod wire;

// Host-runnable parcel allocators (String16 / byte[]) compiled on Android —
// where `sys` uses them — and in `cargo test` on host, so the AOSP allocator
// length convention is verified on every host commit (D1, Phase 1).
#[cfg(any(target_os = "android", test))]
mod alloc;

#[cfg(any(target_os = "android", test))]
mod display_tail;

// Host-runnable probe decoders (the audio-active decode is exercised on every
// host commit; the Android wrapper that opens the service is android-only).
#[cfg(any(target_os = "android", test))]
mod probe;

// ─────────────────────────────────────────────────────────────────────────────
// Android-only implementation
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(target_os = "android")]
mod am;
#[cfg(target_os = "android")]
mod display;
#[cfg(target_os = "android")]
mod fps;
#[cfg(target_os = "android")]
mod handoff;
#[cfg(target_os = "android")]
mod raw;
#[cfg(target_os = "android")]
mod serve;
#[cfg(target_os = "android")]
mod sys;
#[cfg(target_os = "android")]
mod task_stack;

// ── Public re-exports ─────────────────────────────────────────────────────────

#[cfg(target_os = "android")]
pub use am::{ActivityManager, TxCodes, last_foreground_pid, last_uid_event, resolve_tx_codes};
#[cfg(target_os = "android")]
pub use display::{
    DISPLAY_EVENT_FLAG_ADDED, DISPLAY_EVENT_FLAG_CHANGED, DISPLAY_EVENT_FLAG_REFRESH_RATE,
    DISPLAY_EVENT_FLAG_REMOVED, DISPLAY_EVENT_FLAG_STATE, DISPLAY_STATE_DOZE,
    DISPLAY_STATE_DOZE_SUSPEND, DISPLAY_STATE_OFF, DISPLAY_STATE_ON, DISPLAY_STATE_ON_SUSPEND,
    DISPLAY_STATE_UNKNOWN, DISPLAY_STATE_VR, DisplayInfo, DisplayManager,
};
#[cfg(target_os = "android")]
pub use fps::FpsListener;
#[cfg(target_os = "android")]
pub use handoff::{Handoff, ProviderHandle};
#[cfg(target_os = "android")]
pub use probe::audio_has_active_playback;
#[cfg(target_os = "android")]
pub use raw::RawBinderService;
#[cfg(target_os = "android")]
pub use serve::{DeathRecipient, OnewaySender, ServeCall, ServeHandler, ServingBinder};
#[cfg(target_os = "android")]
pub use sys::{OwnedBinder, ParcelReader, ParcelWriter};
#[cfg(target_os = "android")]
pub use task_stack::TaskStackListener;

// ── Non-Android stubs ─────────────────────────────────────────────────────────

#[cfg(not(target_os = "android"))]
pub struct ActivityManager;

#[cfg(not(target_os = "android"))]
impl ActivityManager {
    pub fn open() -> Result<Self, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn open_with_observer() -> Result<(Self, std::os::fd::OwnedFd), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn open_with_fgproc_observer() -> Result<(Self, std::os::fd::OwnedFd), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn get_focused_task(&self) -> Result<Option<(i32, Option<String>)>, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn get_focused_package(&self) -> Result<Option<String>, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn get_focused_task_id(&self) -> Result<Option<i32>, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
}

#[cfg(not(target_os = "android"))]
pub struct DisplayManager;

#[cfg(not(target_os = "android"))]
impl DisplayManager {
    pub fn open_with_callback() -> Result<(Self, crate::fd::Fd), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn open_with_event_mask(_mask: u64) -> Result<(Self, crate::fd::Fd), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn is_interactive(&self) -> Result<bool, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn info(&self, _display_id: i32) -> Result<DisplayInfo, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
}

#[cfg(not(target_os = "android"))]
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct DisplayInfo {
    pub active_mode_id: i32,
    pub vsync_rate: f32,
    pub peak_refresh_rate: f32,
    pub render_frame_rate: f32,
    pub state: i32,
    pub is_interactive: bool,
}

#[cfg(not(target_os = "android"))]
pub struct RawBinderService;

#[cfg(not(target_os = "android"))]
impl RawBinderService {
    pub fn open(_service_name: &str, _descriptor: &str) -> Result<Self, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn transact_bool(&self, _code: u32) -> Result<bool, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn transact_i32(&self, _code: u32, _arg: i32) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn transact_bool_arg(&self, _code: u32, _value: bool) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
}

#[cfg(not(target_os = "android"))]
pub struct FpsListener;

#[cfg(not(target_os = "android"))]
impl FpsListener {
    pub fn open() -> Result<(Self, std::os::fd::OwnedFd), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn register(&mut self, _task_id: i32) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn unregister(&mut self) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn last_fps(&self) -> Option<f32> {
        None
    }
    pub fn task_id(&self) -> Option<i32> {
        None
    }
}

#[cfg(not(target_os = "android"))]
pub struct TaskStackListener;

#[cfg(not(target_os = "android"))]
impl TaskStackListener {
    pub fn open() -> Result<(Self, std::os::fd::OwnedFd), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn register(&self) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn unregister(&self) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
}

#[cfg(not(target_os = "android"))]
pub struct Handoff;

#[cfg(not(target_os = "android"))]
impl Handoff {
    pub fn open() -> Result<Self, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn acquire_provider(
        &self,
        _authority: &str,
        _user_id: i32,
    ) -> Result<ProviderHandle, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn send_binder(
        &self,
        _handle: &ProviderHandle,
        _authority: &str,
        _method: &str,
        _arg: &str,
        _service_binder: *mut std::os::raw::c_void,
    ) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn remove_provider(&self, _authority: &str, _user_id: i32) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn handoff(
        &self,
        _authority: &str,
        _user_id: i32,
        _method: &str,
        _arg: &str,
        _service_binder: *mut std::os::raw::c_void,
    ) -> Result<i32, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
}

#[cfg(not(target_os = "android"))]
pub struct ProviderHandle {
    pub uid: i32,
    pub no_release_needed: bool,
}

#[cfg(not(target_os = "android"))]
pub struct TxCodes {
    pub observer_code: u32,
    pub query_code: u32,
    pub api_mode: u8,
    pub fg_code: u32,
}

#[cfg(not(target_os = "android"))]
pub fn resolve_tx_codes() -> Result<TxCodes, crate::CoreError> {
    Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
}

#[cfg(not(target_os = "android"))]
pub fn last_foreground_pid() -> i32 {
    0
}

// Stub parcel cursors so handlers can be written once and type-checked on
// non-Android builds. They can never be constructed or called there; every
// method returns an unsupported-platform error.
#[cfg(not(target_os = "android"))]
pub struct ParcelReader<'a> {
    _marker: std::marker::PhantomData<&'a ()>,
}

#[cfg(not(target_os = "android"))]
impl<'a> ParcelReader<'a> {
    pub fn read_i32(&self) -> Result<i32, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn read_string(&self) -> Result<Option<String>, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn read_bytes(&self) -> Result<Option<Vec<u8>>, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn read_strong_binder(&self) -> Result<Option<OwnedBinder>, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn read_float(&self) -> Result<f32, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn read_int64(&self) -> Result<i64, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn read_bool(&self) -> Result<bool, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
}

#[cfg(not(target_os = "android"))]
pub struct ParcelWriter<'a> {
    _marker: std::marker::PhantomData<&'a ()>,
}

#[cfg(not(target_os = "android"))]
impl<'a> ParcelWriter<'a> {
    pub fn write_i32(&self, _v: i32) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn write_strong_binder(
        &self,
        _b: *mut std::os::raw::c_void,
    ) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn write_string(&self, _s: Option<&str>) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn write_bytes(&self, _b: Option<&[u8]>) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
}

#[cfg(not(target_os = "android"))]
pub struct OwnedBinder;

#[cfg(not(target_os = "android"))]
impl OwnedBinder {
    pub fn as_raw(&self) -> *mut std::os::raw::c_void {
        std::ptr::null_mut()
    }
}

#[cfg(not(target_os = "android"))]
pub struct ServeCall<'a> {
    pub code: u32,
    pub calling_uid: u32,
    pub calling_pid: i32,
    pub request: ParcelReader<'a>,
    pub reply: Option<ParcelWriter<'a>>,
}

#[cfg(not(target_os = "android"))]
pub type ServeHandler =
    Box<dyn for<'a> FnMut(ServeCall<'a>) -> Result<(), crate::CoreError> + Send>;

#[cfg(not(target_os = "android"))]
pub struct ServingBinder;

#[cfg(not(target_os = "android"))]
impl ServingBinder {
    pub fn open(_descriptor: &[u8], _handler: ServeHandler) -> Result<Self, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn open_bounded(
        _descriptor: &[u8],
        _handler: ServeHandler,
        _max_inflight: usize,
    ) -> Result<Self, crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn as_raw(&self) -> *mut std::os::raw::c_void {
        std::ptr::null_mut()
    }
    pub fn associate(&self, _target: &OwnedBinder) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn push_oneway(
        &self,
        _target: &OwnedBinder,
        _code: u32,
        _writes: impl FnOnce(&ParcelWriter<'_>) -> Result<(), crate::CoreError>,
    ) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn death_recipient(&self, _on_died: Box<dyn FnMut() + Send>) -> DeathRecipient {
        DeathRecipient {
            _cb: Box::new(Box::new(|| {})),
        }
    }
    pub fn oneway_sender(&self) -> OnewaySender {
        OnewaySender
    }
}

#[cfg(not(target_os = "android"))]
#[derive(Clone, Copy)]
pub struct OnewaySender;

#[cfg(not(target_os = "android"))]
impl OnewaySender {
    pub fn push_oneway(
        &self,
        _target: &OwnedBinder,
        _code: u32,
        _writes: impl FnOnce(&ParcelWriter<'_>) -> Result<(), crate::CoreError>,
    ) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
}

#[cfg(not(target_os = "android"))]
pub struct DeathRecipient {
    _cb: Box<Box<dyn FnMut() + Send>>,
}

#[cfg(not(target_os = "android"))]
impl DeathRecipient {
    pub fn link(&self, _target: &OwnedBinder) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
    pub fn unlink(&self, _target: &OwnedBinder) -> Result<(), crate::CoreError> {
        Err(crate::CoreError::binder(-1, "binder:unsupported platform"))
    }
}