Skip to content

Guard mallopt calls when targeting API < 26 - #2343

Merged
dmah42 merged 3 commits into
google:mainfrom
oontvoo:fix_mallopt
Oct 21, 2026
Merged

dmah42 merged 3 commits into
google:mainfrom
oontvoo:fix_mallopt

Conversation

@oontvoo

@oontvoo oontvoo commented Oct 20, 2026

Copy link
Copy Markdown
Member

Details:

mallopt() was introduced in Android Bionic in API 26.
When targeting minSdkVersion < 26:

  • Without -D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__, <malloc.h> hides the mallopt declaration entirely, failing to compile.
  • With -D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__, mallopt is emitted as a weak symbol that resolves to nullptr on devices/emulators running API < 26. In user-mode QEMU/chroot environments where /dev/__properties__ is unavailable, compiler-rt's __builtin_available falls back to __ANDROID_API_FUTURE__ (10000) and returns true, causing a null function pointer call at startup.

Declare mallopt as a weak symbol and check &mallopt != nullptr before calling it.

Details:
`mallopt()` was introduced in Android Bionic in API 26. When targeting `minSdkVersion < 26`:
-  Without `-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__`, `<malloc.h>` hides the `mallopt` declaration entirely, failing to compile.
-  With `-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__`, `mallopt` is emitted as a weak symbol that resolves to `nullptr` on devices/emulators running API < 26. In user-mode QEMU/chroot environments where `/dev/__properties__` is unavailable, compiler-rt's `__builtin_available` falls back to `__ANDROID_API_FUTURE__` (`10000`) and returns `true`, causing a null function pointer call at startup.

Declare `mallopt` as a weak symbol and check `&mallopt != nullptr` before calling it.
@oontvoo

oontvoo commented Oct 20, 2026

Copy link
Copy Markdown
Member Author

@dmah42

Comment thread src/benchmark.cc

#ifdef __ANDROID_NDK__
#include <malloc.h>
extern "C" __attribute__((weak)) int mallopt(int, int);

@LebedevRI LebedevRI Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this kind of symbol re-declaration even allowed in C++?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is not a definition - it's just a weak declaration

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That is what i'm asking, yes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it is. the attribute is an extension so not covered by the standard but this sort of redclaration is fine. it must match the original declaration exactly (does mallopt have noexcept for eg?).

dmah42
dmah42 previously approved these changes Oct 20, 2026
Comment thread src/benchmark.cc Outdated
// Not asserting on the return value, as sanitizers interpose the allocator
// and always return 0
(void)mallopt(M_BIONIC_ZERO_INIT, 0);
if (&mallopt != nullptr) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this might always evaluate true which could trigger compiler warnings/errors. we don't build android on CI so maybe confirm this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why do you think this would trigger a warning? 🤔
This pattern of checking if a weak-symbol is defined is very common everywhere.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

well in the case that mallopt is defined, this is trivially always true. in the case that it's not defined it's trivially always false. we have a warning in clang on conditionals that are always trivially true or false.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Gotcha! I've removed the nullptr checks and added the condition on the weak-decl instead.

@dmah42
dmah42 merged commit ab7405e into google:main Oct 21, 2026
155 of 156 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants