Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / sandbox / win / src / eat_resolver.cc
blob328ee00f2f171f3b43d6371badd6f084bbf692bd
1 // Copyright (c) 2006-2010 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.
5 #include "sandbox/win/src/eat_resolver.h"
7 #include "base/win/pe_image.h"
8 #include "sandbox/win/src/sandbox_nt_util.h"
10 namespace sandbox {
12 NTSTATUS EatResolverThunk::Setup(const void* target_module,
13 const void* interceptor_module,
14 const char* target_name,
15 const char* interceptor_name,
16 const void* interceptor_entry_point,
17 void* thunk_storage,
18 size_t storage_bytes,
19 size_t* storage_used) {
20 NTSTATUS ret = Init(target_module, interceptor_module, target_name,
21 interceptor_name, interceptor_entry_point,
22 thunk_storage, storage_bytes);
23 if (!NT_SUCCESS(ret))
24 return ret;
26 if (!eat_entry_)
27 return STATUS_INVALID_PARAMETER;
29 #if defined(_WIN64)
30 // We have two thunks, in order: the return path and the forward path.
31 if (!SetInternalThunk(thunk_storage, storage_bytes, NULL, target_))
32 return STATUS_BUFFER_TOO_SMALL;
34 size_t thunk_bytes = GetInternalThunkSize();
35 storage_bytes -= thunk_bytes;
36 thunk_storage = reinterpret_cast<char*>(thunk_storage) + thunk_bytes;
37 #endif
39 if (!SetInternalThunk(thunk_storage, storage_bytes, target_, interceptor_))
40 return STATUS_BUFFER_TOO_SMALL;
42 AutoProtectMemory memory;
43 ret = memory.ChangeProtection(eat_entry_, sizeof(DWORD), PAGE_READWRITE);
44 if (!NT_SUCCESS(ret))
45 return ret;
47 // Perform the patch.
48 #pragma warning(push)
49 #pragma warning(disable: 4311)
50 // These casts generate warnings because they are 32 bit specific.
51 *eat_entry_ = reinterpret_cast<DWORD>(thunk_storage) -
52 reinterpret_cast<DWORD>(target_module);
53 #pragma warning(pop)
55 if (NULL != storage_used)
56 *storage_used = GetThunkSize();
58 return ret;
61 NTSTATUS EatResolverThunk::ResolveTarget(const void* module,
62 const char* function_name,
63 void** address) {
64 DCHECK_NT(address);
65 if (!module)
66 return STATUS_INVALID_PARAMETER;
68 base::win::PEImage pe(module);
69 if (!pe.VerifyMagic())
70 return STATUS_INVALID_IMAGE_FORMAT;
72 eat_entry_ = pe.GetExportEntry(function_name);
74 if (!eat_entry_)
75 return STATUS_PROCEDURE_NOT_FOUND;
77 *address = pe.RVAToAddr(*eat_entry_);
79 return STATUS_SUCCESS;
82 size_t EatResolverThunk::GetThunkSize() const {
83 #if defined(_WIN64)
84 return GetInternalThunkSize() * 2;
85 #else
86 return GetInternalThunkSize();
87 #endif
90 } // namespace sandbox