]>
Commit | Line | Data |
---|---|---|
89deb186 | 1 | /* |
77b1029d | 2 | * Copyright (C) 1996-2020 The Squid Software Foundation and contributors |
89deb186 AJ |
3 | * |
4 | * Squid software is distributed under GPLv2+ license and includes | |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
7 | */ | |
8 | ||
f97700a0 AJ |
9 | #ifndef SQUID_SRC_SECURITY_LOCKINGPOINTER_H |
10 | #define SQUID_SRC_SECURITY_LOCKINGPOINTER_H | |
11 | ||
4103b0c1 AJ |
12 | #include "base/HardFun.h" |
13 | ||
eacc1666 | 14 | #if USE_OPENSSL |
24b30fdc | 15 | #include "compat/openssl.h" |
eacc1666 AJ |
16 | #if HAVE_OPENSSL_CRYPTO_H |
17 | #include <openssl/crypto.h> | |
18 | #endif | |
19 | ||
20 | // Macro to be used to define the C++ wrapper function of a sk_*_pop_free | |
21 | // openssl family functions. The C++ function suffixed with the _free_wrapper | |
22 | // extension | |
23 | #define sk_free_wrapper(sk_object, argument, freefunction) \ | |
24 | extern "C++" inline void sk_object ## _free_wrapper(argument a) { \ | |
25 | sk_object ## _pop_free(a, freefunction); \ | |
26 | } | |
27 | ||
fdfa0570 | 28 | #endif /* USE_OPENSSL */ |
eacc1666 AJ |
29 | |
30 | // Macro to be used to define the C++ equivalent function of an extern "C" | |
31 | // function. The C++ function suffixed with the _cpp extension | |
32 | #define CtoCpp1(function, argument) \ | |
33 | extern "C++" inline void function ## _cpp(argument a) { \ | |
34 | function(a); \ | |
35 | } | |
36 | ||
f97700a0 AJ |
37 | namespace Security |
38 | { | |
39 | ||
4103b0c1 AJ |
40 | inline bool nilFunction(const void *) { return false; } |
41 | typedef HardFun<bool, const void *, nilFunction> NilFunctor; | |
42 | ||
f97700a0 | 43 | /** |
d2e36b65 AJ |
44 | * A shared pointer to a reference-counting Object with library-specific |
45 | * absorption, locking, and unlocking implementations. The API largely | |
46 | * follows std::shared_ptr. | |
47 | * | |
35b3559c | 48 | * The constructor and the resetWithoutLocking() method import a raw Object pointer. |
d2e36b65 AJ |
49 | * Normally, reset() would lock(), but libraries like OpenSSL |
50 | * pre-lock objects before they are fed to LockingPointer, necessitating | |
35b3559c | 51 | * this resetWithoutLocking() customization hook. |
89deb186 | 52 | */ |
4103b0c1 | 53 | template <typename T, void (*UnLocker)(T *t), class Locker = NilFunctor> |
fdfa0570 | 54 | class LockingPointer |
f97700a0 AJ |
55 | { |
56 | public: | |
fdfa0570 | 57 | /// a helper label to simplify this objects API definitions below |
4103b0c1 | 58 | typedef Security::LockingPointer<T, UnLocker, Locker> SelfType; |
89deb186 | 59 | |
fdfa0570 AJ |
60 | /** |
61 | * Construct directly from a raw pointer. | |
62 | * This action requires that the producer of that pointer has already | |
63 | * created one reference lock for the object pointed to. | |
d2e36b65 | 64 | * Our destructor will do the matching unlock. |
fdfa0570 | 65 | */ |
58a5291c AJ |
66 | explicit LockingPointer(T *t = nullptr): raw(nullptr) { |
67 | // de-optimized for clarity about non-locking | |
68 | resetWithoutLocking(t); | |
69 | } | |
89deb186 | 70 | |
d2e36b65 AJ |
71 | /// use the custom UnLocker to unlock any value still stored. |
72 | ~LockingPointer() { unlock(); } | |
89deb186 | 73 | |
fdfa0570 | 74 | // copy semantics are okay only when adding a lock reference |
b23f5f9c | 75 | LockingPointer(const SelfType &o) : raw(nullptr) { |
eba8d9bb AJ |
76 | resetAndLock(o.get()); |
77 | } | |
78 | const SelfType &operator =(const SelfType &o) { | |
014a9017 | 79 | resetAndLock(o.get()); |
89deb186 AJ |
80 | return *this; |
81 | } | |
f97700a0 | 82 | |
b9a9207b AJ |
83 | LockingPointer(SelfType &&o) : raw(nullptr) { |
84 | resetWithoutLocking(o.release()); | |
85 | } | |
7007ee99 AJ |
86 | SelfType &operator =(SelfType &&o) { |
87 | if (o.get() != raw) | |
35b3559c | 88 | resetWithoutLocking(o.release()); |
7007ee99 AJ |
89 | return *this; |
90 | } | |
89deb186 | 91 | |
fdfa0570 AJ |
92 | bool operator !() const { return !raw; } |
93 | explicit operator bool() const { return raw; } | |
92e3827b AJ |
94 | bool operator ==(const SelfType &o) const { return (o.get() == raw); } |
95 | bool operator !=(const SelfType &o) const { return (o.get() != raw); } | |
fdfa0570 | 96 | |
ad23e748 AJ |
97 | T *operator ->() const { return raw; } |
98 | ||
fdfa0570 AJ |
99 | /// Returns raw and possibly nullptr pointer |
100 | T *get() const { return raw; } | |
101 | ||
d2e36b65 | 102 | /// Reset raw pointer - unlock any previous one and save new one without locking. |
35b3559c | 103 | void resetWithoutLocking(T *t) { |
d2e36b65 | 104 | unlock(); |
fdfa0570 | 105 | raw = t; |
89deb186 | 106 | } |
f97700a0 | 107 | |
014a9017 | 108 | void resetAndLock(T *t) { |
ccf24f29 | 109 | if (t != get()) { |
35b3559c | 110 | resetWithoutLocking(t); |
d2e36b65 | 111 | lock(t); |
f97700a0 AJ |
112 | } |
113 | } | |
fdfa0570 | 114 | |
58a5291c AJ |
115 | /// Forget the raw pointer - unlock if any value was set. Become a nil pointer. |
116 | void reset() { unlock(); } | |
117 | ||
d2e36b65 | 118 | /// Forget the raw pointer without unlocking it. Become a nil pointer. |
fdfa0570 AJ |
119 | T *release() { |
120 | T *ret = raw; | |
121 | raw = nullptr; | |
122 | return ret; | |
123 | } | |
124 | ||
125 | private: | |
58a5291c | 126 | /// The lock() method increments Object's reference counter. |
d2e36b65 | 127 | void lock(T *t) { |
4103b0c1 AJ |
128 | if (t) { |
129 | Locker doLock; | |
130 | doLock(t); | |
131 | } | |
d2e36b65 AJ |
132 | } |
133 | ||
58a5291c AJ |
134 | /// Become a nil pointer. Decrements any pointed-to Object's reference counter |
135 | /// using UnLocker which ideally destroys the object when the counter reaches zero. | |
d2e36b65 | 136 | void unlock() { |
58a5291c | 137 | if (raw) { |
d2e36b65 | 138 | UnLocker(raw); |
58a5291c AJ |
139 | raw = nullptr; |
140 | } | |
fdfa0570 AJ |
141 | } |
142 | ||
58a5291c AJ |
143 | /** |
144 | * Normally, no other code will have this raw pointer. | |
145 | * | |
146 | * However, OpenSSL does some strange and not always consistent things. | |
147 | * OpenSSL library may keep its own internal raw pointers and manage | |
148 | * their reference counts independently, or it may not. This varies between | |
149 | * API functions, though it is usually documented. | |
150 | * | |
151 | * This means the caller code needs to be carefuly written to use the correct | |
152 | * reset method and avoid the raw-pointer constructor unless OpenSSL function | |
153 | * producing the pointer is clearly documented as incrementing a lock for it. | |
154 | */ | |
155 | T *raw; | |
f97700a0 AJ |
156 | }; |
157 | ||
158 | } // namespace Security | |
159 | ||
160 | #endif /* SQUID_SRC_SECURITY_LOCKINGPOINTER_H */ | |
63b8c4d7 | 161 |