Skip to content

The first stack is 64 KiB, and a program that outgrows it hangs without a fault #3

Closed

Description

@speak-agent

Observed (openkal-platform 0.28.1, row i686-dos-musl, DOSBox Staging 0.83.0 with CWSDPMI, run through dirun 0.4.0):

A program whose stack grows past 64 KiB does not fault. It hangs until the runner's time limit ends it, and any output it had not yet flushed to a file is lost. A program that ran a few levels of calls with several 4 KiB path buffers on the stack hung on a later, unrelated call (closedir). This first looked like a bug in the batch commands that ran just before that call.

Minimal reproduction

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int deep(int n, int limit) {
    volatile char frame[8192];
    memset((char*)frame, n & 0xff, sizeof frame);
    if (n >= limit) return frame[0];
    return deep(n + 1, limit) + frame[100];
}

int main(int argc, char** argv) {
    int limit = argc > 1 ? atoi(argv[1]) : 64;
    printf("recursing to %d frames of 8 KiB\n", limit);
    fflush(stdout);
    printf("returned %d\n", deep(0, limit) & 1);
    return 0;
}
frames of 8 KiB result
1, 4, 7 returned …, status 0
8, 12, 16, 64 hangs; ended by the runner's time limit (status 124), no fault reported

For comparison, on the same row abort() exits with 134, a detected double free faults with #PF, and __builtin_trap() faults with #UD. Only stack exhaustion goes unreported.

Where it comes from

stub/stub.S sets FIRST_STACK to 0x10000, and the README describes the executable's first stack as 64 KiB, allocated in the same block as the loadable segments. Nothing stops the stack from growing past its end, so an overflow writes into the neighbouring memory of the block rather than faulting.

Suggested

  • A first stack sized like other hosted systems' main-thread stacks (1 MiB, as on Windows, or more), or a size the executable states, for example a note or PT_GNU_STACK's size, read by the stub.
  • Make an overflow a fault rather than silent corruption. For example, place the stack at the low end of the block so that growing past it leaves the descriptor's range, or give the stack its own expand-down descriptor. That way a deep call reports #SS or #PF the way the other faults are reported.

Activity

  1. speak-agent commented on Nov 17, 2026

    @speak-agent
    Author

    Fixed in openkal-dos 0.1.1 (and openkal-dos-tools 0.1.1), in the version group of openkal-platform 0.29.1:

    • The stub sizes the program's first stack from PT_GNU_STACK (lld's -z stack-size); without it the stack is 1 MiB. openkal-platform 0.29.1 links i686-dos-musl with -z stack-size=1048576.
    • Below the stack lies a guard page, uncommitted through DPMI 0507h, so an overflow is a reported #PF rather than silent corruption. Where the host lacks 0507h, a canary at the stack's floor is checked at exit, and a line at start says the guard page is unavailable.

    Measured: openkal-platform's checks/dos runs a recursion 64 deep (the reproduction's 8 KiB frames); an external verification project's whole batch script, the program that first hung here, now runs to the end on i686-dos-musl under DOSBox Staging 0.83.0 through dirun 0.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions