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