2018-06-26 18:20:44 +08:00
|
|
|
/* Test for libio vtables and their validation. Common code.
|
2024-01-02 02:12:26 +08:00
|
|
|
Copyright (C) 2018-2024 Free Software Foundation, Inc.
|
2018-06-26 18:20:44 +08:00
|
|
|
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
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2018-06-26 18:20:44 +08:00
|
|
|
|
|
|
|
/* This test provides some coverage for how various stdio functions
|
|
|
|
use the vtables in FILE * objects. The focus is mostly on which
|
|
|
|
functions call which methods, not so much on validating data
|
|
|
|
processing. An initial series of tests check that custom vtables
|
|
|
|
do not work without activation through _IO_init.
|
|
|
|
|
|
|
|
Note: libio vtables are deprecated feature. Do not use this test
|
|
|
|
as a documentation source for writing custom vtables. See
|
|
|
|
fopencookie for a different way of creating custom stdio
|
|
|
|
streams. */
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <support/capture_subprocess.h>
|
|
|
|
#include <support/check.h>
|
|
|
|
#include <support/namespace.h>
|
|
|
|
#include <support/support.h>
|
|
|
|
#include <support/test-driver.h>
|
|
|
|
#include <support/xunistd.h>
|
|
|
|
|
|
|
|
#include "libioP.h"
|
|
|
|
|
|
|
|
/* Data shared between the test subprocess and the test driver in the
|
|
|
|
parent. Note that *shared is reset at the start of the check_call
|
|
|
|
function. */
|
|
|
|
struct shared
|
|
|
|
{
|
|
|
|
/* Expected file pointer for method calls. */
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
/* If true, assume that a call to _IO_init is needed to enable
|
|
|
|
custom vtables. */
|
|
|
|
bool initially_disabled;
|
|
|
|
|
|
|
|
/* Requested return value for the methods which have one. */
|
|
|
|
int return_value;
|
|
|
|
|
|
|
|
/* A value (usually a character) recorded by some of the methods
|
|
|
|
below. */
|
|
|
|
int value;
|
|
|
|
|
|
|
|
/* Likewise, for some data. */
|
|
|
|
char buffer[16];
|
|
|
|
size_t buffer_length;
|
|
|
|
|
|
|
|
/* Total number of method calls. */
|
|
|
|
unsigned int calls;
|
|
|
|
|
|
|
|
/* Individual method call counts. */
|
|
|
|
unsigned int calls_finish;
|
|
|
|
unsigned int calls_overflow;
|
|
|
|
unsigned int calls_underflow;
|
|
|
|
unsigned int calls_uflow;
|
|
|
|
unsigned int calls_pbackfail;
|
|
|
|
unsigned int calls_xsputn;
|
|
|
|
unsigned int calls_xsgetn;
|
|
|
|
unsigned int calls_seekoff;
|
|
|
|
unsigned int calls_seekpos;
|
|
|
|
unsigned int calls_setbuf;
|
|
|
|
unsigned int calls_sync;
|
|
|
|
unsigned int calls_doallocate;
|
|
|
|
unsigned int calls_read;
|
|
|
|
unsigned int calls_write;
|
|
|
|
unsigned int calls_seek;
|
|
|
|
unsigned int calls_close;
|
|
|
|
unsigned int calls_stat;
|
|
|
|
unsigned int calls_showmanyc;
|
|
|
|
unsigned int calls_imbue;
|
|
|
|
} *shared;
|
|
|
|
|
|
|
|
/* Method implementations which increment the counters in *shared. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
log_method (FILE *fp, const char *name)
|
|
|
|
{
|
|
|
|
if (test_verbose > 0)
|
|
|
|
printf ("info: %s (%p) called\n", name, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
method_finish (FILE *fp, int dummy)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
method_overflow (FILE *fp, int ch)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_overflow;
|
|
|
|
shared->value = ch;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
method_underflow (FILE *fp)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_underflow;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
method_uflow (FILE *fp)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_uflow;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
method_pbackfail (FILE *fp, int ch)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_pbackfail;
|
|
|
|
shared->value = ch;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
method_xsputn (FILE *fp, const void *data, size_t n)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_xsputn;
|
|
|
|
|
|
|
|
size_t to_copy = n;
|
|
|
|
if (n > sizeof (shared->buffer))
|
|
|
|
to_copy = sizeof (shared->buffer);
|
|
|
|
memcpy (shared->buffer, data, to_copy);
|
|
|
|
shared->buffer_length = to_copy;
|
|
|
|
return to_copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
method_xsgetn (FILE *fp, void *data, size_t n)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_xsgetn;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static off64_t
|
|
|
|
method_seekoff (FILE *fp, off64_t offset, int dir, int mode)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_seekoff;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static off64_t
|
|
|
|
method_seekpos (FILE *fp, off64_t offset, int mode)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_seekpos;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *
|
|
|
|
method_setbuf (FILE *fp, char *buffer, ssize_t length)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_setbuf;
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
method_sync (FILE *fp)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_sync;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
method_doallocate (FILE *fp)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_doallocate;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
method_read (FILE *fp, void *data, ssize_t length)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_read;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
method_write (FILE *fp, const void *data, ssize_t length)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_write;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static off64_t
|
|
|
|
method_seek (FILE *fp, off64_t offset, int mode)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_seek;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
method_close (FILE *fp)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_close;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
method_stat (FILE *fp, void *buffer)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_stat;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
method_showmanyc (FILE *fp)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_showmanyc;
|
|
|
|
return shared->return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
method_imbue (FILE *fp, void *locale)
|
|
|
|
{
|
|
|
|
log_method (fp, __func__);
|
|
|
|
TEST_VERIFY (fp == shared->fp);
|
|
|
|
++shared->calls;
|
|
|
|
++shared->calls_imbue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Our custom vtable. */
|
|
|
|
|
|
|
|
static const struct _IO_jump_t jumps =
|
|
|
|
{
|
|
|
|
JUMP_INIT_DUMMY,
|
|
|
|
JUMP_INIT (finish, method_finish),
|
|
|
|
JUMP_INIT (overflow, method_overflow),
|
|
|
|
JUMP_INIT (underflow, method_underflow),
|
|
|
|
JUMP_INIT (uflow, method_uflow),
|
|
|
|
JUMP_INIT (pbackfail, method_pbackfail),
|
|
|
|
JUMP_INIT (xsputn, method_xsputn),
|
|
|
|
JUMP_INIT (xsgetn, method_xsgetn),
|
|
|
|
JUMP_INIT (seekoff, method_seekoff),
|
|
|
|
JUMP_INIT (seekpos, method_seekpos),
|
|
|
|
JUMP_INIT (setbuf, method_setbuf),
|
|
|
|
JUMP_INIT (sync, method_sync),
|
|
|
|
JUMP_INIT (doallocate, method_doallocate),
|
|
|
|
JUMP_INIT (read, method_read),
|
|
|
|
JUMP_INIT (write, method_write),
|
|
|
|
JUMP_INIT (seek, method_seek),
|
|
|
|
JUMP_INIT (close, method_close),
|
|
|
|
JUMP_INIT (stat, method_stat),
|
|
|
|
JUMP_INIT (showmanyc, method_showmanyc),
|
|
|
|
JUMP_INIT (imbue, method_imbue)
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Our file implementation. */
|
|
|
|
|
|
|
|
struct my_file
|
|
|
|
{
|
|
|
|
FILE f;
|
|
|
|
const struct _IO_jump_t *vtable;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct my_file
|
|
|
|
my_file_create (void)
|
|
|
|
{
|
|
|
|
return (struct my_file)
|
|
|
|
{
|
|
|
|
/* Disable locking, so that we do not have to set up a lock
|
|
|
|
pointer. */
|
|
|
|
.f._flags = _IO_USER_LOCK,
|
|
|
|
|
|
|
|
/* Copy the offset from the an initialized handle, instead of
|
|
|
|
figuring it out from scratch. */
|
|
|
|
.f._vtable_offset = stdin->_vtable_offset,
|
|
|
|
|
|
|
|
.vtable = &jumps,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initial tests which do not enable vtable compatibility. */
|
|
|
|
|
|
|
|
/* Inhibit GCC optimization of fprintf. */
|
|
|
|
typedef int (*fprintf_type) (FILE *, const char *, ...);
|
|
|
|
static const volatile fprintf_type fprintf_ptr = &fprintf;
|
|
|
|
|
|
|
|
static void
|
|
|
|
without_compatibility_fprintf (void *closure)
|
|
|
|
{
|
|
|
|
/* This call should abort. */
|
|
|
|
fprintf_ptr (shared->fp, " ");
|
|
|
|
_exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
without_compatibility_fputc (void *closure)
|
|
|
|
{
|
|
|
|
/* This call should abort. */
|
|
|
|
fputc (' ', shared->fp);
|
|
|
|
_exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
without_compatibility_fgetc (void *closure)
|
|
|
|
{
|
|
|
|
/* This call should abort. */
|
|
|
|
fgetc (shared->fp);
|
|
|
|
_exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
without_compatibility_fflush (void *closure)
|
|
|
|
{
|
|
|
|
/* This call should abort. */
|
|
|
|
fflush (shared->fp);
|
|
|
|
_exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_for_termination (const char *name, void (*callback) (void *))
|
|
|
|
{
|
|
|
|
struct my_file file = my_file_create ();
|
|
|
|
shared->fp = &file.f;
|
|
|
|
shared->return_value = -1;
|
|
|
|
shared->calls = 0;
|
|
|
|
struct support_capture_subprocess proc
|
|
|
|
= support_capture_subprocess (callback, NULL);
|
2018-11-29 02:59:45 +08:00
|
|
|
support_capture_subprocess_check (&proc, name, -SIGABRT,
|
2018-06-26 18:20:44 +08:00
|
|
|
sc_allow_stderr);
|
|
|
|
const char *message
|
|
|
|
= "Fatal error: glibc detected an invalid stdio handle\n";
|
|
|
|
TEST_COMPARE_BLOB (proc.err.buffer, proc.err.length,
|
|
|
|
message, strlen (message));
|
|
|
|
TEST_COMPARE (shared->calls, 0);
|
|
|
|
support_capture_subprocess_free (&proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The test with vtable validation disabled. */
|
|
|
|
|
|
|
|
/* This function does not have a prototype in libioP.h to prevent
|
|
|
|
accidental use from within the library (which would disable vtable
|
|
|
|
verification). */
|
|
|
|
void _IO_init (FILE *fp, int flags);
|
|
|
|
|
|
|
|
static void
|
|
|
|
with_compatibility_fprintf (void *closure)
|
|
|
|
{
|
stdio-common: Convert vfprintf and related functions to buffers
vfprintf is entangled with vfwprintf (of course), __printf_fp,
__printf_fphex, __vstrfmon_l_internal, and the strfrom family of
functions. The latter use the internal snprintf functionality,
so vsnprintf is converted as well.
The simples conversion is __printf_fphex, followed by
__vstrfmon_l_internal and __printf_fp, and finally
__vfprintf_internal and __vfwprintf_internal. __vsnprintf_internal
and strfrom* are mostly consuming the new interfaces, so they
are comparatively simple.
__printf_fp is a public symbol, so the FILE *-based interface
had to preserved.
The __printf_fp rewrite does not change the actual binary-to-decimal
conversion algorithm, and digits are still not emitted directly to
the target buffer. However, the staging buffer now uses bytes
instead of wide characters, and one buffer copy is eliminated.
The changes are at least performance-neutral in my testing.
Floating point printing and snprintf improved measurably, so that
this Lua script
for i=1,5000000 do
print(i, i * math.pi)
end
runs about 5% faster for me. To preserve fprintf performance for
a simple "%d" format, this commit has some logic changes under
LABEL (unsigned_number) to avoid additional function calls. There
are certainly some very easy performance improvements here: binary,
octal and hexadecimal formatting can easily avoid the temporary work
buffer (the number of digits can be computed ahead-of-time using one
of the __builtin_clz* built-ins). Decimal formatting can use a
specialized version of _itoa_word for base 10.
The existing (inconsistent) width handling between strfmon and printf
is preserved here. __print_fp_buffer_1 would have to use
__translated_number_width to achieve ISO conformance for printf.
Test expectations in libio/tst-vtables-common.c are adjusted because
the internal staging buffer merges all virtual function calls into
one.
In general, stack buffer usage is greatly reduced, particularly for
unbuffered input streams. __printf_fp can still use a large buffer
in binary128 mode for %g, though.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2022-12-20 01:56:54 +08:00
|
|
|
/* A temporary staging buffer is used in the current fprintf
|
|
|
|
implementation, which is why there is just one call to
|
|
|
|
xsputn. */
|
2018-06-26 18:20:44 +08:00
|
|
|
TEST_COMPARE (fprintf_ptr (shared->fp, "A%sCD", "B"), 4);
|
stdio-common: Convert vfprintf and related functions to buffers
vfprintf is entangled with vfwprintf (of course), __printf_fp,
__printf_fphex, __vstrfmon_l_internal, and the strfrom family of
functions. The latter use the internal snprintf functionality,
so vsnprintf is converted as well.
The simples conversion is __printf_fphex, followed by
__vstrfmon_l_internal and __printf_fp, and finally
__vfprintf_internal and __vfwprintf_internal. __vsnprintf_internal
and strfrom* are mostly consuming the new interfaces, so they
are comparatively simple.
__printf_fp is a public symbol, so the FILE *-based interface
had to preserved.
The __printf_fp rewrite does not change the actual binary-to-decimal
conversion algorithm, and digits are still not emitted directly to
the target buffer. However, the staging buffer now uses bytes
instead of wide characters, and one buffer copy is eliminated.
The changes are at least performance-neutral in my testing.
Floating point printing and snprintf improved measurably, so that
this Lua script
for i=1,5000000 do
print(i, i * math.pi)
end
runs about 5% faster for me. To preserve fprintf performance for
a simple "%d" format, this commit has some logic changes under
LABEL (unsigned_number) to avoid additional function calls. There
are certainly some very easy performance improvements here: binary,
octal and hexadecimal formatting can easily avoid the temporary work
buffer (the number of digits can be computed ahead-of-time using one
of the __builtin_clz* built-ins). Decimal formatting can use a
specialized version of _itoa_word for base 10.
The existing (inconsistent) width handling between strfmon and printf
is preserved here. __print_fp_buffer_1 would have to use
__translated_number_width to achieve ISO conformance for printf.
Test expectations in libio/tst-vtables-common.c are adjusted because
the internal staging buffer merges all virtual function calls into
one.
In general, stack buffer usage is greatly reduced, particularly for
unbuffered input streams. __printf_fp can still use a large buffer
in binary128 mode for %g, though.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2022-12-20 01:56:54 +08:00
|
|
|
TEST_COMPARE (shared->calls, 1);
|
|
|
|
TEST_COMPARE (shared->calls_xsputn, 1);
|
2018-06-26 18:20:44 +08:00
|
|
|
TEST_COMPARE_BLOB (shared->buffer, shared->buffer_length,
|
stdio-common: Convert vfprintf and related functions to buffers
vfprintf is entangled with vfwprintf (of course), __printf_fp,
__printf_fphex, __vstrfmon_l_internal, and the strfrom family of
functions. The latter use the internal snprintf functionality,
so vsnprintf is converted as well.
The simples conversion is __printf_fphex, followed by
__vstrfmon_l_internal and __printf_fp, and finally
__vfprintf_internal and __vfwprintf_internal. __vsnprintf_internal
and strfrom* are mostly consuming the new interfaces, so they
are comparatively simple.
__printf_fp is a public symbol, so the FILE *-based interface
had to preserved.
The __printf_fp rewrite does not change the actual binary-to-decimal
conversion algorithm, and digits are still not emitted directly to
the target buffer. However, the staging buffer now uses bytes
instead of wide characters, and one buffer copy is eliminated.
The changes are at least performance-neutral in my testing.
Floating point printing and snprintf improved measurably, so that
this Lua script
for i=1,5000000 do
print(i, i * math.pi)
end
runs about 5% faster for me. To preserve fprintf performance for
a simple "%d" format, this commit has some logic changes under
LABEL (unsigned_number) to avoid additional function calls. There
are certainly some very easy performance improvements here: binary,
octal and hexadecimal formatting can easily avoid the temporary work
buffer (the number of digits can be computed ahead-of-time using one
of the __builtin_clz* built-ins). Decimal formatting can use a
specialized version of _itoa_word for base 10.
The existing (inconsistent) width handling between strfmon and printf
is preserved here. __print_fp_buffer_1 would have to use
__translated_number_width to achieve ISO conformance for printf.
Test expectations in libio/tst-vtables-common.c are adjusted because
the internal staging buffer merges all virtual function calls into
one.
In general, stack buffer usage is greatly reduced, particularly for
unbuffered input streams. __printf_fp can still use a large buffer
in binary128 mode for %g, though.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2022-12-20 01:56:54 +08:00
|
|
|
"ABCD", 4);
|
2018-06-26 18:20:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
with_compatibility_fputc (void *closure)
|
|
|
|
{
|
|
|
|
shared->return_value = '@';
|
|
|
|
TEST_COMPARE (fputc ('@', shared->fp), '@');
|
|
|
|
TEST_COMPARE (shared->calls, 1);
|
|
|
|
TEST_COMPARE (shared->calls_overflow, 1);
|
|
|
|
TEST_COMPARE (shared->value, '@');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
with_compatibility_fgetc (void *closure)
|
|
|
|
{
|
|
|
|
shared->return_value = 'X';
|
|
|
|
TEST_COMPARE (fgetc (shared->fp), 'X');
|
|
|
|
TEST_COMPARE (shared->calls, 1);
|
|
|
|
TEST_COMPARE (shared->calls_uflow, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
with_compatibility_fflush (void *closure)
|
|
|
|
{
|
|
|
|
TEST_COMPARE (fflush (shared->fp), 0);
|
|
|
|
TEST_COMPARE (shared->calls, 1);
|
|
|
|
TEST_COMPARE (shared->calls_sync, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call CALLBACK in a subprocess, after setting up a custom file
|
|
|
|
object and updating shared->fp. */
|
|
|
|
static void
|
|
|
|
check_call (const char *name, void (*callback) (void *),
|
|
|
|
bool initially_disabled)
|
|
|
|
{
|
|
|
|
*shared = (struct shared)
|
|
|
|
{
|
|
|
|
.initially_disabled = initially_disabled,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Set up a custom file object. */
|
|
|
|
struct my_file file = my_file_create ();
|
|
|
|
shared->fp = &file.f;
|
|
|
|
if (shared->initially_disabled)
|
|
|
|
_IO_init (shared->fp, file.f._flags);
|
|
|
|
|
|
|
|
if (test_verbose > 0)
|
|
|
|
printf ("info: calling test %s\n", name);
|
|
|
|
support_isolate_in_subprocess (callback, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run the tests. INITIALLY_DISABLED indicates whether custom vtables
|
|
|
|
are disabled when the test starts. */
|
|
|
|
static int
|
|
|
|
run_tests (bool initially_disabled)
|
|
|
|
{
|
|
|
|
/* The test relies on fatal error messages being printed to standard
|
|
|
|
error. */
|
|
|
|
setenv ("LIBC_FATAL_STDERR_", "1", 1);
|
|
|
|
|
|
|
|
shared = support_shared_allocate (sizeof (*shared));
|
|
|
|
shared->initially_disabled = initially_disabled;
|
|
|
|
|
|
|
|
if (initially_disabled)
|
|
|
|
{
|
|
|
|
check_for_termination ("fprintf", without_compatibility_fprintf);
|
|
|
|
check_for_termination ("fputc", without_compatibility_fputc);
|
|
|
|
check_for_termination ("fgetc", without_compatibility_fgetc);
|
|
|
|
check_for_termination ("fflush", without_compatibility_fflush);
|
|
|
|
}
|
|
|
|
|
|
|
|
check_call ("fprintf", with_compatibility_fprintf, initially_disabled);
|
|
|
|
check_call ("fputc", with_compatibility_fputc, initially_disabled);
|
|
|
|
check_call ("fgetc", with_compatibility_fgetc, initially_disabled);
|
|
|
|
check_call ("fflush", with_compatibility_fflush, initially_disabled);
|
|
|
|
|
|
|
|
support_shared_free (shared);
|
|
|
|
shared = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|