annotate libpam-sys/libpam-sys-helpers/src/lib.rs @ 149:14708d9061dc

Add safety section to BinaryPayload::total_bytes.
author Paul Fisher <paul@pfish.zone>
date Mon, 07 Jul 2025 12:30:25 -0400
parents 4b3a5095f68c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
148
4b3a5095f68c Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents: 143
diff changeset
1 //! This package contains helpers to deal with memory management and
4b3a5095f68c Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents: 143
diff changeset
2 //! annoying type stuff in `libpam-sys` (and LibPAM in general).
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
3
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
4 use std::error::Error;
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
5 use std::marker::{PhantomData, PhantomPinned};
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
6 use std::mem::ManuallyDrop;
109
bb465393621f Minor cleanup and reorg.
Paul Fisher <paul@pfish.zone>
parents: 106
diff changeset
7 use std::ptr::NonNull;
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
8 use std::{any, fmt, mem, ptr, slice};
148
4b3a5095f68c Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents: 143
diff changeset
9 // Type aliases:
4b3a5095f68c Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents: 143
diff changeset
10
4b3a5095f68c Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents: 143
diff changeset
11 // Memory management
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
12
136
efbc235f01d3 Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents: 134
diff changeset
13 /// A pointer-to-pointer-to-message container for PAM's conversation callback.
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
14 ///
136
efbc235f01d3 Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents: 134
diff changeset
15 /// The PAM conversation callback requires a pointer to a pointer of
efbc235f01d3 Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents: 134
diff changeset
16 /// `pam_message`s. Linux-PAM handles this differently than all other
efbc235f01d3 Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents: 134
diff changeset
17 /// PAM implementations (including the X/SSO PAM standard).
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
18 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
19 /// X/SSO appears to specify a pointer-to-pointer-to-array:
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
20 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
21 /// ```text
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
22 /// points to ┌────────────┐ ╔═ Message[] ═╗
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
23 /// messages ┄┄┄┄┄┄┄┄┄┄> │ *messages ┄┼┄┄┄┄┄> ║ style ║
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
24 /// └────────────┘ ║ data ┄┄┄┄┄┄┄╫┄┄> ...
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
25 /// ╟─────────────╢
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
26 /// ║ style ║
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
27 /// ║ data ┄┄┄┄┄┄┄╫┄┄> ...
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
28 /// ╟─────────────╢
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
29 /// ║ ... ║
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
30 /// ```
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
31 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
32 /// whereas Linux-PAM uses an `**argv`-style pointer-to-array-of-pointers:
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
33 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
34 /// ```text
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
35 /// points to ┌──────────────┐ ╔═ Message ═╗
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
36 /// messages ┄┄┄┄┄┄┄┄┄┄> │ messages[0] ┄┼┄┄┄┄> ║ style ║
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
37 /// │ messages[1] ┄┼┄┄┄╮ ║ data ┄┄┄┄┄╫┄┄> ...
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
38 /// │ ... │ ┆ ╚═══════════╝
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
39 /// ┆
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
40 /// ┆ ╔═ Message ═╗
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
41 /// ╰┄┄> ║ style ║
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
42 /// ║ data ┄┄┄┄┄╫┄┄> ...
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
43 /// ╚═══════════╝
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
44 /// ```
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
45 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
46 /// Because the `messages` remain owned by the application which calls into PAM,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
47 /// we can solve this with One Simple Trick: make the intermediate list point
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
48 /// into the same array:
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
49 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
50 /// ```text
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
51 /// points to ┌──────────────┐ ╔═ Message[] ═╗
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
52 /// messages ┄┄┄┄┄┄┄┄┄┄> │ messages[0] ┄┼┄┄┄┄> ║ style ║
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
53 /// │ messages[1] ┄┼┄┄╮ ║ data ┄┄┄┄┄┄┄╫┄┄> ...
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
54 /// │ ... │ ┆ ╟─────────────╢
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
55 /// ╰┄> ║ style ║
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
56 /// ║ data ┄┄┄┄┄┄┄╫┄┄> ...
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
57 /// ╟─────────────╢
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
58 /// ║ ... ║
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
59 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
60 /// ```
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
61 #[derive(Debug)]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
62 pub struct PtrPtrVec<T> {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
63 data: Vec<T>,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
64 pointers: Vec<*const T>,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
65 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
66
125
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
67 // Since this is a wrapper around a Vec with no dangerous functionality*,
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
68 // this can be Send and Sync provided the original Vec is.
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
69 //
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
70 // * It will only become unsafe when the user dereferences a pointer or sends it
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
71 // to an unsafe function.
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
72 unsafe impl<T> Send for PtrPtrVec<T> where Vec<T>: Send {}
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
73 unsafe impl<T> Sync for PtrPtrVec<T> where Vec<T>: Sync {}
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
74
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
75 impl<T> PtrPtrVec<T> {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
76 /// Takes ownership of the given Vec and creates a vec of pointers to it.
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
77 pub fn new(data: Vec<T>) -> Self {
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
78 let start = data.as_ptr();
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
79 // We do this slightly tricky little dance to satisfy Miri:
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
80 //
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
81 // A pointer extracted from a reference can only legally access
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
82 // that reference's memory. This means that if we say:
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
83 // pointers[0] = &data[0] as *const T;
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
84 // we can't traverse through pointers[0] to reach data[1],
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
85 // we can only use pointers[1].
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
86 //
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
87 // However, if we use the start-of-vec pointer from the `data` vector,
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
88 // its "provenance"* is valid for the entire array (even if the address
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
89 // of the pointer is the same). This avoids some behavior which is
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
90 // technically undefined. While the CPU sees no difference between
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
91 // those two pointers, the compiler is allowed to make optimizations
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
92 // based on that provenance (even if, in this case, it isn't likely
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
93 // to do so).
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
94 //
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
95 // data.as_ptr() points here, and is valid for the whole Vec.
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
96 // ┃
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
97 // ┠─────────────────╮
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
98 // ┌─────┬─────┬─────┐
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
99 // data │ [0] │ [1] │ [2] │
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
100 // └─────┴─────┴─────┘
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
101 // ┠─────╯ ┊
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
102 // ┃ ┊ ┊
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
103 // (&data[0] as *const T) points to the same place, but is valid
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
104 // only for that 0th element.
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
105 // ┊ ┊
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
106 // ┠─────╯
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
107 // ┃
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
108 // (&data[1] as *const T) points here, and is only valid
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
109 // for that element.
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
110 //
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
111 // We only have to do this for pointers[0] because only that pointer
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
112 // is used for accessing elements other than data[0] (in XSSO).
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
113 //
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
114 // * "provenance" is kind of like if every pointer in your program
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
115 // remembered where it came from and, based on that, it had an implied
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
116 // memory range it was valid for, separate from its address.
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
117 // https://doc.rust-lang.org/std/ptr/#provenance
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
118 // (It took a long time for me to understand this.)
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
119 let mut pointers = Vec::with_capacity(data.len());
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
120 // Ensure the 0th pointer has provenance from the entire vec
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
121 // (even though it's numerically identical to &data[0] as *const T).
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
122 pointers.push(start);
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
123 // The 1st and everything thereafter only need to have the provenance
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
124 // of their own memory.
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
125 pointers.extend(data[1..].iter().map(|r| r as *const T));
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
126 Self { data, pointers }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
127 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
128
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
129 /// Gives you back your Vec.
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
130 pub fn into_inner(self) -> Vec<T> {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
131 self.data
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
132 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
133
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
134 /// Gets a pointer-to-pointer suitable for passing into the Conversation.
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
135 pub fn as_ptr<Dest>(&self) -> *const *const Dest {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
136 Self::assert_size::<Dest>();
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
137 self.pointers.as_ptr().cast::<*const Dest>()
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
138 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
139
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
140 /// Iterates over a Linux-PAM–style pointer-to-array-of-pointers.
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
141 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
142 /// # Safety
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
143 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
144 /// `ptr_ptr` must be a valid pointer to an array of pointers,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
145 /// there must be at least `count` valid pointers in the array,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
146 /// and each pointer in that array must point to a valid `T`.
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
147 #[deprecated = "use [`Self::iter_over`] instead, unless you really need this specific version"]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
148 #[allow(dead_code)]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
149 pub unsafe fn iter_over_linux<'a, Src>(
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
150 ptr_ptr: *const *const Src,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
151 count: usize,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
152 ) -> impl Iterator<Item = &'a T>
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
153 where
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
154 T: 'a,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
155 {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
156 Self::assert_size::<Src>();
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
157 slice::from_raw_parts(ptr_ptr.cast::<&T>(), count)
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
158 .iter()
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
159 .copied()
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
160 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
161
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
162 /// Iterates over an X/SSO–style pointer-to-pointer-to-array.
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
163 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
164 /// # Safety
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
165 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
166 /// You must pass a valid pointer to a valid pointer to an array,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
167 /// there must be at least `count` elements in the array,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
168 /// and each value in that array must be a valid `T`.
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
169 #[deprecated = "use [`Self::iter_over`] instead, unless you really need this specific version"]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
170 #[allow(dead_code)]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
171 pub unsafe fn iter_over_xsso<'a, Src>(
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
172 ptr_ptr: *const *const Src,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
173 count: usize,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
174 ) -> impl Iterator<Item = &'a T>
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
175 where
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
176 T: 'a,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
177 {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
178 Self::assert_size::<Src>();
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
179 slice::from_raw_parts(*ptr_ptr.cast(), count).iter()
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
180 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
181
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
182 /// Iterates over a PAM message list appropriate to your system's impl.
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
183 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
184 /// This selects the correct pointer/array structure to use for a message
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
185 /// that was given to you by your system.
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
186 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
187 /// # Safety
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
188 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
189 /// `ptr_ptr` must point to a valid message list, there must be at least
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
190 /// `count` messages in the list, and all messages must be a valid `Src`.
134
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 127
diff changeset
191 #[allow(deprecated)]
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
192 pub unsafe fn iter_over<'a, Src>(
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
193 ptr_ptr: *const *const Src,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
194 count: usize,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
195 ) -> impl Iterator<Item = &'a T>
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
196 where
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
197 T: 'a,
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
198 {
134
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 127
diff changeset
199 #[cfg(pam_impl = "LinuxPam")]
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 127
diff changeset
200 return Self::iter_over_linux(ptr_ptr, count);
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 127
diff changeset
201 #[cfg(not(pam_impl = "LinuxPam"))]
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 127
diff changeset
202 return Self::iter_over_xsso(ptr_ptr, count);
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
203 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
204
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
205 fn assert_size<That>() {
140
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
206 assert_eq!(
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
207 mem::size_of::<T>(),
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
208 mem::size_of::<That>(),
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
209 "type {t} is not the size of {that}",
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
210 t = any::type_name::<T>(),
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
211 that = any::type_name::<That>(),
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
212 );
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
213 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
214 }
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
215
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
216 /// Error returned when attempting to allocate a buffer that is too big.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
217 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
218 /// This is specifically used in [`OwnedBinaryPayload`] when you try to allocate
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
219 /// a message larger than 2<sup>32</sup> bytes.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
220 #[derive(Debug, PartialEq)]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
221 pub struct TooBigError {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
222 pub size: usize,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
223 pub max: usize,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
224 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
225
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
226 impl Error for TooBigError {}
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
227
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
228 impl fmt::Display for TooBigError {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
229 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
230 write!(
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
231 f,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
232 "can't allocate a message of {size} bytes (max {max})",
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
233 size = self.size,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
234 max = self.max
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
235 )
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
236 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
237 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
238
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
239 /// A trait wrapping memory management.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
240 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
241 /// This is intended to allow you to bring your own allocator for
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
242 /// [`OwnedBinaryPayload`]s.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
243 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
244 /// For an implementation example, see the implementation of this trait
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
245 /// for [`Vec`].
140
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
246 #[allow(clippy::wrong_self_convention)]
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
247 pub trait Buffer {
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
248 /// Allocates a buffer of `len` elements, filled with the default.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
249 fn allocate(len: usize) -> Self;
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
250
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
251 fn as_ptr(this: &Self) -> *const u8;
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
252
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
253 /// Returns a slice view of `size` elements of the given memory.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
254 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
255 /// # Safety
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
256 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
257 /// The caller must not request more elements than are allocated.
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
258 unsafe fn as_mut_slice(this: &mut Self, len: usize) -> &mut [u8];
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
259
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
260 /// Consumes this ownership and returns a pointer to the start of the arena.
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
261 fn into_ptr(this: Self) -> NonNull<u8>;
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
262
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
263 /// "Adopts" the memory at the given pointer, taking it under management.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
264 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
265 /// Running the operation:
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
266 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
267 /// ```
148
4b3a5095f68c Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents: 143
diff changeset
268 /// # use libpam_sys_helpers::Buffer;
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
269 /// # fn test<T: Default, OwnerType: Buffer>(bytes: usize) {
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
270 /// let owner = OwnerType::allocate(bytes);
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
271 /// let ptr = OwnerType::into_ptr(owner);
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
272 /// let owner = unsafe { OwnerType::from_ptr(ptr, bytes) };
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
273 /// # }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
274 /// ```
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
275 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
276 /// must be a no-op.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
277 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
278 /// # Safety
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
279 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
280 /// The pointer must be valid, and the caller must provide the exact size
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
281 /// of the given arena.
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
282 unsafe fn from_ptr(ptr: NonNull<u8>, bytes: usize) -> Self;
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
283 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
284
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
285 impl Buffer for Vec<u8> {
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
286 fn allocate(bytes: usize) -> Self {
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
287 vec![0; bytes]
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
288 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
289
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
290 fn as_ptr(this: &Self) -> *const u8 {
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
291 Vec::as_ptr(this)
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
292 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
293
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
294 unsafe fn as_mut_slice(this: &mut Self, bytes: usize) -> &mut [u8] {
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
295 &mut this[..bytes]
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
296 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
297
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
298 fn into_ptr(this: Self) -> NonNull<u8> {
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
299 let mut me = ManuallyDrop::new(this);
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
300 // SAFETY: a Vec is guaranteed to have a nonzero pointer.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
301 unsafe { NonNull::new_unchecked(me.as_mut_ptr()) }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
302 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
303
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
304 unsafe fn from_ptr(ptr: NonNull<u8>, bytes: usize) -> Self {
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
305 Vec::from_raw_parts(ptr.as_ptr(), bytes, bytes)
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
306 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
307 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
308
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
309 /// The structure of the "binary message" payload for the `PAM_BINARY_PROMPT`
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
310 /// extension from Linux-PAM.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
311 pub struct BinaryPayload {
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
312 /// The total byte size of the message, including this header,
140
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
313 /// as u32 in network byte order (big endian).
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
314 pub total_bytes_u32be: [u8; 4],
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
315 /// A tag used to provide some kind of hint as to what the data is.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
316 /// Its meaning is undefined.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
317 pub data_type: u8,
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
318 /// Where the data itself would start, used as a marker to make this
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
319 /// not [`Unpin`] (since it is effectively an intrusive data structure
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
320 /// pointing to immediately after itself).
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
321 pub _marker: PhantomData<PhantomPinned>,
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
322 }
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
323
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
324 impl BinaryPayload {
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
325 /// The most data it's possible to put into a [`BinaryPayload`].
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
326 pub const MAX_SIZE: usize = (u32::MAX - 5) as usize;
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
327
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
328 /// Fills in the provided buffer with the given data.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
329 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
330 /// This uses [`copy_from_slice`](slice::copy_from_slice) internally,
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
331 /// so `buf` must be exactly 5 bytes longer than `data`, or this function
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
332 /// will panic.
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
333 pub fn fill(buf: &mut [u8], data: &[u8], data_type: u8) {
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
334 let ptr: *mut Self = buf.as_mut_ptr().cast();
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
335 // SAFETY: We're given a slice, which always has a nonzero pointer.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
336 let me = unsafe { ptr.as_mut().unwrap_unchecked() };
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
337 me.total_bytes_u32be = u32::to_be_bytes(buf.len() as u32);
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
338 me.data_type = data_type;
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
339 buf[5..].copy_from_slice(data)
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
340 }
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
341
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
342 /// The total storage needed for the message, including header.
149
14708d9061dc Add safety section to BinaryPayload::total_bytes.
Paul Fisher <paul@pfish.zone>
parents: 148
diff changeset
343 ///
14708d9061dc Add safety section to BinaryPayload::total_bytes.
Paul Fisher <paul@pfish.zone>
parents: 148
diff changeset
344 /// # Safety
14708d9061dc Add safety section to BinaryPayload::total_bytes.
Paul Fisher <paul@pfish.zone>
parents: 148
diff changeset
345 ///
14708d9061dc Add safety section to BinaryPayload::total_bytes.
Paul Fisher <paul@pfish.zone>
parents: 148
diff changeset
346 /// The pointer must point to a valid `BinaryPayload`.
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
347 pub unsafe fn total_bytes(this: *const Self) -> usize {
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
348 let header = this.as_ref().unwrap_unchecked();
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
349 u32::from_be_bytes(header.total_bytes_u32be) as usize
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
350 }
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
351
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
352 /// Gets the total byte buffer of the BinaryMessage stored at the pointer.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
353 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
354 /// The returned data slice is borrowed from where the pointer points to.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
355 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
356 /// # Safety
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
357 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
358 /// - The pointer must point to a valid `BinaryPayload`.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
359 /// - The borrowed data must not outlive the pointer's validity.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
360 pub unsafe fn buffer_of<'a>(ptr: *const Self) -> &'a [u8] {
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
361 slice::from_raw_parts(ptr.cast(), Self::total_bytes(ptr).max(5))
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
362 }
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
363
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
364 /// Gets the contents of the BinaryMessage stored at the given pointer.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
365 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
366 /// The returned data slice is borrowed from where the pointer points to.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
367 /// This is a cheap operation and doesn't do *any* copying.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
368 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
369 /// We don't take a `&self` reference here because accessing beyond
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
370 /// the range of the `Self` data (i.e., beyond the 5 bytes of `self`)
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
371 /// is undefined behavior. Instead, you have to pass a raw pointer
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
372 /// directly to the data.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
373 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
374 /// # Safety
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
375 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
376 /// - The pointer must point to a valid `BinaryPayload`.
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
377 /// - The borrowed data must not outlive the pointer's validity.
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
378 pub unsafe fn contents<'a>(ptr: *const Self) -> (&'a [u8], u8) {
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
379 let header: &Self = ptr.as_ref().unwrap_unchecked();
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
380 (&Self::buffer_of(ptr)[5..], header.data_type)
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
381 }
143
ebb71a412b58 Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents: 140
diff changeset
382
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
383 /// Zeroes out the data of this payload.
143
ebb71a412b58 Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents: 140
diff changeset
384 ///
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
385 /// # Safety
143
ebb71a412b58 Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents: 140
diff changeset
386 ///
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
387 /// - The pointer must point to a valid `BinaryPayload`.
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
388 /// - The binary payload must not be used in the future,
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
389 /// since its length metadata is gone and so its buffer is unknown.
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
390 pub unsafe fn zero(ptr: *mut Self) {
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
391 let size = Self::total_bytes(ptr);
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
392 let ptr: *mut u8 = ptr.cast();
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
393 for x in 0..size {
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
394 ptr::write_volatile(ptr.byte_add(x), mem::zeroed())
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
395 }
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
396 }
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
397 }
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
398
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
399 /// A binary message owned by some storage.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
400 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
401 /// This is an owned, memory-managed version of [`BinaryPayload`].
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
402 /// The `O` type manages the memory where the payload lives.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
403 /// [`Vec<u8>`] is one such manager and can be used when ownership
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
404 /// of the data does not need to transit through PAM.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
405 #[derive(Debug)]
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
406 pub struct OwnedBinaryPayload<Owner: Buffer>(Owner);
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
407
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
408 impl<O: Buffer> OwnedBinaryPayload<O> {
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
409 /// Allocates a new OwnedBinaryPayload.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
410 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
411 /// This will return a [`TooBigError`] if you try to allocate too much
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
412 /// (more than [`BinaryPayload::MAX_SIZE`]).
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
413 pub fn new(data: &[u8], type_: u8) -> Result<Self, TooBigError> {
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
414 let total_len: u32 = (data.len() + 5).try_into().map_err(|_| TooBigError {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
415 size: data.len(),
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
416 max: BinaryPayload::MAX_SIZE,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
417 })?;
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
418 let total_len = total_len as usize;
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
419 let mut buf = O::allocate(total_len);
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
420 // SAFETY: We just allocated this exact size.
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
421 BinaryPayload::fill(
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
422 unsafe { Buffer::as_mut_slice(&mut buf, total_len) },
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
423 data,
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
424 type_,
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
425 );
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
426 Ok(Self(buf))
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
427 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
428
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
429 /// The contents of the buffer.
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
430 pub fn contents(&self) -> (&[u8], u8) {
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
431 unsafe { BinaryPayload::contents(self.as_ptr()) }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
432 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
433
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
434 /// The total bytes needed to store this, including the header.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
435 pub fn total_bytes(&self) -> usize {
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
436 unsafe { BinaryPayload::buffer_of(Buffer::as_ptr(&self.0).cast()).len() }
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
437 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
438
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
439 /// Unwraps this into the raw storage backing it.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
440 pub fn into_inner(self) -> O {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
441 self.0
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
442 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
443
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
444 /// Gets a const pointer to the start of the message's buffer.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
445 pub fn as_ptr(&self) -> *const BinaryPayload {
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
446 Buffer::as_ptr(&self.0).cast()
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
447 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
448
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
449 /// Consumes ownership of this message and converts it to a raw pointer
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
450 /// to the start of the message.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
451 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
452 /// To clean this up, you should eventually pass it into [`Self::from_ptr`]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
453 /// with the same `O` ownership type.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
454 pub fn into_ptr(self) -> NonNull<BinaryPayload> {
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
455 Buffer::into_ptr(self.0).cast()
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
456 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
457
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
458 /// Takes ownership of the given pointer.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
459 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
460 /// # Safety
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
461 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
462 /// You must provide a valid pointer, allocated by (or equivalent to one
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
463 /// allocated by) [`Self::new`]. For instance, passing a pointer allocated
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
464 /// by `malloc` to `OwnedBinaryPayload::<Vec<u8>>::from_ptr` is not allowed.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
465 pub unsafe fn from_ptr(ptr: NonNull<BinaryPayload>) -> Self {
143
ebb71a412b58 Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents: 140
diff changeset
466 Self(O::from_ptr(
ebb71a412b58 Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents: 140
diff changeset
467 ptr.cast(),
ebb71a412b58 Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents: 140
diff changeset
468 BinaryPayload::total_bytes(ptr.as_ptr()),
ebb71a412b58 Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents: 140
diff changeset
469 ))
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
470 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
471 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
472
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
473 #[cfg(test)]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
474 mod tests {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
475 use super::*;
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
476 use std::ptr;
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
477
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
478 type VecPayload = OwnedBinaryPayload<Vec<u8>>;
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
479
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
480 #[test]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
481 fn test_binary_payload() {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
482 let simple_message = &[0u8, 0, 0, 16, 0xff, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
483 let empty = &[0u8; 5];
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
484
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
485 assert_eq!((&[0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10][..], 0xff), unsafe {
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
486 BinaryPayload::contents(simple_message.as_ptr().cast())
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
487 });
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
488 assert_eq!((&[][..], 0x00), unsafe {
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
489 BinaryPayload::contents(empty.as_ptr().cast())
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
490 });
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
491 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
492
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
493 #[test]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
494 fn test_owned_binary_payload() {
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
495 let (data, typ) = (
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
496 &[0, 1, 1, 8, 9, 9, 9, 8, 8, 1, 9, 9, 9, 1, 1, 9, 7, 2, 5, 3][..],
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
497 112,
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
498 );
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
499 let payload = VecPayload::new(data, typ).unwrap();
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
500 assert_eq!((data, typ), payload.contents());
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
501 let ptr = payload.into_ptr();
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
502 let payload = unsafe { VecPayload::from_ptr(ptr) };
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
503 assert_eq!((data, typ), payload.contents());
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
504 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
505
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
506 #[test]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
507 #[ignore]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
508 fn test_owned_too_big() {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
509 let data = vec![0xFFu8; 0x1_0000_0001];
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
510 assert_eq!(
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
511 TooBigError {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
512 max: 0xffff_fffa,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
513 size: 0x1_0000_0001
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
514 },
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
515 VecPayload::new(&data, 5).unwrap_err()
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
516 )
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
517 }
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
518
123
98a624cacd82 Get rid of all the warnings, and arrange attributes.
Paul Fisher <paul@pfish.zone>
parents: 119
diff changeset
519 #[cfg(debug_assertions)]
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
520 #[test]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
521 #[should_panic]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
522 fn test_new_wrong_size() {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
523 let bad_vec = vec![0; 19];
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
524 let msg = PtrPtrVec::new(bad_vec);
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
525 let _ = msg.as_ptr::<u64>();
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
526 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
527
123
98a624cacd82 Get rid of all the warnings, and arrange attributes.
Paul Fisher <paul@pfish.zone>
parents: 119
diff changeset
528 #[allow(deprecated)]
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
529 #[test]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
530 #[should_panic]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
531 fn test_iter_xsso_wrong_size() {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
532 unsafe {
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
533 let _ = PtrPtrVec::<u8>::iter_over_xsso::<f64>(ptr::null(), 1);
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
534 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
535 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
536
123
98a624cacd82 Get rid of all the warnings, and arrange attributes.
Paul Fisher <paul@pfish.zone>
parents: 119
diff changeset
537 #[allow(deprecated)]
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
538 #[test]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
539 #[should_panic]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
540 fn test_iter_linux_wrong_size() {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
541 unsafe {
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
542 let _ = PtrPtrVec::<u128>::iter_over_linux::<()>(ptr::null(), 1);
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
543 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
544 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
545
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
546 #[allow(deprecated)]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
547 #[test]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
548 fn test_right_size() {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
549 let good_vec = vec![(1u64, 2u64), (3, 4), (5, 6)];
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
550 let ptr = good_vec.as_ptr();
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
551 let msg = PtrPtrVec::new(good_vec);
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
552 let msg_ref: *const *const (i64, i64) = msg.as_ptr();
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
553 assert_eq!(unsafe { *msg_ref }, ptr.cast());
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
554
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
555 let linux_result: Vec<(i64, i64)> = unsafe { PtrPtrVec::iter_over_linux(msg_ref, 3) }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
556 .cloned()
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
557 .collect();
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
558 let xsso_result: Vec<(i64, i64)> = unsafe { PtrPtrVec::iter_over_xsso(msg_ref, 3) }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
559 .cloned()
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
560 .collect();
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
561 assert_eq!(vec![(1, 2), (3, 4), (5, 6)], linux_result);
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
562 assert_eq!(vec![(1, 2), (3, 4), (5, 6)], xsso_result);
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
563 drop(msg)
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
564 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
565
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
566 #[allow(deprecated)]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
567 #[test]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
568 fn test_iter_ptr_ptr() {
140
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
569 // These boxes are larger than a single pointer because we want to
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
570 // make sure they're not accidentally allocated adjacently
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
571 // in such a way that it's compatible with X/SSO.
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
572 //
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
573 // a pointer to (&str, i32) can be treated as a pointer to (&str).
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
574 #[repr(C)]
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
575 struct pair(&'static str, i32);
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
576 let boxes = vec![
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
577 Box::new(pair("a", 1)),
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
578 Box::new(pair("b", 2)),
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
579 Box::new(pair("c", 3)),
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
580 Box::new(pair("D", 4)),
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
581 ];
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
582 let ptr: *const *const &str = boxes.as_ptr().cast();
125
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
583 let got: Vec<&str> = unsafe { PtrPtrVec::iter_over_linux(ptr, 4) }
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
584 .cloned()
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
585 .collect();
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
586 assert_eq!(vec!["a", "b", "c", "D"], got);
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
587
140
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
588 // On the other hand, we explicitly want these to be adjacent.
123
98a624cacd82 Get rid of all the warnings, and arrange attributes.
Paul Fisher <paul@pfish.zone>
parents: 119
diff changeset
589 let nums = [-1i8, 2, 3];
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
590 let ptr = nums.as_ptr();
125
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
591 let got: Vec<u8> = unsafe { PtrPtrVec::iter_over_xsso(&ptr, 3) }
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
592 .cloned()
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
593 .collect();
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
594 assert_eq!(vec![255, 2, 3], got);
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
595 }
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
596 }