Skip to content

CDRIVER-4231: New random generation of OID values #931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/libbson/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ else ()
set (BSON_HAVE_GMTIME_R 1)
endif ()

CHECK_SYMBOL_EXISTS (arc4random_buf stdlib.h BSON_HAVE_ARC4RANDOM_BUF)
if (NOT BSON_HAVE_ARC4RANDOM_BUF)
set (BSON_HAVE_ARC4RANDOM_BUF 0)
else ()
set (BSON_HAVE_ARC4RANDOM_BUF 1)
endif ()

CHECK_FUNCTION_EXISTS (rand_r BSON_HAVE_RAND_R)
if (NOT BSON_HAVE_RAND_R)
set (BSON_HAVE_RAND_R 0)
Expand Down Expand Up @@ -109,7 +102,6 @@ CHECK_INCLUDE_FILE (stdbool.h BSON_HAVE_STDBOOL_H)
if (MSVC)
set (BSON_HAVE_CLOCK_GETTIME 0)
set (BSON_HAVE_STRNLEN 0)
set (BSON_HAVE_SYSCALL_TID 0)
else ()
check_symbol_exists (clock_gettime time.h BSON_HAVE_CLOCK_GETTIME)
if (NOT BSON_HAVE_CLOCK_GETTIME)
Expand All @@ -119,11 +111,6 @@ else ()
if (NOT BSON_HAVE_STRNLEN)
set (BSON_HAVE_STRNLEN 0)
endif ()
CHECK_SYMBOL_EXISTS (SYS_gettid sys/syscall.h BSON_HAVE_SYSCALL_TID)
check_symbol_exists (syscall unistd.h _TMP_HAVE_SYSCALL)
if (NOT BSON_HAVE_SYSCALL_TID OR NOT _TMP_HAVE_SYSCALL OR APPLE OR ANDROID)
set (BSON_HAVE_SYSCALL_TID 0)
endif ()
endif ()

if (BSON_BIG_ENDIAN)
Expand Down
7 changes: 5 additions & 2 deletions src/libbson/doc/bson_context_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ Parameters

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

To use multiple flags, xor them together.
The following flags are deprecated and have no effect:

- ``BSON_CONTEXT_DISABLE_HOST_CACHE``
- ``BSON_CONTEXT_THREAD_SAFE``
- ``BSON_CONTEXT_USE_TASK_ID``

Description
-----------
Expand Down
7 changes: 1 addition & 6 deletions src/libbson/doc/bson_context_t.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ Synopsis

typedef enum {
BSON_CONTEXT_NONE = 0,
BSON_CONTEXT_THREAD_SAFE = (1 << 0),
BSON_CONTEXT_DISABLE_HOST_CACHE = (1 << 1),
BSON_CONTEXT_DISABLE_PID_CACHE = (1 << 2),
#ifdef BSON_HAVE_SYSCALL_TID
BSON_CONTEXT_USE_TASK_ID = (1 << 3),
#endif
} bson_context_flags_t;

typedef struct _bson_context_t bson_context_t;
Expand Down Expand Up @@ -69,7 +64,7 @@ Example
bson_oid_init (&oid, NULL);

/* specify a local context for additional control */
ctx = bson_context_new (BSON_CONTEXT_THREAD_SAFE);
ctx = bson_context_new (BSON_CONTEXT_NONE);
bson_oid_init (&oid, ctx);

bson_context_destroy (ctx);
Expand Down
18 changes: 0 additions & 18 deletions src/libbson/src/bson/bson-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,6 @@
#endif


/*
* Define to 1 if you have SYS_gettid syscall
*/
#define BSON_HAVE_SYSCALL_TID @BSON_HAVE_SYSCALL_TID@
#if BSON_HAVE_SYSCALL_TID != 1
# undef BSON_HAVE_SYSCALL_TID
#endif


/*
* Define to 1 if you have arc4random_buf available on your platform.
*/
#define BSON_HAVE_ARC4RANDOM_BUF @BSON_HAVE_ARC4RANDOM_BUF@
#if BSON_HAVE_ARC4RANDOM_BUF != 1
# undef BSON_HAVE_ARC4RANDOM_BUF
#endif


/*
* Define to 1 if you have rand_r available on your platform.
*/
Expand Down
51 changes: 41 additions & 10 deletions src/libbson/src/bson/bson-context-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,55 @@
BSON_BEGIN_DECLS


enum {
BSON_OID_RANDOMESS_OFFSET = 4,
BSON_OID_RANDOMNESS_SIZE = 5,
BSON_OID_SEQ32_OFFSET = 9,
BSON_OID_SEQ32_SIZE = 3,
BSON_OID_SEQ64_OFFSET = 4,
BSON_OID_SEQ64_SIZE = 8
};

struct _bson_context_t {
/* flags are defined in bson_context_flags_t */
int flags;
int32_t seq32;
int64_t seq64;
uint8_t rand[5];
uint16_t pid;

void (*oid_set_seq32) (bson_context_t *context, bson_oid_t *oid);
void (*oid_set_seq64) (bson_context_t *context, bson_oid_t *oid);

/* this function pointer allows us to mock gethostname for testing. */
void (*gethostname) (char *out);
uint32_t seq32;
uint64_t seq64;
uint8_t randomness[BSON_OID_RANDOMNESS_SIZE];
uint64_t pid;
};

/**
* @brief Insert the context's randomness data into the given OID
*
* @param context A context for some random data
* @param oid The OID to update.
*/
void
_bson_context_set_oid_rand (bson_context_t *context, bson_oid_t *oid);

/**
* @brief Insert the context's sequence counter into the given OID. Increments
* the context's sequence counter.
*
* @param context The context with the counter to get+update
* @param oid The OID to modify
*/
void
_bson_context_set_oid_seq32 (bson_context_t *context, bson_oid_t *oid);

/**
* @brief Write a 64-bit counter from the given context into the OID. Increments
* the context's sequence counter.
*
* @param context The context with the counter to get+update
* @param oid The OID to modify
*
* @note Only used by the deprecated @ref bson_oid_init_sequence
*/
void
_bson_context_set_oid_seq64 (bson_context_t *context, bson_oid_t *oid);


BSON_END_DECLS

Expand Down
Loading