From 7c70764508643c45b037f1ed9d77a95c24b05f6e Mon Sep 17 00:00:00 2001
From: Matt Oliver <protogonoi@gmail.com>
Date: Mon, 29 Aug 2016 16:46:10 +1000
Subject: [PATCH 2/2] Modify existing dll loading to use new safe dlopen.

---
 libavcodec/nvenc.c          |  9 ++-------
 libavformat/avisynth.c      | 13 ++++---------
 libavutil/hwcontext_dxva2.c | 16 ++++++++--------
 3 files changed, 14 insertions(+), 24 deletions(-)

diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
index 283f29f..f94a930 100644
--- a/libavcodec/nvenc.c
+++ b/libavcodec/nvenc.c
@@ -33,16 +33,11 @@
 # define NVENC_LIBNAME "libnvidia-encode.so.1"
 #endif
 
-#if defined(_WIN32)
-#include <windows.h>
-
-#define dlopen(filename, flags) LoadLibrary(TEXT(filename))
-#define dlsym(handle, symbol)   GetProcAddress(handle, symbol)
-#define dlclose(handle)         FreeLibrary(handle)
-#else
+#if !defined(_WIN32)
 #include <dlfcn.h>
 #endif
 
+#include "libavformat/os_support.h"
 #include "libavutil/hwcontext.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/avassert.h"
diff --git a/libavformat/avisynth.c b/libavformat/avisynth.c
index 04ac257..5741384 100644
--- a/libavformat/avisynth.c
+++ b/libavformat/avisynth.c
@@ -29,7 +29,6 @@
 
 /* Platform-specific directives for AviSynth vs AvxSynth. */
 #ifdef _WIN32
-  #include <windows.h>
   #undef EXTERN_C
   #include "compat/avisynth/avisynth_c.h"
   #define AVISYNTH_LIB "avisynth"
@@ -39,10 +38,6 @@
   #include "compat/avisynth/avxsynth_c.h"
   #define AVISYNTH_NAME "libavxsynth"
   #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
-
-  #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_LOCAL)
-  #define GetProcAddress dlsym
-  #define FreeLibrary dlclose
 #endif
 
 typedef struct AviSynthLibrary {
@@ -108,13 +103,13 @@ static av_cold void avisynth_atexit_handler(void);
 
 static av_cold int avisynth_load_library(void)
 {
-    avs_library.library = LoadLibrary(AVISYNTH_LIB);
+    avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);
     if (!avs_library.library)
         return AVERROR_UNKNOWN;
 
 #define LOAD_AVS_FUNC(name, continue_on_fail)                          \
         avs_library.name =                                             \
-            (void *)GetProcAddress(avs_library.library, #name);        \
+            (void *)dlsym(avs_library.library, #name);                 \
         if (!continue_on_fail && !avs_library.name)                    \
             goto fail;
 
@@ -145,7 +140,7 @@ static av_cold int avisynth_load_library(void)
     return 0;
 
 fail:
-    FreeLibrary(avs_library.library);
+    dlclose(avs_library.library);
     return AVERROR_UNKNOWN;
 }
 
@@ -213,7 +208,7 @@ static av_cold void avisynth_atexit_handler(void)
         avisynth_context_destroy(avs);
         avs = next;
     }
-    FreeLibrary(avs_library.library);
+    dlclose(avs_library.library);
 
     avs_atexit_called = 1;
 }
diff --git a/libavutil/hwcontext_dxva2.c b/libavutil/hwcontext_dxva2.c
index e79254b..bbba048 100644
--- a/libavutil/hwcontext_dxva2.c
+++ b/libavutil/hwcontext_dxva2.c
@@ -16,7 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <windows.h>
+#include "libavformat/os_support.h"
 
 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
 #undef _WIN32_WINNT
@@ -318,10 +318,10 @@ static void dxva2_device_free(AVHWDeviceContext *ctx)
         IDirect3D9_Release(priv->d3d9);
 
     if (priv->d3dlib)
-        FreeLibrary(priv->d3dlib);
+        dlclose(priv->d3dlib);
 
     if (priv->dxva2lib)
-        FreeLibrary(priv->dxva2lib);
+        dlclose(priv->dxva2lib);
 
     av_freep(&ctx->user_opaque);
 }
@@ -352,24 +352,24 @@ static int dxva2_device_create(AVHWDeviceContext *ctx, const char *device,
 
     priv->device_handle = INVALID_HANDLE_VALUE;
 
-    priv->d3dlib = LoadLibrary("d3d9.dll");
+    priv->d3dlib = dlopen("d3d9.dll", 0);
     if (!priv->d3dlib) {
         av_log(ctx, AV_LOG_ERROR, "Failed to load D3D9 library\n");
         return AVERROR_UNKNOWN;
     }
-    priv->dxva2lib = LoadLibrary("dxva2.dll");
+    priv->dxva2lib = dlopen("dxva2.dll", 0);
     if (!priv->dxva2lib) {
         av_log(ctx, AV_LOG_ERROR, "Failed to load DXVA2 library\n");
         return AVERROR_UNKNOWN;
     }
 
-    createD3D = (pDirect3DCreate9 *)GetProcAddress(priv->d3dlib, "Direct3DCreate9");
+    createD3D = (pDirect3DCreate9 *)dlsym(priv->d3dlib, "Direct3DCreate9");
     if (!createD3D) {
         av_log(ctx, AV_LOG_ERROR, "Failed to locate Direct3DCreate9\n");
         return AVERROR_UNKNOWN;
     }
-    createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(priv->dxva2lib,
-                                                                  "DXVA2CreateDirect3DDeviceManager9");
+    createDeviceManager = (pCreateDeviceManager9 *)dlsym(priv->dxva2lib,
+                                                         "DXVA2CreateDirect3DDeviceManager9");
     if (!createDeviceManager) {
         av_log(ctx, AV_LOG_ERROR, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
         return AVERROR_UNKNOWN;
-- 
2.9.0.windows.1

