GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Detect if calloc calls malloc by apangin · Pull Request #1500 · async-profiler/async-profiler

Conversation

@apangin

@apangin apangin commented Sep 21, 2025

Copy link
Copy Markdown
Collaborator

Description

Use temporary malloc hook to detect if calloc implementation calls malloc under the hood. This is required for proper nativemem profiling.

Related issues

#1466

Motivation and context

Various malloc implementations may or may not do cross-calls. For example, default calloc in musl libc calls malloc internally. Async-profiler handles this case specially to prevent double-counting of native memory alocations. Previously, it simply checked OS::isMusl, but this was inaccurate, since musl libc could be built with different malloc libraries. For example, in Alpaquita Linux, musl is linked with mimalloc, where calloc does not calls malloc.

How has this been tested?

nativemem tests now pass on Alpaquita Linux as well as on vanilla Alpine.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@apangin
Detect if calloc calls malloc
f3d26cc
@Baraa-Hasheesh

Baraa-Hasheesh commented Sep 21, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

While thinking about this issue, the solution that made most sense to me was something along the lines of what is implented here

However I have some doubts
It's assumeed that if calloc calls malloc, then the posix_memalign would also call aligned_alloc
I think it's better to check for that separately, as user might change calloc via LD_PRELOAD but keep posix_memalign the same

On a similar note, do we want to support other patterns the user might introduce via an LD_PRELOAD? (For example a user might change malloc to call calloc)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apangin

apangin commented Sep 22, 2025

Copy link
Copy Markdown
Collaborator Author

@Baraa-Hasheesh Checking only one type of relation (calloc calling malloc) was a conscious choice. This relation is a distinctive characteristic of musl's own memory allocator. Other popular allocators (glibc's malloc, jemalloc, tcmalloc, mimalloc) do not do cross-calls via PLT.

Checking relation between posix_memalign and aligned_alloc would require writing more code in vain (with no effect for the above allocators). At the same time, it would not be a complete solution for a custom user-provided allocator anyway.

Support for custom allocators that hook arbitrary malloc functions is out of scope of nativemem feature. Doing LD_PRELOAD hacks you've mentioned while running nativemem profiling sounds extraordinary. I think it is unreasonable to build a complicated solution for a theoretical problem that noone have probably seen.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krk
krk reviewed Sep 22, 2025
View reviewed changes
Comment thread src/mallocTracer.cpp Outdated
static bool _recursive_malloc = false;

// Test if calloc() implementation calls malloc()
static void* recursive_malloc_hook(size_t size) {

@krk krk Sep 22, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Not sure if "recursive" is the right naming, as function is not calling itself.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@apangin apangin Sep 22, 2025

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yeah, I had doubts about that too, but decided it's still recursive in the sense the malloc algorithm (as the whole) calls to itself via PLT. Do you have a suggestion how to name it better?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@apangin apangin Sep 22, 2025

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Renamed to nested

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@krk krk Sep 23, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

"Nested" sounds good, as we are calling all malloc-family functions malloc here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@apangin
Recursive -> nested
1aee858
krk
krk approved these changes Sep 23, 2025
View reviewed changes
@apangin
apangin merged commit d97a7d3 into master Sep 23, 2025
@apangin
apangin deleted the recursive-malloc branch September 23, 2025 21:54
jbachorik pushed a commit to DataDog/async-profiler that referenced this pull request Jan 2, 2026
@apangin @jbachorik
Detect if calloc calls malloc (async-profiler#1500)
795a83c
Fixes `nativemem` profiling on Alpaquita Linux
jbachorik pushed a commit to DataDog/async-profiler that referenced this pull request Jan 6, 2026
@apangin @jbachorik
Detect if calloc calls malloc (async-profiler#1500)
06c99a8
Fixes `nativemem` profiling on Alpaquita Linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@krk krk krk approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@apangin @Baraa-Hasheesh @krk
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.