run.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env node
  2. // USAGE:
  3. // node certora/run.js [[CONTRACT_NAME:]SPEC_NAME] [OPTIONS...]
  4. // EXAMPLES:
  5. // node certora/run.js AccessControl
  6. // node certora/run.js AccessControlHarness:AccessControl
  7. const MAX_PARALLEL = 4;
  8. let specs = require(__dirname + '/specs.json');
  9. const proc = require('child_process');
  10. const { PassThrough } = require('stream');
  11. const events = require('events');
  12. const limit = require('p-limit')(MAX_PARALLEL);
  13. let [, , request = '', ...extraOptions] = process.argv;
  14. if (request.startsWith('-')) {
  15. extraOptions.unshift(request);
  16. request = '';
  17. }
  18. if (request) {
  19. const [reqSpec, reqContract] = request.split(':').reverse();
  20. specs = Object.values(specs).filter(s => reqSpec === s.spec && (!reqContract || reqContract === s.contract));
  21. if (specs.length === 0) {
  22. console.error(`Error: Requested spec '${request}' not found in specs.json`);
  23. process.exit(1);
  24. }
  25. }
  26. for (const { spec, contract, files, options = [] } of Object.values(specs)) {
  27. limit(runCertora, spec, contract, files, [...options.flatMap(opt => opt.split(' ')), ...extraOptions]);
  28. }
  29. // Run certora, aggregate the output and print it at the end
  30. async function runCertora(spec, contract, files, options = []) {
  31. const args = [...files, '--verify', `${contract}:certora/specs/${spec}.spec`, ...options];
  32. const child = proc.spawn('certoraRun', args);
  33. const stream = new PassThrough();
  34. const output = collect(stream);
  35. child.stdout.pipe(stream, { end: false });
  36. child.stderr.pipe(stream, { end: false });
  37. // as soon as we have a jobStatus link, print it
  38. stream.on('data', function logStatusUrl(data) {
  39. const urls = data.toString('utf8').match(/https?:\S*/g);
  40. for (const url of urls ?? []) {
  41. if (url.includes('/jobStatus/')) {
  42. console.error(`[${spec}] ${url.replace('/jobStatus/', '/output/')}`);
  43. stream.off('data', logStatusUrl);
  44. break;
  45. }
  46. }
  47. });
  48. // wait for process end
  49. const [code, signal] = await events.once(child, 'exit');
  50. // error
  51. if (code || signal) {
  52. console.error(`[${spec}] Exited with code ${code || signal}`);
  53. process.exitCode = 1;
  54. }
  55. // get all output
  56. stream.end();
  57. // write results in markdown format
  58. writeEntry(spec, contract, code || signal, (await output).match(/https:\S*/)?.[0]);
  59. // write all details
  60. console.error(`+ certoraRun ${args.join(' ')}\n` + (await output));
  61. }
  62. // Collects stream data into a string
  63. async function collect(stream) {
  64. const buffers = [];
  65. for await (const data of stream) {
  66. const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
  67. buffers.push(buf);
  68. }
  69. return Buffer.concat(buffers).toString('utf8');
  70. }
  71. // Formatting
  72. let hasHeader = false;
  73. function formatRow(...array) {
  74. return ['', ...array, ''].join(' | ');
  75. }
  76. function writeHeader() {
  77. console.log(formatRow('spec', 'contract', 'result', 'status', 'output'));
  78. console.log(formatRow('-', '-', '-', '-', '-'));
  79. }
  80. function writeEntry(spec, contract, success, url) {
  81. if (!hasHeader) {
  82. hasHeader = true;
  83. writeHeader();
  84. }
  85. console.log(
  86. formatRow(
  87. spec,
  88. contract,
  89. success ? ':x:' : ':heavy_check_mark:',
  90. url ? `[link](${url})` : 'error',
  91. url ? `[link](${url?.replace('/jobStatus/', '/output/')})` : 'error',
  92. ),
  93. );
  94. }