test_runner.c 88 KB

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