diff options
author | 2023-07-11 20:52:10 -0700 | |
---|---|---|
committer | 2023-07-11 20:52:10 -0700 | |
commit | 666feb3b7e3fdfaa9e1cb7ece39095a83df09fd1 (patch) | |
tree | 1ae7b316fb3941c3d2fdf53dc85e6503267335b0 | |
parent | 5ea32a976ec85ac7cb4b2e56aaa3e5c2cb9e9bf1 (diff) | |
download | bun-666feb3b7e3fdfaa9e1cb7ece39095a83df09fd1.tar.gz bun-666feb3b7e3fdfaa9e1cb7ece39095a83df09fd1.tar.zst bun-666feb3b7e3fdfaa9e1cb7ece39095a83df09fd1.zip |
Fix crash on linux when throwing SystemError from C++
-rw-r--r-- | src/bun.js/bindings/Process.cpp | 40 | ||||
-rw-r--r-- | src/bun.js/bindings/helpers.cpp | 25 | ||||
-rw-r--r-- | src/bun.js/bindings/helpers.h | 13 |
3 files changed, 47 insertions, 31 deletions
diff --git a/src/bun.js/bindings/Process.cpp b/src/bun.js/bindings/Process.cpp index 745be0e47..dd5c41c44 100644 --- a/src/bun.js/bindings/Process.cpp +++ b/src/bun.js/bindings/Process.cpp @@ -1022,10 +1022,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_functiongetgroups, (JSGlobalObject * globalObje int ngroups = getgroups(0, nullptr); auto throwScope = DECLARE_THROW_SCOPE(vm); if (ngroups == -1) { - SystemError error; - error.errno_ = errno; - error.syscall = Bun::toString("getgroups"_s); - throwException(globalObject, throwScope, JSValue::decode(SystemError__toErrorInstance(&error, globalObject))); + throwSystemError(throwScope, globalObject, "getgroups"_s, errno); return JSValue::encode(jsUndefined()); } @@ -1200,11 +1197,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionCpuUsage, auto throwScope = DECLARE_THROW_SCOPE(vm); struct rusage rusage; if (getrusage(RUSAGE_SELF, &rusage) != 0) { - SystemError error; - error.errno_ = errno; - error.syscall = Bun::toString("getrusage"_s); - error.message = Bun::toString("Failed to get CPU usage"_s); - throwException(globalObject, throwScope, JSValue::decode(SystemError__toErrorInstance(&error, globalObject))); + throwSystemError(throwScope, globalObject, "Failed to get CPU usage"_s, "getrusage"_s, errno); return JSValue::encode(jsUndefined()); } @@ -1359,11 +1352,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionMemoryUsage, size_t current_rss = 0; if (getRSS(¤t_rss) != 0) { - SystemError error; - error.errno_ = errno; - error.syscall = Bun::toString("memoryUsage"_s); - error.message = Bun::toString("Failed to get memory usage"_s); - throwException(globalObject, throwScope, JSValue::decode(SystemError__toErrorInstance(&error, globalObject))); + throwSystemError(throwScope, globalObject, "Failed to get memory usage"_s, "memoryUsage"_s, errno); return JSC::JSValue::encode(JSC::JSValue {}); } @@ -1406,11 +1395,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionMemoryUsageRSS, size_t current_rss = 0; if (getRSS(¤t_rss) != 0) { - SystemError error; - error.errno_ = errno; - error.syscall = Bun::toString("memoryUsage"_s); - error.message = Bun::toString("Failed to get memory usage"_s); - throwException(globalObject, throwScope, JSValue::decode(SystemError__toErrorInstance(&error, globalObject))); + throwSystemError(throwScope, globalObject, "Failed to get memory usage"_s, "memoryUsage"_s, errno); return JSC::JSValue::encode(JSC::JSValue {}); } @@ -1618,15 +1603,11 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionReallyKill, RETURN_IF_EXCEPTION(scope, {}); int result = kill(pid, signal); - if (result == -1) { - SystemError error; - error.errno_ = errno; - error.syscall = Bun::toString("kill"_s); - throwException(globalObject, scope, JSValue::decode(SystemError__toErrorInstance(&error, globalObject))); - return JSValue::encode(jsUndefined()); + if (result < 0) { + throwSystemError(scope, globalObject, "kill"_s, errno); } - return JSValue::encode(jsUndefined()); + RELEASE_AND_RETURN(scope, JSValue::encode(jsUndefined())); } JSC_DEFINE_HOST_FUNCTION(Process_functionKill, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) @@ -1665,11 +1646,8 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionKill, int result = kill(pid, signal); - if (result == -1) { - SystemError error; - error.errno_ = errno; - error.syscall = Bun::toString("kill"_s); - throwException(globalObject, scope, JSValue::decode(SystemError__toErrorInstance(&error, globalObject))); + if (result < 0) { + throwSystemError(scope, globalObject, "kill"_s, errno); return JSValue::encode(jsUndefined()); } diff --git a/src/bun.js/bindings/helpers.cpp b/src/bun.js/bindings/helpers.cpp new file mode 100644 index 000000000..88f2a5be2 --- /dev/null +++ b/src/bun.js/bindings/helpers.cpp @@ -0,0 +1,25 @@ +#include "root.h" +#include "helpers.h" +#include "BunClientData.h" + +JSC::JSValue createSystemError(JSC::JSGlobalObject* global, ASCIILiteral message, ASCIILiteral syscall, int err) +{ + auto* instance = JSC::createError(global, String(message)); + auto& vm = global->vm(); + auto& builtinNames = WebCore::builtinNames(vm); + instance->putDirect(vm, builtinNames.syscallPublicName(), jsString(vm, String(syscall)), 0); + instance->putDirect(vm, builtinNames.errnoPublicName(), JSC::jsNumber(err), 0); + instance->putDirect(vm, vm.propertyNames->name, jsString(vm, String("SystemError"_s)), JSC::PropertyAttribute::DontEnum | 0); + return instance; +} + +JSC::JSValue createSystemError(JSC::JSGlobalObject* global, ASCIILiteral syscall, int err) +{ + auto* instance = JSC::createError(global, makeString(String(syscall), "() failed"_s)); + auto& vm = global->vm(); + auto& builtinNames = WebCore::builtinNames(vm); + instance->putDirect(vm, builtinNames.syscallPublicName(), jsString(vm, String(syscall)), 0); + instance->putDirect(vm, builtinNames.errnoPublicName(), JSC::jsNumber(err), 0); + instance->putDirect(vm, vm.propertyNames->name, jsString(vm, String("SystemError"_s)), JSC::PropertyAttribute::DontEnum | 0); + return instance; +}
\ No newline at end of file diff --git a/src/bun.js/bindings/helpers.h b/src/bun.js/bindings/helpers.h index 00777c304..79aa3d472 100644 --- a/src/bun.js/bindings/helpers.h +++ b/src/bun.js/bindings/helpers.h @@ -391,6 +391,19 @@ static JSC::JSValue getRangeErrorInstance(const ZigString* str, JSC__JSGlobalObj }; // namespace Zig +JSC::JSValue createSystemError(JSC::JSGlobalObject* global, ASCIILiteral message, ASCIILiteral syscall, int err); +JSC::JSValue createSystemError(JSC::JSGlobalObject* global, ASCIILiteral syscall, int err); + +static void throwSystemError(JSC::ThrowScope& scope, JSC::JSGlobalObject* globalObject, ASCIILiteral syscall, int err) +{ + scope.throwException(globalObject, createSystemError(globalObject, syscall, err)); +} + +static void throwSystemError(JSC::ThrowScope& scope, JSC::JSGlobalObject* globalObject, ASCIILiteral message, ASCIILiteral syscall, int err) +{ + scope.throwException(globalObject, createSystemError(globalObject, message, syscall, err)); +} + template<typename WebCoreType, typename OutType> OutType* WebCoreCast(JSC__JSValue JSValue0) { |