Repository navigation
Add an optional stack paint helper. - #27
Draft
kristjanvalur wants to merge 3 commits into
Draft
kristjanvalur wants to merge 3 commits into
kristjanvalur wants to merge 3 commits into
Conversation
stackman_guard.c is plain C and is not part of the switch. It paints a band at the near end of a caller-owned block. The near bound is the overflow side. Extent 0 means that bound is unknown, and the helpers leave memory alone. A stackman_call block knows both ends.
A switch snapshot is the used range between near and far and has no untouched tail. stackman_stack_unused keeps those same coordinates. A malloc (low, size) pair swaps which end it names when the stack direction changes.
The malloc pair becomes far and extent in one place. The paint, the check, and the unused count keep those coordinates.
kristjanvalur
force-pushed
the
stack-guard
branch
from
October 16, 2026 09:30
2fbc829 to
c555bc3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Optional paint of a caller-owned block. Plain C, compiled by the program that wants it, not part of
libstackman, no library calls. The primary use is a pre-allocated block passed tostackman_call. Astackman_switchsnapshot is a different range and is not what this paints.Paint and check
You allocated a block, you are about to run a callback on it, and you want to know if that callback grew into the end of the block. The near end is the overflow side. Paint a band there, make the call, then read the band back. The check does not stop the write. It tells you the block was too small.
stackman_block_callturns the malloc pair intofarandextent.faris the pointer you pass tostackman_call.extentis the byte count from there toward near. Alignment can make that a few bytes shorter than themallocsize. The sliver outsidefaris not part of the stack.stackman_stack_unusedcounts a solid run of the pattern from the near end back towardfar. It is only meaningful after the whole extent was painted and the stack then grew fromfar. A pattern byte the program itself stores counts as untouched.Why this is not the switch snapshot
stackman_switch, as libtealet uses it, saves a slice of the real C stack. The saved range is the bytes already in use: from the near pointer (the stack pointer at save) out to that tealet'sstack_far. A copy of that range has no untouched tail. Painting it and scanning for a pattern does not measure overflow, and it does not measure how large a stack the coroutine needs. Tealet already stores that size on the chunk.The main stack is the case where the extent is genuinely unknown. Its far marker is often
STACKMAN_SP_FURTHEST, a sentinel, not an address you can paint from. The near limit is the operating system's guard, which this library does not ask about. Extent 0 is that input. The helpers then leave memory alone.So the two ranges are:
STACKMAN_SP_DIFF(far, near), and tealet copies those bytes. No paint.stackman_callblock: amallocregion whose far end you choose and whose near end is the end of the allocation. Both ends are known. This is the paint.stackman_stack_unusedstays(far, extent)stackman_block_callis the conversion from a low address and a size. The paint, the check, and the unused count take the pair it returns, the same pointerstackman_calltakes.On a descending stack the low address is the near end. On an ascending stack the low address is the far end. A paint function that took the malloc pair directly would change which end that pointer names when
STACKMAN_STACK_DIRchanges. The helper does that conversion once and drops the alignment sliver.bin/test_guardruns the descending case, including astackman_callon a block obtained fromstackman_block_call.bin/test_guard_uprebuilds the unit withSTACKMAN_STACK_DIR=1, checks the other branch of the address math, and paints a block from the same helper. No supported ABI grows that way. Nativemake testpassed.Closes #14.