Lab 1: Platform Match Order, Probe Deferral, and devres Unwind
Implement, compile, and verify lab_m01_platform_devm against ./check.sh.
Prerequisites in this module
What ./check.sh verifies
- Strict C11 library, CLI, and test build.
step 1/15 · inspect starter files and specification
A platform driver that binds on one board and silently misbehaves on the next usually has an identity bug, not a register bug. The driver core tries driver_override, then the OF compatible, then ACPI, then the driver's id_table, and only then the device name. When an id_table is present but has no entry for the device, the core stops there: the name is never compared. A driver that assumes otherwise binds where it should not, or reports the wrong match method to code that reads platform_get_device_id().
Probe adds a second trap. A supplier that is not ready makes platform_get_irq() or devm_gpiod_get() return -EPROBE_DEFER (-517). That is a failed probe, so every managed resource acquired so far is released newest first, and the next attempt starts empty. In this lab you implement a host model of both rules and repair a starter whose name branch reports the wrong method while still compiling cleanly.
learner@ksa:~/gl-e05/lab-m01ls -lastarter/Makefilestarter/contract_tests.cstarter/match.cstarter/match.hstarter/match_cli.clearner@ksa:~/gl-e05/lab-m01starter: starter/Makefile
learner@ksa:~/gl-e05/lab-m01$ bat starter/Makefile File: starter/Makefile CC ?= gcc CFLAGS := -std=c11 -Wall -Wextra -Werror -I. .PHONY: all clean all: libmatch.a match_cli contract_tests match.o: match.c match.h $(CC) $(CFLAGS) -c $< -o $@ libmatch.a: match.o ar rcs $@ $^ match_cli: match_cli.c libmatch.a $(CC) $(CFLAGS) $< libmatch.a -o $@ contract_tests: contract_tests.c libmatch.a $(CC) $(CFLAGS) $< libmatch.a -o $@ clean: rm -f match.o libmatch.a match_cli contract_testsstarter: starter/contract_tests.c
learner@ksa:~/gl-e05/lab-m01$ bat starter/contract_tests.c File: starter/contract_tests.c #define _POSIX_C_SOURCE 200809L #include "match.h" #include <assert.h> #include <limits.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static void check(unsigned group) { char out[32]; const char *s[]={"mmio","irq","gpio"}; if (group==1) { assert(bus_match("x","x","y","y","z","z","n","n")==MATCH_OF); assert(bus_match(NULL,NULL,"y","y","z","z","n","n")==MATCH_ACPI); assert(bus_match(NULL,NULL,NULL,NULL,"z","z","n","n")==MATCH_ID); assert(bus_match(NULL,NULL,NULL,NULL,"z","bad","n","n")==MATCH_NONE); assert(bus_match(NULL,NULL,NULL,NULL,NULL,NULL,"n","n")==MATCH_NAME); assert(bus_match(NULL,NULL,NULL,NULL,NULL,NULL,"n","bad")==MATCH_NONE); } else if (group==2) { assert(devres_release(s,3,out,sizeof out)==0); assert(strcmp(out,"gpio,irq,mmio")==0); assert(devres_release(NULL,0,out,sizeof out)==0 && out[0]=='\0'); } else if (group==3) { assert(probe_status(0)==-517 && probe_status(1)==0); for (int acquired=0; acquired<=3; ++acquired) { assert(devres_release(s,acquired,out,sizeof out)==0); assert((acquired==0)==(out[0]=='\0')); } } else { assert(devres_release(NULL,1,out,sizeof out)<0); assert(devres_release(s,-1,out,sizeof out)<0); assert(devres_release(s,3,out,2)<0); assert(devres_release(s,3,NULL,0)<0); const char *bad[]={NULL}; assert(devres_release(bad,1,out,sizeof out)<0); } } static uint64_t monotonic_ns(void) { struct timespec ts; assert(clock_gettime(CLOCK_MONOTONIC,&ts)==0); return (uint64_t)ts.tv_sec*UINT64_C(1000000000)+(uint64_t)ts.tv_nsec; } static int compare_ns(const void *a,const void *b) { uint64_t x=*(const uint64_t *)a,y=*(const uint64_t *)b; return x>y ? 1 : x<y ? -1 : 0; } static int benchmark(void) { enum { WARMUP=5, SAMPLES=40 }; uint64_t samples[SAMPLES]; for (unsigned i=0;i<WARMUP;++i) for (unsigned n=1;n<=4;++n) check(n); for (unsigned i=0;i<SAMPLES;++i) { uint64_t start=monotonic_ns(); for (unsigned n=1;n<=4;++n) check(n); samples[i]=monotonic_ns()-start; } qsort(samples,SAMPLES,sizeof(samples[0]),compare_ns); printf("benchmark_samples=40 warmup=5 p50_ns=%llu p95_ns=%llu p99_ns=%llu\n", (unsigned long long)samples[20],(unsigned long long)samples[38], (unsigned long long)samples[39]); return 0; } int main(int argc,char **argv) { if (argc==2 && strcmp(argv[1],"bench")==0) return benchmark(); if (argc==2) { unsigned n=(unsigned)strtoul(argv[1],NULL,10); if (n<1 || n>4) return 2; check(n); printf("contract_group=%u passed\n",n); return 0; } for (unsigned n=1;n<=4;++n) check(n); puts("contract_groups=4 passed"); return 0; }starter: starter/match.c
learner@ksa:~/gl-e05/lab-m01$ bat starter/match.c File: starter/match.c #include "match.h" #include <string.h> static int eq(const char *a,const char *b) { return a && b && strcmp(a,b)==0; } int bus_match(const char *a,const char *b,const char *c,const char *d, const char *e,const char *f,const char *g,const char *h) { if (eq(a,b)) return MATCH_OF; if (eq(c,d)) return MATCH_ACPI; if (f) return eq(e,f) ? MATCH_ID : MATCH_NONE; return eq(g,h) ? MATCH_ID : MATCH_NONE; } int probe_status(int ready) { return ready ? 0 : -517; } int devres_release(const char *const *stack,int n,char *out,size_t cap) { if (!out || !cap || n<0 || (n && !stack)) return -1; size_t used=0; out[0]='\0'; for (int i=n; i>0; --i) { if (!stack[i-1]) return -1; size_t len=strlen(stack[i-1]), sep=used ? 1U : 0U; if (sep >= cap-used || len >= cap-used-sep) return -1; if (sep) out[used++]=','; memcpy(out+used,stack[i-1],len); used+=len; out[used]='\0'; } return 0; }starter: starter/match.h
learner@ksa:~/gl-e05/lab-m01$ bat starter/match.h File: starter/match.h #ifndef MATCH_H #define MATCH_H #include <stddef.h> enum { MATCH_NONE=0, MATCH_OF=1, MATCH_ACPI=2, MATCH_ID=3, MATCH_NAME=4 }; int bus_match(const char *,const char *,const char *,const char *, const char *,const char *,const char *,const char *); int probe_status(int ready); int devres_release(const char *const *,int,char *,size_t); #endifstarter: starter/match_cli.c
learner@ksa:~/gl-e05/lab-m01$ bat starter/match_cli.c File: starter/match_cli.c #define _POSIX_C_SOURCE 200809L #include "match.h" #include <assert.h> #include <limits.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static void check(unsigned group) { char out[32]; const char *s[]={"mmio","irq","gpio"}; if (group==1) { assert(bus_match("x","x","y","y","z","z","n","n")==MATCH_OF); assert(bus_match(NULL,NULL,"y","y","z","z","n","n")==MATCH_ACPI); assert(bus_match(NULL,NULL,NULL,NULL,"z","z","n","n")==MATCH_ID); assert(bus_match(NULL,NULL,NULL,NULL,"z","bad","n","n")==MATCH_NONE); assert(bus_match(NULL,NULL,NULL,NULL,NULL,NULL,"n","n")==MATCH_NAME); assert(bus_match(NULL,NULL,NULL,NULL,NULL,NULL,"n","bad")==MATCH_NONE); } else if (group==2) { assert(devres_release(s,3,out,sizeof out)==0); assert(strcmp(out,"gpio,irq,mmio")==0); assert(devres_release(NULL,0,out,sizeof out)==0 && out[0]=='\0'); } else if (group==3) { assert(probe_status(0)==-517 && probe_status(1)==0); for (int acquired=0; acquired<=3; ++acquired) { assert(devres_release(s,acquired,out,sizeof out)==0); assert((acquired==0)==(out[0]=='\0')); } } else { assert(devres_release(NULL,1,out,sizeof out)<0); assert(devres_release(s,-1,out,sizeof out)<0); assert(devres_release(s,3,out,2)<0); assert(devres_release(s,3,NULL,0)<0); const char *bad[]={NULL}; assert(devres_release(bad,1,out,sizeof out)<0); } } static uint64_t monotonic_ns(void) { struct timespec ts; assert(clock_gettime(CLOCK_MONOTONIC,&ts)==0); return (uint64_t)ts.tv_sec*UINT64_C(1000000000)+(uint64_t)ts.tv_nsec; } static int compare_ns(const void *a,const void *b) { uint64_t x=*(const uint64_t *)a,y=*(const uint64_t *)b; return x>y ? 1 : x<y ? -1 : 0; } static int benchmark(void) { enum { WARMUP=5, SAMPLES=40 }; uint64_t samples[SAMPLES]; for (unsigned i=0;i<WARMUP;++i) for (unsigned n=1;n<=4;++n) check(n); for (unsigned i=0;i<SAMPLES;++i) { uint64_t start=monotonic_ns(); for (unsigned n=1;n<=4;++n) check(n); samples[i]=monotonic_ns()-start; } qsort(samples,SAMPLES,sizeof(samples[0]),compare_ns); printf("benchmark_samples=40 warmup=5 p50_ns=%llu p95_ns=%llu p99_ns=%llu\n", (unsigned long long)samples[20],(unsigned long long)samples[38], (unsigned long long)samples[39]); return 0; } int main(int argc,char **argv) { if (argc==2 && strcmp(argv[1],"bench")==0) return benchmark(); if (argc==2) { unsigned n=(unsigned)strtoul(argv[1],NULL,10); if (n<1 || n>4) return 2; check(n); printf("contract_group=%u passed\n",n); return 0; } for (unsigned n=1;n<=4;++n) check(n); puts("contract_groups=4 passed"); return 0; }step 2/15 · 2. learning objectives
- Encode the OF, ACPI, ID-table, name priority so that a present but missing ID table returns no match.
- Release managed resources in reverse acquisition order and produce an empty unwind when nothing was acquired.
- Represent
-EPROBE_DEFERseparately from identity and unwind only the resources a partial probe obtained. - Reject NULL tables, negative counts, NULL entries, and output buffers too small for the release list.
- Report a 40-sample
CLOCK_MONOTONICdistribution for the four contract groups and explain why it says nothing about target probe time.
step 3/15 · 3. environment requirements
The grader runs entirely on a development host: a POSIX shell, GNU Make, and a C11 compiler with clock_gettime(CLOCK_MONOTONIC). It needs no root access, no kernel headers, and no module loading. The recorded transcripts in captures.json come from a native x86_64 host kernel (7.0.0-42-generic); they are host evidence about the model only. Target acceptance happens on the Cortex-A7 lab board running Linux 6.12 built from arch/arm, with the course Device Tree.
step 4/15 · 4. required software and hardware
Host: gcc (any version accepting -std=c11 -Wall -Wextra -Werror), make, ar, and sh. Target (optional, for section 13): the lab board, a serial console, a kernel with CONFIG_DEBUG_FS=y so /sys/kernel/debug/devices_deferred is available, and a Device Tree node for a platform device whose interrupt parent can be made late.
step 5/15 · 5. complete executable commands
learner@ksa:~/gl-e05/lab-m01$ bat script.sh File: script.sh make -C files ./files/match_cli ./files/contract_tests 1 ./files/contract_tests bench ./check.shmake -C files builds libmatch.a, the match_cli driver program, and contract_tests. Running contract_tests with a group number exercises one contract group and prints contract_group=N passed. ./check.sh copies files/ into a temporary directory, rebuilds with make -B, and runs groups 1 to 4 as TAP results 2 to 5.
To see the starter fail, copy the lab to a scratch directory, replace that copy's files/ with starter/, and run its check.sh. Keep the reference tree untouched.
step 6/15 · 6. annotated source code
bus_match() takes four string pairs. Arguments 1 and 2 model the driver and device OF compatibles, 3 and 4 the ACPI IDs, 5 and 6 the device name and the driver's ID-table entry, and 7 and 8 the driver and device names. A NULL sixth argument means the driver has no id_table.
learner@ksa:~/gl-e05/lab-m01$ bat example.c File: example.c if (eq(a,b)) return MATCH_OF; if (eq(c,d)) return MATCH_ACPI; if (f) return eq(e,f) ? MATCH_ID : MATCH_NONE; /* a present table is terminal */ return eq(g,h) ? MATCH_NAME : MATCH_NONE;probe_status() returns 0 or -517. devres_release() walks the acquisition stack from index n - 1 down to 0, joining names with commas and checking capacity before every copy. The model leaves out driver_override, which the kernel checks before OF; section 13 asks you to add it.
The starter changes one token in the last line, returning MATCH_ID from the name branch. Both values are nonzero, so a caller that only tests for a match never notices.
step 7/15 · 7. expected output
The reference build prints TAP version 13, 1..5, five ok lines, and # pass 5/5, then exits 0. The recorded starter run passes the build, fails not ok 2 - contract group 1 with a FAIL: line, and passes groups 2 to 4, ending # pass 4/5. The recorded benchmark line is benchmark_samples=40 warmup=5 p50_ns=358 p95_ns=411 p99_ns=688.
step 8/15 · 8. explanation of observed behavior
Group 1 fails on the starter because the assertion for a name-only match expects MATCH_NAME and receives MATCH_ID. In the kernel the same confusion has a concrete cost: a driver bound by name gets NULL from platform_get_device_id(), so code that believes it matched by ID dereferences a NULL driver_data. The other three ID-table assertions still pass, which is why one wrong token survives a clean build.
Group 2 proves the stack mmio, irq, gpio releases as gpio,irq,mmio and that an empty stack yields an empty string. Group 3 combines -517 with every partial count from 0 to 3, so a deferred probe never releases something it did not acquire. Group 4 shows each invalid input returns a negative value before any write past the buffer.
step 9/15 · 9. experiment variations
Near transfer: give the driver a non-NULL ID-table entry that differs from the device name, keep the names equal, and predict the result before running it. Then add a fifth identity pair for driver_override and decide where it belongs in the chain. Far transfer: the I2C core also prefers an OF match over its own i2c_device_id table. Sketch the equivalent function for i2c_device_match() and list which assertions from group 1 carry over unchanged.
step 10/15 · 10. performance measurements
./files/contract_tests bench runs five warmup passes over all four groups, then times 40 passes with CLOCK_MONOTONIC and sorts the results. The harness reports sample 20 as p50, sample 38 as p95, and sample 39 as p99. With only 40 samples, the p99 value is the slowest run, so 688 ns against a 358 ns median reflects host scheduling noise, not a property of the code. These numbers time string comparisons in user space and must never be quoted as probe latency on the board. On the target, measure probe time with initcall_debug or by comparing kernel log timestamps for the probe call and the bound message.
step 11/15 · 11. troubleshooting
| Symptom | Root cause | Fix |
|---|---|---|
not ok 1 with a compiler error about an unused parameter | A new identity pair was added to the prototype but not used | Use every argument or remove it from match.h and both callers |
| Group 1 fails only on the name-only assertion | The name branch reports the wrong method | Return MATCH_NAME from the final comparison |
| Group 4 crashes instead of failing | A capacity check runs after memcpy | Test sep and len against the remaining space before copying |
step 12/15 · 12. guided questions
- After the ID-table branch returns
MATCH_NONE, which kernel function would the driver core call next for this device? Hint 1: matching happens once per driver. Hint 2: it moves on to the next registered driver on the bus. - Why is it safe for group 3 to call
devres_release()with a count of 0 after-517? Hint 1: what was acquired before the first supplier check? Hint 2: compare with a probe that defers on its first call.
step 13/15 · 13. independent challenges
Add driver_override as a ninth and tenth argument without breaking the existing assertions, and write one assertion that overrides an OF match. Then, on the target, give a platform device an interrupt from a controller whose driver loads as a module. Before loading the module, read /sys/kernel/debug/devices_deferred; after loading it, confirm the device disappears from that list and gains a driver link. Explain why the host model cannot produce that evidence.
step 14/15 · 14. solutions and explanations
Change the final branch back to return eq(g,h) ? MATCH_NAME : MATCH_NONE;. The repair restores the reported method without touching the ID-table rule, so all six group 1 assertions pass. The ninth-argument challenge places the override test first and returns a distinct value only when the override names this driver. For the target challenge, the deferred list shows the device while its interrupt supplier is missing, and the retry after the supplier binds succeeds because the first attempt released everything it took.
step 15/15 · run verification and inspect output
Run the verification command and confirm the output matches the expected contract.
learner@ksa:~/gl-e05/lab-m01./files/contract_tests 1learner@ksa:~/gl-e05/lab-m01TAP version 131..5ok 1 - strict C11 library, CLI, and test buildProgressive hints & solution
hint 1
Compare starter/Makefile against the module lessons. Focus first on: Strict C11 library, CLI, and test build.
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-e05/lab-m01$ bat files/Makefile File: files/Makefile CC ?= gcc CFLAGS := -std=c11 -Wall -Wextra -Werror -I. .PHONY: all clean all: libmatch.a match_cli contract_tests match.o: match.c match.h $(CC) $(CFLAGS) -c $< -o $@ libmatch.a: match.o ar rcs $@ $^ match_cli: match_cli.c libmatch.a $(CC) $(CFLAGS) $< libmatch.a -o $@ contract_tests: contract_tests.c libmatch.a $(CC) $(CFLAGS) $< libmatch.a -o $@ clean: rm -f match.o libmatch.a match_cli contract_testssolution: files/contract_tests.c
learner@ksa:~/gl-e05/lab-m01$ bat files/contract_tests.c File: files/contract_tests.c #define _POSIX_C_SOURCE 200809L #include "match.h" #include <assert.h> #include <limits.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static void check(unsigned group) { char out[32]; const char *s[]={"mmio","irq","gpio"}; if (group==1) { assert(bus_match("x","x","y","y","z","z","n","n")==MATCH_OF); assert(bus_match(NULL,NULL,"y","y","z","z","n","n")==MATCH_ACPI); assert(bus_match(NULL,NULL,NULL,NULL,"z","z","n","n")==MATCH_ID); assert(bus_match(NULL,NULL,NULL,NULL,"z","bad","n","n")==MATCH_NONE); assert(bus_match(NULL,NULL,NULL,NULL,NULL,NULL,"n","n")==MATCH_NAME); assert(bus_match(NULL,NULL,NULL,NULL,NULL,NULL,"n","bad")==MATCH_NONE); } else if (group==2) { assert(devres_release(s,3,out,sizeof out)==0); assert(strcmp(out,"gpio,irq,mmio")==0); assert(devres_release(NULL,0,out,sizeof out)==0 && out[0]=='\0'); } else if (group==3) { assert(probe_status(0)==-517 && probe_status(1)==0); for (int acquired=0; acquired<=3; ++acquired) { assert(devres_release(s,acquired,out,sizeof out)==0); assert((acquired==0)==(out[0]=='\0')); } } else { assert(devres_release(NULL,1,out,sizeof out)<0); assert(devres_release(s,-1,out,sizeof out)<0); assert(devres_release(s,3,out,2)<0); assert(devres_release(s,3,NULL,0)<0); const char *bad[]={NULL}; assert(devres_release(bad,1,out,sizeof out)<0); } } static uint64_t monotonic_ns(void) { struct timespec ts; assert(clock_gettime(CLOCK_MONOTONIC,&ts)==0); return (uint64_t)ts.tv_sec*UINT64_C(1000000000)+(uint64_t)ts.tv_nsec; } static int compare_ns(const void *a,const void *b) { uint64_t x=*(const uint64_t *)a,y=*(const uint64_t *)b; return x>y ? 1 : x<y ? -1 : 0; } static int benchmark(void) { enum { WARMUP=5, SAMPLES=40 }; uint64_t samples[SAMPLES]; for (unsigned i=0;i<WARMUP;++i) for (unsigned n=1;n<=4;++n) check(n); for (unsigned i=0;i<SAMPLES;++i) { uint64_t start=monotonic_ns(); for (unsigned n=1;n<=4;++n) check(n); samples[i]=monotonic_ns()-start; } qsort(samples,SAMPLES,sizeof(samples[0]),compare_ns); printf("benchmark_samples=40 warmup=5 p50_ns=%llu p95_ns=%llu p99_ns=%llu\n", (unsigned long long)samples[20],(unsigned long long)samples[38], (unsigned long long)samples[39]); return 0; } int main(int argc,char **argv) { if (argc==2 && strcmp(argv[1],"bench")==0) return benchmark(); if (argc==2) { unsigned n=(unsigned)strtoul(argv[1],NULL,10); if (n<1 || n>4) return 2; check(n); printf("contract_group=%u passed\n",n); return 0; } for (unsigned n=1;n<=4;++n) check(n); puts("contract_groups=4 passed"); return 0; }solution: files/match.c
learner@ksa:~/gl-e05/lab-m01$ bat files/match.c File: files/match.c #include "match.h" #include <string.h> static int eq(const char *a,const char *b) { return a && b && strcmp(a,b)==0; } int bus_match(const char *a,const char *b,const char *c,const char *d, const char *e,const char *f,const char *g,const char *h) { if (eq(a,b)) return MATCH_OF; if (eq(c,d)) return MATCH_ACPI; if (f) return eq(e,f) ? MATCH_ID : MATCH_NONE; return eq(g,h) ? MATCH_NAME : MATCH_NONE; } int probe_status(int ready) { return ready ? 0 : -517; } int devres_release(const char *const *stack,int n,char *out,size_t cap) { if (!out || !cap || n<0 || (n && !stack)) return -1; size_t used=0; out[0]='\0'; for (int i=n; i>0; --i) { if (!stack[i-1]) return -1; size_t len=strlen(stack[i-1]), sep=used ? 1U : 0U; if (sep >= cap-used || len >= cap-used-sep) return -1; if (sep) out[used++]=','; memcpy(out+used,stack[i-1],len); used+=len; out[used]='\0'; } return 0; }solution: files/match.h
learner@ksa:~/gl-e05/lab-m01$ bat files/match.h File: files/match.h #ifndef MATCH_H #define MATCH_H #include <stddef.h> enum { MATCH_NONE=0, MATCH_OF=1, MATCH_ACPI=2, MATCH_ID=3, MATCH_NAME=4 }; int bus_match(const char *,const char *,const char *,const char *, const char *,const char *,const char *,const char *); int probe_status(int ready); int devres_release(const char *const *,int,char *,size_t); #endifsolution: files/match_cli.c
learner@ksa:~/gl-e05/lab-m01$ bat files/match_cli.c File: files/match_cli.c #define _POSIX_C_SOURCE 200809L #include "match.h" #include <assert.h> #include <limits.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static void check(unsigned group) { char out[32]; const char *s[]={"mmio","irq","gpio"}; if (group==1) { assert(bus_match("x","x","y","y","z","z","n","n")==MATCH_OF); assert(bus_match(NULL,NULL,"y","y","z","z","n","n")==MATCH_ACPI); assert(bus_match(NULL,NULL,NULL,NULL,"z","z","n","n")==MATCH_ID); assert(bus_match(NULL,NULL,NULL,NULL,"z","bad","n","n")==MATCH_NONE); assert(bus_match(NULL,NULL,NULL,NULL,NULL,NULL,"n","n")==MATCH_NAME); assert(bus_match(NULL,NULL,NULL,NULL,NULL,NULL,"n","bad")==MATCH_NONE); } else if (group==2) { assert(devres_release(s,3,out,sizeof out)==0); assert(strcmp(out,"gpio,irq,mmio")==0); assert(devres_release(NULL,0,out,sizeof out)==0 && out[0]=='\0'); } else if (group==3) { assert(probe_status(0)==-517 && probe_status(1)==0); for (int acquired=0; acquired<=3; ++acquired) { assert(devres_release(s,acquired,out,sizeof out)==0); assert((acquired==0)==(out[0]=='\0')); } } else { assert(devres_release(NULL,1,out,sizeof out)<0); assert(devres_release(s,-1,out,sizeof out)<0); assert(devres_release(s,3,out,2)<0); assert(devres_release(s,3,NULL,0)<0); const char *bad[]={NULL}; assert(devres_release(bad,1,out,sizeof out)<0); } } static uint64_t monotonic_ns(void) { struct timespec ts; assert(clock_gettime(CLOCK_MONOTONIC,&ts)==0); return (uint64_t)ts.tv_sec*UINT64_C(1000000000)+(uint64_t)ts.tv_nsec; } static int compare_ns(const void *a,const void *b) { uint64_t x=*(const uint64_t *)a,y=*(const uint64_t *)b; return x>y ? 1 : x<y ? -1 : 0; } static int benchmark(void) { enum { WARMUP=5, SAMPLES=40 }; uint64_t samples[SAMPLES]; for (unsigned i=0;i<WARMUP;++i) for (unsigned n=1;n<=4;++n) check(n); for (unsigned i=0;i<SAMPLES;++i) { uint64_t start=monotonic_ns(); for (unsigned n=1;n<=4;++n) check(n); samples[i]=monotonic_ns()-start; } qsort(samples,SAMPLES,sizeof(samples[0]),compare_ns); printf("benchmark_samples=40 warmup=5 p50_ns=%llu p95_ns=%llu p99_ns=%llu\n", (unsigned long long)samples[20],(unsigned long long)samples[38], (unsigned long long)samples[39]); return 0; } int main(int argc,char **argv) { if (argc==2 && strcmp(argv[1],"bench")==0) return benchmark(); if (argc==2) { unsigned n=(unsigned)strtoul(argv[1],NULL,10); if (n<1 || n>4) return 2; check(n); printf("contract_group=%u passed\n",n); return 0; } for (unsigned n=1;n<=4;++n) check(n); puts("contract_groups=4 passed"); return 0; }Run the check
From labs/lab_m01_platform_devm/, run ./check.sh. A complete solution emits TAP version 13 with every test point marked ok and exits 0:
learner@ksa:~/gl-e05/lab-m01./check.shTAP version 131..5ok 1 - strict C11 library, CLI, and test buildok 2 - contract group 1ok 3 - contract group 2ok 4 - contract group 3ok 5 - contract group 4# 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-e05/lab-m01./check.shTAP version 131..5ok 1 - strict C11 library, CLI, and test buildnot ok 2 - contract group 1FAIL: repair functional contract group 1; see README for required behavior --- message: "repair functional contract group 1; see README for required behavior" ...ok 3 - contract group 2ok 4 - contract group 3ok 5 - contract group 4# 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-e05/lab-m01$ bat check.sh File: check.sh #!/bin/sh set -u BASE=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) WORK=$(mktemp -d "$BASE/.check.XXXXXX") || exit 1 trap 'rm -rf "$WORK"' EXIT HUP INT TERM PASS=0 printf 'TAP version 13\n1..5\n' diag() { printf 'FAIL: %s\n' "$1" printf ' ---\n message: "%s"\n ...\n' "$1" } if cp -R "$BASE/files" "$WORK/src" && make -B -C "$WORK/src" CFLAGS="-std=c11 -Wall -Wextra -Werror -I." >"$WORK/build.log" 2>&1; then printf 'ok 1 - strict C11 library, CLI, and test build\n' PASS=$((PASS+1)) else printf 'not ok 1 - strict C11 library, CLI, and test build\n' diag "repair the Makefile and warning-clean C sources" fi N=2 for GROUP in 1 2 3 4; do if [ -x "$WORK/src/contract_tests" ] && "$WORK/src/contract_tests" "$GROUP" >"$WORK/case.log" 2>&1; then printf 'ok %d - contract group %d\n' "$N" "$GROUP" PASS=$((PASS+1)) else printf 'not ok %d - contract group %d\n' "$N" "$GROUP" diag "repair functional contract group $GROUP; see README for required behavior" fi N=$((N+1)) done 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_m01_platform_devm/ so relative paths to files/ resolve directly.