taproot-c-scape 0.22.5

A libc bottom-half implementation in Rust (c-scape fork for the taproot libc)
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
//! The `printf` formatting engine, walking the va.rs tag on stable Rust.
//!
//! Vendored from the printf-compat crate, version 0.4.0, by lights0123
//! (<https://github.com/lights0123/printf-compat>), licensed MIT OR
//! Apache-2.0. The port replaces the crate's nightly `VaList` argument
//! source with `&mut crate::va::VaListTag` (each `next_arg` call site
//! becomes a `tag.arg::<T>()` call), drops the `VaList`-holding `display`
//! adapter and the std-only `io_write` adapter that c-scape never used,
//! and replaces the parser's `itertools::tuple_windows` walk with a
//! peekable iterator. The parsing and formatting behavior is otherwise
//! unchanged, including its documented differences from glibc (see
//! [`output::fmt_write`]).
//!
//! This module is x86_64-only, like the walker it consumes; other
//! architectures keep formatting through the printf-compat crate itself
//! on the nightly `VaList` path.

use core::{ffi::*, fmt};

pub mod output;
mod parser;
use argument::*;
pub use parser::format;

pub mod argument {
    use super::*;

    bitflags::bitflags! {
        /// Flags field.
        ///
        /// Definitions from
        /// [Wikipedia](https://en.wikipedia.org/wiki/Printf_format_string#Flags_field).
        #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
        pub struct Flags: u8 {
            /// Left-align the output of this placeholder. (The default is to
            /// right-align the output.)
            const LEFT_ALIGN = 0b00000001;
            /// Prepends a plus for positive signed-numeric types. positive =
            /// `+`, negative = `-`.
            ///
            /// (The default doesn't prepend anything in front of positive
            /// numbers.)
            const PREPEND_PLUS = 0b00000010;
            /// Prepends a space for positive signed-numeric types. positive = `
            /// `, negative = `-`. This flag is ignored if the
            /// [`PREPEND_PLUS`][Flags::PREPEND_PLUS] flag exists.
            ///
            /// (The default doesn't prepend anything in front of positive
            /// numbers.)
            const PREPEND_SPACE = 0b00000100;
            /// When the 'width' option is specified, prepends zeros for numeric
            /// types. (The default prepends spaces.)
            ///
            /// For example, `printf("%4X",3)` produces `   3`, while
            /// `printf("%04X",3)` produces `0003`.
            const PREPEND_ZERO = 0b00001000;
            /// The integer or exponent of a decimal has the thousands grouping
            /// separator applied.
            const THOUSANDS_GROUPING = 0b00010000;
            /// Alternate form:
            ///
            /// For `g` and `G` types, trailing zeros are not removed. \
            /// For `f`, `F`, `e`, `E`, `g`, `G` types, the output always
            /// contains a decimal point. \ For `o`, `x`, `X` types,
            /// the text `0`, `0x`, `0X`, respectively, is prepended
            /// to non-zero numbers.
            const ALTERNATE_FORM = 0b00100000;
        }
    }

    #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
    pub enum DoubleFormat {
        /// `f`
        Normal,
        /// `F`
        UpperNormal,
        /// `e`
        Scientific,
        /// `E`
        UpperScientific,
        /// `g`
        Auto,
        /// `G`
        UpperAuto,
        /// `a`
        Hex,
        /// `A`
        UpperHex,
    }

    impl DoubleFormat {
        /// If the format is uppercase.
        pub fn is_upper(self) -> bool {
            use DoubleFormat::*;
            matches!(self, UpperNormal | UpperScientific | UpperAuto | UpperHex)
        }

        pub fn set_upper(self, upper: bool) -> Self {
            use DoubleFormat::*;
            match self {
                Normal | UpperNormal => {
                    if upper {
                        UpperNormal
                    } else {
                        Normal
                    }
                }
                Scientific | UpperScientific => {
                    if upper {
                        UpperScientific
                    } else {
                        Scientific
                    }
                }
                Auto | UpperAuto => {
                    if upper {
                        UpperAuto
                    } else {
                        Auto
                    }
                }
                Hex | UpperHex => {
                    if upper {
                        UpperHex
                    } else {
                        Hex
                    }
                }
            }
        }
    }

