Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/api.c
Line
Count
Source
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
#include "curl_setup.h"
25
26
#include "urldata.h"
27
#include "api.h"
28
#include "curl_threads.h"
29
#include "multiif.h"
30
#include "vtls/vtls_scache.h"
31
32
typedef char mapi_fn_ids_fit_uint8[
33
  (CURL_MAPI_FN_LAST <= CURL_CBAPI_FN_START) ? 1 : -1];
34
typedef char cbapi_fn_ids_fit_uint8[
35
  ((CURL_CBAPI_FN_LAST - 1) <= UINT8_MAX) ? 1 : -1];
36
typedef char mapi_depth_fits_uint8[
37
  (CURL_MAPI_MAX_RECURSION <= UINT8_MAX) ? 1 : -1];
38
39
struct Curl_eapi_fn_props {
40
  Curl_eapi_fn fn;
41
  uint8_t data_is_killed; /* easy handle is killed in call */
42
  uint8_t recurse;        /* may be called when another call is in progress */
43
  uint8_t readonly;       /* call is not modifying easy handle */
44
  uint8_t no_scache_lock; /* may not be called with easy's vtls_scache
45
                             locked by current thread */
46
};
47
48
static const struct Curl_eapi_fn_props eapi_fn_props[CURL_EAPI_FN_LAST] = {
49
  /* function                   kill rec  RO !sc */
50
  { CURL_EAPI_FN_easy_cleanup,     1,  0,  0,  0 },
51
  { CURL_EAPI_FN_easy_duphandle,   0,  1,  1,  0 },
52
  { CURL_EAPI_FN_easy_getinfo,     0,  1,  1,  0 },
53
  { CURL_EAPI_FN_easy_header,      0,  1,  1,  0 },
54
  { CURL_EAPI_FN_easy_nextheader,  0,  1,  1,  0 },
55
  { CURL_EAPI_FN_easy_pause,       0,  1,  0,  0 },
56
  { CURL_EAPI_FN_easy_perform_ev,  0,  0,  0,  1 },
57
  { CURL_EAPI_FN_easy_perform,     0,  0,  0,  1 },
58
  { CURL_EAPI_FN_easy_recv,        0,  0,  0,  1 },
59
  { CURL_EAPI_FN_easy_reset,       0,  0,  0,  0 },
60
  { CURL_EAPI_FN_easy_send,        0,  0,  0,  1 },
61
  { CURL_EAPI_FN_easy_setopt,      0,  1,  0,  0 },
62
  { CURL_EAPI_FN_easy_ssls_export, 0,  0,  0,  1 },
63
  { CURL_EAPI_FN_easy_ssls_import, 0,  0,  0,  1 },
64
  { CURL_EAPI_FN_easy_upkeep,      0,  0,  0,  1 },
65
  { CURL_EAPI_FN_ws_recv,          0,  1,  0,  1 },
66
  { CURL_EAPI_FN_ws_send,          0,  1,  0,  1 },
67
  { CURL_EAPI_FN_ws_start_frame,   0,  1,  0,  0 },
68
};
69
70
struct Curl_mapi_fn_props {
71
  Curl_mapi_fn fn;
72
  uint8_t multi_is_killed; /* multi handle is killed during call */
73
  uint8_t recurse;         /* may be called when another call is in progress */
74
  uint8_t allow_ntfy_cb;   /* may be called during a notify callback */
75
  uint8_t no_scache_lock;  /* may not be called with multi's vtls_scache
76
                              locked by current thread */
77
};
78
79
static const struct Curl_mapi_fn_props mapi_fn_props[CURL_MAPI_FN_LAST] = {
80
  /* function                       kill rec ntfy !scach */
81
  { CURL_MAPI_FN_multi_add_handle,     0,  0,   1,  0 },
82
  { CURL_MAPI_FN_multi_assign,         0,  1,   1,  0 },
83
  { CURL_MAPI_FN_multi_cleanup,        1,  0,   0,  1 },
84
  { CURL_MAPI_FN_multi_fdset,          0,  0,   1,  0 },
85
  { CURL_MAPI_FN_multi_get_handles,    0,  1,   1,  0 },
86
  { CURL_MAPI_FN_multi_get_offt,       0,  1,   1,  0 },
87
  { CURL_MAPI_FN_multi_info_read,      0,  1,   1,  0 },
88
  { CURL_MAPI_FN_multi_notify_disable, 0,  1,   1,  0 },
89
  { CURL_MAPI_FN_multi_notify_enable,  0,  1,   1,  0 },
90
  { CURL_MAPI_FN_multi_perform,        0,  0,   0,  1 },
91
  { CURL_MAPI_FN_multi_poll,           0,  0,   1,  0 },
92
  { CURL_MAPI_FN_multi_remove_handle,  0,  0,   1,  0 },
93
  { CURL_MAPI_FN_multi_setopt,         0,  0,   1,  0 },
94
  { CURL_MAPI_FN_multi_socket_action,  0,  0,   0,  1 },
95
  { CURL_MAPI_FN_multi_socket_all,     0,  0,   0,  1 },
96
  { CURL_MAPI_FN_multi_socket,         0,  0,   0,  1 },
97
  { CURL_MAPI_FN_multi_timeout,        0,  0,   1,  1 },
98
  { CURL_MAPI_FN_multi_wait,           0,  0,   1,  0 },
99
  { CURL_MAPI_FN_multi_waitfds,        0,  0,   1,  0 },
100
};
101
102
struct Curl_cbapi_fn_props {
103
  Curl_cbapi_fn fn;
104
  uint8_t set_readonly;    /* only allows readonly, recursive easy calls */
105
};
106
107
static const struct Curl_cbapi_fn_props
108
cbapi_fn_props[CURL_CBAPI_FN_LAST - CURL_CBAPI_FN_START] = {
109
  { CURL_CBAPI_FN_easy_chunk_bgn,             0 },
110
  { CURL_CBAPI_FN_easy_chunk_end,             0 },
111
  { CURL_CBAPI_FN_easy_closesocket,           0 },
112
  { CURL_CBAPI_FN_easy_cr_in_read,            0 },
113
  { CURL_CBAPI_FN_easy_cr_in_resume_from,     0 },
114
  { CURL_CBAPI_FN_easy_cw_out_cb,             0 },
115
  { CURL_CBAPI_FN_easy_fdebug,                1 },
116
  { CURL_CBAPI_FN_easy_fnmatch_data,          0 },
117
  { CURL_CBAPI_FN_easy_fopensocket,           0 },
118
  { CURL_CBAPI_FN_easy_fprereq,               0 },
119
  { CURL_CBAPI_FN_easy_fprogress,             0 },
120
  { CURL_CBAPI_FN_easy_fread_func,            0 },
121
  { CURL_CBAPI_FN_easy_fsockopt,              0 },
122
  { CURL_CBAPI_FN_easy_fsslctx,               0 },
123
  { CURL_CBAPI_FN_easy_fwrite_rtp,            0 },
124
  { CURL_CBAPI_FN_easy_fxferinfo,             0 },
125
  { CURL_CBAPI_FN_easy_ioctl_func,            0 },
126
  { CURL_CBAPI_FN_easy_resolver_start,        0 },
127
  { CURL_CBAPI_FN_easy_seek_func,             0 },
128
  { CURL_CBAPI_FN_easy_ssh_hostkeyfunc,       0 },
129
  { CURL_CBAPI_FN_easy_ssh_keyfunc,           0 },
130
  { CURL_CBAPI_FN_easy_trailer_callback,      0 },
131
132
  { CURL_CBAPI_FN_multi_ntfy_cb,              0 },
133
  { CURL_CBAPI_FN_multi_push_cb,              0 },
134
  { CURL_CBAPI_FN_multi_socket_cb,            1 },
135
  { CURL_CBAPI_FN_multi_timer_cb,             1 },
136
};
137
138
static bool eapi_in_readonly_cb(struct Curl_easy *data)
139
278k
{
140
278k
  struct Curl_multi *multi = data->multi;
141
278k
  if(multi && multi->callstack.count) {
142
0
    size_t i;
143
0
    for(i = 0; i < multi->callstack.count; ++i) {
144
0
      if(multi->callstack.calls[i] >= CURL_CBAPI_FN_START) {
145
0
        uint8_t fn = multi->callstack.calls[i];
146
0
        if((fn < CURL_CBAPI_FN_LAST) &&
147
0
           cbapi_fn_props[fn - CURL_CBAPI_FN_START].set_readonly)
148
0
          return TRUE;
149
0
      }
150
0
    }
151
0
  }
152
278k
  return FALSE;
153
278k
}
154
155
static bool mapi_in_ntfy_cb(struct Curl_multi *multi)
156
48.6k
{
157
  /* The exception only applies when the notify callback is
158
   * the latest in the callstack, e.g. the application calls
159
   * directly from it and not from some nested callback. */
160
48.6k
  return multi && multi->callstack.count &&
161
0
    (multi->callstack.calls[multi->callstack.count - 1] ==
162
0
     CURL_CBAPI_FN_multi_ntfy_cb);
163
48.6k
}
164
165
bool Curl_api_multi_is_in_callback(struct Curl_multi *multi)
166
0
{
167
0
  if(multi && multi->callstack.count) {
168
0
    size_t i;
169
0
    for(i = 0; i < multi->callstack.count; ++i) {
170
0
      if(multi->callstack.calls[i] >= CURL_CBAPI_FN_START)
171
0
        return TRUE;
172
0
    }
173
0
  }
174
0
  return FALSE;
175
0
}
176
177
bool Curl_api_is_in_callback(struct Curl_easy *data)
178
0
{
179
0
  if(data && data->multi) {
180
0
    return Curl_api_multi_is_in_callback(data->multi);
181
0
  }
182
0
  return FALSE;
183
0
}
184
185
bool Curl_eapi_enter(struct Curl_eapi_guard *guard,
186
                     CURL *curl,
187
                     Curl_eapi_fn fn,
188
                     CURLcode *presult)
