Coverage Report

Created: 2025-10-10 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/lib/system.h
Line
Count
Source
1
/* Declarations for common convenience functions.
2
   Copyright (C) 2006-2011 Red Hat, Inc.
3
   Copyright (C) 2022 Mark J. Wielaard <mark@klomp.org>
4
   Copyright (C) 2023 Khem Raj.
5
   This file is part of elfutils.
6
7
   This file is free software; you can redistribute it and/or modify
8
   it under the terms of either
9
10
     * the GNU Lesser General Public License as published by the Free
11
       Software Foundation; either version 3 of the License, or (at
12
       your option) any later version
13
14
   or
15
16
     * the GNU General Public License as published by the Free
17
       Software Foundation; either version 2 of the License, or (at
18
       your option) any later version
19
20
   or both in parallel, as here.
21
22
   elfutils is distributed in the hope that it will be useful, but
23
   WITHOUT ANY WARRANTY; without even the implied warranty of
24
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
   General Public License for more details.
26
27
   You should have received copies of the GNU General Public License and
28
   the GNU Lesser General Public License along with this program.  If
29
   not, see <http://www.gnu.org/licenses/>.  */
30
31
#ifndef LIB_SYSTEM_H
32
#define LIB_SYSTEM_H  1
33
34
/* Prevent double inclusion of config.h, config.h includes eu-config.h.  */
35
#ifdef HAVE_CONFIG_H
36
#ifndef EU_CONFIG_H
37
# include <config.h>
38
#endif
39
#endif
40
41
#include <errno.h>
42
#include <stdbool.h>
43
#include <stddef.h>
44
#include <stdint.h>
45
#include <string.h>
46
#include <stdarg.h>
47
#include <stdlib.h>
48
49
/* System dependent headers */
50
#include <byteswap.h>
51
#include <endian.h>
52
#include <sys/mman.h>
53
#include <sys/param.h>
54
#include <unistd.h>
55
56
#if defined(HAVE_ERROR_H)
57
#include <error.h>
58
#elif defined(HAVE_ERR_H)
59
extern int error_message_count;
60
void error(int status, int errnum, const char *format, ...);
61
#else
62
#error "err.h or error.h must be available"
63
#endif
64
65
/* error (EXIT_FAILURE, ...) should be noreturn but on some systems it
66
   isn't.  This may cause warnings about code that should not be reachable.
67
   So have an explicit error_exit wrapper that is noreturn (because it
68
   calls exit explicitly).  */
69
#define error_exit(errnum,...) do { \
70
    error (EXIT_FAILURE,errnum,__VA_ARGS__); \
71
    exit (EXIT_FAILURE); \
72
  } while (0)
73
74
#if BYTE_ORDER == LITTLE_ENDIAN
75
# define LE32(n)  (n)
76
# define LE64(n)  (n)
77
# define BE32(n)  bswap_32 (n)
78
# define BE64(n)  bswap_64 (n)
79
#elif BYTE_ORDER == BIG_ENDIAN
80
# define BE32(n)  (n)
81
# define BE64(n)  (n)
82
# define LE32(n)  bswap_32 (n)
83
# define LE64(n)  bswap_64 (n)
84
#else
85
# error "Unknown byte order"
86
#endif
87
88
#ifndef MAX
89
#define MAX(m, n) ((m) < (n) ? (n) : (m))
90
#endif
91
92
#ifndef MIN
93
#define MIN(m, n) ((m) < (n) ? (m) : (n))
94
#endif
95
96
#if !HAVE_DECL_POWEROF2
97
#define powerof2(x) (((x) & ((x) - 1)) == 0)
98
#endif
99
100
#if !HAVE_DECL_MEMPCPY
101
#define mempcpy(dest, src, n) \
102
    ((void *) ((char *) memcpy (dest, src, n) + (size_t) n))
