gen_fixtures.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env node
  2. "use strict";
  3. const fs = require("fs");
  4. const os = require("os");
  5. const path = require("path");
  6. const { spawn, spawnSync } = require("child_process");
  7. const DEBUG = process.env.DEBUG || false;
  8. // Maximum number of gdb processes to spawn in parallel
  9. const MAX_PARALLEL_PROCS = +process.env.MAX_PARALLEL_PROCS || 32;
  10. const SYNC_GDB_EXECUTION = process.env.SYNC_GDB_EXECUTION || false;
  11. // Usage: console.log(CYAN_FMT, "This shows up in cyan!")
  12. const CYAN_FMT = "\x1b[36m%s\x1b[0m";
  13. const YELLOW_FMT = "\x1b[33m%s\x1b[0m";
  14. const TEST_DIR = __dirname + "/";
  15. const BUILD_DIR = path.join(TEST_DIR, "build");
  16. const GDB_DEFAULT_ARGS = [
  17. "-batch",
  18. `--command=${TEST_DIR}gdb-extract-def`,
  19. // Set a breakpoint "in the future", which all the test binaries can then share
  20. "--eval-command=set breakpoint pending on",
  21. "--eval-command=break loop",
  22. "--eval-command=catch signal SIGFPE",
  23. "--eval-command=catch signal SIGILL",
  24. ];
  25. /* Split up an array into semi-evenly sized chunks */
  26. function chunk(source, num_chunks)
  27. {
  28. const arr = source.slice();
  29. const ret = [];
  30. let rem_chunks = num_chunks;
  31. while(rem_chunks > 0)
  32. {
  33. // We guarantee that the entire array is processed because when rem_chunk=1 -> len/1 = len
  34. ret.push(arr.splice(0, Math.floor(arr.length / rem_chunks)));
  35. rem_chunks--;
  36. }
  37. return ret;
  38. }
  39. console.assert(
  40. JSON.stringify(chunk("0 0 1 1 2 2 2 3 3 3".split(" "), 4)) ===
  41. JSON.stringify([["0", "0"],
  42. ["1", "1"],
  43. ["2", "2", "2"],
  44. ["3", "3", "3"]]),
  45. "Chunk"
  46. );
  47. const dir_files = fs.readdirSync(BUILD_DIR);
  48. const test_files = dir_files.filter(name => {
  49. return name.endsWith(".bin");
  50. }).map(name => {
  51. return name.slice(0, -4);
  52. }).filter(name => {
  53. const bin_file = path.join(BUILD_DIR, `${name}.bin`);
  54. const fixture_file = path.join(BUILD_DIR, `${name}.fixture`);
  55. if(!fs.existsSync(fixture_file))
  56. {
  57. return true;
  58. }
  59. return fs.statSync(bin_file).mtime > fs.statSync(fixture_file).mtime;
  60. });
  61. const nr_of_cpus = Math.min(
  62. os.cpus().length || 1,
  63. test_files.length,
  64. MAX_PARALLEL_PROCS
  65. );
  66. console.log("[+] Using %d cpus to generate %d fixtures", nr_of_cpus, test_files.length);
  67. const workloads = chunk(test_files, nr_of_cpus);
  68. function test_arg_formatter(workload)
  69. {
  70. return workload.map(test => {
  71. const test_path = path.join(BUILD_DIR, test);
  72. return `--eval-command=extract-state ${test_path}.bin ${test_path}.fixture`;
  73. });
  74. }
  75. function set_proc_handlers(proc, n)
  76. {
  77. proc.on("close", (code) => on_proc_close(code, n));
  78. if(DEBUG)
  79. {
  80. proc.stdout.on("data", (data) => {
  81. console.log(CYAN_FMT, "stdout", `${n}: ${data}`);
  82. });
  83. proc.stderr.on("data", (data) => {
  84. console.log(YELLOW_FMT, "stderr", `${n}: ${data}`);
  85. });
  86. }
  87. }
  88. function on_proc_close(code, n)
  89. {
  90. console.log(`[+] child process ${n} exited with code ${code}`);
  91. if(code !== 0)
  92. {
  93. process.exit(code);
  94. }
  95. }
  96. for(let i = 0; i < nr_of_cpus; i++)
  97. {
  98. const gdb_args = GDB_DEFAULT_ARGS.concat(test_arg_formatter(workloads[i]));
  99. if(DEBUG)
  100. {
  101. console.log(CYAN_FMT, "[DEBUG]", "gdb", gdb_args.join(" "));
  102. }
  103. if(SYNC_GDB_EXECUTION)
  104. {
  105. const { status: code } = spawnSync("gdb", gdb_args);
  106. on_proc_close(code, i);
  107. }
  108. else
  109. {
  110. const gdb = spawn("gdb", gdb_args);
  111. set_proc_handlers(gdb, i);
  112. }
  113. }