test_runner.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725
  1. /*
  2. * Runner for littlefs tests
  3. *
  4. * Copyright (c) 2022, The littlefs authors.
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #ifndef _POSIX_C_SOURCE
  8. #define _POSIX_C_SOURCE 199309L
  9. #endif
  10. #include "runners/test_runner.h"
  11. #include "bd/lfs_emubd.h"
  12. #include <getopt.h>
  13. #include <sys/types.h>
  14. #include <errno.h>
  15. #include <setjmp.h>
  16. #include <fcntl.h>
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <time.h>
  21. #include <execinfo.h>
  22. // some helpers
  23. // append to an array with amortized doubling
  24. void *mappend(void **p,
  25. size_t size,
  26. size_t *count,
  27. size_t *capacity) {
  28. uint8_t *p_ = *p;
  29. size_t count_ = *count;
  30. size_t capacity_ = *capacity;
  31. count_ += 1;
  32. if (count_ > capacity_) {
  33. capacity_ = (2*capacity_ < 4) ? 4 : 2*capacity_;
  34. p_ = realloc(p_, capacity_*size);
  35. if (!p_) {
  36. return NULL;
  37. }
  38. }
  39. *p = p_;
  40. *count = count_;
  41. *capacity = capacity_;
  42. return &p_[(count_-1)*size];
  43. }
  44. // a quick self-terminating text-safe varint scheme
  45. static void leb16_print(uintmax_t x) {
  46. while (true) {
  47. char nibble = (x & 0xf) | (x > 0xf ? 0x10 : 0);
  48. printf("%c", (nibble < 10) ? '0'+nibble : 'a'+nibble-10);
  49. if (x <= 0xf) {
  50. break;
  51. }
  52. x >>= 4;
  53. }
  54. }
  55. static uintmax_t leb16_parse(const char *s, char **tail) {
  56. uintmax_t x = 0;
  57. size_t i = 0;
  58. while (true) {
  59. uintmax_t nibble = s[i];
  60. if (nibble >= '0' && nibble <= '9') {
  61. nibble = nibble - '0';
  62. } else if (nibble >= 'a' && nibble <= 'v') {
  63. nibble = nibble - 'a' + 10;
  64. } else {
  65. // invalid?
  66. if (tail) {
  67. *tail = (char*)s;
  68. }
  69. return 0;
  70. }
  71. x |= (nibble & 0xf) << (4*i);
  72. i += 1;
  73. if (!(nibble & 0x10)) {
  74. break;
  75. }
  76. }
  77. if (tail) {
  78. *tail = (char*)s + i;
  79. }
  80. return x;
  81. }
  82. // test_runner types
  83. typedef struct test_geometry {
  84. const char *name;
  85. test_define_t defines[TEST_GEOMETRY_DEFINE_COUNT];
  86. } test_geometry_t;
  87. typedef struct test_powerloss {
  88. const char *name;
  89. void (*run)(
  90. const lfs_emubd_powercycles_t *cycles,
  91. size_t cycle_count,
  92. const struct test_suite *suite,
  93. const struct test_case *case_);
  94. const lfs_emubd_powercycles_t *cycles;
  95. size_t cycle_count;
  96. } test_powerloss_t;
  97. typedef struct test_id {
  98. const char *name;
  99. const test_define_t *defines;
  100. size_t define_count;
  101. const lfs_emubd_powercycles_t *cycles;
  102. size_t cycle_count;
  103. } test_id_t;
  104. // test suites are linked into a custom ld section
  105. extern struct test_suite __start__test_suites;
  106. extern struct test_suite __stop__test_suites;
  107. const struct test_suite *test_suites = &__start__test_suites;
  108. #define TEST_SUITE_COUNT \
  109. ((size_t)(&__stop__test_suites - &__start__test_suites))
  110. // test define management
  111. typedef struct test_define_map {
  112. const test_define_t *defines;
  113. size_t count;
  114. } test_define_map_t;
  115. typedef struct test_define_names {
  116. const char *const *names;
  117. size_t count;
  118. } test_define_names_t;
  119. intmax_t test_define_lit(void *data) {
  120. return (intptr_t)data;
  121. }
  122. #define TEST_CONST(x) {test_define_lit, (void*)(uintptr_t)(x)}
  123. #define TEST_LIT(x) ((test_define_t)TEST_CONST(x))
  124. #define TEST_DEF(k, v) \
  125. intmax_t test_define_##k(void *data) { \
  126. (void)data; \
  127. return v; \
  128. }
  129. TEST_IMPLICIT_DEFINES
  130. #undef TEST_DEF
  131. #define TEST_DEFINE_MAP_EXPLICIT 0
  132. #define TEST_DEFINE_MAP_OVERRIDE 1
  133. #define TEST_DEFINE_MAP_PERMUTATION 2
  134. #define TEST_DEFINE_MAP_GEOMETRY 3
  135. #define TEST_DEFINE_MAP_IMPLICIT 4
  136. #define TEST_DEFINE_MAP_COUNT 5
  137. test_define_map_t test_define_maps[TEST_DEFINE_MAP_COUNT] = {
  138. [TEST_DEFINE_MAP_IMPLICIT] = {
  139. (const test_define_t[TEST_IMPLICIT_DEFINE_COUNT]) {
  140. #define TEST_DEF(k, v) \
  141. [k##_i] = {test_define_##k, NULL},
  142. TEST_IMPLICIT_DEFINES
  143. #undef TEST_DEF
  144. },
  145. TEST_IMPLICIT_DEFINE_COUNT,
  146. },
  147. };
  148. #define TEST_DEFINE_NAMES_SUITE 0
  149. #define TEST_DEFINE_NAMES_IMPLICIT 1
  150. #define TEST_DEFINE_NAMES_COUNT 2
  151. test_define_names_t test_define_names[TEST_DEFINE_NAMES_COUNT] = {
  152. [TEST_DEFINE_NAMES_IMPLICIT] = {
  153. (const char *const[TEST_IMPLICIT_DEFINE_COUNT]){
  154. #define TEST_DEF(k, v) \
  155. [k##_i] = #k,
  156. TEST_IMPLICIT_DEFINES
  157. #undef TEST_DEF
  158. },
  159. TEST_IMPLICIT_DEFINE_COUNT,
  160. },
  161. };
  162. intmax_t *test_define_cache;
  163. size_t test_define_cache_count;
  164. unsigned *test_define_cache_mask;
  165. const char *test_define_name(size_t define) {
  166. // lookup in our test names
  167. for (size_t i = 0; i < TEST_DEFINE_NAMES_COUNT; i++) {
  168. if (define < test_define_names[i].count
  169. && test_define_names[i].names
  170. && test_define_names[i].names[define]) {
  171. return test_define_names[i].names[define];
  172. }
  173. }
  174. return NULL;
  175. }
  176. bool test_define_ispermutation(size_t define) {
  177. // is this define specific to the permutation?
  178. for (size_t i = 0; i < TEST_DEFINE_MAP_IMPLICIT; i++) {
  179. if (define < test_define_maps[i].count
  180. && test_define_maps[i].defines[define].cb) {
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. intmax_t test_define(size_t define) {
  187. // is the define in our cache?
  188. if (define < test_define_cache_count
  189. && (test_define_cache_mask[define/(8*sizeof(unsigned))]
  190. & (1 << (define%(8*sizeof(unsigned)))))) {
  191. return test_define_cache[define];
  192. }
  193. // lookup in our test defines
  194. for (size_t i = 0; i < TEST_DEFINE_MAP_COUNT; i++) {
  195. if (define < test_define_maps[i].count
  196. && test_define_maps[i].defines[define].cb) {
  197. intmax_t v = test_define_maps[i].defines[define].cb(
  198. test_define_maps[i].defines[define].data);
  199. // insert into cache!
  200. test_define_cache[define] = v;
  201. test_define_cache_mask[define / (8*sizeof(unsigned))]
  202. |= 1 << (define%(8*sizeof(unsigned)));
  203. return v;
  204. }
  205. }
  206. return 0;
  207. // not found?
  208. const char *name = test_define_name(define);
  209. fprintf(stderr, "error: undefined define %s (%zd)\n",
  210. name ? name : "(unknown)",
  211. define);
  212. assert(false);
  213. exit(-1);
  214. }
  215. void test_define_flush(void) {
  216. // clear cache between permutations
  217. memset(test_define_cache_mask, 0,
  218. sizeof(unsigned)*(
  219. (test_define_cache_count+(8*sizeof(unsigned))-1)
  220. / (8*sizeof(unsigned))));
  221. }
  222. // geometry updates
  223. const test_geometry_t *test_geometry = NULL;
  224. void test_define_geometry(const test_geometry_t *geometry) {
  225. test_define_maps[TEST_DEFINE_MAP_GEOMETRY] = (test_define_map_t){
  226. geometry->defines, TEST_GEOMETRY_DEFINE_COUNT};
  227. }
  228. // override updates
  229. typedef struct test_override {
  230. const char *name;
  231. const intmax_t *defines;
  232. size_t permutations;
  233. } test_override_t;
  234. const test_override_t *test_overrides = NULL;
  235. size_t test_override_count = 0;
  236. test_define_t *test_override_defines = NULL;
  237. size_t test_override_define_count = 0;
  238. size_t test_override_define_permutations = 1;
  239. size_t test_override_define_capacity = 0;
  240. // suite/perm updates
  241. void test_define_suite(const struct test_suite *suite) {
  242. test_define_names[TEST_DEFINE_NAMES_SUITE] = (test_define_names_t){
  243. suite->define_names, suite->define_count};
  244. // make sure our cache is large enough
  245. if (lfs_max(suite->define_count, TEST_IMPLICIT_DEFINE_COUNT)
  246. > test_define_cache_count) {
  247. // align to power of two to avoid any superlinear growth
  248. size_t ncount = 1 << lfs_npw2(
  249. lfs_max(suite->define_count, TEST_IMPLICIT_DEFINE_COUNT));
  250. test_define_cache = realloc(test_define_cache, ncount*sizeof(intmax_t));
  251. test_define_cache_mask = realloc(test_define_cache_mask,
  252. sizeof(unsigned)*(
  253. (ncount+(8*sizeof(unsigned))-1)
  254. / (8*sizeof(unsigned))));
  255. test_define_cache_count = ncount;
  256. }
  257. // map any overrides
  258. if (test_override_count > 0) {
  259. // first figure out the total size of override permutations
  260. size_t count = 0;
  261. size_t permutations = 1;
  262. for (size_t i = 0; i < test_override_count; i++) {
  263. for (size_t d = 0;
  264. d < lfs_max(
  265. suite->define_count,
  266. TEST_IMPLICIT_DEFINE_COUNT);
  267. d++) {
  268. // define name match?
  269. const char *name = test_define_name(d);
  270. if (name && strcmp(name, test_overrides[i].name) == 0) {
  271. count = lfs_max(count, d+1);
  272. permutations *= test_overrides[i].permutations;
  273. break;
  274. }
  275. }
  276. }
  277. test_override_define_count = count;
  278. test_override_define_permutations = permutations;
  279. // make sure our override arrays are big enough
  280. if (count * permutations > test_override_define_capacity) {
  281. // align to power of two to avoid any superlinear growth
  282. size_t ncapacity = 1 << lfs_npw2(count * permutations);
  283. test_override_defines = realloc(
  284. test_override_defines,
  285. sizeof(test_define_t)*ncapacity);
  286. test_override_define_capacity = ncapacity;
  287. }
  288. // zero unoverridden defines
  289. memset(test_override_defines, 0,
  290. sizeof(test_define_t) * count * permutations);
  291. // compute permutations
  292. size_t p = 1;
  293. for (size_t i = 0; i < test_override_count; i++) {
  294. for (size_t d = 0;
  295. d < lfs_max(
  296. suite->define_count,
  297. TEST_IMPLICIT_DEFINE_COUNT);
  298. d++) {
  299. // define name match?
  300. const char *name = test_define_name(d);
  301. if (name && strcmp(name, test_overrides[i].name) == 0) {
  302. // scatter the define permutations based on already
  303. // seen permutations
  304. for (size_t j = 0; j < permutations; j++) {
  305. test_override_defines[j*count + d] = TEST_LIT(
  306. test_overrides[i].defines[(j/p)
  307. % test_overrides[i].permutations]);
  308. }
  309. // keep track of how many permutations we've seen so far
  310. p *= test_overrides[i].permutations;
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. void test_define_perm(
  318. const struct test_suite *suite,
  319. const struct test_case *case_,
  320. size_t perm) {
  321. if (case_->defines) {
  322. test_define_maps[TEST_DEFINE_MAP_PERMUTATION] = (test_define_map_t){
  323. case_->defines + perm*suite->define_count,
  324. suite->define_count};
  325. } else {
  326. test_define_maps[TEST_DEFINE_MAP_PERMUTATION] = (test_define_map_t){
  327. NULL, 0};
  328. }
  329. }
  330. void test_define_override(size_t perm) {
  331. test_define_maps[TEST_DEFINE_MAP_OVERRIDE] = (test_define_map_t){
  332. test_override_defines + perm*test_override_define_count,
  333. test_override_define_count};
  334. }
  335. void test_define_explicit(
  336. const test_define_t *defines,
  337. size_t define_count) {
  338. test_define_maps[TEST_DEFINE_MAP_EXPLICIT] = (test_define_map_t){
  339. defines, define_count};
  340. }
  341. void test_define_cleanup(void) {
  342. // test define management can allocate a few things
  343. free(test_define_cache);
  344. free(test_define_cache_mask);
  345. free(test_override_defines);
  346. }
  347. // test state
  348. extern const test_geometry_t *test_geometries;
  349. extern size_t test_geometry_count;
  350. extern const test_powerloss_t *test_powerlosses;
  351. extern size_t test_powerloss_count;
  352. const test_id_t *test_ids = (const test_id_t[]) {
  353. {NULL, NULL, 0, NULL, 0},
  354. };
  355. size_t test_id_count = 1;
  356. size_t test_step_start = 0;
  357. size_t test_step_stop = -1;
  358. size_t test_step_step = 1;
  359. const char *test_disk_path = NULL;
  360. const char *test_trace_path = NULL;
  361. bool test_trace_backtrace = false;
  362. uint32_t test_trace_period = 0;
  363. uint32_t test_trace_freq = 0;
  364. FILE *test_trace_file = NULL;
  365. uint32_t test_trace_cycles = 0;
  366. uint64_t test_trace_time = 0;
  367. uint64_t test_trace_open_time = 0;
  368. lfs_emubd_sleep_t test_read_sleep = 0.0;
  369. lfs_emubd_sleep_t test_prog_sleep = 0.0;
  370. lfs_emubd_sleep_t test_erase_sleep = 0.0;
  371. // this determines both the backtrace buffer and the trace printf buffer, if
  372. // trace ends up interleaved or truncated this may need to be increased
  373. #ifndef TEST_TRACE_BACKTRACE_BUFFER_SIZE
  374. #define TEST_TRACE_BACKTRACE_BUFFER_SIZE 8192
  375. #endif
  376. void *test_trace_backtrace_buffer[
  377. TEST_TRACE_BACKTRACE_BUFFER_SIZE / sizeof(void*)];
  378. // trace printing
  379. void test_trace(const char *fmt, ...) {
  380. if (test_trace_path) {
  381. // sample at a specific period?
  382. if (test_trace_period) {
  383. if (test_trace_cycles % test_trace_period != 0) {
  384. test_trace_cycles += 1;
  385. return;
  386. }
  387. test_trace_cycles += 1;
  388. }
  389. // sample at a specific frequency?
  390. if (test_trace_freq) {
  391. struct timespec t;
  392. clock_gettime(CLOCK_MONOTONIC, &t);
  393. uint64_t now = (uint64_t)t.tv_sec*1000*1000*1000
  394. + (uint64_t)t.tv_nsec;
  395. if (now - test_trace_time < (1000*1000*1000) / test_trace_freq) {
  396. return;
  397. }
  398. test_trace_time = now;
  399. }
  400. if (!test_trace_file) {
  401. // Tracing output is heavy and trying to open every trace
  402. // call is slow, so we only try to open the trace file every
  403. // so often. Note this doesn't affect successfully opened files
  404. struct timespec t;
  405. clock_gettime(CLOCK_MONOTONIC, &t);
  406. uint64_t now = (uint64_t)t.tv_sec*1000*1000*1000
  407. + (uint64_t)t.tv_nsec;
  408. if (now - test_trace_open_time < 100*1000*1000) {
  409. return;
  410. }
  411. test_trace_open_time = now;
  412. // try to open the trace file
  413. int fd;
  414. if (strcmp(test_trace_path, "-") == 0) {
  415. fd = dup(1);
  416. if (fd < 0) {
  417. return;
  418. }
  419. } else {
  420. fd = open(
  421. test_trace_path,
  422. O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK,
  423. 0666);
  424. if (fd < 0) {
  425. return;
  426. }
  427. int err = fcntl(fd, F_SETFL, O_WRONLY | O_CREAT | O_APPEND);
  428. assert(!err);
  429. }
  430. FILE *f = fdopen(fd, "a");
  431. assert(f);
  432. int err = setvbuf(f, NULL, _IOFBF,
  433. TEST_TRACE_BACKTRACE_BUFFER_SIZE);
  434. assert(!err);
  435. test_trace_file = f;
  436. }
  437. // print trace
  438. va_list va;
  439. va_start(va, fmt);
  440. int res = vfprintf(test_trace_file, fmt, va);
  441. va_end(va);
  442. if (res < 0) {
  443. fclose(test_trace_file);
  444. test_trace_file = NULL;
  445. return;
  446. }
  447. if (test_trace_backtrace) {
  448. // print backtrace
  449. size_t count = backtrace(
  450. test_trace_backtrace_buffer,
  451. TEST_TRACE_BACKTRACE_BUFFER_SIZE);
  452. // note we skip our own stack frame
  453. for (size_t i = 1; i < count; i++) {
  454. res = fprintf(test_trace_file, "\tat %p\n",
  455. test_trace_backtrace_buffer[i]);
  456. if (res < 0) {
  457. fclose(test_trace_file);
  458. test_trace_file = NULL;
  459. return;
  460. }
  461. }
  462. }
  463. // flush immediately
  464. fflush(test_trace_file);
  465. }
  466. }
  467. // encode our permutation into a reusable id
  468. static void perm_printid(
  469. const struct test_suite *suite,
  470. const struct test_case *case_,
  471. const lfs_emubd_powercycles_t *cycles,
  472. size_t cycle_count) {
  473. (void)suite;
  474. // case[:permutation[:powercycles]]
  475. printf("%s:", case_->name);
  476. for (size_t d = 0;
  477. d < lfs_max(
  478. suite->define_count,
  479. TEST_IMPLICIT_DEFINE_COUNT);
  480. d++) {
  481. if (test_define_ispermutation(d)) {
  482. leb16_print(d);
  483. leb16_print(TEST_DEFINE(d));
  484. }
  485. }
  486. // only print power-cycles if any occured
  487. if (cycles) {
  488. printf(":");
  489. for (size_t i = 0; i < cycle_count; i++) {
  490. leb16_print(cycles[i]);
  491. }
  492. }
  493. }
  494. // a quick trie for keeping track of permutations we've seen
  495. typedef struct test_seen {
  496. struct test_seen_branch *branches;
  497. size_t branch_count;
  498. size_t branch_capacity;
  499. } test_seen_t;
  500. struct test_seen_branch {
  501. intmax_t define;
  502. struct test_seen branch;
  503. };
  504. bool test_seen_insert(
  505. test_seen_t *seen,
  506. const struct test_suite *suite,
  507. const struct test_case *case_) {
  508. (void)case_;
  509. bool was_seen = true;
  510. // use the currently set defines
  511. for (size_t d = 0;
  512. d < lfs_max(
  513. suite->define_count,
  514. TEST_IMPLICIT_DEFINE_COUNT);
  515. d++) {
  516. // treat unpermuted defines the same as 0
  517. intmax_t define = test_define_ispermutation(d) ? TEST_DEFINE(d) : 0;
  518. // already seen?
  519. struct test_seen_branch *branch = NULL;
  520. for (size_t i = 0; i < seen->branch_count; i++) {
  521. if (seen->branches[i].define == define) {
  522. branch = &seen->branches[i];
  523. break;
  524. }
  525. }
  526. // need to create a new node
  527. if (!branch) {
  528. was_seen = false;
  529. branch = mappend(
  530. (void**)&seen->branches,
  531. sizeof(struct test_seen_branch),
  532. &seen->branch_count,
  533. &seen->branch_capacity);
  534. branch->define = define;
  535. branch->branch = (test_seen_t){NULL, 0, 0};
  536. }
  537. seen = &branch->branch;
  538. }
  539. return was_seen;
  540. }
  541. void test_seen_cleanup(test_seen_t *seen) {
  542. for (size_t i = 0; i < seen->branch_count; i++) {
  543. test_seen_cleanup(&seen->branches[i].branch);
  544. }
  545. free(seen->branches);
  546. }
  547. static void run_powerloss_none(
  548. const lfs_emubd_powercycles_t *cycles,
  549. size_t cycle_count,
  550. const struct test_suite *suite,
  551. const struct test_case *case_);
  552. static void run_powerloss_cycles(
  553. const lfs_emubd_powercycles_t *cycles,
  554. size_t cycle_count,
  555. const struct test_suite *suite,
  556. const struct test_case *case_);
  557. // iterate through permutations in a test case
  558. static void case_forperm(
  559. const struct test_suite *suite,
  560. const struct test_case *case_,
  561. const test_define_t *defines,
  562. size_t define_count,
  563. const lfs_emubd_powercycles_t *cycles,
  564. size_t cycle_count,
  565. void (*cb)(
  566. void *data,
  567. const struct test_suite *suite,
  568. const struct test_case *case_,
  569. const test_powerloss_t *powerloss),
  570. void *data) {
  571. if (defines) {
  572. test_define_explicit(defines, define_count);
  573. test_define_flush();
  574. if (cycles) {
  575. cb(data, suite, case_, &(test_powerloss_t){
  576. .run=run_powerloss_cycles,
  577. .cycles=cycles,
  578. .cycle_count=cycle_count});
  579. } else {
  580. for (size_t p = 0; p < test_powerloss_count; p++) {
  581. // skip non-reentrant tests when powerloss testing
  582. if (test_powerlosses[p].run != run_powerloss_none
  583. && !(case_->flags & TEST_REENTRANT)) {
  584. continue;
  585. }
  586. cb(data, suite, case_, &test_powerlosses[p]);
  587. }
  588. }
  589. return;
  590. }
  591. test_seen_t seen = {NULL, 0, 0};
  592. for (size_t k = 0; k < case_->permutations; k++) {
  593. // define permutation
  594. test_define_perm(suite, case_, k);
  595. for (size_t v = 0; v < test_override_define_permutations; v++) {
  596. // define override permutation
  597. test_define_override(v);
  598. for (size_t g = 0; g < test_geometry_count; g++) {
  599. // define geometry
  600. test_define_geometry(&test_geometries[g]);
  601. test_define_flush();
  602. // have we seen this permutation before?
  603. bool was_seen = test_seen_insert(&seen, suite, case_);
  604. if (!(k == 0 && v == 0 && g == 0) && was_seen) {
  605. continue;
  606. }
  607. if (cycles) {
  608. cb(data, suite, case_, &(test_powerloss_t){
  609. .run=run_powerloss_cycles,
  610. .cycles=cycles,
  611. .cycle_count=cycle_count});
  612. } else {
  613. for (size_t p = 0; p < test_powerloss_count; p++) {
  614. // skip non-reentrant tests when powerloss testing
  615. if (test_powerlosses[p].run != run_powerloss_none
  616. && !(case_->flags & TEST_REENTRANT)) {
  617. continue;
  618. }
  619. cb(data, suite, case_, &test_powerlosses[p]);
  620. }
  621. }
  622. }
  623. }
  624. }
  625. test_seen_cleanup(&seen);
  626. }
  627. // how many permutations are there actually in a test case
  628. struct perm_count_state {
  629. size_t total;
  630. size_t filtered;
  631. };
  632. void perm_count(
  633. void *data,
  634. const struct test_suite *suite,
  635. const struct test_case *case_,
  636. const test_powerloss_t *powerloss) {
  637. struct perm_count_state *state = data;
  638. (void)suite;
  639. (void)case_;
  640. (void)powerloss;
  641. state->total += 1;
  642. if (case_->filter && !case_->filter()) {
  643. return;
  644. }
  645. state->filtered += 1;
  646. }
  647. // operations we can do
  648. static void summary(void) {
  649. printf("%-23s %7s %7s %7s %11s\n",
  650. "", "flags", "suites", "cases", "perms");
  651. size_t suites = 0;
  652. size_t cases = 0;
  653. test_flags_t flags = 0;
  654. struct perm_count_state perms = {0, 0};
  655. for (size_t t = 0; t < test_id_count; t++) {
  656. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  657. test_define_suite(&test_suites[i]);
  658. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  659. // does neither suite nor case name match?
  660. if (test_ids[t].name && !(
  661. strcmp(test_ids[t].name,
  662. test_suites[i].name) == 0
  663. || strcmp(test_ids[t].name,
  664. test_suites[i].cases[j].name) == 0)) {
  665. continue;
  666. }
  667. cases += 1;
  668. case_forperm(
  669. &test_suites[i],
  670. &test_suites[i].cases[j],
  671. test_ids[t].defines,
  672. test_ids[t].define_count,
  673. test_ids[t].cycles,
  674. test_ids[t].cycle_count,
  675. perm_count,
  676. &perms);
  677. }
  678. suites += 1;
  679. flags |= test_suites[i].flags;
  680. }
  681. }
  682. char perm_buf[64];
  683. sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
  684. char flag_buf[64];
  685. sprintf(flag_buf, "%s%s",
  686. (flags & TEST_REENTRANT) ? "r" : "",
  687. (!flags) ? "-" : "");
  688. printf("%-23s %7s %7zu %7zu %11s\n",
  689. "TOTAL",
  690. flag_buf,
  691. suites,
  692. cases,
  693. perm_buf);
  694. }
  695. static void list_suites(void) {
  696. // at least size so that names fit
  697. unsigned name_width = 23;
  698. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  699. size_t len = strlen(test_suites[i].name);
  700. if (len > name_width) {
  701. name_width = len;
  702. }
  703. }
  704. name_width = 4*((name_width+1+4-1)/4)-1;
  705. printf("%-*s %7s %7s %11s\n",
  706. name_width, "suite", "flags", "cases", "perms");
  707. for (size_t t = 0; t < test_id_count; t++) {
  708. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  709. test_define_suite(&test_suites[i]);
  710. size_t cases = 0;
  711. struct perm_count_state perms = {0, 0};
  712. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  713. // does neither suite nor case name match?
  714. if (test_ids[t].name && !(
  715. strcmp(test_ids[t].name,
  716. test_suites[i].name) == 0
  717. || strcmp(test_ids[t].name,
  718. test_suites[i].cases[j].name) == 0)) {
  719. continue;
  720. }
  721. cases += 1;
  722. case_forperm(
  723. &test_suites[i],
  724. &test_suites[i].cases[j],
  725. test_ids[t].defines,
  726. test_ids[t].define_count,
  727. test_ids[t].cycles,
  728. test_ids[t].cycle_count,
  729. perm_count,
  730. &perms);
  731. }
  732. // no tests found?
  733. if (!cases) {
  734. continue;
  735. }
  736. char perm_buf[64];
  737. sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
  738. char flag_buf[64];
  739. sprintf(flag_buf, "%s%s",
  740. (test_suites[i].flags & TEST_REENTRANT) ? "r" : "",
  741. (!test_suites[i].flags) ? "-" : "");
  742. printf("%-*s %7s %7zu %11s\n",
  743. name_width,
  744. test_suites[i].name,
  745. flag_buf,
  746. cases,
  747. perm_buf);
  748. }
  749. }
  750. }
  751. static void list_cases(void) {
  752. // at least size so that names fit
  753. unsigned name_width = 23;
  754. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  755. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  756. size_t len = strlen(test_suites[i].cases[j].name);
  757. if (len > name_width) {
  758. name_width = len;
  759. }
  760. }
  761. }
  762. name_width = 4*((name_width+1+4-1)/4)-1;
  763. printf("%-*s %7s %11s\n", name_width, "case", "flags", "perms");
  764. for (size_t t = 0; t < test_id_count; t++) {
  765. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  766. test_define_suite(&test_suites[i]);
  767. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  768. // does neither suite nor case name match?
  769. if (test_ids[t].name && !(
  770. strcmp(test_ids[t].name,
  771. test_suites[i].name) == 0
  772. || strcmp(test_ids[t].name,
  773. test_suites[i].cases[j].name) == 0)) {
  774. continue;
  775. }
  776. struct perm_count_state perms = {0, 0};
  777. case_forperm(
  778. &test_suites[i],
  779. &test_suites[i].cases[j],
  780. test_ids[t].defines,
  781. test_ids[t].define_count,
  782. test_ids[t].cycles,
  783. test_ids[t].cycle_count,
  784. perm_count,
  785. &perms);
  786. char perm_buf[64];
  787. sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
  788. char flag_buf[64];
  789. sprintf(flag_buf, "%s%s",
  790. (test_suites[i].cases[j].flags & TEST_REENTRANT)
  791. ? "r" : "",
  792. (!test_suites[i].cases[j].flags)
  793. ? "-" : "");
  794. printf("%-*s %7s %11s\n",
  795. name_width,
  796. test_suites[i].cases[j].name,
  797. flag_buf,
  798. perm_buf);
  799. }
  800. }
  801. }
  802. }
  803. static void list_suite_paths(void) {
  804. // at least size so that names fit
  805. unsigned name_width = 23;
  806. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  807. size_t len = strlen(test_suites[i].name);
  808. if (len > name_width) {
  809. name_width = len;
  810. }
  811. }
  812. name_width = 4*((name_width+1+4-1)/4)-1;
  813. printf("%-*s %s\n", name_width, "suite", "path");
  814. for (size_t t = 0; t < test_id_count; t++) {
  815. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  816. size_t cases = 0;
  817. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  818. // does neither suite nor case name match?
  819. if (test_ids[t].name && !(
  820. strcmp(test_ids[t].name,
  821. test_suites[i].name) == 0
  822. || strcmp(test_ids[t].name,
  823. test_suites[i].cases[j].name) == 0)) {
  824. continue;
  825. }
  826. cases += 1;
  827. }
  828. // no tests found?
  829. if (!cases) {
  830. continue;
  831. }
  832. printf("%-*s %s\n",
  833. name_width,
  834. test_suites[i].name,
  835. test_suites[i].path);
  836. }
  837. }
  838. }
  839. static void list_case_paths(void) {
  840. // at least size so that names fit
  841. unsigned name_width = 23;
  842. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  843. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  844. size_t len = strlen(test_suites[i].cases[j].name);
  845. if (len > name_width) {
  846. name_width = len;
  847. }
  848. }
  849. }
  850. name_width = 4*((name_width+1+4-1)/4)-1;
  851. printf("%-*s %s\n", name_width, "case", "path");
  852. for (size_t t = 0; t < test_id_count; t++) {
  853. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  854. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  855. // does neither suite nor case name match?
  856. if (test_ids[t].name && !(
  857. strcmp(test_ids[t].name,
  858. test_suites[i].name) == 0
  859. || strcmp(test_ids[t].name,
  860. test_suites[i].cases[j].name) == 0)) {
  861. continue;
  862. }
  863. printf("%-*s %s\n",
  864. name_width,
  865. test_suites[i].cases[j].name,
  866. test_suites[i].cases[j].path);
  867. }
  868. }
  869. }
  870. }
  871. struct list_defines_define {
  872. const char *name;
  873. intmax_t *values;
  874. size_t value_count;
  875. size_t value_capacity;
  876. };
  877. struct list_defines_defines {
  878. struct list_defines_define *defines;
  879. size_t define_count;
  880. size_t define_capacity;
  881. };
  882. static void list_defines_add(
  883. struct list_defines_defines *defines,
  884. size_t d) {
  885. const char *name = test_define_name(d);
  886. intmax_t value = TEST_DEFINE(d);
  887. // define already in defines?
  888. for (size_t i = 0; i < defines->define_count; i++) {
  889. if (strcmp(defines->defines[i].name, name) == 0) {
  890. // value already in values?
  891. for (size_t j = 0; j < defines->defines[i].value_count; j++) {
  892. if (defines->defines[i].values[j] == value) {
  893. return;
  894. }
  895. }
  896. *(intmax_t*)mappend(
  897. (void**)&defines->defines[i].values,
  898. sizeof(intmax_t),
  899. &defines->defines[i].value_count,
  900. &defines->defines[i].value_capacity) = value;
  901. return;
  902. }
  903. }
  904. // new define?
  905. struct list_defines_define *define = mappend(
  906. (void**)&defines->defines,
  907. sizeof(struct list_defines_define),
  908. &defines->define_count,
  909. &defines->define_capacity);
  910. define->name = name;
  911. define->values = malloc(sizeof(intmax_t));
  912. define->values[0] = value;
  913. define->value_count = 1;
  914. define->value_capacity = 1;
  915. }
  916. void perm_list_defines(
  917. void *data,
  918. const struct test_suite *suite,
  919. const struct test_case *case_,
  920. const test_powerloss_t *powerloss) {
  921. struct list_defines_defines *defines = data;
  922. (void)suite;
  923. (void)case_;
  924. (void)powerloss;
  925. // collect defines
  926. for (size_t d = 0;
  927. d < lfs_max(suite->define_count,
  928. TEST_IMPLICIT_DEFINE_COUNT);
  929. d++) {
  930. if (d < TEST_IMPLICIT_DEFINE_COUNT
  931. || test_define_ispermutation(d)) {
  932. list_defines_add(defines, d);
  933. }
  934. }
  935. }
  936. void perm_list_permutation_defines(
  937. void *data,
  938. const struct test_suite *suite,
  939. const struct test_case *case_,
  940. const test_powerloss_t *powerloss) {
  941. struct list_defines_defines *defines = data;
  942. (void)suite;
  943. (void)case_;
  944. (void)powerloss;
  945. // collect permutation_defines
  946. for (size_t d = 0;
  947. d < lfs_max(suite->define_count,
  948. TEST_IMPLICIT_DEFINE_COUNT);
  949. d++) {
  950. if (test_define_ispermutation(d)) {
  951. list_defines_add(defines, d);
  952. }
  953. }
  954. }
  955. extern const test_geometry_t builtin_geometries[];
  956. static void list_defines(void) {
  957. struct list_defines_defines defines = {NULL, 0, 0};
  958. // add defines
  959. for (size_t t = 0; t < test_id_count; t++) {
  960. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  961. test_define_suite(&test_suites[i]);
  962. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  963. // does neither suite nor case name match?
  964. if (test_ids[t].name && !(
  965. strcmp(test_ids[t].name,
  966. test_suites[i].name) == 0
  967. || strcmp(test_ids[t].name,
  968. test_suites[i].cases[j].name) == 0)) {
  969. continue;
  970. }
  971. case_forperm(
  972. &test_suites[i],
  973. &test_suites[i].cases[j],
  974. test_ids[t].defines,
  975. test_ids[t].define_count,
  976. test_ids[t].cycles,
  977. test_ids[t].cycle_count,
  978. perm_list_defines,
  979. &defines);
  980. }
  981. }
  982. }
  983. for (size_t i = 0; i < defines.define_count; i++) {
  984. printf("%s=", defines.defines[i].name);
  985. for (size_t j = 0; j < defines.defines[i].value_count; j++) {
  986. printf("%jd", defines.defines[i].values[j]);
  987. if (j != defines.defines[i].value_count-1) {
  988. printf(",");
  989. }
  990. }
  991. printf("\n");
  992. }
  993. for (size_t i = 0; i < defines.define_count; i++) {
  994. free(defines.defines[i].values);
  995. }
  996. free(defines.defines);
  997. }
  998. static void list_permutation_defines(void) {
  999. struct list_defines_defines defines = {NULL, 0, 0};
  1000. // add permutation defines
  1001. for (size_t t = 0; t < test_id_count; t++) {
  1002. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  1003. test_define_suite(&test_suites[i]);
  1004. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  1005. // does neither suite nor case name match?
  1006. if (test_ids[t].name && !(
  1007. strcmp(test_ids[t].name,
  1008. test_suites[i].name) == 0
  1009. || strcmp(test_ids[t].name,
  1010. test_suites[i].cases[j].name) == 0)) {
  1011. continue;
  1012. }
  1013. case_forperm(
  1014. &test_suites[i],
  1015. &test_suites[i].cases[j],
  1016. test_ids[t].defines,
  1017. test_ids[t].define_count,
  1018. test_ids[t].cycles,
  1019. test_ids[t].cycle_count,
  1020. perm_list_permutation_defines,
  1021. &defines);
  1022. }
  1023. }
  1024. }
  1025. for (size_t i = 0; i < defines.define_count; i++) {
  1026. printf("%s=", defines.defines[i].name);
  1027. for (size_t j = 0; j < defines.defines[i].value_count; j++) {
  1028. printf("%jd", defines.defines[i].values[j]);
  1029. if (j != defines.defines[i].value_count-1) {
  1030. printf(",");
  1031. }
  1032. }
  1033. printf("\n");
  1034. }
  1035. for (size_t i = 0; i < defines.define_count; i++) {
  1036. free(defines.defines[i].values);
  1037. }
  1038. free(defines.defines);
  1039. }
  1040. static void list_implicit_defines(void) {
  1041. struct list_defines_defines defines = {NULL, 0, 0};
  1042. // yes we do need to define a suite, this does a bit of bookeeping
  1043. // such as setting up the define cache
  1044. test_define_suite(&(const struct test_suite){0});
  1045. // make sure to include builtin geometries here
  1046. extern const test_geometry_t builtin_geometries[];
  1047. for (size_t g = 0; builtin_geometries[g].name; g++) {
  1048. test_define_geometry(&builtin_geometries[g]);
  1049. test_define_flush();
  1050. // add implicit defines
  1051. for (size_t d = 0; d < TEST_IMPLICIT_DEFINE_COUNT; d++) {
  1052. list_defines_add(&defines, d);
  1053. }
  1054. }
  1055. for (size_t i = 0; i < defines.define_count; i++) {
  1056. printf("%s=", defines.defines[i].name);
  1057. for (size_t j = 0; j < defines.defines[i].value_count; j++) {
  1058. printf("%jd", defines.defines[i].values[j]);
  1059. if (j != defines.defines[i].value_count-1) {
  1060. printf(",");
  1061. }
  1062. }
  1063. printf("\n");
  1064. }
  1065. for (size_t i = 0; i < defines.define_count; i++) {
  1066. free(defines.defines[i].values);
  1067. }
  1068. free(defines.defines);
  1069. }
  1070. // geometries to test
  1071. const test_geometry_t builtin_geometries[] = {
  1072. {"default", {{NULL}, TEST_CONST(16), TEST_CONST(512), {NULL}}},
  1073. {"eeprom", {{NULL}, TEST_CONST(1), TEST_CONST(512), {NULL}}},
  1074. {"emmc", {{NULL}, {NULL}, TEST_CONST(512), {NULL}}},
  1075. {"nor", {{NULL}, TEST_CONST(1), TEST_CONST(4096), {NULL}}},
  1076. {"nand", {{NULL}, TEST_CONST(4096), TEST_CONST(32768), {NULL}}},
  1077. {NULL, {{NULL}, {NULL}, {NULL}, {NULL}}},
  1078. };
  1079. const test_geometry_t *test_geometries = builtin_geometries;
  1080. size_t test_geometry_count = 5;
  1081. static void list_geometries(void) {
  1082. // at least size so that names fit
  1083. unsigned name_width = 23;
  1084. for (size_t g = 0; builtin_geometries[g].name; g++) {
  1085. size_t len = strlen(builtin_geometries[g].name);
  1086. if (len > name_width) {
  1087. name_width = len;
  1088. }
  1089. }
  1090. name_width = 4*((name_width+1+4-1)/4)-1;
  1091. // yes we do need to define a suite, this does a bit of bookeeping
  1092. // such as setting up the define cache
  1093. test_define_suite(&(const struct test_suite){0});
  1094. printf("%-*s %7s %7s %7s %7s %11s\n",
  1095. name_width, "geometry", "read", "prog", "erase", "count", "size");
  1096. for (size_t g = 0; builtin_geometries[g].name; g++) {
  1097. test_define_geometry(&builtin_geometries[g]);
  1098. test_define_flush();
  1099. printf("%-*s %7ju %7ju %7ju %7ju %11ju\n",
  1100. name_width,
  1101. builtin_geometries[g].name,
  1102. READ_SIZE,
  1103. PROG_SIZE,
  1104. BLOCK_SIZE,
  1105. BLOCK_COUNT,
  1106. BLOCK_SIZE*BLOCK_COUNT);
  1107. }
  1108. }
  1109. // scenarios to run tests under power-loss
  1110. static void run_powerloss_none(
  1111. const lfs_emubd_powercycles_t *cycles,
  1112. size_t cycle_count,
  1113. const struct test_suite *suite,
  1114. const struct test_case *case_) {
  1115. (void)cycles;
  1116. (void)cycle_count;
  1117. (void)suite;
  1118. // create block device and configuration
  1119. lfs_emubd_t bd;
  1120. struct lfs_config cfg = {
  1121. .context = &bd,
  1122. .read = lfs_emubd_read,
  1123. .prog = lfs_emubd_prog,
  1124. .erase = lfs_emubd_erase,
  1125. .sync = lfs_emubd_sync,
  1126. .read_size = READ_SIZE,
  1127. .prog_size = PROG_SIZE,
  1128. .block_size = BLOCK_SIZE,
  1129. .block_count = BLOCK_COUNT,
  1130. .block_cycles = BLOCK_CYCLES,
  1131. .cache_size = CACHE_SIZE,
  1132. .lookahead_size = LOOKAHEAD_SIZE,
  1133. };
  1134. struct lfs_emubd_config bdcfg = {
  1135. .erase_value = ERASE_VALUE,
  1136. .erase_cycles = ERASE_CYCLES,
  1137. .badblock_behavior = BADBLOCK_BEHAVIOR,
  1138. .disk_path = test_disk_path,
  1139. .read_sleep = test_read_sleep,
  1140. .prog_sleep = test_prog_sleep,
  1141. .erase_sleep = test_erase_sleep,
  1142. };
  1143. int err = lfs_emubd_createcfg(&cfg, test_disk_path, &bdcfg);
  1144. if (err) {
  1145. fprintf(stderr, "error: could not create block device: %d\n", err);
  1146. exit(-1);
  1147. }
  1148. // run the test
  1149. printf("running ");
  1150. perm_printid(suite, case_, NULL, 0);
  1151. printf("\n");
  1152. case_->run(&cfg);
  1153. printf("finished ");
  1154. perm_printid(suite, case_, NULL, 0);
  1155. printf("\n");
  1156. // cleanup
  1157. err = lfs_emubd_destroy(&cfg);
  1158. if (err) {
  1159. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  1160. exit(-1);
  1161. }
  1162. }
  1163. static void powerloss_longjmp(void *c) {
  1164. jmp_buf *powerloss_jmp = c;
  1165. longjmp(*powerloss_jmp, 1);
  1166. }
  1167. static void run_powerloss_linear(
  1168. const lfs_emubd_powercycles_t *cycles,
  1169. size_t cycle_count,
  1170. const struct test_suite *suite,
  1171. const struct test_case *case_) {
  1172. (void)cycles;
  1173. (void)cycle_count;
  1174. (void)suite;
  1175. // create block device and configuration
  1176. lfs_emubd_t bd;
  1177. jmp_buf powerloss_jmp;
  1178. volatile lfs_emubd_powercycles_t i = 1;
  1179. struct lfs_config cfg = {
  1180. .context = &bd,
  1181. .read = lfs_emubd_read,
  1182. .prog = lfs_emubd_prog,
  1183. .erase = lfs_emubd_erase,
  1184. .sync = lfs_emubd_sync,
  1185. .read_size = READ_SIZE,
  1186. .prog_size = PROG_SIZE,
  1187. .block_size = BLOCK_SIZE,
  1188. .block_count = BLOCK_COUNT,
  1189. .block_cycles = BLOCK_CYCLES,
  1190. .cache_size = CACHE_SIZE,
  1191. .lookahead_size = LOOKAHEAD_SIZE,
  1192. };
  1193. struct lfs_emubd_config bdcfg = {
  1194. .erase_value = ERASE_VALUE,
  1195. .erase_cycles = ERASE_CYCLES,
  1196. .badblock_behavior = BADBLOCK_BEHAVIOR,
  1197. .disk_path = test_disk_path,
  1198. .read_sleep = test_read_sleep,
  1199. .prog_sleep = test_prog_sleep,
  1200. .erase_sleep = test_erase_sleep,
  1201. .power_cycles = i,
  1202. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  1203. .powerloss_cb = powerloss_longjmp,
  1204. .powerloss_data = &powerloss_jmp,
  1205. };
  1206. int err = lfs_emubd_createcfg(&cfg, test_disk_path, &bdcfg);
  1207. if (err) {
  1208. fprintf(stderr, "error: could not create block device: %d\n", err);
  1209. exit(-1);
  1210. }
  1211. // run the test, increasing power-cycles as power-loss events occur
  1212. printf("running ");
  1213. perm_printid(suite, case_, NULL, 0);
  1214. printf("\n");
  1215. while (true) {
  1216. if (!setjmp(powerloss_jmp)) {
  1217. // run the test
  1218. case_->run(&cfg);
  1219. break;
  1220. }
  1221. // power-loss!
  1222. printf("powerloss ");
  1223. perm_printid(suite, case_, NULL, 0);
  1224. printf(":");
  1225. for (lfs_emubd_powercycles_t j = 1; j <= i; j++) {
  1226. leb16_print(j);
  1227. }
  1228. printf("\n");
  1229. i += 1;
  1230. lfs_emubd_setpowercycles(&cfg, i);
  1231. }
  1232. printf("finished ");
  1233. perm_printid(suite, case_, NULL, 0);
  1234. printf("\n");
  1235. // cleanup
  1236. err = lfs_emubd_destroy(&cfg);
  1237. if (err) {
  1238. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  1239. exit(-1);
  1240. }
  1241. }
  1242. static void run_powerloss_log(
  1243. const lfs_emubd_powercycles_t *cycles,
  1244. size_t cycle_count,
  1245. const struct test_suite *suite,
  1246. const struct test_case *case_) {
  1247. (void)cycles;
  1248. (void)cycle_count;
  1249. (void)suite;
  1250. // create block device and configuration
  1251. lfs_emubd_t bd;
  1252. jmp_buf powerloss_jmp;
  1253. volatile lfs_emubd_powercycles_t i = 1;
  1254. struct lfs_config cfg = {
  1255. .context = &bd,
  1256. .read = lfs_emubd_read,
  1257. .prog = lfs_emubd_prog,
  1258. .erase = lfs_emubd_erase,
  1259. .sync = lfs_emubd_sync,
  1260. .read_size = READ_SIZE,
  1261. .prog_size = PROG_SIZE,
  1262. .block_size = BLOCK_SIZE,
  1263. .block_count = BLOCK_COUNT,
  1264. .block_cycles = BLOCK_CYCLES,
  1265. .cache_size = CACHE_SIZE,
  1266. .lookahead_size = LOOKAHEAD_SIZE,
  1267. };
  1268. struct lfs_emubd_config bdcfg = {
  1269. .erase_value = ERASE_VALUE,
  1270. .erase_cycles = ERASE_CYCLES,
  1271. .badblock_behavior = BADBLOCK_BEHAVIOR,
  1272. .disk_path = test_disk_path,
  1273. .read_sleep = test_read_sleep,
  1274. .prog_sleep = test_prog_sleep,
  1275. .erase_sleep = test_erase_sleep,
  1276. .power_cycles = i,
  1277. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  1278. .powerloss_cb = powerloss_longjmp,
  1279. .powerloss_data = &powerloss_jmp,
  1280. };
  1281. int err = lfs_emubd_createcfg(&cfg, test_disk_path, &bdcfg);
  1282. if (err) {
  1283. fprintf(stderr, "error: could not create block device: %d\n", err);
  1284. exit(-1);
  1285. }
  1286. // run the test, increasing power-cycles as power-loss events occur
  1287. printf("running ");
  1288. perm_printid(suite, case_, NULL, 0);
  1289. printf("\n");
  1290. while (true) {
  1291. if (!setjmp(powerloss_jmp)) {
  1292. // run the test
  1293. case_->run(&cfg);
  1294. break;
  1295. }
  1296. // power-loss!
  1297. printf("powerloss ");
  1298. perm_printid(suite, case_, NULL, 0);
  1299. printf(":");
  1300. for (lfs_emubd_powercycles_t j = 1; j <= i; j *= 2) {
  1301. leb16_print(j);
  1302. }
  1303. printf("\n");
  1304. i *= 2;
  1305. lfs_emubd_setpowercycles(&cfg, i);
  1306. }
  1307. printf("finished ");
  1308. perm_printid(suite, case_, NULL, 0);
  1309. printf("\n");
  1310. // cleanup
  1311. err = lfs_emubd_destroy(&cfg);
  1312. if (err) {
  1313. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  1314. exit(-1);
  1315. }
  1316. }
  1317. static void run_powerloss_cycles(
  1318. const lfs_emubd_powercycles_t *cycles,
  1319. size_t cycle_count,
  1320. const struct test_suite *suite,
  1321. const struct test_case *case_) {
  1322. (void)suite;
  1323. // create block device and configuration
  1324. lfs_emubd_t bd;
  1325. jmp_buf powerloss_jmp;
  1326. volatile size_t i = 0;
  1327. struct lfs_config cfg = {
  1328. .context = &bd,
  1329. .read = lfs_emubd_read,
  1330. .prog = lfs_emubd_prog,
  1331. .erase = lfs_emubd_erase,
  1332. .sync = lfs_emubd_sync,
  1333. .read_size = READ_SIZE,
  1334. .prog_size = PROG_SIZE,
  1335. .block_size = BLOCK_SIZE,
  1336. .block_count = BLOCK_COUNT,
  1337. .block_cycles = BLOCK_CYCLES,
  1338. .cache_size = CACHE_SIZE,
  1339. .lookahead_size = LOOKAHEAD_SIZE,
  1340. };
  1341. struct lfs_emubd_config bdcfg = {
  1342. .erase_value = ERASE_VALUE,
  1343. .erase_cycles = ERASE_CYCLES,
  1344. .badblock_behavior = BADBLOCK_BEHAVIOR,
  1345. .disk_path = test_disk_path,
  1346. .read_sleep = test_read_sleep,
  1347. .prog_sleep = test_prog_sleep,
  1348. .erase_sleep = test_erase_sleep,
  1349. .power_cycles = (i < cycle_count) ? cycles[i] : 0,
  1350. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  1351. .powerloss_cb = powerloss_longjmp,
  1352. .powerloss_data = &powerloss_jmp,
  1353. };
  1354. int err = lfs_emubd_createcfg(&cfg, test_disk_path, &bdcfg);
  1355. if (err) {
  1356. fprintf(stderr, "error: could not create block device: %d\n", err);
  1357. exit(-1);
  1358. }
  1359. // run the test, increasing power-cycles as power-loss events occur
  1360. printf("running ");
  1361. perm_printid(suite, case_, NULL, 0);
  1362. printf("\n");
  1363. while (true) {
  1364. if (!setjmp(powerloss_jmp)) {
  1365. // run the test
  1366. case_->run(&cfg);
  1367. break;
  1368. }
  1369. // power-loss!
  1370. assert(i <= cycle_count);
  1371. printf("powerloss ");
  1372. perm_printid(suite, case_, cycles, i+1);
  1373. printf("\n");
  1374. i += 1;
  1375. lfs_emubd_setpowercycles(&cfg,
  1376. (i < cycle_count) ? cycles[i] : 0);
  1377. }
  1378. printf("finished ");
  1379. perm_printid(suite, case_, NULL, 0);
  1380. printf("\n");
  1381. // cleanup
  1382. err = lfs_emubd_destroy(&cfg);
  1383. if (err) {
  1384. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  1385. exit(-1);
  1386. }
  1387. }
  1388. struct powerloss_exhaustive_state {
  1389. struct lfs_config *cfg;
  1390. lfs_emubd_t *branches;
  1391. size_t branch_count;
  1392. size_t branch_capacity;
  1393. };
  1394. struct powerloss_exhaustive_cycles {
  1395. lfs_emubd_powercycles_t *cycles;
  1396. size_t cycle_count;
  1397. size_t cycle_capacity;
  1398. };
  1399. static void powerloss_exhaustive_branch(void *c) {
  1400. struct powerloss_exhaustive_state *state = c;
  1401. // append to branches
  1402. lfs_emubd_t *branch = mappend(
  1403. (void**)&state->branches,
  1404. sizeof(lfs_emubd_t),
  1405. &state->branch_count,
  1406. &state->branch_capacity);
  1407. if (!branch) {
  1408. fprintf(stderr, "error: exhaustive: out of memory\n");
  1409. exit(-1);
  1410. }
  1411. // create copy-on-write copy
  1412. int err = lfs_emubd_copy(state->cfg, branch);
  1413. if (err) {
  1414. fprintf(stderr, "error: exhaustive: could not create bd copy\n");
  1415. exit(-1);
  1416. }
  1417. // also trigger on next power cycle
  1418. lfs_emubd_setpowercycles(state->cfg, 1);
  1419. }
  1420. static void run_powerloss_exhaustive_layer(
  1421. struct powerloss_exhaustive_cycles *cycles,
  1422. const struct test_suite *suite,
  1423. const struct test_case *case_,
  1424. struct lfs_config *cfg,
  1425. struct lfs_emubd_config *bdcfg,
  1426. size_t depth) {
  1427. (void)suite;
  1428. struct powerloss_exhaustive_state state = {
  1429. .cfg = cfg,
  1430. .branches = NULL,
  1431. .branch_count = 0,
  1432. .branch_capacity = 0,
  1433. };
  1434. // run through the test without additional powerlosses, collecting possible
  1435. // branches as we do so
  1436. lfs_emubd_setpowercycles(state.cfg, depth > 0 ? 1 : 0);
  1437. bdcfg->powerloss_data = &state;
  1438. // run the tests
  1439. case_->run(cfg);
  1440. // aggressively clean up memory here to try to keep our memory usage low
  1441. int err = lfs_emubd_destroy(cfg);
  1442. if (err) {
  1443. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  1444. exit(-1);
  1445. }
  1446. // recurse into each branch
  1447. for (size_t i = 0; i < state.branch_count; i++) {
  1448. // first push and print the branch
  1449. lfs_emubd_powercycles_t *cycle = mappend(
  1450. (void**)&cycles->cycles,
  1451. sizeof(lfs_emubd_powercycles_t),
  1452. &cycles->cycle_count,
  1453. &cycles->cycle_capacity);
  1454. if (!cycle) {
  1455. fprintf(stderr, "error: exhaustive: out of memory\n");
  1456. exit(-1);
  1457. }
  1458. *cycle = i+1;
  1459. printf("powerloss ");
  1460. perm_printid(suite, case_, cycles->cycles, cycles->cycle_count);
  1461. printf("\n");
  1462. // now recurse
  1463. cfg->context = &state.branches[i];
  1464. run_powerloss_exhaustive_layer(cycles,
  1465. suite, case_,
  1466. cfg, bdcfg, depth-1);
  1467. // pop the cycle
  1468. cycles->cycle_count -= 1;
  1469. }
  1470. // clean up memory
  1471. free(state.branches);
  1472. }
  1473. static void run_powerloss_exhaustive(
  1474. const lfs_emubd_powercycles_t *cycles,
  1475. size_t cycle_count,
  1476. const struct test_suite *suite,
  1477. const struct test_case *case_) {
  1478. (void)cycles;
  1479. (void)suite;
  1480. // create block device and configuration
  1481. lfs_emubd_t bd;
  1482. struct lfs_config cfg = {
  1483. .context = &bd,
  1484. .read = lfs_emubd_read,
  1485. .prog = lfs_emubd_prog,
  1486. .erase = lfs_emubd_erase,
  1487. .sync = lfs_emubd_sync,
  1488. .read_size = READ_SIZE,
  1489. .prog_size = PROG_SIZE,
  1490. .block_size = BLOCK_SIZE,
  1491. .block_count = BLOCK_COUNT,
  1492. .block_cycles = BLOCK_CYCLES,
  1493. .cache_size = CACHE_SIZE,
  1494. .lookahead_size = LOOKAHEAD_SIZE,
  1495. };
  1496. struct lfs_emubd_config bdcfg = {
  1497. .erase_value = ERASE_VALUE,
  1498. .erase_cycles = ERASE_CYCLES,
  1499. .badblock_behavior = BADBLOCK_BEHAVIOR,
  1500. .disk_path = test_disk_path,
  1501. .read_sleep = test_read_sleep,
  1502. .prog_sleep = test_prog_sleep,
  1503. .erase_sleep = test_erase_sleep,
  1504. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  1505. .powerloss_cb = powerloss_exhaustive_branch,
  1506. .powerloss_data = NULL,
  1507. };
  1508. int err = lfs_emubd_createcfg(&cfg, test_disk_path, &bdcfg);
  1509. if (err) {
  1510. fprintf(stderr, "error: could not create block device: %d\n", err);
  1511. exit(-1);
  1512. }
  1513. // run the test, increasing power-cycles as power-loss events occur
  1514. printf("running ");
  1515. perm_printid(suite, case_, NULL, 0);
  1516. printf("\n");
  1517. // recursively exhaust each layer of powerlosses
  1518. run_powerloss_exhaustive_layer(
  1519. &(struct powerloss_exhaustive_cycles){NULL, 0, 0},
  1520. suite, case_,
  1521. &cfg, &bdcfg, cycle_count);
  1522. printf("finished ");
  1523. perm_printid(suite, case_, NULL, 0);
  1524. printf("\n");
  1525. }
  1526. const test_powerloss_t builtin_powerlosses[] = {
  1527. {"none", run_powerloss_none, NULL, 0},
  1528. {"log", run_powerloss_log, NULL, 0},
  1529. {"linear", run_powerloss_linear, NULL, 0},
  1530. {"exhaustive", run_powerloss_exhaustive, NULL, SIZE_MAX},
  1531. {NULL, NULL, NULL, 0},
  1532. };
  1533. const char *const builtin_powerlosses_help[] = {
  1534. "Run with no power-losses.",
  1535. "Run with exponentially-decreasing power-losses.",
  1536. "Run with linearly-decreasing power-losses.",
  1537. "Run a all permutations of power-losses, this may take a while.",
  1538. "Run a all permutations of n power-losses.",
  1539. "Run a custom comma-separated set of power-losses.",
  1540. "Run a custom leb16-encoded set of power-losses.",
  1541. };
  1542. const test_powerloss_t *test_powerlosses = (const test_powerloss_t[]){
  1543. {"none", run_powerloss_none, NULL, 0},
  1544. };
  1545. size_t test_powerloss_count = 1;
  1546. static void list_powerlosses(void) {
  1547. // at least size so that names fit
  1548. unsigned name_width = 23;
  1549. for (size_t i = 0; builtin_powerlosses[i].name; i++) {
  1550. size_t len = strlen(builtin_powerlosses[i].name);
  1551. if (len > name_width) {
  1552. name_width = len;
  1553. }
  1554. }
  1555. name_width = 4*((name_width+1+4-1)/4)-1;
  1556. printf("%-*s %s\n", name_width, "scenario", "description");
  1557. size_t i = 0;
  1558. for (; builtin_powerlosses[i].name; i++) {
  1559. printf("%-*s %s\n",
  1560. name_width,
  1561. builtin_powerlosses[i].name,
  1562. builtin_powerlosses_help[i]);
  1563. }
  1564. // a couple more options with special parsing
  1565. printf("%-*s %s\n", name_width, "1,2,3", builtin_powerlosses_help[i+0]);
  1566. printf("%-*s %s\n", name_width, "{1,2,3}", builtin_powerlosses_help[i+1]);
  1567. printf("%-*s %s\n", name_width, ":1248g1", builtin_powerlosses_help[i+2]);
  1568. }
  1569. // global test step count
  1570. size_t test_step = 0;
  1571. void perm_run(
  1572. void *data,
  1573. const struct test_suite *suite,
  1574. const struct test_case *case_,
  1575. const test_powerloss_t *powerloss) {
  1576. (void)data;
  1577. // skip this step?
  1578. if (!(test_step >= test_step_start
  1579. && test_step < test_step_stop
  1580. && (test_step-test_step_start) % test_step_step == 0)) {
  1581. test_step += 1;
  1582. return;
  1583. }
  1584. test_step += 1;
  1585. // filter?
  1586. if (case_->filter && !case_->filter()) {
  1587. printf("skipped ");
  1588. perm_printid(suite, case_, NULL, 0);
  1589. printf("\n");
  1590. return;
  1591. }
  1592. powerloss->run(
  1593. powerloss->cycles, powerloss->cycle_count,
  1594. suite, case_);
  1595. }
  1596. static void run(void) {
  1597. // ignore disconnected pipes
  1598. signal(SIGPIPE, SIG_IGN);
  1599. for (size_t t = 0; t < test_id_count; t++) {
  1600. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  1601. test_define_suite(&test_suites[i]);
  1602. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  1603. // does neither suite nor case name match?
  1604. if (test_ids[t].name && !(
  1605. strcmp(test_ids[t].name,
  1606. test_suites[i].name) == 0
  1607. || strcmp(test_ids[t].name,
  1608. test_suites[i].cases[j].name) == 0)) {
  1609. continue;
  1610. }
  1611. case_forperm(
  1612. &test_suites[i],
  1613. &test_suites[i].cases[j],
  1614. test_ids[t].defines,
  1615. test_ids[t].define_count,
  1616. test_ids[t].cycles,
  1617. test_ids[t].cycle_count,
  1618. perm_run,
  1619. NULL);
  1620. }
  1621. }
  1622. }
  1623. }
  1624. // option handling
  1625. enum opt_flags {
  1626. OPT_HELP = 'h',
  1627. OPT_SUMMARY = 'Y',
  1628. OPT_LIST_SUITES = 'l',
  1629. OPT_LIST_CASES = 'L',
  1630. OPT_LIST_SUITE_PATHS = 1,
  1631. OPT_LIST_CASE_PATHS = 2,
  1632. OPT_LIST_DEFINES = 3,
  1633. OPT_LIST_PERMUTATION_DEFINES = 4,
  1634. OPT_LIST_IMPLICIT_DEFINES = 5,
  1635. OPT_LIST_GEOMETRIES = 6,
  1636. OPT_LIST_POWERLOSSES = 7,
  1637. OPT_DEFINE = 'D',
  1638. OPT_GEOMETRY = 'G',
  1639. OPT_POWERLOSS = 'P',
  1640. OPT_STEP = 's',
  1641. OPT_DISK = 'd',
  1642. OPT_TRACE = 't',
  1643. OPT_TRACE_BACKTRACE = 8,
  1644. OPT_TRACE_PERIOD = 9,
  1645. OPT_TRACE_FREQ = 10,
  1646. OPT_READ_SLEEP = 11,
  1647. OPT_PROG_SLEEP = 12,
  1648. OPT_ERASE_SLEEP = 13,
  1649. };
  1650. const char *short_opts = "hYlLD:G:P:s:d:t:";
  1651. const struct option long_opts[] = {
  1652. {"help", no_argument, NULL, OPT_HELP},
  1653. {"summary", no_argument, NULL, OPT_SUMMARY},
  1654. {"list-suites", no_argument, NULL, OPT_LIST_SUITES},
  1655. {"list-cases", no_argument, NULL, OPT_LIST_CASES},
  1656. {"list-suite-paths", no_argument, NULL, OPT_LIST_SUITE_PATHS},
  1657. {"list-case-paths", no_argument, NULL, OPT_LIST_CASE_PATHS},
  1658. {"list-defines", no_argument, NULL, OPT_LIST_DEFINES},
  1659. {"list-permutation-defines",
  1660. no_argument, NULL, OPT_LIST_PERMUTATION_DEFINES},
  1661. {"list-implicit-defines",
  1662. no_argument, NULL, OPT_LIST_IMPLICIT_DEFINES},
  1663. {"list-geometries", no_argument, NULL, OPT_LIST_GEOMETRIES},
  1664. {"list-powerlosses", no_argument, NULL, OPT_LIST_POWERLOSSES},
  1665. {"define", required_argument, NULL, OPT_DEFINE},
  1666. {"geometry", required_argument, NULL, OPT_GEOMETRY},
  1667. {"powerloss", required_argument, NULL, OPT_POWERLOSS},
  1668. {"step", required_argument, NULL, OPT_STEP},
  1669. {"disk", required_argument, NULL, OPT_DISK},
  1670. {"trace", required_argument, NULL, OPT_TRACE},
  1671. {"trace-backtrace", no_argument, NULL, OPT_TRACE_BACKTRACE},
  1672. {"trace-period", required_argument, NULL, OPT_TRACE_PERIOD},
  1673. {"trace-freq", required_argument, NULL, OPT_TRACE_FREQ},
  1674. {"read-sleep", required_argument, NULL, OPT_READ_SLEEP},
  1675. {"prog-sleep", required_argument, NULL, OPT_PROG_SLEEP},
  1676. {"erase-sleep", required_argument, NULL, OPT_ERASE_SLEEP},
  1677. {NULL, 0, NULL, 0},
  1678. };
  1679. const char *const help_text[] = {
  1680. "Show this help message.",
  1681. "Show quick summary.",
  1682. "List test suites.",
  1683. "List test cases.",
  1684. "List the path for each test suite.",
  1685. "List the path and line number for each test case.",
  1686. "List all defines in this test-runner.",
  1687. "List explicit defines in this test-runner.",
  1688. "List implicit defines in this test-runner.",
  1689. "List the available disk geometries.",
  1690. "List the available power-loss scenarios.",
  1691. "Override a test define.",
  1692. "Comma-separated list of disk geometries to test.",
  1693. "Comma-separated list of power-loss scenarios to test.",
  1694. "Comma-separated range of test permutations to run (start,stop,step).",
  1695. "Direct block device operations to this file.",
  1696. "Direct trace output to this file.",
  1697. "Include a backtrace with every trace statement.",
  1698. "Sample trace output at this period in cycles.",
  1699. "Sample trace output at this frequency in hz.",
  1700. "Artificial read delay in seconds.",
  1701. "Artificial prog delay in seconds.",
  1702. "Artificial erase delay in seconds.",
  1703. };
  1704. int main(int argc, char **argv) {
  1705. void (*op)(void) = run;
  1706. size_t test_override_capacity = 0;
  1707. size_t test_geometry_capacity = 0;
  1708. size_t test_powerloss_capacity = 0;
  1709. size_t test_id_capacity = 0;
  1710. // parse options
  1711. while (true) {
  1712. int c = getopt_long(argc, argv, short_opts, long_opts, NULL);
  1713. switch (c) {
  1714. // generate help message
  1715. case OPT_HELP: {
  1716. printf("usage: %s [options] [test_id]\n", argv[0]);
  1717. printf("\n");
  1718. printf("options:\n");
  1719. size_t i = 0;
  1720. while (long_opts[i].name) {
  1721. size_t indent;
  1722. if (long_opts[i].has_arg == no_argument) {
  1723. if (long_opts[i].val >= '0' && long_opts[i].val < 'z') {
  1724. indent = printf(" -%c, --%s ",
  1725. long_opts[i].val,
  1726. long_opts[i].name);
  1727. } else {
  1728. indent = printf(" --%s ",
  1729. long_opts[i].name);
  1730. }
  1731. } else {
  1732. if (long_opts[i].val >= '0' && long_opts[i].val < 'z') {
  1733. indent = printf(" -%c %s, --%s %s ",
  1734. long_opts[i].val,
  1735. long_opts[i].name,
  1736. long_opts[i].name,
  1737. long_opts[i].name);
  1738. } else {
  1739. indent = printf(" --%s %s ",
  1740. long_opts[i].name,
  1741. long_opts[i].name);
  1742. }
  1743. }
  1744. // a quick, hacky, byte-level method for text wrapping
  1745. size_t len = strlen(help_text[i]);
  1746. size_t j = 0;
  1747. if (indent < 24) {
  1748. printf("%*s %.80s\n",
  1749. (int)(24-1-indent),
  1750. "",
  1751. &help_text[i][j]);
  1752. j += 80;
  1753. } else {
  1754. printf("\n");
  1755. }
  1756. while (j < len) {
  1757. printf("%24s%.80s\n", "", &help_text[i][j]);
  1758. j += 80;
  1759. }
  1760. i += 1;
  1761. }
  1762. printf("\n");
  1763. exit(0);
  1764. }
  1765. // summary/list flags
  1766. case OPT_SUMMARY:
  1767. op = summary;
  1768. break;
  1769. case OPT_LIST_SUITES:
  1770. op = list_suites;
  1771. break;
  1772. case OPT_LIST_CASES:
  1773. op = list_cases;
  1774. break;
  1775. case OPT_LIST_SUITE_PATHS:
  1776. op = list_suite_paths;
  1777. break;
  1778. case OPT_LIST_CASE_PATHS:
  1779. op = list_case_paths;
  1780. break;
  1781. case OPT_LIST_DEFINES:
  1782. op = list_defines;
  1783. break;
  1784. case OPT_LIST_PERMUTATION_DEFINES:
  1785. op = list_permutation_defines;
  1786. break;
  1787. case OPT_LIST_IMPLICIT_DEFINES:
  1788. op = list_implicit_defines;
  1789. break;
  1790. case OPT_LIST_GEOMETRIES:
  1791. op = list_geometries;
  1792. break;
  1793. case OPT_LIST_POWERLOSSES:
  1794. op = list_powerlosses;
  1795. break;
  1796. // configuration
  1797. case OPT_DEFINE: {
  1798. // allocate space
  1799. test_override_t *override = mappend(
  1800. (void**)&test_overrides,
  1801. sizeof(test_override_t),
  1802. &test_override_count,
  1803. &test_override_capacity);
  1804. // parse into string key/intmax_t value, cannibalizing the
  1805. // arg in the process
  1806. char *sep = strchr(optarg, '=');
  1807. char *parsed = NULL;
  1808. if (!sep) {
  1809. goto invalid_define;
  1810. }
  1811. *sep = '\0';
  1812. override->name = optarg;
  1813. optarg = sep+1;
  1814. // parse comma-separated permutations
  1815. {
  1816. override->defines = NULL;
  1817. override->permutations = 0;
  1818. size_t override_capacity = 0;
  1819. while (true) {
  1820. optarg += strspn(optarg, " ");
  1821. if (strncmp(optarg, "range", strlen("range")) == 0) {
  1822. // range of values
  1823. optarg += strlen("range");
  1824. optarg += strspn(optarg, " ");
  1825. if (*optarg != '(') {
  1826. goto invalid_define;
  1827. }
  1828. optarg += 1;
  1829. intmax_t start = strtoumax(optarg, &parsed, 0);
  1830. intmax_t stop = -1;
  1831. intmax_t step = 1;
  1832. // allow empty string for start=0
  1833. if (parsed == optarg) {
  1834. start = 0;
  1835. }
  1836. optarg = parsed + strspn(parsed, " ");
  1837. if (*optarg != ',' && *optarg != ')') {
  1838. goto invalid_define;
  1839. }
  1840. if (*optarg == ',') {
  1841. optarg += 1;
  1842. stop = strtoumax(optarg, &parsed, 0);
  1843. // allow empty string for stop=end
  1844. if (parsed == optarg) {
  1845. stop = -1;
  1846. }
  1847. optarg = parsed + strspn(parsed, " ");
  1848. if (*optarg != ',' && *optarg != ')') {
  1849. goto invalid_define;
  1850. }
  1851. if (*optarg == ',') {
  1852. optarg += 1;
  1853. step = strtoumax(optarg, &parsed, 0);
  1854. // allow empty string for stop=1
  1855. if (parsed == optarg) {
  1856. step = 1;
  1857. }
  1858. optarg = parsed + strspn(parsed, " ");
  1859. if (*optarg != ')') {
  1860. goto invalid_define;
  1861. }
  1862. }
  1863. } else {
  1864. // single value = stop only
  1865. stop = start;
  1866. start = 0;
  1867. }
  1868. if (*optarg != ')') {
  1869. goto invalid_define;
  1870. }
  1871. optarg += 1;
  1872. // calculate the range of values
  1873. assert(step != 0);
  1874. for (intmax_t i = start;
  1875. (step < 0)
  1876. ? i > stop
  1877. : (uintmax_t)i < (uintmax_t)stop;
  1878. i += step) {
  1879. *(intmax_t*)mappend(
  1880. (void**)&override->defines,
  1881. sizeof(intmax_t),
  1882. &override->permutations,
  1883. &override_capacity) = i;
  1884. }
  1885. } else if (*optarg != '\0') {
  1886. // single value
  1887. intmax_t define = strtoimax(optarg, &parsed, 0);
  1888. if (parsed == optarg) {
  1889. goto invalid_define;
  1890. }
  1891. optarg = parsed + strspn(parsed, " ");
  1892. *(intmax_t*)mappend(
  1893. (void**)&override->defines,
  1894. sizeof(intmax_t),
  1895. &override->permutations,
  1896. &override_capacity) = define;
  1897. } else {
  1898. break;
  1899. }
  1900. if (*optarg == ',') {
  1901. optarg += 1;
  1902. }
  1903. }
  1904. }
  1905. assert(override->permutations > 0);
  1906. break;
  1907. invalid_define:
  1908. fprintf(stderr, "error: invalid define: %s\n", optarg);
  1909. exit(-1);
  1910. }
  1911. case OPT_GEOMETRY: {
  1912. // reset our geometry scenarios
  1913. if (test_geometry_capacity > 0) {
  1914. free((test_geometry_t*)test_geometries);
  1915. }
  1916. test_geometries = NULL;
  1917. test_geometry_count = 0;
  1918. test_geometry_capacity = 0;
  1919. // parse the comma separated list of disk geometries
  1920. while (*optarg) {
  1921. // allocate space
  1922. test_geometry_t *geometry = mappend(
  1923. (void**)&test_geometries,
  1924. sizeof(test_geometry_t),
  1925. &test_geometry_count,
  1926. &test_geometry_capacity);
  1927. // parse the disk geometry
  1928. optarg += strspn(optarg, " ");
  1929. // named disk geometry
  1930. size_t len = strcspn(optarg, " ,");
  1931. for (size_t i = 0; builtin_geometries[i].name; i++) {
  1932. if (len == strlen(builtin_geometries[i].name)
  1933. && memcmp(optarg,
  1934. builtin_geometries[i].name,
  1935. len) == 0) {
  1936. *geometry = builtin_geometries[i];
  1937. optarg += len;
  1938. goto geometry_next;
  1939. }
  1940. }
  1941. // comma-separated read/prog/erase/count
  1942. if (*optarg == '{') {
  1943. lfs_size_t sizes[4];
  1944. size_t count = 0;
  1945. char *s = optarg + 1;
  1946. while (count < 4) {
  1947. char *parsed = NULL;
  1948. sizes[count] = strtoumax(s, &parsed, 0);
  1949. count += 1;
  1950. s = parsed + strspn(parsed, " ");
  1951. if (*s == ',') {
  1952. s += 1;
  1953. continue;
  1954. } else if (*s == '}') {
  1955. s += 1;
  1956. break;
  1957. } else {
  1958. goto geometry_unknown;
  1959. }
  1960. }
  1961. // allow implicit r=p and p=e for common geometries
  1962. memset(geometry, 0, sizeof(test_geometry_t));
  1963. if (count >= 3) {
  1964. geometry->defines[READ_SIZE_i]
  1965. = TEST_LIT(sizes[0]);
  1966. geometry->defines[PROG_SIZE_i]
  1967. = TEST_LIT(sizes[1]);
  1968. geometry->defines[BLOCK_SIZE_i]
  1969. = TEST_LIT(sizes[2]);
  1970. } else if (count >= 2) {
  1971. geometry->defines[PROG_SIZE_i]
  1972. = TEST_LIT(sizes[0]);
  1973. geometry->defines[BLOCK_SIZE_i]
  1974. = TEST_LIT(sizes[1]);
  1975. } else {
  1976. geometry->defines[BLOCK_SIZE_i]
  1977. = TEST_LIT(sizes[0]);
  1978. }
  1979. if (count >= 4) {
  1980. geometry->defines[BLOCK_COUNT_i]
  1981. = TEST_LIT(sizes[3]);
  1982. }
  1983. optarg = s;
  1984. goto geometry_next;
  1985. }
  1986. // leb16-encoded read/prog/erase/count
  1987. if (*optarg == ':') {
  1988. lfs_size_t sizes[4];
  1989. size_t count = 0;
  1990. char *s = optarg + 1;
  1991. while (true) {
  1992. char *parsed = NULL;
  1993. uintmax_t x = leb16_parse(s, &parsed);
  1994. if (parsed == s || count >= 4) {
  1995. break;
  1996. }
  1997. sizes[count] = x;
  1998. count += 1;
  1999. s = parsed;
  2000. }
  2001. // allow implicit r=p and p=e for common geometries
  2002. memset(geometry, 0, sizeof(test_geometry_t));
  2003. if (count >= 3) {
  2004. geometry->defines[READ_SIZE_i]
  2005. = TEST_LIT(sizes[0]);
  2006. geometry->defines[PROG_SIZE_i]
  2007. = TEST_LIT(sizes[1]);
  2008. geometry->defines[BLOCK_SIZE_i]
  2009. = TEST_LIT(sizes[2]);
  2010. } else if (count >= 2) {
  2011. geometry->defines[PROG_SIZE_i]
  2012. = TEST_LIT(sizes[0]);
  2013. geometry->defines[BLOCK_SIZE_i]
  2014. = TEST_LIT(sizes[1]);
  2015. } else {
  2016. geometry->defines[BLOCK_SIZE_i]
  2017. = TEST_LIT(sizes[0]);
  2018. }
  2019. if (count >= 4) {
  2020. geometry->defines[BLOCK_COUNT_i]
  2021. = TEST_LIT(sizes[3]);
  2022. }
  2023. optarg = s;
  2024. goto geometry_next;
  2025. }
  2026. geometry_unknown:
  2027. // unknown scenario?
  2028. fprintf(stderr, "error: unknown disk geometry: %s\n",
  2029. optarg);
  2030. exit(-1);
  2031. geometry_next:
  2032. optarg += strspn(optarg, " ");
  2033. if (*optarg == ',') {
  2034. optarg += 1;
  2035. } else if (*optarg == '\0') {
  2036. break;
  2037. } else {
  2038. goto geometry_unknown;
  2039. }
  2040. }
  2041. break;
  2042. }
  2043. case OPT_POWERLOSS: {
  2044. // reset our powerloss scenarios
  2045. if (test_powerloss_capacity > 0) {
  2046. free((test_powerloss_t*)test_powerlosses);
  2047. }
  2048. test_powerlosses = NULL;
  2049. test_powerloss_count = 0;
  2050. test_powerloss_capacity = 0;
  2051. // parse the comma separated list of power-loss scenarios
  2052. while (*optarg) {
  2053. // allocate space
  2054. test_powerloss_t *powerloss = mappend(
  2055. (void**)&test_powerlosses,
  2056. sizeof(test_powerloss_t),
  2057. &test_powerloss_count,
  2058. &test_powerloss_capacity);
  2059. // parse the power-loss scenario
  2060. optarg += strspn(optarg, " ");
  2061. // named power-loss scenario
  2062. size_t len = strcspn(optarg, " ,");
  2063. for (size_t i = 0; builtin_powerlosses[i].name; i++) {
  2064. if (len == strlen(builtin_powerlosses[i].name)
  2065. && memcmp(optarg,
  2066. builtin_powerlosses[i].name,
  2067. len) == 0) {
  2068. *powerloss = builtin_powerlosses[i];
  2069. optarg += len;
  2070. goto powerloss_next;
  2071. }
  2072. }
  2073. // comma-separated permutation
  2074. if (*optarg == '{') {
  2075. lfs_emubd_powercycles_t *cycles = NULL;
  2076. size_t cycle_count = 0;
  2077. size_t cycle_capacity = 0;
  2078. char *s = optarg + 1;
  2079. while (true) {
  2080. char *parsed = NULL;
  2081. *(lfs_emubd_powercycles_t*)mappend(
  2082. (void**)&cycles,
  2083. sizeof(lfs_emubd_powercycles_t),
  2084. &cycle_count,
  2085. &cycle_capacity)
  2086. = strtoumax(s, &parsed, 0);
  2087. s = parsed + strspn(parsed, " ");
  2088. if (*s == ',') {
  2089. s += 1;
  2090. continue;
  2091. } else if (*s == '}') {
  2092. s += 1;
  2093. break;
  2094. } else {
  2095. goto powerloss_unknown;
  2096. }
  2097. }
  2098. *powerloss = (test_powerloss_t){
  2099. .run = run_powerloss_cycles,
  2100. .cycles = cycles,
  2101. .cycle_count = cycle_count,
  2102. };
  2103. optarg = s;
  2104. goto powerloss_next;
  2105. }
  2106. // leb16-encoded permutation
  2107. if (*optarg == ':') {
  2108. lfs_emubd_powercycles_t *cycles = NULL;
  2109. size_t cycle_count = 0;
  2110. size_t cycle_capacity = 0;
  2111. char *s = optarg + 1;
  2112. while (true) {
  2113. char *parsed = NULL;
  2114. uintmax_t x = leb16_parse(s, &parsed);
  2115. if (parsed == s) {
  2116. break;
  2117. }
  2118. *(lfs_emubd_powercycles_t*)mappend(
  2119. (void**)&cycles,
  2120. sizeof(lfs_emubd_powercycles_t),
  2121. &cycle_count,
  2122. &cycle_capacity) = x;
  2123. s = parsed;
  2124. }
  2125. *powerloss = (test_powerloss_t){
  2126. .run = run_powerloss_cycles,
  2127. .cycles = cycles,
  2128. .cycle_count = cycle_count,
  2129. };
  2130. optarg = s;
  2131. goto powerloss_next;
  2132. }
  2133. // exhaustive permutations
  2134. {
  2135. char *parsed = NULL;
  2136. size_t count = strtoumax(optarg, &parsed, 0);
  2137. if (parsed == optarg) {
  2138. goto powerloss_unknown;
  2139. }
  2140. *powerloss = (test_powerloss_t){
  2141. .run = run_powerloss_exhaustive,
  2142. .cycles = NULL,
  2143. .cycle_count = count,
  2144. };
  2145. optarg = (char*)parsed;
  2146. goto powerloss_next;
  2147. }
  2148. powerloss_unknown:
  2149. // unknown scenario?
  2150. fprintf(stderr, "error: unknown power-loss scenario: %s\n",
  2151. optarg);
  2152. exit(-1);
  2153. powerloss_next:
  2154. optarg += strspn(optarg, " ");
  2155. if (*optarg == ',') {
  2156. optarg += 1;
  2157. } else if (*optarg == '\0') {
  2158. break;
  2159. } else {
  2160. goto powerloss_unknown;
  2161. }
  2162. }
  2163. break;
  2164. }
  2165. case OPT_STEP: {
  2166. char *parsed = NULL;
  2167. test_step_start = strtoumax(optarg, &parsed, 0);
  2168. test_step_stop = -1;
  2169. test_step_step = 1;
  2170. // allow empty string for start=0
  2171. if (parsed == optarg) {
  2172. test_step_start = 0;
  2173. }
  2174. optarg = parsed + strspn(parsed, " ");
  2175. if (*optarg != ',' && *optarg != '\0') {
  2176. goto step_unknown;
  2177. }
  2178. if (*optarg == ',') {
  2179. optarg += 1;
  2180. test_step_stop = strtoumax(optarg, &parsed, 0);
  2181. // allow empty string for stop=end
  2182. if (parsed == optarg) {
  2183. test_step_stop = -1;
  2184. }
  2185. optarg = parsed + strspn(parsed, " ");
  2186. if (*optarg != ',' && *optarg != '\0') {
  2187. goto step_unknown;
  2188. }
  2189. if (*optarg == ',') {
  2190. optarg += 1;
  2191. test_step_step = strtoumax(optarg, &parsed, 0);
  2192. // allow empty string for stop=1
  2193. if (parsed == optarg) {
  2194. test_step_step = 1;
  2195. }
  2196. optarg = parsed + strspn(parsed, " ");
  2197. if (*optarg != '\0') {
  2198. goto step_unknown;
  2199. }
  2200. }
  2201. } else {
  2202. // single value = stop only
  2203. test_step_stop = test_step_start;
  2204. test_step_start = 0;
  2205. }
  2206. break;
  2207. step_unknown:
  2208. fprintf(stderr, "error: invalid step: %s\n", optarg);
  2209. exit(-1);
  2210. }
  2211. case OPT_DISK:
  2212. test_disk_path = optarg;
  2213. break;
  2214. case OPT_TRACE:
  2215. test_trace_path = optarg;
  2216. break;
  2217. case OPT_TRACE_BACKTRACE:
  2218. test_trace_backtrace = true;
  2219. break;
  2220. case OPT_TRACE_PERIOD: {
  2221. char *parsed = NULL;
  2222. test_trace_period = strtoumax(optarg, &parsed, 0);
  2223. if (parsed == optarg) {
  2224. fprintf(stderr, "error: invalid trace-period: %s\n", optarg);
  2225. exit(-1);
  2226. }
  2227. break;
  2228. }
  2229. case OPT_TRACE_FREQ: {
  2230. char *parsed = NULL;
  2231. test_trace_freq = strtoumax(optarg, &parsed, 0);
  2232. if (parsed == optarg) {
  2233. fprintf(stderr, "error: invalid trace-freq: %s\n", optarg);
  2234. exit(-1);
  2235. }
  2236. break;
  2237. }
  2238. case OPT_READ_SLEEP: {
  2239. char *parsed = NULL;
  2240. double read_sleep = strtod(optarg, &parsed);
  2241. if (parsed == optarg) {
  2242. fprintf(stderr, "error: invalid read-sleep: %s\n", optarg);
  2243. exit(-1);
  2244. }
  2245. test_read_sleep = read_sleep*1.0e9;
  2246. break;
  2247. }
  2248. case OPT_PROG_SLEEP: {
  2249. char *parsed = NULL;
  2250. double prog_sleep = strtod(optarg, &parsed);
  2251. if (parsed == optarg) {
  2252. fprintf(stderr, "error: invalid prog-sleep: %s\n", optarg);
  2253. exit(-1);
  2254. }
  2255. test_prog_sleep = prog_sleep*1.0e9;
  2256. break;
  2257. }
  2258. case OPT_ERASE_SLEEP: {
  2259. char *parsed = NULL;
  2260. double erase_sleep = strtod(optarg, &parsed);
  2261. if (parsed == optarg) {
  2262. fprintf(stderr, "error: invalid erase-sleep: %s\n", optarg);
  2263. exit(-1);
  2264. }
  2265. test_erase_sleep = erase_sleep*1.0e9;
  2266. break;
  2267. }
  2268. // done parsing
  2269. case -1:
  2270. goto getopt_done;
  2271. // unknown arg, getopt prints a message for us
  2272. default:
  2273. exit(-1);
  2274. }
  2275. }
  2276. getopt_done: ;
  2277. if (argc > optind) {
  2278. // reset our test identifier list
  2279. test_ids = NULL;
  2280. test_id_count = 0;
  2281. test_id_capacity = 0;
  2282. }
  2283. // parse test identifier, if any, cannibalizing the arg in the process
  2284. for (; argc > optind; optind++) {
  2285. test_define_t *defines = NULL;
  2286. size_t define_count = 0;
  2287. lfs_emubd_powercycles_t *cycles = NULL;
  2288. size_t cycle_count = 0;
  2289. // parse name, can be suite or case
  2290. char *name = argv[optind];
  2291. char *defines_ = strchr(name, ':');
  2292. if (defines_) {
  2293. *defines_ = '\0';
  2294. defines_ += 1;
  2295. }
  2296. // remove optional path and .toml suffix
  2297. char *slash = strrchr(name, '/');
  2298. if (slash) {
  2299. name = slash+1;
  2300. }
  2301. size_t name_len = strlen(name);
  2302. if (name_len > 5 && strcmp(&name[name_len-5], ".toml") == 0) {
  2303. name[name_len-5] = '\0';
  2304. }
  2305. if (defines_) {
  2306. // parse defines
  2307. char *cycles_ = strchr(defines_, ':');
  2308. if (cycles_) {
  2309. *cycles_ = '\0';
  2310. cycles_ += 1;
  2311. }
  2312. while (true) {
  2313. char *parsed;
  2314. size_t d = leb16_parse(defines_, &parsed);
  2315. intmax_t v = leb16_parse(parsed, &parsed);
  2316. if (parsed == defines_) {
  2317. break;
  2318. }
  2319. defines_ = parsed;
  2320. if (d >= define_count) {
  2321. // align to power of two to avoid any superlinear growth
  2322. size_t ncount = 1 << lfs_npw2(d+1);
  2323. defines = realloc(defines,
  2324. ncount*sizeof(test_define_t));
  2325. memset(defines+define_count, 0,
  2326. (ncount-define_count)*sizeof(test_define_t));
  2327. define_count = ncount;
  2328. }
  2329. defines[d] = TEST_LIT(v);
  2330. }
  2331. if (cycles_) {
  2332. // parse power cycles
  2333. size_t cycle_capacity = 0;
  2334. while (*cycles_ != '\0') {
  2335. char *parsed = NULL;
  2336. *(lfs_emubd_powercycles_t*)mappend(
  2337. (void**)&cycles,
  2338. sizeof(lfs_emubd_powercycles_t),
  2339. &cycle_count,
  2340. &cycle_capacity)
  2341. = leb16_parse(cycles_, &parsed);
  2342. if (parsed == cycles_) {
  2343. fprintf(stderr, "error: "
  2344. "could not parse test cycles: %s\n",
  2345. cycles_);
  2346. exit(-1);
  2347. }
  2348. cycles_ = parsed;
  2349. }
  2350. }
  2351. }
  2352. // append to identifier list
  2353. *(test_id_t*)mappend(
  2354. (void**)&test_ids,
  2355. sizeof(test_id_t),
  2356. &test_id_count,
  2357. &test_id_capacity) = (test_id_t){
  2358. .name = name,
  2359. .defines = defines,
  2360. .define_count = define_count,
  2361. .cycles = cycles,
  2362. .cycle_count = cycle_count,
  2363. };
  2364. }
  2365. // do the thing
  2366. op();
  2367. // cleanup (need to be done for valgrind testing)
  2368. test_define_cleanup();
  2369. if (test_overrides) {
  2370. for (size_t i = 0; i < test_override_count; i++) {
  2371. free((void*)test_overrides[i].defines);
  2372. }
  2373. free((void*)test_overrides);
  2374. }
  2375. if (test_geometry_capacity) {
  2376. free((void*)test_geometries);
  2377. }
  2378. if (test_powerloss_capacity) {
  2379. for (size_t i = 0; i < test_powerloss_count; i++) {
  2380. free((void*)test_powerlosses[i].cycles);
  2381. }
  2382. free((void*)test_powerlosses);
  2383. }
  2384. if (test_id_capacity) {
  2385. for (size_t i = 0; i < test_id_count; i++) {
  2386. free((void*)test_ids[i].defines);
  2387. free((void*)test_ids[i].cycles);
  2388. }
  2389. free((void*)test_ids);
  2390. }
  2391. }