This repository was archived by the owner on Dec 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathextapi.cpp
More file actions
167 lines (141 loc) · 4.94 KB
/
extapi.cpp
File metadata and controls
167 lines (141 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <Python.h>
#include <err.h>
#include "extapi.hpp"
#ifdef __NT__
#include <windows.h>
#include <psapi.h>
//-------------------------------------------------------------------------
bool ext_api_t::load(qstring *errbuf)
{
QASSERT(30602, lib_path.empty() && lib_handle == nullptr);
// Inspired by https://docs.microsoft.com/en-us/windows/win32/psapi/enumerating-all-modules-for-a-process
HANDLE hProcess = GetCurrentProcess();
HMODULE hMods[1024];
DWORD cbNeeded;
if ( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded) == 0 )
return false;
const void *wanted = (const void *) Py_IsInitialized;
for ( size_t i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
{
MODULEINFO module_info;
if ( GetModuleInformation(hProcess, hMods[i], &module_info, sizeof(module_info)) )
{
if ( wanted >= module_info.lpBaseOfDll )
{
LPVOID end = (LPVOID) ((const char *) module_info.lpBaseOfDll + module_info.SizeOfImage);
if ( wanted < end )
{
// found module
lib_handle = (void *) hMods[i];
break;
}
}
}
}
if ( lib_handle == nullptr )
return false;
#define BIND_SYMBOL_x(Name, fail) \
do \
{ \
* (FARPROC *) &Name ## _ptr = GetProcAddress( \
(HMODULE) lib_handle, TEXT(#Name)); \
if ( Name ## _ptr == nullptr && fail ) \
{ \
errbuf->sprnt("GetProcAddress(\"%s\") failed: %s", \
#Name, qstrerror(-1)); \
return false; \
} \
} while ( 0 )
#define BIND_SYMBOL(Name) BIND_SYMBOL_x(Name, true)
#define BIND_SYMBOL_WEAK(Name) BIND_SYMBOL_x(Name, false)
BIND_SYMBOL(PyEval_SetTrace);
BIND_SYMBOL(PyRun_SimpleStringFlags);
BIND_SYMBOL(PyRun_StringFlags);
#if PY_MAJOR_VERSION < 3
BIND_SYMBOL(Py_CompileString);
#else
BIND_SYMBOL(Py_CompileStringExFlags);
#endif
BIND_SYMBOL(PyFunction_New);
BIND_SYMBOL(PyFunction_GetCode);
BIND_SYMBOL(_PyLong_AsByteArray);
BIND_SYMBOL_WEAK(PyEval_ThreadsInitialized);
BIND_SYMBOL_WEAK(PyEval_InitThreads);
BIND_SYMBOL_WEAK(Py_NoSiteFlag);
#undef BIND_SYMBOL
return true;
}
//-------------------------------------------------------------------------
void ext_api_t::clear()
{
lib_handle = nullptr;
}
#else
#include <dlfcn.h>
//-------------------------------------------------------------------------
bool ext_api_t::load(qstring *errbuf)
{
if ( !lib_path.empty() && lib_handle != nullptr )
{
return true;
}
// QASSERT(30603, lib_path.empty() && lib_handle == nullptr);
// first, check if we have global symbols
if ( dlsym(RTLD_DEFAULT, "Py_IsInitialized") != nullptr )
lib_handle = RTLD_DEFAULT;
else
{
// otherwise, look for shared library to import from
Dl_info dl_info;
memset(&dl_info, 0, sizeof(dl_info));
int rc = dladdr((void *) Py_IsInitialized, &dl_info);
if ( rc == 0 )
{
*errbuf = "Cannot determine path to shared object";
return false;
}
lib_path = dl_info.dli_fname;
lib_handle = dlopen(lib_path.c_str(), RTLD_NOLOAD | RTLD_GLOBAL | RTLD_LAZY);
if ( lib_handle == nullptr )
{
errbuf->sprnt("dlopen(\"%s\") failed: %s", lib_path.c_str(), qstrerror(-1));
return false;
}
}
#define BIND_SYMBOL(Name) \
do \
{ \
Name ## _ptr = (Name ## _t *) dlsym(lib_handle, #Name); \
if ( Name ## _ptr == nullptr ) \
{ \
errbuf->sprnt("dlsym(\"%s\") failed: %s", #Name, qstrerror(-1)); \
return false; \
} \
} while ( 0 )
BIND_SYMBOL(PyEval_SetTrace);
BIND_SYMBOL(PyRun_SimpleStringFlags);
BIND_SYMBOL(PyRun_StringFlags);
#if PY_MAJOR_VERSION < 3
BIND_SYMBOL(Py_CompileString);
#else
BIND_SYMBOL(Py_CompileStringExFlags);
#endif
BIND_SYMBOL(PyFunction_New);
BIND_SYMBOL(PyFunction_GetCode);
BIND_SYMBOL(_PyLong_AsByteArray);
BIND_SYMBOL(Py_NoSiteFlag);
#undef BIND_SYMBOL
return true;
}
//-------------------------------------------------------------------------
void ext_api_t::clear()
{
if ( lib_handle != nullptr )
{
if ( lib_handle != RTLD_DEFAULT )
dlclose(lib_handle);
lib_handle = nullptr;
}
}
#endif
ext_api_t extapi;