annotate libpam-sys/libpam-sys-helpers/src/memory.rs @ 140:add7228adb2f

Neaten up some stuff in libpam-sys memory module.
author Paul Fisher <paul@pfish.zone>
date Sat, 05 Jul 2025 17:11:33 -0400
parents 33b9622ed6d2
children ebb71a412b58
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
136
efbc235f01d3 Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents: 134
diff changeset
1 //! Helpers to deal with annoying memory management in the PAM API.
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
2
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
3 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
4 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
5 use std::mem::ManuallyDrop;
109
bb465393621f Minor cleanup and reorg.
Paul Fisher <paul@pfish.zone>
parents: 106
diff changeset
6 use std::ptr::NonNull;
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
7 use std::{any, fmt, mem, ptr, slice};
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
8
136
efbc235f01d3 Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents: 134
diff changeset
9 /// 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
10 ///
136
efbc235f01d3 Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents: 134
diff changeset
11 /// 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
12 /// `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
13 /// 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
14 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
15 /// 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
16 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
17 /// ```text
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
18 /// 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
19 /// 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
20 /// └────────────┘ ║ data ┄┄┄┄┄┄┄╫┄┄> ...
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
21 /// ╟─────────────╢
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
22 /// ║ style ║
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
23 /// ║ data ┄┄┄┄┄┄┄╫┄┄> ...
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
24 /// ╟─────────────╢
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 /// ```
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
27 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
28 /// 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
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 /// ```text
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
31 /// 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
32 /// 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
33 /// │ 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
34 /// │ ... │ ┆ ╚═══════════╝
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
35 /// ┆
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
36 /// ┆ ╔═ Message ═╗
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
37 /// ╰┄┄> ║ style ║
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
38 /// ║ data ┄┄┄┄┄╫┄┄> ...
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 /// ```
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
41 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
42 /// 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
43 /// 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
44 /// 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
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 /// ```text
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
47 /// 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
48 /// 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
49 /// │ 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
50 /// │ ... │ ┆ ╟─────────────╢
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
51 /// ╰┄> ║ style ║
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
52 /// ║ data ┄┄┄┄┄┄┄╫┄┄> ...
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
53 /// ╟─────────────╢
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 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
56 /// ```
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
57 #[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
58 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
59 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
60 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
61 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
62
125
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
63 // 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
64 // 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
65 //
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
66 // * 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
67 // 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
68 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
69 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
70
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
71 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
72 /// 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
73 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
74 let start = data.as_ptr();
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
75 // 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
76 //
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
77 // 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
78 // 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
79 // 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
80 // 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
81 // 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
82 //
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
83 // 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
84 // 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
85 // 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
86 // 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
87 // 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
88 // 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
89 // to do so).
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
90 //
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
91 // 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
92 // ┃
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
93 // ┠─────────────────╮
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 │ [0] │ [1] │ [2] │
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] 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
100 // only for that 0th element.
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 // ┃
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
104 // (&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
105 // for that element.
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 // 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
108 // 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
109 //
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
110 // * "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
111 // 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
112 // 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
113 // 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
114 // (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
115 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
116 // 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
117 // (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
118 pointers.push(start);
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
119 // 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
120 // of their own memory.
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
121 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
122 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
123 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
124
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
125 /// 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
126 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
127 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
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
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
130 /// 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
131 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
132 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
133 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
134 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
135
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
136 /// 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
137 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
138 /// # Safety
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 /// `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
141 /// 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
142 /// 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
143 #[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
144 #[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
145 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
146 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
147 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
148 ) -> 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
149 where
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
150 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
151 {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
152 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
153 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
154 .iter()
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
155 .copied()
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
156 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
157
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
158 /// 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
159 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
160 /// # Safety
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 /// 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
163 /// 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
164 /// 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
165 #[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
166 #[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
167 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
168 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
169 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
170 ) -> 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
171 where
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
172 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
173 {
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
174 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
175 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
176 }
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 /// 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
179 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
180 /// 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
181 /// 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
182 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
183 /// # Safety
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
184 ///
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
185 /// `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
186 /// `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
187 #[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
188 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
189 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
190 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
191 ) -> 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
192 where
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
193 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
194 {
134
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 127
diff changeset
195 #[cfg(pam_impl = "LinuxPam")]
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 127
diff changeset
196 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
197 #[cfg(not(pam_impl = "LinuxPam"))]
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 127
diff changeset
198 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
199 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
200
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
201 fn assert_size<That>() {
140
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
202 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
203 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
204 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
205 "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
206 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
207 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
208 );
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
209 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
210 }
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
211
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
212 /// 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
213 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
214 /// 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
215 /// 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
216 #[derive(Debug, PartialEq)]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
217 pub struct TooBigError {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
218 pub size: usize,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
219 pub max: usize,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
220 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
221
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
222 impl Error for TooBigError {}
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
223
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
224 impl fmt::Display for TooBigError {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
225 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
226 write!(
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
227 f,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
228 "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
229 size = self.size,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
230 max = self.max
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
231 )
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
232 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
233 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
234
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
235 /// A trait wrapping memory management.
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 /// 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
238 /// [`OwnedBinaryPayload`]s.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
239 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
240 /// 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
241 /// for [`Vec`].
140
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
242 #[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
243 pub trait Buffer {
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
244 /// 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
245 fn allocate(len: usize) -> Self;
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
246
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
247 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
248
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
249 /// 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
250 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
251 /// # Safety
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 /// 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
254 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
255
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
256 /// 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
257 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
258
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
259 /// "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
260 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
261 /// Running the operation:
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 /// ```
136
efbc235f01d3 Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents: 134
diff changeset
264 /// # use libpam_sys_helpers::memory::Buffer;
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
265 /// # 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
266 /// 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
267 /// 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
268 /// 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
269 /// # }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
270 /// ```
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
271 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
272 /// must be a no-op.
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 /// # Safety
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 /// 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
277 /// of the given arena.
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
278 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
279 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
280
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
281 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
282 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
283 vec![0; bytes]
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
284 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
285
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
286 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
287 Vec::as_ptr(this)
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 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
291 &mut this[..bytes]
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 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
295 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
296 // 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
297 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
298 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
299
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
300 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
301 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
302 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
303 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
304
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
305 /// 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
306 /// 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
307 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
308 /// 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
309 /// 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
310 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
311 /// 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
312 /// 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
313 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
314 /// 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
315 /// 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
316 /// 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
317 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
318 }
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
319
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
320 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
321 /// 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
322 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
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 /// 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
325 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
326 /// 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
327 /// 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
328 /// will panic.
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
329 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
330 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
331 // 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
332 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
333 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
334 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
335 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
336 }
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
337
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
338 /// The total storage needed for the message, including header.
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
339 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
340 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
341 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
342 }
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
343
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
344 /// 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
345 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
346 /// 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
347 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
348 /// # Safety
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
349 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
350 /// - 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
351 /// - 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
352 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
353 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
354 }
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 /// 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
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 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
359 /// 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
360 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
361 /// 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
362 /// 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
363 /// 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
364 /// 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
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 /// # Safety
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
367 ///
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
368 /// - 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
369 /// - 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
370 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
371 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
372 (&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
373 }
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
374
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
375 /// Zeroes out the data of this payload.
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
376 ///
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
377 /// # Safety
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
378 ///
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
379 /// - 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
380 /// - 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
381 /// 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
382 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
383 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
384 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
385 for x in 0..size {
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
386 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
387 }
118
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
388 }
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
389 }
39760dfc9b3b Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents: 117
diff changeset
390
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
391 /// 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
392 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
393 /// 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
394 /// 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
395 /// [`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
396 /// 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
397 #[derive(Debug)]
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
398 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
399
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
400 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
401 /// Allocates a new OwnedBinaryPayload.
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
402 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
403 /// 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
404 /// (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
405 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
406 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
407 size: data.len(),
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
408 max: BinaryPayload::MAX_SIZE,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
409 })?;
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
410 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
411 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
412 // 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
413 BinaryPayload::fill(
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
414 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
415 data,
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
416 type_,
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
417 );
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
418 Ok(Self(buf))
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
419 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
420
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
421 /// 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
422 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
423 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
424 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
425
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
426 /// 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
427 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
428 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
429 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
430
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
431 /// 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
432 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
433 self.0
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
434 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
435
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
436 /// 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
437 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
438 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
439 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
440
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
441 /// 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
442 /// 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
443 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
444 /// 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
445 /// 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
446 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
447 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
448 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
449
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
450 /// 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
451 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
452 /// # Safety
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
453 ///
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
454 /// 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
455 /// 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
456 /// 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
457 pub unsafe fn from_ptr(ptr: NonNull<BinaryPayload>) -> Self {
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
458 Self(O::from_ptr(ptr.cast(), BinaryPayload::total_bytes(ptr.as_ptr())))
106
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 }
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 #[cfg(test)]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
463 mod tests {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
464 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
465 use std::ptr;
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
466
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
467 type VecPayload = OwnedBinaryPayload<Vec<u8>>;
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
468
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
469 #[test]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
470 fn test_binary_payload() {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
471 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
472 let empty = &[0u8; 5];
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
473
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
474 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
475 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
476 });
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
477 assert_eq!((&[][..], 0x00), unsafe {
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
478 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
479 });
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
480 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
481
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
482 #[test]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
483 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
484 let (data, typ) = (
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
485 &[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
486 112,
106
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 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
489 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
490 let ptr = payload.into_ptr();
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
491 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
492 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
493 }
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
494
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
495 #[test]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
496 #[ignore]
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
497 fn test_owned_too_big() {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
498 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
499 assert_eq!(
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
500 TooBigError {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
501 max: 0xffff_fffa,
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
502 size: 0x1_0000_0001
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
503 },
139
33b9622ed6d2 Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents: 136
diff changeset
504 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
505 )
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
506 }
119
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
507
123
98a624cacd82 Get rid of all the warnings, and arrange attributes.
Paul Fisher <paul@pfish.zone>
parents: 119
diff changeset
508 #[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
509 #[test]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
510 #[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
511 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
512 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
513 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
514 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
515 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
516
123
98a624cacd82 Get rid of all the warnings, and arrange attributes.
Paul Fisher <paul@pfish.zone>
parents: 119
diff changeset
517 #[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
518 #[test]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
519 #[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
520 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
521 unsafe {
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
522 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
523 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
524 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
525
123
98a624cacd82 Get rid of all the warnings, and arrange attributes.
Paul Fisher <paul@pfish.zone>
parents: 119
diff changeset
526 #[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
527 #[test]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
528 #[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
529 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
530 unsafe {
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
531 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
532 }
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
533 }
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 #[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
536 #[test]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
537 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
538 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
539 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
540 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
541 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
542 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
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 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
545 .cloned()
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
546 .collect();
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
547 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
548 .cloned()
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
549 .collect();
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
550 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
551 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
552 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
553 }
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 #[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
556 #[test]
476a22db8639 Add PtrPtrVec to make it easy to pass pointer-to-pointers to PAM.
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
557 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
558 // 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
559 // 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
560 // 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
561 //
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
562 // 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
563 #[repr(C)]
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
564 struct pair(&'static str, i32);
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
565 let boxes = vec![
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
566 Box::new(pair("a", 1)),
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
567 Box::new(pair("b", 2)),
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
568 Box::new(pair("c", 3)),
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
569 Box::new(pair("D", 4)),
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
570 ];
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
571 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
572 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
573 .cloned()
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
574 .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
575 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
576
140
add7228adb2f Neaten up some stuff in libpam-sys memory module.
Paul Fisher <paul@pfish.zone>
parents: 139
diff changeset
577 // 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
578 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
579 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
580 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
581 .cloned()
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 123
diff changeset
582 .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
583 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
584 }
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
585 }