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

fixed setSignal and added catch all exception implementation #10

Merged
merged 7 commits into from
May 30, 2024
Merged
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
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/src/cpp",
"${workspaceFolder}/node_modules/addon-tools-raub/include",
"${workspaceFolder}/node_modules/node-addon-api",
"${LocalAppData}/node-gyp/Cache/20.14.0/include/node"
],
Expand Down
61 changes: 61 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,65 @@ declare module "segfault-raub" {
* Issue an actual segfault, accessing some unavailable memory
*/
export const causeSegfault: () => void;
export const causeDivisionInt: () => void;
export const causeOverflow: () => void;
export const causeIllegal: () => void;

// enable / disable signal handlers
export const setSignal: (signalId: number | null, value: boolean) => void;

// windows constants
export const EXCEPTION_ALL: number | null;
export const EXCEPTION_ACCESS_VIOLATION: number | null;
export const EXCEPTION_DATATYPE_MISALIGNMENT: number | null;
export const EXCEPTION_BREAKPOINT: number | null;
export const EXCEPTION_SINGLE_STEP: number | null;
export const EXCEPTION_ARRAY_BOUNDS_EXCEEDED: number | null;
export const EXCEPTION_FLT_DENORMAL_OPERAND: number | null;
export const EXCEPTION_FLT_DIVIDE_BY_ZERO: number | null;
export const EXCEPTION_FLT_INEXACT_RESULT: number | null;
export const EXCEPTION_FLT_INVALID_OPERATION: number | null;
export const EXCEPTION_FLT_OVERFLOW: number | null;
export const EXCEPTION_FLT_STACK_CHECK: number | null;
export const EXCEPTION_FLT_UNDERFLOW: number | null;
export const EXCEPTION_INT_DIVIDE_BY_ZERO: number | null;
export const EXCEPTION_INT_OVERFLOW: number | null;
export const EXCEPTION_PRIV_INSTRUCTION: number | null;
export const EXCEPTION_IN_PAGE_ERROR: number | null;
export const EXCEPTION_ILLEGAL_INSTRUCTION: number | null;
export const EXCEPTION_NONCONTINUABLE_EXCEPTION: number | null;
export const EXCEPTION_STACK_OVERFLOW: number | null;
export const EXCEPTION_INVALID_DISPOSITION: number | null;
export const EXCEPTION_GUARD_PAGE: number | null;
export const EXCEPTION_INVALID_HANDLE: number | null;
export const STATUS_STACK_BUFFER_OVERRUN: number | null;

// linux constants
export const SIGABRT: number | null;
export const SIGFPE: number | null;
export const SIGSEGV: number | null;
export const SIGTERM: number | null;
export const SIGILL: number | null;
export const SIGINT: number | null;
export const SIGALRM: number | null;
export const SIGBUS: number | null;
export const SIGCHLD: number | null;
export const SIGCONT: number | null;
export const SIGHUP: number | null;
export const SIGKILL: number | null;
export const SIGPIPE: number | null;
export const SIGQUIT: number | null;
export const SIGSTOP: number | null;
export const SIGTSTP: number | null;
export const SIGTTIN: number | null;
export const SIGTTOU: number | null;
export const SIGUSR1: number | null;
export const SIGUSR2: number | null;
export const SIGPROF: number | null;
export const SIGSYS: number | null;
export const SIGTRAP: number | null;
export const SIGURG: number | null;
export const SIGVTALRM: number | null;
export const SIGXCPU: number | null;
export const SIGXFSZ: number | null;
}
21 changes: 18 additions & 3 deletions src/cpp/segfault-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ time_t timeInfo;

const std::map<uint32_t, std::string> signalNames = {
#ifdef _WIN32
#define EXCEPTION_ALL 0x0
{ EXCEPTION_ALL, "CAPTURE ALL THE EXCEPTIONS" },
{ EXCEPTION_ACCESS_VIOLATION, "ACCESS_VIOLATION" },
{ EXCEPTION_DATATYPE_MISALIGNMENT, "DATATYPE_MISALIGNMENT" },
{ EXCEPTION_BREAKPOINT, "BREAKPOINT" },
Expand All @@ -69,6 +71,8 @@ const std::map<uint32_t, std::string> signalNames = {
{ EXCEPTION_INVALID_DISPOSITION, "INVALID_DISPOSITION" },
{ EXCEPTION_GUARD_PAGE, "GUARD_PAGE" },
{ EXCEPTION_INVALID_HANDLE, "INVALID_HANDLE" },
{ STATUS_STACK_BUFFER_OVERRUN, "STACK_BUFFER_OVERRUN" },
// { EXCEPTION_POSSIBLE_DEADLOCK, "POSSIBLE_DEADLOCK" },
#else
{ SIGABRT, "SIGABRT" },
{ SIGFPE, "SIGFPE" },
Expand Down Expand Up @@ -103,6 +107,7 @@ const std::map<uint32_t, std::string> signalNames = {

std::map<uint32_t, bool> signalActivity = {
#ifdef _WIN32
{ EXCEPTION_ALL, false },
{ EXCEPTION_ACCESS_VIOLATION, true },
{ EXCEPTION_ARRAY_BOUNDS_EXCEEDED, true },
{ EXCEPTION_FLT_DIVIDE_BY_ZERO, true },
Expand All @@ -125,6 +130,8 @@ std::map<uint32_t, bool> signalActivity = {
{ EXCEPTION_IN_PAGE_ERROR, false },
{ EXCEPTION_INVALID_DISPOSITION, false },
{ EXCEPTION_GUARD_PAGE, false },
{ STATUS_STACK_BUFFER_OVERRUN, false },
// { EXCEPTION_POSSIBLE_DEADLOCK, false },
#else
{ SIGABRT, true },
{ SIGFPE, true },
Expand Down Expand Up @@ -159,7 +166,9 @@ std::map<uint32_t, bool> signalActivity = {

static inline bool _isSignalEnabled(uint32_t signalId) {
#ifdef _WIN32
return (signalNames.count(signalId) && signalActivity.count(signalId) && signalActivity[signalId]);
return signalActivity[EXCEPTION_ALL] || (
signalNames.count(signalId) && signalActivity.count(signalId) && signalActivity[signalId]
);
#else
return true;
#endif
Expand Down Expand Up @@ -228,8 +237,14 @@ static inline void _writeTimeToFile(std::ofstream &outfile) {
}

static inline void _writeHeaderToOstream(std::ostream &stream, int pid, uint32_t signalId, uint64_t address) {
std::string signalName;
if (signalNames.count(signalId)) {
signalName = signalNames.at(signalId);
} else {
signalName = std::to_string(signalId);
}
stream
<< "\nPID " << pid << " received " << signalNames.at(signalId)
<< "\nPID " << pid << " received " << signalName
<< " for address: 0x" << std::hex << address << std::endl;
}

Expand Down Expand Up @@ -366,7 +381,7 @@ DBG_EXPORT JS_METHOD(setSignal) { NAPI_ENV;
}

LET_INT32_ARG(0, signalId);
LET_BOOL_ARG(0, value);
LET_BOOL_ARG(1, value);

if (!signalNames.count(signalId)) {
RET_UNDEFINED;
Expand Down