1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-02-04 01:47:02 +03:00

dict: Move local RNG state to global state

Don't use TLS variables directly.
This commit is contained in:
Nick Wellnhofer 2023-12-24 15:33:12 +01:00
parent 2e9e758d1e
commit 9c2c87b55d
5 changed files with 48 additions and 19 deletions

27
dict.c
View File

@ -35,6 +35,7 @@
#endif
#include "private/dict.h"
#include "private/globals.h"
#include "private/threads.h"
#include <libxml/parser.h>
@ -919,11 +920,6 @@ static xmlMutex xmlRngMutex;
static unsigned globalRngState[2];
#ifdef XML_THREAD_LOCAL
static XML_THREAD_LOCAL int localRngInitialized = 0;
static XML_THREAD_LOCAL unsigned localRngState[2];
#endif
ATTRIBUTE_NO_SANITIZE_INTEGER
void
xmlInitRandom(void) {
@ -985,18 +981,7 @@ xoroshiro64ss(unsigned *s) {
}
unsigned
xmlRandom(void) {
#ifdef XML_THREAD_LOCAL
if (!localRngInitialized) {
xmlMutexLock(&xmlRngMutex);
localRngState[0] = xoroshiro64ss(globalRngState);
localRngState[1] = xoroshiro64ss(globalRngState);
localRngInitialized = 1;
xmlMutexUnlock(&xmlRngMutex);
}
return(xoroshiro64ss(localRngState));
#else
xmlGlobalRandom(void) {
unsigned ret;
xmlMutexLock(&xmlRngMutex);
@ -1004,6 +989,14 @@ xmlRandom(void) {
xmlMutexUnlock(&xmlRngMutex);
return(ret);
}
unsigned
xmlRandom(void) {
#ifdef LIBXML_THREAD_ENABLED
return(xoroshiro64ss(xmlGetLocalRngState()));
#else
return(xmlGlobalRandom());
#endif
}

View File

@ -26,6 +26,7 @@
#include <libxml/SAX.h>
#include <libxml/SAX2.h>
#include "private/dict.h"
#include "private/error.h"
#include "private/globals.h"
#include "private/threads.h"
@ -75,6 +76,10 @@ struct _xmlGlobalState {
void *waitHandle;
#endif
#ifdef LIBXML_THREAD_ENABLED
unsigned localRngState[2];
#endif
#define XML_OP XML_DECLARE_MEMBER
XML_GLOBALS_ALLOC
XML_GLOBALS_ERROR
@ -162,6 +167,10 @@ xmlFreeGlobalState(void *state);
* *
************************************************************************/
#ifdef LIBXML_THREAD_ENABLED
static unsigned xmlMainThreadRngState[2];
#endif
/*
* Memory allocation routines
*/
@ -593,6 +602,11 @@ void xmlInitGlobalsInternal(void) {
#endif
mainthread = GetCurrentThreadId();
#endif
#ifdef LIBXML_THREAD_ENABLED
xmlMainThreadRngState[0] = xmlGlobalRandom();
xmlMainThreadRngState[1] = xmlGlobalRandom();
#endif
}
/**
@ -753,6 +767,11 @@ static void
xmlInitGlobalState(xmlGlobalStatePtr gs) {
xmlMutexLock(&xmlThrDefMutex);
#ifdef LIBXML_THREAD_ENABLED
gs->localRngState[0] = xmlGlobalRandom();
gs->localRngState[1] = xmlGlobalRandom();
#endif
gs->gs_xmlBufferAllocScheme = xmlBufferAllocSchemeThrDef;
gs->gs_xmlDefaultBufferSize = xmlDefaultBufferSizeThrDef;
gs->gs_xmlDoValidityCheckingDefaultValue =
@ -893,6 +912,16 @@ XML_GLOBALS_PARSER
XML_GLOBALS_TREE
#undef XML_OP
#ifdef LIBXML_THREAD_ENABLED
unsigned *
xmlGetLocalRngState(void) {
if (IS_MAIN_THREAD)
return(xmlMainThreadRngState);
else
return(xmlGetThreadLocalStorage(0)->localRngState);
}
#endif
/* For backward compatibility */
const char *const *

View File

@ -67,6 +67,8 @@ xmlInitRandom(void);
XML_HIDDEN void
xmlCleanupRandom(void);
XML_HIDDEN unsigned
xmlGlobalRandom(void);
XML_HIDDEN unsigned
xmlRandom(void);
#endif /* XML_DICT_H_PRIVATE__ */

View File

@ -6,4 +6,9 @@ xmlInitGlobalsInternal(void);
XML_HIDDEN void
xmlCleanupGlobalsInternal(void);
#ifdef LIBXML_THREAD_ENABLED
XML_HIDDEN unsigned *
xmlGetLocalRngState(void);
#endif
#endif /* XML_GLOBALS_H_PRIVATE__ */

View File

@ -583,9 +583,9 @@ xmlInitParser(void) {
atexit(xmlCleanupParser);
#endif
xmlInitMemoryInternal(); /* Should come second */
xmlInitRandom(); /* Required by xmlInitGlobalsInternal */
xmlInitMemoryInternal();
xmlInitGlobalsInternal();
xmlInitRandom();
xmlInitDictInternal();
xmlInitEncodingInternal();
#if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)