Lab 4: Kernel C11 Macro Safety, Static Linkage & LIFO Unwind Auditor
Enforce Linux kernel C11 formatting, internal static symbol hygiene, centralized LIFO goto error unwinding, and single-evaluation macro safety verified by a host static-rule and unwind auditor.
Prerequisites in this module
What ./check.sh verifies
- Builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror.
- Fix compiler warnings or errors in files/core.c.
- Archives core.o into libcore.a and links independent TAP verifier.
- Preserve public declarations in files/core.h so tests.c links.
step 1/15 · inspect starter files and specification
Production kernel patches fail review immediately when macros hide return jumps, private helpers omit static (STB_GLOBAL namespace pollution), or goto cleanup labels release clocks before unmapping registers.
In this studio, you implement a deterministic C11 static-rule and resource-unwind verifier that catches all three classes of defects automatically before target compilation.
learner@ksa:~/gl-e00/lab-m04ls -lastarter/Makefilestarter/cli.cstarter/core.cstarter/core.hlearner@ksa:~/gl-e00/lab-m04starter: starter/Makefile
learner@ksa:~/gl-e00/lab-m04$ bat starter/Makefile File: starter/Makefile CC_BIN = gcc AR = ar CFLAGS_ALL = -std=c11 -Wall -Wextra -Werror -O2 LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.c core.h $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clistarter: starter/cli.c
learner@ksa:~/gl-e00/lab-m04$ bat starter/cli.c File: starter/cli.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static int cmp_u64(const void *a, const void *b) { uint64_t va = *(const uint64_t *)a; uint64_t vb = *(const uint64_t *)b; return (va > vb) - (va < vb); } static int run_bench(void) { const struct sym_decl syms[4] = { {"imx_sai_probe", false, true}, {"sai_fifo_reset", true, false}, {"lpBadHelper", false, false}, {"sai_clk_disable", true, false}, }; const uint8_t lifo_ok[3] = {2U, 1U, 0U}; const struct unwind_trace tr = {3U, 3U, lifo_ok}; uint64_t samples[40]; volatile uint32_t sink = 0U; for (int w = 0; w < 5; w++) { struct sym_report rep; sink ^= audit_macro_def("x", "do { *(r) = (x); } while (0)"); (void)audit_symbol_hygiene(syms, 4U, &rep); sink ^= (uint32_t)verify_lifo_unwind(&tr); } for (int s = 0; s < 40; s++) { struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); for (int iter = 0; iter < 500; iter++) { struct sym_report rep; sink ^= audit_macro_def("x", "do { *(r) = (x); *(s) = (x) + 1U; } while (0)"); sink ^= audit_macro_def("x", "if (x < 0) return -1;"); (void)audit_symbol_hygiene(syms, 4U, &rep); sink ^= (uint32_t)verify_lifo_unwind(&tr) + (uint32_t)rep.global_leak_count; } clock_gettime(CLOCK_MONOTONIC, &t1); samples[s] = (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000000ULL + (uint64_t)(t1.tv_nsec - t0.tv_nsec); } qsort(samples, 40U, sizeof(samples[0]), cmp_u64); printf("BENCH lab_m04_c_style_auditor clock=CLOCK_MONOTONIC batches=40x500 " "p50_ns=%llu p95_ns=%llu p99_ns=%llu sink=%u\n", (unsigned long long)samples[20], (unsigned long long)samples[38], (unsigned long long)samples[39], sink); return 0; } int main(int argc, char **argv) { if (argc > 1 && strcmp(argv[1], "--bench") == 0) return run_bench(); uint32_t clean_m = audit_macro_def("v", "do { *r0 = (v); *r1 = (v) + 1U; } while (0)"); uint32_t bad_m = audit_macro_def("v", "{ *r0 = v; return -1; };"); const struct sym_decl syms[3] = { {"imx_uart_probe", false, true}, {"uart_fifo_flush", true, false}, {"dwInternalLeak", false, false}, }; struct sym_report rep = {0}; const uint8_t rel_ok[3] = {2U, 1U, 0U}; const uint8_t rel_fifo[3] = {0U, 1U, 2U}; struct unwind_trace t_ok = {3U, 3U, rel_ok}; struct unwind_trace t_bad = {3U, 3U, rel_fifo}; (void)audit_symbol_hygiene(syms, 3U, &rep); printf("MACRO clean_flags=0x%x hazard_flags=0x%x\n", clean_m, bad_m); printf("SYMBOLS global_leaks=%zu naming_violations=%zu\n", rep.global_leak_count, rep.naming_violation_count); printf("UNWIND lifo_valid=%d fifo_rejected=%d\n", verify_lifo_unwind(&t_ok) ? 1 : 0, !verify_lifo_unwind(&t_bad) ? 1 : 0); return 0; }starter: starter/core.c
learner@ksa:~/gl-e00/lab-m04$ bat starter/core.c File: starter/core.c #include "core.h" #include <ctype.h> #include <string.h> static bool is_ident_char(char c) { return isalnum((unsigned char)c) || c == '_'; } static bool contains_token(const char *hay, const char *tok) { size_t tlen = strlen(tok); const char *p = hay; if (tlen == 0U) return false; while ((p = strstr(p, tok)) != NULL) { bool left_ok = (p == hay) || !is_ident_char(p[-1]); bool right_ok = !is_ident_char(p[tlen]); if (left_ok && right_ok) return true; p += tlen; } return false; } static bool param_always_parenthesized(const char *body, const char *param) { size_t plen = strlen(param); const char *p = body; if (plen == 0U) return true; while ((p = strstr(p, param)) != NULL) { bool left_tok = (p == body) || !is_ident_char(p[-1]); bool right_tok = !is_ident_char(p[plen]); if (left_tok && right_tok) { bool wrapped = (p > body && p[-1] == '(' && p[plen] == ')'); if (!wrapped) return false; } p += plen; } return true; } uint32_t audit_macro_def(const char *param, const char *body) { uint32_t flags = MACRO_CLEAN; size_t len; size_t semis = 0U; if (!body) return MACRO_HIDDEN_FLOW; len = strlen(body); while (len > 0U && isspace((unsigned char)body[len - 1U])) len--; if (param && !param_always_parenthesized(body, param)) flags |= MACRO_UNPAREN_PARAM; /* BUG: starter ignores hidden return/goto control flow inside macros */ for (size_t i = 0U; i < len; i++) { if (body[i] == ';') semis++; } if (semis > 0U) { bool has_do = (strncmp(body, "do {", 4U) == 0 || strncmp(body, "do{", 3U) == 0); bool ends_while0 = (len >= 9U && strncmp(body + len - 9U, "while (0)", 9U) == 0) || (len >= 8U && strncmp(body + len - 8U, "while(0)", 8U) == 0); if (!has_do || !ends_while0) flags |= MACRO_MISSING_DO_WHILE; if (len > 0U && body[len - 1U] == ';') flags |= MACRO_TRAILING_SEMI; } return flags; } static bool is_valid_snake_case(const char *s) { if (!s || s[0] == '\0' || isdigit((unsigned char)s[0])) return false; for (size_t i = 0U; s[i] != '\0'; i++) { unsigned char c = (unsigned char)s[i]; if (!(islower(c) || isdigit(c) || c == '_')) return false; } return true; } int audit_symbol_hygiene(const struct sym_decl *syms, size_t n, struct sym_report *out) { if (!syms || !out) return -1; out->global_leak_count = 0U; out->naming_violation_count = 0U; for (size_t i = 0U; i < n; i++) { /* BUG: starter forgets to flag non-static private STB_GLOBAL leaks */ if (!is_valid_snake_case(syms[i].name)) out->naming_violation_count++; } return 0; } bool verify_lifo_unwind(const struct unwind_trace *tr) { if (!tr) return false; if (tr->acquired_count == 0U) return tr->released_count == 0U; if (!tr->released_ids || tr->released_count != tr->acquired_count) return false; for (size_t i = 0U; i < tr->released_count; i++) { uint8_t expected_id = (uint8_t)i; /* BUG: checks FIFO 0..N-1 instead of LIFO N-1..0! */ if (tr->released_ids[i] != expected_id) return false; } return true; }starter: starter/core.h
learner@ksa:~/gl-e00/lab-m04$ bat starter/core.h File: starter/core.h #ifndef KSA_LAB_M04_C_STYLE_AUDITOR_H #define KSA_LAB_M04_C_STYLE_AUDITOR_H #include <stdbool.h> #include <stddef.h> #include <stdint.h> enum macro_flag { MACRO_CLEAN = 0U, MACRO_UNPAREN_PARAM = 1U << 0, MACRO_MISSING_DO_WHILE = 1U << 1, MACRO_HIDDEN_FLOW = 1U << 2, MACRO_TRAILING_SEMI = 1U << 3, }; struct sym_decl { const char *name; bool is_static; bool in_public_header; }; struct sym_report { size_t global_leak_count; size_t naming_violation_count; }; struct unwind_trace { size_t acquired_count; size_t released_count; const uint8_t *released_ids; /* Expected reverse LIFO: acquired_count-1 .. 0 */ }; uint32_t audit_macro_def(const char *param, const char *body); int audit_symbol_hygiene(const struct sym_decl *syms, size_t n, struct sym_report *out); bool verify_lifo_unwind(const struct unwind_trace *tr); #endif /* KSA_LAB_M04_C_STYLE_AUDITOR_H */step 2/15 · 2. learning objectives
- Detect unparenthesized macro parameters, bare multi-statement bodies, trailing semicolons, and hidden
return/gotoexits inaudit_macro_def(). - Flag file-private symbols lacking
static(STB_LOCAL) linkage and Hungarian/camelCasenaming violations inaudit_symbol_hygiene(). - Verify reverse-order
LIFOresource release (k-1 .. 0) and rejectFIFOor leaked unwind traces inverify_lifo_unwind().
step 3/15 · 3. environment requirements
All automated verification steps run in unprivileged user space on a POSIX Linux host using gcc -std=c11 -Wall -Wextra -Werror and clock_gettime(CLOCK_MONOTONIC). Recorded transcripts in captures.json were generated on x86_64 running kernel 7.0.0-42-generic and prove protocol, state-machine, and memory invariants only. Physical bus timing, DMA cache coherency, and board peripheral behavior require target validation on Linux kernel 6.12 LTS (ARMv7-A Cortex-A7).
step 4/15 · 4. required software and hardware
- Host workstation:
gcc(-std=c11 -Wall -Wextra -Werror), GNUmake,ar(binutils), and POSIX/bin/sh. - Target environment (optional hardware step, Section 13): Any
ARMv7-ACortex-A7driver.kobuilt againstLinux kernel6.12 withscripts/checkpatch.plandnm --defined-only.
step 5/15 · 5. complete executable commands
learner@ksa:~/gl-e00/lab-m04$ bat script.sh File: script.sh make -B -C files ./files/lab_cli ./files/lab_cli --bench sh check.shstep 6/15 · 6. annotated source code
files/core.c enforces the production invariants that starter/core.c breaks:
learner@ksa:~/gl-e00/lab-m04$ bat example.c File: example.c for (size_t i = 0U; i < tr->released_count; i++) { uint8_t expected_id = (uint8_t)(tr->acquired_count - 1U - i); if (tr->released_ids[i] != expected_id) return false; }starter/core.c omits the MACRO_HIDDEN_FLOW check on return/goto tokens, skips counting non-static private symbol leaks, and checks FIFO (0 .. k-1) instead of LIFO (k-1 .. 0) release order.
step 7/15 · 7. expected output
Passing files/ run (sh check.sh, exit code 0):
learner@ksa:~/gl-e00/lab-m04$ bat listing.txt File: listing.txt TAP version 13 1..5 ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror ok 2 - archives core.o into libcore.a and links independent TAP verifier ok 3 - detects unparenthesized macro params, missing do-while(0), trailing semicolons, and hidden return/goto ok 4 - flags non-static private functions leaking STB_GLOBAL symbols and non-snake_case identifiers ok 5 - enforces strict reverse LIFO goto unwind order (k-1 down to 0) and rejects FIFO or partial-leak traces # pass 5/5CLI verification output (./files/lab_cli):
learner@ksa:~/gl-e00/lab-m04$ bat listing.txt File: listing.txt MACRO clean_flags=0x0 hazard_flags=0xf SYMBOLS global_leaks=1 naming_violations=1 UNWIND lifo_valid=1 fifo_rejected=1Defective starter/ run (exit code 1): emits FAIL: diagnostics on the broken contract assertions.
step 8/15 · 8. explanation of observed behavior
When all three invariants are restored, audit_macro_def() verifies token-bounded return/goto absence alongside (param) and do { ... } while (0) framing, audit_symbol_hygiene() enforces STB_LOCAL static linkage on non-header declarations, and verify_lifo_unwind() confirms exact reverse teardown across every partial failure count.
step 9/15 · 9. experiment variations
- Near transfer: Extend
audit_macro_def()to detectbreakandcontinuetokens hidden inside loop-unsafe macros. - Far transfer: Parse
nm -Psymbol table output from compiled.ofiles to verify zero unexpectedT/D/Bglobals outside a module's public header.
step 10/15 · 10. performance measurements
Recorded host benchmark (./files/lab_cli --bench, CLOCK_MONOTONIC, warmup=5, N=40 batches):
learner@ksa:~/gl-e00/lab-m04$ bat listing.txt File: listing.txt BENCH lab_m04_c_style_auditor clock=CLOCK_MONOTONIC batches=40x500 p50_ns=106462 p95_ns=125711 p99_ns=126434 sink=1The CLOCK_MONOTONIC benchmark measures user-space string/array auditing throughput (40 x 500 checks per sample) on the Linux host CPU; these nanosecond figures characterize static-check overhead only and never represent target ARMv7-A runtime execution.
step 11/15 · 11. troubleshooting
| Symptom | Root cause | Fix | ||
|---|---|---|---|---|
Check 3 fails with flow=0x0 | audit_macro_def() is not scanning for token-bounded return and goto keywords. | Restore `contains_token(body, "return") | contains_token(body, "goto") setting MACRO_HIDDEN_FLOW`. | |
Check 4 fails with leaks=0 | audit_symbol_hygiene() ignores symbols where !in_public_header && !is_static. | Increment out->global_leak_count whenever a private symbol omits static. | ||
Check 5 accepts fifo trace and rejects lifo trace | expected_id uses i (0..N-1) instead of tr->acquired_count - 1U - i. | Compute reverse LIFO index (uint8_t)(tr->acquired_count - 1U - i). |
step 12/15 · 12. guided questions
- Why does
contains_token()check!is_ident_char(p[-1])and!is_ident_char(p[tlen])around"return"instead of using plainstrstr(body, "return")?
- *Hint 1:* Consider a valid macro calling a C helper named
return_code_clamp(x)or readinggoto_count. - *Hint 2:* Without identifier-boundary checks,
strstrtriggers false positives wheneverreturnorgotoappears as a substring of a validsnake_caseidentifier.
- Why must
verify_lifo_unwind()verifytr->released_count == tr->acquired_countbefore looping overreleased_ids?
- *Hint 1:* What happens if 4 resources were acquired (
0..3) and only3, 2were released before returning-ENOMEM? - *Hint 2:* Checking only the prefix loop
0 .. released_count - 1would pass3, 2even though resources1and0leaked permanently!
step 13/15 · 13. independent challenges
Add a multi-parameter macro auditor audit_macro_multi(const char **params, size_t n_params, const char *body) that detects both unparenthesized occurrences AND duplicate evaluation (count > 1) of any parameter so macros susceptible to fn(i++) double-evaluation are flagged for static inline conversion.
step 14/15 · 14. solutions and explanations
The reference files/core.c combines token-bounded control-flow scanning, (param) and do { ... } while (0) structural checks, STB_LOCAL static linkage verification, and acquired_count - 1 - i reverse LIFO indexing to pass all 5/5 TAP checks cleanly.
step 15/15 · run verification and inspect output
Run the verification command and confirm the output matches the expected contract.
learner@ksa:~/gl-e00/lab-m04./files/lab_clilearner@ksa:~/gl-e00/lab-m04MACRO clean_flags=0x0 hazard_flags=0xfSYMBOLS global_leaks=1 naming_violations=1UNWIND lifo_valid=1 fifo_rejected=1Progressive hints & solution
hint 1
Compare starter/Makefile against the module lessons. Focus first on: Builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror.
hint 2
Compile with -std=c11 -Wall -Wextra -Werror, check every system-call return value before touching output buffers, and verify cleanup on error paths.
solution
warning: Reveal the reference solution files? Running ./check.sh against your own attempt first builds stronger retention.
solution: files/Makefile
learner@ksa:~/gl-e00/lab-m04$ bat files/Makefile File: files/Makefile CC_BIN = gcc AR = ar CFLAGS_ALL = -std=c11 -Wall -Wextra -Werror -O2 LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.c core.h $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clisolution: files/cli.c
learner@ksa:~/gl-e00/lab-m04$ bat files/cli.c File: files/cli.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static int cmp_u64(const void *a, const void *b) { uint64_t va = *(const uint64_t *)a; uint64_t vb = *(const uint64_t *)b; return (va > vb) - (va < vb); } static int run_bench(void) { const struct sym_decl syms[4] = { {"imx_sai_probe", false, true}, {"sai_fifo_reset", true, false}, {"lpBadHelper", false, false}, {"sai_clk_disable", true, false}, }; const uint8_t lifo_ok[3] = {2U, 1U, 0U}; const struct unwind_trace tr = {3U, 3U, lifo_ok}; uint64_t samples[40]; volatile uint32_t sink = 0U; for (int w = 0; w < 5; w++) { struct sym_report rep; sink ^= audit_macro_def("x", "do { *(r) = (x); } while (0)"); (void)audit_symbol_hygiene(syms, 4U, &rep); sink ^= (uint32_t)verify_lifo_unwind(&tr); } for (int s = 0; s < 40; s++) { struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); for (int iter = 0; iter < 500; iter++) { struct sym_report rep; sink ^= audit_macro_def("x", "do { *(r) = (x); *(s) = (x) + 1U; } while (0)"); sink ^= audit_macro_def("x", "if (x < 0) return -1;"); (void)audit_symbol_hygiene(syms, 4U, &rep); sink ^= (uint32_t)verify_lifo_unwind(&tr) + (uint32_t)rep.global_leak_count; } clock_gettime(CLOCK_MONOTONIC, &t1); samples[s] = (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000000ULL + (uint64_t)(t1.tv_nsec - t0.tv_nsec); } qsort(samples, 40U, sizeof(samples[0]), cmp_u64); printf("BENCH lab_m04_c_style_auditor clock=CLOCK_MONOTONIC batches=40x500 " "p50_ns=%llu p95_ns=%llu p99_ns=%llu sink=%u\n", (unsigned long long)samples[20], (unsigned long long)samples[38], (unsigned long long)samples[39], sink); return 0; } int main(int argc, char **argv) { if (argc > 1 && strcmp(argv[1], "--bench") == 0) return run_bench(); uint32_t clean_m = audit_macro_def("v", "do { *r0 = (v); *r1 = (v) + 1U; } while (0)"); uint32_t bad_m = audit_macro_def("v", "{ *r0 = v; return -1; };"); const struct sym_decl syms[3] = { {"imx_uart_probe", false, true}, {"uart_fifo_flush", true, false}, {"dwInternalLeak", false, false}, }; struct sym_report rep = {0}; const uint8_t rel_ok[3] = {2U, 1U, 0U}; const uint8_t rel_fifo[3] = {0U, 1U, 2U}; struct unwind_trace t_ok = {3U, 3U, rel_ok}; struct unwind_trace t_bad = {3U, 3U, rel_fifo}; (void)audit_symbol_hygiene(syms, 3U, &rep); printf("MACRO clean_flags=0x%x hazard_flags=0x%x\n", clean_m, bad_m); printf("SYMBOLS global_leaks=%zu naming_violations=%zu\n", rep.global_leak_count, rep.naming_violation_count); printf("UNWIND lifo_valid=%d fifo_rejected=%d\n", verify_lifo_unwind(&t_ok) ? 1 : 0, !verify_lifo_unwind(&t_bad) ? 1 : 0); return 0; }solution: files/core.c
learner@ksa:~/gl-e00/lab-m04$ bat files/core.c File: files/core.c #include "core.h" #include <ctype.h> #include <string.h> static bool is_ident_char(char c) { return isalnum((unsigned char)c) || c == '_'; } static bool contains_token(const char *hay, const char *tok) { size_t tlen = strlen(tok); const char *p = hay; if (tlen == 0U) return false; while ((p = strstr(p, tok)) != NULL) { bool left_ok = (p == hay) || !is_ident_char(p[-1]); bool right_ok = !is_ident_char(p[tlen]); if (left_ok && right_ok) return true; p += tlen; } return false; } static bool param_always_parenthesized(const char *body, const char *param) { size_t plen = strlen(param); const char *p = body; if (plen == 0U) return true; while ((p = strstr(p, param)) != NULL) { bool left_tok = (p == body) || !is_ident_char(p[-1]); bool right_tok = !is_ident_char(p[plen]); if (left_tok && right_tok) { bool wrapped = (p > body && p[-1] == '(' && p[plen] == ')'); if (!wrapped) return false; } p += plen; } return true; } uint32_t audit_macro_def(const char *param, const char *body) { uint32_t flags = MACRO_CLEAN; size_t len; size_t semis = 0U; if (!body) return MACRO_HIDDEN_FLOW; len = strlen(body); while (len > 0U && isspace((unsigned char)body[len - 1U])) len--; if (param && !param_always_parenthesized(body, param)) flags |= MACRO_UNPAREN_PARAM; if (contains_token(body, "return") || contains_token(body, "goto")) flags |= MACRO_HIDDEN_FLOW; for (size_t i = 0U; i < len; i++) { if (body[i] == ';') semis++; } if (semis > 0U) { bool has_do = (strncmp(body, "do {", 4U) == 0 || strncmp(body, "do{", 3U) == 0); bool ends_while0 = (len >= 9U && strncmp(body + len - 9U, "while (0)", 9U) == 0) || (len >= 8U && strncmp(body + len - 8U, "while(0)", 8U) == 0); if (!has_do || !ends_while0) flags |= MACRO_MISSING_DO_WHILE; if (len > 0U && body[len - 1U] == ';') flags |= MACRO_TRAILING_SEMI; } return flags; } static bool is_valid_snake_case(const char *s) { if (!s || s[0] == '\0' || isdigit((unsigned char)s[0])) return false; for (size_t i = 0U; s[i] != '\0'; i++) { unsigned char c = (unsigned char)s[i]; if (!(islower(c) || isdigit(c) || c == '_')) return false; } return true; } int audit_symbol_hygiene(const struct sym_decl *syms, size_t n, struct sym_report *out) { if (!syms || !out) return -1; out->global_leak_count = 0U; out->naming_violation_count = 0U; for (size_t i = 0U; i < n; i++) { if (!syms[i].in_public_header && !syms[i].is_static) out->global_leak_count++; if (!is_valid_snake_case(syms[i].name)) out->naming_violation_count++; } return 0; } bool verify_lifo_unwind(const struct unwind_trace *tr) { if (!tr) return false; if (tr->acquired_count == 0U) return tr->released_count == 0U; if (!tr->released_ids || tr->released_count != tr->acquired_count) return false; for (size_t i = 0U; i < tr->released_count; i++) { uint8_t expected_id = (uint8_t)(tr->acquired_count - 1U - i); if (tr->released_ids[i] != expected_id) return false; } return true; }solution: files/core.h
learner@ksa:~/gl-e00/lab-m04$ bat files/core.h File: files/core.h #ifndef KSA_LAB_M04_C_STYLE_AUDITOR_H #define KSA_LAB_M04_C_STYLE_AUDITOR_H #include <stdbool.h> #include <stddef.h> #include <stdint.h> enum macro_flag { MACRO_CLEAN = 0U, MACRO_UNPAREN_PARAM = 1U << 0, MACRO_MISSING_DO_WHILE = 1U << 1, MACRO_HIDDEN_FLOW = 1U << 2, MACRO_TRAILING_SEMI = 1U << 3, }; struct sym_decl { const char *name; bool is_static; bool in_public_header; }; struct sym_report { size_t global_leak_count; size_t naming_violation_count; }; struct unwind_trace { size_t acquired_count; size_t released_count; const uint8_t *released_ids; /* Expected reverse LIFO: acquired_count-1 .. 0 */ }; uint32_t audit_macro_def(const char *param, const char *body); int audit_symbol_hygiene(const struct sym_decl *syms, size_t n, struct sym_report *out); bool verify_lifo_unwind(const struct unwind_trace *tr); #endif /* KSA_LAB_M04_C_STYLE_AUDITOR_H */Run the check
From labs/lab_m04_c_style_auditor/, run ./check.sh. A complete solution emits TAP version 13 with every test point marked ok and exits 0:
learner@ksa:~/gl-e00/lab-m04./check.shTAP version 131..5ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werrorok 2 - archives core.o into libcore.a and links independent TAP verifierok 3 - detects unparenthesized macro params, missing do-while(0), trailing semicolons, and hidden return/gotook 4 - flags non-static private functions leaking STB_GLOBAL symbols and non-snake_case identifiersok 5 - enforces strict reverse LIFO goto unwind order (k-1 down to 0) and rejects FIFO or partial-leak traces# pass 5/5On an incomplete starter tree, ./check.sh exits non-zero and marks the unmet assertion not ok with an indented YAML diagnostic block:
learner@ksa:~/gl-e00/lab-m04./check.shTAP version 131..5not ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -WerrorFAIL: fix compiler warnings or errors in files/core.cok 2 - archives core.o into libcore.a and links independent TAP verifierok 3 - detects unparenthesized macro params, missing do-while(0), trailing semicolons, and hidden return/gotook 4 - flags non-static private functions leaking STB_GLOBAL symbols and non-snake_case identifiersok 5 - enforces strict reverse LIFO goto unwind order (k-1 down to 0) and rejects FIFO or partial-leak traces# pass 4/5note: How to read TAP diagnostics: 1..N states the total test plan; each ok k confirms a passed check; not ok k pinpoints the failing check alongside message: (what went wrong) and hint: (where to look next) between --- and ....
inspect harness: check.sh
learner@ksa:~/gl-e00/lab-m04$ bat check.sh File: check.sh #!/bin/sh set -u cd "$(dirname "$0")" || exit 1 PASS=0 WORK=$(mktemp -d "${TMPDIR:-/tmp}/ksa_lab.XXXXXX") || exit 1 trap 'rm -rf "$WORK"' EXIT HUP INT TERM cp -R files "$WORK/files" printf 'TAP version 13\n1..5\n' if make -B -C "$WORK/files" >"$WORK/build.log" 2>&1; then printf 'ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror\n' PASS=$((PASS + 1)) else printf 'not ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror\n' printf 'FAIL: fix compiler warnings or errors in files/core.c\n' fi if [ -f "$WORK/files/libcore.a" ] && gcc -std=c11 -Wall -Wextra -Werror -O2 -I"$WORK/files" tests.c "$WORK/files/libcore.a" -o "$WORK/verify" >>"$WORK/build.log" 2>&1; then printf 'ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' PASS=$((PASS + 1)) else printf 'not ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' printf 'FAIL: preserve public declarations in files/core.h so tests.c links\n' fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 1 2>&1); then printf 'ok 3 - detects unparenthesized macro params, missing do-while(0), trailing semicolons, and hidden return/goto\n' PASS=$((PASS + 1)) else printf 'not ok 3 - detects unparenthesized macro params, missing do-while(0), trailing semicolons, and hidden return/goto\n' printf 'FAIL: flag return/goto tokens with MACRO_HIDDEN_FLOW and enforce (param) plus do { ... } while (0) in audit_macro_def\n' printf " ---\n message: 'got %s'\n hint: 'flag return/goto tokens with MACRO_HIDDEN_FLOW and enforce (param) plus do { ... } while (0) in audit_macro_def'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 2 2>&1); then printf 'ok 4 - flags non-static private functions leaking STB_GLOBAL symbols and non-snake_case identifiers\n' PASS=$((PASS + 1)) else printf 'not ok 4 - flags non-static private functions leaking STB_GLOBAL symbols and non-snake_case identifiers\n' printf 'FAIL: increment global_leak_count when !in_public_header && !is_static in audit_symbol_hygiene\n' printf " ---\n message: 'got %s'\n hint: 'increment global_leak_count when !in_public_header && !is_static in audit_symbol_hygiene'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 3 2>&1); then printf 'ok 5 - enforces strict reverse LIFO goto unwind order (k-1 down to 0) and rejects FIFO or partial-leak traces\n' PASS=$((PASS + 1)) else printf 'not ok 5 - enforces strict reverse LIFO goto unwind order (k-1 down to 0) and rejects FIFO or partial-leak traces\n' printf 'FAIL: compare released_ids[i] against (acquired_count - 1 - i) in verify_lifo_unwind\n' printf " ---\n message: 'got %s'\n hint: 'compare released_ids[i] against (acquired_count - 1 - i) in verify_lifo_unwind'\n ...\n" "${OUT:-build_failed}" fi printf '# pass %d/5\n' "$PASS" [ "$PASS" -eq 5 ]Definition of done
Verification checklist
status: 0/3 checks verified
Troubleshooting
Compiler fails under -Werror on sign conversion or unused args
Use exact POSIX return types (ssize_t for read()/write(), size_t for byte counts) rather than relaxing flags.
./check.sh reports missing files in files/
Invoke ./check.sh from inside labs/lab_m04_c_style_auditor/ so relative paths to files/ resolve directly.