run.js 3.7 KB

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