103
#endif
104
105
#if !HAVE_DECL_REALLOCARRAY
106
static inline void *
107
reallocarray (void *ptr, size_t nmemb, size_t size)
108
{
109
  if (size > 0 && nmemb > SIZE_MAX / size)
110
    {
111
      errno = ENOMEM;
112
      return NULL;
113
    }
114
  return realloc (ptr, nmemb * size);
115
}
116
#endif
117
118
/* Return TRUE if the start of STR matches PREFIX, FALSE otherwise.  */
119
120
static inline int
121
startswith (const char *str, const char *prefix)
122
0
{
123
0
  return strncmp (str, prefix, strlen (prefix)) == 0;
124
0
}
Unexecuted instantiation: fuzz-libelf.c:startswith
Unexecuted instantiation: elf_version.c:startswith
Unexecuted instantiation: elf_error.c:startswith
Unexecuted instantiation: elf_begin.c:startswith
Unexecuted instantiation: elf_end.c:startswith
Unexecuted instantiation: elf_rawfile.c:startswith
Unexecuted instantiation: elf_readall.c:startswith
Unexecuted instantiation: elf_nextscn.c:startswith
Unexecuted instantiation: gelf_getshdr.c:startswith
Unexecuted instantiation: elf_strptr.c:startswith
Unexecuted instantiation: elf_getdata.c:startswith
Unexecuted instantiation: elf_getshdrstrndx.c:startswith
Unexecuted instantiation: elf_compress.c:startswith
Unexecuted instantiation: elf_compress_gnu.c:startswith
Unexecuted instantiation: gelf_fsize.c:startswith
Unexecuted instantiation: gelf_xlate.c:startswith
Unexecuted instantiation: gelf_getehdr.c:startswith
Unexecuted instantiation: elf32_getshdr.c:startswith
Unexecuted instantiation: elf64_getshdr.c:startswith
Unexecuted instantiation: gelf_xlatetof.c:startswith
Unexecuted instantiation: elf_getshdrnum.c:startswith
Unexecuted instantiation: gelf_getchdr.c:startswith
Unexecuted instantiation: elf32_xlatetof.c:startswith
Unexecuted instantiation: elf64_xlatetof.c:startswith
Unexecuted instantiation: elf32_getchdr.c:startswith
Unexecuted instantiation: elf64_getchdr.c:startswith
125
126
/* Return TRUE if STR[FROM] is a valid string with a zero terminator
127
   at or before STR[TO - 1].  Note FROM is an index into the STR
128
   array, while TO is the maximum size of the STR array.  This
129
   function returns FALSE when TO is zero or FROM >= TO.  */
