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
// Copyright 2026 Jeff Kim <hiking90@gmail.com>
// SPDX-License-Identifier: Apache-2.0
//! Shared-memory IPC trait skeleton.
//!
//! AOSP `IMemory` / `IMemoryHeap`
//! ([`IMemory.h`](https://cs.android.com/android/platform/superproject/+/android-16.0.0_r4:frameworks/native/libs/binder/include/binder/IMemory.h))
//! are **handwritten** C++ binders, not AIDL. The Rust traits in this
//! module mirror that surface for in-process use. Not yet implemented,
//! left as future work:
//!
//! * A `memfd_create(2)` + `F_ADD_SEALS` backed `MemoryHeapBase`
//! (Linux/Android only). Receiver-side `F_GET_SEALS` verification is
//! mandatory for the read-only PROT negotiation.
//! * A `MemoryDealer` chunk allocator on top of a heap.
//! * Hermetic verification that two processes can mmap the same page
//! through `ParcelFileDescriptor` transfer.
//!
//! Interop with real `IMemory`/`IMemoryHeap` peers is out of scope — it
//! would require manual `Bn`/`Bp` matching AOSP's handwritten transaction
//! codes (`HEAP_ID_TRANSACTION` etc.).
//!
//! The macOS host build compiles every trait declaration in this module
//! but the concrete `MemoryHeapBase` stub returns
//! `Err(StatusCode::InvalidOperation)` from every operation — there is no
//! `memfd_create` equivalent on darwin. The Linux/Android impl, once
//! added, is gated on `cfg(any(target_os = "linux", target_os = "android"))`.
use crate;
/// AOSP `IMemoryHeap::READ_ONLY` flag
/// ([IMemory.h:37-39](https://cs.android.com/android/platform/superproject/+/android-16.0.0_r4:frameworks/native/libs/binder/include/binder/IMemory.h;l=37)).
/// Receivers MUST honor this by mapping `PROT_READ` only and rejecting
/// any subsequent `mprotect(PROT_WRITE)`. The PROT negotiation also
/// requires the sender to enforce `F_SEAL_WRITE` on the underlying
/// `memfd` so the kernel rejects a mismatched mmap from a peer that
/// ignores this flag.
pub const FLAG_READ_ONLY: u32 = 0x0000_0001;
/// Server-side representation of a heap. AOSP `IMemoryHeap` is keyed by
/// the heap fd; this trait deliberately exposes the fd as a borrowed
/// raw fd (`i32`) rather than an owned [`std::os::fd::OwnedFd`] so the
/// transaction marshalling can dup the fd into a
/// [`crate::ParcelFileDescriptor`] without taking ownership away from
/// the heap object.
///
/// All methods return `&` borrows (heap geometry is immutable for the
/// lifetime of the heap); the size and offset are captured at heap
/// construction time and never mutate. Mutation surface is intentionally
/// absent — heap resize is not in AOSP `IMemoryHeap` either
/// ([IMemory.h:41-45](https://cs.android.com/android/platform/superproject/+/android-16.0.0_r4:frameworks/native/libs/binder/include/binder/IMemory.h;l=41)).
/// Sub-region of an [`IMemoryHeap`]. AOSP
/// [`IMemory`](https://cs.android.com/android/platform/superproject/+/android-16.0.0_r4:frameworks/native/libs/binder/include/binder/IMemory.h;l=69)
/// equivalent. An `IMemory` references a heap plus an `(offset, size)`
/// pair so that one large heap can host many small allocations (the
/// AOSP `MemoryDealer` pattern).
/// Concrete heap allocator. Currently only the public surface is
/// present; the actual `memfd_create` + `F_ADD_SEALS` implementation is
/// future work, gated on Linux/Android.
///
/// On macOS this is a *compile-only* stub: every constructor returns
/// `Err(StatusCode::InvalidOperation)` so callers can write
/// `cfg`-portable code that depends on the trait surface but doesn't
/// actually rely on shared memory backing existing.