    #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
    #[non_exhaustive]
    pub enum SignedInt {
        Int(c_int),
        Char(c_schar),
        Short(c_short),
        Long(c_long),
        LongLong(c_longlong),
        Isize(isize),
    }

    impl From<SignedInt> for i64 {
        fn from(num: SignedInt) -> Self {
            // Some casts are only needed on some platforms.
            #[allow(clippy::unnecessary_cast)]
            match num {
                SignedInt::Int(x) => x as i64,
                SignedInt::Char(x) => x as i64,
                SignedInt::Short(x) => x as i64,
                SignedInt::Long(x) => x as i64,
                SignedInt::LongLong(x) => x as i64,
                SignedInt::Isize(x) => x as i64,
            }
        }
    }

    impl SignedInt {
        pub fn is_sign_negative(self) -> bool {
            match self {
                SignedInt::Int(x) => x < 0,
                SignedInt::Char(x) => x < 0,
                SignedInt::Short(x) => x < 0,
                SignedInt::Long(x) => x < 0,
                SignedInt::LongLong(x) => x < 0,
                SignedInt::Isize(x) => x < 0,
            }
        }
    }

    impl fmt::Display for SignedInt {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                SignedInt::Int(x) => fmt::Display::fmt(x, f),
                SignedInt::Char(x) => fmt::Display::fmt(x, f),
                SignedInt::Short(x) => fmt::Display::fmt(x, f),
                SignedInt::Long(x) => fmt::Display::fmt(x, f),
                SignedInt::LongLong(x) => fmt::Display::fmt(x, f),
                SignedInt::Isize(x) => fmt::Display::fmt(x, f),
            }
        }
    }

    #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
    #[non_exhaustive]
    pub enum UnsignedInt {
        Int(c_uint),
        Char(c_uchar),
        Short(c_ushort),
        Long(c_ulong),
        LongLong(c_ulonglong),
        Isize(usize),
    }

    impl From<UnsignedInt> for u64 {
        fn from(num: UnsignedInt) -> Self {
            // Some casts are only needed on some platforms.
            #[allow(clippy::unnecessary_cast)]
            match num {
                UnsignedInt::Int(x) => x as u64,
                UnsignedInt::Char(x) => x as u64,
                UnsignedInt::Short(x) => x as u64,
                UnsignedInt::Long(x) => x as u64,
                UnsignedInt::LongLong(x) => x as u64,
                UnsignedInt::Isize(x) => x as u64,
            }
        }
    }

    impl fmt::Display for UnsignedInt {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                UnsignedInt::Int(x) => fmt::Display::fmt(x, f),
                UnsignedInt::Char(x) => fmt::Display::fmt(x, f),
                UnsignedInt::Short(x) => fmt::Display::fmt(x, f),
                UnsignedInt::Long(x) => fmt::Display::fmt(x, f),
                UnsignedInt::LongLong(x) => fmt::Display::fmt(x, f),
                UnsignedInt::Isize(x) => fmt::Display::fmt(x, f),
            }
        }
    }

    impl fmt::LowerHex for UnsignedInt {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                UnsignedInt::Int(x) => fmt::LowerHex::fmt(x, f),
                UnsignedInt::Char(x) => fmt::LowerHex::fmt(x, f),
                UnsignedInt::Short(x) => fmt::LowerHex::fmt(x, f),
                UnsignedInt::Long(x) => fmt::LowerHex::fmt(x, f),
                UnsignedInt::LongLong(x) => fmt::LowerHex::fmt(x, f),
                UnsignedInt::Isize(x) => fmt::LowerHex::fmt(x, f),
            }
        }
    }

    impl fmt::UpperHex for UnsignedInt {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                UnsignedInt::Int(x) => fmt::UpperHex::fmt(x, f),
                UnsignedInt::Char(x) => fmt::UpperHex::fmt(x, f),
                UnsignedInt::Short(x) => fmt::UpperHex::fmt(x, f),
                UnsignedInt::Long(x) => fmt::UpperHex::fmt(x, f),
                UnsignedInt::LongLong(x) => fmt::UpperHex::fmt(x, f),
                UnsignedInt::Isize(x) => fmt::UpperHex::fmt(x, f),
            }
        }
    }

    impl fmt::Octal for UnsignedInt {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                UnsignedInt::Int(x) => fmt::Octal::fmt(x, f),
                UnsignedInt::Char(x) => fmt::Octal::fmt(x, f),
                UnsignedInt::Short(x) => fmt::Octal::fmt(x, f),
                UnsignedInt::Long(x) => fmt::Octal::fmt(x, f),
                UnsignedInt::LongLong(x) => fmt::Octal::fmt(x, f),
                UnsignedInt::Isize(x) => fmt::Octal::fmt(x, f),
            }
        }
    }

    /// An argument as passed to [`format()`].
    #[derive(Debug, Copy, Clone, PartialEq)]
    pub struct Argument<'a> {
        pub flags: Flags,
        pub width: c_int,
        pub precision: Option<c_int>,
        pub specifier: Specifier<'a>,
    }

    impl<'a> From<Specifier<'a>> for Argument<'a> {
        fn from(specifier: Specifier<'a>) -> Self {
            Self {
                flags: Flags::empty(),
                width: 0,
                precision: None,
                specifier,
            }
        }
    }

    /// A [format specifier](https://en.wikipedia.org/wiki/Printf_format_string#Type_field).
    #[derive(Debug, Copy, Clone, PartialEq)]
    #[non_exhaustive]
    pub enum Specifier<'a> {
        /// `%`
        Percent,
        /// `d`, `i`
        Int(SignedInt),
        /// `u`
        Uint(UnsignedInt),
        /// `o`
        Octal(UnsignedInt),
        /// `f`, `F`, `e`, `E`, `g`, `G`, `a`, `A`
        Double { value: f64, format: DoubleFormat },
        /// string outside of formatting
        Bytes(&'a [u8]),
        /// `s`
        ///
        /// The same as [`Bytes`][Specifier::Bytes] but guaranteed to be
        /// null-terminated. This can be used for optimizations, where if you
        /// need to null terminate a string to print it, you can skip that step.
        String(&'a CStr),
        /// `c`
        Char(c_char),
        /// `x`
        Hex(UnsignedInt),
        /// `X`
        UpperHex(UnsignedInt),
        /// `p`
        Pointer(*const ()),
        /// `n`
        ///
        /// # Safety
        ///
        /// This can be a serious security vulnerability if the format specifier
        /// of `printf` is allowed to be user-specified. This shouldn't ever
        /// happen, but poorly-written software may do so.
        WriteBytesWritten(c_int, *const c_int),
    }
}

