Skip to content

build: stop overriding standard memory allocation functions - #19626

Closed
vszakats wants to merge 107 commits into
curl:masterfrom
vszakats:xmem
Closed

vszakats wants to merge 107 commits into
curl:masterfrom
vszakats:xmem

Conversation

@vszakats

@vszakats vszakats commented Nov 20, 2025 •

Copy link
Copy Markdown
Member

Before this patch curl used the C preprocessor to override standard
memory allocation symbols: malloc, calloc, strdup, realloc, free.
The goal of these is to replace them with curl's debug wrappers in
CURLDEBUG builds, another was to replace them with the wrappers
calling user-defined allocators in libcurl. This solution needed a bunch
of workarounds to avoid breaking external headers: it relied on include
order to do the overriding last. For "unity" builds it needed to reset
overrides before external includes. Also in test apps, which are always
built as single source files. It also needed the (symbol) trick
to avoid overrides in some places. This would still not fix cases where
the standard symbols were macros. It was also fragile and difficult
to figure out which was the actual function behind an alloc or free call
in a specific piece of code. This in turn caused bugs where the wrong
allocator was accidentally called.

To avoid these problems, this patch replaces this solution with
curlx_-prefixed allocator macros, and mapping them once to either
the libcurl wrappers, the debug wrappers or the standard ones, matching
the rest of the code in libtests.

This concludes the long journey to avoid redefining standard functions
in the curl codebase.

