pep_engine_sequoia_backend 1.1.0

An implementation of the p≡p Engine's cryptotech interface using Sequoia.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! A reimplementation of the engine's stringlist module in Rust.
//!
//! We could call out to the implementation in the engine, however,
//! then it would only be possible to use this crate when also linking
//! to the engine.  This would mean that the CLI and tests would need
//! to link to the engine, which is undesirable.
//!
//! We only implement the functionality that we actually use:
//!
//!   - new_stringlist
//!   - stringlist_length
//!   - stringlist_add
//!   - stringlist_add_unique
//!   - stringlist_append

use std::ptr;
use std::ffi::CStr;

use libc::c_char;

use crate::ffi::MM;
use crate::buffer::{
    malloc_cleared,
    rust_str_to_c_str,
};

#[repr(C)]
pub struct StringListItem {
    value: *mut c_char,
    next: *mut StringListItem,
}

impl StringListItem {
    /// Allocates a new string item with no value (i.e., NULL).
    ///
    /// The memory is allocated using the libc allocator.  The caller
    /// is responsible for freeing it explicitly.
    fn empty(mm: MM) -> &'static mut Self {
        let buffer = if let Ok(buffer) = malloc_cleared::<Self>(mm) {
            buffer
        } else {
            panic!("Out of memory allocating a StringListItem");
        };
        unsafe { &mut *(buffer as *mut Self) }
    }

    /// Allocates a new string item with the specified value and next
    /// pointer.
    ///
    /// The memory is allocated using the libc allocator.  The caller
    /// is responsible for freeing it explicitly.
    fn new<S: AsRef<str>>(mm: MM, value: S, next: *mut Self) -> &'static mut Self {
        let item = Self::empty(mm);

        item.value = rust_str_to_c_str(mm, value)
            .expect("Out of memory allocating StringListItem");
        item.next = next;

        item
    }

    /// Converts the raw pointer to a Rust reference.
    ///
    /// This does not take ownership of the object.
    fn as_mut(ptr: *mut Self) -> Option<&'static mut Self> {
        unsafe { ptr.as_mut() }
    }
}

// We wrap the StringList in a Rust object, because NULL is a valid
// stringlist_t (it's an empty string list).
pub struct StringList {
    head: *mut StringListItem,
    // If set, when the StringList is dropped, the items are freed.
    owned: bool,
    mm: MM,
}

impl StringList {
    /// Converts the raw pointer to a Rust object.
    ///
    /// `owned` indicates whether the rust code should own the items.
    /// If so, when the `StringList` is dropped, the items will also
    /// be freed.
    pub fn to_rust(mm: MM, sl: *mut StringListItem, owned: bool) -> Self
    {
        StringList {
            head: sl,
            owned,
            mm,
        }
    }

    /// Converts the Rust object to a raw pointer.
    ///
    /// The items are owned by the raw pointer and need to be freed
    /// explicitly using libc's `free`.
    pub fn to_c(mut self) -> *mut StringListItem {
        std::mem::replace(&mut self.head, ptr::null_mut())
    }

    /// Creates a new string list.
    ///
    /// The items are owned by the `StringList`, and when it is
    /// dropped, they are freed.  To take ownership of the items, call
    /// `StringList::to_c`.
    pub fn new<S: AsRef<str>>(mm: MM, value: S) -> Self {
        StringList {
            head: StringListItem::new(mm, value, ptr::null_mut()),
            owned: true,
            mm,
        }
    }

    /// Creates a new, empty string list.
    ///
    /// Any added items are owned by the `StringList`, and when it is
    /// dropped, they are freed.  To take ownership of the items, call
    /// `StringList::to_c`.
    pub fn empty(mm: MM) -> Self
    {
        StringList {
            head: ptr::null_mut(),
            owned: true,
            mm,
        }
    }

    /// There are two ways to make an empty list.  Either head is NULL
    /// or the first element's value is NULL.  This creates the second
    /// variant, which we use for testing.
    #[cfg(test)]
    fn empty_alt() -> Self {
        let mm = MM { malloc: libc::malloc, free: libc::free };

        StringList {
            head: StringListItem::empty(mm),
            owned: true,
            mm,
        }
    }

