toku-mac commented on code in PR #18883:
URL: https://github.com/apache/nuttx/pull/18883#discussion_r3252713055
##########
arch/sim/src/sim/posix/sim_macho_init.c:
##########
@@ -96,23 +99,28 @@ static void save_and_replace_init_funcs(int argc, const
char *argv[],
g_saved_envp = envp;
g_saved_apple = apple;
- g_saved_init_funcs = malloc(g_num_saved_init_funcs *
- sizeof(*g_saved_init_funcs));
+ g_saved_init_funcs = malloc((nfuncs - 1) * sizeof(*g_saved_init_funcs));
allow_write(&mod_init_func_start, &mod_init_func_end);
- int i = 0;
+ i = 0;
for (fp = &mod_init_func_start; fp < &mod_init_func_end; fp++)
{
if (*fp == save_and_replace_init_funcs)
{
- assert(i == 0);
Review Comment:
On macOS Mach-O, a function registered with __attribute__((constructor)) is
not guaranteed to be placed at the beginning of the __DATA,__mod_init_func
section. When CMake, Apple’s toolchain, coverage runtime, or initialization
functions originating from Rust/Cargo are involved, another constructor may be
placed before save_and_replace_init_funcs. If assert(i == 0); is kept in that
situation, the program may fail with an assertion failure at startup, or the
old logic may handle g_saved_init_funcs incorrectly because its assumption is
no longer valid.
Therefore, this change was necessary to handle the reality that, on macOS,
save_and_replace_init_funcs does not always come first. The new logic searches
for save_and_replace_init_funcs in the constructor array and safely saves and
replaces only the constructors that appear after it. That said, from NuttX’s
design perspective, it is still preferable for this function to be the first
entry. A complete solution would be to separately consider a way to guarantee
that placement, for example by controlling the link order or using a dedicated
section.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]