Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/ms/uplink.c

raw
1 /*
2 * Copyright 2004-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #if (defined(_WIN64) || defined(_WIN32_WCE)) && !defined(UNICODE)
11 #define UNICODE
12 #endif
13 #if defined(UNICODE) && !defined(_UNICODE)
14 #define _UNICODE
15 #endif
16 #if defined(_UNICODE) && !defined(UNICODE)
17 #define UNICODE
18 #endif
19
20 #include <windows.h>
21 #include <tchar.h>
22 #include <stdio.h>
23 #include <stddef.h>
24 #include "uplink.h"
25 void OPENSSL_showfatal(const char *, ...);
26
27 static TCHAR msg[128];
28
29 static void unimplemented(void)
30 {
31 OPENSSL_showfatal(sizeof(TCHAR) == sizeof(char) ? "%s\n" : "%S\n", msg);
32 TerminateProcess(GetCurrentProcess(), 1);
33 }
34
35 void OPENSSL_Uplink(volatile void **table, int index)
36 {
37 static HMODULE volatile apphandle = NULL;
38 static void **volatile applinktable = NULL;
39 int len;
40 void (*func)(void) = unimplemented;
41 HANDLE h;
42 void **p;
43
44 /*
45 * Note that the below code is not MT-safe in respect to msg buffer, but
46 * what's the worst thing that can happen? Error message might be
47 * misleading or corrupted. As error condition is fatal and should never
48 * be risen, I accept the risk...
49 */
50 /*
51 * One can argue that I should have used InterlockedExchangePointer or
52 * something to update static variables and table[]. Well, store
53 * instructions are as atomic as they can get and assigned values are
54 * effectively constant... So that volatile qualifier should be
55 * sufficient [it prohibits compiler to reorder memory access
56 * instructions].
57 */
58 do {
59 len = _sntprintf(msg, sizeof(msg) / sizeof(TCHAR),
60 _T("OPENSSL_Uplink(%p,%02X): "), table, index);
61 _tcscpy(msg + len, _T("unimplemented function"));
62
63 if ((h = apphandle) == NULL) {
64 if ((h = GetModuleHandle(NULL)) == NULL) {
65 apphandle = (HMODULE)-1;
66 _tcscpy(msg + len, _T("no host application"));
67 break;
68 }
69 apphandle = h;
70 }
71 if ((h = apphandle) == (HMODULE)-1) /* revalidate */
72 break;
73
74 if (applinktable == NULL) {
75 void **(*applink)();
76
77 applink = (void **(*)())GetProcAddress(h, "OPENSSL_Applink");
78 if (applink == NULL) {
79 apphandle = (HMODULE)-1;
80 _tcscpy(msg + len, _T("no OPENSSL_Applink"));
81 break;
82 }
83 p = (*applink)();
84 if (p == NULL) {
85 apphandle = (HMODULE)-1;
86 _tcscpy(msg + len, _T("no ApplinkTable"));
87 break;
88 }
89 applinktable = p;
90 } else
91 p = applinktable;
92
93 if (index > (intptr_t)p[0])
94 break;
95
96 if (p[index])
97 func = p[index];
98 } while (0);
99
100 table[index] = func;
101 }
102
103 #if (defined(_MSC_VER) || defined(__BORLANDC__)) && defined(_M_IX86)
104 #if defined(_MSC_VER)
105 #define LAZY(i) \
106 __declspec(naked) static void lazy##i(void) \
107 { \
108 _asm push i _asm push OFFSET OPENSSL_UplinkTable _asm call OPENSSL_Uplink _asm add esp, 8 _asm jmp OPENSSL_UplinkTable + 4 * i \
109 }
110 #elif defined(__BORLANDC__) && defined(__clang__)
111 void *OPENSSL_UplinkTable[26]; /* C++Builder requires declaration before use */
112 #define LAZY(i) \
113 __declspec(naked) static void lazy##i(void) \
114 { \
115 __asm__("pushl $" #i "; " \
116 "pushl %0; " \
117 "call %P1; " \
118 "addl $8, %%esp; " \
119 "jmp *%2 " \
120 : /* no outputs */ \
121 : "i"(OPENSSL_UplinkTable), \
122 "i"(OPENSSL_Uplink), \
123 "m"(OPENSSL_UplinkTable[i])); \
124 }
125 #endif
126
127 #if APPLINK_MAX > 25
128 #error "Add more stubs..."
129 #endif
130 /* make some in advance... */
131 LAZY(1)
132 LAZY(2)
133 LAZY(3)
134 LAZY(4)
135 LAZY(5)
136 LAZY(6)
137 LAZY(7)
138 LAZY(8)
139 LAZY(9)
140 LAZY(10)
141 LAZY(11)
142 LAZY(12)
143 LAZY(13)
144 LAZY(14)
145 LAZY(15)
146 LAZY(16)
147 LAZY(17)
148 LAZY(18)
149 LAZY(19)
150 LAZY(20)
151 LAZY(21)
152 LAZY(22)
153 LAZY(23)
154 LAZY(24)
155 LAZY(25)
156 void *OPENSSL_UplinkTable[] = {
157 (void *)APPLINK_MAX,
158 lazy1,
159 lazy2,
160 lazy3,
161 lazy4,
162 lazy5,
163 lazy6,
164 lazy7,
165 lazy8,
166 lazy9,
167 lazy10,
168 lazy11,
169 lazy12,
170 lazy13,
171 lazy14,
172 lazy15,
173 lazy16,
174 lazy17,
175 lazy18,
176 lazy19,
177 lazy20,
178 lazy21,
179 lazy22,
180 lazy23,
181 lazy24,
182 lazy25,
183 };
184 #endif
185
186 #ifdef SELFTEST
187 main()
188 {
189 UP_fprintf(UP_stdout, "hello, world!\n");
190 }
191 #endif