Coverage Report

Created: 2025-07-11 06:59

/src/Python-3.8.3/Python/dynload_shlib.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_pystate.h"
6
#include "importdl.h"
7
8
#include <sys/types.h>
9
#include <sys/stat.h>
10
11
#if defined(__NetBSD__)
12
#include <sys/param.h>
13
#if (NetBSD < 199712)
14
#include <nlist.h>
15
#include <link.h>
16
#define dlerror() "error in dynamic linking"
17
#endif
18
#endif /* NetBSD */
19
20
#ifdef HAVE_DLFCN_H
21
#include <dlfcn.h>
22
#endif
23
24
#if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
25
#define LEAD_UNDERSCORE "_"
26
#else
27
0
#define LEAD_UNDERSCORE ""
28
#endif
29
30
/* The .so extension module ABI tag, supplied by the Makefile via
31
   Makefile.pre.in and configure.  This is used to discriminate between
32
   incompatible .so files so that extensions for different Python builds can
33
   live in the same directory.  E.g. foomodule.cpython-32.so
34
*/
35
36
const char *_PyImport_DynLoadFiletab[] = {
37
#ifdef __CYGWIN__
38
    ".dll",
39
#else  /* !__CYGWIN__ */
40
    "." SOABI ".so",
41
#ifdef ALT_SOABI
42
    "." ALT_SOABI ".so",
43
#endif
44
    ".abi" PYTHON_ABI_STRING ".so",
45
    ".so",
46
#endif  /* __CYGWIN__ */
47
    NULL,
48
};
49
50
static struct {
51
    dev_t dev;
52
    ino_t ino;
53
    void *handle;
54
} handles[128];
55
static int nhandles = 0;
56
57
58
dl_funcptr
59
_PyImport_FindSharedFuncptr(const char *prefix,
60
                            const char *shortname,
61
                            const char *pathname, FILE *fp)
62
0
{
63
0
    dl_funcptr p;
64
0
    void *handle;
65
0
    char funcname[258];
66
0
    char pathbuf[260];
67
0
    int dlopenflags=0;
68
69
0
    if (strchr(pathname, '/') == NULL) {
70
        /* Prefix bare filename with "./" */
71
0
        PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
72
0
        pathname = pathbuf;
73
0
    }
74
75
0
    PyOS_snprintf(funcname, sizeof(funcname),
76
0
                  LEAD_UNDERSCORE "%.20s_%.200s", prefix, shortname);
77
78
0
    if (fp != NULL) {
79
0
        int i;
80
0
        struct _Py_stat_struct status;
81
0
        if (_Py_fstat(fileno(fp), &status) == -1)
82
0
            return NULL;
83
0
        for (i = 0; i < nhandles; i++) {
84
0
            if (status.st_dev == handles[i].dev &&
85
0
                status.st_ino == handles[i].ino) {
86
0
                p = (dl_funcptr) dlsym(handles[i].handle,
87
0
                                       funcname);
88
0
                return p;
89
0
            }
90
0
        }
91
0
        if (nhandles < 128) {
92
0
            handles[nhandles].dev = status.st_dev;
93
0
            handles[nhandles].ino = status.st_ino;
94
0
        }
95
0
    }
96
97
0
    dlopenflags = _PyInterpreterState_Get()->dlopenflags;
98
99
0
    handle = dlopen(pathname, dlopenflags);
100
101
0
    if (handle == NULL) {
102
0
        PyObject *mod_name;
103
0
        PyObject *path;
104
0
        PyObject *error_ob;
105
0
        const char *error = dlerror();
106
0
        if (error == NULL)
107
0
            error = "unknown dlopen() error";
108
0
        error_ob = PyUnicode_FromString(error);
109
0
        if (error_ob == NULL)
110
0
            return NULL;
111
0
        mod_name = PyUnicode_FromString(shortname);
112
0
        if (mod_name == NULL) {
113
0
            Py_DECREF(error_ob);
114
0
            return NULL;
115
0
        }
116
0
        path = PyUnicode_FromString(pathname);
117
0
        if (path == NULL) {
118
0
            Py_DECREF(error_ob);
119
0
            Py_DECREF(mod_name);
120
0
            return NULL;
121
0
        }
122
0
        PyErr_SetImportError(error_ob, mod_name, path);
123
0
        Py_DECREF(error_ob);
124
0
        Py_DECREF(mod_name);
125
0
        Py_DECREF(path);
126
0
        return NULL;
127
0
    }
128
0
    if (fp != NULL && nhandles < 128)
129
0
        handles[nhandles++].handle = handle;
130
0
    p = (dl_funcptr) dlsym(handle, funcname);
131
0
    return p;
132
0
}