Digest #244 2023-12-24


[323] 2023-12-22 22:14:01 kumaym wrote:

referenceImplementation - stack checking

Is there any standard about checking for stack underflow when using the word DROP (and similar ones)? I'd also like to know if there's overflow checking in words like DUP and similar


[r1147] 2023-12-23 08:40:48 AntonErtl replies:

referenceImplementation - stack checking

The standard mentions stack overflow and underflow as ambiguous conditions in section 4.1.2. Therefore, standard programs must not overflow or underflow these stacks (and I expect one can find related wording for other stacks). Standard systems can do anything if they do.

Many Forth systems do not check for stack overflow or underflow, or check only the stack depth in the text interpreter (i.e., there may have been a temporary underflow in the meantime, which is not reported); the over- or underflow can overwrite other data on these systems.

One exception is the gforth debugging engine on an OS with mmap() (e.g., Linux); there the stacks are bounded by unaccessible pages, and every stack underflow or overflow leads to an appropriate exception. Note that the engine gforth-fast does not report most underflows, but the stacks are still surrounded by unaccessible pages, which prevents data corruption on underflow.