1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <intrin.h>
6 #include <limits>
7 #include <new.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <windows.h>
11 
12 extern "C" {
13 #include "../libavutil/mem.h"
14 }
15 
16 namespace {
17 
OnNoMemory(size_t size)18 int OnNoMemory(size_t size) {
19   // Kill the process. This is important for security, since ffmpeg doesn't
20   // NULL-check many memory allocations. If a malloc fails, returns NULL, and
21   // the buffer is then used, it provides a handy mapping of memory starting at
22   // address 0 for an attacker to utilize.
23   __debugbreak();
24   _exit(1);
25 }
26 
27 }  // namespace
28 
DllMain(HINSTANCE instance,DWORD reason,LPVOID reserved)29 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) {
30   if (reason == DLL_PROCESS_ATTACH) {
31     DisableThreadLibraryCalls(instance);
32     // Remove allocation limit from ffmpeg, so calls go down to shim layer.
33     av_max_alloc(std::numeric_limits<size_t>::max());
34     // Enable OOM crashes in the shim for all malloc calls that fail.
35     _set_new_mode(1);
36     _set_new_handler(&OnNoMemory);
37   }
38   return true;
39 }
40