Coverage Report

Created: 2025-07-04 06:49

/src/cpython/Python/importdl.c
Line
Count
Source (jump to first uncovered line)
1
2
/* Support for dynamic loading of extension modules */
3
4
#include "Python.h"
5
#include "pycore_call.h"          // _PyObject_CallMethod()
6
#include "pycore_import.h"        // _PyImport_SwapPackageContext()
7
#include "pycore_importdl.h"
8
#include "pycore_moduleobject.h"  // _PyModule_GetDef()
9
#include "pycore_pyerrors.h"      // _PyErr_FormatFromCause()
10
#include "pycore_runtime.h"       // _Py_ID()
11
12
13
/* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
14
   supported on this platform. configure will then compile and link in one
15
   of the dynload_*.c files, as appropriate. We will call a function in
16
   those modules to get a function pointer to the module's init function.
17
*/
18
#ifdef HAVE_DYNAMIC_LOADING
19
20
#ifdef MS_WINDOWS
21
extern dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
22
                                                     const char *shortname,
23
                                                     PyObject *pathname,
24
                                                     FILE *fp);
25
#else
26
extern dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
27
                                              const char *shortname,
28
                                              const char *pathname, FILE *fp);
29
#endif
30
31
#endif /* HAVE_DYNAMIC_LOADING */
32
33
34
/***********************************/
35
/* module info to use when loading */
36
/***********************************/
37
38
static const char * const ascii_only_prefix = "PyInit";
39
static const char * const nonascii_prefix = "PyInitU";
40
41
/* Get the variable part of a module's export symbol name.
42
 * Returns a bytes instance. For non-ASCII-named modules, the name is
43
 * encoded as per PEP 489.
44
 * The hook_prefix pointer is set to either ascii_only_prefix or
45
 * nonascii_prefix, as appropriate.
46
 */
47
static PyObject *
48
79
get_encoded_name(PyObject *name, const char **hook_prefix) {
49
79
    PyObject *tmp;
50
79
    PyObject *encoded = NULL;
51
79
    PyObject *modname = NULL;
52
79
    Py_ssize_t name_len, lastdot;
53
54
    /* Get the short name (substring after last dot) */
55
79
    name_len = PyUnicode_GetLength(name);
56
79
    if (name_len < 0) {
57
0
        return NULL;
58
0
    }
59
79
    lastdot = PyUnicode_FindChar(name, '.', 0, name_len, -1);
60
79
    if (lastdot < -1) {
61
0
        return NULL;
62
79
    } else if (lastdot >= 0) {
63
0
        tmp = PyUnicode_Substring(name, lastdot + 1, name_len);
64
0
        if (tmp == NULL)
65
0
            return NULL;
66
0
        name = tmp;
67
        /* "name" now holds a new reference to the substring */
68
79
    } else {
69
79
        Py_INCREF(name);
70
79
    }
71
72
    /* Encode to ASCII or Punycode, as needed */
73
79
    encoded = PyUnicode_AsEncodedString(name, "ascii", NULL);
74
79
    if (encoded != NULL) {
75
79
        *hook_prefix = ascii_only_prefix;
76
79
    } else {
77
0
        if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
78
0
            PyErr_Clear();
79
0
            encoded = PyUnicode_AsEncodedString(name, "punycode", NULL);
80
0
            if (encoded == NULL) {
81
0
                goto error;
82
0
            }
83
0
            *hook_prefix = nonascii_prefix;
84
0
        } else {
85
0
            goto error;
86
0
        }
87
0
    }
88
89
    /* Replace '-' by '_' */
90
79
    modname = _PyObject_CallMethod(encoded, &_Py_ID(replace), "cc", '-', '_');
91
79
    if (modname == NULL)
92
0
        goto error;
93
94
79
    Py_DECREF(name);
95
79
    Py_DECREF(encoded);
96
79
    return modname;
97
0
error:
98
0
    Py_DECREF(name);
99
0
    Py_XDECREF(encoded);
100
0
    return NULL;
101
79
}
102
103
void
104
_Py_ext_module_loader_info_clear(struct _Py_ext_module_loader_info *info)
105
375
{
106
375
    Py_CLEAR(info->filename);
107
375
#ifndef MS_WINDOWS
108
375
    Py_CLEAR(info->filename_encoded);
109
375
#endif
110
375
    Py_CLEAR(info->name);
111
375
    Py_CLEAR(info->name_encoded);
112
375
}
113
114
int
115
_Py_ext_module_loader_info_init(struct _Py_ext_module_loader_info *p_info,
116
                                PyObject *name, PyObject *filename,
117
                                _Py_ext_module_origin origin)
