Skip to content

Commit de7722c

Browse files
committed
Auto merge of #95702 - Dylan-DPC:rollup-793rz6v, r=Dylan-DPC
Rollup of 8 pull requests Successful merges: - #88025 (ScmCredentials netbsd implementation.) - #95473 (track individual proc-macro expansions in the self-profiler) - #95547 (caution against ptr-to-int transmutes) - #95585 (Explain why `&T` is cloned when `T` is not `Clone`) - #95591 (Use revisions to track NLL test output (part 1)) - #95663 (diagnostics: give a special note for unsafe fn / Fn/FnOnce/FnMut) - #95673 (:arrow_up: rust-analyzer) - #95681 (resolve: Fix resolution of empty paths passed from rustdoc) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 92024a9 + c8efecf commit de7722c

File tree

6 files changed

+131
-13
lines changed

6 files changed

+131
-13
lines changed

core/src/intrinsics.rs

+10
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,16 @@ extern "rust-intrinsic" {
991991
/// let ptr_num_cast = ptr as *const i32 as usize;
992992
/// ```
993993
///
994+
/// Note that using `transmute` to turn a pointer to a `usize` is (as noted above) [undefined
995+
/// behavior][ub] in `const` contexts. Also outside of consts, this operation might not behave
996+
/// as expected -- this is touching on many unspecified aspects of the Rust memory model.
997+
/// Depending on what the code is doing, the following alternatives are preferrable to
998+
/// pointer-to-integer transmutation:
999+
/// - If the code just wants to store data of arbitrary type in some buffer and needs to pick a
1000+
/// type for that buffer, it can use [`MaybeUninit`][mem::MaybeUninit].
1001+
/// - If the code actually wants to work on the address the pointer points to, it can use `as`
1002+
/// casts or [`ptr.addr()`][pointer::addr].
1003+
///
9941004
/// Turning a `*mut T` into an `&mut T`:
9951005
///
9961006
/// ```

core/src/ops/function.rs

+18
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@
6060
Args = "()",
6161
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
6262
),
63+
on(
64+
_Self = "unsafe fn",
65+
note = "unsafe function cannot be called generically without an unsafe block",
66+
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
67+
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
68+
),
6369
message = "expected a `{Fn}<{Args}>` closure, found `{Self}`",
6470
label = "expected an `Fn<{Args}>` closure, found `{Self}`"
6571
)]
@@ -141,6 +147,12 @@ pub trait Fn<Args>: FnMut<Args> {
141147
Args = "()",
142148
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
143149
),
150+
on(
151+
_Self = "unsafe fn",
152+
note = "unsafe function cannot be called generically without an unsafe block",
153+
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
154+
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
155+
),
144156
message = "expected a `{FnMut}<{Args}>` closure, found `{Self}`",
145157
label = "expected an `FnMut<{Args}>` closure, found `{Self}`"
146158
)]
@@ -214,6 +226,12 @@ pub trait FnMut<Args>: FnOnce<Args> {
214226
Args = "()",
215227
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
216228
),
229+
on(
230+
_Self = "unsafe fn",
231+
note = "unsafe function cannot be called generically without an unsafe block",
232+
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
233+
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
234+
),
217235
message = "expected a `{FnOnce}<{Args}>` closure, found `{Self}`",
218236
label = "expected an `FnOnce<{Args}>` closure, found `{Self}`"
219237
)]

std/src/os/unix/net/ancillary.rs

+88-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::slice::from_raw_parts;
1010
use crate::sys::net::Socket;
1111

1212
// FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
13-
#[cfg(all(doc, not(target_os = "linux"), not(target_os = "android")))]
13+
#[cfg(all(doc, not(target_os = "linux"), not(target_os = "android"), not(target_os = "netbsd")))]
1414
#[allow(non_camel_case_types)]
1515
mod libc {
1616
pub use libc::c_int;
@@ -177,13 +177,24 @@ impl<'a, T> Iterator for AncillaryDataIter<'a, T> {
177177
}
178178
}
179179

