1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-01-25 06:03:34 +03:00

120 Commits

Author SHA1 Message Date
Nick Wellnhofer
1f7d4af355 globals: Clean up macros and add comments 2024-07-16 19:58:09 +02:00
Nick Wellnhofer
4f08a1a249 globals: Also use thread-specific storage on "main" thread
Don't treat "main" thread specially. This simplifies access to
thread-specific data.

xmlGetGlobalState can now also fail on the former main thread, leading
to an unrecoverable condition if malloc fails.

The globals were never defined in public header files when compiling
with thread support. Now they're only defined in a legacy build.

Move TlsFree to DllMain to make cleanup more robust on Windows.

Obsoletes #1.
2024-07-16 19:48:40 +02:00
Nick Wellnhofer
a87944e970 windows: Use DllMain for cleanup 2024-07-16 17:42:10 +02:00
Nick Wellnhofer
5f3f66c683 threads: Use pthread_once and InitOnceExecuteOnce
Static initialization flags aren't thread-safe. Also avoids an
allocation on Windows.

TODO: Breaks xmllint on Windows.
2024-07-16 17:42:10 +02:00
Nick Wellnhofer
79e119954c error: Make xmlLastError const 2024-07-16 17:42:10 +02:00
Nick Wellnhofer
8e871a31f8 buf: Rework xmlBuffer code
Port most changes made to the xmlBuf code in f3807d76, except that
"size" still includes the terminating NULL byte.

Make xmlSetBufferAllocationScheme, xmlBufferAllocScheme and
xmlDefaultBufferSize no-ops.

Deprecate a few functions.
2024-07-16 17:42:10 +02:00
Nick Wellnhofer
728869809e error: Add helper functions to print errors and abort 2024-07-15 16:33:38 +02:00
Nick Wellnhofer
6794c1b91d globals: Document remaining thread-local vars as deprecated
See #407.
2024-07-02 20:03:23 +02:00
Nick Wellnhofer
35146ff31c save: Implement xmlSaveSetIndentString
Allow to set indent string without using global xmlTreeIndentString.

See #736.
2024-07-02 20:03:23 +02:00
Nick Wellnhofer
7cc619d568 save: Implement save options for indenting
Implement XML_SAVE_NO_INDENT to disable and XML_SAVE_INDENT to enable
indenting regardless of the global xmlIndentTreeOutput.

Implement XML_SAVE_EMPTY to enable empty tags regardless of the global
xmlSaveNoEmptyTags.

See #736.
2024-07-02 20:03:23 +02:00
Nick Wellnhofer
f505dcaea0 tree: Remove underscores from xmlRegisterCallbacks 2024-06-27 14:45:35 +02:00
Nick Wellnhofer
2b0c4abb1f threads: Remove pthread weak symbol hack
On Linux, we tried to detect the presence of libpthread to disable
things like locks. This questionable hack doesn't work since glibc 2.34
which merged libpthread into libc.
2024-06-17 00:54:47 +02:00
Nick Wellnhofer
e75e878e02 doc: Update and fix documentation 2024-05-20 14:23:39 +02:00
Nick Wellnhofer
1cdfece12b memory: Remove memory debugging
This is useless compared to sanitizers or valgrind and has a
considerable performance impact if enabled accidentally.
2024-04-28 20:42:55 +02:00
Nick Wellnhofer
12f0bb9478 parser: Synchronize more options 2024-01-05 20:39:40 +01:00
Nick Wellnhofer
9c2c87b55d dict: Move local RNG state to global state
Don't use TLS variables directly.
2023-12-24 16:24:34 +01:00
Nick Wellnhofer
54c70ed57f parser: Improve error handling
Introduce xmlCtxtSetErrorHandler allowing to set a structured error for
a parser context. There already was the "serror" SAX handler but this
always receives the parser context as argument.

Start to use xmlRaiseMemoryError.

Remove useless arguments from memory error functions. Rename
xmlErrMemory to xmlCtxtErrMemory.

Remove a few calls to xmlGenericError.

