Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't call _kill when its not observable #15756

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 50 additions & 28 deletions src/bun.js/bindings/BunProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2904,6 +2904,18 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionCwd,
return JSValue::encode(getCachedCwd(globalObject));
}

static int reallyReallyKill(int pid, int signal)
{
#if !OS(WINDOWS)
int result = kill(pid, signal);
if (result < 0)
return errno;
return 0;
#else
return uv_kill(pid, signal);
#endif
}

JSC_DEFINE_HOST_FUNCTION(Process_functionReallyKill,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
Expand All @@ -2920,17 +2932,18 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionReallyKill,
int signal = callFrame->argument(1).toInt32(globalObject);
RETURN_IF_EXCEPTION(scope, {});

#if !OS(WINDOWS)
int result = kill(pid, signal);
if (result < 0)
result = errno;
#else
int result = uv_kill(pid, signal);
#endif
int result = reallyReallyKill(pid, signal);

RELEASE_AND_RETURN(scope, JSValue::encode(jsNumber(result)));
}

static JSValue Process_constructReallyKill(VM& vm, JSObject* processObject)
{
auto* process = static_cast<Process*>(processObject);
process->m_isKillFunctionObservable = true;
return JSFunction::create(vm, processObject->globalObject(), 2, String("_kill"_s), Process_functionReallyKill, ImplementationVisibility::Public);
}

JSC_DEFINE_HOST_FUNCTION(Process_functionKill,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
Expand Down Expand Up @@ -2961,30 +2974,39 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionKill,

auto global = jsCast<Zig::GlobalObject*>(globalObject);
auto& vm = global->vm();
JSValue _killFn = global->processObject()->get(globalObject, Identifier::fromString(vm, "_kill"_s));
RETURN_IF_EXCEPTION(scope, {});
if (!_killFn.isCallable()) {
throwTypeError(globalObject, scope, "process._kill is not a function"_s);
return {};
}

JSC::MarkedArgumentBuffer args;
args.append(jsNumber(pid));
args.append(jsNumber(signal));
JSC::CallData callData = JSC::getCallData(_killFn);
int rc;

NakedPtr<JSC::Exception> returnedException = nullptr;
auto result = JSC::call(globalObject, _killFn, callData, globalObject->globalThis(), args, returnedException);
RETURN_IF_EXCEPTION(scope, {});
if (callFrame->thisValue() == global->processObject() && !jsCast<Process*>(callFrame->thisValue())->m_isKillFunctionObservable) {
rc = reallyReallyKill(pid, signal);
} else {
JSValue _killFn = global->processObject()->get(globalObject, Identifier::fromString(vm, "_kill"_s));
RETURN_IF_EXCEPTION(scope, {});
if (!_killFn.isCallable()) {
throwTypeError(globalObject, scope, "process._kill is not a function"_s);
return {};
}

if (auto* exception = returnedException.get()) {
scope.throwException(globalObject, exception->value());
returnedException.clear();
return {};
JSC::MarkedArgumentBuffer args;
args.append(jsNumber(pid));
args.append(jsNumber(signal));
JSC::CallData callData = JSC::getCallData(_killFn);

NakedPtr<JSC::Exception> returnedException = nullptr;
auto result = JSC::call(globalObject, _killFn, callData, globalObject->globalThis(), args, returnedException);
RETURN_IF_EXCEPTION(scope, {});

if (auto* exception = returnedException.get()) {
scope.throwException(globalObject, exception->value());
returnedException.clear();
return {};
}
rc = result.toInt32(globalObject);
RETURN_IF_EXCEPTION(scope, {});
}
auto err = result.toInt32(globalObject);
if (err) {
throwSystemError(scope, globalObject, "kill"_s, err);

if (rc) {
throwSystemError(scope, globalObject, "kill"_s, rc);
return {};
}

Expand Down Expand Up @@ -3092,7 +3114,7 @@ extern "C" void Process__emitErrorEvent(Zig::GlobalObject* global, EncodedJSValu
_startProfilerIdleNotifier Process_stubEmptyFunction Function 0
_stopProfilerIdleNotifier Process_stubEmptyFunction Function 0
_tickCallback Process_stubEmptyFunction Function 0
_kill Process_functionReallyKill Function 2
_kill Process_constructReallyKill PropertyCallback
#if !OS(WINDOWS)
getegid Process_functiongetegid Function 0
geteuid Process_functiongeteuid Function 0
Expand Down
1 change: 1 addition & 0 deletions src/bun.js/bindings/BunProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Process : public WebCore::JSEventEmitter {
~Process();

bool m_isExitCodeObservable = false;
bool m_isKillFunctionObservable = false;

static constexpr unsigned StructureFlags = Base::StructureFlags | HasStaticPropertyTable;

Expand Down
Loading