Avoid stack overflows (happened invoking emacs scripts)
diff --git a/Makefile b/Makefile index daf0f3f..8af4002 100644 --- a/Makefile +++ b/Makefile
@@ -11,7 +11,9 @@ OSSRCS=src/win/inject.c src/win/dbg.c LDOBJS=$(ROOT64)\x86_64-w64-mingw32\lib\CRT_noglob.o INSTALLDIR=$(APPDATA)\local\bin - +LDFLAGS=-s +LDFLAGS32=-s +LDFLAGS64=-s else PLAT=unix @@ -19,7 +21,6 @@ LDFLAGS=-g OS=$(shell uname -s) -LS=$(shell which ls) ifeq ($(OS),Linux) LDLIBS=-ldl -lrt @@ -29,7 +30,9 @@ endif -CFLAGS+= -g -std=c99 -Wall -O2 -fomit-frame-pointer -fno-stack-protector -MMD +LS=$(shell which ls) + +CFLAGS+= -std=c99 -Wall -O2 -fomit-frame-pointer -fno-stack-protector -MMD SRCS=src/fsatrace.c src/$(PLAT)/proc.c src/$(PLAT)/shm.c $(OSSRCS)
diff --git a/src/fsatrace.c b/src/fsatrace.c index 82bccc8..a558428 100644 --- a/src/fsatrace.c +++ b/src/fsatrace.c
@@ -95,6 +95,20 @@ uniq(d, tot, end + 1, last, lastsz); } +static void +workaround(const char *oenv, size_t buf_size) +{ +#ifdef _WIN32 + // Workaround, bash distributed with ghc 8.6.5 seems to discard + // most environment variables, pass environment variables as the + // first few PATH components. + char env[65536]; + snprintf(env, sizeof(env), "PATH=%s;%ld;%s", oenv, (long)buf_size, + getenv("PATH")); + putenv(env); +#endif +} + int main(int argc, char *const argv[]) { @@ -141,17 +155,7 @@ snprintf( envbufsize, sizeof(envbufsize), ENVBUFSIZE "=%ld", (long)buf_size); putenv(envbufsize); -#ifdef _WIN32 - { - // Workaround, bash distributed with ghc 8.6.5 seems to discard - // most environment variables, pass environment variables as the - // first few PATH components. - char env[65536]; - snprintf(env, sizeof(env), "PATH=%s;%ld;%s", out, - (long)buf_size, getenv("PATH")); - putenv(env); - } -#endif + workaround(out, buf_size); fflush(stdout); opts = (const unsigned char *)argv[1]; bopts = shm.buf + 4;
diff --git a/src/win/fsatracedll.c b/src/win/fsatracedll.c index c911608..bab324e 100644 --- a/src/win/fsatracedll.c +++ b/src/win/fsatracedll.c
@@ -18,6 +18,34 @@ return ret; } +static void +dlldeps() +{ + // DLLs that were loaded before we got hooked, so mark + // them as a read dependency + DWORD cb = 0; + wchar_t winBuf[PATH_MAX]; + char utfBuf[PATH_MAX]; + HANDLE hProcess = GetCurrentProcess(); + HMODULE modules[8000]; // Pick a huge value to make sure + // we pick up everything + if (EnumProcessModules(hProcess, modules, sizeof(modules), &cb)) { + // If the buffer we passed was too small, just + // ignore it + if (sizeof(modules) >= cb) { + for (int i = 0; i < cb / sizeof(HMODULE); i++) { + DWORD res = GetModuleFileNameExW( + hProcess, modules[i], winBuf, PATH_MAX); + if (res != 0) + emitOp('r', + utf8PathFromWide( + utfBuf, winBuf, res), + 0); + } + } + } +} + INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved) { @@ -26,36 +54,7 @@ switch (Reason) { case DLL_PROCESS_ATTACH: emitInit(); - { - // DLLs that were loaded before we got hooked, so mark - // them as a read dependency - DWORD cb = 0; - wchar_t winBuf[PATH_MAX]; - char utfBuf[PATH_MAX]; - HANDLE hProcess = GetCurrentProcess(); - HMODULE modules[8000]; // Pick a huge value to make sure - // we pick up everything - if (EnumProcessModules( - hProcess, modules, sizeof(modules), &cb)) { - // If the buffer we passed was too small, just - // ignore it - if (sizeof(modules) >= cb) { - for (int i = 0; - i < cb / sizeof(HMODULE); i++) { - DWORD res = - GetModuleFileNameExW( - hProcess, modules[i], - winBuf, PATH_MAX); - if (res != 0) - emitOp('r', - utf8PathFromWide( - utfBuf, winBuf, - res), - 0); - } - } - } - } + dlldeps(); patchInit(); hooksInit(resolve); break;
diff --git a/win.mk b/win.mk index d3dce2e..9e6cf14 100644 --- a/win.mk +++ b/win.mk
@@ -15,10 +15,10 @@ $(CC32) -c $(CPPFLAGS32) $(CFLAGS) -march=i686 $< -o $@ fsatracehelper.exe: $(HELPER_OBJ) - $(CC32) $< -o $@ + $(CC32) $(LDFLAGS) $< -o $@ fsatest32.exe: src/fsatest32.o - $(CC32) $< -o $@ + $(CC32) $(LDFLAGS) $< -o $@ fsatrace64.dll: $(OBJS64) $(CC) -shared $(LDFLAGS64) $^ -o $@ $(LDLIBS)