1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include "uv.h"
23 #include "internal.h"
24 
25 static int uv__dlerror(uv_lib_t* lib, const char* filename, DWORD errorno);
26 
27 
uv_dlopen(const char * filename,uv_lib_t * lib)28 int uv_dlopen(const char* filename, uv_lib_t* lib) {
29   WCHAR filename_w[32768];
30 
31   lib->handle = NULL;
32   lib->errmsg = NULL;
33 
34   if (!MultiByteToWideChar(CP_UTF8,
35                            0,
36                            filename,
37                            -1,
38                            filename_w,
39                            ARRAY_SIZE(filename_w))) {
40     return uv__dlerror(lib, filename, GetLastError());
41   }
42 
43   lib->handle = LoadLibraryExW(filename_w, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
44   if (lib->handle == NULL) {
45     return uv__dlerror(lib, filename, GetLastError());
46   }
47 
48   return 0;
49 }
50 
51 
uv_dlclose(uv_lib_t * lib)52 void uv_dlclose(uv_lib_t* lib) {
53   if (lib->errmsg) {
54     LocalFree((void*)lib->errmsg);
55     lib->errmsg = NULL;
56   }
57 
58   if (lib->handle) {
59     /* Ignore errors. No good way to signal them without leaking memory. */
60     FreeLibrary(lib->handle);
61     lib->handle = NULL;
62   }
63 }
64 
65 
uv_dlsym(uv_lib_t * lib,const char * name,void ** ptr)66 int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr) {
67   /* Cast though integer to suppress pedantic warning about forbidden cast. */
68   *ptr = (void*)(uintptr_t) GetProcAddress(lib->handle, name);
69   return uv__dlerror(lib, "", *ptr ? 0 : GetLastError());
70 }
71 
72 
uv_dlerror(const uv_lib_t * lib)73 const char* uv_dlerror(const uv_lib_t* lib) {
74   return lib->errmsg ? lib->errmsg : "no error";
75 }
76 
77 
uv__format_fallback_error(uv_lib_t * lib,int errorno)78 static void uv__format_fallback_error(uv_lib_t* lib, int errorno){
79   static const CHAR fallback_error[] = "error: %1!d!";
80   DWORD_PTR args[1];
81   args[0] = (DWORD_PTR) errorno;
82 
83   FormatMessageA(FORMAT_MESSAGE_FROM_STRING |
84                  FORMAT_MESSAGE_ARGUMENT_ARRAY |
85                  FORMAT_MESSAGE_ALLOCATE_BUFFER,
86                  fallback_error, 0, 0,
87                  (LPSTR) &lib->errmsg,
88                  0, (va_list*) args);
89 }
90 
91 
92 
uv__dlerror(uv_lib_t * lib,const char * filename,DWORD errorno)93 static int uv__dlerror(uv_lib_t* lib, const char* filename, DWORD errorno) {
94   DWORD_PTR arg;
95   DWORD res;
96   char* msg;
97 
98   if (lib->errmsg) {
99     LocalFree(lib->errmsg);
100     lib->errmsg = NULL;
101   }
102 
103   if (errorno == 0)
104     return 0;
105 
106   res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
107                        FORMAT_MESSAGE_FROM_SYSTEM |
108                        FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
109                        MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
110                        (LPSTR) &lib->errmsg, 0, NULL);
111 
112   if (!res && (GetLastError() == ERROR_MUI_FILE_NOT_FOUND ||
113                GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND)) {
114     res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
115                          FORMAT_MESSAGE_FROM_SYSTEM |
116                          FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
117                          0, (LPSTR) &lib->errmsg, 0, NULL);
118   }
119 
120   if (res && errorno == ERROR_BAD_EXE_FORMAT && strstr(lib->errmsg, "%1")) {
121     msg = lib->errmsg;
122     lib->errmsg = NULL;
123     arg = (DWORD_PTR) filename;
124     res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
125                          FORMAT_MESSAGE_ARGUMENT_ARRAY |
126                          FORMAT_MESSAGE_FROM_STRING,
127                          msg,
128                          0, 0, (LPSTR) &lib->errmsg, 0, (va_list*) &arg);
129     LocalFree(msg);
130   }
131 
132   if (!res)
133     uv__format_fallback_error(lib, errorno);
134 
135   return -1;
136 }
137