diff options
| author | Florian Weimer <fweimer@redhat.com> | 2022-12-19 18:56:54 +0100 |
|---|---|---|
| committer | Florian Weimer <fweimer@redhat.com> | 2022-12-19 18:56:54 +0100 |
| commit | 659fe9fdd14b0772f4e9722b751b9b010665e053 (patch) | |
| tree | 3098a69345fbd3474154bbba45e8f21de449f266 /stdio-common | |
| parent | ffde06c915d10c0717a0980508ccb28506c6ec63 (diff) | |
| download | glibc-659fe9fdd14b0772f4e9722b751b9b010665e053.tar.xz glibc-659fe9fdd14b0772f4e9722b751b9b010665e053.zip | |
stdio-common: Introduce buffers for implementing printf
These buffers will eventually be used instead of FILE * objects
to implement printf functions. The multibyte buffer is struct
__printf_buffer, the wide buffer is struct __wprintf_buffer.
To enable writing type-generic code, the header files
printf_buffer-char.h and printf_buffer-wchar_t.h define the
Xprintf macro differently, enabling Xprintf (buffer) to stand
for __printf_buffer and __wprintf_buffer as appropriate. For
common cases, macros like Xprintf_buffer are provided as a more
syntactically convenient shortcut.
Buffer-specific flush callbacks are implemented with a switch
statement instead of a function pointer, to avoid hardening issues
similar to those of libio vtables. struct __printf_buffer_as_file
is needed to support custom printf specifiers because the public
interface for that requires passing a FILE *, which is why there
is a trapdoor back from these buffers to FILE * streams.
Since the immediate user of these interfaces knows when processing
has finished, there is no flush callback for the end of processing,
only a flush callback for the intermediate buffer flush.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'stdio-common')
27 files changed, 1241 insertions, 0 deletions
diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 8f2524959d..120d66ea93 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -53,6 +53,14 @@ routines := \ perror \ printf \ printf-prs \ + printf_buffer_as_file \ + printf_buffer_done \ + printf_buffer_flush \ + printf_buffer_pad_1 \ + printf_buffer_putc_1 \ + printf_buffer_puts_1 \ + printf_buffer_to_file \ + printf_buffer_write \ printf_fp \ printf_fphex \ printf_size \ @@ -85,6 +93,14 @@ routines := \ vfwscanf \ vfwscanf-internal \ vprintf \ + wprintf_buffer_as_file \ + wprintf_buffer_done \ + wprintf_buffer_flush \ + wprintf_buffer_pad_1 \ + wprintf_buffer_putc_1 \ + wprintf_buffer_puts_1 \ + wprintf_buffer_to_file \ + wprintf_buffer_write \ # routines aux := \ diff --git a/stdio-common/Xprintf_buffer_done.c b/stdio-common/Xprintf_buffer_done.c new file mode 100644 index 0000000000..a3f51c43ef --- /dev/null +++ b/stdio-common/Xprintf_buffer_done.c @@ -0,0 +1,40 @@ +/* Final status reporting for struct __*printf_buffer. Generic version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <intprops.h> +#include <stdint.h> + +int +Xprintf_buffer_done (struct Xprintf_buffer *buf) +{ + if (Xprintf_buffer_has_failed (buf)) + return -1; + + /* Use uintptr_t here because for sprintf, the buffer range may + cover more than half of the address space. */ + uintptr_t written_current = buf->write_ptr - buf->write_base; + int written_total; + if (INT_ADD_WRAPV (buf->written, written_current, &written_total)) + { + __set_errno (EOVERFLOW); + return -1; + } + else + return written_total; +} diff --git a/stdio-common/Xprintf_buffer_flush.c b/stdio-common/Xprintf_buffer_flush.c new file mode 100644 index 0000000000..1368cfe684 --- /dev/null +++ b/stdio-common/Xprintf_buffer_flush.c @@ -0,0 +1,72 @@ +/* Flush wrapper for struct __*printf_buffer. Generic version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <printf_buffer.h> + +#include <assert.h> +#include <stdint.h> + +/* Xprintf (buffer_do_flush) (BUF) performs the flush operation. The + actual implementation is specific to the multibyte and wide + variants. + + If the flush fails, Xprintf_buffer_mark_failed (BUF) must be + called, and BUF->write_ptr and BUF->write_end can be left + unchanged. + + The function must not do anything if failure has already occurred, + that is, if BUF->mode == Xprintf (buffer_mode_failed). + + The framework implicitly invokes flush with BUF->write_ptr == + BUF->write_end only. (This is particularly relevant to the + __sprintf_chk flush, which just calls __chk_fail.) But in some + cases, Xprintf_buffer_flush may be called explicitly (when + BUF->mode/the backing function is known). In that case, it is + possible that BUF->write_ptr < BUF->write_end is true. + + If the flush succeeds, the pointers are changed so that + BUF->write_ptr < BUF->write_end. It is possible to switch to a + completely different buffer here. If the buffer is moved, it may + be necessary to updated BUF->write_base and BUF->written from the + flush function as well. + + Note that when chaining buffers, in the flush function for the + outer buffer (to which data is written first), it is necessary to + check for BUF->next->failed (for the inner buffer) and set + BUF->base.failed to true (for the outer buffer). This should come + towards the end of the outer flush function. Usually, there is + also some unwrapping step afterwards; it has to check the outer + buffer (BUF->base.failed) and propagate any error to the inner + buffer (BUF->next->failed), so essentially in the other + direction. */ +static void Xprintf (buffer_do_flush) (struct Xprintf_buffer *buf); + +bool +Xprintf_buffer_flush (struct Xprintf_buffer *buf) +{ + if (__glibc_unlikely (Xprintf_buffer_has_failed (buf))) + return false; + + Xprintf (buffer_do_flush) (buf); + if (Xprintf_buffer_has_failed (buf)) + return false; + + /* Ensure that the flush has made available some bytes. */ + assert (buf->write_ptr != buf->write_end); + return true; +} diff --git a/stdio-common/Xprintf_buffer_pad_1.c b/stdio-common/Xprintf_buffer_pad_1.c new file mode 100644 index 0000000000..2be8270ad2 --- /dev/null +++ b/stdio-common/Xprintf_buffer_pad_1.c @@ -0,0 +1,44 @@ +/* Write repeated characters to struct __*printf_buffer. Generic version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <assert.h> +#include <string.h> + +void +Xprintf (buffer_pad_1) (struct Xprintf_buffer *buf, CHAR_T ch, size_t count) +{ + if (__glibc_unlikely (Xprintf_buffer_has_failed (buf))) + return; + + do + { + /* Proactively make room. __*printf_buffer_pad has already + checked for a zero-length write, so this function is only + called when there is actually data to write. */ + if (buf->write_ptr == buf->write_end && !Xprintf_buffer_flush (buf)) + return; + assert (buf->write_ptr != buf->write_end); + size_t to_fill = buf->write_end - buf->write_ptr; + if (to_fill > count) + to_fill = count; + MEMSET (buf->write_ptr, ch, to_fill); + buf->write_ptr += to_fill; + count -= to_fill; + } + while (count > 0); +} diff --git a/stdio-common/Xprintf_buffer_putc_1.c b/stdio-common/Xprintf_buffer_putc_1.c new file mode 100644 index 0000000000..3458775a1c --- /dev/null +++ b/stdio-common/Xprintf_buffer_putc_1.c @@ -0,0 +1,29 @@ +/* Overflow write function for struct __*printf_buffer. Generic version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <assert.h> + +void +Xprintf (buffer_putc_1) (struct Xprintf_buffer *buf, CHAR_T ch) +{ + if (__glibc_unlikely (Xprintf_buffer_has_failed (buf)) + || !Xprintf_buffer_flush (buf)) + return; + assert (buf->write_ptr < buf->write_end); + *buf->write_ptr++ = ch; +} diff --git a/stdio-common/Xprintf_buffer_puts_1.c b/stdio-common/Xprintf_buffer_puts_1.c new file mode 100644 index 0000000000..88d0b8b2bd --- /dev/null +++ b/stdio-common/Xprintf_buffer_puts_1.c @@ -0,0 +1,38 @@ +/* String write function for struct __*printf_buffer. Generic version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <assert.h> +#include <string.h> + +void +Xprintf (buffer_puts_1) (struct Xprintf_buffer *buf, const CHAR_T *s) +{ + if (__glibc_unlikely (Xprintf_buffer_has_failed (buf))) + return; + + while (*s != 0) + { + if (buf->write_ptr == buf->write_end && !Xprintf_buffer_flush (buf)) + return; + assert (buf->write_ptr != buf->write_end); + size_t to_copy = STRNLEN (s, buf->write_end - buf->write_ptr); + MEMCPY (buf->write_ptr, s, to_copy); + buf->write_ptr += to_copy; + s += to_copy; + } +} diff --git a/stdio-common/Xprintf_buffer_write.c b/stdio-common/Xprintf_buffer_write.c new file mode 100644 index 0000000000..9db2b5066a --- /dev/null +++ b/stdio-common/Xprintf_buffer_write.c @@ -0,0 +1,44 @@ +/* Blob write function for struct __*printf_buffer. Generic version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <printf_buffer.h> + +#include <assert.h> +#include <string.h> + +void +Xprintf_buffer_write (struct Xprintf_buffer *buf, + const CHAR_T *s, size_t count) +{ + if (__glibc_unlikely (Xprintf_buffer_has_failed (buf))) + return; + + while (count > 0) + { + if (buf->write_ptr == buf->write_end && !Xprintf_buffer_flush (buf)) + return; + assert (buf->write_ptr != buf->write_end); + size_t to_copy = buf->write_end - buf->write_ptr; + if (to_copy > count) + to_copy = count; + MEMCPY (buf->write_ptr, s, to_copy); + buf->write_ptr += to_copy; + s += to_copy; + count -= to_copy; + } +} diff --git a/stdio-common/printf_buffer-char.h b/stdio-common/printf_buffer-char.h new file mode 100644 index 0000000000..44264e24c0 --- /dev/null +++ b/stdio-common/printf_buffer-char.h @@ -0,0 +1,24 @@ +/* Macros for the multibyte (char) implementation of struct __printf_buffer. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#define Xprintf(n) __printf_##n + +#define CHAR_T char +#define MEMCPY memcpy +#define MEMSET memset +#define STRNLEN __strnlen diff --git a/stdio-common/printf_buffer-wchar_t.h b/stdio-common/printf_buffer-wchar_t.h new file mode 100644 index 0000000000..d8d0c5da46 --- /dev/null +++ b/stdio-common/printf_buffer-wchar_t.h @@ -0,0 +1,24 @@ +/* Macros for wide (wchar_t) implementation of struct __wprintf_buffer. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#define Xprintf(n) __wprintf_##n + +#define CHAR_T wchar_t +#define MEMCPY __wmemcpy +#define MEMSET __wmemset +#define STRNLEN __wcsnlen diff --git a/stdio-common/printf_buffer_as_file.c b/stdio-common/printf_buffer_as_file.c new file mode 100644 index 0000000000..f27b000d78 --- /dev/null +++ b/stdio-common/printf_buffer_as_file.c @@ -0,0 +1,148 @@ +/* FILE * interface to a struct __printf_buffer. Multibyte version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <printf_buffer_as_file.h> + +#include <assert.h> +#include <printf_buffer.h> + +/* Commit the data directly written through the stdio stream. */ +static void +__printf_buffer_as_file_commit (struct __printf_buffer_as_file *file) +{ + /* Check that the write pointers in the file stream are consistent + with the next buffer. */ + assert (file->stream._IO_wri |
