Skip to content

Commit 231c393

Browse files
Abseil Teamsuertreus
Abseil Team
authored andcommitted
Export of internal Abseil changes
-- 5dc885f2b62993bccf33a3f3b99f7e460c819c89 by Derek Mauro <dmauro@google.com>: Remove the internal-only ABSL_INTERNAL_ASSUME now that ABSL_ASSUME is available and ABSL_INTERNAL_ASSUME has no more users. Improve the documentation to ABSL_ASSUME somewhat. PiperOrigin-RevId: 434803125 Change-Id: I7c27418463ffc1c7e10ecd50e2d17f348f686af7 -- 4aea19a0ef596228c9136a4c2446e6f25085f23c by Derek Mauro <dmauro@google.com>: Update documentation to warn against using absl::Hash across dynamically loaded libraries Fixes #1128 PiperOrigin-RevId: 434723247 Change-Id: Ib0c7ba03b2cab98b42e19e85be6833192d4b4067 GitOrigin-RevId: 5dc885f2b62993bccf33a3f3b99f7e460c819c89
1 parent 5ed7766 commit 231c393

File tree

6 files changed

+49
-23
lines changed

6 files changed

+49
-23
lines changed

absl/base/optimization.h

+28-23
Original file line numberDiff line numberDiff line change
@@ -181,32 +181,21 @@
181181
#define ABSL_PREDICT_TRUE(x) (x)
182182
#endif
183183

184-
// Platform and compilation mode dependent implementation of ABSL_ASSUME.
185-
#if !defined(NDEBUG)
186-
#define ABSL_INTERNAL_ASSUME(cond) assert(cond)
187-
#elif ABSL_HAVE_BUILTIN(__builtin_assume)
188-
#define ABSL_INTERNAL_ASSUME(cond) __builtin_assume(cond)
189-
#elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
190-
#define ABSL_INTERNAL_ASSUME(cond) \
191-
do { \
192-
if (!(cond)) __builtin_unreachable(); \
193-
} while (0)
194-
#elif defined(_MSC_VER)
195-
#define ABSL_INTERNAL_ASSUME(cond) __assume(cond)
196-
#else
197-
#define ABSL_INTERNAL_ASSUME(cond) \
198-
do { \
199-
static_cast<void>(false && (cond)); \
200-
} while (0)
201-
#endif
202-
203184
// ABSL_ASSUME(cond)
185+
//
204186
// Informs the compiler that a condition is always true and that it can assume
205-
// it to be true for optimization purposes. The call has undefined behavior if
206-
// the condition is false.
187+
// it to be true for optimization purposes.
188+
//
189+
// WARNING: If the condition is false, the program can produce undefined and
190+
// potentially dangerous behavior.
191+
//
207192
// In !NDEBUG mode, the condition is checked with an assert().
193+
//
208194
// NOTE: The expression must not have side effects, as it may only be evaluated
209-
// in some compilation modes and not others.
195+
// in some compilation modes and not others. Some compilers may issue a warning
196+
// if the compiler cannot prove the expression has no side effects. For example,
197+
// the expression should not use a function call since the compiler cannot prove
198+
// that a function call does not have side effects.
210199
//
211200
// Example:
212201
//
@@ -216,7 +205,23 @@
216205
// // assumption specified above.
217206
// int y = x / 16;
218207
//
219-
#define ABSL_ASSUME(cond) ABSL_INTERNAL_ASSUME(cond)
208+
#if !defined(NDEBUG)
209+
#define ABSL_ASSUME(cond) assert(cond)
210+
#elif ABSL_HAVE_BUILTIN(__builtin_assume)
211+
#define ABSL_ASSUME(cond) __builtin_assume(cond)
212+
#elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
213+
#define ABSL_ASSUME(cond) \
214+
do { \
215+
if (!(cond)) __builtin_unreachable(); \
216+
} while (0)
217+
#elif defined(_MSC_VER)
218+
#define ABSL_ASSUME(cond) __assume(cond)
219+
#else
220+
#define ABSL_ASSUME(cond) \
221+
do { \
222+
static_cast<void>(false && (cond)); \
223+
} while (0)
224+
#endif
220225

221226
// ABSL_INTERNAL_UNIQUE_SMALL_NAME(cond)
222227
// This macro forces small unique name on a static file level symbols like

absl/container/flat_hash_map.h

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ struct FlatHashMapPolicy;
7676
// absl/hash/hash.h for information on extending Abseil hashing to user-defined
7777
// types.
7878
//
79+
// Using `absl::flat_hash_map` at interface boundries in dynamically loaded
80+
// libraries (e.g. .dll, .so) is unsupported due to way `absl::Hash` values may
81+
// be randomized across dynamically loaded libraries.
82+
//
7983
// NOTE: A `flat_hash_map` stores its value types directly inside its
8084
// implementation array to avoid memory indirection. Because a `flat_hash_map`
8185
// is designed to move data when rehashed, map values will not retain pointer

absl/container/flat_hash_set.h

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ struct FlatHashSetPolicy;
7272
// absl/hash/hash.h for information on extending Abseil hashing to user-defined
7373
// types.
7474
//
75+
// Using `absl::flat_hash_set` at interface boundries in dynamically loaded
76+
// libraries (e.g. .dll, .so) is unsupported due to way `absl::Hash` values may
77+
// be randomized across dynamically loaded libraries.
78+
//
7579
// NOTE: A `flat_hash_set` stores its keys directly inside its implementation
7680
// array to avoid memory indirection. Because a `flat_hash_set` is designed to
7781
// move data when rehashed, set keys will not retain pointer stability. If you

absl/container/node_hash_map.h

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class NodeHashMapPolicy;
7878
// absl/hash/hash.h for information on extending Abseil hashing to user-defined
7979
// types.
8080
//
81+
// Using `absl::node_hash_map` at interface boundries in dynamically loaded
82+
// libraries (e.g. .dll, .so) is unsupported due to way `absl::Hash` values may
83+
// be randomized across dynamically loaded libraries.
84+
//
8185
// Example:
8286
//
8387
// // Create a node hash map of three strings (that map to strings)

absl/container/node_hash_set.h

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ struct NodeHashSetPolicy;
7474
// absl/hash/hash.h for information on extending Abseil hashing to user-defined
7575
// types.
7676
//
77+
// Using `absl::node_hash_set` at interface boundries in dynamically loaded
78+
// libraries (e.g. .dll, .so) is unsupported due to way `absl::Hash` values may
79+
// be randomized across dynamically loaded libraries.
80+
//
7781
// Example:
7882
//
7983
// // Create a node hash set of three strings

absl/hash/hash.h

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
// each process. E.g., `absl::Hash<int>{}(9)` in one process and
4141
// `absl::Hash<int>{}(9)` in another process are likely to differ.
4242
//
43+
// `absl::Hash` may also produce different values from different dynamically
44+
// loaded libraries. For this reason, `absl::Hash` values must never cross
45+
// boundries in dynamically loaded libraries (including when used in types like
46+
// hash containers.)
47+
//
4348
// `absl::Hash` is intended to strongly mix input bits with a target of passing
4449
// an [Avalanche Test](https://en.wikipedia.org/wiki/Avalanche_effect).
4550
//

0 commit comments

Comments
 (0)