189
376k
{
190
376k
  struct Curl_easy *data = curl;
191
376k
  const struct Curl_eapi_fn_props *fn_props;
192
376k
  CURLcode result = CURLE_OK;
193
194
376k
  guard->depth = 0;
195
196
  /* Verify that we got an easy handle we can work with. */
197
376k
  if(!GOOD_EASY_HANDLE(data) || data->state.internal) {
198
0
    result = CURLE_BAD_FUNCTION_ARGUMENT;
199
0
    goto out;
200
0
  }
201
  /* verify that is either not added to a multi handle OR has a
202
   * GOOD multi handle that knows `data` for `data->mid`. */
203
376k
  if(data->mid != UINT32_MAX) {
204
25.3k
    if(GOOD_MULTI_HANDLE(data->multi)) {
205
25.3k
      if(!Curl_multi_knows_easy(data->multi, data)) {
206
        /* But multi does not know it, something is fishy, better deny call */
207
0
        DEBUGASSERT(0);
208
0
        result = CURLE_BAD_FUNCTION_ARGUMENT;
209
0
        goto out;
210
0
      }
211
25.3k
    }
212
0
    else {
213
0
      DEBUGASSERT(0); /* data needs to have a GOOD multi handle */
214
0
      result = CURLE_BAD_FUNCTION_ARGUMENT;
215
0
      goto out;
216
0
    }
217
25.3k
  }
218
351k
  else if(data->multi) {
219
0
    DEBUGASSERT(0); /* data should not have a multi handle */
220
0
    result = CURLE_BAD_FUNCTION_ARGUMENT;
221
0
    goto out;
222
0
  }
223
  /* verify that the call `fn` we're about to enter is known
224
   * and check call properties to be admitting. */
225
376k
  if(fn >= CURL_EAPI_FN_LAST) {
226
0
    result = CURLE_BAD_FUNCTION_ARGUMENT;
227
0
    goto out;
228
0
  }
229
376k
  fn_props = &eapi_fn_props[fn];
230
376k
  DEBUGASSERT(fn_props->fn == fn);
231
376k
  if(!fn_props->recurse) {
232
15.8k
    if(data->callstack.count) {
233
0
#ifdef CURLVERBOSE
234
0
      DEBUGF(curl_mfprintf(stderr,
235
0
        "EAPI guard: calling %hu with call to %u ongoing\n", (uint16_t)fn,
236
0
        data->callstack.calls[data->callstack.count - 1]));
237
0
#endif
238
0
      result = CURLE_RECURSIVE_API_CALL;
239
0
      goto out;
240
0
    }
241
15.8k
    if(data->multi && data->multi->callstack.count) {
242
0
#ifdef CURLVERBOSE
243
244
0
      DEBUGF(curl_mfprintf(stderr,
245
0
        "EAPI guard: calling %hu with multi call to %u ongoing\n",
246
0
        (uint16_t)fn,
247
0
        data->multi->callstack.calls[data->multi->callstack.count - 1]));
248
0
#endif
249
0
      result = CURLE_RECURSIVE_API_CALL;
250
0
      goto out;
251
0
    }
252
15.8k
  }
253
254
376k
  if(!fn_props->readonly && eapi_in_readonly_cb(data)) {
255
    /* Not allowed to be invoked while an event cb is ongoing */
256
0
#ifdef CURLVERBOSE
257
0
      DEBUGF(curl_mfprintf(stderr,
258
0
        "EAPI guard: calling %hu in readonly enforcing callback\n",
259
0
        (uint16_t)fn));
260
0
#endif
261
0
    result = CURLE_RECURSIVE_API_CALL;
262
0
    goto out;
263
0
  }
264
265
376k
#if defined(USE_SSL) && defined(USE_MUTEX)
266
376k
  if(fn_props->no_scache_lock &&
267
5.88k
     Curl_ssl_scache_is_locked_by_current_thread(data)) {
268
0
#ifdef CURLVERBOSE
269
0
      DEBUGF(curl_mfprintf(stderr,
270
0
        "EAPI guard: calling %hu while vtls_scache is locked by "
271
0
        "current thread\n", (uint16_t)fn));
272
0
#endif
273
0
    result = CURLE_RECURSIVE_API_CALL;
274
0
    goto out;
275
0
  }
276
376k
#endif
277
278
  /* all fine, add to data's callstack */
279
376k
  if(data->callstack.count >= CURL_EAPI_MAX_RECURSION) {
280
0
    result = CURLE_RECURSIVE_API_CALL;
281
0
    goto out;
282
0
  }
283
376k
  data->callstack.calls[data->callstack.count] = (uint16_t)fn;
284
376k
  ++data->callstack.count;
285
376k
  guard->depth = data->callstack.count;
286
376k
  guard->data = fn_props->data_is_killed ? NULL : data;
287
288
376k
out:
289
376k
  if(presult)
290
366k
    *presult = result;
291
376k
  return guard->depth > 0;
292
376k
}
293
294
void Curl_eapi_leave(struct Curl_eapi_guard *guard)
295
376k
{
296
376k
  if(guard->depth) {
297
    /* guard->data is set when handle is supposed to stay alive during call */
298
376k
    if(guard->data && GOOD_EASY_HANDLE(guard->data)) {
299
366k
      if(guard->depth > guard->data->callstack.count) {
300
0
        DEBUGASSERT(0); /* something very wrong */
301
0
      }
302
366k
      else {
303
366k
        if(guard->depth < guard->data->callstack.count) {
304
0
          DEBUGASSERT(0); /* someone forgot to clean up */
305
0
        }
306
        /* reset to depth the guard was entered in */
307
366k
        guard->data->callstack.count = (uint16_t)(guard->depth - 1);
308
366k
      }
309
366k
    }
310
376k
  }
311
376k
}
312
313
CURLHcode Curl_eapi_hcode(CURLcode result)
314
0
{
315
0
  switch(result) {
316
0
  case CURLE_OK:
317
0
    return CURLHE_OK;
318
0
  case CURLE_BAD_FUNCTION_ARGUMENT:
319
0
    return CURLHE_BAD_ARGUMENT;
320
0
  case CURLE_OUT_OF_MEMORY:
321
0
    return CURLHE_OUT_OF_MEMORY;
322
0
  case CURLE_NOT_BUILT_IN:
323
0
    return CURLHE_NOT_BUILT_IN;
324
0
  default:
325
    /* Unfortunately, we cannot convert RECURSIVE_API_CALL,
326
     * but since the header API is reentrant, this should not happen. */
327
0
    return CURLHE_BAD_ARGUMENT;
328
0
  }
329
0
}
330
331
bool Curl_mapi_enter(struct Curl_mapi_guard *guard,
332
                     CURLM *m,
333
                     Curl_mapi_fn fn,
334
                     CURLMcode *pmresult)
