{"author":"Viktor Szakats","author_email":"commit@vsz.me","author_time":1751706864,"commit_time":1753717504,"committer":"Viktor Szakats","committer_email":"commit@vsz.me","hash":"3bb5e58c105d7be450b667858d1b8e7ae3ded555","message":"memory: make function overrides work reliably in unity builds\n\nFixing:\n- HTTPS-RR builds with c-ares and Linux MUSL.\n- curl-for-win minimal builds with Linux MUSL.\n\nIt should fix all other kinds of entaglement between curl's redefintions\nof system symbols and system (or 3rd-party) headers sensitive to that.\n\nIt also syncs memory override behavior between unity & non-unity builds,\nthus reducing build variations.\n\nThe idea is to define and declare everything once in `curl_setup.h`,\nwithout overriding any system symbols with curl ones yet. Then, like\nbefore this patch, override them, if necessary, in each source file via\n`curl_memory.h` and `memdebug.h`, after including system headers.\nTo ensure a clean slate with no overrides at the beginning of each\nsource file, reset all of them unconditionally at the end of\n`curl_setup.h`, by including `curl_mem_undef.h`. (This assumes\n`curl_setup.h` is always included first, which is already the case\nthroughout the codebase.)\n\n`curl_mem_undef.h` can also be included explicitly wherever overrides\nare causing problems. E.g. in tests which use unity-style builds and\na previously included `curl_memory.h`/`memdebug.h` can be spilling into\nother source files.\n\nThe simplified role of the two override headers:\n- `curl_memory.h`: overrides system memory allocator functions to\n  libcurl ones, when memory tracing (aka `CURLDEBUG`) is disabled.\n- `memdebug.h`: overrides system memory allocator and some other\n  functions to curl debug functions, when memory tracing is enabled.\n\nChanged made in this patch, step-by-step:\n- curl_memory.h: move allocator typedefs and protos to `curl_setup.h`.\n- memdebug.h: move `ALLOC_*` macros to `curl_setup.h`.\n- memdebug.h: move allocator protos to `curl_setup.h`.\n- memdebug.h: move `Curl_safefree()` macro to `curl_setup.h`.\n  (it's a regular macro, with a one-time, global, definition.)\n- curl_memory.h: move system symbol undefs to a new, separate header:\n  `curl_mem_undef.h`.\n- curl_setup.h: include `curl_mem_undef.h` at the end, unconditionally,\n  to reset system symbol macros after each inclusion.\n- handle `sclose()` and `fake_sclose()` in `curl_setup.h`. They are not\n  system symbols, a one-time definition does the job.\n\nAlso:\n- GHA/linux: enable unity mode for the HTTP-RR c-ares MUSL job.\n  Follow-up to 17ab4d62e62bd3bfb6dbf6cb51460629f21c03e7 #16413\n\nThat said, I'd still find it better to avoid redefining system macros.\nTo communicate clearly the fact that they are not the original system\ncalls and they do behave differently. And, it would allow dropping the\nundef/redef dance in each source file, and maintaining the logic with\nit. The \"last #include files should be in this order\" comments in each\nsource would also become unnecessary. Also the trick of using\n`(func)` (or interim macros) to call the non-overridden function where\nrequired. This method works for printf and most everything else already.\nFor `_tcsdup`, socket and fopen functions this could work without\ndisturbing the codebase much.\n\nRef: #16428 (clean reboot of)\n\nCloses #17827\n","parents":["113f6aacd4ae4eaa25fca91932013425fca3788d"],"tree_hash":"0a26cdee0db5747ff1b6af14613f3d7812348553"}