118
79
{
119
79
    struct _Py_ext_module_loader_info info = {
120
79
        .origin=origin,
121
79
    };
122
123
79
    assert(name != NULL);
124
79
    if (!PyUnicode_Check(name)) {
125
0
        PyErr_SetString(PyExc_TypeError,
126
0
                        "module name must be a string");
127
0
        _Py_ext_module_loader_info_clear(&info);
128
0
        return -1;
129
0
    }
130
79
    assert(PyUnicode_GetLength(name) > 0);
131
79
    info.name = Py_NewRef(name);
132
133
79
    info.name_encoded = get_encoded_name(info.name, &info.hook_prefix);
134
79
    if (info.name_encoded == NULL) {
135
0
        _Py_ext_module_loader_info_clear(&info);
136
0
        return -1;
137
0
    }
138
139
79
    info.newcontext = PyUnicode_AsUTF8(info.name);
140
79
    if (info.newcontext == NULL) {
141
0
        _Py_ext_module_loader_info_clear(&info);
142
0
        return -1;
143
0
    }
144
145
79
    if (filename != NULL) {
146
79
        if (!PyUnicode_Check(filename)) {
147
0
            PyErr_SetString(PyExc_TypeError,
148
0
                            "module filename must be a string");
149
0
            _Py_ext_module_loader_info_clear(&info);
150
0
            return -1;
151
0
        }
152
79
        info.filename = Py_NewRef(filename);
153
154
79
#ifndef MS_WINDOWS
155
79
        info.filename_encoded = PyUnicode_EncodeFSDefault(info.filename);
156
79
        if (info.filename_encoded == NULL) {
157
0
            _Py_ext_module_loader_info_clear(&info);
158
0
            return -1;
159
0
        }
160
79
#endif
161
162
79
        info.path = info.filename;
163
79
    }
164
0
    else {
165
0
        info.path = info.name;
166
0
    }
167
168
79
    *p_info = info;
169
79
    return 0;
170
79
}
171
172
int
173
_Py_ext_module_loader_info_init_for_builtin(
174
                            struct _Py_ext_module_loader_info *info,
175
                            PyObject *name)
176
296
{
177
296
    assert(PyUnicode_Check(name));
178
296
    assert(PyUnicode_FindChar(name, '.', 0, PyUnicode_GetLength(name), -1) == -1);
179
296
    assert(PyUnicode_GetLength(name) > 0);
180
181
296
    PyObject *name_encoded = PyUnicode_AsEncodedString(name, "ascii", NULL);
182
296
    if (name_encoded == NULL) {
183
0
        return -1;
184
0
    }
185
186
296
    *info = (struct _Py_ext_module_loader_info){
187
296
        .name=Py_NewRef(name),
188
296
        .name_encoded=name_encoded,
189
        /* We won't need filename. */
190
296
        .path=name,
191
296
        .origin=_Py_ext_module_origin_BUILTIN,
192
296
        .hook_prefix=ascii_only_prefix,
193
296
        .newcontext=NULL,
194
296
    };
195
296
    return 0;
196
296
}
197
198
int
199
_Py_ext_module_loader_info_init_for_core(
200
                            struct _Py_ext_module_loader_info *info,
201
                            PyObject *name)
202
0
{
203
0
    if (_Py_ext_module_loader_info_init_for_builtin(info, name) < 0) {
204
0
        return -1;
205
0
    }
206
0
    info->origin = _Py_ext_module_origin_CORE;
207
0
    return 0;
208
0
}
209
210
#ifdef HAVE_DYNAMIC_LOADING
211
int
212
_Py_ext_module_loader_info_init_from_spec(
213
                            struct _Py_ext_module_loader_info *p_info,
214
                            PyObject *spec)
