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
//! Safe audited binding from committed active mappings to core capabilities.
//!
//! This module is the safe, fully audited alternative to the feature-gated
//! `raw-pointer` escape. It converts a committed [`ActiveReader`] or
//! [`ActiveWriter`] into the matching `native-ipc-core` capability owner
//! ([`ReaderRegion`]/[`WriterRegion`]) by consuming the active mapping into a
//! witness the core boundary trusts. Running the audited core protocol over a
//! session-transferred region needs no consumer unsafe code and no
//! `raw-pointer` feature on this path.
//!
//! # Witness soundness
//!
//! An active mapping is uniquely owned and cannot be cloned. Its local native
//! view is released only by the owner's own `Drop`; session poison or close
//! gates the safe accessors but never unmaps the view. Moving the consumed
//! active value inside a witness therefore keeps the whole `base..base+len`
//! extent mapped and initialized — and, for the read side, OS-enforced
//! read-only — for the entire witness lifetime. `len` is the mapping's
//! validated logical extent ([`ActiveReader::len`]/[`ActiveWriter::len`]); the
//! page-rounded tail beyond it is deliberately excluded, matching the range the
//! [`ValidatedRegionLayout`] was validated over.
//!
//! After the peer session ends the peer is gone and the bytes are frozen or
//! stale: read witnesses still observe only hostile, memory-safe bytes, and
//! write witnesses simply publish to nobody. Liveness re-checking is
//! deliberately not part of the witness contract; a consumer that needs it
//! keeps the owning session handle and quiesces before dropping the witness.
//!
//! [`ActiveReader`]: crate::active::ActiveReader
//! [`ActiveReader::len`]: crate::active::ActiveReader::len
//! [`ActiveWriter`]: crate::active::ActiveWriter
//! [`ActiveWriter::len`]: crate::active::ActiveWriter::len
//! [`ReaderRegion`]: crate::core::mapping::ReaderRegion
//! [`WriterRegion`]: crate::core::mapping::WriterRegion
//! [`ValidatedRegionLayout`]: crate::core::layout::ValidatedRegionLayout
use crate;
use crate;
use crate;
use fmt;
use NonNull;
/// Read-only witness that owns its consumed active mapping.
///
/// A region bound over this witness is a unique capability; neither the witness
/// nor the region it backs can be duplicated:
///
/// ```compile_fail
/// use native_ipc::binding::BoundReadMapping;
/// use native_ipc::core::mapping::ReaderRegion;
/// fn duplicate(
/// region: ReaderRegion<BoundReadMapping>,
/// ) -> (ReaderRegion<BoundReadMapping>, ReaderRegion<BoundReadMapping>) {
/// let copy = region.clone();
/// (region, copy)
/// }
/// ```
// SAFETY: the witness owns an `ActiveReader`, which is itself `Send + Sync`, and
// the cached `base` is a copy of that mapping's own base that can never outlive
// the owned reader. Moving or sharing the witness across threads only moves or
// shares the reader it already permits, so the markers grant no authority the
// inner mapping did not already have.
unsafe
unsafe
// SAFETY: the owned `ActiveReader` uniquely owns exactly one page-aligned,
// OS-enforced read-only native view and releases it only in its own `Drop`;
// session poison or close gates the safe accessors without unmapping, so the
// view stays mapped and initialized for as long as this witness lives. `base`
// is that mapping's own base carrying its allocation provenance, and `len`
// reports its validated logical extent — precisely the bytes the caller's
// `ValidatedRegionLayout` was validated over. Peer mutation may race, but the
// core boundary only ever performs volatile loads through `base` and never
// forms a shared reference, so exposing this witness cannot violate memory
// safety.
unsafe
/// Sole-writer witness that owns its consumed active mapping.
// SAFETY: the witness owns an `ActiveWriter`, which is `Send` but deliberately
// not `Sync`; moving it between threads transfers the single writable view
// without ever aliasing it, and the cached `base` cannot outlive that owned
// writer. Matching the inner mapping, the witness is `Send` only, so no shared
// cross-thread writer alias becomes reachable.
unsafe
// SAFETY: the owned `ActiveWriter` is, by construction, the region's only
// writable native view: it is non-cloneable, holds sole store authority, and
// releases its page-aligned view only in its own `Drop`. Session poison or
// close gates the safe accessors without unmapping, so the view stays mapped
// and writable for the witness lifetime. `base` is that mapping's own base with
// allocation provenance and `len` is its validated logical extent, exactly the
// range the `ValidatedRegionLayout` was validated over. While a `WriterRegion`
// owns this witness, safe code cannot recover a second writer for the region.
unsafe
/// A rejected bind that returns the consumed active mapping to its caller.
///
/// The bind boundary consumes the active mapping by value; on rejection this
/// carrier hands the exact same value back so the caller recovers it instead of
/// losing the committed mapping.
// The recovered active mapping is deliberately opaque, so this does not require
// `T: Debug`; only the binding error is reported.