To: vim_dev@googlegroups.com Subject: Patch 8.2.4354 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.4354 Problem: Dynamic loading of libsodium not handled properly. Solution: Fix has() and :version. Show an error message when loading fails. Fix memory leaks. (Ken Takata, closes #9754) Files: src/crypt.c, src/evalfunc.c, src/gui_dwrite.cpp, src/if_cscope.c, src/os_win32.c, src/proto/crypt.pro, src/proto/os_win32.pro, src/version.c *** ../vim-8.2.4353/src/crypt.c 2022-01-19 13:32:53.443929932 +0000 --- src/crypt.c 2022-02-12 11:12:49.745338717 +0000 *************** *** 162,167 **** --- 162,183 ---- # ifdef DYNAMIC_SODIUM + # ifdef MSWIN + # define SODIUM_PROC FARPROC + # define load_dll vimLoadLib + # define symbol_from_dll GetProcAddress + # define close_dll FreeLibrary + # define load_dll_error GetWin32Error + # else + # error Dynamic loading of libsodium is not supported for now. + //# define HINSTANCE void* + //# define SODIUM_PROC void* + //# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) + //# define symbol_from_dll dlsym + //# define close_dll dlclose + //# define load_dll_error dlerror + # endif + # define sodium_init load_sodium # define sodium_free dll_sodium_free # define sodium_malloc dll_sodium_malloc *************** *** 214,266 **** static struct { const char *name; ! FARPROC *ptr; } sodium_funcname_table[] = { ! {"sodium_init", (FARPROC*)&dll_sodium_init}, ! {"sodium_free", (FARPROC*)&dll_sodium_free}, ! {"sodium_malloc", (FARPROC*)&dll_sodium_malloc}, ! {"sodium_memzero", (FARPROC*)&dll_sodium_memzero}, ! {"sodium_mlock", (FARPROC*)&dll_sodium_mlock}, ! {"sodium_munlock", (FARPROC*)&dll_sodium_munlock}, ! {"crypto_secretstream_xchacha20poly1305_init_push", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_init_push}, ! {"crypto_secretstream_xchacha20poly1305_push", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_push}, ! {"crypto_secretstream_xchacha20poly1305_init_pull", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_init_pull}, ! {"crypto_secretstream_xchacha20poly1305_pull", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_pull}, ! {"crypto_pwhash", (FARPROC*)&dll_crypto_pwhash}, ! {"randombytes_buf", (FARPROC*)&dll_randombytes_buf}, {NULL, NULL} }; static int ! load_sodium(void) { ! static HANDLE hsodium = NULL; int i; if (hsodium != NULL) ! return 0; ! hsodium = vimLoadLib("libsodium.dll"); if (hsodium == NULL) { ! // TODO: Show error message. ! return -1; } for (i = 0; sodium_funcname_table[i].ptr; ++i) { ! if ((*sodium_funcname_table[i].ptr = GetProcAddress(hsodium, sodium_funcname_table[i].name)) == NULL) { FreeLibrary(hsodium); hsodium = NULL; ! // TODO: Show error message. ! return -1; } } return dll_sodium_init(); } # endif #endif #define CRYPT_MAGIC_LEN 12 // cannot change --- 230,301 ---- static struct { const char *name; ! SODIUM_PROC *ptr; } sodium_funcname_table[] = { ! {"sodium_init", (SODIUM_PROC*)&dll_sodium_init}, ! {"sodium_free", (SODIUM_PROC*)&dll_sodium_free}, ! {"sodium_malloc", (SODIUM_PROC*)&dll_sodium_malloc}, ! {"sodium_memzero", (SODIUM_PROC*)&dll_sodium_memzero}, ! {"sodium_mlock", (SODIUM_PROC*)&dll_sodium_mlock}, ! {"sodium_munlock", (SODIUM_PROC*)&dll_sodium_munlock}, ! {"crypto_secretstream_xchacha20poly1305_init_push", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_init_push}, ! {"crypto_secretstream_xchacha20poly1305_push", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_push}, ! {"crypto_secretstream_xchacha20poly1305_init_pull", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_init_pull}, ! {"crypto_secretstream_xchacha20poly1305_pull", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_pull}, ! {"crypto_pwhash", (SODIUM_PROC*)&dll_crypto_pwhash}, ! {"randombytes_buf", (SODIUM_PROC*)&dll_randombytes_buf}, {NULL, NULL} }; static int ! sodium_runtime_link_init(int verbose) { ! static HINSTANCE hsodium = NULL; ! const char *libname = "libsodium.dll"; int i; if (hsodium != NULL) ! return OK; ! hsodium = load_dll(libname); if (hsodium == NULL) { ! if (verbose) ! semsg(_(e_could_not_load_library_str_str), libname, load_dll_error()); ! return FAIL; } for (i = 0; sodium_funcname_table[i].ptr; ++i) { ! if ((*sodium_funcname_table[i].ptr = symbol_from_dll(hsodium, sodium_funcname_table[i].name)) == NULL) { FreeLibrary(hsodium); hsodium = NULL; ! if (verbose) ! semsg(_(e_could_not_load_library_function_str), sodium_funcname_table[i].name); ! return FAIL; } } + return OK; + } + + static int + load_sodium(void) + { + if (sodium_runtime_link_init(TRUE) == FAIL) + return -1; return dll_sodium_init(); } # endif + + # if defined(DYNAMIC_SODIUM) || defined(PROTO) + int + sodium_enabled(int verbose) + { + return sodium_runtime_link_init(verbose) == OK; + } + # endif #endif #define CRYPT_MAGIC_LEN 12 // cannot change *** ../vim-8.2.4353/src/evalfunc.c 2022-02-08 12:07:41.831496906 +0000 --- src/evalfunc.c 2022-02-12 11:12:49.749338736 +0000 *************** *** 5997,6003 **** #endif }, {"sodium", ! #ifdef FEAT_SODIUM 1 #else 0 --- 5997,6003 ---- #endif }, {"sodium", ! #if defined(FEAT_SODIUM) && !defined(DYNAMIC_SODIUM) 1 #else 0 *************** *** 6318,6323 **** --- 6318,6327 ---- else if (STRICMP(name, "tcl") == 0) n = tcl_enabled(FALSE); #endif + #ifdef DYNAMIC_SODIUM + else if (STRICMP(name, "sodium") == 0) + n = sodium_enabled(FALSE); + #endif #if defined(FEAT_TERMINAL) && defined(MSWIN) else if (STRICMP(name, "terminal") == 0) n = terminal_enabled(); *** ../vim-8.2.4353/src/gui_dwrite.cpp 2020-12-08 20:12:37.055815310 +0000 --- src/gui_dwrite.cpp 2022-02-12 11:12:49.749338736 +0000 *************** *** 59,65 **** #endif #ifdef DYNAMIC_DIRECTX ! extern "C" HINSTANCE vimLoadLib(char *name); typedef int (WINAPI *PGETUSERDEFAULTLOCALENAME)(LPWSTR, int); typedef HRESULT (WINAPI *PD2D1CREATEFACTORY)(D2D1_FACTORY_TYPE, --- 59,65 ---- #endif #ifdef DYNAMIC_DIRECTX ! extern "C" HINSTANCE vimLoadLib(const char *name); typedef int (WINAPI *PGETUSERDEFAULTLOCALENAME)(LPWSTR, int); typedef HRESULT (WINAPI *PD2D1CREATEFACTORY)(D2D1_FACTORY_TYPE, *************** *** 1212,1219 **** { #ifdef DYNAMIC_DIRECTX // Load libraries. ! hD2D1DLL = vimLoadLib(const_cast("d2d1.dll")); ! hDWriteDLL = vimLoadLib(const_cast("dwrite.dll")); if (hD2D1DLL == NULL || hDWriteDLL == NULL) { DWrite_Final(); --- 1212,1219 ---- { #ifdef DYNAMIC_DIRECTX // Load libraries. ! hD2D1DLL = vimLoadLib("d2d1.dll"); ! hDWriteDLL = vimLoadLib("dwrite.dll"); if (hD2D1DLL == NULL || hDWriteDLL == NULL) { DWrite_Final(); *** ../vim-8.2.4353/src/if_cscope.c 2022-01-29 15:12:35.172146951 +0000 --- src/if_cscope.c 2022-02-12 11:12:49.749338736 +0000 *************** *** 1371,1380 **** char *winmsg = GetWin32Error(); if (winmsg != NULL) - { (void)semsg(cant_msg, winmsg); - LocalFree(winmsg); - } else // subst filename if can't get error text (void)semsg(cant_msg, fname); --- 1371,1377 ---- *** ../vim-8.2.4353/src/os_win32.c 2022-02-07 13:53:56.376933433 +0000 --- src/os_win32.c 2022-02-12 11:12:49.749338736 +0000 *************** *** 520,526 **** * Load library "name". */ HINSTANCE ! vimLoadLib(char *name) { HINSTANCE dll = NULL; --- 520,526 ---- * Load library "name". */ HINSTANCE ! vimLoadLib(const char *name) { HINSTANCE dll = NULL; *************** *** 8279,8293 **** --- 8279,8298 ---- char * GetWin32Error(void) { + static char *oldmsg = NULL; char *msg = NULL; + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL); + if (oldmsg != NULL) + LocalFree(oldmsg); if (msg != NULL) { // remove trailing \r\n char *pcrlf = strstr(msg, "\r\n"); if (pcrlf != NULL) *pcrlf = '\0'; + oldmsg = msg; } return msg; } *** ../vim-8.2.4353/src/proto/crypt.pro 2022-01-19 13:32:53.443929932 +0000 --- src/proto/crypt.pro 2022-02-12 11:12:49.749338736 +0000 *************** *** 1,4 **** --- 1,5 ---- /* crypt.c */ + int sodium_enabled(int verbose); int crypt_method_nr_from_name(char_u *name); int crypt_method_nr_from_magic(char *ptr, int len); int crypt_works_inplace(cryptstate_T *state); *** ../vim-8.2.4353/src/proto/os_win32.pro 2022-02-07 13:53:56.376933433 +0000 --- src/proto/os_win32.pro 2022-02-12 11:12:49.749338736 +0000 *************** *** 1,5 **** /* os_win32.c */ ! HINSTANCE vimLoadLib(char *name); int mch_is_gui_executable(void); HINSTANCE find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname); void *get_dll_import_func(HINSTANCE hInst, const char *funcname); --- 1,5 ---- /* os_win32.c */ ! HINSTANCE vimLoadLib(const char *name); int mch_is_gui_executable(void); HINSTANCE find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname); void *get_dll_import_func(HINSTANCE hInst, const char *funcname); *** ../vim-8.2.4353/src/version.c 2022-02-12 10:53:00.341857471 +0000 --- src/version.c 2022-02-12 11:16:15.838128309 +0000 *************** *** 548,554 **** --- 548,558 ---- "-smartindent", #endif #ifdef FEAT_SODIUM + # ifdef DYNAMIC_SODIUM + "+sodium/dyn", + # else "+sodium", + # endif #else "-sodium", #endif *** ../vim-8.2.4353/src/version.c 2022-02-12 10:53:00.341857471 +0000 --- src/version.c 2022-02-12 11:16:15.838128309 +0000 *************** *** 748,749 **** --- 752,755 ---- { /* Add new patch number below this line */ + /**/ + 4354, /**/ -- What a wonderfully exciting cough! Do you mind if I join you? -- Douglas Adams, "The Hitchhiker's Guide to the Galaxy" /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///