    /// Returns an iterator over the items.
    pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'a CStr> {
        StringListIter {
            item: &self.head,
        }
    }

    /// Returns a mutable iterator over the items.
    pub fn iter_mut<'a>(&'a mut self) -> StringListIterMut {
        StringListIterMut {
            item: &mut self.head,
        }
    }

    /// Returns the number of items in the list.
    pub fn len(&self) -> usize {
        self.iter().count()
    }

    fn add_<S: AsRef<str>>(&mut self, value: S, dedup: bool) {
        let mm = self.mm;

        let value = value.as_ref();

        // See if the value already exists in the string list.
        let mut iter = self.iter_mut();
        for i in &mut iter {
            if dedup && i.to_bytes() == value.as_bytes() {
                return;
            }
        }

        // It's not present yet.  Add it.

        // There are three cases to consider:
        let itemp = iter.item();
        if (*itemp).is_null() {
            // 1. head is NULL (this is the case if item is NULL).
            *itemp = StringListItem::new(mm, value, ptr::null_mut());
        } else {
            let item: &mut StringListItem
                = StringListItem::as_mut(*itemp).expect("just checked");

            if item.value.is_null() {
                // 2. head is not NULL, but head.value is NULL.
                item.value = rust_str_to_c_str(mm, value)
                    .expect("Out of memory allocating StringList.value");
            } else {
                // 3. neither head nor head.value are NULL.
                assert!(item.next.is_null());
                item.next = StringListItem::new(mm, value, ptr::null_mut());
            }
        }
    }

    /// Appends the item to the list.
    ///
    /// The item's ownership is determined by the list's ownership
    /// property.
    pub fn add<S: AsRef<str>>(&mut self, value: S) {
        self.add_(value, false)
    }

    /// Appends the item to the list if it isn't already present.
    ///
    /// The item's ownership is determined by the list's ownership
    /// property.
    pub fn add_unique<S: AsRef<str>>(&mut self, value: S) {
        self.add_(value, true)
    }

    /// Appends `other` to the list.
    ///
    /// The items in other have the same ownership as items in `self`.
    /// `other` is reset to an empty list.
    pub fn append(&mut self, other: &mut StringList) {
        let free = self.mm.free;

        let mut iter = self.iter_mut();
        (&mut iter).last();

        // There are three cases to consider:
        let itemp = iter.item();
        if (*itemp).is_null() {
            // 1. head is NULL (this is the case if item is NULL).
            *itemp = other.head;
        } else {
            let item: &mut StringListItem
                = StringListItem::as_mut(*itemp).expect("just checked");

            if item.value.is_null() {
                // 2. head is not NULL, but head.value is NULL.
                unsafe { free((*itemp) as *mut _) };
                *itemp = other.head;
            } else {
                // 3. neither head nor head.value are NULL.
                assert!(item.next.is_null());
                item.next = other.head;
            }
        }

        other.head = ptr::null_mut();
    }
}

impl Drop for StringList {
    fn drop(&mut self) {
        let free = self.mm.free;

        let mut curr: *mut StringListItem = self.head;
        self.head = ptr::null_mut();

        if self.owned {
            loop {
                let next = if let Some(curr) = StringListItem::as_mut(curr) {
                    let next = curr.next;
                    unsafe { free(curr.value as *mut _) };
                    curr.value = ptr::null_mut();
                    next
                } else {
                    break;
                };

                unsafe { free(curr as *mut _) };
                curr = next;
            }
        }
    }
}

pub struct StringListIterMut<'a> {
    item: &'a mut *mut StringListItem,
}

impl<'a> StringListIterMut<'a> {
    /// Returns a reference to the StringListItem that will be
    /// returned next, or, if none, then a reference to the last
    /// StringListItem.  If the list is empty, this returns a
    /// reference to the initial pointer, which will be NULL.
    fn item(&'a mut self) -> &'a mut *mut StringListItem {
        self.item
    }
}

impl<'a> Iterator for StringListIterMut<'a> {
    type Item = &'a CStr;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(item) = StringListItem::as_mut(*self.item) {
            if item.value.is_null() {
                None
            } else {
                self.item = &mut item.next;
                Some(unsafe { CStr::from_ptr(item.value) })
            }
        } else {
            None
        }
    }
}

pub struct StringListIter<'a> {
    item: &'a *mut StringListItem,
}

