Skip to content

Commit ee64af1

Browse files
CDRIVER-4231: New random generation of OID values (#931)
* New random generation of OID values [Fixes CDRIVER-4231] * Restore hashing of machine attributes, plus an atomic init-counter, plus the process ID. * Instead of MD5 hashing, use SipHash. * No more magic numbers, make bson-context -Wconversion clean, comment what's happening in the random init. * Remove old deprecated docs and no-op tests. * Host name mocking is gone
1 parent ec13e0b commit ee64af1

File tree

12 files changed

+318
-469
lines changed

12 files changed

+318
-469
lines changed

src/libbson/CMakeLists.txt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,6 @@ else ()
7070
set (BSON_HAVE_GMTIME_R 1)
7171
endif ()
7272

73-
CHECK_SYMBOL_EXISTS (arc4random_buf stdlib.h BSON_HAVE_ARC4RANDOM_BUF)
74-
if (NOT BSON_HAVE_ARC4RANDOM_BUF)
75-
set (BSON_HAVE_ARC4RANDOM_BUF 0)
76-
else ()
77-
set (BSON_HAVE_ARC4RANDOM_BUF 1)
78-
endif ()
79-
8073
CHECK_FUNCTION_EXISTS (rand_r BSON_HAVE_RAND_R)
8174
if (NOT BSON_HAVE_RAND_R)
8275
set (BSON_HAVE_RAND_R 0)
@@ -109,7 +102,6 @@ CHECK_INCLUDE_FILE (stdbool.h BSON_HAVE_STDBOOL_H)
109102
if (MSVC)
110103
set (BSON_HAVE_CLOCK_GETTIME 0)
111104
set (BSON_HAVE_STRNLEN 0)
112-
set (BSON_HAVE_SYSCALL_TID 0)
113105
else ()
114106
check_symbol_exists (clock_gettime time.h BSON_HAVE_CLOCK_GETTIME)
115107
if (NOT BSON_HAVE_CLOCK_GETTIME)
@@ -119,11 +111,6 @@ else ()
119111
if (NOT BSON_HAVE_STRNLEN)
120112
set (BSON_HAVE_STRNLEN 0)
121113
endif ()
122-
CHECK_SYMBOL_EXISTS (SYS_gettid sys/syscall.h BSON_HAVE_SYSCALL_TID)
123-
check_symbol_exists (syscall unistd.h _TMP_HAVE_SYSCALL)
124-
if (NOT BSON_HAVE_SYSCALL_TID OR NOT _TMP_HAVE_SYSCALL OR APPLE OR ANDROID)
125-
set (BSON_HAVE_SYSCALL_TID 0)
126-
endif ()
127114
endif ()
128115

129116
if (BSON_BIG_ENDIAN)

src/libbson/doc/bson_context_new.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ Parameters
1818

1919
The following ``flags`` may be used:
2020
* ``BSON_CONTEXT_NONE`` meaning creating ObjectIDs with this context is not a thread-safe operation.
21-
* ``BSON_CONTEXT_THREAD_SAFE`` meaning creating ObjectIDs with this context is a thread-safe operation.
2221
* ``BSON_CONTEXT_DISABLE_PID_CACHE`` meaning creating ObjectIDs will also check if the process has
2322
changed by calling ``getpid()`` on every ObjectID generation.
2423

25-
To use multiple flags, xor them together.
24+
The following flags are deprecated and have no effect:
25+
26+
- ``BSON_CONTEXT_DISABLE_HOST_CACHE``
27+
- ``BSON_CONTEXT_THREAD_SAFE``
28+
- ``BSON_CONTEXT_USE_TASK_ID``
2629

2730
Description
2831
-----------

src/libbson/doc/bson_context_t.rst

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ Synopsis
1414
1515
typedef enum {
1616
BSON_CONTEXT_NONE = 0,
17-
BSON_CONTEXT_THREAD_SAFE = (1 << 0),
18-
BSON_CONTEXT_DISABLE_HOST_CACHE = (1 << 1),
1917
BSON_CONTEXT_DISABLE_PID_CACHE = (1 << 2),
20-
#ifdef BSON_HAVE_SYSCALL_TID
21-
BSON_CONTEXT_USE_TASK_ID = (1 << 3),
22-
#endif
2318
} bson_context_flags_t;
2419
2520
typedef struct _bson_context_t bson_context_t;
@@ -69,7 +64,7 @@ Example
6964
bson_oid_init (&oid, NULL);
7065
7166
/* specify a local context for additional control */
72-
ctx = bson_context_new (BSON_CONTEXT_THREAD_SAFE);
67+
ctx = bson_context_new (BSON_CONTEXT_NONE);
7368
bson_oid_init (&oid, ctx);
7469
7570
bson_context_destroy (ctx);

src/libbson/src/bson/bson-config.h.in

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,6 @@
105105
#endif
106106

107107

108-
/*
109-
* Define to 1 if you have SYS_gettid syscall
110-
*/
111-
#define BSON_HAVE_SYSCALL_TID @BSON_HAVE_SYSCALL_TID@
112-
#if BSON_HAVE_SYSCALL_TID != 1
113-
# undef BSON_HAVE_SYSCALL_TID
114-
#endif
115-
116-
117-
/*
118-
* Define to 1 if you have arc4random_buf available on your platform.
119-
*/
120-
#define BSON_HAVE_ARC4RANDOM_BUF @BSON_HAVE_ARC4RANDOM_BUF@
121-
#if BSON_HAVE_ARC4RANDOM_BUF != 1
122-
# undef BSON_HAVE_ARC4RANDOM_BUF
123-
#endif
124-
125-
126108
/*
127109
* Define to 1 if you have rand_r available on your platform.
128110
*/

src/libbson/src/bson/bson-context-private.h

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,55 @@
2828
BSON_BEGIN_DECLS
2929

3030

31+
enum {
32+
BSON_OID_RANDOMESS_OFFSET = 4,
33+
BSON_OID_RANDOMNESS_SIZE = 5,
34+
BSON_OID_SEQ32_OFFSET = 9,
35+
BSON_OID_SEQ32_SIZE = 3,
36+
BSON_OID_SEQ64_OFFSET = 4,
37+
BSON_OID_SEQ64_SIZE = 8
38+
};
39+
3140
struct _bson_context_t {
3241
/* flags are defined in bson_context_flags_t */
3342
int flags;
34-
int32_t seq32;
35-
int64_t seq64;
36-
uint8_t rand[5];
37-
uint16_t pid;
38-
39-
void (*oid_set_seq32) (bson_context_t *context, bson_oid_t *oid);
40-
void (*oid_set_seq64) (bson_context_t *context, bson_oid_t *oid);
41-
42-
/* this function pointer allows us to mock gethostname for testing. */
43-
void (*gethostname) (char *out);
43+
uint32_t seq32;
44+
uint64_t seq64;
45+
uint8_t randomness[BSON_OID_RANDOMNESS_SIZE];
46+
uint64_t pid;
4447
};
4548

49+
/**
50+
* @brief Insert the context's randomness data into the given OID
51+
*
52+
* @param context A context for some random data
53+
* @param oid The OID to update.
54+
*/
4655
void
4756
_bson_context_set_oid_rand (bson_context_t *context, bson_oid_t *oid);
4857

58+
/**
59+
* @brief Insert the context's sequence counter into the given OID. Increments
60+
* the context's sequence counter.
61+
*
62+
* @param context The context with the counter to get+update
63+
* @param oid The OID to modify
64+
*/
65+
void
66+
_bson_context_set_oid_seq32 (bson_context_t *context, bson_oid_t *oid);
67+
68+
/**
69+
* @brief Write a 64-bit counter from the given context into the OID. Increments
70+
* the context's sequence counter.
71+
*
72+
* @param context The context with the counter to get+update
73+
* @param oid The OID to modify
74+
*
75+
* @note Only used by the deprecated @ref bson_oid_init_sequence
76+
*/
77+
void
78+
_bson_context_set_oid_seq64 (bson_context_t *context, bson_oid_t *oid);
79+
4980

5081
BSON_END_DECLS
5182

0 commit comments

Comments
 (0)