180+
#[cfg(all(doc, not(target_os = "android"), not(target_os = "linux"), not(target_os = "netbsd")))]
181+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
182+
#[derive(Clone)]
183+
pub struct SocketCred(());
184+
180185
/// Unix credential.
181-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
186+
#[cfg(any(target_os = "android", target_os = "linux",))]
182187
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
183188
#[derive(Clone)]
184189
pub struct SocketCred(libc::ucred);
185190

186-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
191+
#[cfg(target_os = "netbsd")]
192+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
193+
#[derive(Clone)]
194+
pub struct SocketCred(libc::sockcred);
195+
196+
#[doc(cfg(any(target_os = "android", target_os = "linux")))]
197+
#[cfg(any(target_os = "android", target_os = "linux"))]
187198
impl SocketCred {
188199
/// Create a Unix credential struct.
189200
///
@@ -234,6 +245,61 @@ impl SocketCred {
234245
}
235246
}
236247

248+
#[cfg(target_os = "netbsd")]
249+
impl SocketCred {
250+
/// Create a Unix credential struct.
251+
///
252+
/// PID, UID and GID is set to 0.
253+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
254+
pub fn new() -> SocketCred {
255+
SocketCred(libc::sockcred {
256+
sc_pid: 0,
257+
sc_uid: 0,
258+
sc_euid: 0,
259+
sc_gid: 0,
260+
sc_egid: 0,
261+
sc_ngroups: 0,
262+
sc_groups: [0u32; 1],
263+
})
264+
}
265+
266+
/// Set the PID.
267+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
268+
pub fn set_pid(&mut self, pid: libc::pid_t) {
269+
self.0.sc_pid = pid;
270+
}
271+
272+
/// Get the current PID.
273+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
274+
pub fn get_pid(&self) -> libc::pid_t {
275+
self.0.sc_pid
276+
}
277+
278+
/// Set the UID.
279+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
280+
pub fn set_uid(&mut self, uid: libc::uid_t) {
281+
self.0.sc_uid = uid;
282+
}
283+
284+
/// Get the current UID.
285+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
286+
pub fn get_uid(&self) -> libc::uid_t {
287+
self.0.sc_uid
288+
}
289+
290+
/// Set the GID.
291+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
292+
pub fn set_gid(&mut self, gid: libc::gid_t) {
293+
self.0.sc_gid = gid;
294+
}
295+
296+
/// Get the current GID.
297+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
298+
pub fn get_gid(&self) -> libc::gid_t {
299+
self.0.sc_gid
300+
}
301+
}
302+
237303
/// This control message contains file descriptors.
238304
///
239305
/// The level is equal to `SOL_SOCKET` and the type is equal to `SCM_RIGHTS`.
@@ -249,14 +315,22 @@ impl<'a> Iterator for ScmRights<'a> {
249315
}
250316
}
251317

318+
#[cfg(all(doc, not(target_os = "android"), not(target_os = "linux"), not(target_os = "netbsd")))]
319+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
320+
pub struct ScmCredentials<'a>(AncillaryDataIter<'a, ()>);
321+
252322
/// This control message contains unix credentials.
253323
///
254324
/// The level is equal to `SOL_SOCKET` and the type is equal to `SCM_CREDENTIALS` or `SCM_CREDS`.
255-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
325+
#[cfg(any(target_os = "android", target_os = "linux",))]
256326
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
257327
pub struct ScmCredentials<'a>(AncillaryDataIter<'a, libc::ucred>);
258328

