Skip to content

fix(libc): keep the guest heap alive through static finalization - #19496

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
imellag:fix/libc-guest-heap-outlives-exit
Nov 13, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
imellag:fix/libc-guest-heap-outlives-exit

Conversation

@imellag

@imellag imellag commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What

guest_heap_lifetime fails on macOS with guest heap during exit: mutex lock failed: Invalid argument. The guest heap (ArenaHeap::Get()) is a function-local static, so its destructor runs during static finalization, and an atexit handler registered before the heap's first use runs after it. Its size-class std::mutexes are then destroyed. On glibc, locking a destroyed mutex happens to work; on macOS pthread_mutex_lock on a destroyed mutex fails with EINVAL, and libc++ throws std::system_error. A title whose static destructor or atexit handler allocates or frees guest memory therefore stops with that error at exit on macOS.

ArenaHeap::Get() now returns a heap that is created once and never destroyed (static ArenaHeap& heap = *new ArenaHeap;). The heap only holds arena spans, free lists and locks, which the process releases at exit anyway, so nothing that the destructor did is lost. Linux and Windows get the same object lifetime.

The draft #12849 (optional span reclaim) makes the same one-line change as part of a larger feature. This PR is that line alone, with the macOS failure it fixes; whichever lands second drops it.

Tested

macOS 27.2, Apple M4 Pro, x86-64 build under Rosetta 2, main d4556df:

  • main: guest_heap_lifetime fails (guest heap during exit: mutex lock failed: Invalid argument).
  • this PR: guest_heap_lifetime passes 5 runs out of 5; guest_heap_report_for_game, guest_heap_trace_info, guest_malloc_lifecycle, guest_reallocalign pass.

Not run on Linux or Windows.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@github-actions github-actions Bot added the libc label Nov 13, 2026
@JJenkx