impl<'a> Iterator for StringListIter<'a> {
    type Item = &'a CStr;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(item) = StringListItem::as_mut(*self.item) {
            if item.value.is_null() {
                None
            } else {
                self.item = &item.next;
                Some(unsafe { CStr::from_ptr(item.value) })
            }
        } else {
            None
        }
    }
}

impl<'a> IntoIterator for &'a StringList {
    type Item = &'a CStr;
    type IntoIter = StringListIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        StringListIter {
            item: &self.head,
        }
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty() {
        let mm = MM { malloc: libc::malloc, free: libc::free };

        // There are two ways to make an empty list.  Either head is
        // NULL or the string list item's value and next are NULL.
        let empty = StringList {
            head: ptr::null_mut(),
            owned: true,
            mm: mm,
        };
        assert_eq!(empty.len(), 0);

        let empty = StringList {
            head: StringListItem::empty(mm),
            owned: true,
            mm: mm,
        };
        assert_eq!(empty.len(), 0);
    }

    #[test]
    fn add() {
        let mm = MM { malloc: libc::malloc, free: libc::free };

        for variant in 0..3 {
            let (mut list, mut v) = match variant {
                0 => {
                    let list = StringList::new(mm, "abc");
                    assert_eq!(list.len(), 1);

                    let mut v: Vec<String> = Vec::new();
                    v.push("abc".into());

                    (list, v)
                },
                1 => (StringList::empty(mm), Vec::new()),
                2 => (StringList::empty_alt(), Vec::new()),
                _ => unreachable!(),
            };

            let mut add_one = |s: String| {
                list.add(&s);
                v.push(s);

                assert_eq!(list.len(), v.len());
                assert_eq!(
                    &list
                        .iter()
                        .map(|s| String::from(s.to_str().unwrap()))
                        .collect::<Vec<String>>(),
                    &v);
            };

            for i in 1..100 {
                add_one(format!("{}", i));
            }
        }
    }

    #[test]
    fn add_unique() {
        let mm = MM { malloc: libc::malloc, free: libc::free };

        for variant in 0..3 {
            let (mut list, mut v) = match variant {
                0 => {
                    let list = StringList::new(mm, "abc");
                    assert_eq!(list.len(), 1);

                    let mut v: Vec<String> = Vec::new();
                    v.push("abc".into());

                    (list, v)
                },
                1 => (StringList::empty(mm), Vec::new()),
                2 => (StringList::empty_alt(), Vec::new()),
                _ => unreachable!(),
            };

            let mut add_one = |s: String| {
                list.add_unique(&s);
                // Add adds to the back.
                if v.iter().find(|&x| x == &s).is_none() {
                    v.push(s);
                }

                assert_eq!(list.len(), v.len());
                assert_eq!(
                    &list
                        .iter()
                        .map(|s| String::from(s.to_str().unwrap()))
                        .collect::<Vec<String>>(),
                    &v);
            };

            for i in 1..13 {
                add_one(format!("{}", i));
            }
            for i in 1..19 {
                add_one(format!("{}", i));
            }
            for i in 1..19 {
                add_one(format!("{}", i));
            }
        }
    }

    #[test]
    fn append() {
        let mm = MM { malloc: libc::malloc, free: libc::free };

        for variant in 0..2 {
            // Returns a list and a vector with `count` items whose
            // values are `prefix_0`, `prefix_1`, etc.
            let list = |count: usize, prefix: &str| -> (StringList, Vec<String>) {
                let mut l = match variant {
                    0 => StringList::empty(mm),
                    1 => StringList::empty_alt(),
                    _ => unreachable!(),
                };

                let mut v = Vec::new();
                for i in 0..count {
                    let value = format!("{}_{}", prefix, i);
                    l.add(&value);
                    v.push(value);
                }
                (l, v)
            };

            for i in 0..10 {
                for j in 0..10 {
                    let (mut a, mut av) = list(i, "a");
                    let (mut b, mut bv) = list(j, "b");

                    a.append(&mut b);
                    assert_eq!(a.len(), i + j);

                    av.append(&mut bv);
                    assert_eq!(av.len(), i + j);

                    for (i, (a, av)) in a.iter().zip(av.iter()).enumerate() {
                        assert_eq!(a.to_bytes(), av.as_bytes(),
                                   "index: {}", i);
                    }
                }
            }
        }
    }
}