napi backend: Pointer::finalize erases g_pointerCache re-entrantly on the JS thread — map corruption / SIGSEGV under GC finalization

Abierto
#109 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
32/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
cpp, objective-c
Área
backend

Línea de trabajo

Start in NativeScript/ffi/napi/Interop.mm at Pointer::finalize, then follow it to PostFinalizer in ffi/napi/ObjCBridge.mm, where finalizePointerNow runs inline on the JS thread. Check the iterator use in getCachedPointer, cachePointer and interop_free around napi calls, and the unconditional it->second dereference. Done means finalization is deferred to the JS run loop and null-ref entries are purged before use. No test is named in the payload, so you will need to find or write a reproduction.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Summary

Pointer::finalize (NativeScript/ffi/napi/Interop.mm) can mutate g_pointerCache re-entrantly during GC finalization on the JS thread, corrupting the std::unordered_map under live iterators and crashing intermittently.

Pointer::finalize defers through PostFinalizer (ffi/napi/ObjCBridge.mm), which runs finalizePointerNow inline whenever finalization happens on the JS thread (bridgeState->jsThreadId == this_thread). A JS engine may finalize inside any napi call that allocates — e.g. get_ref_value, napi_create_reference, napi_delete_reference. getCachedPointer, cachePointer, and interop_free all hold g_pointerCache iterators across napi calls, so an inline finalizePointerNow can erase nodes underneath them: the outer frame then dereferences a recycled iterator (SIGSEGV in __hash_table::remove) or passes a stale napi_ref to napi_delete_reference (fault in js_delete_reference on JSC).

We observed intermittent SIGSEGVs during run_file eval in a libjsc-based host once bridge work increased pointer-cache churn at boot — timing-dependent, not a script bug.

A second, related hazard: a stale entry can hold a null napi_ref. getCachedPointer dereferences it->second unconditionally (get_ref_value, napi_delete_reference); a null-ref entry should be purged and treated as a miss before use.

Expected

finalizePointerNow should never run re-entrantly inside an outer napi call that holds map iterators — e.g., always defer pointer finalization to the JS run loop (CFRunLoopPerformBlock(jsRunLoop, kCFRunLoopCommonModes, …) + CFRunLoopWakeUp) rather than inline — and getCachedPointer should purge null-ref entries before dereferencing them.

Environment

  • NativeScript/runtimes main (verified identical at 5b697b39)
  • Generic napi backend, TARGET_PLATFORM=macos, arm64, holepunchto libjsc env
  • Reproduced intermittently at script-eval time; Interop.mm is backend-shared, so other napi embedders carry the same risk profile.
Lenguaje dominante
C++
Estrellas
27
Forks
4
Merge medio
5 d 11 h
PR fusionados (30 d)
12

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de NativeScript/runtimes

Todos los issues de NativeScript/runtimes

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.