diff --git a/Source/JavaScriptCore/API/tests/testForeignStackSuspension.cpp b/Source/JavaScriptCore/API/tests/testForeignStackSuspension.cpp new file mode 100644 index 0000000000000..e87e64eefb30b --- /dev/null +++ b/Source/JavaScriptCore/API/tests/testForeignStackSuspension.cpp @@ -0,0 +1,297 @@ +/* + * Copyright (C) 2026 Peter Steinberger. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND ITS CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ITS + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if !ENABLE(SIGNAL_BASED_VM_TRAPS) +#error This regression requires signal-based VM traps. +#endif + +#include "APICast.h" +#include "Exception.h" +#include "GCRequest.h" +#include "Heap.h" +#include "InitializeThreading.h" +#include "JSCellInlines.h" +#include "JSFunction.h" +#include "JSGlobalObject.h" +#include "JSLock.h" +#include "JSObjectInlines.h" +#include "JavaScript.h" +#include "Options.h" +#include "ThrowScope.h" +#include "VM.h" +#include "VMInlines.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if ASAN_ENABLED +#include +#endif +#if TSAN_ENABLED +#include +#endif + +using JSC::CollectionScope; +using JSC::JSLockHolder; +using JSC::Options; +using JSC::Sync; +using JSC::VM; + +namespace { +struct State { + std::atomic entered { false }; + std::atomic release { false }; + std::atomic returned { false }; + std::atomic finish { false }; + std::atomic terminated { false }; + std::atomic vm { nullptr }; + ucontext_t original; + ucontext_t alternate; +#if ASAN_ENABLED + void* originalFakeStack { nullptr }; + const void* originalStackBottom { nullptr }; + size_t originalStackSize { 0 }; +#endif +#if TSAN_ENABLED + void* originalFiber { nullptr }; + void* alternateFiber { nullptr }; +#endif +}; +thread_local State* activeState; +struct sigaction previousSuspendAction; +std::atomic suspensionSignals { 0 }; +static_assert(std::atomic::is_always_lock_free); + +void observeSuspendSignal(int signal, siginfo_t* info, void* context) +{ + if (activeState && activeState->entered.load(std::memory_order_relaxed)) + suspensionSignals.fetch_add(1, std::memory_order_relaxed); + previousSuspendAction.sa_sigaction(signal, info, context); +} + +void waitFor(const std::atomic& flag) +{ + while (!flag.load(std::memory_order_acquire)) + std::this_thread::yield(); +} + +void foreignMain() +{ + auto& state = *activeState; +#if ASAN_ENABLED + __sanitizer_finish_switch_fiber(nullptr, &state.originalStackBottom, &state.originalStackSize); +#endif + RELEASE_ASSERT(!Thread::currentSingleton().stack().contains(currentStackPointer())); + state.entered.store(true, std::memory_order_release); + waitFor(state.release); +#if ASAN_ENABLED + __sanitizer_start_switch_fiber(nullptr, state.originalStackBottom, state.originalStackSize); +#endif +#if TSAN_ENABLED + __tsan_switch_to_fiber(state.originalFiber, 0); +#endif + RELEASE_ASSERT(!swapcontext(&state.alternate, &state.original)); + RELEASE_ASSERT_NOT_REACHED(); +} + +void runOnForeignStack(State& state) +{ + constexpr size_t stackSize = 1024 * 1024; + size_t guardSize = static_cast(sysconf(_SC_PAGESIZE)); + auto* mapping = static_cast(mmap(nullptr, stackSize + guardSize * 2, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + RELEASE_ASSERT(mapping != MAP_FAILED); + RELEASE_ASSERT(!mprotect(mapping + guardSize, stackSize, PROT_READ | PROT_WRITE)); + activeState = &state; + Thread::currentSingleton(); + RELEASE_ASSERT(!getcontext(&state.alternate)); + state.alternate.uc_stack.ss_sp = mapping + guardSize; + state.alternate.uc_stack.ss_size = stackSize; + state.alternate.uc_link = nullptr; + makecontext(&state.alternate, foreignMain, 0); +#if TSAN_ENABLED + state.originalFiber = __tsan_get_current_fiber(); + state.alternateFiber = __tsan_create_fiber(0); +#endif +#if ASAN_ENABLED + __sanitizer_start_switch_fiber(&state.originalFakeStack, mapping + guardSize, stackSize); +#endif +#if TSAN_ENABLED + __tsan_switch_to_fiber(state.alternateFiber, 0); +#endif + RELEASE_ASSERT(!swapcontext(&state.original, &state.alternate)); +#if ASAN_ENABLED + __sanitizer_finish_switch_fiber(state.originalFakeStack, nullptr, nullptr); +#endif +#if TSAN_ENABLED + __tsan_destroy_fiber(state.alternateFiber); +#endif + RELEASE_ASSERT(!munmap(mapping, stackSize + guardSize * 2)); + state.returned.store(true, std::memory_order_release); + activeState = nullptr; +} + +JSC_DECLARE_HOST_FUNCTION(park); +JSC_DEFINE_HOST_FUNCTION(park, (JSC::JSGlobalObject* globalObject, JSC::CallFrame*)) +{ + auto& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + // A host function retains the executing VM's ownership throughout the + // native call, as Bun's N-API bridge does. + RELEASE_ASSERT(vm.currentThreadIsHoldingAPILock()); + auto& state = *activeState; + runOnForeignStack(state); + RELEASE_ASSERT(vm.traps().needHandling(JSC::VMTraps::NeedTermination)); + RETURN_IF_EXCEPTION(scope, { }); + RELEASE_AND_RETURN(scope, JSC::JSValue::encode(JSC::jsUndefined())); +} + +JSValueRef evaluate(JSGlobalContextRef context, const char* text, JSValueRef* exception) +{ + JSStringRef script = JSStringCreateWithUTF8CString(text); + JSValueRef result = JSEvaluateScript(context, script, nullptr, nullptr, 1, exception); + JSStringRelease(script); + return result; +} + +#if !defined(TEST_FOREIGN_STACK_BLOCKING_CONTROL) +void testMessageRefusal() +{ + State state; + auto worker = Thread::create("Foreign stack test"_s, [&] { + runOnForeignStack(state); + waitFor(state.finish); + }); + waitFor(state.entered); + bool called = false; + { + ThreadSuspendLocker locker; + auto result = WTF::trySendMessage(locker, worker.get(), [&](PlatformRegisters&) { called = true; }); + RELEASE_ASSERT(result == WTF::MessageStatus::TemporarilyUnavailable); + RELEASE_ASSERT(!called); + } + RELEASE_ASSERT(!state.returned.load()); + state.release.store(true, std::memory_order_release); + waitFor(state.returned); + { + ThreadSuspendLocker locker; + auto result = WTF::trySendMessage(locker, worker.get(), [&](PlatformRegisters&) { called = true; }); + RELEASE_ASSERT(result == WTF::MessageStatus::MessageRan && called); + // A refused attempt must not leave an outstanding suspension count. + RELEASE_ASSERT(worker->suspend(locker)); + RELEASE_ASSERT(worker->suspend(locker)); + worker->resume(locker); + worker->resume(locker); + } + state.finish.store(true, std::memory_order_release); + worker->waitForCompletion(); + std::puts("PASS temporary refusal, later delivery, nested suspension and resume"); +} +#endif + +void testTerminationAndGC() +{ + State state; + JSGlobalContextRef parent = JSGlobalContextCreate(nullptr); + JSValueRef exception = nullptr; + JSValueRef root = evaluate(parent, "({value: 240, child: {value: 5718}})", &exception); + RELEASE_ASSERT(root && !exception); + JSValueProtect(parent, root); + auto worker = Thread::create("Foreign stack VM"_s, [&] { + JSGlobalContextRef context = JSGlobalContextCreate(nullptr); + auto& vm = toJS(context)->vm(); + activeState = &state; + { + JSLockHolder lock(vm); + vm.ensureTerminationException(); + auto* globalObject = toJS(context); + auto name = JSC::Identifier::fromString(vm, "park"_s); + auto* function = JSC::JSFunction::create(vm, globalObject, 0, name.string(), park, JSC::ImplementationVisibility::Public); + globalObject->putDirect(vm, name, function); + } + state.vm.store(&vm, std::memory_order_release); + JSValueRef thrown = nullptr; + JSValueRef result = evaluate(context, "park(); while (true) {}", &thrown); + { + JSLockHolder lock(vm); + state.terminated.store(!result && thrown && toJS(toJS(context), thrown) == vm.terminationException()->value(), std::memory_order_release); + JSGlobalContextRelease(context); + } + }); + waitFor(state.entered); + unsigned signalsBefore = suspensionSignals.load(); + state.vm.load(std::memory_order_acquire)->notifyNeedTermination(); + while (suspensionSignals.load() == signalsBefore) + std::this_thread::yield(); + { + // The release flag is deliberately written only after the parent GC. + // An unbounded trap-sender retry holds the lock this collection needs. + JSLockHolder lock(toJS(parent)->vm()); + toJS(parent)->vm().heap.collectNow(Sync, CollectionScope::Full); + } + RELEASE_ASSERT(!state.returned.load()); + JSStringRef key = JSStringCreateWithUTF8CString("value"); + JSValueRef value = JSObjectGetProperty(parent, JSValueToObject(parent, root, nullptr), key, nullptr); + RELEASE_ASSERT(JSValueToNumber(parent, value, nullptr) == 240); + JSStringRelease(key); + state.release.store(true, std::memory_order_release); + worker->waitForCompletion(); + RELEASE_ASSERT(state.returned.load() && state.terminated.load()); + JSValueUnprotect(parent, root); + { + JSLockHolder lock(toJS(parent)->vm()); + JSGlobalContextRelease(parent); + } + std::puts("PASS parent full GC before native release, rooted object retained, pending termination delivered"); +} +} + +int main() +{ + alarm(30); + WTF::initializeMainThread(); + JSC::initialize(); + RELEASE_ASSERT(Options::setOptions("usePollingTraps=false")); + RELEASE_ASSERT(!sigaction(g_wtfConfig.sigThreadSuspendResume, nullptr, &previousSuspendAction)); + RELEASE_ASSERT(previousSuspendAction.sa_flags & SA_SIGINFO); + auto action = previousSuspendAction; + action.sa_sigaction = observeSuspendSignal; + RELEASE_ASSERT(!sigaction(g_wtfConfig.sigThreadSuspendResume, &action, nullptr)); +#if !defined(TEST_FOREIGN_STACK_BLOCKING_CONTROL) + testMessageRefusal(); +#endif + testTerminationAndGC(); + RELEASE_ASSERT(!sigaction(g_wtfConfig.sigThreadSuspendResume, &previousSuspendAction, nullptr)); + alarm(0); + return 0; +} diff --git a/Source/JavaScriptCore/runtime/VMTraps.cpp b/Source/JavaScriptCore/runtime/VMTraps.cpp index 80eeab351f3ab..0d51148384f5a 100644 --- a/Source/JavaScriptCore/runtime/VMTraps.cpp +++ b/Source/JavaScriptCore/runtime/VMTraps.cpp @@ -307,7 +307,10 @@ class VMTraps::SignalSender final : public ThreadSafeRefCounteduid(); ThreadSuspendLocker locker; - sendMessage(locker, *optionalOwnerThread.value().get(), [&] (PlatformRegisters& registers) -> void { + // Foreign native stacks may wait for another VM to run. Leave the + // trap pending and release the global suspension lock before the + // scheduled retry, so that VM's collector can make progress. + WTF::trySendMessage(locker, *optionalOwnerThread.value().get(), [&](PlatformRegisters& registers) -> void { auto signalContext = SignalContext::tryCreate(registers); if (!signalContext) return; diff --git a/Source/JavaScriptCore/shell/CMakeLists.txt b/Source/JavaScriptCore/shell/CMakeLists.txt index 1c9601f25ec74..0e65b83abb179 100644 --- a/Source/JavaScriptCore/shell/CMakeLists.txt +++ b/Source/JavaScriptCore/shell/CMakeLists.txt @@ -104,6 +104,26 @@ if (USE_BUN_JSC_ADDITIONS) WEBKIT_EXECUTABLE_DECLARE(testFFI) endif () +if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND ENABLE_DFG_JIT AND (WTF_CPU_X86_64 OR WTF_CPU_ARM64 OR WTF_CPU_RISCV64)) + include(CheckCXXSymbolExists) + check_cxx_symbol_exists(getcontext "ucontext.h" HAVE_FOREIGN_STACK_TEST_GETCONTEXT) + check_cxx_symbol_exists(makecontext "ucontext.h" HAVE_FOREIGN_STACK_TEST_MAKECONTEXT) + check_cxx_symbol_exists(swapcontext "ucontext.h" HAVE_FOREIGN_STACK_TEST_SWAPCONTEXT) + + if (HAVE_FOREIGN_STACK_TEST_GETCONTEXT AND HAVE_FOREIGN_STACK_TEST_MAKECONTEXT AND HAVE_FOREIGN_STACK_TEST_SWAPCONTEXT) + set(testForeignStackSuspension_SOURCES ../API/tests/testForeignStackSuspension.cpp) + set(testForeignStackSuspension_DEFINITIONS ${jsc_PRIVATE_DEFINITIONS}) + set(testForeignStackSuspension_PRIVATE_INCLUDE_DIRECTORIES ${jsc_PRIVATE_INCLUDE_DIRECTORIES}) + set(testForeignStackSuspension_FRAMEWORKS ${jsc_FRAMEWORKS}) + set(testForeignStackSuspension_LIBRARIES ${CMAKE_DL_LIBS}) + if (USE_EXTERNAL_MIMALLOC) + list(APPEND testForeignStackSuspension_SOURCES ExternalMimallocShims.cpp) + list(APPEND testForeignStackSuspension_LIBRARIES $) + endif () + WEBKIT_EXECUTABLE_DECLARE(testForeignStackSuspension) + endif () +endif () + if (APPLE) WEBKIT_EXECUTABLE_DECLARE(mya) endif () @@ -244,6 +264,10 @@ if (USE_BUN_JSC_ADDITIONS) WEBKIT_EXECUTABLE(testFFI) endif () +if (TARGET testForeignStackSuspension) + WEBKIT_EXECUTABLE(testForeignStackSuspension) +endif () + if (SHOULD_INSTALL_JS_SHELL) install(TARGETS jsc DESTINATION "${LIBEXEC_INSTALL_DIR}") endif () diff --git a/Source/WTF/wtf/ThreadMessage.cpp b/Source/WTF/wtf/ThreadMessage.cpp index 83fb83603d0ee..a79779fff62ea 100644 --- a/Source/WTF/wtf/ThreadMessage.cpp +++ b/Source/WTF/wtf/ThreadMessage.cpp @@ -29,6 +29,21 @@ namespace WTF { +MessageStatus trySendMessageScoped(const ThreadSuspendLocker& locker, Thread& thread, NOESCAPE const ThreadMessage& message) +{ + auto result = thread.trySuspend(locker); + if (!result) + return MessageStatus::SuspensionFailed; + if (*result == Thread::SuspendResult::TemporarilyUnavailable) + return MessageStatus::TemporarilyUnavailable; + + PlatformRegisters registers; + thread.getRegisters(locker, registers); + message(registers); + thread.resume(locker); + return MessageStatus::MessageRan; +} + MessageStatus sendMessageScoped(const ThreadSuspendLocker& locker, Thread& thread, NOESCAPE const ThreadMessage& message) { auto result = thread.suspend(locker); diff --git a/Source/WTF/wtf/ThreadMessage.h b/Source/WTF/wtf/ThreadMessage.h index 4e69cf7bcd3c9..a7d5a290df455 100644 --- a/Source/WTF/wtf/ThreadMessage.h +++ b/Source/WTF/wtf/ThreadMessage.h @@ -35,6 +35,8 @@ using ThreadMessage = ScopedLambda; enum class MessageStatus { MessageRan, ThreadExited, + TemporarilyUnavailable, + SuspensionFailed, }; // Suspends targetThread, snapshots its register state, and runs func on the calling thread with @@ -45,6 +47,18 @@ enum class MessageStatus { // deadlock. Returns ThreadExited if targetThread had already exited. WTF_EXPORT_PRIVATE MessageStatus sendMessageScoped(const ThreadSuspendLocker&, Thread&, NOESCAPE const ThreadMessage&); +// Like sendMessageScoped, but a foreign-stack refusal leaves the thread running +// and returns TemporarilyUnavailable without invoking the message. This is for +// optional sampling, never for deciding whether a thread has GC roots. +// Platform errors return SuspensionFailed; they do not establish thread exit. +WTF_EXPORT_PRIVATE MessageStatus trySendMessageScoped(const ThreadSuspendLocker&, Thread&, NOESCAPE const ThreadMessage&); + +template +MessageStatus trySendMessage(const ThreadSuspendLocker& locker, Thread& targetThread, NOESCAPE const Functor& func) +{ + return trySendMessageScoped(locker, targetThread, func); +} + template MessageStatus sendMessage(const ThreadSuspendLocker& locker, Thread& targetThread, NOESCAPE const Functor& func) { diff --git a/Source/WTF/wtf/Threading.cpp b/Source/WTF/wtf/Threading.cpp index d1b34f1b430d5..91eef1a20d3f8 100644 --- a/Source/WTF/wtf/Threading.cpp +++ b/Source/WTF/wtf/Threading.cpp @@ -100,6 +100,16 @@ static constexpr pas_thread_suspender s_pasThreadSuspender = { namespace WTF { +#if OS(DARWIN) || !USE(PTHREADS) +std::expected Thread::trySuspend(const ThreadSuspendLocker& locker) +{ + auto result = suspend(locker); + if (!result) + return makeUnexpected(result.error()); + return SuspendResult::Suspended; +} +#endif + // During suspend, suspend or resume should not be executed from the other threads. // We use global lock instead of per thread lock. // Consider the following case, there are threads A and B. diff --git a/Source/WTF/wtf/Threading.h b/Source/WTF/wtf/Threading.h index 4e582e5ba64ce..5b7850eaadb16 100644 --- a/Source/WTF/wtf/Threading.h +++ b/Source/WTF/wtf/Threading.h @@ -194,6 +194,10 @@ class WTF_CAPABILITY("is current") Thread : public ThreadSafeRefCountedAndCanMak #endif WTF_EXPORT_PRIVATE std::expected suspend(const ThreadSuspendLocker&); + enum class SuspendResult : uint8_t { Suspended, TemporarilyUnavailable }; + // One suspension attempt. A live thread on a foreign stack remains running; + // callers must release their suspension locker before retrying it. + WTF_EXPORT_PRIVATE std::expected trySuspend(const ThreadSuspendLocker&); WTF_EXPORT_PRIVATE void resume(const ThreadSuspendLocker&); WTF_EXPORT_PRIVATE size_t getRegisters(const ThreadSuspendLocker&, PlatformRegisters&); diff --git a/Source/WTF/wtf/posix/ThreadingPOSIX.cpp b/Source/WTF/wtf/posix/ThreadingPOSIX.cpp index 8146af32e594e..35f67527515da 100644 --- a/Source/WTF/wtf/posix/ThreadingPOSIX.cpp +++ b/Source/WTF/wtf/posix/ThreadingPOSIX.cpp @@ -641,40 +641,50 @@ bool Thread::signal(int signalNumber) return !errNo; // A 0 errNo means success. } -auto Thread::suspend(const ThreadSuspendLocker&) -> std::expected +auto Thread::suspend(const ThreadSuspendLocker& locker) -> std::expected { // currentMayBeNull, not currentSingleton: the libpas scavenger calls this while holding // the heap lock, and currentSingleton would lazy-allocate a Thread for it. RELEASE_ASSERT_WITH_MESSAGE(this != Thread::currentMayBeNull(), "We do not support suspending the current thread itself."); #if OS(DARWIN) + UNUSED_PARAM(locker); kern_return_t result = thread_suspend(m_platformThread); if (result != KERN_SUCCESS) return makeUnexpected(result); return { }; #else + while (true) { + auto result = trySuspend(locker); + if (!result) + return makeUnexpected(result.error()); + if (*result == SuspendResult::Suspended) + return { }; + Thread::yield(); + } +#endif +} + +#if !OS(DARWIN) +std::expected Thread::trySuspend(const ThreadSuspendLocker&) +{ + RELEASE_ASSERT_WITH_MESSAGE(this != Thread::currentMayBeNull(), "We do not support suspending the current thread itself."); if (!m_suspendCount) { targetThread.store(this); - - while (true) { - publishSuspendResumeRequest(m_handle, SuspendResumeRequest::Suspend); - // We must use pthread_kill to avoid queue-overflow problem with real-time signals. - int result = pthread_kill(m_handle, g_wtfConfig.sigThreadSuspendResume); - if (result) { - clearSuspendResumeRequest(); - return makeUnexpected(result); - } - globalSemaphoreForSuspendResume->wait(); - if (m_platformRegisters) - break; - // Because of an alternative signal stack, we failed to suspend this thread. - // Retry suspension again after yielding. - Thread::yield(); + publishSuspendResumeRequest(m_handle, SuspendResumeRequest::Suspend); + // We must use pthread_kill to avoid queue-overflow problem with real-time signals. + int result = pthread_kill(m_handle, g_wtfConfig.sigThreadSuspendResume); + if (result) { + clearSuspendResumeRequest(); + return makeUnexpected(result); } + globalSemaphoreForSuspendResume->wait(); + if (!m_platformRegisters) + return SuspendResult::TemporarilyUnavailable; } ++m_suspendCount; - return { }; -#endif + return SuspendResult::Suspended; } +#endif void Thread::resume(const ThreadSuspendLocker&) {