215
79
{
216
79
    PyObject *name = PyObject_GetAttrString(spec, "name");
217
79
    if (name == NULL) {
218
0
        return -1;
219
0
    }
220
79
    PyObject *filename = PyObject_GetAttrString(spec, "origin");
221
79
    if (filename == NULL) {
222
0
        Py_DECREF(name);
223
0
        return -1;
224
0
    }
225
    /* We could also accommodate builtin modules here without much trouble. */
226
79
    _Py_ext_module_origin origin = _Py_ext_module_origin_DYNAMIC;
227
79
    int err = _Py_ext_module_loader_info_init(p_info, name, filename, origin);
228
79
    Py_DECREF(name);
229
79
    Py_DECREF(filename);
230
79
    return err;
231
79
}
232
#endif /* HAVE_DYNAMIC_LOADING */
233
234
235
/********************************/
236
/* module init function results */
237
/********************************/
238
239
void
240
_Py_ext_module_loader_result_clear(struct _Py_ext_module_loader_result *res)
241
375
{
242
    /* Instead, the caller should have called
243
     * _Py_ext_module_loader_result_apply_error(). */
244
375
    assert(res->err == NULL);
245
375
    *res = (struct _Py_ext_module_loader_result){0};
246
375
}
247
248
static void
249
_Py_ext_module_loader_result_set_error(
250
                            struct _Py_ext_module_loader_result *res,
251
                            enum _Py_ext_module_loader_result_error_kind kind)
252
0
{
253
#ifndef NDEBUG
254
    switch (kind) {
255
    case _Py_ext_module_loader_result_EXCEPTION: _Py_FALLTHROUGH;
256
    case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC:
257
        assert(PyErr_Occurred());
258
        break;
259
    case _Py_ext_module_loader_result_ERR_MISSING: _Py_FALLTHROUGH;
260
    case _Py_ext_module_loader_result_ERR_UNINITIALIZED: _Py_FALLTHROUGH;
261
    case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: _Py_FALLTHROUGH;
262
    case _Py_ext_module_loader_result_ERR_NOT_MODULE: _Py_FALLTHROUGH;
263
    case _Py_ext_module_loader_result_ERR_MISSING_DEF:
264
        assert(!PyErr_Occurred());
265
        break;
266
    default:
267
        /* We added a new error kind but forgot to add it to this switch. */
268
        assert(0);
269
    }
270
#endif
271
272
0
    assert(res->err == NULL && res->_err.exc == NULL);
273
0
    res->err = &res->_err;
274
0
    *res->err = (struct _Py_ext_module_loader_result_error){
275
0
        .kind=kind,
276
0
        .exc=PyErr_GetRaisedException(),
277
0
    };
278
279
    /* For some kinds, we also set/check res->kind. */
280
0
    switch (kind) {
281
0
    case _Py_ext_module_loader_result_ERR_UNINITIALIZED:
282
0
        assert(res->kind == _Py_ext_module_kind_UNKNOWN);
283
0
        res->kind = _Py_ext_module_kind_INVALID;
284
0
        break;
285
    /* None of the rest affect the result kind. */
286
0
    case _Py_ext_module_loader_result_EXCEPTION: _Py_FALLTHROUGH;
287
0
    case _Py_ext_module_loader_result_ERR_MISSING: _Py_FALLTHROUGH;
288
0
    case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: _Py_FALLTHROUGH;
289
0
    case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: _Py_FALLTHROUGH;
290
0
    case _Py_ext_module_loader_result_ERR_NOT_MODULE: _Py_FALLTHROUGH;
291
0
    case _Py_ext_module_loader_result_ERR_MISSING_DEF:
292
0
        break;
293
0
    default:
294
        /* We added a new error kind but forgot to add it to this switch. */
295
0
        assert(0);
296
0
    }
297
0
}
298
299
void
300
_Py_ext_module_loader_result_apply_error(
301
                            struct _Py_ext_module_loader_result *res,
302
                            const char *name)
