lx_loader: don't unmap a segment that holds the live stack
Build / Linux (pull_request) Canceled after 0s

TI's r5.01 gspac and gspcg free their automatic data segment (DGROUP) as
the last thing they do before DosExit. That segment also holds the 16-bit
stack, and the 16->32 bridge is still running on it, so lxFreeSegment's
immediate munmap()+lxFreeSelector() pulled the stack out from under the
bridge: the return path faulted, the process died with SIGSEGV (shell
status 139) before DosExit ran, and gspcl saw every child as failed and
stopped after gspac.

Keep both the mapping and its LDT selector when the segment contains the
current ESP, mirroring freeLxModule's existing "don't unmap the stack
we're probably using" rule. _exit() reclaims the mapping. Verified with
the full r5.01 pipeline: gspac/gspopt/gspcg/gspa/gsplnk/gsprom all exit 0
and produce the same output; error paths now report their real code (1)
instead of 139, and gspcl chains the whole sequence.
This commit is contained in:
arti committed 2026-11-16 17:16:10 +00:00
1 parent 3bcdfc3641
commit cad0c719a0
1 file changed
+22
+22
View File
@@ -730,12 +730,34 @@ static void *lxAllocSegment(uint16 *selector, const int iscode)
return segment;
} // lxAllocSegment
// A segment can be freed while it still holds the stack we are running on:
// TI's r5.01 C tools do exactly that, freeing their automatic data segment
// as the last thing they do before DosExit. OS/2 keeps such a segment valid
// until the process terminates. Unmapping it here would make the 16->32
// bridge fault on its own stack on the way back, so DosExit never runs and
// the process dies with SIGSEGV (shell exit status 139) instead of the
// tool's real exit code. freeLxModule() already has the same rule for the
// process stack at shutdown ("don't unmap the stack we're probably using!").
static int lxSegmentContainsCurrentStack(const uint32 addr)
{
uint32 esp;
__asm__ __volatile__ ("movl %%esp, %0" : "=r" (esp));
return (esp - addr) < 0x10000;
} // lxSegmentContainsCurrentStack
static void lxFreeSegment(const uint16 selector)
{
const uint32 addr32 = GLoaderState.ldt[selector];
if (addr32) {
void *addr = (void *) ((size_t) addr32);
//printf("lxFreeSegment: addr=%p, selector=%x\n", addr, (uint) selector);
if (lxSegmentContainsCurrentStack(addr32)) {
// Keep both the mapping and its LDT selector: we still have to
// return through this stack, and the bridge reloads %ss on the
// way out. _exit() reclaims the mapping when the process ends.
FIXME("segment is still the active stack; deferring free until exit");
return;
} // if
munmap(addr, 0x10000);
lxFreeSelector(selector);
} // if