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
|
/* ld.so error exception allocation and deallocation.
Copyright (C) 1995-2025 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 <ldsodefs.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <_itoa.h>
/* 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";
/* 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);
}
/* 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;
/* 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;
}
/* Fall through. */
case 'x':
length += INT_WIDTH / 4;
break;
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;
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 */
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);
va_end (ap);
}
}
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)
|