JJenkx commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Windows check of head ed7df0a7d7, merged onto main 28f73b40b4 (20e1378e2a, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • application_heap_default, application_heap_exit_order, application_heap_invalid_metadata, application_heap_lazy, application_heap_replacement, application_heap_scripting, application_heap_stats, application_heap_stats_default, application_heap_stats_missing, application_heap_without_parameters, exception_personality, file_position, file_stream_status, file_streams, guest_access, guest_allocating_strings, guest_allocator_failures, guest_array_callbacks, guest_atoi, guest_atomic_builtins, guest_backtrace, guest_bad_any_cast_node_access, guest_bad_optional_access, guest_bessel, guest_binary_word, guest_bounded_search_guards, guest_bounds_checking, guest_bsearch_packed, guest_bzero_extent, guest_call_trace, guest_call_trace_abort, guest_case_compare_random, guest_char_search_boundary, guest_character, guest_cnd_at_thread_exit, guest_collate, guest_cxa_guard, guest_daemon, guest_decimal_boundary, guest_demangle, guest_device_name, guest_dinkumware_atomics, guest_dinkumware_division, guest_dinkumware_float_bits, guest_dinkumware_internals, guest_directory, guest_dsign, guest_dynamic_cast, guest_environment, guest_errors, guest_execute_once, guest_filesystem_error, guest_fiopen, guest_float_builtins, guest_floating_point, guest_fopen_errors, guest_fopen_long_host_path, guest_formatting, guest_freopen_host_log, guest_freopen_host_standard, guest_future_category, guest_fwide, guest_generic_category, guest_gets, guest_heap_report_for_game, guest_heap_rounding_overflow, guest_heap_trace_info, guest_hostname, guest_index, guest_integer_builtins, guest_interfaces, guest_libc_cnd_init_default_name, guest_libc_cnd_timed_wait, guest_libc_mutex_init_with_name, guest_libc_shared_mutex, guest_libc_static_imports, guest_libc_threads, guest_lifecycle, guest_locale, guest_malloc_lifecycle, guest_math, guest_math_elementary, guest_math_exact, guest_memccpy_bounds, guest_memcmp_protected, guest_memcpy_protected, guest_memmem_boundary, guest_memmove_snapshot, guest_memory_resource, guest_memrchr, guest_memset_extent, guest_mktime, guest_multibyte_locale, guest_new_handler, guest_new_handler_default_heap, guest_overflow_error, guest_owned_memory, guest_owned_memory_limits, guest_path_case, guest_path_drive_component, guest_qsort, guest_queue, guest_radix64, guest_raise_except, guest_random_device_seed, guest_random_state, guest_reallocalign, guest_realpath, guest_rune_locale, guest_safe_errors, guest_scan_count, guest_sce_v2_at_thread_exit, guest_sce_v2_binary_scaling, guest_sce_v2_exceptions, guest_sce_v2_exp, guest_sce_v2_facet_ids, guest_sce_v2_filesystem, guest_sce_v2_filesystem_calls, guest_sce_v2_float_constants, guest_sce_v2_float_helpers, guest_sce_v2_iomanip, guest_sce_v2_locale, guest_sce_v2_locale_basics, guest_sce_v2_locimp_references, guest_sce_v2_log_gamma, guest_sce_v2_multiprecision, guest_sce_v2_pmsw, guest_sce_v2_stream_init, guest_sce_v2_string_to_integer, guest_sce_v2_timespec64, guest_sce_v2_xtime_diff_to_millis, guest_search, guest_set_env, guest_sigvec, guest_span_protected, guest_standard_streams, guest_standard_streams_closed_stdout, guest_standard_streams_exclusive_stdout, guest_standard_streams_reopen_closed_stdout, guest_standard_streams_update_stdout, guest_std_atomic_wait, guest_std_atomic_wait_kernel, guest_std_execute_once, guest_std_thread_hardware_concurrency, guest_stdio_variadic, guest_stpcpy_extent, guest_strcat_extent, guest_strcmp_boundary, guest_strcoll_boundary, guest_strcpy_extent, guest_stream_buffer_host_write, guest_stream_purge, guest_strftime, guest_string_compare, guest_strings, guest_strlcat_boundary, guest_strncat_protected, guest_strncmp_boundary, guest_strncpy_protected, guest_strndup_guarded, guest_strnstr_boundary, guest_strpbrk_boundary, guest_strtod_subnormal, guest_strxfrm_boundary, guest_substring_protected, guest_syslog, guest_syslog_reload, guest_thrd_start, guest_thread_callback_abi, guest_throw_bad_array_new_length, guest_time_conversion, guest_time_extras, guest_time_return, guest_timespec_get, guest_timingsafe_boundary, guest_tokenizer_protected, guest_underflow_error, guest_unwind_accessors, guest_update_env, guest_vector_algorithms, guest_wcscmp_boundary, guest_wcslen_boundary, guest_wcsncat_extent, guest_wcsncmp_boundary, guest_wcsncpy_extent, guest_wcsrtombs, guest_wcsrtombs_s, guest_wide_char_boundary, guest_wide_compare_blocks, guest_wide_conversion, guest_wide_copy_extent, guest_wide_formatting, guest_wide_length_protected, guest_wide_scan_blocks, guest_wide_set_boundary, guest_wide_span_boundary, guest_wide_stream_scan, guest_wide_strings, guest_wide_substring_boundary, guest_wide_token_boundary, guest_wmemchr_boundary, guest_wmemcmp_boundary, guest_wmemcpy_extent, guest_wmemmove_snapshot, guest_wmemset_extent, guest_working_directory, log_file, log_macros, log_macros_timestamps, mspace, precise_wait, run_report, run_report_revision, thread_name, unwind_cursor, unwind_enclosing_function, windows_backtrace_failure_report, windows_backtrace_failure_report_once, windows_backtrace_report, windows_crash_stack_words, windows_crash_without_stack_words, windows_exceptions, windows_mappings_release pass in 3 of 3 runs, none skipped.

AI-assisted: yes.

@github-actions

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 12849 core/libs/prx/libc/src/GuestHeap.cpp

@oneandonlydean
oneandonlydean merged commit 1c24f21 into boykopovar:main Nov 13, 2026
22 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants