Skip to content

memdebug: avoid macro overrides for low-level allocator functions #16746

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

Closed
wants to merge 4 commits into from

Conversation

vszakats
Copy link
Member

@vszakats vszakats commented Mar 17, 2025

To make it resistent to a memdebug.h included before compiling it in
CURLDEBUG mode, and making the MEMDEBUG_NODEFINES hack unnecessary.
Also bringing it a step closer for inclusion in unity builds.


@vszakats vszakats marked this pull request as draft March 17, 2025 10:50
@github-actions github-actions bot added the tests label Mar 17, 2025
@vszakats vszakats changed the title memdebug: avoid macro overrides for standard functions memdebug: avoid macro overrides for low-level allocator functions Mar 17, 2025
@vszakats vszakats marked this pull request as ready for review March 19, 2025 01:16
#else
# define sclose(x) close((x))
# define CURL_SCLOSE(x) close((x))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this? Are you actually having something else that collides with sclose?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but sclose is easy to mistook for a system function.

But most importantly: There are two things going on here:

  • the platform-dependent mapping here above.
  • the memdebug mapping in memdebug/curl_memory.

Without a stable internal macro keeping the platform-dependent
mapping around, this is lost when undefining and redefining later.

Such interim macro may be dropped once we avoided temporarily
redefining things for memdebug, but this may be a few steps
down the road. It's hard (for me) to see it at this point.

A curl-specific name is IMO nicer here regardless to match with
other SOCK-things and avoid mistaking it for a system function.

It has only a dozen callers in libcurl and test code. With the rest in
test server code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but sclose is easy to mistook for a system function.

I'm certainly biased on this topic since I introduced this name. But it is sort of the point that it is similar to close but not identical. I can't recall when this has ever been a problem?

I suppose I don't understand what problem you're fixing here.

Copy link
Member Author

@vszakats vszakats Apr 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took a while for me to figure out this is a curl macro. But I don't much care about
the name (any name). The main issue is that the platform-specific definitions (from
curl_setup_once.h) may be lost depending on #include order.

The safe way is to make one macro with the platform defs, and map that to another
macro that can be undefined, redefined as needed. Once there is no longer any
undef / redef, it's still useful because curl_dbg_sclose() will always want to call
the "raw", platform-specific sclose from memdebug.c.

These names can be sclose and SCLOSE, but they need to be two names.

curl_setup_once.h:#  define sclose(x)  closesocket((x))
curl_setup_once.h:#  define sclose(x)  CloseSocket((x))
curl_setup_once.h:#  define sclose(x)  close_s((x))
curl_setup_once.h:#  define sclose(x)  lwip_close((x))
curl_setup_once.h:#  define sclose(x)  close((x))
curl_memory.h:#undef sclose
memdebug.h:/* sclose is probably already defined, redefine it! */
memdebug.h:#undef sclose
memdebug.h:#define sclose(sockfd) curl_dbg_sclose(sockfd,__LINE__,__FILE__)
memdebug.c:  int res = sclose(sockfd);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The (single) macro has existed like this for twenty-five years and aren't things building fine in CI? You say "they need to be two names" - I assume then that there's some breakage somewhere this fixes?

Copy link
Member Author

@vszakats vszakats Apr 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying it's broken, but fragile. Might break when moving around other things.

(it may well have gotten broken locally, that I had to touch it while prototyping these changes. I have no logs to point to, and there is just too many things going on to remember precisely. I'm quite sure it wasn't a l'art pour l'art change and there was a reason.)

vszakats added 4 commits April 5, 2025 23:39
To make it resistent to a `memdebug.h` included before compiling these
functions and bringing it one step closer for inclusion in unity builds.
to try lessen the confusion that `sclose` is all curl-specific,
and to untie the target-specific logic from the memdebug logic.
@bagder
Copy link
Member

bagder commented Apr 5, 2025

If the goal is to get the memdebug to work in unity builds, isn't my unit test split the easier route?

If we just make sure each unity bundle has the same memory situation, things are easier. No?

@vszakats
Copy link
Member Author

vszakats commented Apr 5, 2025

If the goal is to get the memdebug to work in unity builds, isn't my unit test split the easier route?

If we just make sure each unity bundle has the same memory situation, things are easier. No?

It seems so, yes.

I still think there is room for tidying up around these macro. These
aren't bug changes and may be worth it. We will better see them
once MALLOC/FREE is in place.

What would be real nice if all the memdebug.h and curl_memory.h stuff
could be moved to curl_setup.h and initialized there, once, for the whole
compilation units.

@bagder
Copy link
Member

bagder commented Apr 5, 2025

What would be real nice if all the memdebug.h and curl_memory.h stuff could be moved to curl_setup.h and initialized there, once, for the whole compilation units.

That can only work if each unity bundle has the same memory setup, but sure. It can probably get done.

@vszakats vszakats closed this in cde81e4 Jun 16, 2025
@vszakats vszakats deleted the memdebug-low branch June 16, 2025 07:37
denandz pushed a commit to denandz/curl that referenced this pull request Jun 21, 2025
Before this patch `memdebug.c` was compiled as a separate source in
unity builds. This was necessary because `memdebug.c` failed to compile
if `memdebug.h` was included before it, in `CURLDEBUG` mode. This patch
fixes this issue and allows to compile `memdebug.c` as part of the unity
source batch. This removes an exception and makes builds perform a notch
better.

- introduce `CURL_SCLOSE()` macro as an immutable synonym of `sclose()`.
- memdebug: replace `sclose()` reference with `CURL_SCLOSE()` to compile
  as expected when `sclose()` is overridden by `memdebug.h`.
- memdebug: make it not break when including `memdebug.h` before it in
  `CURLDEBUG` mode. Do this by calling low-level functions as
  `(function)`.
- autotools, cmake: drop memdebug exception, include it like any other
  source file. This is now possible because `memdebug.c` doesn't break
  if `memdebug.h` was included before it, in `CURLDEBUG` builds.
- mk-unity: drop `--exclude` option. No longer used after this patch.
- drop `MEMDEBUG_NODEFINES` macro hack. No longer necessary.

Ref: curl#16747
Closes curl#16746
Closes curl#16738
Closes curl#17631
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.

2 participants