335
78.8k
{
336
78.8k
  struct Curl_multi *multi = m;
337
78.8k
  const struct Curl_mapi_fn_props *fn_props;
338
78.8k
  CURLMcode mresult = CURLM_OK;
339
340
78.8k
  guard->depth = 0;
341
342
  /* Verify that we got an easy handle we can work with. */
343
78.8k
  if(!GOOD_MULTI_HANDLE(multi)) {
344
0
    mresult = CURLM_BAD_HANDLE;
345
0
    goto out;
346
0
  }
347
78.8k
  if(fn >= CURL_MAPI_FN_LAST) {
348
0
    mresult = CURLM_BAD_FUNCTION_ARGUMENT;
349
0
    goto out;
350
0
  }
351
78.8k
  fn_props = &mapi_fn_props[fn];
352
78.8k
  DEBUGASSERT(fn_props->fn == fn);
353
78.8k
  if(fn_props->allow_ntfy_cb && mapi_in_ntfy_cb(multi)) {
354
    /* explicitly allowed, even though normal recursion may not */
355
0
  }
356
78.8k
  else if(!fn_props->recurse && multi->callstack.count) {
357
0
#ifdef CURLVERBOSE
358
0
      DEBUGF(curl_mfprintf(stderr,
359
0
        "MAPI guard: calling %hu with call to %u ongoing\n", (uint16_t)fn,
360
0
        multi->callstack.calls[multi->callstack.count - 1]));
361
0
#endif
362
0
    mresult = CURLM_RECURSIVE_API_CALL;
363
0
    goto out;
364
0
  }
365
366
78.8k
#if defined(USE_SSL) && defined(USE_MUTEX)
367
78.8k
  if(fn_props->no_scache_lock && multi->ssl_scache &&
368
30.2k
     Curl_ssl_scache_is_locked_by_current_thread(multi->admin)) {
369
0
#ifdef CURLVERBOSE
370
0
      DEBUGF(curl_mfprintf(stderr,
371
0
        "MAPI guard: calling %hu while its vtls_scache is locked by "
372
0
        "current thread\n", (uint16_t)fn));
373
0
#endif
374
0
    mresult = CURLM_RECURSIVE_API_CALL;
375
0
    goto out;
376
0
  }
377
78.8k
#endif
378
379
  /* all fine, add to data's callstack */
380
78.8k
  if(multi->callstack.count >= CURL_MAPI_MAX_RECURSION) {
381
0
    mresult = CURLM_RECURSIVE_API_CALL;
382
0
    goto out;
383
0
  }
384
78.8k
  multi->callstack.calls[multi->callstack.count] = (uint8_t)fn;
385
78.8k
  ++multi->callstack.count;
386
78.8k
  guard->depth = multi->callstack.count;
387
78.8k
  guard->multi = fn_props->multi_is_killed ? NULL : multi;
388
389
78.8k
out:
390
78.8k
  if(pmresult)
391
49.7k
    *pmresult = mresult;
392
78.8k
  return guard->depth > 0;
393
78.8k
}
394
395
void Curl_mapi_leave(struct Curl_mapi_guard *guard)
396
180k
{
397
180k
  if(guard->depth) {
398
    /* guard->data is set when handle is supposed to stay alive during call */
399
178k
    if(guard->multi && GOOD_MULTI_HANDLE(guard->multi)) {
400
169k
      if(guard->depth > guard->multi->callstack.count) {
401
0
        DEBUGASSERT(0); /* something very wrong */
402
0
      }
403
169k
      else {
404
169k
        if(guard->depth < guard->multi->callstack.count) {
405
0
          DEBUGASSERT(0); /* someone forgot to clean up */
406
0
        }
407
        /* reset to depth the guard was entered in */
408
169k
        guard->multi->callstack.count = (uint8_t)(guard->depth - 1);
409
169k
      }
410
169k
    }
411
178k
  }
412
180k
}
413
414
void Curl_cbapi_enter(struct Curl_mapi_guard *guard,
415
                      struct Curl_easy *data,
416
                      struct Curl_multi *multi,
417
                      Curl_cbapi_fn fn)
