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": "pty",
"seed": 5264473,
"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",
"tty.h": "#ifndef ABI_TTY_H\n#define ABI_TTY_H\n#include <termios.h>\n\ntypedef struct {\n int (*getattr_call)(int fd, struct termios *attributes);\n int (*setattr_call)(int fd, int action, const struct termios *attributes);\n int (*getflags_call)(int fd);\n int (*setflags_call)(int fd, int flags);\n} tty_ops;\n\ntypedef struct {\n unsigned baud;\n unsigned minimum;\n unsigned deciseconds;\n int nonblocking;\n} tty_config;\n\ntypedef struct {\n int active;\n int fd;\n int saved_flags;\n struct termios saved_attributes;\n const tty_ops *ops;\n} tty_session;\n\n/* These helpers never perform a syscall and do not alter source attributes. */\nint tty_baud(unsigned baud, speed_t *speed);\nint tty_prepare(const struct termios *source, const tty_config *config,\n struct termios *destination);\nvoid tty_init(tty_session *session);\nint tty_begin(tty_session *session, int fd, const tty_config *config,\n const tty_ops *ops);\nint tty_update(tty_session *session, const tty_config *config);\nint tty_end(tty_session *session);\n#endif\n",
"pty_console.c": "#define _XOPEN_SOURCE 600\n#include \"tty.h\"\n#include \"text.h\"\n#include <errno.h>\n#include <fcntl.h>\n#include <poll.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <unistd.h>\n\ntypedef struct {\n int error;\n unsigned remaining;\n} fault;\n\ntypedef struct {\n int master;\n int slave;\n tty_session session;\n} endpoint;\n\nstatic fault failures[4];\nstatic endpoint endpoints[2];\n\nstatic int fail(unsigned operation)\n{\n if (failures[operation].remaining == 0)\n return 0;\n --failures[operation].remaining;\n errno = failures[operation].error;\n return 1;\n}\n\nstatic int get_attributes(int fd, struct termios *attributes)\n{\n return fail(0) ? -1 : tcgetattr(fd, attributes);\n}\n\nstatic int set_attributes(int fd, int action, const struct termios *attributes)\n{\n return fail(1) ? -1 : tcsetattr(fd, action, attributes);\n}\n\nstatic int get_flags(int fd)\n{\n return fail(2) ? -1 : fcntl(fd, F_GETFL);\n}\n\nstatic int set_flags(int fd, int flags)\n{\n return fail(3) ? -1 : fcntl(fd, F_SETFL, flags);\n}\n\nstatic const tty_ops operations = {\n get_attributes, set_attributes, get_flags, set_flags\n};\n\nstatic unsigned baud_number(speed_t speed)\n{\n static const unsigned baud[] = {\n 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200\n };\n static const speed_t code[] = {\n B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200\n };\n for (size_t index = 0; index < sizeof baud / sizeof baud[0]; ++index) {\n if (code[index] == speed)\n return baud[index];\n }\n return 0;\n}\n\nstatic int create_endpoint(endpoint *item)\n{\n char *name;\n item->master = posix_openpt(O_RDWR | O_NOCTTY);\n if (item->master < 0)\n return 0;\n if (grantpt(item->master) != 0 || unlockpt(item->master) != 0)\n return 0;\n name = ptsname(item->master);\n if (name == NULL)\n return 0;\n item->slave = open(name, O_RDWR | O_NOCTTY);\n if (item->slave < 0)\n return 0;\n tty_init(&item->session);\n return 1;\n}\n\nstatic int seed_endpoint(endpoint *item, unsigned seed)\n{\n struct termios attributes;\n if (tcgetattr(item->slave, &attributes) != 0)\n return 0;\n attributes.c_iflag = IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR |\n IGNCR | ICRNL | IXON | IXOFF | IXANY;\n attributes.c_lflag = ECHO | ECHONL | ICANON | ISIG | IEXTEN | NOFLSH;\n attributes.c_oflag = OPOST | ONLCR;\n attributes.c_cflag = CS8 | CREAD | HUPCL;\n for (size_t index = 0; index < NCCS; ++index)\n attributes.c_cc[index] = (cc_t)((seed + (unsigned)index) % 251u + 1u);\n attributes.c_cc[VMIN] = 7;\n attributes.c_cc[VTIME] = 9;\n if (cfsetispeed(&attributes, B38400) != 0 || cfsetospeed(&attributes, B38400) != 0)\n return 0;\n if (tcsetattr(item->slave, TCSANOW, &attributes) != 0)\n return 0;\n int flags = fcntl(item->slave, F_GETFL);\n if (flags < 0)\n return 0;\n flags &= ~(O_NONBLOCK | O_APPEND);\n if (seed & 1u)\n flags |= O_APPEND;\n if (seed & 2u)\n flags |= O_NONBLOCK;\n return fcntl(item->slave, F_SETFL, flags) == 0;\n}\n\nstatic int show_endpoint(endpoint *item)\n{\n struct termios attributes;\n int flags = fcntl(item->slave, F_GETFL);\n if (flags < 0 || tcgetattr(item->slave, &attributes) != 0)\n return 0;\n printf(\"S %d %lu %lu %lu %d %d %d %d %d %u %u %d %d \",\n item->session.active,\n (unsigned long)attributes.c_iflag,\n (unsigned long)attributes.c_lflag,\n (unsigned long)attributes.c_oflag,\n (attributes.c_cflag & CSIZE) == CS8,\n !!(attributes.c_cflag & PARENB),\n !!(attributes.c_cflag & CLOCAL),\n !!(attributes.c_cflag & CREAD),\n !!(attributes.c_cflag & HUPCL),\n baud_number(cfgetispeed(&attributes)),\n baud_number(cfgetospeed(&attributes)),\n !!(flags & O_NONBLOCK), !!(flags & O_APPEND));\n text_hex(stdout, attributes.c_cc, NCCS);\n putchar('\\n');\n return 1;\n}\n\nstatic int receive_bytes(endpoint *item, const uint8_t *bytes, size_t length)\n{\n uint8_t received[TEXT_BYTES];\n ssize_t amount;\n struct pollfd descriptor = { item->slave, POLLIN, 0 };\n if (length == 0) {\n puts(\"D -\");\n return 1;\n }\n do {\n amount = write(item->master, bytes, length);\n } while (amount < 0 && errno == EINTR);\n if (amount < 0 || (size_t)amount != length)\n return 0;\n size_t used = 0;\n while (used < length) {\n int result = poll(&descriptor, 1, 2000);\n if (result < 0 && errno == EINTR)\n continue;\n if (result <= 0)\n break;\n amount = read(item->slave, received + used, sizeof received - used);\n if (amount < 0 && (errno == EINTR || errno == EAGAIN))\n continue;\n if (amount <= 0)\n break;\n used += (size_t)amount;\n }\n printf(\"D \");\n text_hex(stdout, received, used);\n putchar('\\n');\n return 1;\n}\n\nstatic int config_command(text_reader *reader, endpoint *item, int begin)\n{\n unsigned baud, minimum, deciseconds;\n int nonblocking;\n if (!text_count(reader, 5) || !text_unsigned(reader, 1, 1000000, &baud) ||\n !text_unsigned(reader, 2, 1000, &minimum) ||\n !text_unsigned(reader, 3, 1000, &deciseconds) ||\n !text_signed(reader, 4, -1, 2, &nonblocking))\n return 0;\n tty_config config = { baud, minimum, deciseconds, nonblocking };\n int result = begin ? tty_begin(&item->session, item->slave, &config, &operations) :\n tty_update(&item->session, &config);\n printf(\"R %d\\n\", result);\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 if (!create_endpoint(&endpoints[index]) || !seed_endpoint(&endpoints[index], 0))\n return 3;\n }\n while (text_next(&reader, stdin) > 0) {\n endpoint *item = &endpoints[selected];\n const char *command = reader.word[0];\n int okay = 0;\n if (strcmp(command, \"A\") == 0) {\n okay = text_count(&reader, 2) && text_unsigned(&reader, 1, 1, &selected);\n item = &endpoints[selected];\n } else if (strcmp(command, \"Z\") == 0) {\n unsigned seed;\n okay = text_count(&reader, 2) && text_unsigned(&reader, 1, 250, &seed) &&\n seed_endpoint(item, seed);\n } else if (strcmp(command, \"B\") == 0 || strcmp(command, \"U\") == 0) {\n okay = config_command(&reader, item, command[0] == 'B');\n } else if (strcmp(command, \"E\") == 0) {\n okay = text_count(&reader, 1);\n if (okay)\n printf(\"R %d\\n\", tty_end(&item->session));\n } else if (strcmp(command, \"F\") == 0) {\n unsigned operation, remaining;\n int error;\n okay = text_count(&reader, 4) && text_unsigned(&reader, 1, 3, &operation) &&\n text_signed(&reader, 2, 1, 255, &error) &&\n text_unsigned(&reader, 3, 10, &remaining);\n if (okay)\n failures[operation] = (fault){ error, remaining };\n } else if (strcmp(command, \"D\") == 0) {\n uint8_t bytes[TEXT_BYTES];\n size_t length;\n okay = text_count(&reader, 2) &&\n text_bytes(reader.word[1], bytes, sizeof bytes, &length);\n if (okay)\n okay = receive_bytes(item, bytes, length);\n }\n if (!okay || !show_endpoint(item)) {\n text_error(&reader, \"invalid command or host syscall failed\");\n return 2;\n }\n }\n for (size_t index = 0; index < 2; ++index) {\n close(endpoints[index].slave);\n close(endpoints[index].master);\n }\n return reader.failed ? 2 : 0;\n}\n",
"Makefile": "CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -pedantic\nall: abi_demo\nabi_demo: pty_console.c text.c session.c settings.c tty.h text.h\n\t$(CC) $(CFLAGS) pty_console.c text.c session.c settings.c -o $@\nclean:\n\trm -f abi_demo\n.PHONY: all clean\n",
"README.md": "# PTY 串口会话修复\n\n串口服务切换速率后退出,有时把终端留在 raw 模式;初始化失败后重试时还可能看到半套配置,控制字节和高位字节在透传中丢失。当前目录是可编译的 C11 工程。修复串口会话管理,保持公开接口与控制台格式。最终只提交并评分 `session.c`、`settings.c`;其余源码、头文件与 Makefile 为只读上下文,正式评分会从可信版本重建。\n\n## P1:配置与状态\n\n`tty.h` 给出全部结构和签名。`tty_init` 初始化独立会话,inactive、fd=-1;传给后续接口的 session/config/ops 指针均有效。支持 1200、2400、4800、9600、19200、38400、57600、115200 baud;VMIN/VTIME 各为 0..255,VTIME 单位 0.1 秒;nonblocking 只能 0/1。其他配置返回 `-EINVAL`,不修改终端或会话,也不调用 ops。`tty_baud` 按表转换,未知速率或 NULL 输出返回 `-EINVAL`;`tty_prepare` 不执行系统调用,成功输出以下配置、失败不修改 destination,source 可与 destination 相同,输入 config 不变。\n\nraw 配置清除 c_iflag 中 IGNBRK/BRKINT/PARMRK/ISTRIP/INLCR/IGNCR/ICRNL/IXON/IXOFF,清除 c_lflag 中 ECHO/ECHONL/ICANON/ISIG/IEXTEN,清除 c_oflag 的 OPOST;c_cflag 清除 CSIZE/PARENB 后设置 CS8/CLOCAL/CREAD。设置输入和输出速率、c_cc[VMIN]/c_cc[VTIME]。其余 flag 位和控制字符必须保留。nonblocking 只改变文件状态标志 O_NONBLOCK,其余文件状态位保留。配置用于真实 Linux PTY,已配置后的原始输入字节必须原样通过,不能被翻译、吞掉或作为信号。\n\n## P2:会话生命周期与错误\n\n所有返回均为 0 或负 errno。inactive 时 `tty_begin(session,fd,config,ops)` 读取原始 termios 和文件状态、应用新配置,全部成功后 active=1 并保存**首次开始前**的快照。active 上 begin 返回 `-EALREADY` 且无副作用。ops 必须包含四个非 NULL 回调,否则 begin 返回 `-EINVAL`。`tty_update` 在 inactive 上返回 `-ENODEV`;active 时更新当前配置,成功更新不改变最初保存的快照。各会话状态独立。\n\nbegin/update 依次获取 termios、F_GETFL,TCSANOW 应用 termios,再 F_SETFL。任何阶段失败返回其负 errno,原始配置与会话保持不变;若 termios 已写入但 F_SETFL 失败,先还原本次操作前的 termios。注入的永久错误每次仅涉及一个阶段,失败调用无副作用,回滚本身可成功。不要求处理同时发生的回滚失败。\n\n`tty_end` inactive 时返回 0、不调用 ops。active 时先 TCSANOW 恢复最初 termios,再恢复最初文件状态;两步成功后 active=0、fd=-1,保留其他状态字段无要求。如果恢复的一步失败,返回负 errno、保持 active 和最初快照,之前成功的恢复操作不撤销;后续 end 必须可重试。所有 ops 在返回 -1 且 errno=EINTR 时都原参数重试;其他负返回以当时 errno 结束。除指定配置、active/fd 和必要快照外不能修改调用者资产,不关闭 fd。调用者在 active 期间不直接改写该 fd 的属性。\n\n## P3:公开控制台与观测\n\n`pty_console.c` 使用两对真实 PTY、调用 ops 并输出原始观察,没有正确答案或断言。stdin 一行一命令,十进制整数;空行和以 # 开头的行忽略。初始两端 inactive,seed=0;`A 0|1` 选择端点;`Z seed`(0..250,仅 inactive 使用)重设选择端的原始属性;`B baud min time nonblock` 开始;`U ...` 更新;`E` 结束;`F operation errno count` 为下一批该回调注入 count 次 -1/errno,operation 0=getattr、1=setattr、2=getflags、3=setflags;`D hex` 向 master 写入并从 slave 读取字节(`-` 表示空),仅在 min=1,time=0 且 raw 已配置后调用,最多 4096 字节。\n\nZ 的完整 seed 实现公开在控制台:所有输入模式位加 IXANY,所有本地模式位加 NOFLSH,OPOST|ONLCR,CS8|CREAD|HUPCL,38400 baud,各 cc=(seed+index)%251+1,但 VMIN=7/VTIME=9;Linux n_tty 的 19 个控制字符槽位实际生效,其余 libc 保留槽位回读为零。seed 位 0 控制 O_APPEND,位 1 控制 O_NONBLOCK。B/U/E 先输出 `R result`,D 输出 `D hex`;每条命令后输出 `S active iflag lflag oflag cs8 parenb clocal cread hupcl ispeed ospeed nonblock append cc_hex`。flag 数值为实际 Linux termios 位值,布尔值为 0/1,速度以 baud 数字表示;cc_hex 是全部 NCCS 控制字符。D 在实际 PTY 中使用 2000ms poll 防挂死,这不是候选 deadline 算法验收。\n\n## P4:构建与自测\n\n`make` 等价于 `gcc -std=c11 -O2 -Wall -Wextra -pedantic pty_console.c text.c session.c settings.c -o abi_demo`。评分使用完全相同参数,干净重建限 20 秒,程序运行限 15 秒。有效脚本退出码 0,stdout 只允许上述观察行,stderr 可诊断;非法控制台输入退出 2,主机 PTY 建立故障退出 3。可用 `printf 'Z 17\\nB 115200 1 0 1\\nU 9600 1 0 0\\nE\\n' | ./abi_demo` 自测;比较前后 S 的终端属性和 flag,端点会话在 E 后 inactive。验收包含多次 begin/update/end、失败回滚后重试、EINTR、两个端点交错,以及真实 PTY 原始字节透传。只验证软件 ABI 行为,不代表真实串口电气或板卡验证。\n"
},
"candidates": [
"session.c",
"settings.c"
],
"sources": [
"pty_console.c",
"text.c",
"session.c",
"settings.c"
]
}