130
static inline bool
131
validate_str (const char *str, size_t from, size_t to)
132
403k
{
133
403k
#if HAVE_DECL_MEMRCHR
134
  // Check end first, which is likely a zero terminator,
135
  // to prevent function call
136
403k
  return (to > 0
137
403k
    && (str[to - 1] == '\0'
138
207k
        || (to > from
139
207k
      && memrchr (&str[from], '\0', to - from - 1) != NULL)));
140
#else
141
  do {
142
    if (to <= from)
143
      return false;
144
145
    to--;
146
  } while (str[to]);
147
148
  return true;
149
#endif
150
403k
}
Unexecuted instantiation: fuzz-libelf.c:validate_str
Unexecuted instantiation: elf_version.c:validate_str
Unexecuted instantiation: elf_error.c:validate_str
Unexecuted instantiation: elf_begin.c:validate_str
Unexecuted instantiation: elf_end.c:validate_str
Unexecuted instantiation: elf_rawfile.c:validate_str
Unexecuted instantiation: elf_readall.c:validate_str
Unexecuted instantiation: elf_nextscn.c:validate_str
Unexecuted instantiation: gelf_getshdr.c:validate_str
elf_strptr.c:validate_str
Line
Count
Source
132
403k
{
133
403k
#if HAVE_DECL_MEMRCHR
134
  // Check end first, which is likely a zero terminator,
135
  // to prevent function call
136
403k
  return (to > 0
137
403k
    && (str[to - 1] == '\0'
138
207k
        || (to > from
139
207k
      && memrchr (&str[from], '\0', to - from - 1) != NULL)));
140
#else
141
  do {
142
    if (to <= from)
143
      return false;
144
145
    to--;
146
  } while (str[to]);
147
148
  return true;
149
#endif
150
403k
}
Unexecuted instantiation: elf_getdata.c:validate_str
Unexecuted instantiation: elf_getshdrstrndx.c:validate_str
Unexecuted instantiation: elf_compress.c:validate_str
Unexecuted instantiation: elf_compress_gnu.c:validate_str
Unexecuted instantiation: gelf_fsize.c:validate_str
Unexecuted instantiation: gelf_xlate.c:validate_str
Unexecuted instantiation: gelf_getehdr.c:validate_str
Unexecuted instantiation: elf32_getshdr.c:validate_str
Unexecuted instantiation: elf64_getshdr.c:validate_str
Unexecuted instantiation: gelf_xlatetof.c:validate_str
Unexecuted instantiation: elf_getshdrnum.c:validate_str
Unexecuted instantiation: gelf_getchdr.c:validate_str
Unexecuted instantiation: elf32_xlatetof.c:validate_str
Unexecuted instantiation: elf64_xlatetof.c:validate_str
Unexecuted instantiation: elf32_getchdr.c:validate_str
Unexecuted instantiation: elf64_getchdr.c:validate_str
151
152
/* A special gettext function we use if the strings are too short.  */
153
#define sgettext(Str) \
154
  ({ const char *__res = strrchr (_(Str), '|');           \
155
     __res ? __res + 1 : Str; })
156
157
#define gettext_noop(Str) Str
158
159
#ifndef TEMP_FAILURE_RETRY
160
#define TEMP_FAILURE_RETRY(expression) \
161
  ({ ssize_t __res; \
162
     do \
163
       __res = expression; \
164
     while (__res == -1 && errno == EINTR); \
165
     __res; })