Note: I did not update packages/OS400/*.c sources. They did not
#include curl_setup.h, curl_memory.h or memdebug.h, meaning
the overrides were never applied to them. This may or may not have been
correct. For now I suppressed the direct use of standard allocators
via a local .checksrc. Probably they (except for curlcl.c) should be
updated to include curl_setup.h and use the curlx_ macros.

This patch changes mappings in two places:

  • lib/curl_threads.c in libtests: Before this patch it mapped to
    libcurl allocators. After, it maps to standard allocators, like
    the rest of libtests code.
  • units: before this patch it mapped to standard allocators. After, it
    maps to libcurl allocators.

Also:

  • drop all position-dependent curl_memory.h and memdebug.h includes,
    and delete the now unnecessary headers.
  • rename Curl_tcsdup macro to curlx_tcsdup and define like the other
    allocators.
  • map curlx_strdup() to _strdup() on Windows (was: strdup()).
    To fix warnings silenced via _CRT_NONSTDC_NO_DEPRECATE.
  • multibyte: map curlx_convert_*() to _strdup() on Windows
    (was: strdup()).
  • src: do not reuse the strdup name for the local replacement.
  • lib509: call _strdup() on Windows (was: strdup()).
  • test1132: delete test obsoleted by this patch.
  • CHECKSRC.md: update text for SNPRINTF.
  • checksrc: ban standard allocator symbols.

Follow-up to b12da22 #18866
Follow-up to db98daa #18844
Follow-up to 4deea93 #18814
Follow-up to 9678ff5 #18776
Follow-up to 10bac43 #18774
Follow-up to 20142f5 #18634
Follow-up to bf7375e #18503
Follow-up to 9863599 #18502
Follow-up to 3bb5e58 #17827


raw diff: https://patch-diff.githubusercontent.com/raw/curl/curl/pull/19626.diff

  • add new macros
  • search and replace bare allocators with curlx_ ones
  • map to _strdup for _WIN32
  • replace (fwrite) → fwrite in one place. (unrelated tidy-up)
  • verify if new mapping match the old overrides (DEFAULT/STANDARD, CURLALLOC, DEBUG)
  • add CURL_STANDARD_ALLOC macro and set it in src, libtests, tunit, unit to match existing builds.
  • src: rename local strdup implementation to tool_strdup to avoid tripping checksrc and make it clear it's a local implementation with an explicit mapping over Curl_strdup which is the same thing in libcurl.
  • packages/OS400/*.c: code does not include curl_setup.h, curl_memory.h or memdebug.h,
    meaning the overrides did not apply to it. This may have been incorrect.
    For now I just suppressed the issue via .checksrc.
  • drop overrides.
  • check if old overrides were doing what's expected (looking at tests/unit, tests/libtest non-debug-enabled, also to curl_threads.c when compiled into libtests)
  • try cleaning up the macros controlling the overrides? perhaps rely on BUILDING_LIBCURL more to enable overrides, and disable for test/units, the rest needs it disabled anyway.
  • de-dupe lib and src strdup local implementations into curlx. [→ FUTURE PR]
  • perhaps de-dupe lib and src memdup0 implementations into curlx. [→ FUTURE PR]
  • replace (func) style hacks with the real functions with a per-line checksrc exception
  • drop old headers (memdebug.h, curl_memory.h, curl_mem_undef.h) which should all be empty at this point
  • perhaps replace curl_free -> curlx_free, a 1-to-1 wrapper, not useful and hinders readability
    Tricky, they look the same, but their mappings may be different!
  • update documentation if/where necessary?
  • perhaps replace curlx_unicodefree() with free()? (according to comment its only purpose is to not be overridden, which can no longer happen after this patch, though perhaps the current name better tells when to use it exactly.)
  • move commits to sep PR for unrelated finds (see commit messages, e.g. (fwrite) change) → curl_setup.h: drop superfluous parenthesis from Curl_safefree macro #19734 memdebug: replace (fwrite) with fwrite #19735 unit1653: replace local macro with Curl_safefree() #19736
  • de-dupe CLONE_STRING and DUP macros [they are subtly different, skip]
  • revisit tests: robustify system symbol overrides #17865
    which should most like just need to be closed without action at this point.
  • figure out why libtests.E is 75 MB (from 253 files) and how to make it smaller. (I guess this breaks CodeQL)
    -DCMAKE_C_FLAGS=-DCURL_DISABLE_TYPECHECK was the solution. It fixed CodeQL builds.
  • revisit [WIP] GHA/codeql: try building libtests vszakats/curl#11 → GHA/codeql: add tweak to successfully build libtests for CodeQL #19632
    and the idea of trying to build tests in non-unity-style,
    which may be easier now there is no need to carefully
    watch the order of includes.

@vszakats
vszakats marked this pull request as draft November 20, 2025 13:19
@vszakats

Copy link
Copy Markdown
Member Author

Thinking it's time to delete the OpenBSD CI job. 7.8 made tests flaky, had to revert to 7.7, now 7.7 also broke at package install.

@vszakats

vszakats commented Nov 20, 2025 •

Copy link
Copy Markdown
Member Author

Snags, some may be big:

  1. seems in some build cases the mapping may differ per file within the same target (curlx, see BUILDING_LIBCURL).
    It breaks the idea to setup mappings once for each source. This trick was possible before, and since it was possible it also got used in practice. I also remember causing head-scratching earlier.
    Solution: ?

    • split curlx stuff to their separate static lib
    • have a pubic curl_malloc()/realloc() to complement public curl_free() and use that from curlx code.
    • option to tear down and restore mappings for specific code sections → trying this method
  2. libtests: before this PR the lib/curl_threads.c source was built with Curl_* allocator, while the rest with the system one. This PR at the moment compiles everything with the standard one.

@vszakats vszakats mentioned this pull request Nov 21, 2025
3 tasks done
@vszakats vszakats changed the title [WIP] build: stop overriding system allocation functions [WIP] build: stop overriding standard allocation functions Nov 21, 2025
@bagder

bagder commented Nov 21, 2025 •

Copy link
Copy Markdown
Member

split curlx stuff to their separate static lib

How does that change anything? The sources are already built separately, right?

have a pubic curl_malloc()/realloc() to complement public curl_free() and use that from curlx code.

That sounds like a bad idea and counter the whole premise of the curlx functions.

option to tear down and restore mappings for specific code sections → trying this method

I'm not sure I understand this. What sections need different setups?

  1. libtests: before this PR the lib/curl_threads.c source was built with Curl_* allocator, while the rest with the system one. This PR at the moment compiles everything with the standard one.

libtests should use the standard one since they are libcurl-using applications. Right?

@vszakats
vszakats marked this pull request as ready for review November 27, 2025 02:26
@vszakats vszakats changed the title [WIP] build: stop overriding standard allocation functions build: stop overriding standard allocation functions Nov 27, 2025
@vszakats

Copy link
Copy Markdown
Member Author

I plan to merge this preferably today to avoid merge conflicts with
master, and then explore if the macros can be simplified a bit. (it's
already fine, but I hunch some room to improve it.)

@vszakats
vszakats force-pushed the xmem branch 2 times, most recently from 61df11c to 15fd09e Compare November 27, 2025 18:03
vszakats added a commit to vszakats/curl that referenced this pull request Nov 27, 2025
vszakats added a commit that referenced this pull request Nov 27, 2025
vszakats added a commit to vszakats/curl that referenced this pull request Nov 27, 2025
vszakats added a commit that referenced this pull request Nov 27, 2025
vszakats added a commit that referenced this pull request Nov 27, 2025
@vszakats

vszakats commented Nov 27, 2025 •

Copy link
Copy Markdown
Member Author

My reply from a week ago, where instead of replying to, I accidentally
selected edit and edited my replies into @bagder's post. (sorry!)

   * split curlx stuff to their separate static lib

How does that change anything? The sources are already built separately, right?

My basis was that mappings are set once per lib.
By splitting into two libs, the two can have different mappings.
But, this would complicate things so much that it's not worth doing just for this.

   * have a pubic curl_malloc()/realloc() to complement public curl_free() and use that from curlx code.

That sounds like a bad idea and counter the whole premise of the curlx functions.

Just a brainstorm idea, and probably not a good one!

   * option to tear down and restore mappings for specific code sections → trying this method

I'm not sure I understand this. What sections need different setups?

The issue is that curlx/base64 and curlx/dynbuf both have a local
hack that changes the global mapping default when !BUILDING_LIBCURL.
This could be done with relative ease with the override system.

With the new system, I had to figure out a way to implement these
exceptions. I'm still not at the end of this, so hard to say if's all
settled, but the solution I went with seems light and easy: Unmap
temporarily with an extra macro, and remap to the original at the
end of the file.

2. libtests: before this PR the lib/curl_threads.c source was built with Curl_* allocator, while the rest with the system one. This PR at the moment compiles everything with the standard one.

libtests should use the standard one since they are libcurl-using applications. Right?

It was my assumption, yes, based on how it worked so far. This exception
(that I noticed while diffing .E outputs) raised the flag and some doubts.
With the new system, curl_threads is also using standard ones. I need
to reproduce and figure out locally the red CI issues before returning to
this one, and see if the old behavior is required or just coincidence.

@vszakats

vszakats commented Nov 27, 2025 •

Copy link
Copy Markdown
Member Author

The PR passed after fixing what turned out to be a prosaic issue.

With this done, mappings before this patch remained, with one
exception in libtests' curl_thread.c which were previously
mapped to libcurl allocators, while now they are using standard
ones, like the rest of libtests. This looks like a fix, and the previous
mappings were wrong (though undetected in CI.)
This iteration also needed a trick to keep using libcurl allocator in
some targets for curlx base64 and dynbuf, to match previous
behavior.

Then I had a second look and made a simpification, which also made
the trick above unnecessary, kept all mappings, except switching units
to using libcurl allocators. All tests still pass.

TL;DR at the current state, this patch selects libcurl allocators
whenever BUILDING_LIBCURL is set. This allows dropping tricks,
makes units use libcurl allocators, while they were previously using
standard ones. And also fixes curl_threads.c to use standard allocators
in libtests. The rest is unchanged compared to before this patch.

edit: GitHub's JS-laden web UI is no longer capable of displaying a diff
of this size (+1,456 −2,785...), here's the raw diff that still works:
https://patch-diff.githubusercontent.com/raw/curl/curl/pull/19626.diff

@vszakats vszakats changed the title build: stop overriding standard allocation functions build: stop overriding standard memory allocation functions Nov 28, 2025
@vszakats vszakats closed this in 193cb00 Nov 28, 2025
@vszakats
vszakats deleted the xmem branch November 28, 2025 09:46
@bagder

bagder commented Nov 28, 2025

Copy link
Copy Markdown
Member

@vszakats there are 7 malloc() calls left in lib/. I presume on purpose, but it is not clear why?

@bagder

bagder commented Nov 28, 2025

Copy link
Copy Markdown
Member

Also, these mallocs are not covered by the memdebug functions?

@yedayak

yedayak commented Nov 28, 2025

Copy link
Copy Markdown
Contributor

The comment on top explains why:

curl/lib/curlx/fopen.c

Lines 25 to 28 in 193cb00

/*
* Use system allocators to avoid infinite recursion when called by curl's
* memory tracker memdebug functions.
*/

vszakats pushed a commit that referenced this pull request Nov 28, 2025
@bagder

bagder commented Nov 28, 2025

Copy link
Copy Markdown
Member

I saw that but what makes these so special? Why do they cause recursion? It's not like they are mallocs calling malloc.

@vszakats

vszakats commented Nov 28, 2025 •

Copy link
Copy Markdown
Member Author

These were raw allocators all along. They are used in low-level Windows-specific wrappers
which wrappers are used from memdebug-wrappers. To avoid infinite recursion, while also
making sure the debug wrappers do not change behavior of fopen/open/etc, they need to
avoid calling memdebug stuff once again.

I think they are fine. They also caused many weeks of pain in the past till figuring this stuff
out (edit: for unity build).

@vszakats vszakats mentioned this pull request Feb 2, 2026
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants