security/sandbox/win/src/sharedmem_ipc_server.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/win/src/sharedmem_ipc_server.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#ifndef SANDBOX_SRC_SHAREDMEM_IPC_SERVER_H_
     1.9 +#define SANDBOX_SRC_SHAREDMEM_IPC_SERVER_H_
    1.10 +
    1.11 +#include <list>
    1.12 +
    1.13 +#include "base/basictypes.h"
    1.14 +#include "base/gtest_prod_util.h"
    1.15 +#include "sandbox/win/src/crosscall_params.h"
    1.16 +#include "sandbox/win/src/crosscall_server.h"
    1.17 +#include "sandbox/win/src/sharedmem_ipc_client.h"
    1.18 +
    1.19 +// IPC transport implementation that uses shared memory.
    1.20 +// This is the server side
    1.21 +//
    1.22 +// The server side has knowledge about the layout of the shared memory
    1.23 +// and the state transitions. Both are explained in sharedmem_ipc_client.h
    1.24 +//
    1.25 +// As opposed to SharedMemIPClient, the Server object should be one for the
    1.26 +// entire lifetime of the target process. The server is in charge of creating
    1.27 +// the events (ping, pong) both for the client and for the target that are used
    1.28 +// to signal the IPC and also in charge of setting the initial state of the
    1.29 +// channels.
    1.30 +//
    1.31 +// When an IPC is ready, the server relies on being called by on the
    1.32 +// ThreadPingEventReady callback. The IPC server then retrieves the buffer,
    1.33 +// marshals it into a CrossCallParam object and calls the Dispatcher, who is in
    1.34 +// charge of fulfilling the IPC request.
    1.35 +namespace sandbox {
    1.36 +
    1.37 +// the shared memory implementation of the IPC server. There should be one
    1.38 +// of these objects per target (IPC client) process
    1.39 +class SharedMemIPCServer {
    1.40 + public:
    1.41 +  // Creates the IPC server.
    1.42 +  // target_process: handle to the target process. It must be suspended.
    1.43 +  // target_process_id: process id of the target process.
    1.44 +  // target_job: the job object handle associated with the target process.
    1.45 +  // thread_provider: a thread provider object.
    1.46 +  // dispatcher: an object that can service IPC calls.
    1.47 +  SharedMemIPCServer(HANDLE target_process, DWORD target_process_id,
    1.48 +                     HANDLE target_job, ThreadProvider* thread_provider,
    1.49 +                     Dispatcher* dispatcher);
    1.50 +
    1.51 +  ~SharedMemIPCServer();
    1.52 +
    1.53 +  // Initializes the server structures, shared memory structures and
    1.54 +  // creates the kernels events used to signal the IPC.
    1.55 +  bool Init(void* shared_mem, uint32 shared_size, uint32 channel_size);
    1.56 +
    1.57 + private:
    1.58 +  // Allow tests to be marked DISABLED_. Note that FLAKY_ and FAILS_ prefixes
    1.59 +  // do not work with sandbox tests.
    1.60 +  FRIEND_TEST_ALL_PREFIXES(IPCTest, SharedMemServerTests);
    1.61 +  // When an event fires (IPC request). A thread from the ThreadProvider
    1.62 +  // will call this function. The context parameter should be the same as
    1.63 +  // provided when ThreadProvider::RegisterWait was called.
    1.64 +  static void __stdcall ThreadPingEventReady(void* context,
    1.65 +                                             unsigned char);
    1.66 +
    1.67 +  // Makes the client and server events. This function is called once
    1.68 +  // per channel.
    1.69 +  bool MakeEvents(HANDLE* server_ping, HANDLE* server_pong,
    1.70 +                  HANDLE* client_ping, HANDLE* client_pong);
    1.71 +
    1.72 +  // A copy this structure is maintained per channel.
    1.73 +  // Note that a lot of the fields are just the same of what we have in the IPC
    1.74 +  // object itself. It is better to have the copies since we can dispatch in the
    1.75 +  // static method without worrying about converting back to a member function
    1.76 +  // call or about threading issues.
    1.77 +  struct ServerControl {
    1.78 +    // This channel server ping event.
    1.79 +    HANDLE ping_event;
    1.80 +    // This channel server pong event.
    1.81 +    HANDLE pong_event;
    1.82 +    // The size of this channel.
    1.83 +    uint32 channel_size;
    1.84 +    // The pointer to the actual channel data.
    1.85 +    char* channel_buffer;
    1.86 +    // The pointer to the base of the shared memory.
    1.87 +    char* shared_base;
    1.88 +    // A pointer to this channel's client-side control structure this structure
    1.89 +    // lives in the shared memory.
    1.90 +    ChannelControl* channel;
    1.91 +    // the IPC dispatcher associated with this channel.
    1.92 +    Dispatcher* dispatcher;
    1.93 +    // The target process information associated with this channel.
    1.94 +    ClientInfo target_info;
    1.95 +  };
    1.96 +
    1.97 +  // Looks for the appropriate handler for this IPC and invokes it.
    1.98 +  static bool InvokeCallback(const ServerControl* service_context,
    1.99 +                             void* ipc_buffer, CrossCallReturn* call_result);
   1.100 +
   1.101 +  // Points to the shared memory channel control which lives at
   1.102 +  // the start of the shared section.
   1.103 +  IPCControl* client_control_;
   1.104 +
   1.105 +  // Keeps track of the server side objects that are used to answer an IPC.
   1.106 +  typedef std::list<ServerControl*> ServerContexts;
   1.107 +  ServerContexts server_contexts_;
   1.108 +
   1.109 +  // The thread provider provides the threads that call back into this object
   1.110 +  // when the IPC events fire.
   1.111 +  ThreadProvider* thread_provider_;
   1.112 +
   1.113 +  // The IPC object is associated with a target process.
   1.114 +  HANDLE target_process_;
   1.115 +
   1.116 +  // The target process id associated with the IPC object.
   1.117 +  DWORD target_process_id_;
   1.118 +
   1.119 +  // The target object is inside a job too.
   1.120 +  HANDLE target_job_object_;
   1.121 +
   1.122 +  // The dispatcher handles 'ready' IPC calls.
   1.123 +  Dispatcher* call_dispatcher_;
   1.124 +
   1.125 +  DISALLOW_COPY_AND_ASSIGN(SharedMemIPCServer);
   1.126 +};
   1.127 +
   1.128 +}  // namespace sandbox
   1.129 +
   1.130 +#endif  // SANDBOX_SRC_SHAREDMEM_IPC_SERVER_H_

mercurial