run.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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, ...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 = [
  58. ...files,
  59. '--verify',
  60. `${contract}:certora/specs/${spec}.spec`,
  61. ...options.flatMap(opt => opt.split(' ')),
  62. ];
  63. const child = proc.spawn('certoraRun', args);
  64. const stream = new PassThrough();
  65. const output = collect(stream);
  66. child.stdout.pipe(stream, { end: false });
  67. child.stderr.pipe(stream, { end: false });
  68. // as soon as we have a job id, print the output link
  69. stream.on('data', function logStatusUrl(data) {
  70. const { '-DjobId': jobId, '-DuserId': userId } = Object.fromEntries(
  71. data
  72. .toString('utf8')
  73. .match(/-D\S+=\S+/g)
  74. ?.map(s => s.split('=')) || [],
  75. );
  76. if (jobId && userId) {
  77. console.error(`[${spec}] https://prover.certora.com/output/${userId}/${jobId}/`);
  78. stream.off('data', logStatusUrl);
  79. }
  80. });
  81. // wait for process end
  82. const [code, signal] = await events.once(child, 'exit');
  83. // error
  84. if (code || signal) {
  85. console.error(`[${spec}] Exited with code ${code || signal}`);
  86. process.exitCode = 1;
  87. }
  88. // get all output
  89. stream.end();
  90. // write results in markdown format
  91. writeEntry(spec, contract, code || signal, (await output).match(/https:\/\/prover.certora.com\/output\/\S*/)?.[0]);
  92. // write all details
  93. console.error(`+ certoraRun ${args.join(' ')}\n` + (await output));
  94. }
  95. // Collects stream data into a string
  96. async function collect(stream) {
  97. const buffers = [];
  98. for await (const data of stream) {
  99. const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
  100. buffers.push(buf);
  101. }
  102. return Buffer.concat(buffers).toString('utf8');
  103. }
  104. // Formatting
  105. let hasHeader = false;
  106. function formatRow(...array) {
  107. return ['', ...array, ''].join(' | ');
  108. }
  109. function writeHeader() {
  110. console.log(formatRow('spec', 'contract', 'result', 'status', 'output'));
  111. console.log(formatRow('-', '-', '-', '-', '-'));
  112. }
  113. function writeEntry(spec, contract, success, url) {
  114. if (!hasHeader) {
  115. hasHeader = true;
  116. writeHeader();
  117. }
  118. console.log(
  119. formatRow(
  120. spec,
  121. contract,
  122. success ? ':x:' : ':heavy_check_mark:',
  123. url ? `[link](${url?.replace('/output/', '/jobStatus/')})` : 'error',
  124. url ? `[link](${url})` : 'error',
  125. ),
  126. );
  127. }