Coverage Report

Created: 2023-06-07 07:02

/src/curl/lib/memdebug.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
#include "curl_setup.h"
26
27
#ifdef CURLDEBUG
28
29
#include <curl/curl.h>
30
31
#include "urldata.h"
32
33
#define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
34
35
/* The last 3 #include files should be in this order */
36
#include "curl_printf.h"
37
#include "curl_memory.h"
38
#include "memdebug.h"
39
40
struct memdebug {
41
  size_t size;
42
  union {
43
    curl_off_t o;
44
    double d;
45
    void *p;
46
  } mem[1];
47
  /* I'm hoping this is the thing with the strictest alignment
48
   * requirements.  That also means we waste some space :-( */
49
};
50
51
/*
52
 * Note that these debug functions are very simple and they are meant to
53
 * remain so. For advanced analysis, record a log file and write perl scripts
54
 * to analyze them!
55
 *
56
 * Don't use these with multithreaded test programs!
57
 */
58
59
FILE *curl_dbg_logfile = NULL;
60
static bool registered_cleanup = FALSE; /* atexit registered cleanup */
61
static bool memlimit = FALSE; /* enable memory limit */
62
static long memsize = 0;  /* set number of mallocs allowed */
63
64
/* LeakSantizier (LSAN) calls _exit() instead of exit() when a leak is detected
65
   on exit so the logfile must be closed explicitly or data could be lost.
66
   Though _exit() does not call atexit handlers such as this, LSAN's call to
67
   _exit() comes after the atexit handlers are called. curl/curl#6620 */
68
static void curl_dbg_cleanup(void)
69
0
{
70
0
  if(curl_dbg_logfile &&
71
0
     curl_dbg_logfile != stderr &&
72
0
     curl_dbg_logfile != stdout) {
73
0
    fclose(curl_dbg_logfile);
74
0
  }
75
0
  curl_dbg_logfile = NULL;
76
0
}
77
78
/* this sets the log file name */
79
void curl_dbg_memdebug(const char *logname)
80
0
{
81
0
  if(!curl_dbg_logfile) {
82
0
    if(logname && *logname)
83
0
      curl_dbg_logfile = fopen(logname, FOPEN_WRITETEXT);
84
0
    else
85
0
      curl_dbg_logfile = stderr;
86
#ifdef MEMDEBUG_LOG_SYNC
87
    /* Flush the log file after every line so the log isn't lost in a crash */
88
    if(curl_dbg_logfile)
89
      setbuf(curl_dbg_logfile, (char *)NULL);
90
#endif
91
0
  }
92
0
  if(!registered_cleanup)
93
0
    registered_cleanup = !atexit(curl_dbg_cleanup);
94
0
}
95
96
/* This function sets the number of malloc() calls that should return
97
   successfully! */
98
void curl_dbg_memlimit(long limit)
99
0
{
100
0
  if(!memlimit) {
101
0
    memlimit = TRUE;
102
0
    memsize = limit;
103
0
  }
104
0
}
105
106
/* returns TRUE if this isn't allowed! */
107
static bool countcheck(const char *func, int line, const char *source)
108
78.9k
{
109
  /* if source is NULL, then the call is made internally and this check
110
     should not be made */
111
78.9k
  if(memlimit && source) {
112
0
    if(!memsize) {
113
      /* log to file */
114
0
      curl_dbg_log("LIMIT %s:%d %s reached memlimit\n",
115
0
                   source, line, func);
116
      /* log to stderr also */
117
0
      fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
118
0
              source, line, func);
119
0
      fflush(curl_dbg_logfile); /* because it might crash now */
120
0
      errno = ENOMEM;
121
0
      return TRUE; /* RETURN ERROR! */
122
0
    }
123
0
    else
124
0
      memsize--; /* countdown */
125
126
127
0
  }
128
129
78.9k
  return FALSE; /* allow this */
130
78.9k
}
131
132
ALLOC_FUNC void *curl_dbg_malloc(size_t wantedsize,
133
                                 int line, const char *source)