#[cfg(test)]
mod tests {
    use super::{format, output};
    use crate::va::{vararg_entry, VaListTag};
    use alloc::string::String;
    use libc::{c_char, c_int};

    /// The implementation half of the test entry: run the REAL formatting
    /// path (tag walk, parser, formatter) into a byte buffer, NUL
    /// terminate it, and hand back `format`'s return value.
    unsafe extern "C" fn fmt_into_impl(
        out: *mut u8,
        cap: usize,
        fmt: *const c_char,
        tag: *mut VaListTag,
    ) -> c_int {
        // SAFETY: the entry hands us a live tag, and every test call site
        // passes arguments matching its format string and a buffer with
        // room for the formatted bytes plus a NUL.
        unsafe {
            let mut s = String::new();
            let n = format(fmt, &mut *tag, output::fmt_write(&mut s));
            if n < 0 {
                return n;
            }
            let bytes = s.as_bytes();
            assert!(bytes.len() < cap, "test buffer too small");
            core::ptr::copy_nonoverlapping(bytes.as_ptr(), out, bytes.len());
            *out.add(bytes.len()) = 0;
            n
        }
    }

    vararg_entry! {
        #[no_mangle]
        unsafe extern "C" fn __taproot_printf_test_fmt(
            out: *mut u8,
            cap: usize,
            fmt: *const c_char,
            ...
        ) -> c_int => fmt_into_impl
    }

