Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef SANDBOX_SRC_SHAREDMEM_IPC_SERVER_H_ |
michael@0 | 6 | #define SANDBOX_SRC_SHAREDMEM_IPC_SERVER_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <list> |
michael@0 | 9 | |
michael@0 | 10 | #include "base/basictypes.h" |
michael@0 | 11 | #include "base/gtest_prod_util.h" |
michael@0 | 12 | #include "sandbox/win/src/crosscall_params.h" |
michael@0 | 13 | #include "sandbox/win/src/crosscall_server.h" |
michael@0 | 14 | #include "sandbox/win/src/sharedmem_ipc_client.h" |
michael@0 | 15 | |
michael@0 | 16 | // IPC transport implementation that uses shared memory. |
michael@0 | 17 | // This is the server side |
michael@0 | 18 | // |
michael@0 | 19 | // The server side has knowledge about the layout of the shared memory |
michael@0 | 20 | // and the state transitions. Both are explained in sharedmem_ipc_client.h |
michael@0 | 21 | // |
michael@0 | 22 | // As opposed to SharedMemIPClient, the Server object should be one for the |
michael@0 | 23 | // entire lifetime of the target process. The server is in charge of creating |
michael@0 | 24 | // the events (ping, pong) both for the client and for the target that are used |
michael@0 | 25 | // to signal the IPC and also in charge of setting the initial state of the |
michael@0 | 26 | // channels. |
michael@0 | 27 | // |
michael@0 | 28 | // When an IPC is ready, the server relies on being called by on the |
michael@0 | 29 | // ThreadPingEventReady callback. The IPC server then retrieves the buffer, |
michael@0 | 30 | // marshals it into a CrossCallParam object and calls the Dispatcher, who is in |
michael@0 | 31 | // charge of fulfilling the IPC request. |
michael@0 | 32 | namespace sandbox { |
michael@0 | 33 | |
michael@0 | 34 | // the shared memory implementation of the IPC server. There should be one |
michael@0 | 35 | // of these objects per target (IPC client) process |
michael@0 | 36 | class SharedMemIPCServer { |
michael@0 | 37 | public: |
michael@0 | 38 | // Creates the IPC server. |
michael@0 | 39 | // target_process: handle to the target process. It must be suspended. |
michael@0 | 40 | // target_process_id: process id of the target process. |
michael@0 | 41 | // target_job: the job object handle associated with the target process. |
michael@0 | 42 | // thread_provider: a thread provider object. |
michael@0 | 43 | // dispatcher: an object that can service IPC calls. |
michael@0 | 44 | SharedMemIPCServer(HANDLE target_process, DWORD target_process_id, |
michael@0 | 45 | HANDLE target_job, ThreadProvider* thread_provider, |
michael@0 | 46 | Dispatcher* dispatcher); |
michael@0 | 47 | |
michael@0 | 48 | ~SharedMemIPCServer(); |
michael@0 | 49 | |
michael@0 | 50 | // Initializes the server structures, shared memory structures and |
michael@0 | 51 | // creates the kernels events used to signal the IPC. |
michael@0 | 52 | bool Init(void* shared_mem, uint32 shared_size, uint32 channel_size); |
michael@0 | 53 | |
michael@0 | 54 | private: |
michael@0 | 55 | // Allow tests to be marked DISABLED_. Note that FLAKY_ and FAILS_ prefixes |
michael@0 | 56 | // do not work with sandbox tests. |
michael@0 | 57 | FRIEND_TEST_ALL_PREFIXES(IPCTest, SharedMemServerTests); |
michael@0 | 58 | // When an event fires (IPC request). A thread from the ThreadProvider |
michael@0 | 59 | // will call this function. The context parameter should be the same as |
michael@0 | 60 | // provided when ThreadProvider::RegisterWait was called. |
michael@0 | 61 | static void __stdcall ThreadPingEventReady(void* context, |
michael@0 | 62 | unsigned char); |
michael@0 | 63 | |
michael@0 | 64 | // Makes the client and server events. This function is called once |
michael@0 | 65 | // per channel. |
michael@0 | 66 | bool MakeEvents(HANDLE* server_ping, HANDLE* server_pong, |
michael@0 | 67 | HANDLE* client_ping, HANDLE* client_pong); |
michael@0 | 68 | |
michael@0 | 69 | // A copy this structure is maintained per channel. |
michael@0 | 70 | // Note that a lot of the fields are just the same of what we have in the IPC |
michael@0 | 71 | // object itself. It is better to have the copies since we can dispatch in the |
michael@0 | 72 | // static method without worrying about converting back to a member function |
michael@0 | 73 | // call or about threading issues. |
michael@0 | 74 | struct ServerControl { |
michael@0 | 75 | // This channel server ping event. |
michael@0 | 76 | HANDLE ping_event; |
michael@0 | 77 | // This channel server pong event. |
michael@0 | 78 | HANDLE pong_event; |
michael@0 | 79 | // The size of this channel. |
michael@0 | 80 | uint32 channel_size; |
michael@0 | 81 | // The pointer to the actual channel data. |
michael@0 | 82 | char* channel_buffer; |
michael@0 | 83 | // The pointer to the base of the shared memory. |
michael@0 | 84 | char* shared_base; |
michael@0 | 85 | // A pointer to this channel's client-side control structure this structure |
michael@0 | 86 | // lives in the shared memory. |
michael@0 | 87 | ChannelControl* channel; |
michael@0 | 88 | // the IPC dispatcher associated with this channel. |
michael@0 | 89 | Dispatcher* dispatcher; |
michael@0 | 90 | // The target process information associated with this channel. |
michael@0 | 91 | ClientInfo target_info; |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | // Looks for the appropriate handler for this IPC and invokes it. |
michael@0 | 95 | static bool InvokeCallback(const ServerControl* service_context, |
michael@0 | 96 | void* ipc_buffer, CrossCallReturn* call_result); |
michael@0 | 97 | |
michael@0 | 98 | // Points to the shared memory channel control which lives at |
michael@0 | 99 | // the start of the shared section. |
michael@0 | 100 | IPCControl* client_control_; |
michael@0 | 101 | |
michael@0 | 102 | // Keeps track of the server side objects that are used to answer an IPC. |
michael@0 | 103 | typedef std::list<ServerControl*> ServerContexts; |
michael@0 | 104 | ServerContexts server_contexts_; |
michael@0 | 105 | |
michael@0 | 106 | // The thread provider provides the threads that call back into this object |
michael@0 | 107 | // when the IPC events fire. |
michael@0 | 108 | ThreadProvider* thread_provider_; |
michael@0 | 109 | |
michael@0 | 110 | // The IPC object is associated with a target process. |
michael@0 | 111 | HANDLE target_process_; |
michael@0 | 112 | |
michael@0 | 113 | // The target process id associated with the IPC object. |
michael@0 | 114 | DWORD target_process_id_; |
michael@0 | 115 | |
michael@0 | 116 | // The target object is inside a job too. |
michael@0 | 117 | HANDLE target_job_object_; |
michael@0 | 118 | |
michael@0 | 119 | // The dispatcher handles 'ready' IPC calls. |
michael@0 | 120 | Dispatcher* call_dispatcher_; |
michael@0 | 121 | |
michael@0 | 122 | DISALLOW_COPY_AND_ASSIGN(SharedMemIPCServer); |
michael@0 | 123 | }; |
michael@0 | 124 | |
michael@0 | 125 | } // namespace sandbox |
michael@0 | 126 | |
michael@0 | 127 | #endif // SANDBOX_SRC_SHAREDMEM_IPC_SERVER_H_ |