summaryrefslogtreecommitdiffstats
path: root/chrome_frame/vtable_patch_manager.h
blob: 3e9e65b209b1ab3c02b055ac507a87485d45ad05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_FRAME_VTABLE_PATCH_MANAGER_H_
#define CHROME_FRAME_VTABLE_PATCH_MANAGER_H_

#include <windows.h>

#include <list>

#include "base/synchronization/lock.h"

struct FunctionStub;

// This namespace provides methods to patch VTable methods of COM interfaces.
namespace vtable_patch {

// Internal implementation, exposed only for testing.
namespace internal {

// Replaces *entry with new_proc iff *entry is curr_proc.
// Returns true iff *entry was rewritten.
// Note: does not crash on access violation.
bool ReplaceFunctionPointer(void** entry, void* new_proc, void* curr_proc);

}  // namespace internal

// This structure represents information about one VTable method.
// We allocate an array of these structures per VTable that we patch to
// remember the original method. We also use this structure to actually
// describe the VTable patch functions
struct MethodPatchInfo {
  int index_;
  PROC method_;
  FunctionStub* stub_;
};

// Patches methods in the passed in COM interface. The indexes of the
// methods to patch and the actual patch functions are described in the
// array pointed to by patches.
// @param[in] unknown  The pointer of the COM interface to patch
// @param[in] patches  An array of MethodPatchInfo structures describing
//  the methods to patch and the patch functions.
//  The last entry of patches must have index_ set to -1.
HRESULT PatchInterfaceMethods(void* unknown, MethodPatchInfo* patches);

// Using the patch info provided in |patches| the function goes through the
// list of patched methods and modifies thunks so that they no longer point
// to a hook method but rather go straight through to the original target.
// The thunk itself is not destroyed to support chaining.
// @param[in] patches  An array of MethodPatchInfo structures describing
//  the methods to patch and the patch functions.
//  The last entry of patches must have index_ set to -1.
HRESULT UnpatchInterfaceMethods(MethodPatchInfo* patches);

// Disabled as we're not using it atm.
#if 0
// Used when dynamically patching zero or more (usually more than 1)
// implementations of a particular interface.
class DynamicPatchManager {
 public:
  explicit DynamicPatchManager(const MethodPatchInfo* patch_prototype);
  ~DynamicPatchManager();

  // Returns S_OK if the object was successfully patched, S_FALSE if it was
  // already patched or an error value if something bad happened.
  HRESULT PatchObject(void* unknown);

  bool UnpatchAll();

 protected:
  struct PatchedObject {
    void* vtable_;
    MethodPatchInfo patch_info_[1];

    // Used to match PatchedObject instances based on the vtable when
    // searching through the patch list.
    bool operator==(const PatchedObject& that) const {
      return vtable_ == that.vtable_;
    }
  };

  typedef std::list<PatchedObject*> PatchList;
  const MethodPatchInfo* patch_prototype_;
  mutable base::Lock patch_list_lock_;
  PatchList patch_list_;
};
#endif  // disable DynamicPatchManager

}  // namespace vtable_patch

// Begins the declaration of a VTable patch
// @param IFName The name of the interface to patch
#define BEGIN_VTABLE_PATCHES(IFName) \
    vtable_patch::MethodPatchInfo IFName##_PatchInfo[] = {
// Defines a single method patch in a VTable
// @param index The index of the method to patch
// @param PatchFunction The patch function
#define VTABLE_PATCH_ENTRY(index, PatchFunction) {\
      index, \
      reinterpret_cast<PROC>(PatchFunction), \
      NULL, \
    },

#define DCHECK_IS_NOT_PATCHED(IFName) \
    for (vtable_patch::MethodPatchInfo* it = IFName##_PatchInfo; \
         it->index_ != -1; ++it) { \
      DCHECK(it->stub_ == NULL); \
    }

#define DCHECK_IS_PATCHED(IFName) \
    for (vtable_patch::MethodPatchInfo* it = IFName##_PatchInfo; \
         it->index_ != -1; ++it) { \
      DCHECK(it->stub_ != NULL); \
    }

// Checks if the interface is patched.  Note that only the first method
// is checked and subsequent methods are assumed to have the same state.
#define IS_PATCHED(IFName) \
  (IFName##_PatchInfo[0].stub_ != NULL)

// Ends the declaration of a VTable patch by adding an entry with
// index set to -1.
#define END_VTABLE_PATCHES() \
      -1, NULL, NULL \
    };

#endif  // CHROME_FRAME_VTABLE_PATCH_MANAGER_H_