166
#endif
167
168
#ifndef ACCESSPERMS
169
#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
170
#endif
171
172
#ifndef ALLPERMS
173
#define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
174
#endif
175
176
#ifndef DEFFILEMODE
177
#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)/* 0666 */
178
#endif
179
180
static inline ssize_t __attribute__ ((unused))
181
pwrite_retry (int fd, const void *buf, size_t len, off_t off)
182
0
{
183
0
  ssize_t recvd = 0;
184
0
185
0
  do
186
0
    {
187
0
      ssize_t ret = TEMP_FAILURE_RETRY (pwrite (fd, ((char *)buf) + recvd, len - recvd,
188
0
            off + recvd));
189
0
      if (ret <= 0)
190
0
  return ret < 0 ? ret : recvd;
191
0
192
0
      recvd += ret;
193
0
    }
194
0
  while ((size_t) recvd < len);
195
0
196
0
  return recvd;
197
0
}
Unexecuted instantiation: fuzz-libelf.c:pwrite_retry
Unexecuted instantiation: elf_version.c:pwrite_retry
Unexecuted instantiation: elf_error.c:pwrite_retry
Unexecuted instantiation: elf_begin.c:pwrite_retry
Unexecuted instantiation: elf_end.c:pwrite_retry
Unexecuted instantiation: elf_rawfile.c:pwrite_retry
Unexecuted instantiation: elf_readall.c:pwrite_retry
Unexecuted instantiation: elf_nextscn.c:pwrite_retry
Unexecuted instantiation: gelf_getshdr.c:pwrite_retry
Unexecuted instantiation: elf_strptr.c:pwrite_retry
Unexecuted instantiation: elf_getdata.c:pwrite_retry
Unexecuted instantiation: elf_getshdrstrndx.c:pwrite_retry
Unexecuted instantiation: elf_compress.c:pwrite_retry
Unexecuted instantiation: elf_compress_gnu.c:pwrite_retry
Unexecuted instantiation: gelf_fsize.c:pwrite_retry
Unexecuted instantiation: gelf_xlate.c:pwrite_retry
Unexecuted instantiation: gelf_getehdr.c:pwrite_retry
Unexecuted instantiation: elf32_getshdr.c:pwrite_retry
Unexecuted instantiation: elf64_getshdr.c:pwrite_retry
Unexecuted instantiation: gelf_xlatetof.c:pwrite_retry
Unexecuted instantiation: elf_getshdrnum.c:pwrite_retry
Unexecuted instantiation: gelf_getchdr.c:pwrite_retry
Unexecuted instantiation: elf32_xlatetof.c:pwrite_retry
Unexecuted instantiation: elf64_xlatetof.c:pwrite_retry
Unexecuted instantiation: elf32_getchdr.c:pwrite_retry
Unexecuted instantiation: elf64_getchdr.c:pwrite_retry
198
199
static inline ssize_t __attribute__ ((unused))
200
write_retry (int fd, const void *buf, size_t len)
201
6.19k
{
202
6.19k
  ssize_t recvd = 0;
203
204
6.19k
  do
205
6.19k
    {
206
6.19k
      ssize_t ret = TEMP_FAILURE_RETRY (write (fd, ((char *)buf) + recvd, len - recvd));
207
6.19k
      if (ret <= 0)
208
0
  return ret < 0 ? ret : recvd;
209
210
6.19k
      recvd += ret;
211
6.19k
    }
212
6.19k
  while ((size_t) recvd < len);
213
214
6.19k
  return recvd;
215
6.19k
}
fuzz-libelf.c:write_retry
Line
Count
Source
201
6.19k
{
202
6.19k
  ssize_t recvd = 0;
203
204
6.19k
  do
205
6.19k
    {
206
6.19k
      ssize_t ret = TEMP_FAILURE_RETRY (write (fd, ((char *)buf) + recvd, len - recvd));
207
6.19k
      if (ret <= 0)
208
0
  return ret < 0 ? ret : recvd;
209
210
6.19k
      recvd += ret;
211
6.19k
    }
212
6.19k
  while ((size_t) recvd < len);
213
214
6.19k
  return recvd;
215
6.19k
}
Unexecuted instantiation: elf_version.c:write_retry
Unexecuted instantiation: elf_error.c:write_retry
Unexecuted instantiation: elf_begin.c:write_retry
Unexecuted instantiation: elf_end.c:write_retry
Unexecuted instantiation: elf_rawfile.c:write_retry
Unexecuted instantiation: elf_readall.c:write_retry
Unexecuted instantiation: elf_nextscn.c:write_retry
Unexecuted instantiation: gelf_getshdr.c:write_retry
Unexecuted instantiation: elf_strptr.c:write_retry
Unexecuted instantiation: elf_getdata.c:write_retry
Unexecuted instantiation: elf_getshdrstrndx.c:write_retry
Unexecuted instantiation: elf_compress.c:write_retry
Unexecuted instantiation: elf_compress_gnu.c:write_retry
Unexecuted instantiation: gelf_fsize.c:write_retry
Unexecuted instantiation: gelf_xlate.c:write_retry
Unexecuted instantiation: gelf_getehdr.c:write_retry
Unexecuted instantiation: elf32_getshdr.c:write_retry
Unexecuted instantiation: elf64_getshdr.c:write_retry
Unexecuted instantiation: gelf_xlatetof.c:write_retry
Unexecuted instantiation: elf_getshdrnum.c:write_retry
Unexecuted instantiation: gelf_getchdr.c:write_retry
Unexecuted instantiation: elf32_xlatetof.c:write_retry
Unexecuted instantiation: elf64_xlatetof.c:write_retry
Unexecuted instantiation: elf32_getchdr.c:write_retry
Unexecuted instantiation: elf64_getchdr.c:write_retry
216
217
static inline ssize_t __attribute__ ((unused))
218
pread_retry (int fd, void *buf, size_t len, off_t off)
219
153k
{
220
153k
  ssize_t recvd = 0;
221
222
153k
  do
223
153k
    {
224
153k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
225
153k
                 off + recvd));
