run.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. const 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. const [reqSpec, reqContract] = request.split(':').reverse();
  19. for (const { spec, contract, files, options = [] } of Object.values(specs)) {
  20. if ((!reqSpec || reqSpec === spec) && (!reqContract || reqContract === contract)) {
  21. limit(runCertora, spec, contract, files, [...options, ...extraOptions]);
  22. }
  23. }
  24. // Run certora, aggregate the output and print it at the end
  25. async function runCertora(spec, contract, files, options = []) {
  26. const args = [...files, '--verify', `${contract}:certora/specs/${spec}.spec`, ...options];
  27. const child = proc.spawn('certoraRun', args);
  28. const stream = new PassThrough();
  29. const output = collect(stream);
  30. child.stdout.pipe(stream, { end: false });
  31. child.stderr.pipe(stream, { end: false });
  32. // as soon as we have a jobStatus link, print it
  33. stream.on('data', function logStatusUrl(data) {
  34. const urls = data.toString('utf8').match(/https?:\S*/g);
  35. for (const url of urls ?? []) {
  36. if (url.includes('/jobStatus/')) {
  37. console.error(`[${spec}] ${url}`);
  38. stream.off('data', logStatusUrl);
  39. break;
  40. }
  41. }
  42. });
  43. // wait for process end
  44. const [code, signal] = await events.once(child, 'exit');
  45. // error
  46. if (code || signal) {
  47. console.error(`[${spec}] Exited with code ${code || signal}`);
  48. process.exitCode = 1;
  49. }
  50. // get all output
  51. stream.end();
  52. // write results in markdown format
  53. writeEntry(spec, contract, code || signal, (await output).match(/https:\S*/)[0]);
  54. // write all details
  55. console.error(`+ certoraRun ${args.join(' ')}\n` + (await output));
  56. }
  57. // Collects stream data into a string
  58. async function collect(stream) {
  59. const buffers = [];
  60. for await (const data of stream) {
  61. const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
  62. buffers.push(buf);
  63. }
  64. return Buffer.concat(buffers).toString('utf8');
  65. }
  66. // Formatting
  67. let hasHeader = false;
  68. function formatRow(...array) {
  69. return ['', ...array, ''].join(' | ');
  70. }
  71. function writeHeader() {
  72. console.log(formatRow('spec', 'contract', 'result', 'status', 'output'));
  73. console.log(formatRow('-', '-', '-', '-', '-'));
  74. }
  75. function writeEntry(spec, contract, success, url) {
  76. if (!hasHeader) {
  77. hasHeader = true;
  78. writeHeader();
  79. }
  80. console.log(
  81. formatRow(
  82. spec,
  83. contract,
  84. success ? ':x:' : ':heavy_check_mark:',
  85. `[link](${url})`,
  86. `[link](${url.replace('/jobStatus/', '/output/')})`,
  87. ),
  88. );
  89. }