Remove support for runtime entity debugging.
2023-12-21 02:46:27 +01:00
Nick Wellnhofer
9122ad0ce6 include: Move globals from xmlsave.h to parser.h
Fix downstream build issues after reworking globals.h.
2023-12-07 12:31:06 +01:00
Nick Wellnhofer
c011e7605d globals: Remove unused globals from thread storage
Setting these deprecated globals hasn't had an effect for a long time.
Make them constants. This reduces the size of per-thread storage from
~700 to ~250 bytes.
2023-12-06 20:07:54 +01:00
Nick Wellnhofer
4c17804050 globals: Disable TLS in static Windows builds
The cleanup callback would run after TLS was deallocated.
2023-12-01 17:19:55 +01:00
Mike Dalessio
62d318f86c
fix: more pthread weak references in globals.c 2023-11-18 15:37:26 -05:00
Mike Dalessio
1ac88300c1 fix: pthread weak references in globals.c
Linking executables will fail on systems with glibc < 2.34 without
declaring these symbols as weak references.

In commit c19771c1f13de9196f98260d142d8c8672eb5733 these references
were moved to globals.c from threads.c, but the `#pragma weak`
declarations were lost in the process.

Also removing unneeded weak declarations from threads.c.
2023-11-18 18:26:48 +00:00
Nick Wellnhofer
253f260bb1 threads: Fix --with-thread-alloc
Fixes #606.
2023-10-18 20:07:04 +02:00
Nick Wellnhofer
b8961a75e9 parser: Fix reinitialization 2023-09-27 17:24:46 +02:00
Nick Wellnhofer
bc4e82ff42 globals: Don't use thread-local storage on Darwin
It seems that thread-local storage destructors are run before pthread
thread-specific data destructors on Darwin, defeating our scheme to use
TSD to clean up TLS.

Here's an example program that reports a use-after-free when compiled
with `-fsanitize=address` on macOS:

    #include <pthread.h>

    typedef struct {
	int v;
    } my_struct;

    static _Thread_local my_struct tls;
    pthread_key_t key;

    void dtor(void *tsd) {
	my_struct *s = (my_struct *) tsd;
	/*
	 * This will crash ASan, apparently because
	 * TLS has already been freed.
	 */
	s->v = 1;
    }

    void *thread(void *p) {
	pthread_setspecific(key, &tls);
	return NULL;
    }

    int main(void) {
	pthread_key_create(&key, dtor);

	pthread_t handle;
	pthread_create(&handle, NULL, thread, NULL);
	pthread_join(handle, NULL);

	return 0;
    }
2023-09-22 13:37:28 +02:00
Nick Wellnhofer
8c084ebdc7 doc: Make apibuild.py happy 2023-09-21 22:57:33 +02:00
Nick Wellnhofer
05135536b1 globals: Fix build --with-threads --without-output
Fixes #593.
2023-09-21 20:40:32 +02:00
Nick Wellnhofer
f0e8358eae globals: Final fixes 2023-09-20 23:18:21 +02:00
Nick Wellnhofer
11a1839ddd globals: Move remaining globals back to correct header files
This undoes a lot of damage.
2023-09-20 22:06:49 +02:00
Nick Wellnhofer
eb985d6f8e globals: Move error globals back to xmlerror.c 2023-09-20 22:06:49 +02:00
Nick Wellnhofer
d1336fd393 globals: Move malloc hooks back to xmlmemory.h 2023-09-20 22:06:49 +02:00
Nick Wellnhofer
a77f9ab84c globals: Don't include SAX2.h from globals.h 2023-09-20 22:06:49 +02:00
Nick Wellnhofer
2e6c49a74d globals: Don't store xmlParserVersion in global state
This is a constant.
2023-09-20 22:06:49 +02:00
Nick Wellnhofer
ea29b95144 globals: Abort if lazy allocation of global state failed
There's really nothing we can do in this situation, so it's better to
abort with an error message.
2023-09-20 22:06:49 +02:00
Nick Wellnhofer
dc3382ef97 globals: Move xmlRegisterNodeDefault to tree.c
Code in globals.c must not try to access globals itself since the
accessor macros aren't defined and we would only see the main
variable.
2023-09-20 22:06:49 +02:00
Nick Wellnhofer
759767423a globals: Add a few comments 2023-09-20 22:06:49 +02:00
Nick Wellnhofer
f7a403c21f globals: Move xmlIsMainThread to globals.c
xmlIsMainThread is mainly needed for global variables.
2023-09-20 22:06:49 +02:00
Nick Wellnhofer
b173b724d1 globals: Use thread-local storage if available
Also use thread-local storage to store globals on POSIX platforms.