    /// The C-side view of the entry, as in va.rs's suite: calling through
    /// this makes each test a real-ABI round trip, with rustc performing
    /// the caller half of the variadic protocol.
    mod decl {
        use libc::{c_char, c_int};

        unsafe extern "C" {
            pub fn __taproot_printf_test_fmt(
                out: *mut u8,
                cap: usize,
                fmt: *const c_char,
                ...
            ) -> c_int;
        }
    }

    fn formatted(buf: &[u8], n: c_int) -> &[u8] {
        assert!(n >= 0, "formatting failed: {n}");
        let n = n as usize;
        assert_eq!(buf[n], 0, "missing NUL terminator");
        &buf[..n]
    }

    #[test]
    fn formats_d_s_f_exactly() {
        let mut buf = [0xAA_u8; 64];
        let n = unsafe {
            decl::__taproot_printf_test_fmt(
                buf.as_mut_ptr(),
                buf.len(),
                c"%d %s %.2f".as_ptr(),
                42 as c_int,
                c"carrot".as_ptr(),
                3.14159_f64,
            )
        };
        assert_eq!(formatted(&buf, n), b"42 carrot 3.14");
        assert_eq!(n, 14);
    }

    #[test]
    fn width_precision_and_alignment() {
        // A zero-padded width on a precision'd double, a left-aligned
        // width on an int, and a right-aligned width on a string.
        let mut buf = [0xAA_u8; 64];
        let n = unsafe {
            decl::__taproot_printf_test_fmt(
                buf.as_mut_ptr(),
                buf.len(),
                c"%08.3f|%-6d|%5s".as_ptr(),
                2.5_f64,
                42 as c_int,
                c"ab".as_ptr(),
            )
        };
        assert_eq!(formatted(&buf, n), b"0002.500|42    |   ab");
    }

    #[test]
    fn star_width_comes_off_the_walk() {
        // `%*d` fetches its width as an argument before the value.
        let mut buf = [0xAA_u8; 32];
        let n = unsafe {
            decl::__taproot_printf_test_fmt(
                buf.as_mut_ptr(),
                buf.len(),
                c"%*d".as_ptr(),
                5 as c_int,
                42 as c_int,
            )
        };
        assert_eq!(formatted(&buf, n), b"   42");
    }

    #[test]
    fn args_cross_the_overflow_area() {
        // Three named INTEGER arguments plus seven variadic ints exhaust
        // the six GP registers mid-walk, and the double rides xmm0: the
        // formatter's fetches must agree with the walker across both the
        // register and stack areas.
        let mut buf = [0xAA_u8; 64];
        let n = unsafe {
            decl::__taproot_printf_test_fmt(
                buf.as_mut_ptr(),
                buf.len(),
                c"%d %d %d %d %d %d %d %.1f".as_ptr(),
                1 as c_int,
                2 as c_int,
                3 as c_int,
                4 as c_int,
                5 as c_int,
                6 as c_int,
                7 as c_int,
                8.5_f64,
            )
        };
        assert_eq!(formatted(&buf, n), b"1 2 3 4 5 6 7 8.5");
    }

    #[test]
    fn percent_n_is_rejected() {
        // The vendored formatter parses `%n` but refuses to write through
        // it, reporting an error instead; the guard c-scape's `__*_chk`
        // comments rely on.
        let mut sink: c_int = 0;
        let mut buf = [0xAA_u8; 32];
        let n = unsafe {
            decl::__taproot_printf_test_fmt(
                buf.as_mut_ptr(),
                buf.len(),
                c"a%nb".as_ptr(),
                &mut sink as *mut c_int,
            )
        };
        assert_eq!(n, -1);
        assert_eq!(sink, 0);
    }
}