Skip to content

Commit

Permalink
Avoid stack overflows (happened invoking emacs scripts)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacereda committed Jan 31, 2022
1 parent b663992 commit 9b81c5d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 46 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ CPPFLAGS32=-D_WIN32_WINNT=0x600 -DIS32=1 -isystem$(ROOT32)\i686-w64-mingw32\incl
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
CPPFLAGS=-D_GNU_SOURCE -D_DEFAULT_SOURCE=1
LDFLAGS=-g

OS=$(shell uname -s)
LS=$(shell which ls)

ifeq ($(OS),Linux)
LDLIBS=-ldl -lrt
Expand All @@ -29,7 +30,9 @@ INSTALLDIR=$(HOME)/.local/bin

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)

Expand Down
26 changes: 15 additions & 11 deletions src/fsatrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ uniq(char *d, size_t *tot, const char *s, const char *last, size_t lastsz)
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[])
{
Expand Down Expand Up @@ -141,17 +155,7 @@ main(int argc, char *const argv[])
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;
Expand Down
59 changes: 29 additions & 30 deletions src/win/fsatracedll.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ resolve(const char *name)
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)
{
Expand All @@ -26,36 +54,7 @@ DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
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;
Expand Down
4 changes: 2 additions & 2 deletions win.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ all: fsatest32.exe
$(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)
Expand Down

0 comments on commit 9b81c5d

Please sign in to comment.