134
50.5k
{
135
50.5k
  struct memdebug *mem;
136
50.5k
  size_t size;
137
138
50.5k
  DEBUGASSERT(wantedsize != 0);
139
140
50.5k
  if(countcheck("malloc", line, source))
141
0
    return NULL;
142
143
  /* alloc at least 64 bytes */
144
50.5k
  size = sizeof(struct memdebug) + wantedsize;
145
146
50.5k
  mem = (Curl_cmalloc)(size);
147
50.5k
  if(mem) {
148
50.5k
    mem->size = wantedsize;
149
50.5k
  }
150
151
50.5k
  if(source)
152
26.0k
    curl_dbg_log("MEM %s:%d malloc(%zu) = %p\n",
153
26.0k
                 source, line, wantedsize,
154
26.0k
                 mem ? (void *)mem->mem : (void *)0);
155
156
50.5k
  return (mem ? mem->mem : NULL);
157
50.5k
}
158
159
ALLOC_FUNC void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size,
160
                                 int line, const char *source)
161
3.87k
{
162
3.87k
  struct memdebug *mem;
163
3.87k
  size_t size, user_size;
164
165
3.87k
  DEBUGASSERT(wanted_elements != 0);
166
3.87k
  DEBUGASSERT(wanted_size != 0);
167
168
3.87k
  if(countcheck("calloc", line, source))
169
0
    return NULL;
170
171
  /* alloc at least 64 bytes */
172
3.87k
  user_size = wanted_size * wanted_elements;
173
3.87k
  size = sizeof(struct memdebug) + user_size;
174
175
3.87k
  mem = (Curl_ccalloc)(1, size);
176
3.87k
  if(mem)
177
3.87k
    mem->size = user_size;
178
179
3.87k
  if(source)
180
3.87k
    curl_dbg_log("MEM %s:%d calloc(%zu,%zu) = %p\n",
181
3.87k
                 source, line, wanted_elements, wanted_size,
182
3.87k
                 mem ? (void *)mem->mem : (void *)0);
183
184
3.87k
  return (mem ? mem->mem : NULL);
185
3.87k
}
186
187
ALLOC_FUNC char *curl_dbg_strdup(const char *str,
188
                                 int line, const char *source)
189
24.4k
{
190
24.4k
  char *mem;
191
24.4k
  size_t len;
192
193
24.4k
  DEBUGASSERT(str != NULL);
194
195
24.4k
  if(countcheck("strdup", line, source))
196
0
    return NULL;
197
198
24.4k
  len = strlen(str) + 1;
199
200
24.4k
  mem = curl_dbg_malloc(len, 0, NULL); /* NULL prevents logging */
201
24.4k
  if(mem)
202
24.4k
    memcpy(mem, str, len);
203
204
24.4k
  if(source)
205
24.4k
    curl_dbg_log("MEM %s:%d strdup(%p) (%zu) = %p\n",
206
24.4k
                 source, line, (const void *)str, len, (const void *)mem);
207
208
24.4k
  return mem;
209
24.4k
}
210
211
#if defined(WIN32) && defined(UNICODE)
212
ALLOC_FUNC wchar_t *curl_dbg_wcsdup(const wchar_t *str,
213
                                    int line, const char *source)
214
{
215
  wchar_t *mem;
216
  size_t wsiz, bsiz;
217
218
  DEBUGASSERT(str != NULL);
219
220
  if(countcheck("wcsdup", line, source))
221
    return NULL;
222
223
  wsiz = wcslen(str) + 1;
224
  bsiz = wsiz * sizeof(wchar_t);
225
226
  mem = curl_dbg_malloc(bsiz, 0, NULL); /* NULL prevents logging */
227
  if(mem)
228
    memcpy(mem, str, bsiz);
229
230
  if(source)
231
    curl_dbg_log("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
232
                source, line, (void *)str, bsiz, (void *)mem);
233
234
  return mem;
235
}
236
#endif
237
238
/* We provide a realloc() that accepts a NULL as pointer, which then
239
   performs a malloc(). In order to work with ares. */
240
void *curl_dbg_realloc(void *ptr, size_t wantedsize,
241
                      int line, const char *source)