259-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
329+
#[cfg(target_os = "netbsd")]
330+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
331+
pub struct ScmCredentials<'a>(AncillaryDataIter<'a, libc::sockcred>);
332+
333+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
260334
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
261335
impl<'a> Iterator for ScmCredentials<'a> {
262336
type Item = SocketCred;
@@ -278,7 +352,7 @@ pub enum AncillaryError {
278352
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
279353
pub enum AncillaryData<'a> {
280354
ScmRights(ScmRights<'a>),
281-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
355+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
282356
ScmCredentials(ScmCredentials<'a>),
283357
}
284358

@@ -300,8 +374,8 @@ impl<'a> AncillaryData<'a> {
300374
/// # Safety
301375
///
302376
/// `data` must contain a valid control message and the control message must be type of
303-
/// `SOL_SOCKET` and level of `SCM_CREDENTIALS` or `SCM_CREDENTIALS`.
304-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
377+
/// `SOL_SOCKET` and level of `SCM_CREDENTIALS` or `SCM_CREDS`.
378+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
305379
unsafe fn as_credentials(data: &'a [u8]) -> Self {
306380
let ancillary_data_iter = AncillaryDataIter::new(data);
307381
let scm_credentials = ScmCredentials(ancillary_data_iter);
@@ -320,6 +394,8 @@ impl<'a> AncillaryData<'a> {
320394
libc::SCM_RIGHTS => Ok(AncillaryData::as_rights(data)),
321395
#[cfg(any(target_os = "android", target_os = "linux",))]
322396
libc::SCM_CREDENTIALS => Ok(AncillaryData::as_credentials(data)),
397+
#[cfg(target_os = "netbsd")]
398+
libc::SCM_CREDS => Ok(AncillaryData::as_credentials(data)),
323399
cmsg_type => {
324400
Err(AncillaryError::Unknown { cmsg_level: libc::SOL_SOCKET, cmsg_type })
325401
}
@@ -531,7 +607,7 @@ impl<'a> SocketAncillary<'a> {
531607
/// Technically, that means this operation adds a control message with the level `SOL_SOCKET`
532608
/// and type `SCM_CREDENTIALS` or `SCM_CREDS`.
533609
///
534-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
610+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
535611
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
536612
pub fn add_creds(&mut self, creds: &[SocketCred]) -> bool {
537613
self.truncated = false;
@@ -540,7 +616,10 @@ impl<'a> SocketAncillary<'a> {
540616
&mut self.length,
541617
creds,
542618
libc::SOL_SOCKET,
619+
#[cfg(not(target_os = "netbsd"))]
543620
libc::SCM_CREDENTIALS,
621+
#[cfg(target_os = "netbsd")]
622+
libc::SCM_CREDS,
544623
)
545624
}
546625

std/src/os/unix/net/datagram.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ impl UnixDatagram {
865865
/// Ok(())
866866
/// }
867867
/// ```
868-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
868+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
869869
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
870870
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
871871
self.0.set_passcred(passcred)
@@ -877,7 +877,7 @@ impl UnixDatagram {
877877
/// Get the socket option `SO_PASSCRED`.
878878
///
879879
/// [`set_passcred`]: UnixDatagram::set_passcred
880-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
880+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
881881
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
882882
pub fn passcred(&self) -> io::Result<bool> {
883883
self.0.passcred()

std/src/os/unix/net/stream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ impl UnixStream {
415415
/// Ok(())
416416
/// }
417417
/// ```
418-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
418+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
419419
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
420420
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
421421
self.0.set_passcred(passcred)
@@ -427,7 +427,7 @@ impl UnixStream {
427427
/// Get the socket option `SO_PASSCRED`.
428428
///
429429
/// [`set_passcred`]: UnixStream::set_passcred
430-
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
430+
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
431431
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
432432
pub fn passcred(&self) -> io::Result<bool> {
433433
self.0.passcred()

std/src/sys/unix/net.rs

+11
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,17 @@ impl Socket {
419419
Ok(passcred != 0)
420420
}
421421

422+
#[cfg(target_os = "netbsd")]
423+
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
424+
setsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS, passcred as libc::c_int)
425+
}
426+
427+
#[cfg(target_os = "netbsd")]
428+
pub fn passcred(&self) -> io::Result<bool> {
429+
let passcred: libc::c_int = getsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS)?;
430+
Ok(passcred != 0)
431+
}
432+
422433
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
423434
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
424435
let mut nonblocking = nonblocking as libc::c_int;

0 commit comments

Comments
 (0)