226
153k
      if (ret <= 0)
227
12.3k
  return ret < 0 ? ret : recvd;
228
229
140k
      recvd += ret;
230
140k
    }
231
153k
  while ((size_t) recvd < len);
232
233
140k
  return recvd;
234
153k
}
Unexecuted instantiation: fuzz-libelf.c:pread_retry
Unexecuted instantiation: elf_version.c:pread_retry
Unexecuted instantiation: elf_error.c:pread_retry
elf_begin.c:pread_retry
Line
Count
Source
219
31.8k
{
220
31.8k
  ssize_t recvd = 0;
221
222
31.8k
  do
223
31.8k
    {
224
31.8k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
225
31.8k
                 off + recvd));
226
31.8k
      if (ret <= 0)
227
12.3k
  return ret < 0 ? ret : recvd;
228
229
19.4k
      recvd += ret;
230
19.4k
    }
231
31.8k
  while ((size_t) recvd < len);
232
233
19.4k
  return recvd;
234
31.8k
}
Unexecuted instantiation: elf_end.c:pread_retry
Unexecuted instantiation: elf_rawfile.c:pread_retry
elf_readall.c:pread_retry
Line
Count
Source
219
6.12k
{
220
6.12k
  ssize_t recvd = 0;
221
222
6.12k
  do
223
6.12k
    {
224
6.12k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
225
6.12k
                 off + recvd));
226
6.12k
      if (ret <= 0)
227
0
  return ret < 0 ? ret : recvd;
228
229
6.12k
      recvd += ret;
230
6.12k
    }
231
6.12k
  while ((size_t) recvd < len);
232
233
6.12k
  return recvd;
234
6.12k
}
Unexecuted instantiation: elf_nextscn.c:pread_retry
Unexecuted instantiation: gelf_getshdr.c:pread_retry
Unexecuted instantiation: elf_strptr.c:pread_retry
elf_getdata.c:pread_retry
Line
Count
Source
219
104k
{
220
104k
  ssize_t recvd = 0;
221
222
104k
  do
223
104k
    {
224
104k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
225
104k
                 off + recvd));
226
104k
      if (ret <= 0)
227
0
  return ret < 0 ? ret : recvd;
228
229
104k
      recvd += ret;
230
104k
    }
231
104k
  while ((size_t) recvd < len);
232
233
104k
  return recvd;
234
104k
}
elf_getshdrstrndx.c:pread_retry
Line
Count
Source
219
234
{
220
234
  ssize_t recvd = 0;
221
222
234
  do
223
234
    {
224
234
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
225
234
                 off + recvd));
226
234
      if (ret <= 0)
227
0
  return ret < 0 ? ret : recvd;
228
229
234
      recvd += ret;
230
234
    }
231
234
  while ((size_t) recvd < len);
232
233
234
  return recvd;
234
234
}
Unexecuted instantiation: elf_compress.c:pread_retry
Unexecuted instantiation: elf_compress_gnu.c:pread_retry
Unexecuted instantiation: gelf_fsize.c:pread_retry
Unexecuted instantiation: gelf_xlate.c:pread_retry
Unexecuted instantiation: gelf_getehdr.c:pread_retry
elf32_getshdr.c:pread_retry
Line
Count
Source
219
8.75k
{
220
8.75k
  ssize_t recvd = 0;
221
222
8.75k
  do
223
8.75k
    {
224
8.75k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
225
8.75k
                 off + recvd));
226
8.75k
      if (ret <= 0)
227
0
  return ret < 0 ? ret : recvd;
228
229
8.75k
      recvd += ret;
230
8.75k
    }
