Skip to content

Fix(sim): pass context helpers to AICore SO instead of dlsym(RTLD_DEFAULT)#451

Open
hw-native-sys-bot wants to merge 1 commit intohw-native-sys:mainfrom
hw-native-sys-bot:fix/sim-context-dlsym
Open

Fix(sim): pass context helpers to AICore SO instead of dlsym(RTLD_DEFAULT)#451
hw-native-sys-bot wants to merge 1 commit intohw-native-sys:mainfrom
hw-native-sys-bot:fix/sim-context-dlsym

Conversation

@hw-native-sys-bot
Copy link
Copy Markdown
Collaborator

Summary

The AICore kernel SO used dlsym(RTLD_DEFAULT, "pto_cpu_sim_*") to resolve sim context functions defined in cpu_sim_context.cpp. This works with RTLD_GLOBAL but fails with RTLD_LOCAL — the symbols are not in the global table, so dlsym(RTLD_DEFAULT) returns NULL.

This is a prerequisite for loading libhost_runtime.so with RTLD_LOCAL (needed when multiple runtimes coexist in the same process without symbol pollution).

Fix: Replace dlsym(RTLD_DEFAULT) with explicit function pointer injection:

  • kernel.cpp (a2a3 + a5): add set_sim_context_helpers() extern-C entry point, stores 3 function pointers
  • inner_kernel.h (a2a3 + a5): use stored pointers instead of static auto fn = dlsym(RTLD_DEFAULT, ...)
  • device_runner.cpp (a2a3 + a5): call set_sim_context_helpers() after loading AICore SO
  • cpu_sim_context.h (a2a3 + a5): declare the extern-C context functions for device_runner

No behavioral change with current RTLD_GLOBAL loading — the function pointers are resolved at the same time (AICore SO load) and point to the same functions.

Testing

  • python ci.py -p a2a3sim -c 6622890 -t 600 — 20/20 PASS

…AULT)

The AICore kernel SO used dlsym(RTLD_DEFAULT, "pto_cpu_sim_*") to resolve
sim context functions. This fails when libhost_runtime.so is loaded with
RTLD_LOCAL, which is needed for multi-runtime in-process isolation.

Replace with explicit function pointer injection via set_sim_context_helpers.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the simulation context bridging between the AICore kernel and host runtime for the a2a3 and a5 platforms. By replacing dlsym(RTLD_DEFAULT) with an explicit function pointer registration mechanism (set_sim_context_helpers), the implementation avoids resolution failures when the host library is loaded with RTLD_LOCAL. The review feedback recommends enhancing the robustness of symbol resolution in DeviceRunner by using dlerror() to check for errors after calling dlsym and ensuring proper error reporting.

Comment on lines +174 to +182
auto set_helpers =
reinterpret_cast<void (*)(void *, void *, void *)>(dlsym(aicore_so_handle_, "set_sim_context_helpers"));
if (set_helpers != nullptr) {
set_helpers(
reinterpret_cast<void *>(pto_cpu_sim_set_execution_context),
reinterpret_cast<void *>(pto_cpu_sim_set_task_cookie),
reinterpret_cast<void *>(platform_get_cpu_sim_task_cookie)
);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

To robustly check for errors with dlsym, call dlerror() to clear any prior errors before the call, and then check the result of dlerror() afterwards. Additionally, ensure that resource cleanup (like dlclose) is handled if initialization fails to prevent leaks.

Suggested change
auto set_helpers =
reinterpret_cast<void (*)(void *, void *, void *)>(dlsym(aicore_so_handle_, "set_sim_context_helpers"));
if (set_helpers != nullptr) {
set_helpers(
reinterpret_cast<void *>(pto_cpu_sim_set_execution_context),
reinterpret_cast<void *>(pto_cpu_sim_set_task_cookie),
reinterpret_cast<void *>(platform_get_cpu_sim_task_cookie)
);
}
(void)dlerror();
auto set_helpers = reinterpret_cast<void (*)(void *, void *, void *)>(dlsym(aicore_so_handle_, "set_sim_context_helpers"));
const char *dlsym_err = dlerror();
if (dlsym_err != nullptr) {
LOG_ERROR("dlsym failed for set_sim_context_helpers: %s", dlsym_err);
return -1;
}
set_helpers(reinterpret_cast<void *>(pto_cpu_sim_set_execution_context), reinterpret_cast<void *>(pto_cpu_sim_set_task_cookie),
reinterpret_cast<void *>(platform_get_cpu_sim_task_cookie));
References
  1. To robustly check for errors with dlsym, call dlerror() to clear any prior errors, then call dlsym, and then check the result of dlerror() to see if an error occurred.
  2. To prevent resource leaks with handles acquired via dlopen, use a try-catch block to ensure dlclose is called if any subsequent initialization steps fail.

Comment on lines +174 to +182
auto set_helpers =
reinterpret_cast<void (*)(void *, void *, void *)>(dlsym(aicore_so_handle_, "set_sim_context_helpers"));
if (set_helpers != nullptr) {
set_helpers(
reinterpret_cast<void *>(pto_cpu_sim_set_execution_context),
reinterpret_cast<void *>(pto_cpu_sim_set_task_cookie),
reinterpret_cast<void *>(platform_get_cpu_sim_task_cookie)
);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

To robustly check for errors with dlsym, call dlerror() to clear any prior errors before the call, and then check the result of dlerror() afterwards. Additionally, ensure that resource cleanup (like dlclose) is handled if initialization fails to prevent leaks.

Suggested change
auto set_helpers =
reinterpret_cast<void (*)(void *, void *, void *)>(dlsym(aicore_so_handle_, "set_sim_context_helpers"));
if (set_helpers != nullptr) {
set_helpers(
reinterpret_cast<void *>(pto_cpu_sim_set_execution_context),
reinterpret_cast<void *>(pto_cpu_sim_set_task_cookie),
reinterpret_cast<void *>(platform_get_cpu_sim_task_cookie)
);
}
(void)dlerror();
auto set_helpers = reinterpret_cast<void (*)(void *, void *, void *)>(dlsym(aicore_so_handle_, "set_sim_context_helpers"));
const char *dlsym_err = dlerror();
if (dlsym_err != nullptr) {
LOG_ERROR("dlsym failed for set_sim_context_helpers: %s", dlsym_err);
return -1;
}
set_helpers(reinterpret_cast<void *>(pto_cpu_sim_set_execution_context), reinterpret_cast<void *>(pto_cpu_sim_set_task_cookie),
reinterpret_cast<void *>(platform_get_cpu_sim_task_cookie));
References
  1. To robustly check for errors with dlsym, call dlerror() to clear any prior errors, then call dlsym, and then check the result of dlerror() to see if an error occurred.
  2. To prevent resource leaks with handles acquired via dlopen, use a try-catch block to ensure dlclose is called if any subsequent initialization steps fail.

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.

1 participant