| 1 |
#ifndef CFFI_MISC_WIN32_H |
| 2 |
#define CFFI_MISC_WIN32_H |
| 3 |
|
| 4 |
|
| 5 |
#include <malloc.h> /* for alloca() */ |
| 6 |
|
| 7 |
|
| 8 |
/************************************************************/ |
| 9 |
/* errno and GetLastError support */ |
| 10 |
|
| 11 |
#include "misc_thread_common.h" |
| 12 |
|
| 13 |
static DWORD cffi_tls_index = TLS_OUT_OF_INDEXES; |
| 14 |
|
| 15 |
BOOL WINAPI DllMain(HINSTANCE hinstDLL, |
| 16 |
DWORD reason_for_call, |
| 17 |
LPVOID reserved) |
| 18 |
{ |
| 19 |
LPVOID p; |
| 20 |
|
| 21 |
switch (reason_for_call) { |
| 22 |
|
| 23 |
case DLL_THREAD_DETACH: |
| 24 |
if (cffi_tls_index != TLS_OUT_OF_INDEXES) { |
| 25 |
p = TlsGetValue(cffi_tls_index); |
| 26 |
if (p != NULL) { |
| 27 |
TlsSetValue(cffi_tls_index, NULL); |
| 28 |
cffi_thread_shutdown(p); |
| 29 |
} |
| 30 |
} |
| 31 |
break; |
| 32 |
|
| 33 |
default: |
| 34 |
break; |
| 35 |
} |
| 36 |
return TRUE; |
| 37 |
} |
| 38 |
|
| 39 |
static void init_cffi_tls(void) |
| 40 |
{ |
| 41 |
if (cffi_tls_index == TLS_OUT_OF_INDEXES) { |
| 42 |
cffi_tls_index = TlsAlloc(); |
| 43 |
if (cffi_tls_index == TLS_OUT_OF_INDEXES) |
| 44 |
PyErr_SetString(PyExc_WindowsError, "TlsAlloc() failed"); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
static struct cffi_tls_s *get_cffi_tls(void) |
| 49 |
{ |
| 50 |
LPVOID p = TlsGetValue(cffi_tls_index); |
| 51 |
|
| 52 |
if (p == NULL) { |
| 53 |
p = malloc(sizeof(struct cffi_tls_s)); |
| 54 |
if (p == NULL) |
| 55 |
return NULL; |
| 56 |
memset(p, 0, sizeof(struct cffi_tls_s)); |
| 57 |
TlsSetValue(cffi_tls_index, p); |
| 58 |
} |
| 59 |
return (struct cffi_tls_s *)p; |
| 60 |
} |
| 61 |
|
| 62 |
#ifdef USE__THREAD |
| 63 |
# error "unexpected USE__THREAD on Windows" |
| 64 |
#endif |
| 65 |
|
| 66 |
static void save_errno(void) |
| 67 |
{ |
| 68 |
int current_err = errno; |
| 69 |
int current_lasterr = GetLastError(); |
| 70 |
struct cffi_tls_s *p = get_cffi_tls(); |
| 71 |
if (p != NULL) { |
| 72 |
p->saved_errno = current_err; |
| 73 |
p->saved_lasterror = current_lasterr; |
| 74 |
} |
| 75 |
/* else: cannot report the error */ |
| 76 |
} |
| 77 |
|
| 78 |
static void restore_errno(void) |
| 79 |
{ |
| 80 |
struct cffi_tls_s *p = get_cffi_tls(); |
| 81 |
if (p != NULL) { |
| 82 |
SetLastError(p->saved_lasterror); |
| 83 |
errno = p->saved_errno; |
| 84 |
} |
| 85 |
/* else: cannot report the error */ |
| 86 |
} |
| 87 |
|
| 88 |
/************************************************************/ |
| 89 |
|
| 90 |
|
| 91 |
static PyObject *b_getwinerror(PyObject *self, PyObject *args, PyObject *kwds) |
| 92 |
{ |
| 93 |
int err = -1; |
| 94 |
int len; |
| 95 |
WCHAR *s_buf = NULL; /* Free via LocalFree */ |
| 96 |
PyObject *v, *message; |
| 97 |
static char *keywords[] = {"code", NULL}; |
| 98 |
|
| 99 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", keywords, &err)) |
| 100 |
return NULL; |
| 101 |
|
| 102 |
if (err == -1) { |
| 103 |
struct cffi_tls_s *p = get_cffi_tls(); |
| 104 |
if (p == NULL) |
| 105 |
return PyErr_NoMemory(); |
| 106 |
err = p->saved_lasterror; |
| 107 |
} |
| 108 |
|
| 109 |
len = FormatMessageW( |
| 110 |
/* Error API error */ |
| 111 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 112 |
FORMAT_MESSAGE_FROM_SYSTEM | |
| 113 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 114 |
NULL, /* no message source */ |
| 115 |
err, |
| 116 |
MAKELANGID(LANG_NEUTRAL, |
| 117 |
SUBLANG_DEFAULT), /* Default language */ |
| 118 |
(LPWSTR) &s_buf, |
| 119 |
0, /* size not used */ |
| 120 |
NULL); /* no args */ |
| 121 |
if (len==0) { |
| 122 |
/* Only seen this in out of mem situations */ |
| 123 |
message = PyUnicode_FromFormat("Windows Error 0x%X", err); |
| 124 |
} else { |
| 125 |
/* remove trailing cr/lf and dots */ |
| 126 |
while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 127 |
s_buf[--len] = L'\0'; |
| 128 |
message = PyUnicode_FromWideChar(s_buf, len); |
| 129 |
} |
| 130 |
if (message != NULL) { |
| 131 |
v = Py_BuildValue("(iO)", err, message); |
| 132 |
Py_DECREF(message); |
| 133 |
} |
| 134 |
else |
| 135 |
v = NULL; |
| 136 |
LocalFree(s_buf); |
| 137 |
return v; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/************************************************************/ |
| 142 |
/* Emulate dlopen()&co. from the Windows API */ |
| 143 |
|
| 144 |
#define RTLD_LAZY 0 |
| 145 |
#define RTLD_NOW 0 |
| 146 |
#define RTLD_GLOBAL 0 |
| 147 |
#define RTLD_LOCAL 0 |
| 148 |
|
| 149 |
static void *dlopen(const char *filename, int flags) |
| 150 |
{ |
| 151 |
if (flags == 0) { |
| 152 |
for (const char *p = filename; *p != 0; p++) |
| 153 |
if (*p == '\\' || *p == '/') { |
| 154 |
flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | |
| 155 |
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR; |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
return (void *)LoadLibraryExA(filename, NULL, flags); |
| 160 |
} |
| 161 |
|
| 162 |
static void *dlopenWinW(const wchar_t *filename, int flags) |
| 163 |
{ |
| 164 |
if (flags == 0) { |
| 165 |
for (const wchar_t *p = filename; *p != 0; p++) |
| 166 |
if (*p == '\\' || *p == '/') { |
| 167 |
flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | |
| 168 |
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR; |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
return (void *)LoadLibraryExW(filename, NULL, flags); |
| 173 |
} |
| 174 |
|
| 175 |
static void *dlsym(void *handle, const char *symbol) |
| 176 |
{ |
| 177 |
void *address = GetProcAddress((HMODULE)handle, symbol); |
| 178 |
#ifndef MS_WIN64 |
| 179 |
if (!address) { |
| 180 |
/* If 'symbol' is not found, then try '_symbol@N' for N in |
| 181 |
(0, 4, 8, 12, ..., 124). Unlike ctypes, we try to do that |
| 182 |
for any symbol, although in theory it should only be done |
| 183 |
for __stdcall functions. |
| 184 |
*/ |
| 185 |
int i; |
| 186 |
char *mangled_name = alloca(1 + strlen(symbol) + 1 + 3 + 1); |
| 187 |
if (!mangled_name) |
| 188 |
return NULL; |
| 189 |
for (i = 0; i < 32; i++) { |
| 190 |
sprintf(mangled_name, "_%s@%d", symbol, i * 4); |
| 191 |
address = GetProcAddress((HMODULE)handle, mangled_name); |
| 192 |
if (address) |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
#endif |
| 197 |
return address; |
| 198 |
} |
| 199 |
|
| 200 |
static int dlclose(void *handle) |
| 201 |
{ |
| 202 |
return FreeLibrary((HMODULE)handle) ? 0 : -1; |
| 203 |
} |
| 204 |
|
| 205 |
static const char *dlerror(void) |
| 206 |
{ |
| 207 |
static char buf[32]; |
| 208 |
DWORD dw = GetLastError(); |
| 209 |
if (dw == 0) |
| 210 |
return NULL; |
| 211 |
sprintf(buf, "error 0x%x", (unsigned int)dw); |
| 212 |
return buf; |
| 213 |
} |
| 214 |
|
| 215 |
/* Minimal atomic support */ |
| 216 |
static int cffi_atomic_compare_exchange(void **ptr, void **expected, |
| 217 |
void *value) |
| 218 |
{ |
| 219 |
void *initial = _InterlockedCompareExchangePointer(ptr, value, expected); |
| 220 |
if (initial == *expected) { |
| 221 |
return 1; |
| 222 |
} |
| 223 |
*expected = initial; |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
static void *cffi_atomic_load(void **ptr) |
| 228 |
{ |
| 229 |
#if defined(__GNUC__) || defined(__clang__) |
| 230 |
return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); |
| 231 |
#elif defined(_M_X64) || defined(_M_IX86) |
| 232 |
return *(volatile void **)ptr; |
| 233 |
#elif defined(_M_ARM64) |
| 234 |
return (void *)__ldar64((volatile unsigned __int64 *)ptr); |
| 235 |
#else |
| 236 |
# error "no implementation of cffi_atomic_load" |
| 237 |
#endif |
| 238 |
} |
| 239 |
|
| 240 |
static uint8_t cffi_atomic_load_uint8(uint8_t *ptr) |
| 241 |
{ |
| 242 |
#if defined(__GNUC__) || defined(__clang__) |
| 243 |
return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); |
| 244 |
#elif defined(_M_X64) || defined(_M_IX86) |
| 245 |
return *(volatile uint8_t *)ptr; |
| 246 |
#elif defined(_M_ARM64) |
| 247 |
return (uint8_t)__ldar8((volatile uint8_t *)ptr); |
| 248 |
#else |
| 249 |
# error "no implementation of cffi_atomic_load_uint8" |
| 250 |
#endif |
| 251 |
} |
| 252 |
|
| 253 |
static Py_ssize_t cffi_atomic_load_ssize(Py_ssize_t *ptr) |
| 254 |
{ |
| 255 |
#if defined(__GNUC__) || defined(__clang__) |
| 256 |
return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); |
| 257 |
#elif defined(_M_X64) || defined(_M_IX86) |
| 258 |
return *(volatile Py_ssize_t *)ptr; |
| 259 |
#elif defined(_M_ARM64) |
| 260 |
return (Py_ssize_t)__ldar64((volatile unsigned __int64 *)ptr); |
| 261 |
#else |
| 262 |
# error "no implementation of cffi_atomic_load_ssize" |
| 263 |
#endif |
| 264 |
} |
| 265 |
|
| 266 |
static void cffi_atomic_store_ssize(Py_ssize_t *ptr, Py_ssize_t value) |
| 267 |
{ |
| 268 |
#if defined(__GNUC__) || defined(__clang__) |
| 269 |
__atomic_store_n(ptr, value, __ATOMIC_SEQ_CST); |
| 270 |
#else |
| 271 |
_InterlockedExchangePointer(ptr, value); |
| 272 |
#endif |
| 273 |
} |
| 274 |
|
| 275 |
static void cffi_atomic_store(void **ptr, void *value) |
| 276 |
{ |
| 277 |
#if defined(__GNUC__) || defined(__clang__) |
| 278 |
__atomic_store_n(ptr, value, __ATOMIC_SEQ_CST); |
| 279 |
#else |
| 280 |
_InterlockedExchangePointer(ptr, value); |
| 281 |
#endif |
| 282 |
} |
| 283 |
|
| 284 |
static void cffi_atomic_store_uint8(uint8_t *ptr, uint8_t value) |
| 285 |
{ |
| 286 |
#if defined(__GNUC__) || defined(__clang__) |
| 287 |
__atomic_store_n(ptr, value, __ATOMIC_SEQ_CST); |
| 288 |
#else |
| 289 |
_InterlockedExchange8(ptr, value); |
| 290 |
#endif |
| 291 |
} |
| 292 |
|
| 293 |
#endif /* CFFI_MISC_WIN32_H */ |