231
8.75k
  while ((size_t) recvd < len);
232
233
8.75k
  return recvd;
234
8.75k
}
elf64_getshdr.c:pread_retry
Line
Count
Source
219
2.16k
{
220
2.16k
  ssize_t recvd = 0;
221
222
2.16k
  do
223
2.16k
    {
224
2.16k
      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
225
2.16k
                 off + recvd));
226
2.16k
      if (ret <= 0)
227
0
  return ret < 0 ? ret : recvd;
228
229
2.16k
      recvd += ret;
230
2.16k
    }
231
2.16k
  while ((size_t) recvd < len);
232
233
2.16k
  return recvd;
234
2.16k
}
Unexecuted instantiation: gelf_xlatetof.c:pread_retry
Unexecuted instantiation: elf_getshdrnum.c:pread_retry
Unexecuted instantiation: gelf_getchdr.c:pread_retry
Unexecuted instantiation: elf32_xlatetof.c:pread_retry
Unexecuted instantiation: elf64_xlatetof.c:pread_retry
Unexecuted instantiation: elf32_getchdr.c:pread_retry
Unexecuted instantiation: elf64_getchdr.c:pread_retry
235
236
/* The demangler from libstdc++.  */
237
extern char *__cxa_demangle (const char *mangled_name, char *output_buffer,
238
           size_t *length, int *status);
239
240
/* A static assertion.  This will cause a compile-time error if EXPR,
241
   which must be a compile-time constant, is false.  */
242
243
#define eu_static_assert(expr)            \
244
  extern int never_defined_just_used_for_checking[(expr) ? 1 : -1]  \
245
    __attribute__ ((unused))
246
247
/* We really want a basename implementation that doesn't modify the
248
   input argument.  Normally you get that from string.h with _GNU_SOURCE
249
   define.  But some libc implementations don't define it and other
250
   define it, but provide an implementation that still modifies the
251
   argument.  So define our own and poison a bare basename symbol.  */
252
static inline const char *
253
xbasename(const char *s)
254
0
{
255
0
  const char *p = strrchr(s, '/');
256
0
  return p ? p+1 : s;
257
0
}
Unexecuted instantiation: fuzz-libelf.c:xbasename
Unexecuted instantiation: elf_version.c:xbasename
Unexecuted instantiation: elf_error.c:xbasename
Unexecuted instantiation: elf_begin.c:xbasename
Unexecuted instantiation: elf_end.c:xbasename
Unexecuted instantiation: elf_rawfile.c:xbasename
Unexecuted instantiation: elf_readall.c:xbasename
Unexecuted instantiation: elf_nextscn.c:xbasename
Unexecuted instantiation: gelf_getshdr.c:xbasename
Unexecuted instantiation: elf_strptr.c:xbasename
Unexecuted instantiation: elf_getdata.c:xbasename
Unexecuted instantiation: elf_getshdrstrndx.c:xbasename
Unexecuted instantiation: elf_compress.c:xbasename
Unexecuted instantiation: elf_compress_gnu.c:xbasename
Unexecuted instantiation: gelf_fsize.c:xbasename
Unexecuted instantiation: gelf_xlate.c:xbasename
Unexecuted instantiation: gelf_getehdr.c:xbasename
Unexecuted instantiation: elf32_getshdr.c:xbasename
Unexecuted instantiation: elf64_getshdr.c:xbasename
Unexecuted instantiation: gelf_xlatetof.c:xbasename
Unexecuted instantiation: elf_getshdrnum.c:xbasename
Unexecuted instantiation: gelf_getchdr.c:xbasename
Unexecuted instantiation: elf32_xlatetof.c:xbasename
Unexecuted instantiation: elf64_xlatetof.c:xbasename
Unexecuted instantiation: elf32_getchdr.c:xbasename
Unexecuted instantiation: elf64_getchdr.c:xbasename
258
#pragma GCC poison basename
259
260
#endif /* system.h */