amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

6.2 KiB

Service contract

Platform policy

SD_OK is 0 and SD_EINVAL is 22. All sizes and revision values use unsigned 64-bit integers. SD_LIMIT is 1099511627776. The service does not depend on the machine's actual pthread defaults.

sd_port_make(out, minimum, quantum, ceiling, has_default, default_bytes) constructs a policy. out is required. has_default is 0 or 1. minimum, quantum, and ceiling are positive, minimum <= ceiling, quantum <= ceiling, and ceiling <= SD_LIMIT.

For a requested size in [minimum, ceiling], the accepted size is the smallest multiple of quantum at least as large as the request. If that multiple exceeds ceiling, the request is invalid. Calculations must reject unrepresentable results without wrapping.

When has_default is 0, default_bytes is ignored and the default is the accepted size for minimum. When has_default is 1, default_bytes is a requested size under the same rules. A supplied zero is invalid. If the default cannot be accepted, construction returns SD_EINVAL and preserves every byte of out. Successful construction returns SD_OK; its default is positive.

sd_port_round(port, bytes, out) returns the accepted size. sd_port_default(port, out) returns the captured default. Both require a constructed policy and a nonnull result pointer; success writes the result and returns SD_OK. Invalid arguments return SD_EINVAL and preserve the result. These functions preserve every byte of their policy input.

Attribute lifecycle

An all-zero sd_port or sd_attr is inactive. Object storage may be overwritten by successful construction or initialization. Only successful APIs create live objects; callers do not alter fields of live objects. Every API returns SD_EINVAL for a required null pointer or inactive source. Failures preserve every byte of all objects and all nonnull output arguments. Const inputs are preserved over their entire object spans. Objects in separate contexts have no shared mutable state. Argument objects and result storage do not overlap, except for the documented self-copy.

sd_attr_init(out, port) captures a complete policy snapshot, sets its size to that policy's default, revision to 1, and explicit flag to 0. Later policy reconstruction does not change existing attributes. Initialization can replace a live attribute.

sd_attr_set(attr, bytes) validates and accepts a request against the attribute's captured policy, records the accepted size, increments revision by one, and sets the explicit flag to 1. An unchanged accepted size still increments revision. sd_attr_reset(attr) restores the captured default, increments revision by one, and sets the flag to 0. A revision that cannot be incremented makes either operation fail without changes.

sd_attr_get(attr, bytes, revision, explicit_size) requires all three result pointers and writes the current values on success. The result pointers name separate storage outside the input object. A read has no side effects. The explicit result is the integer 0 or 1.

sd_attr_copy(out, source) copies the complete captured attribute, including policy, size, revision, and explicit flag. The destination may be inactive or live. Subsequent changes to either object do not affect the other. Copying an object to itself succeeds and preserves every byte. Distinct object arguments do not overlap.

sd_attr_destroy(attr) makes a live attribute all-zero. Destroying an inactive object returns SD_EINVAL. The same storage can then be initialized again.

Decimal helper

sd_number(text, length, out) consumes the complete byte span, with no terminator requirement. It accepts a nonempty sequence of ASCII decimal digits whose value is at most UINT64_MAX; leading zero digits are allowed. Signs, whitespace, embedded NUL bytes, and other byte values are invalid. Null text, null out, empty spans, and overflow return SD_EINVAL. Success returns SD_OK and writes the value. Failure leaves out unchanged. The complete input span remains unchanged. Input and result storage do not overlap.

Console

sd_console() reads stdin and writes responses to stdout, returning 0 after normal EOF, including empty stdin. Empty stdin produces zero bytes on stdout. main invokes this interface. An I/O error returns nonzero. Each invocation starts with eight inactive attribute slots numbered 0 through 7 and a current policy (16384, 4096, 1048576, absent default).

Input consists of byte lines separated by LF. A final nonempty line without LF is also processed. A line of at most 16384 bytes excluding LF is supported. Longer lines produce one invalid response and are drained through LF or EOF. Tokens are separated by ASCII space, HT, or CR; these separators may also precede or follow commands. A blank line is invalid. Decimal tokens follow sd_number rules. The command and token counts are listed below; other byte sequences are invalid.

Command Effect
P minimum quantum ceiling default Reconstruct the current policy; - means no default resource, otherwise the fourth argument is a decimal default request. Existing slots keep their captured policy.
I slot Initialize or replace the slot from the current policy.
S slot bytes Set the slot's requested stack size.
Q slot Read the slot.
C destination source Copy the source attribute to the destination.
R slot Reset the slot to its captured default.
D slot Destroy the slot.

Responses form a stream of JSON objects separated by JSON whitespace; each processed input line contributes one object with integer members rc, size, revision, and explicit. Additional JSON members and ordinary JSON whitespace are allowed. Boolean values are not integer results. For a syntactically valid P, rc is the API result, size is the resulting current default (including its preserved value after failure), and the other results are zero. For a syntactically valid slot command, rc is the API result and the other members describe the resulting destination slot, or are zero if it is inactive. A malformed command, invalid decimal token, extra/missing token, or invalid slot index emits rc=22 and zero for the other members, and changes no state. Errors do not terminate the service.