diff --git a/tslang/include/TypeScript/UncaughtException.h b/tslang/include/TypeScript/UncaughtException.h index ebb98e248..6148a9059 100644 --- a/tslang/include/TypeScript/UncaughtException.h +++ b/tslang/include/TypeScript/UncaughtException.h @@ -101,16 +101,19 @@ inline void describeUncaught(EXCEPTION_POINTERS *pointers, char *uncaughtDescrip uncaughtDescription[0] = '\0'; __try { + // x64 and ARM64: the ThrowInfo's references are relative to the image base, the 4th parameter; x86 has 3 + // parameters, and its references are addresses (a base of 0) auto *record = pointers->ExceptionRecord; - if (record->NumberParameters < 4) + if (record->NumberParameters < (sizeof(void *) == 4 ? 3u : 4u)) { return; } auto *object = reinterpret_cast(record->ExceptionInformation[1]); auto *throwInfo = reinterpret_cast(record->ExceptionInformation[2]); - auto *imageBase = reinterpret_cast(record->ExceptionInformation[3]); - if (!object || !throwInfo || !imageBase) + auto *imageBase = + record->NumberParameters >= 4 ? reinterpret_cast(record->ExceptionInformation[3]) : nullptr; + if (!object || !throwInfo || (!imageBase && sizeof(void *) != 4)) { return; } diff --git a/tslang/include/TypeScript/UncaughtStackTrace.h b/tslang/include/TypeScript/UncaughtStackTrace.h new file mode 100644 index 000000000..c736987a7 --- /dev/null +++ b/tslang/include/TypeScript/UncaughtStackTrace.h @@ -0,0 +1,899 @@ +#ifndef TYPESCRIPT_UNCAUGHTSTACKTRACE_H_ +#define TYPESCRIPT_UNCAUGHTSTACKTRACE_H_ + +// Where an exception nothing caught was thrown from, said under "uncaught exception: ..." by the JIT +// (tslang/jit.cpp) and an executable's runtime (TypeScriptAsyncRuntime/UncaughtException.cpp): +// +// error: uncaught exception: Failure: not caught +// at fail (I:\src\program.ts:7) +// at main (I:\src\program.ts:12) +// +// Always, not only with --di: the frames are walked only on the way out of a program that failed (on +// Windows a throw also copies its context, see noteThrow). What a +// frame can say depends on the build. A Windows executable without --di has no PDB, so its frames are +// `program.exe+0x1a2b` and a line says to compile with --di; with --di dbghelp reads the function and the +// line from the PDB. A Linux executable keeps its symbol table, so its frames have names, and with --di +// `addr2line` (when it is installed) adds the line. JIT'd code is named by the JIT (an UncaughtFrameNamer). +// +// The frames are those of the throw: on Windows the context of the exception record, read in the +// unhandled-exception filter, before anything is unwound - or, when that is a `finally` throwing it again, +// the context of the first throw (thrownFrom); on Linux std::terminate() is called where the throw found +// no catch (a `finally` is a cleanup there, which the search passes), so the throw is still on the stack. The runtime's own frames are left out - the +// throw (RaiseException, _CxxThrowException, __cxa_throw), the C runtime's startup after main and, in the +// JIT, tslang's own frames around the program. + +#include "TypeScript/UncaughtException.h" + +#ifdef TSLANG_DESCRIBES_UNCAUGHT + +#include +#include +#include + +#ifdef _WIN32 +#include // the types; dbghelp.dll is loaded only when a trace is said +#else +#include +#include +#include +#include +#include +#include +#include +#endif + +namespace typescript +{ + +constexpr int uncaughtStackMaxFrames = 64; + +// What a frame is called: its function, and its source line when debug info has one +struct UncaughtFrameName +{ + char function[256]; + char file[512]; + unsigned line; +}; + +// Names a frame of code no module holds (the JIT's); false for one it does not know +using UncaughtFrameNamer = bool (*)(uintptr_t pc, UncaughtFrameName &name); + +namespace uncaught_stack +{ + +enum class FrameKind +{ + Program, + // the C/C++ runtime and the system: never said + System, + // the JIT: tslang itself, around the program + Host, +}; + +inline bool startsWith(const char *text, const char *prefix) +{ + return strncmp(text, prefix, strlen(prefix)) == 0; +} + +#ifdef _WIN32 + +inline bool startsWithNoCase(const char *text, const char *prefix) +{ + return _strnicmp(text, prefix, strlen(prefix)) == 0; +} + +inline const char *baseName(const char *path) +{ + auto *name = path; + for (auto *at = path; *at; at++) + { + if (*at == '\\' || *at == '/') + { + name = at + 1; + } + } + + return name; +} + +struct DbgHelp +{ + HANDLE process; + decltype(&SymFromAddr) fromAddr; + decltype(&SymGetLineFromAddr64) lineFromAddr; + decltype(&SymGetModuleInfo64) moduleInfo; +}; + +// dbghelp.dll from System32, initialized once; null when it can't be had +inline DbgHelp *dbgHelp() +{ + static DbgHelp dbg; + static bool tried; + if (tried) + { + return dbg.fromAddr ? &dbg : nullptr; + } + + tried = true; + auto library = LoadLibraryExW(L"dbghelp.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + if (!library) + { + return nullptr; + } + + auto setOptions = reinterpret_cast(GetProcAddress(library, "SymSetOptions")); + auto initialize = reinterpret_cast(GetProcAddress(library, "SymInitialize")); + auto fromAddr = reinterpret_cast(GetProcAddress(library, "SymFromAddr")); + dbg.lineFromAddr = reinterpret_cast(GetProcAddress(library, "SymGetLineFromAddr64")); + dbg.moduleInfo = reinterpret_cast(GetProcAddress(library, "SymGetModuleInfo64")); + if (!setOptions || !initialize || !fromAddr || !dbg.lineFromAddr || !dbg.moduleInfo) + { + return nullptr; + } + + dbg.process = GetCurrentProcess(); + setOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES | SYMOPT_FAIL_CRITICAL_ERRORS | + SYMOPT_NO_PROMPTS); + + // A PDB is looked for where the image says it is, then beside the executable - not on a symbol server + // _NT_SYMBOL_PATH may name, which a failing program would wait on. Already initialized (LLVM's crash + // handler in tslang) is as good. + char searchPath[MAX_PATH] = ""; + auto length = GetModuleFileNameA(nullptr, searchPath, MAX_PATH); + if (length > 0 && length < MAX_PATH) + { + const_cast(baseName(searchPath))[0] = '\0'; + } + + initialize(dbg.process, searchPath[0] ? searchPath : nullptr, TRUE); + dbg.fromAddr = fromAddr; + return &dbg; +} + +} // namespace uncaught_stack +} // namespace typescript + +// vcruntime's: the exception whose catch block runs on this thread, and the context it was thrown in +extern "C" void **__cdecl __current_exception(); +extern "C" void **__cdecl __current_exception_context(); + +namespace typescript +{ +namespace uncaught_stack +{ + +constexpr DWORD cxxExceptionCode = 0xE06D7363; // 'msc' | 0xE0000000, a throw + +// The object a throw's record throws: null for `throw;` as the JIT raises it (the exception being caught) +inline ULONG_PTR thrownObject(const EXCEPTION_RECORD *record) +{ + return record->NumberParameters >= 2 ? record->ExceptionInformation[1] : 0; +} + +// The object of the exception whose catch block runs on this thread, or 0 +inline ULONG_PTR caughtObject() +{ + __try + { + auto *current = static_cast(*__current_exception()); + return current ? thrownObject(current) : 0; + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + return 0; + } +} + +// A throw again of the exception a catch block caught: `throw;`, which a `finally` and a scope that releases +// what it holds lower to +inline bool isRethrow(const EXCEPTION_RECORD *record) +{ + auto object = thrownObject(record); + return !object || object == caughtObject(); +} + +// The last throw on this thread that was not a rethrow: its record and context, copied as it is raised +struct ThrowSite +{ + EXCEPTION_RECORD record; + CONTEXT context; + EXCEPTION_POINTERS pointers; + bool set; +}; + +inline ThrowSite &throwSite() +{ + static thread_local ThrowSite site; + return site; +} + +// A vectored handler: it sees every throw before any frame's handler does, and a rethrow does not replace +// the throw it throws again. The copy is all it costs a throw. +inline LONG WINAPI noteThrow(EXCEPTION_POINTERS *pointers) +{ + auto *record = pointers->ExceptionRecord; + if (record->ExceptionCode == cxxExceptionCode && !isRethrow(record)) + { + auto &site = throwSite(); + site.record = *record; + site.record.ExceptionRecord = nullptr; + site.context = *pointers->ContextRecord; + site.set = true; + } + + return EXCEPTION_CONTINUE_SEARCH; +} + +inline void noteThrows() +{ + AddVectoredExceptionHandler(0, noteThrow); +} + +// Where the exception that reached the filter was thrown. A `finally` catches everything and throws it +// again: what reaches the filter is then that rethrow, from a catch block that runs where vcruntime has +// unwound to the function with the `finally`, so its frames end there. The first throw's frames are still on +// the stack, under the catch blocks, and its record (noteThrow) says what it threw - for the JIT's `throw;`, +// which raises no object, too. +inline EXCEPTION_POINTERS *thrownFrom(EXCEPTION_POINTERS *pointers) +{ + auto *record = pointers->ExceptionRecord; + auto &site = throwSite(); + if (record->ExceptionCode != cxxExceptionCode || !site.set || !isRethrow(record)) + { + return pointers; + } + + // a throw caught and done with inside the catch block is not the one thrown again + auto object = thrownObject(record) ? thrownObject(record) : caughtObject(); + if (object && object != thrownObject(&site.record)) + { + return pointers; + } + + site.pointers.ExceptionRecord = &site.record; + site.pointers.ContextRecord = &site.context; + return &site.pointers; +} + +// The return addresses from the context of a throw outward, the first the throw's own pc +inline int walk(const CONTEXT *start, uintptr_t *pcs, int max) +{ + auto context = *start; + auto count = 0; + __try + { +#if defined(_M_X64) || defined(_M_ARM64) + while (count < max) + { +#ifdef _M_X64 + auto pc = context.Rip; + auto sp = context.Rsp; +#else + auto pc = context.Pc; + auto sp = context.Sp; +#endif + if (!pc) + { + break; + } + + pcs[count++] = static_cast(pc); + // the JIT's code too: it registers its .pdata (RtlAddFunctionTable) + DWORD64 imageBase; + auto *function = RtlLookupFunctionEntry(pc, &imageBase, nullptr); + if (function) + { + void *handlerData; + DWORD64 establisherFrame; + RtlVirtualUnwind(UNW_FLAG_NHANDLER, imageBase, pc, function, &context, &handlerData, + &establisherFrame, nullptr); + } + else + { + // a leaf function: where it returns to is on top of the stack, or in the link register +#ifdef _M_X64 + context.Rip = *reinterpret_cast(context.Rsp); + context.Rsp += 8; +#else + context.Pc = context.Lr; +#endif + } + +#ifdef _M_X64 + if (context.Rip == pc && context.Rsp == sp) +#else + if (context.Pc == pc && context.Sp == sp) +#endif + { + break; + } + } +#elif defined(_M_IX86) + // The chain of frame pointers: [ebp] the caller's ebp, [ebp + 4] the return address. StackWalk64 lost + // the caller of every function LLVM compiled (it has no FPO record of them), and a function with no + // frame pointer is only left out here: ebp goes through it unchanged. + auto *tib = reinterpret_cast(NtCurrentTeb()); + auto low = reinterpret_cast(tib->StackLimit); + auto high = reinterpret_cast(tib->StackBase); + pcs[count++] = static_cast(context.Eip); + auto frame = static_cast(context.Ebp); + while (count < max && frame >= low && frame + 2 * sizeof(uintptr_t) <= high && frame % sizeof(uintptr_t) == 0) + { + auto *slots = reinterpret_cast(frame); + auto returnAddress = slots[1]; + if (!returnAddress) + { + break; + } + + pcs[count++] = returnAddress; + if (slots[0] <= frame) + { + break; + } + + frame = slots[0]; + } +#endif + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + } + + return count; +} + +// The module that holds `pc`, null for code no module holds (the JIT's) +inline HMODULE moduleOf(uintptr_t pc) +{ + HMODULE module = nullptr; + if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + reinterpret_cast(pc), &module)) + { + return nullptr; + } + + return module; +} + +// the system's DLLs, and the C/C++ runtime's wherever it is installed +inline bool isSystemModule(HMODULE module) +{ + char path[MAX_PATH]; + auto length = GetModuleFileNameA(module, path, MAX_PATH); + if (length == 0 || length >= MAX_PATH) + { + return false; + } + + char systemDirectory[MAX_PATH]; + auto systemLength = GetSystemDirectoryA(systemDirectory, MAX_PATH); + if (systemLength > 0 && systemLength < MAX_PATH && startsWithNoCase(path, systemDirectory)) + { + return true; + } + + auto *name = baseName(path); + return startsWithNoCase(name, "vcruntime") || startsWithNoCase(name, "ucrtbase") || + startsWithNoCase(name, "msvcp") || startsWithNoCase(name, "concrt"); +} + +// Whether `pc` is in the function that starts at `function` (the same unwind entry; on x86, which has +// none, close behind its start) +inline bool inFunction(uintptr_t pc, const void *function) +{ + if (!function) + { + return false; + } + + auto start = reinterpret_cast(function); +#if defined(_M_X64) || defined(_M_ARM64) + DWORD64 pcBase, startBase; + auto *pcEntry = RtlLookupFunctionEntry(pc, &pcBase, nullptr); + auto *startEntry = RtlLookupFunctionEntry(start, &startBase, nullptr); + return pcEntry && pcEntry == startEntry; +#else + return pc > start && pc - start < 0x100; +#endif +} + +inline bool nameFrame(uintptr_t pc, UncaughtFrameName &name) +{ + auto *dbg = dbgHelp(); + if (!dbg) + { + return false; + } + + char buffer[sizeof(SYMBOL_INFO) + sizeof(name.function)]; + auto *symbol = reinterpret_cast(buffer); + memset(symbol, 0, sizeof(SYMBOL_INFO)); + symbol->SizeOfStruct = sizeof(SYMBOL_INFO); + symbol->MaxNameLen = sizeof(name.function) - 1; + DWORD64 displacement = 0; + if (!dbg->fromAddr(dbg->process, pc, &displacement, symbol)) + { + return false; + } + + // an export table's nearest name is as likely another function's, and so is a PDB of another build of + // the image (one with --di, beside it under the same name): names from its own debug info only + IMAGEHLP_MODULE64 module = {}; + module.SizeOfStruct = sizeof(module); + if (!dbg->moduleInfo(dbg->process, pc, &module) || module.SymType == SymNone || + module.SymType == SymExport || module.SymType == SymDeferred || module.PdbUnmatched || module.DbgUnmatched) + { + return false; + } + + snprintf(name.function, sizeof(name.function), "%s", symbol->Name); + IMAGEHLP_LINE64 line = {}; + line.SizeOfStruct = sizeof(line); + DWORD lineDisplacement = 0; + if (dbg->lineFromAddr(dbg->process, pc, &lineDisplacement, &line) && line.FileName) + { + snprintf(name.file, sizeof(name.file), "%s", line.FileName); + name.line = line.LineNumber; + } + + return true; +} + +#else // Linux + +// The return addresses from here outward +struct WalkState +{ + uintptr_t *pcs; + int max; + int count; +}; + +inline _Unwind_Reason_Code walkFrame(_Unwind_Context *context, void *argument) +{ + auto *state = static_cast(argument); + auto pc = static_cast(_Unwind_GetIP(context)); + if (!pc || state->count >= state->max) + { + return _URC_END_OF_STACK; + } + + state->pcs[state->count++] = pc; + return _URC_NO_REASON; +} + +inline int walk(uintptr_t *pcs, int max) +{ + WalkState state = {pcs, max, 0}; + _Unwind_Backtrace(walkFrame, &state); + return state.count; +} + +// A module (the executable or a shared library) and its symbol table, read from its file +struct Module +{ + char path[512]; + // where it is loaded: its addresses are `pc - bias` + uintptr_t bias; + const char *image; + size_t size; + const ElfW(Sym) *symbols; + size_t symbolCount; + const char *names; + bool hasLines; +}; + +struct FindModule +{ + uintptr_t pc; + bool found; + bool isProgram; + char path[512]; + uintptr_t bias; +}; + +inline int findModule(dl_phdr_info *info, size_t, void *argument) +{ + auto *find = static_cast(argument); + for (auto i = 0; i < info->dlpi_phnum; i++) + { + auto &header = info->dlpi_phdr[i]; + auto start = info->dlpi_addr + header.p_vaddr; + if (header.p_type == PT_LOAD && find->pc >= start && find->pc < start + header.p_memsz) + { + find->found = true; + find->isProgram = !info->dlpi_name || !info->dlpi_name[0]; + snprintf(find->path, sizeof(find->path), "%s", find->isProgram ? "/proc/self/exe" : info->dlpi_name); + if (find->isProgram) + { + // the executable by its own name, as a frame without one says it + auto length = readlink("/proc/self/exe", find->path, sizeof(find->path) - 1); + find->path[length > 0 ? length : 0] = '\0'; + if (length <= 0) + { + snprintf(find->path, sizeof(find->path), "/proc/self/exe"); + } + } + find->bias = info->dlpi_addr; + return 1; + } + } + + return 0; +} + +// Its .symtab (or, stripped, .dynsym), mapped once and kept: the program is ending +inline void readModule(Module &module) +{ + auto file = open(module.path, O_RDONLY | O_CLOEXEC); + if (file < 0) + { + return; + } + + struct stat status; + if (fstat(file, &status) != 0 || status.st_size < static_cast(sizeof(ElfW(Ehdr)))) + { + close(file); + return; + } + + auto *image = mmap(nullptr, status.st_size, PROT_READ, MAP_PRIVATE, file, 0); + close(file); + if (image == MAP_FAILED) + { + return; + } + + module.image = static_cast(image); + module.size = status.st_size; + auto *header = reinterpret_cast(module.image); + if (memcmp(header->e_ident, ELFMAG, SELFMAG) != 0 || header->e_shoff == 0 || + header->e_shoff + header->e_shnum * sizeof(ElfW(Shdr)) > module.size) + { + return; + } + + auto *sections = reinterpret_cast(module.image + header->e_shoff); + auto *sectionNames = header->e_shstrndx < header->e_shnum ? module.image + sections[header->e_shstrndx].sh_offset + : nullptr; + const ElfW(Shdr) *symbolSection = nullptr; + for (auto i = 0; i < header->e_shnum; i++) + { + auto §ion = sections[i]; + if (section.sh_type == SHT_SYMTAB || (section.sh_type == SHT_DYNSYM && !symbolSection)) + { + symbolSection = §ion; + } + + if (sectionNames && strcmp(sectionNames + section.sh_name, ".debug_line") == 0) + { + module.hasLines = true; + } + } + + if (!symbolSection || symbolSection->sh_link >= header->e_shnum || + symbolSection->sh_offset + symbolSection->sh_size > module.size) + { + return; + } + + module.symbols = reinterpret_cast(module.image + symbolSection->sh_offset); + module.symbolCount = symbolSection->sh_size / sizeof(ElfW(Sym)); + module.names = module.image + sections[symbolSection->sh_link].sh_offset; +} + +// The module that holds `pc`, read on first use; null for code no module holds (the JIT's) +inline Module *moduleOf(uintptr_t pc, bool *isProgram = nullptr) +{ + static Module modules[16]; + static int moduleCount; + + FindModule find = {}; + find.pc = pc; + dl_iterate_phdr(findModule, &find); + if (!find.found) + { + return nullptr; + } + + if (isProgram) + { + *isProgram = find.isProgram; + } + + for (auto i = 0; i < moduleCount; i++) + { + if (modules[i].bias == find.bias && strcmp(modules[i].path, find.path) == 0) + { + return &modules[i]; + } + } + + if (moduleCount == 16) + { + return nullptr; + } + + auto &module = modules[moduleCount++]; + snprintf(module.path, sizeof(module.path), "%s", find.path); + module.bias = find.bias; + readModule(module); + return &module; +} + +inline bool isSystemModule(const Module *module) +{ + auto *name = strrchr(module->path, '/'); + name = name ? name + 1 : module->path; + return startsWith(name, "libc.so") || startsWith(name, "libc-") || startsWith(name, "libstdc++") || + startsWith(name, "libgcc_s") || startsWith(name, "libc++") || startsWith(name, "ld-linux") || + startsWith(name, "ld-musl") || startsWith(name, "libm.so") || startsWith(name, "libpthread") || + startsWith(name, "libunwind"); +} + +// The function of the symbol table that holds `pc` +inline const char *functionAt(const Module *module, uintptr_t pc) +{ + if (!module->symbols) + { + return nullptr; + } + + auto address = pc - module->bias; + for (size_t i = 0; i < module->symbolCount; i++) + { + auto &symbol = module->symbols[i]; + if (ELF64_ST_TYPE(symbol.st_info) == STT_FUNC && symbol.st_value && address >= symbol.st_value && + address < symbol.st_value + (symbol.st_size ? symbol.st_size : 1)) + { + return module->names + symbol.st_name; + } + } + + return nullptr; +} + +// The source line from debug info, by `addr2line` - when the module has a .debug_line and the tool is there +inline void lineAt(const Module *module, uintptr_t pc, UncaughtFrameName &name) +{ + if (!module->hasLines || strchr(module->path, '\'') || access("/usr/bin/addr2line", X_OK) != 0) + { + return; + } + + char command[700]; + snprintf(command, sizeof(command), "/usr/bin/addr2line -e '%s' 0x%llx 2>/dev/null", module->path, + static_cast(pc - module->bias)); + auto *pipe = popen(command, "r"); + if (!pipe) + { + return; + } + + char result[600]; + if (fgets(result, sizeof(result), pipe)) + { + // file:line, or file:line (discriminator N); ??:0 or ??:? without one + result[strcspn(result, "\r\n")] = '\0'; + auto *discriminator = strstr(result, " (discriminator"); + if (discriminator) + { + *discriminator = '\0'; + } + + auto *colon = strrchr(result, ':'); + if (colon && result[0] != '?' && colon[1] >= '1' && colon[1] <= '9') + { + *colon = '\0'; + snprintf(name.file, sizeof(name.file), "%s", result); + name.line = static_cast(strtoul(colon + 1, nullptr, 10)); + } + } + + pclose(pipe); +} + +#endif + +// A frame with no name: its module and the offset in it (what a symbolizer takes), or its address +inline void sayUnnamedFrame(FILE *out, uintptr_t pc, uintptr_t lookup) +{ +#ifdef _WIN32 + if (auto module = moduleOf(lookup)) + { + char path[MAX_PATH]; + auto length = GetModuleFileNameA(module, path, MAX_PATH); + fprintf(out, " at %s+0x%llx\n", length > 0 && length < MAX_PATH ? baseName(path) : "?", + static_cast(pc - reinterpret_cast(module))); + return; + } +#else + if (auto *module = moduleOf(lookup)) + { + auto *name = strrchr(module->path, '/'); + fprintf(out, " at %s+0x%llx\n", name ? name + 1 : module->path, + static_cast(pc - module->bias)); + return; + } +#endif + + fprintf(out, " at 0x%llx\n", static_cast(pc)); +} + +struct Frame +{ + uintptr_t pc; + FrameKind kind; + bool named; + bool isMain; + UncaughtFrameName name; +}; + +} // namespace uncaught_stack + +// Where the trace is said from +struct UncaughtStackOptions +{ + // The JIT's own module, whose frames are left out and end the trace: on Windows its HMODULE, on Linux + // any non-null for the executable + const void *host = nullptr; + // Windows: _CxxThrowException as the program calls it - it may be in the program, from a static C runtime + const void *throwFunction = nullptr; + // Windows: the program's main, after which the C runtime's startup is left out even with no names + const void *mainFunction = nullptr; + // names the JIT's code + UncaughtFrameNamer namer = nullptr; +}; + +// Says the frames under "uncaught exception: ...". `pcs` are as walked from the throw (the first one is +// the throw's own pc, the others return addresses). +inline void sayUncaughtStack(FILE *out, const uintptr_t *pcs, int count, const UncaughtStackOptions &options) +{ + using namespace uncaught_stack; + + static Frame frames[uncaughtStackMaxFrames]; + if (count > uncaughtStackMaxFrames) + { + count = uncaughtStackMaxFrames; + } + + for (auto i = 0; i < count; i++) + { + auto &frame = frames[i]; + memset(&frame, 0, sizeof(frame)); + frame.pc = pcs[i]; + // a return address is the instruction after the call: the call is the one before it + auto lookup = i == 0 ? frame.pc : frame.pc - 1; +#ifdef _WIN32 + auto *module = moduleOf(lookup); + if (module && module == static_cast(const_cast(options.host))) + { + frame.kind = FrameKind::Host; + } + else if (module && isSystemModule(module)) + { + frame.kind = FrameKind::System; + } + else if (inFunction(lookup, options.throwFunction)) + { + frame.kind = FrameKind::System; + } + else if (module) + { + frame.isMain = inFunction(lookup, options.mainFunction); + frame.named = nameFrame(lookup, frame.name); + // a C runtime of the program's own (--jit-native's DLL): its throw + if (frame.named && (strcmp(frame.name.function, "_CxxThrowException") == 0 || + strcmp(frame.name.function, "CxxThrowException") == 0)) + { + frame.kind = FrameKind::System; + } + } + else if (options.namer) + { + frame.named = options.namer(lookup, frame.name); + } +#else + auto isProgram = false; + auto *module = moduleOf(lookup, &isProgram); + if (module && options.host && isProgram) + { + frame.kind = FrameKind::Host; + } + else if (module && isSystemModule(module)) + { + frame.kind = FrameKind::System; + } + else if (module) + { + if (auto *function = functionAt(module, lookup)) + { + frame.named = true; + snprintf(frame.name.function, sizeof(frame.name.function), "%s", function); + lineAt(module, lookup, frame.name); + } + } + else if (options.namer) + { + frame.named = options.namer(lookup, frame.name); + } +#endif + if (frame.named && strcmp(frame.name.function, "main") == 0) + { + frame.isMain = true; + } + } + + // The throw, and what said it: on Windows the frames from RaiseException to _CxxThrowException; on Linux + // this handler, std::terminate() and __cxa_throw (in the C++ runtime, or in tslang's own when it has + // one linked in), after which the program's frames start. + auto first = 0; +#ifndef _WIN32 + while (first < count && frames[first].kind != FrameKind::System) + { + first++; + } + + if (first == count) + { + first = 0; + } +#endif + while (first < count && frames[first].kind != FrameKind::Program) + { + first++; + } + + auto said = 0; + auto namedAny = false; + for (auto i = first; i < count; i++) + { + auto &frame = frames[i]; + if (frame.kind == FrameKind::Host) + { + break; + } + + if (frame.kind == FrameKind::System) + { + continue; + } + + said++; + if (frame.named) + { + namedAny = true; + if (frame.name.file[0]) + { + fprintf(out, " at %s (%s:%u)\n", frame.name.function, frame.name.file, frame.name.line); + } + else + { + fprintf(out, " at %s\n", frame.name.function); + } + } + else + { + sayUnnamedFrame(out, frame.pc, i == 0 ? frame.pc : frame.pc - 1); + } + + // the C runtime's startup, after it, is not the program's + if (frame.isMain) + { + break; + } + } + + if (said > 0 && !namedAny) + { + fprintf(out, " (compile with --di for function names and source lines)\n"); + } +} + +} // namespace typescript + +#endif // TSLANG_DESCRIBES_UNCAUGHT + +#endif // TYPESCRIPT_UNCAUGHTSTACKTRACE_H_ diff --git a/tslang/lib/TypeScriptAsyncRuntime/UncaughtException.cpp b/tslang/lib/TypeScriptAsyncRuntime/UncaughtException.cpp index 0219db529..18001b5d4 100644 --- a/tslang/lib/TypeScriptAsyncRuntime/UncaughtException.cpp +++ b/tslang/lib/TypeScriptAsyncRuntime/UncaughtException.cpp @@ -2,12 +2,12 @@ // program ends with exit code 1. Left to the C++ runtime, the program ended in abort() - on Windows with // exit code 3 (or 0xC0000409, a fast fail) and not a word, on Linux with libstdc++'s "terminate called after // throwing an instance of 'char const*'" and SIGABRT - and what the program had printed, still in stdout's -// buffer, was lost. +// buffer, was lost. Under it, the frames the exception was thrown from (TypeScript/UncaughtStackTrace.h). // // An executable tslang links takes this file with `-include:__tslang_report_uncaught` (Windows) or // `-u __tslang_report_uncaught` (Linux), from tslang/exe.cpp; its initializer sets the handler. -#include "TypeScript/UncaughtException.h" +#include "TypeScript/UncaughtStackTrace.h" #ifdef TSLANG_DESCRIBES_UNCAUGHT @@ -17,10 +17,23 @@ #include #endif +#ifdef _WIN32 +// _CxxThrowException, which the compiler declares, is the throw as the program calls it: from +// vcruntime140.dll, or a copy of its own (a static C runtime). +// +// The program's main, by another name: C++ may not name main itself. Only an executable links this file. +extern "C" void tslangProgramMain(); +#ifdef _M_IX86 +#pragma comment(linker, "/alternatename:_tslangProgramMain=_main") +#else +#pragma comment(linker, "/alternatename:tslangProgramMain=main") +#endif +#endif + namespace { -void sayUncaught(const char *description) +void sayUncaught(const char *description, const uintptr_t *pcs, int count) { fflush(stdout); if (description[0]) @@ -32,12 +45,18 @@ void sayUncaught(const char *description) fprintf(stderr, "error: uncaught exception\n"); } + typescript::UncaughtStackOptions options; +#ifdef _WIN32 + options.throwFunction = reinterpret_cast(&_CxxThrowException); + options.mainFunction = reinterpret_cast(&tslangProgramMain); +#endif + typescript::sayUncaughtStack(stderr, pcs, count, options); fflush(stderr); } #ifdef _WIN32 -constexpr DWORD cxxExceptionCode = 0xE06D7363; // 'msc' | 0xE0000000, a throw +using typescript::uncaught_stack::cxxExceptionCode; LPTOP_LEVEL_EXCEPTION_FILTER runtimeFilter; @@ -50,9 +69,13 @@ LONG WINAPI reportUncaught(EXCEPTION_POINTERS *pointers) return runtimeFilter ? runtimeFilter(pointers) : EXCEPTION_CONTINUE_SEARCH; } + // the first throw of it, when this is a rethrow from a `finally` + auto *thrown = typescript::uncaught_stack::thrownFrom(pointers); char description[512]; - typescript::describeUncaught(pointers, description, sizeof(description)); - sayUncaught(description); + typescript::describeUncaught(thrown, description, sizeof(description)); + uintptr_t pcs[typescript::uncaughtStackMaxFrames]; + auto count = typescript::uncaught_stack::walk(thrown->ContextRecord, pcs, typescript::uncaughtStackMaxFrames); + sayUncaught(description, pcs, count); // as the JIT does: nothing of the program's teardown runs on a thread an exception has left half done TerminateProcess(GetCurrentProcess(), 1); @@ -62,6 +85,7 @@ LONG WINAPI reportUncaught(EXCEPTION_POINTERS *pointers) int installReportUncaught() { runtimeFilter = SetUnhandledExceptionFilter(reportUncaught); + typescript::uncaught_stack::noteThrows(); return 1; } @@ -83,7 +107,10 @@ std::terminate_handler runtimeHandler; abort(); } - sayUncaught(description); + // where the throw found no catch: still on the stack, under this handler and std::terminate() + uintptr_t pcs[typescript::uncaughtStackMaxFrames]; + auto count = typescript::uncaught_stack::walk(pcs, typescript::uncaughtStackMaxFrames); + sayUncaught(description, pcs, count); // nothing of the program's teardown runs on a thread an exception has left half done _exit(1); diff --git a/tslang/runtime/CMakeLists.txt b/tslang/runtime/CMakeLists.txt index 620e39ea4..91714cb6b 100644 --- a/tslang/runtime/CMakeLists.txt +++ b/tslang/runtime/CMakeLists.txt @@ -23,12 +23,15 @@ endif() set(SRC "${CMAKE_CURRENT_SOURCE_DIR}/../lib/TypeScriptAsyncRuntime") # The same sources as lib/TypeScriptAsyncRuntime/CMakeLists.txt: without AsyncGCThreads.cpp a `gc` -# program using async does not link (GC_enable_threads); ProcessHeap.cpp is empty off Windows. +# program using async does not link (GC_enable_threads); ProcessHeap.cpp is empty off Windows. Without +# UncaughtException.cpp an x86 program that threw what nothing caught ended in abort()'s message box, +# which an unattended run waits on forever (exe.cpp takes the file only when the runtime defines it). add_library(TypeScriptAsyncRuntime STATIC "${SRC}/AsyncRuntime.cpp" "${SRC}/AsyncGCThreads.cpp" "${SRC}/ProcessHeap.cpp" - "${SRC}/DynamicRuntime.cpp") + "${SRC}/DynamicRuntime.cpp" + "${SRC}/UncaughtException.cpp") target_include_directories(TypeScriptAsyncRuntime PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../include" "${CMAKE_CURRENT_SOURCE_DIR}/../lib" diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 0eec523ba..83f07a56a 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -6152,6 +6152,90 @@ if (WIN32 OR (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT ANDROID)) endforeach() endforeach() endforeach() + + # Under the error, the frames it was thrown from - from the throw, not from the `finally` blocks that + # throw it again (TypeScript/UncaughtStackTrace.h). The JIT names its functions, with no lines. + set(uncaught_trace_source "${PROJECT_SOURCE_DIR}/test/tester/uncaught/throw_through_finally.ts") + set(uncaught_trace_names "at fail[^a]*at inner[^a]*at outer[^a]*at main") + foreach(uncaught_model gc rc own) + add_test(NAME test-jit-${uncaught_model}-uncaught-trace + COMMAND $ --emit=jit --no-default-lib -mm=${uncaught_model} --opt_level=0 + --shared-libs=$ "${uncaught_trace_source}") + set_tests_properties(test-jit-${uncaught_model}-uncaught-trace + PROPERTIES PASS_REGULAR_EXPRESSION "uncaught exception: Failure[^a]*${uncaught_trace_names}" + FAIL_REGULAR_EXPRESSION "Stack dump|PLEASE submit") + endforeach() + + # An executable: with --di its functions and lines. Without it, on Windows, where there is no PDB, the + # module and offset of each frame and a word on --di; on Linux the symbol table names the functions. + set(uncaught_trace_lines "at fail \\([^)]*throw_through_finally.ts:8\\)[^a]*at inner \\([^)]*throw_through_finally.ts:13\\)[^a]*at outer \\([^)]*throw_through_finally.ts:21\\)[^a]*at main \\([^)]*throw_through_finally.ts:29\\)") + if (WIN32) + set(uncaught_trace_expected_nodi "at program\\.exe\\+0x[0-9a-f]+.*\\(compile with --di for function names and source lines\\)") + set(uncaught_trace_expected_di "${uncaught_trace_lines}") + else() + set(uncaught_trace_expected_nodi "${uncaught_trace_names}") + # the lines come from addr2line, when it is installed + if (EXISTS /usr/bin/addr2line) + set(uncaught_trace_expected_di "${uncaught_trace_lines}") + else() + set(uncaught_trace_expected_di "${uncaught_trace_names}") + endif() + endif() + + foreach(uncaught_model gc rc own) + foreach(uncaught_di nodi di) + if (uncaught_di STREQUAL "di") + set(uncaught_opt_flags "--opt_level=0;--di") + else() + set(uncaught_opt_flags "--opt_level=0") + endif() + set(uncaught_test test-compile-${uncaught_model}-${uncaught_di}-uncaught-trace) + add_test(NAME ${uncaught_test} + COMMAND ${CMAKE_COMMAND} + "-DTSLANG=$" + "-DSOURCE=${uncaught_trace_source}" + "-DMODEL=${uncaught_model}" + "-DEXPECT=error: uncaught exception: Failure" + "-DEXPECT_TRACE=${uncaught_trace_expected_${uncaught_di}}" + "-DWORK_DIR=${CMAKE_CURRENT_BINARY_DIR}/${uncaught_test}" + "-DGC_LIB=${TEST_GC_LIBDIR}" + "-DLLVM_LIB=${LLVM_LIBRARY_DIR}" + "-DTSLANG_LIB=${CMAKE_BINARY_DIR}/lib" + "-DOPT=${uncaught_opt_flags}" + -P "${CMAKE_CURRENT_SOURCE_DIR}/uncaught-exe.cmake") + endforeach() + endforeach() + + # A 32-bit program too (TSLANG_TEST_X86): its runtime once had no UncaughtException.cpp, and the + # program ended in abort()'s message box, which an unattended run waits on forever. Its frames are + # walked by their frame pointers, which tslang keeps in every function for x86. + if (TSLANG_TEST_X86) + get_filename_component(uncaught_x86_tslang_lib "${TEST_TSLANG_LIBPATH_X86}" DIRECTORY) + foreach(uncaught_model gc rc own) + foreach(uncaught_di nodi di) + if (uncaught_di STREQUAL "di") + set(uncaught_opt_flags "-mtriple=i686-pc-windows-msvc;--opt_level=0;--di") + else() + set(uncaught_opt_flags "-mtriple=i686-pc-windows-msvc;--opt_level=0") + endif() + set(uncaught_test test-x86-compile-${uncaught_model}-${uncaught_di}-uncaught-trace) + add_test(NAME ${uncaught_test} + COMMAND ${CMAKE_COMMAND} + "-DTSLANG=$" + "-DSOURCE=${uncaught_trace_source}" + "-DMODEL=${uncaught_model}" + "-DEXPECT=error: uncaught exception: Failure" + "-DEXPECT_TRACE=${uncaught_trace_expected_${uncaught_di}}" + "-DWORK_DIR=${CMAKE_CURRENT_BINARY_DIR}/${uncaught_test}" + "-DGC_LIB=${TEST_GC_LIBDIR}" + "-DLLVM_LIB=${LLVM_LIBRARY_DIR}" + "-DTSLANG_LIB=${uncaught_x86_tslang_lib}" + "-DOPT=${uncaught_opt_flags}" + -P "${CMAKE_CURRENT_SOURCE_DIR}/uncaught-exe.cmake") + set_tests_properties(${uncaught_test} PROPERTIES LABELS x86 TIMEOUT 60) + endforeach() + endforeach() + endif() endif() # Stack promotion (docs/superpowers/specs/2026-10-18-stack-promotion-design.md). Run tests: every diff --git a/tslang/test/tester/uncaught-exe.cmake b/tslang/test/tester/uncaught-exe.cmake index edb075dd8..f2aec7767 100644 --- a/tslang/test/tester/uncaught-exe.cmake +++ b/tslang/test/tester/uncaught-exe.cmake @@ -43,4 +43,9 @@ if(NOT status EQUAL 1 OR NOT out MATCHES "^before\r?\n" OR NOT err MATCHES "${EX "stderr '${err}' (expected '${EXPECT}')") endif() +# and, when given, the frames it was thrown from (TypeScript/UncaughtStackTrace.h) +if(DEFINED EXPECT_TRACE AND NOT err MATCHES "${EXPECT_TRACE}") + message(FATAL_ERROR "the program's stack trace: stderr '${err}' (expected '${EXPECT_TRACE}')") +endif() + message(STATUS "${err}") diff --git a/tslang/test/tester/uncaught/throw_through_finally.ts b/tslang/test/tester/uncaught/throw_through_finally.ts new file mode 100644 index 000000000..03e6bc94c --- /dev/null +++ b/tslang/test/tester/uncaught/throw_through_finally.ts @@ -0,0 +1,30 @@ +// nothing catches it: the trace under the error starts where it was thrown, through the `finally` blocks +// that throw it again +class Failure { + constructor(public message: string) {} +} + +function fail() { + throw new Failure("not caught"); +} + +function inner() { + try { + fail(); + } finally { + print("inner finally"); + } +} + +function outer() { + try { + inner(); + } finally { + print("outer finally"); + } +} + +function main() { + print("before"); + outer(); +} diff --git a/tslang/tslang/jit.cpp b/tslang/tslang/jit.cpp index edbbf3b1c..bc3e40315 100644 --- a/tslang/tslang/jit.cpp +++ b/tslang/tslang/jit.cpp @@ -44,7 +44,9 @@ #include "llvm/Support/ManagedStatic.h" #include "TypeScript/Defines.h" -#include "TypeScript/UncaughtException.h" +#include "TypeScript/UncaughtStackTrace.h" + +#include "llvm/Object/SymbolSize.h" #include "jitcache.h" @@ -660,9 +662,115 @@ static void *jitSprintfS() using JitEntryThunkFn = int (*)(int, char **); +#ifdef TSLANG_DESCRIBES_UNCAUGHT +// The JIT'd functions, by where they were loaded: no module holds them, so an uncaught exception's trace +// (TypeScript/UncaughtStackTrace.h) has their names from here. No source lines: the in-memory JIT's are +// not read (--jit-native's DLL has them, from its PDB). +class JitFrameNames : public llvm::JITEventListener +{ + struct Function + { + uint64_t start; + uint64_t size; + std::string name; + }; + + std::vector functions; + + template static bool take(llvm::Expected expected, T &value) + { + if (!expected) + { + llvm::consumeError(expected.takeError()); + return false; + } + + value = *expected; + return true; + } + + public: + void notifyObjectLoaded(ObjectKey, const llvm::object::ObjectFile &object, + const llvm::RuntimeDyld::LoadedObjectInfo &loaded) override + { + for (auto &[symbol, size] : llvm::object::computeSymbolSizes(object)) + { + llvm::object::SymbolRef::Type type; + llvm::StringRef name; + llvm::object::section_iterator section = object.section_end(); + uint64_t address; + if (!take(symbol.getType(), type) || type != llvm::object::SymbolRef::ST_Function || + !take(symbol.getName(), name) || !take(symbol.getSection(), section) || + section == object.section_end() || !take(symbol.getAddress(), address) || size == 0) + { + continue; + } + + if (auto load = loaded.getSectionLoadAddress(*section)) + { + functions.push_back({load + (address - section->getAddress()), size, name.str()}); + } + } + } + + bool name(uintptr_t pc, typescript::UncaughtFrameName &frameName) const + { + for (auto &function : functions) + { + if (pc >= function.start && pc < function.start + function.size) + { + snprintf(frameName.function, sizeof(frameName.function), "%s", function.name.c_str()); + return true; + } + } + + return false; + } +}; + +static JitFrameNames jitFrameNames; + +static bool nameJitFrame(uintptr_t pc, typescript::UncaughtFrameName &name) +{ + return jitFrameNames.name(pc, name); +} + +// The trace under "uncaught exception: ...": the program's frames, which end where tslang called it +static void sayUncaughtJitStack(const uintptr_t *pcs, int count) +{ + typescript::UncaughtStackOptions options; +#ifdef _WIN32 + options.host = GetModuleHandleW(nullptr); +#else + options.host = &jitFrameNames; // the executable +#endif + options.namer = nameJitFrame; + typescript::sayUncaughtStack(stderr, pcs, count, options); + fflush(stderr); +} +#endif + #ifdef _WIN32 -// What an uncaught exception threw, said after "uncaught exception: ", or empty +// What an uncaught exception threw, said after "uncaught exception: ", or empty; and where it was thrown static char uncaughtDescription[512]; +static uintptr_t uncaughtPcs[typescript::uncaughtStackMaxFrames]; +static int uncaughtPcCount; + +// In the filter, while the throw is still on the stack (the first throw, for a rethrow from a `finally`) +static int noteUncaughtInJit(EXCEPTION_POINTERS *pointers) +{ + auto *thrown = typescript::uncaught_stack::thrownFrom(pointers); + typescript::describeUncaught(thrown, uncaughtDescription, sizeof(uncaughtDescription)); + uncaughtPcCount = typescript::uncaught_stack::walk(thrown->ContextRecord, uncaughtPcs, typescript::uncaughtStackMaxFrames); + return EXCEPTION_EXECUTE_HANDLER; +} + +// so that a rethrow from a `finally` is traced from the first throw (thrownFrom) +static void noteThrowsOnce() +{ + static auto noting = (typescript::uncaught_stack::noteThrows(), true); + (void)noting; +} #elif defined(TSLANG_DESCRIBES_UNCAUGHT) static std::terminate_handler runtimeTerminateHandler; @@ -691,6 +799,11 @@ static std::terminate_handler runtimeTerminateHandler; os << "\n"; os.flush(); + + // where the throw found no catch: still on the stack, under this handler and std::terminate() + uintptr_t pcs[typescript::uncaughtStackMaxFrames]; + auto count = typescript::uncaught_stack::walk(pcs, typescript::uncaughtStackMaxFrames); + sayUncaughtJitStack(pcs, count); _exit(1); } #endif @@ -703,13 +816,14 @@ static std::terminate_handler runtimeTerminateHandler; static bool callEntryThunk(JitEntryThunkFn entryThunk, int argc, char **argv, int &exitCode) { #ifdef _WIN32 - constexpr DWORD cxxExceptionCode = 0xE06D7363; // 'msc' | 0xE0000000 + using typescript::uncaught_stack::cxxExceptionCode; + noteThrowsOnce(); __try { exitCode = entryThunk(argc, argv); return true; } - __except (GetExceptionCode() == cxxExceptionCode ? (typescript::describeUncaught(GetExceptionInformation(), uncaughtDescription, sizeof(uncaughtDescription)), EXCEPTION_EXECUTE_HANDLER) + __except (GetExceptionCode() == cxxExceptionCode ? noteUncaughtInJit(GetExceptionInformation()) : EXCEPTION_CONTINUE_SEARCH) { return false; @@ -1065,6 +1179,9 @@ static std::unique_ptr createJit(llvm::orc::JITTargetMachineBu auto objectLayer = std::make_unique( session, [](const llvm::MemoryBuffer &) { return std::make_unique(); }); +#ifdef TSLANG_DESCRIBES_UNCAUGHT + objectLayer->registerJITEventListener(jitFrameNames); +#endif if (!enableOpt.getValue()) { if (auto *gdbListener = llvm::JITEventListener::createGDBRegistrationListener()) @@ -1265,6 +1382,10 @@ static int runProgram(ProgramSymbolLookup lookup, llvm::ArrayRef in } #endif os << "\n"; + os.flush(); +#ifdef _WIN32 + sayUncaughtJitStack(uncaughtPcs, uncaughtPcCount); +#endif exitCode = 1; } diff --git a/tslang/tslang/obj.cpp b/tslang/tslang/obj.cpp index a804194b7..ae11dad8a 100644 --- a/tslang/tslang/obj.cpp +++ b/tslang/tslang/obj.cpp @@ -448,6 +448,22 @@ int dumpObjOrAssembly(int argc, char **argv, enum Action emitAction, std::string return retCode; } + // 32-bit Windows: every function keeps its frame pointer. An uncaught exception's trace walks the chain + // of them (TypeScript/UncaughtStackTrace.h) - x86 has no unwind tables - and without one in a function + // its caller was missing from the trace; debuggers and profilers walk it too. + if (auto triple = llvm::Triple(llvmModule->getTargetTriple()); + triple.getArch() == llvm::Triple::x86 && triple.isOSWindows()) + { + llvmModule->setFramePointer(llvm::FramePointerKind::All); + for (auto &function : *llvmModule) + { + if (!function.isDeclaration()) + { + function.addFnAttr("frame-pointer", "all"); + } + } + } + auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, compileOptions, Target.get()); if (auto err = optPipeline(llvmModule.get())) {