Most importantly, this makes sure that global variable access can't fail
when allocating the global state struct.
2023-09-20 22:06:49 +02:00
Nick Wellnhofer
e7b6ca156f globals: Rework global state destruction on Windows
If DllMain is used, rely on it working as expected. The old code seemed
to attempt to free global state of other threads if, for some reason,
the DllMain mechanism didn't work.

In a static build, register a destructor with
RegisterWaitForSingleObject.

Make public functions xmlGetGlobalState and xmlInitializeGlobalState
no-ops.

Move initialization and registration of global state objects to
xmlInitGlobalState. Lookup global state with xmlGetThreadLocalStorage
which can be inlined nicely.

Also cleanup global state when using TLS. xmlLastError must be reset.
2023-09-20 22:06:49 +02:00
Nick Wellnhofer
39a275a541 globals: Define globals using macros
Declare and define globals and helper functions by (ab)using the
preprocessor.
2023-09-20 22:06:49 +02:00
Nick Wellnhofer
bf6bd16154 globals: Introduce xmlCheckThreadLocalStorage
Checks whether (emulated) thread-local storage could be allocated.
2023-09-20 22:06:43 +02:00
Nick Wellnhofer
89f4976728 globals: Make xmlGlobalState private
This removes a public struct but it seems impossible to use its members
in a sensible way from external code.
2023-09-19 17:36:29 +02:00
Nick Wellnhofer
4e1c13ebfd debug: Remove debugging code
This is barely useful these days and only clutters the code base.
2023-09-19 17:35:09 +02:00
Nick Wellnhofer
c19771c1f1 globals: Move code from threads.c to globals.c
Move all code that handles globals to the place where it belongs.
2023-09-19 17:34:38 +02:00
Nick Wellnhofer
2a4b811424 globals: Rename members of xmlGlobalState
This is a deliberate first step to remove some internals from the
public API and to avoid issues when redefining tokens.
2023-09-19 17:34:30 +02:00
Nick Wellnhofer
65d381f32c threads: Allocate mutexes statically 2022-11-25 15:12:56 +01:00
Nick Wellnhofer
9dbf137455 parser: Make some module init/cleanup functions private 2022-11-25 15:02:04 +01:00
Nick Wellnhofer
cecd364dd2 parser: Don't call *DefaultSAXHandlerInit from xmlInitParser
Change the default handler definitions to match the result after calling
the initialization functions.

This makes sure that no thread-local variables are accessed when calling
xmlInitParser.
2022-11-25 15:02:04 +01:00
Nick Wellnhofer
ad338ca737 Remove explicit integer casts
Remove explicit integer casts as final operation

- in assignments
- when passing arguments
- when returning values

Remove casts

- to the same type
- from certain range-bound values

The main motivation is that these explicit casts don't change the result
of operations and only render UBSan's implicit-conversion checks
useless. Removing these casts allows UBSan to detect cases where
truncation or sign-changes occur unexpectedly.

Document some explicit casts as truncating and add a few missing ones.
2022-09-01 02:33:57 +02:00
Nick Wellnhofer
0f568c0b73 Consolidate private header files
Private functions were previously declared

- in header files in the root directory
- in public headers guarded with IN_LIBXML
- in libxml.h
- redundantly in source files that used them.

Consolidate all private header files in include/private.
2022-08-26 02:11:56 +02:00