418
101k
{
419
101k
  guard->depth = 0;
420
421
101k
  if(!multi)
422
101k
    multi = data ? data->multi : NULL;
423
  /* if not multi is involved here, just leave */
424
101k
  if(!multi)
425
1.61k
    return;
426
  /* invalid callback specifier? */
427
99.9k
  if((fn >= CURL_CBAPI_FN_LAST) || (fn < CURL_CBAPI_FN_START)) {
428
0
    DEBUGASSERT(0);
429
0
    return;
430
0
  }
431
99.9k
  DEBUGASSERT(cbapi_fn_props[fn - CURL_CBAPI_FN_START].fn == fn);
432
  /* all fine, add to data's callstack */
433
  /* if multi callstack already at max depth, leave */
434
99.9k
  if(multi->callstack.count >= CURL_MAPI_MAX_RECURSION) {
435
0
    DEBUGASSERT(0);
436
0
    return;
437
0
  }
438
99.9k
  multi->callstack.calls[multi->callstack.count] = (uint8_t)fn;
439
99.9k
  ++multi->callstack.count;
440
99.9k
  guard->depth = multi->callstack.count;
441
99.9k
  guard->multi = multi;
442
99.9k
}
443
444
void Curl_cbapi_leave(struct Curl_mapi_guard *guard)
445
101k
{
446
101k
  Curl_mapi_leave(guard);
447
101k
}