summaryrefslogtreecommitdiffstats
path: root/ui/gl/safe_shared_memory_pool.h
blob: 9470cec3cfaa9407c04774a2f2179b17f50104f9 (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
// Copyright (c) 2011 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 UI_GL_SAFE_SHARED_MEMORY_POOL_H_
#define UI_GL_SAFE_SHARED_MEMORY_POOL_H_

#include <map>

#include "base/basictypes.h"
#include "base/shared_memory.h"
#include "base/synchronization/lock.h"
#include "build/build_config.h"

namespace base {
class SharedMemory;
}

namespace gfx {
class SafeSharedMemoryPool;
class ScopedSafeSharedMemory;
}

namespace gfx {

// These classes exist to help protect against deletion of shared
// memory that is being used on a worker thread. It's mainly a
// security measure to prevent use-after-free in the browser, due
// to a misbehaving client. That said, this should be removed
// in favor of higher-level reference counting of an appropriate
// opaque 'memory blob' data-structure.

class ScopedSafeSharedMemory {
 public:
  base::SharedMemory* shared_memory();
  ScopedSafeSharedMemory(SafeSharedMemoryPool* pool,
                         base::SharedMemory* memory,
                         size_t shm_size);
  ~ScopedSafeSharedMemory();
 private:
  base::SharedMemory* safe_shared_memory_;
  base::SharedMemoryHandle original_handle_;
  SafeSharedMemoryPool* pool_;

  DISALLOW_COPY_AND_ASSIGN(ScopedSafeSharedMemory);
};

class SafeSharedMemoryPool {
 public:
  SafeSharedMemoryPool();
  virtual ~SafeSharedMemoryPool();

 private:
  friend class ScopedSafeSharedMemory;

  // Acquires and release shared memory. The acquired shared memory
  // is guaranteed to live until it is released.
  base::SharedMemory* AcquireSafeSharedMemory(base::SharedMemory*, size_t size);
  void ReleaseSafeSharedMemory(const base::SharedMemoryHandle&);

  // Utility function to duplicate shared memory.
  base::SharedMemory* DuplicateSharedMemory(base::SharedMemory*, size_t size);

  // Track all SharedMemory's that we have already duplicated.
  struct TrackedMemory {
    base::SharedMemory* safe_shared_memory;
    size_t shm_size;
    int reference_count;
  };

  typedef std::map<base::SharedMemoryHandle, TrackedMemory> MemoryMap;
  MemoryMap memory_;

  // Track usage to diagnose crashes.
  int handles_acquired_;
  int handles_consumed_;
  size_t address_space_consumed_;
  int max_handles_acquired_;
  int max_handles_consumed_;
  size_t max_address_space_consumed_;

  base::Lock lock_;

  DISALLOW_COPY_AND_ASSIGN(SafeSharedMemoryPool);
};

}  // namespace gfx

#endif  // UI_GL_ASYNC_TASK_DELEGATE_H_