2017-08-10 11:40:22 +00:00
|
|
|
/* ld.so error exception allocation and deallocation.
|
2022-01-01 18:54:23 +00:00
|
|
|
Copyright (C) 1995-2022 Free Software Foundation, Inc.
|
2017-08-10 11:40:22 +00: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 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2017-08-10 11:40:22 +00:00
|
|
|
|
|
|
|
#include <ldsodefs.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2018-11-30 04:03:46 +00:00
|
|
|
#include <_itoa.h>
|
2017-08-10 11:40:22 +00:00
|
|
|
|
|
|
|
/* This message we return as a last resort. We define the string in a
|
|
|
|
variable since we have to avoid freeing it and so have to enable
|
|
|
|
a pointer comparison. See below and in dlfcn/dlerror.c. */
|
|
|
|
static const char _dl_out_of_memory[] = "out of memory";
|
|
|
|
|
2021-04-21 17:49:51 +00:00
|
|
|
/* Call free in the main libc.so. This allows other namespaces to
|
|
|
|
free pointers on the main libc heap, via GLRO (dl_error_free). It
|
|
|
|
also avoids calling free on the special, pre-allocated
|
|
|
|
out-of-memory error message. */
|
|
|
|
void
|
|
|
|
_dl_error_free (void *ptr)
|
|
|
|
{
|
|
|
|
if (ptr != _dl_out_of_memory)
|
|
|
|
free (ptr);
|
|
|
|
}
|
|
|
|
|
2017-08-10 11:40:22 +00:00
|
|
|
/* Dummy allocation object used if allocating the message buffer
|
|
|
|
fails. */
|
|
|
|
static void
|
|
|
|
oom_exception (struct dl_exception *exception)
|
|
|
|
{
|
|
|
|
exception->objname = "";
|
|
|
|
exception->errstring = _dl_out_of_memory;
|
|
|
|
exception->message_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
__attribute__ ((noreturn))
|
|
|
|
length_mismatch (void)
|
|
|
|
{
|
|
|
|
_dl_fatal_printf ("Fatal error: "
|
|
|
|
"length accounting in _dl_exception_create_format\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust the message buffer to indicate whether it is possible to
|
|
|
|
free it. EXCEPTION->errstring must be a potentially deallocatable
|
|
|
|
pointer. */
|
|
|
|
static void
|
|
|
|
adjust_message_buffer (struct dl_exception *exception)
|
|
|
|
{
|
|
|
|
/* If the main executable is relocated it means the libc's malloc
|
|
|
|
is used. */
|
|
|
|
bool malloced = true;
|
|
|
|
#ifdef SHARED
|
|
|
|
malloced = (GL(dl_ns)[LM_ID_BASE]._ns_loaded != NULL
|
|
|
|
&& (GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_relocated != 0));
|
|
|
|
#endif
|
|
|
|
if (malloced)
|
|
|
|
exception->message_buffer = (char *) exception->errstring;
|
|
|
|
else
|
|
|
|
exception->message_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_dl_exception_create (struct dl_exception *exception, const char *objname,
|
|
|
|
const char *errstring)
|
|
|
|
{
|
|
|
|
if (objname == NULL)
|
|
|
|
objname = "";
|
|
|
|
size_t len_objname = strlen (objname) + 1;
|
|
|
|
size_t len_errstring = strlen (errstring) + 1;
|
|
|
|
char *errstring_copy = malloc (len_objname + len_errstring);
|
|
|
|
if (errstring_copy != NULL)
|
|
|
|
{
|
|
|
|
/* Make a copy of the object file name and the error string. */
|
|
|
|
exception->objname = memcpy (__mempcpy (errstring_copy,
|
|
|
|
errstring, len_errstring),
|
|
|
|
objname, len_objname);
|
|
|
|
exception->errstring = errstring_copy;
|
|
|
|
adjust_message_buffer (exception);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
oom_exception (exception);
|
|
|
|
}
|
|
|
|
rtld_hidden_def (_dl_exception_create)
|
|
|
|
|
|
|
|
void
|
|
|
|
_dl_exception_create_format (struct dl_exception *exception, const char *objname,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
if (objname == NULL)
|
|
|
|
objname = "";
|
|
|
|
size_t len_objname = strlen (objname) + 1;
|
|
|
|
/* Compute the length of the result. Include room for two NUL
|
|
|
|
bytes. */
|
|
|
|
size_t length = len_objname + 1;
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
for (const char *p = fmt; *p != '\0'; ++p)
|
|
|
|
if (*p == '%')
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
length += strlen (va_arg (ap, const char *));
|
|
|
|
break;
|
2018-11-29 22:15:01 +00:00
|
|
|
/* Recognize the l modifier. It is only important on some
|
|
|
|
platforms where long and int have a different size. We
|
|
|
|
can use the same code for size_t. */
|
|
|
|
case 'l':
|
|
|
|
case 'z':
|
|
|
|
if (p[1] == 'x')
|
|
|
|
{
|
|
|
|
length += LONG_WIDTH / 4;
|
|
|
|
++p;
|
|
|
|
break;
|
|
|
|
}
|
2019-02-12 10:30:34 +00:00
|
|
|
/* Fall through. */
|
2018-11-29 22:15:01 +00:00
|
|
|
case 'x':
|
|
|
|
length += INT_WIDTH / 4;
|
|
|
|
break;
|
2017-08-10 11:40:22 +00:00
|
|
|
default:
|
|
|
|
/* Assumed to be '%'. */
|
|
|
|
++length;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++length;
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > PTRDIFF_MAX)
|
|
|
|
{
|
|
|
|
oom_exception (exception);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char *errstring = malloc (length);
|
|
|
|
if (errstring == NULL)
|
|
|
|
{
|
|
|
|
oom_exception (exception);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
exception->errstring = errstring;
|
|
|
|
adjust_message_buffer (exception);
|
|
|
|
|
|
|
|
/* Copy the error message to errstring. */
|
|
|
|
{
|
|
|
|
/* Next byte to be written in errstring. */
|
|
|
|
char *wptr = errstring;
|
|
|
|
/* End of the allocated string. */
|
|
|
|
char *const end = errstring + length;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
|
|
|
|
for (const char *p = fmt; *p != '\0'; ++p)
|
|
|
|
if (*p == '%')
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
const char *ptr = va_arg (ap, const char *);
|
|
|
|
size_t len_ptr = strlen (ptr);
|
|
|
|
if (len_ptr > end - wptr)
|
|
|
|
length_mismatch ();
|
|
|
|
wptr = __mempcpy (wptr, ptr, len_ptr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
if (wptr == end)
|
|
|
|
length_mismatch ();
|
|
|
|
*wptr = '%';
|
|
|
|
++wptr;
|
|
|
|
break;
|
2018-11-29 22:15:01 +00:00
|
|
|
case 'x':
|
|
|
|
{
|
|
|
|
unsigned long int num = va_arg (ap, unsigned int);
|
|
|
|
char *start = wptr;
|
|
|
|
wptr += INT_WIDTH / 4;
|
|
|
|
char *cp = _itoa (num, wptr, 16, 0);
|
|
|
|
/* Pad to the full width with 0. */
|
|
|
|
while (cp != start)
|
|
|
|
*--cp = '0';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
case 'z':
|
|
|
|
if (p[1] == 'x')
|
|
|
|
{
|
|
|
|
unsigned long int num = va_arg (ap, unsigned long int);
|
|
|
|
char *start = wptr;
|
|
|
|
wptr += LONG_WIDTH / 4;
|
|
|
|
char *cp = _itoa (num, wptr, 16, 0);
|
|
|
|
/* Pad to the full width with 0. */
|
|
|
|
while (cp != start)
|
|
|
|
*--cp = '0';
|
|
|
|
++p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2017-08-10 11:40:22 +00:00
|
|
|
default:
|
|
|
|
_dl_fatal_printf ("Fatal error:"
|
|
|
|
" invalid format in exception string\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (wptr == end)
|
|
|
|
length_mismatch ();
|
|
|
|
*wptr = *p;
|
|
|
|
++wptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wptr == end)
|
|
|
|
length_mismatch ();
|
|
|
|
*wptr = '\0';
|
|
|
|
++wptr;
|
|
|
|
if (len_objname != end - wptr)
|
|
|
|
length_mismatch ();
|
|
|
|
exception->objname = memcpy (wptr, objname, len_objname);
|
2021-05-11 03:34:29 +00:00
|
|
|
va_end (ap);
|
2017-08-10 11:40:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rtld_hidden_def (_dl_exception_create_format)
|
|
|
|
|
|
|
|
void
|
|
|
|
_dl_exception_free (struct dl_exception *exception)
|
|
|
|
{
|
|
|
|
free (exception->message_buffer);
|
|
|
|
exception->objname = NULL;
|
|
|
|
exception->errstring = NULL;
|
|
|
|
exception->message_buffer = NULL;
|
|
|
|
}
|
|
|
|
rtld_hidden_def (_dl_exception_free)
|