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