303
0
{
304
0
    assert(!PyErr_Occurred());
305
0
    assert(res->err != NULL && res->err == &res->_err);
306
0
    struct _Py_ext_module_loader_result_error err = *res->err;
307
0
    res->err = NULL;
308
309
    /* We're otherwise done with the result at this point. */
310
0
    _Py_ext_module_loader_result_clear(res);
311
312
#ifndef NDEBUG
313
    switch (err.kind) {
314
    case _Py_ext_module_loader_result_EXCEPTION: _Py_FALLTHROUGH;
315
    case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC:
316
        assert(err.exc != NULL);
317
        break;
318
    case _Py_ext_module_loader_result_ERR_MISSING: _Py_FALLTHROUGH;
319
    case _Py_ext_module_loader_result_ERR_UNINITIALIZED: _Py_FALLTHROUGH;
320
    case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: _Py_FALLTHROUGH;
321
    case _Py_ext_module_loader_result_ERR_NOT_MODULE: _Py_FALLTHROUGH;
322
    case _Py_ext_module_loader_result_ERR_MISSING_DEF:
323
        assert(err.exc == NULL);
324
        break;
325
    default:
326
        /* We added a new error kind but forgot to add it to this switch. */
327
        assert(0);
328
    }
329
#endif
330
331
0
    const char *msg = NULL;
332
0
    switch (err.kind) {
333
0
    case _Py_ext_module_loader_result_EXCEPTION:
334
0
        break;
335
0
    case _Py_ext_module_loader_result_ERR_MISSING:
336
0
        msg = "initialization of %s failed without raising an exception";
337
0
        break;
338
0
    case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC:
339
0
        msg = "initialization of %s raised unreported exception";
340
0
        break;
341
0
    case _Py_ext_module_loader_result_ERR_UNINITIALIZED:
342
0
        msg = "init function of %s returned uninitialized object";
343
0
        break;
344
0
    case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE:
345
0
        msg = "initialization of %s did not return PyModuleDef";
346
0
        break;
347
0
    case _Py_ext_module_loader_result_ERR_NOT_MODULE:
348
0
        msg = "initialization of %s did not return an extension module";
349
0
        break;
350
0
    case _Py_ext_module_loader_result_ERR_MISSING_DEF:
351
0
        msg = "initialization of %s did not return a valid extension module";
352
0
        break;
353
0
    default:
354
        /* We added a new error kind but forgot to add it to this switch. */
355
0
        assert(0);
356
0
        PyErr_Format(PyExc_SystemError,
357
0
                     "loading %s failed due to init function", name);
358
0
        return;
359
0
    }
360
361
0
    if (err.exc != NULL) {
362
0
        PyErr_SetRaisedException(err.exc);
363
0
        err.exc = NULL;  /* PyErr_SetRaisedException() stole our reference. */
364
0
        if (msg != NULL) {
365
0
            _PyErr_FormatFromCause(PyExc_SystemError, msg, name);
366
0
        }
367
0
    }
368
0
    else {
369
0
        assert(msg != NULL);
370
0
        PyErr_Format(PyExc_SystemError, msg, name);
371
0
    }
372
0
}
373
374
375
/********************************************/
376
/* getting/running the module init function */
377
/********************************************/
378
379
#ifdef HAVE_DYNAMIC_LOADING
380
PyModInitFunction
381
_PyImport_GetModInitFunc(struct _Py_ext_module_loader_info *info,
382
                         FILE *fp)
383
79
{
384
79
    const char *name_buf = PyBytes_AS_STRING(info->name_encoded);
385
79
    dl_funcptr exportfunc;
386
#ifdef MS_WINDOWS
387
    exportfunc = _PyImport_FindSharedFuncptrWindows(
388
            info->hook_prefix, name_buf, info->filename, fp);
389
#else
390
79
    {
391
79
        const char *path_buf = PyBytes_AS_STRING(info->filename_encoded);
392
79
        exportfunc = _PyImport_FindSharedFuncptr(
393
79
                        info->hook_prefix, name_buf, path_buf, fp);
394
79
    }
395
79
#endif
396
397
79
    if (exportfunc == NULL) {
398
0
        if (!PyErr_Occurred()) {
399
0
            PyObject *msg;
400
0
            msg = PyUnicode_FromFormat(
401
0
                "dynamic module does not define "
402
0
                "module export function (%s_%s)",
403
0
                info->hook_prefix, name_buf);
404
0
            if (msg != NULL) {
405
0
                PyErr_SetImportError(msg, info->name, info->filename);
406
0
                Py_DECREF(msg);
407
0
            }
408
0
        }
409
0
        return NULL;
410
0
    }
411
412
79
    return (PyModInitFunction)exportfunc;
413
79
}
414
#endif /* HAVE_DYNAMIC_LOADING */
415
416
int
417
_PyImport_RunModInitFunc(PyModInitFunction p0,
418
                         struct _Py_ext_module_loader_info *info,
419
                         struct _Py_ext_module_loader_result *p_res)
