Codex 69521e54e0 Add 15 embedded repair tasks and correct grader-read controls
Complete batch 20261016-C-embedded-2: 45 verified GLM/Pi samples, independent regrades and archives; preserve difficulty misses and Salty blockage.
2026-10-16 14:23:48 +08:00

23 lines
20 KiB
JSON
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"kind": "transfer",
"seed": 1347374156,
"context": {
"text.h": "#ifndef ABI_TEXT_H\n#define ABI_TEXT_H\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n\nenum { TEXT_LINE = 16384, TEXT_WORDS = 32, TEXT_BYTES = 4096 };\n\ntypedef struct {\n char line[TEXT_LINE];\n char *word[TEXT_WORDS];\n size_t count;\n unsigned long number;\n int failed;\n} text_reader;\n\nvoid text_init(text_reader *reader);\nint text_next(text_reader *reader, FILE *stream);\nint text_count(text_reader *reader, size_t expected);\nint text_u64(const char *word, uint64_t maximum, uint64_t *value);\nint text_i64(const char *word, int64_t minimum, int64_t maximum, int64_t *value);\nint text_unsigned(text_reader *reader, size_t index, unsigned maximum, unsigned *value);\nint text_signed(text_reader *reader, size_t index, int minimum, int maximum, int *value);\nint text_bytes(const char *word, uint8_t *data, size_t capacity, size_t *length);\nvoid text_hex(FILE *stream, const uint8_t *data, size_t length);\nuint32_t text_hash(const uint8_t *data, size_t length);\nvoid text_error(const text_reader *reader, const char *message);\n#endif\n",
"text.c": "#include \"text.h\"\n#include <errno.h>\n#include <inttypes.h>\n#include <limits.h>\n#include <stdlib.h>\n#include <string.h>\n\nstatic int separator(unsigned char ch)\n{\n return ch == ' ' || ch == '\\t' || ch == '\\r' || ch == '\\n';\n}\n\nvoid text_init(text_reader *reader)\n{\n memset(reader, 0, sizeof *reader);\n}\n\nstatic int split_line(text_reader *reader)\n{\n char *cursor = reader->line;\n reader->count = 0;\n while (*cursor != '\\0') {\n while (separator((unsigned char)*cursor))\n ++cursor;\n if (*cursor == '\\0' || *cursor == '#')\n break;\n if (reader->count == TEXT_WORDS)\n return 0;\n reader->word[reader->count++] = cursor;\n while (*cursor != '\\0' && !separator((unsigned char)*cursor))\n ++cursor;\n if (*cursor != '\\0')\n *cursor++ = '\\0';\n }\n return 1;\n}\n\nint text_next(text_reader *reader, FILE *stream)\n{\n while (fgets(reader->line, sizeof reader->line, stream) != NULL) {\n ++reader->number;\n size_t length = strlen(reader->line);\n if (length == sizeof reader->line - 1 && reader->line[length - 1] != '\\n') {\n reader->failed = 1;\n return -1;\n }\n if (!split_line(reader)) {\n reader->failed = 1;\n return -1;\n }\n if (reader->count != 0)\n return 1;\n }\n if (ferror(stream)) {\n reader->failed = 1;\n return -1;\n }\n return 0;\n}\n\nint text_count(text_reader *reader, size_t expected)\n{\n if (reader->count == expected)\n return 1;\n reader->failed = 1;\n return 0;\n}\n\nint text_u64(const char *word, uint64_t maximum, uint64_t *value)\n{\n char *end = NULL;\n unsigned long long parsed;\n if (word == NULL || *word == '\\0' || *word == '-')\n return 0;\n errno = 0;\n parsed = strtoull(word, &end, 10);\n if (errno != 0 || end == word || *end != '\\0' || parsed > maximum)\n return 0;\n *value = (uint64_t)parsed;\n return 1;\n}\n\nint text_i64(const char *word, int64_t minimum, int64_t maximum, int64_t *value)\n{\n char *end = NULL;\n long long parsed;\n if (word == NULL || *word == '\\0')\n return 0;\n errno = 0;\n parsed = strtoll(word, &end, 10);\n if (errno != 0 || end == word || *end != '\\0' || parsed < minimum || parsed > maximum)\n return 0;\n *value = (int64_t)parsed;\n return 1;\n}\n\nint text_unsigned(text_reader *reader, size_t index, unsigned maximum, unsigned *value)\n{\n uint64_t parsed;\n if (index >= reader->count || !text_u64(reader->word[index], maximum, &parsed)) {\n reader->failed = 1;\n return 0;\n }\n *value = (unsigned)parsed;\n return 1;\n}\n\nint text_signed(text_reader *reader, size_t index, int minimum, int maximum, int *value)\n{\n int64_t parsed;\n if (index >= reader->count || !text_i64(reader->word[index], minimum, maximum, &parsed)) {\n reader->failed = 1;\n return 0;\n }\n *value = (int)parsed;\n return 1;\n}\n\nstatic int hex_digit(unsigned char ch)\n{\n if (ch >= '0' && ch <= '9')\n return (int)(ch - '0');\n if (ch >= 'a' && ch <= 'f')\n return (int)(ch - 'a') + 10;\n if (ch >= 'A' && ch <= 'F')\n return (int)(ch - 'A') + 10;\n return -1;\n}\n\nint text_bytes(const char *word, uint8_t *data, size_t capacity, size_t *length)\n{\n size_t count;\n *length = 0;\n if (strcmp(word, \"-\") == 0)\n return 1;\n count = strlen(word);\n if ((count & 1u) != 0 || count / 2 > capacity)\n return 0;\n for (size_t index = 0; index < count / 2; ++index) {\n int high = hex_digit((unsigned char)word[index * 2]);\n int low = hex_digit((unsigned char)word[index * 2 + 1]);\n if (high < 0 || low < 0)\n return 0;\n data[index] = (uint8_t)((unsigned)high * 16u + (unsigned)low);\n }\n *length = count / 2;\n return 1;\n}\n\nvoid text_hex(FILE *stream, const uint8_t *data, size_t length)\n{\n if (length == 0) {\n fputc('-', stream);\n return;\n }\n for (size_t index = 0; index < length; ++index)\n fprintf(stream, \"%02x\", data[index]);\n}\n\nuint32_t text_hash(const uint8_t *data, size_t length)\n{\n uint32_t value = UINT32_C(2166136261);\n for (size_t index = 0; index < length; ++index) {\n value ^= data[index];\n value *= UINT32_C(16777619);\n }\n return value;\n}\n\nvoid text_error(const text_reader *reader, const char *message)\n{\n fprintf(stderr, \"input line %lu: %s\\n\", reader->number, message);\n}\n",
"transfer.h": "#ifndef ABI_TRANSFER_H\n#define ABI_TRANSFER_H\n#include <stddef.h>\n#include <stdint.h>\n#include <sys/types.h>\n#include <poll.h>\n\nenum { TRANSFER_CAPACITY = 512 };\n\ntypedef struct {\n int64_t (*clock_call)(void *context);\n ssize_t (*read_call)(int fd, void *bytes, size_t capacity, void *context);\n ssize_t (*write_call)(int fd, const void *bytes, size_t length, void *context);\n int (*poll_call)(int fd, short events, int timeout_ms, short *revents, void *context);\n void *context;\n} transfer_ops;\n\ntypedef struct {\n uint8_t request[TRANSFER_CAPACITY];\n uint8_t response[TRANSFER_CAPACITY];\n size_t request_size;\n size_t response_size;\n size_t sent;\n size_t received;\n int done;\n} transfer;\n\ntypedef struct {\n int64_t expires;\n const transfer_ops *ops;\n} deadline;\n\nint deadline_start(deadline *timer, const transfer_ops *ops, unsigned timeout_ms);\nint deadline_left(deadline *timer, int *milliseconds);\nint deadline_wait(deadline *timer, int fd, short events);\nint transfer_init(transfer *state, const uint8_t *request, size_t size, size_t response_size);\nint transfer_drive(transfer *state, int fd, const transfer_ops *ops, unsigned timeout_ms);\n#endif\n",
"transfer_console.c": "#include \"transfer.h\"\n#include \"text.h\"\n#include <errno.h>\n#include <inttypes.h>\n#include <stdio.h>\n#include <string.h>\n\nenum { TAPE = 1024 };\ntypedef struct {\n int result;\n int error;\n unsigned cost;\n short revents;\n uint8_t bytes[TRANSFER_CAPACITY];\n size_t length;\n size_t offset;\n} action;\n\ntypedef struct {\n action reads[TAPE];\n action writes[TAPE];\n action polls[TAPE];\n unsigned read_begin, read_end;\n unsigned write_begin, write_end;\n unsigned poll_begin, poll_end;\n int64_t now;\n uint8_t sent[65536];\n size_t sent_size;\n transfer state;\n} endpoint;\n\nstatic endpoint banks[2];\n\nstatic int64_t clock_call(void *context)\n{\n endpoint *item = context;\n return item->now;\n}\n\nstatic ssize_t write_call(int fd, const void *bytes, size_t length, void *context)\n{\n endpoint *item = context;\n action fallback = { 512, 0, 1, 0, {0}, 0, 0 };\n action *next = &fallback;\n (void)fd;\n if (item->write_begin < item->write_end)\n next = &item->writes[item->write_begin++];\n item->now += next->cost;\n if (next->error) {\n errno = next->error;\n printf(\"W %zu -1 %d -\\n\", length, errno);\n return -1;\n }\n size_t amount = (size_t)next->result;\n if (amount > length)\n amount = length;\n if (amount > sizeof item->sent - item->sent_size) {\n errno = ENOSPC;\n return -1;\n }\n memcpy(item->sent + item->sent_size, bytes, amount);\n item->sent_size += amount;\n printf(\"W %zu %zu 0 \", length, amount);\n text_hex(stdout, bytes, amount);\n putchar('\\n');\n return (ssize_t)amount;\n}\n\nstatic ssize_t read_call(int fd, void *bytes, size_t capacity, void *context)\n{\n endpoint *item = context;\n (void)fd;\n if (item->read_begin == item->read_end) {\n ++item->now;\n errno = EAGAIN;\n printf(\"R %zu -1 %d -\\n\", capacity, errno);\n return -1;\n }\n action *next = &item->reads[item->read_begin];\n item->now += next->cost;\n if (next->error) {\n ++item->read_begin;\n errno = next->error;\n printf(\"R %zu -1 %d -\\n\", capacity, errno);\n return -1;\n }\n size_t amount = next->length - next->offset;\n if (amount > capacity)\n amount = capacity;\n memcpy(bytes, next->bytes + next->offset, amount);\n next->offset += amount;\n if (next->offset == next->length)\n ++item->read_begin;\n printf(\"R %zu %zu 0 \", capacity, amount);\n text_hex(stdout, bytes, amount);\n putchar('\\n');\n return (ssize_t)amount;\n}\n\nstatic int poll_call(int fd, short events, int timeout, short *revents, void *context)\n{\n endpoint *item = context;\n (void)fd;\n if (item->poll_begin == item->poll_end) {\n item->now += timeout;\n *revents = 0;\n printf(\"P %d %d 0 0 0\\n\", events, timeout);\n return 0;\n }\n action *next = &item->polls[item->poll_begin++];\n item->now += next->cost;\n *revents = next->revents;\n errno = next->error;\n printf(\"P %d %d %d %d %d\\n\", events, timeout,\n next->result, (int)*revents, next->error);\n return next->result;\n}\n\nstatic void show(const endpoint *item)\n{\n printf(\"S %zu %zu %d %\" PRId64 \" \", item->state.sent,\n item->state.received, item->state.done, item->now);\n text_hex(stdout, item->state.response, item->state.received);\n putchar(' ');\n text_hex(stdout, item->sent, item->sent_size);\n printf(\" %u %u %u\\n\", item->write_end - item->write_begin,\n item->read_end - item->read_begin, item->poll_end - item->poll_begin);\n}\n\nstatic int add_write(text_reader *reader, endpoint *item)\n{\n unsigned amount, cost;\n int error;\n if (!text_count(reader, 4) || !text_unsigned(reader, 1, 512, &amount) ||\n !text_signed(reader, 2, 0, 255, &error) ||\n !text_unsigned(reader, 3, 100000, &cost) || item->write_end == TAPE)\n return 0;\n item->writes[item->write_end++] = (action){ (int)amount, error, cost, 0, {0}, 0, 0 };\n return 1;\n}\n\nstatic int add_read(text_reader *reader, endpoint *item)\n{\n unsigned cost;\n int error;\n action next;\n memset(&next, 0, sizeof next);\n if (!text_count(reader, 4) || !text_bytes(reader->word[1], next.bytes,\n sizeof next.bytes, &next.length) ||\n !text_signed(reader, 2, 0, 255, &error) ||\n !text_unsigned(reader, 3, 100000, &cost) || item->read_end == TAPE)\n return 0;\n next.error = error;\n next.cost = cost;\n item->reads[item->read_end++] = next;\n return 1;\n}\n\nstatic int add_poll(text_reader *reader, endpoint *item)\n{\n unsigned cost;\n int result, revents, error;\n if (!text_count(reader, 5) || !text_signed(reader, 1, -1, 1, &result) ||\n !text_signed(reader, 2, 0, 32767, &revents) ||\n !text_signed(reader, 3, 0, 255, &error) ||\n !text_unsigned(reader, 4, 100000, &cost) || item->poll_end == TAPE)\n return 0;\n item->polls[item->poll_end++] = (action){ result, error, cost, (short)revents, {0}, 0, 0 };\n return 1;\n}\n\nint main(void)\n{\n text_reader reader;\n unsigned selected = 0;\n text_init(&reader);\n for (size_t index = 0; index < 2; ++index)\n transfer_init(&banks[index].state, NULL, 0, 0);\n while (text_next(&reader, stdin) > 0) {\n endpoint *item = &banks[selected];\n const char *name = reader.word[0];\n int okay = 0;\n if (strcmp(name, \"A\") == 0) {\n okay = text_count(&reader, 2) && text_unsigned(&reader, 1, 1, &selected);\n item = &banks[selected];\n } else if (strcmp(name, \"I\") == 0) {\n uint8_t bytes[TEXT_BYTES];\n size_t length;\n unsigned response_size;\n okay = text_count(&reader, 3) && text_bytes(reader.word[1], bytes, sizeof bytes, &length) &&\n text_unsigned(&reader, 2, 4096, &response_size);\n if (okay) {\n uint32_t before = text_hash(bytes, length);\n int result = transfer_init(&item->state, length ? bytes : NULL, length, response_size);\n printf(\"I %d %\" PRIu32 \" %\" PRIu32 \"\\n\", result, before, text_hash(bytes, length));\n }\n } else if (strcmp(name, \"C\") == 0) {\n int64_t now;\n okay = text_count(&reader, 2) && text_i64(reader.word[1], 0, 1000000000, &now);\n if (okay)\n item->now = now;\n } else if (strcmp(name, \"W\") == 0) {\n okay = add_write(&reader, item);\n } else if (strcmp(name, \"R\") == 0) {\n okay = add_read(&reader, item);\n } else if (strcmp(name, \"P\") == 0) {\n okay = add_poll(&reader, item);\n } else if (strcmp(name, \"D\") == 0) {\n unsigned timeout;\n okay = text_count(&reader, 2) && text_unsigned(&reader, 1, 1000000, &timeout);\n if (okay) {\n transfer_ops ops = { clock_call, read_call, write_call, poll_call, item };\n printf(\"D %d\\n\", transfer_drive(&item->state, (int)selected + 70, &ops, timeout));\n }\n } else if (strcmp(name, \"Z\") == 0) {\n okay = text_count(&reader, 1);\n if (okay) {\n memset(item, 0, sizeof *item);\n transfer_init(&item->state, NULL, 0, 0);\n }\n }\n if (!okay) {\n text_error(&reader, \"invalid transfer command\");\n return 2;\n }\n show(item);\n }\n return reader.failed ? 2 : 0;\n}\n",
"Makefile": "CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -pedantic\nall: abi_demo\nabi_demo: transfer_console.c text.c deadline.c transfer.c transfer.h text.h\n\t$(CC) $(CFLAGS) transfer_console.c text.c deadline.c transfer.c -o $@\nclean:\n\trm -f abi_demo\n.PHONY: all clean\n",
"README.md": "# 非阻塞命令传输的部分进度与超时\n\n设备代理通过同一非阻塞 fd 先写请求、再读固定长度响应。压力下出现请求尾部消失、重试发送重复前缀,信号密集时一次调用又明显超过总超时。修复现成 C11 工程的传输状态与 deadline;最终只提交并评分 `deadline.c`、`transfer.c`,其余工程是冻结的只读上下文。\n\n## P1:数据与生命周期\n\n`transfer.h` 给出结构和签名。`transfer_init` 接收 0..512 字节 request 和 0..512 的 response_size,复制请求、sent=received=0、清空 response,双方长度均为零则 done=1,否则 done=0。request 长度零时可 NULL;非零 NULL 或任何长度超限返回 `-EINVAL`,state 不变。请求输入不可修改。各 transfer 实例独立;候选不关闭 fd、不修改 ops/context,只通过回调进行 IO。\n\n`transfer_drive` 逐步完成请求和响应,返回 0 表示全部完成;中途返回负 errno 时保留 sent/received 与已收到响应,下次 drive 从同一位置继续,不能重发已成功的请求字节、覆盖已接收的响应或提前接收。response 必须恰为要求的字节数,每次回调的 count/capacity 为该方向剩余字节数,不越界读写。write/read 的正数可能是任意短 IO;read=0 返回 `-EPIPE`,write=0 返回 `-EIO`,其他错误返回负 errno。所有回调齐全,否则 drive 返回 `-EINVAL` 且无副作用;done 时在 ops 有效后直接返回 0,无 IO 或 clock。输入合法时回调正数不会超过请求容量。\n\n## P2:deadline 与 readiness\n\ntimeout_ms 为每次 drive **完整调用**的预算(0..1000000ms),涵盖所有短 IO、EAGAIN、poll 和 EINTR,不能每次等待重新计时。clock_call 返回非负单调毫秒,失败 -1/errno。clock EINTR 原参数重试,其他 clock 错误返回负 errno;测试时钟不会溢出 int64。`deadline_start` 取一次当前时刻并保存 now+timeout;`deadline_left` 当前时刻>=expires 时返回 `-ETIMEDOUT`,否则返回剩余毫秒(上限 INT_MAX)。预算为零且传输未完成时不进行 IO。每次 IO 尝试前检查 deadline;若最后一个成功 IO 由期限前发起,即使回调消耗使当前时间达到期限也允许完成。\n\n本工程的可观察调度合同:先直接尝试当前方向的非阻塞 IO;只有 EAGAIN/EWOULDBLOCK 才调用 `deadline_wait`。write/read EINTR 后保留进度,重新检查 deadline,再尝试同方向 IO。等待使用剩余预算;poll EINTR 或无关 readiness 后,重新计算剩余预算继续 poll;poll=0 返回 `-ETIMEDOUT`,其他负数以 errno 结束。poll 返回准备好后,回到 IO 前的 deadline 检查。\n\n等待发送使用 POLLOUT,接收使用 POLLIN。POLLNVAL 优先返回 `-EBADF`;发送时 POLLHUP/POLLERR 返回 `-EPIPE`(即使同时 POLLOUT);接收时 POLLERR 且没有 POLLIN/POLLHUP 返回 `-EIO`;接收时 POLLIN 或 POLLHUP 都应尝试 read(HUP 可能仍有尾部数据),由 read 返回 0 决定 EOF;其他位只继续等待。只接收指定 fd 的观察,不对 revents 作修改。\n\n## P3:可注入 IO 与控制台\n\n`transfer_console.c` 实现伪造非阻塞 fd 和单调时钟,用调用记录和真实传输字节作原始观察,没有断言。stdin 一行命令,十进制整数,空行/#注释忽略。`A 0|1` 选择独立端点;`I request_hex response_size` 初始化(`-` 为空);`C time` 设当前时钟;`W count errno cost` 添加一次 write 结果,errno=0 时接受 min(count,requested),否则 -1/errno;`R hex errno cost` 添加一次 read 结果,errno=0 时按 capacity 读取,剩余字节保留给下次 read,`-` 为 EOF;`P result revents errno cost` 添加 poll 结果;`D budget` 调用 drive;`Z` 重置选择端点的全部状态、时钟和脚本。\n\n每次 IO/poll 回调消耗其 cost 毫秒(0..100000),失败也消耗;clock 不推进时间。write 脚本耗尽时默认完整接受剩余字节、cost=1;read 脚本耗尽时默认 EAGAIN、cost=1;poll 脚本耗尽时等待指定 timeout 后返回 0。脚本是分方向 FIFO,不依赖其他调用先后。事件位为 Linux poll.h 数值。I 输出 `I result input_hash_before input_hash_after`(32 位 FNV-1a,offset=2166136261,multiplier=16777619);实际 write/read 回调输出 `W|R capacity actual errno hex`(失败 actual=-1、hex=-);poll 输出 `P events timeout result revents errno`;drive 后输出 `D result`。每条命令最后输出 `S sent received done current_time response_hex all_sent_hex remaining_write_actions remaining_read_actions remaining_poll_actions`。所有数值和 hex 都为原始观察。\n\n## P4:构建与验收\n\n`make` 等价于 `gcc -std=c11 -O2 -Wall -Wextra -pedantic transfer_console.c text.c deadline.c transfer.c -o abi_demo`,正式同参数干净构建限 20 秒,执行限 15 秒。有效脚本退出 0、stdout 仅观察,stderr 可诊断;无效控制台输入退出 2。自测:`printf 'I 01020304 3\\nW 1 0 1\\nR aabbcc 0 1\\nD 10\\n' | ./abi_demo`。验收覆盖正常/空/最大尺寸、短写短读、跨 drive 超时重试、EINTR、错误后续传、EOF、零写、poll 超时/无关 readiness/组合错误位、NULL 或边界输入与双端点交错。全部 deadline 均通过可注入系统调用桩确定复现,不依赖真实时间或系统负载,不宣称真实设备或网络验证。\n"
},
"candidates": [
"deadline.c",
"transfer.c"
],
"sources": [
"transfer_console.c",
"text.c",
"deadline.c",
"transfer.c"
]
}