242
0
{
243
0
  struct memdebug *mem = NULL;
244
245
0
  size_t size = sizeof(struct memdebug) + wantedsize;
246
247
0
  DEBUGASSERT(wantedsize != 0);
248
249
0
  if(countcheck("realloc", line, source))
250
0
    return NULL;
251
252
#ifdef __INTEL_COMPILER
253
#  pragma warning(push)
254
#  pragma warning(disable:1684)
255
   /* 1684: conversion from pointer to same-sized integral type */
256
#endif
257
258
0
  if(ptr)
259
0
    mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
260
261
#ifdef __INTEL_COMPILER
262
#  pragma warning(pop)
263
#endif
264
265
0
  mem = (Curl_crealloc)(mem, size);
266
0
  if(source)
267
0
    curl_dbg_log("MEM %s:%d realloc(%p, %zu) = %p\n",
268
0
                source, line, (void *)ptr, wantedsize,
269
0
                mem ? (void *)mem->mem : (void *)0);
270
271
0
  if(mem) {
272
0
    mem->size = wantedsize;
273
0
    return mem->mem;
274
0
  }
275
276
0
  return NULL;
277
0
}
278
279
void curl_dbg_free(void *ptr, int line, const char *source)
280
230k
{
281
230k
  if(ptr) {
282
54.4k
    struct memdebug *mem;
283
284
#ifdef __INTEL_COMPILER
285
#  pragma warning(push)
286
#  pragma warning(disable:1684)
287
   /* 1684: conversion from pointer to same-sized integral type */
288
#endif
289
290
54.4k
    mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
291
292
#ifdef __INTEL_COMPILER
293
#  pragma warning(pop)
294
#endif
295
296
    /* free for real */
297
54.4k
    (Curl_cfree)(mem);
298
54.4k
  }
299
300
230k
  if(source && ptr)
301
54.4k
    curl_dbg_log("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
302
230k
}
303
304
curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
305
                             int line, const char *source)
306
0
{
307
0
  const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
308
0
    "FD %s:%d socket() = %d\n" :
309
0
    (sizeof(curl_socket_t) == sizeof(long)) ?
310
0
    "FD %s:%d socket() = %ld\n" :
311
0
    "FD %s:%d socket() = %zd\n";
312
313
0
  curl_socket_t sockfd;
314
315
0
  if(countcheck("socket", line, source))
316
0
    return CURL_SOCKET_BAD;
317
318
0
  sockfd = socket(domain, type, protocol);
319
320
0
  if(source && (sockfd != CURL_SOCKET_BAD))
321
0
    curl_dbg_log(fmt, source, line, sockfd);
322
323
0
  return sockfd;
324
0
}
325
326
SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
327
                            SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
328
                            SEND_TYPE_ARG3 len, SEND_TYPE_ARG4 flags, int line,
329
                            const char *source)
330
0
{
331
0
  SEND_TYPE_RETV rc;
332
0
  if(countcheck("send", line, source))
333
0
    return -1;
334
0
  rc = send(sockfd, buf, len, flags);
335
0
  if(source)
336
0
    curl_dbg_log("SEND %s:%d send(%lu) = %ld\n",
337
0
                source, line, (unsigned long)len, (long)rc);
338
0
  return rc;
339
0
}
340
341
RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd, RECV_TYPE_ARG2 buf,
342
                            RECV_TYPE_ARG3 len, RECV_TYPE_ARG4 flags, int line,
343
                            const char *source)
344
0
{
345
0
  RECV_TYPE_RETV rc;
346
0
  if(countcheck("recv", line, source))
347
0
    return -1;
348
0
  rc = recv(sockfd, buf, len, flags);
349
0
  if(source)
350
0
    curl_dbg_log("RECV %s:%d recv(%lu) = %ld\n",
351
0
                source, line, (unsigned long)len, (long)rc);
352
0
  return rc;
353
0
}
354
355
#ifdef HAVE_SOCKETPAIR
356
int curl_dbg_socketpair(int domain, int type, int protocol,
357
                       curl_socket_t socket_vector[2],
358
                       int line, const char *source)