420
375
{
421
375
    struct _Py_ext_module_loader_result res = {
422
375
        .kind=_Py_ext_module_kind_UNKNOWN,
423
375
    };
424
425
    /* Call the module init function. */
426
427
    /* Package context is needed for single-phase init */
428
375
    const char *oldcontext = _PyImport_SwapPackageContext(info->newcontext);
429
375
    PyObject *m = p0();
430
375
    _PyImport_SwapPackageContext(oldcontext);
431
432
    /* Validate the result (and populate "res". */
433
434
375
    if (m == NULL) {
435
        /* The init func for multi-phase init modules is expected
436
         * to return a PyModuleDef after calling PyModuleDef_Init().
437
         * That function never raises an exception nor returns NULL,
438
         * so at this point it must be a single-phase init modules. */
439
0
        res.kind = _Py_ext_module_kind_SINGLEPHASE;
440
0
        if (PyErr_Occurred()) {
441
0
            _Py_ext_module_loader_result_set_error(
442
0
                        &res, _Py_ext_module_loader_result_EXCEPTION);
443
0
        }
444
0
        else {
445
0
            _Py_ext_module_loader_result_set_error(
446
0
                        &res, _Py_ext_module_loader_result_ERR_MISSING);
447
0
        }
448
0
        goto error;
449
375
    } else if (PyErr_Occurred()) {
450
        /* Likewise, we infer that this is a single-phase init module. */
451
0
        res.kind = _Py_ext_module_kind_SINGLEPHASE;
452
0
        _Py_ext_module_loader_result_set_error(
453
0
                &res, _Py_ext_module_loader_result_ERR_UNREPORTED_EXC);
454
        /* We would probably be correct to decref m here,
455
         * but we weren't doing so before,
456
         * so we stick with doing nothing. */
457
0
        m = NULL;
458
0
        goto error;
459
0
    }
460
461
375
    if (Py_IS_TYPE(m, NULL)) {
462
        /* This can happen when a PyModuleDef is returned without calling
463
         * PyModuleDef_Init on it
464
         */
465
0
        _Py_ext_module_loader_result_set_error(
466
0
                &res, _Py_ext_module_loader_result_ERR_UNINITIALIZED);
467
        /* Likewise, decref'ing here makes sense.  However, the original
468
         * code has a note about "prevent segfault in DECREF",
469
         * so we play it safe and leave it alone. */
470
0
        m = NULL; /* prevent segfault in DECREF */
471
0
        goto error;
472
0
    }
473
474
375
    if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
475
        /* multi-phase init */
476
375
        res.kind = _Py_ext_module_kind_MULTIPHASE;
477
375
        res.def = (PyModuleDef *)m;
478
        /* Run PyModule_FromDefAndSpec() to finish loading the module. */
479
375
    }
480
0
    else if (info->hook_prefix == nonascii_prefix) {
481
        /* Non-ASCII is only supported for multi-phase init. */
482
0
        res.kind = _Py_ext_module_kind_MULTIPHASE;
483
        /* Don't allow legacy init for non-ASCII module names. */
484
0
        _Py_ext_module_loader_result_set_error(
485
0
                &res, _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE);
486
0
        goto error;
487
0
    }
488
0
    else {
489
        /* single-phase init (legacy) */
490
0
        res.kind = _Py_ext_module_kind_SINGLEPHASE;
491
0
        res.module = m;
492
493
0
        if (!PyModule_Check(m)) {
494
0
            _Py_ext_module_loader_result_set_error(
495
0
                    &res, _Py_ext_module_loader_result_ERR_NOT_MODULE);
496
0
            goto error;
497
0
        }
498
499
0
        res.def = _PyModule_GetDef(m);
500
0
        if (res.def == NULL) {
501
0
            PyErr_Clear();
502
0
            _Py_ext_module_loader_result_set_error(
503
0
                    &res, _Py_ext_module_loader_result_ERR_MISSING_DEF);
504
0
            goto error;
505
0
        }
506
0
    }
507
508
375
    assert(!PyErr_Occurred());
509
375
    assert(res.err == NULL);
510
375
    *p_res = res;
511
375
    return 0;
512
513
0
error:
514
0
    assert(!PyErr_Occurred());
515
0
    assert(res.err != NULL);
516
0
    Py_CLEAR(res.module);
517
0
    res.def = NULL;
518
0
    *p_res = res;
519
0
    p_res->err = &p_res->_err;
520
0
    return -1;
521
375
}