359
0
{
360
0
  const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
361
0
    "FD %s:%d socketpair() = %d %d\n" :
362
0
    (sizeof(curl_socket_t) == sizeof(long)) ?
363
0
    "FD %s:%d socketpair() = %ld %ld\n" :
364
0
    "FD %s:%d socketpair() = %zd %zd\n";
365
366
0
  int res = socketpair(domain, type, protocol, socket_vector);
367
368
0
  if(source && (0 == res))
369
0
    curl_dbg_log(fmt, source, line, socket_vector[0], socket_vector[1]);
370
371
0
  return res;
372
0
}
373
#endif
374
375
curl_socket_t curl_dbg_accept(curl_socket_t s, void *saddr, void *saddrlen,
376
                             int line, const char *source)
377
0
{
378
0
  const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
379
0
    "FD %s:%d accept() = %d\n" :
380
0
    (sizeof(curl_socket_t) == sizeof(long)) ?
381
0
    "FD %s:%d accept() = %ld\n" :
382
0
    "FD %s:%d accept() = %zd\n";
383
384
0
  struct sockaddr *addr = (struct sockaddr *)saddr;
385
0
  curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
386
387
0
  curl_socket_t sockfd = accept(s, addr, addrlen);
388
389
0
  if(source && (sockfd != CURL_SOCKET_BAD))
390
0
    curl_dbg_log(fmt, source, line, sockfd);
391
392
0
  return sockfd;
393
0
}
394
395
/* separate function to allow libcurl to mark a "faked" close */
396
void curl_dbg_mark_sclose(curl_socket_t sockfd, int line, const char *source)
397
0
{
398
0
  const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
399
0
    "FD %s:%d sclose(%d)\n":
400
0
    (sizeof(curl_socket_t) == sizeof(long)) ?
401
0
    "FD %s:%d sclose(%ld)\n":
402
0
    "FD %s:%d sclose(%zd)\n";
403
404
0
  if(source)
405
0
    curl_dbg_log(fmt, source, line, sockfd);
406
0
}
407
408
/* this is our own defined way to close sockets on *ALL* platforms */
409
int curl_dbg_sclose(curl_socket_t sockfd, int line, const char *source)
410
0
{
411
0
  int res = sclose(sockfd);
412
0
  curl_dbg_mark_sclose(sockfd, line, source);
413
0
  return res;
414
0
}
415
416
ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode,
417
                                int line, const char *source)
418
2.13k
{
419
2.13k
  FILE *res = fopen(file, mode);
420
421
2.13k
  if(source)
422
2.13k
    curl_dbg_log("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
423
2.13k
                source, line, file, mode, (void *)res);
424
425
2.13k
  return res;
426
2.13k
}
427
428
ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode,
429
                                 int line, const char *source)
430
0
{
431
0
  FILE *res = fdopen(filedes, mode);
432
0
  if(source)
433
0
    curl_dbg_log("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
434
0
                 source, line, filedes, mode, (void *)res);
435
0
  return res;
436
0
}
437
438
int curl_dbg_fclose(FILE *file, int line, const char *source)
439
2.13k
{
440
2.13k
  int res;
441
442
2.13k
  DEBUGASSERT(file != NULL);
443
444
2.13k
  if(source)
445
2.13k
    curl_dbg_log("FILE %s:%d fclose(%p)\n",
446
2.13k
                 source, line, (void *)file);
447
448
2.13k
  res = fclose(file);
449
450
2.13k
  return res;
451
2.13k
}
452
453
0
#define LOGLINE_BUFSIZE  1024
454
455
/* this does the writing to the memory tracking log file */
456
void curl_dbg_log(const char *format, ...)
457
113k
{
458
113k
  char *buf;
459
113k
  int nchars;
460
113k
  va_list ap;
461
462
113k
  if(!curl_dbg_logfile)
463
113k
    return;
464
465
0
  buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
466
0
  if(!buf)
467
0
    return;
468
469
0
  va_start(ap, format);
470
0
  nchars = mvsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
471
0
  va_end(ap);
472
473
0
  if(nchars > LOGLINE_BUFSIZE - 1)
474
0
    nchars = LOGLINE_BUFSIZE - 1;
475
476
0
  if(nchars > 0)
477
0
    fwrite(buf, 1, (size_t)nchars, curl_dbg_logfile);
478
479
0
  (Curl_cfree)(buf);
480
0
}
481
482
#endif /* CURLDEBUG */