challenge.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2016, 2017, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix scripts challenge)
  19. #:use-module (guix ui)
  20. #:use-module (guix scripts)
  21. #:use-module (guix store)
  22. #:use-module (guix utils)
  23. #:use-module (guix grafts)
  24. #:use-module (guix monads)
  25. #:use-module (guix base32)
  26. #:use-module (guix packages)
  27. #:use-module ((guix progress) #:hide (dump-port*))
  28. #:use-module (guix serialization)
  29. #:use-module (guix substitutes)
  30. #:use-module (guix narinfo)
  31. #:use-module (rnrs bytevectors)
  32. #:autoload (guix http-client) (http-fetch)
  33. #:use-module ((guix build syscalls) #:select (terminal-columns))
  34. #:use-module (gcrypt hash)
  35. #:use-module (srfi srfi-1)
  36. #:use-module (srfi srfi-9)
  37. #:use-module (srfi srfi-11)
  38. #:use-module (srfi srfi-26)
  39. #:use-module (srfi srfi-34)
  40. #:use-module (srfi srfi-37)
  41. #:use-module (ice-9 match)
  42. #:use-module (ice-9 vlist)
  43. #:use-module (ice-9 format)
  44. #:use-module (ice-9 ftw)
  45. #:use-module (web uri)
  46. #:export (compare-contents
  47. comparison-report?
  48. comparison-report-item
  49. comparison-report-result
  50. comparison-report-local-sha256
  51. comparison-report-narinfos
  52. comparison-report-match?
  53. comparison-report-mismatch?
  54. comparison-report-inconclusive?
  55. differing-files
  56. call-with-mismatches
  57. guix-challenge))
  58. ;;; Commentary:
  59. ;;;
  60. ;;; Challenge substitute servers, checking whether they provide the same
  61. ;;; binaries as those built locally.
  62. ;;;
  63. ;;; Here we completely bypass the daemon to access substitutes. This is
  64. ;;; because we want to be able to report fine-grain information about
  65. ;;; discrepancies: We need to show the URL of the offending nar, its hash, and
  66. ;;; so on.
  67. ;;;
  68. ;;; Code:
  69. (define ensure-store-item ;XXX: move to (guix ui)?
  70. (@@ (guix scripts size) ensure-store-item))
  71. ;; Representation of a comparison report for ITEM.
  72. (define-record-type <comparison-report>
  73. (%comparison-report item result local-sha256 narinfos)
  74. comparison-report?
  75. (item comparison-report-item) ;string, /gnu/store/… item
  76. (result comparison-report-result) ;'match | 'mismatch | 'inconclusive
  77. (local-sha256 comparison-report-local-sha256) ;bytevector | #f
  78. (narinfos comparison-report-narinfos)) ;list of <narinfo>
  79. (define-syntax comparison-report
  80. ;; Some sort of a an enum to make sure 'result' is correct.
  81. (syntax-rules (match mismatch inconclusive)
  82. ((_ item 'match rest ...)
  83. (%comparison-report item 'match rest ...))
  84. ((_ item 'mismatch rest ...)
  85. (%comparison-report item 'mismatch rest ...))
  86. ((_ item 'inconclusive rest ...)
  87. (%comparison-report item 'inconclusive rest ...))))
  88. (define (comparison-report-predicate result)
  89. "Return a predicate that returns true when pass a REPORT that has RESULT."
  90. (lambda (report)
  91. (eq? (comparison-report-result report) result)))
  92. (define comparison-report-mismatch?
  93. (comparison-report-predicate 'mismatch))
  94. (define comparison-report-match?
  95. (comparison-report-predicate 'match))
  96. (define comparison-report-inconclusive?
  97. (comparison-report-predicate 'inconclusive))
  98. (define (locally-built? store item)
  99. "Return true if ITEM was built locally."
  100. ;; XXX: For now approximate it by checking whether there's a build log for
  101. ;; ITEM. There could be false negatives, if logs have been removed.
  102. (->bool (log-file store item)))
  103. (define (query-locally-built-hash item)
  104. "Return the hash of ITEM, a store item, if ITEM was built locally.
  105. Otherwise return #f."
  106. (lambda (store)
  107. (guard (c ((store-protocol-error? c)
  108. (values #f store)))
  109. (if (locally-built? store item)
  110. (values (query-path-hash store item) store)
  111. (values #f store)))))
  112. (define-syntax-rule (report args ...)
  113. (format (current-error-port) args ...))
  114. (define (compare-contents items servers)
  115. "Challenge the substitute servers whose URLs are listed in SERVERS by
  116. comparing the hash of the substitutes of ITEMS that they serve. Return the
  117. list of <comparison-report> objects.
  118. This procedure does not authenticate narinfos from SERVERS, nor does it verify
  119. that they are signed by an authorized public keys. The reason is that, by
  120. definition, we may want to target unknown servers. Furthermore, no risk is
  121. taken since we do not import the archives."
  122. (define (compare item reference)
  123. ;; Return a procedure to compare the hash of ITEM with REFERENCE.
  124. (lambda (narinfo url)
  125. (or (not narinfo)
  126. (let ((value (narinfo-hash->sha256 (narinfo-hash narinfo))))
  127. (bytevector=? reference value)))))
  128. (define (select-reference item narinfos urls)
  129. ;; Return a "reference" narinfo among NARINFOS.
  130. (match narinfos
  131. ((first narinfos ...)
  132. (match servers
  133. ((url urls ...)
  134. (if (not first)
  135. (select-reference item narinfos urls)
  136. (narinfo-hash->sha256 (narinfo-hash first))))))))
  137. (mlet* %store-monad ((local (mapm %store-monad
  138. query-locally-built-hash items))
  139. (remote -> (append-map (cut lookup-narinfos <> items)
  140. servers))
  141. ;; No 'assert-valid-narinfo' on purpose.
  142. (narinfos -> (fold (lambda (narinfo vhash)
  143. (vhash-cons (narinfo-path narinfo) narinfo
  144. vhash))
  145. vlist-null
  146. remote)))
  147. (return (map (lambda (item local)
  148. (match (vhash-fold* cons '() item narinfos)
  149. (() ;no substitutes
  150. (comparison-report item 'inconclusive local '()))
  151. ((narinfo)
  152. (if local
  153. (if ((compare item local) narinfo (first servers))
  154. (comparison-report item 'match
  155. local (list narinfo))
  156. (comparison-report item 'mismatch
  157. local (list narinfo)))
  158. (comparison-report item 'inconclusive
  159. local (list narinfo))))
  160. ((narinfos ...)
  161. (let ((reference
  162. (or local (select-reference item narinfos
  163. servers))))
  164. (if (every (compare item reference) narinfos servers)
  165. (comparison-report item 'match
  166. local narinfos)
  167. (comparison-report item 'mismatch
  168. local narinfos))))))
  169. items
  170. local))))
  171. ;;;
  172. ;;; Reporting.
  173. ;;;
  174. (define (port-sha256* port size)
  175. ;; Like 'port-sha256', but limited to SIZE bytes.
  176. (let-values (((out get) (open-sha256-port)))
  177. (dump-port* port out size)
  178. (close-port out)
  179. (get)))
  180. (define (archive-contents port)
  181. "Return a list representing the files contained in the nar read from PORT."
  182. (fold-archive (lambda (file type contents result)
  183. (match type
  184. ((or 'regular 'executable)
  185. (match contents
  186. ((port . size)
  187. (cons `(,file ,type ,(port-sha256* port size))
  188. result))))
  189. ('directory result)
  190. ('directory-complete result)
  191. ('symlink
  192. (cons `(,file ,type ,contents) result))))
  193. '()
  194. port
  195. ""))
  196. (define (store-item-contents item)
  197. "Return a list of files and contents for ITEM in the same format as
  198. 'archive-contents'."
  199. (file-system-fold (const #t) ;enter?
  200. (lambda (file stat result) ;leaf
  201. (define short
  202. (string-drop file (string-length item)))
  203. (match (stat:type stat)
  204. ('regular
  205. (let ((size (stat:size stat))
  206. (type (if (zero? (logand (stat:mode stat)
  207. #o100))
  208. 'regular
  209. 'executable)))
  210. (cons `(,short ,type
  211. ,(call-with-input-file file
  212. (cut port-sha256* <> size)))
  213. result)))
  214. ('symlink
  215. (cons `(,short symlink ,(readlink file))
  216. result))))
  217. (lambda (directory stat result) result) ;down
  218. (lambda (directory stat result) result) ;up
  219. (lambda (file stat result) result) ;skip
  220. (lambda (file stat errno result) result) ;error
  221. '()
  222. item
  223. lstat))
  224. (define (call-with-nar narinfo proc)
  225. "Call PROC with an input port from which it can read the nar pointed to by
  226. NARINFO."
  227. (let*-values (((uri compression size)
  228. (narinfo-best-uri narinfo))
  229. ((port actual-size)
  230. (http-fetch uri)))
  231. (define reporter
  232. (progress-reporter/file (narinfo-path narinfo)
  233. (and size
  234. (max size (or actual-size 0))) ;defensive
  235. #:abbreviation (const (uri-host uri))))
  236. (define result
  237. (call-with-decompressed-port (string->symbol compression)
  238. (progress-report-port reporter port)
  239. proc))
  240. (close-port port)
  241. (erase-current-line (current-output-port))
  242. result))
  243. (define (narinfo-contents narinfo)
  244. "Fetch the nar described by NARINFO and return a list representing the file
  245. it contains."
  246. (call-with-nar narinfo archive-contents))
  247. (define (differing-files comparison-report)
  248. "Return a list of files that differ among the nars and possibly the local
  249. store item specified in COMPARISON-REPORT."
  250. (define contents
  251. (map narinfo-contents
  252. (comparison-report-narinfos comparison-report)))
  253. (define local-contents
  254. (and (comparison-report-local-sha256 comparison-report)
  255. (store-item-contents (comparison-report-item comparison-report))))
  256. (match (apply lset-difference equal?
  257. (take (delete-duplicates
  258. (if local-contents
  259. (cons local-contents contents)
  260. contents))
  261. 2))
  262. (((files _ ...) ...)
  263. files)))
  264. (define (report-differing-files comparison-report)
  265. "Report differences among the nars and possibly the local store item
  266. specified in COMPARISON-REPORT."
  267. (match (differing-files comparison-report)
  268. (()
  269. #t)
  270. ((files ...)
  271. (format #t (N_ " differing file:~%"
  272. " differing files:~%"
  273. (length files)))
  274. (format #t "~{ ~a~%~}" files))))
  275. (define (call-with-mismatches comparison-report proc)
  276. "Call PROC with two directories containing the mismatching store items."
  277. (define local-hash
  278. (comparison-report-local-sha256 comparison-report))
  279. (define narinfos
  280. (comparison-report-narinfos comparison-report))
  281. (call-with-temporary-directory
  282. (lambda (directory1)
  283. (call-with-temporary-directory
  284. (lambda (directory2)
  285. (define narinfo1
  286. (if local-hash
  287. (find (lambda (narinfo)
  288. (not (bytevector=? (narinfo-hash->sha256
  289. (narinfo-hash narinfo))
  290. local-hash)))
  291. narinfos)
  292. (first (comparison-report-narinfos comparison-report))))
  293. (define narinfo2
  294. (and (not local-hash)
  295. (find (lambda (narinfo)
  296. (not (eq? narinfo narinfo1)))
  297. narinfos)))
  298. (rmdir directory1)
  299. (call-with-nar narinfo1 (cut restore-file <> directory1))
  300. (when narinfo2
  301. (rmdir directory2)
  302. (call-with-nar narinfo2 (cut restore-file <> directory2)))
  303. (proc directory1
  304. (if local-hash
  305. (comparison-report-item comparison-report)
  306. directory2)))))))
  307. (define %diffoscope-command
  308. ;; Default external diff command. Pass "--exclude-directory-metadata" so
  309. ;; that the mtime/ctime differences are ignored.
  310. '("diffoscope" "--exclude-directory-metadata=yes"))
  311. (define* (report-differing-files/external comparison-report
  312. #:optional
  313. (command %diffoscope-command))
  314. "Run COMMAND to show the file-level differences for the mismatches in
  315. COMPARISON-REPORT."
  316. (call-with-mismatches comparison-report
  317. (lambda (directory1 directory2)
  318. (apply system*
  319. (append command
  320. (list directory1 directory2))))))
  321. (define* (summarize-report comparison-report
  322. #:key
  323. (report-differences (const #f))
  324. (hash->string bytevector->nix-base32-string)
  325. verbose?)
  326. "Write to the current error port a summary of COMPARISON-REPORT, a
  327. <comparison-report> object. When VERBOSE?, display matches in addition to
  328. mismatches and inconclusive reports. Upon mismatch, call REPORT-DIFFERENCES
  329. with COMPARISON-REPORT."
  330. (define (report-hashes item local narinfos)
  331. (if local
  332. (report (G_ " local hash: ~a~%") (hash->string local))
  333. (report (G_ " no local build for '~a'~%") item))
  334. (for-each (lambda (narinfo)
  335. (report (G_ " ~50a: ~a~%")
  336. (uri->string (narinfo-best-uri narinfo))
  337. (hash->string
  338. (narinfo-hash->sha256 (narinfo-hash narinfo)))))
  339. narinfos))
  340. (match comparison-report
  341. (($ <comparison-report> item 'mismatch local (narinfos ...))
  342. (report (G_ "~a contents differ:~%") item)
  343. (report-hashes item local narinfos)
  344. (report-differences comparison-report))
  345. (($ <comparison-report> item 'inconclusive #f narinfos)
  346. (warning (G_ "could not challenge '~a': no local build~%") item))
  347. (($ <comparison-report> item 'inconclusive locals ())
  348. (warning (G_ "could not challenge '~a': no substitutes~%") item))
  349. (($ <comparison-report> item 'match local (narinfos ...))
  350. (when verbose?
  351. (report (G_ "~a contents match:~%") item)
  352. (report-hashes item local narinfos)))))
  353. (define (summarize-report-list reports)
  354. "Display the overall summary of REPORTS."
  355. (let ((total (length reports))
  356. (inconclusive (count comparison-report-inconclusive? reports))
  357. (matches (count comparison-report-match? reports))
  358. (discrepancies (count comparison-report-mismatch? reports)))
  359. (report (G_ "~h store items were analyzed:~%") total)
  360. (report (G_ " - ~h (~,1f%) were identical~%")
  361. matches (* 100. (/ matches total)))
  362. (report (G_ " - ~h (~,1f%) differed~%")
  363. discrepancies (* 100. (/ discrepancies total)))
  364. (report (G_ " - ~h (~,1f%) were inconclusive~%")
  365. inconclusive (* 100. (/ inconclusive total)))))
  366. ;;;
  367. ;;; Command-line options.
  368. ;;;
  369. (define (show-help)
  370. (display (G_ "Usage: guix challenge [PACKAGE...]
  371. Challenge the substitutes for PACKAGE... provided by one or more servers.\n"))
  372. (display (G_ "
  373. --substitute-urls=URLS
  374. compare build results with those at URLS"))
  375. (display (G_ "
  376. -v, --verbose show details about successful comparisons"))
  377. (display (G_ "
  378. --diff=MODE show differences according to MODE"))
  379. (newline)
  380. (display (G_ "
  381. -h, --help display this help and exit"))
  382. (display (G_ "
  383. -V, --version display version information and exit"))
  384. (newline)
  385. (show-bug-report-information))
  386. (define %options
  387. (list (option '(#\h "help") #f #f
  388. (lambda args
  389. (show-help)
  390. (exit 0)))
  391. (option '(#\V "version") #f #f
  392. (lambda args
  393. (show-version-and-exit "guix challenge")))
  394. (option '("diff") #t #f
  395. (lambda (opt name arg result . rest)
  396. (define mode
  397. (match arg
  398. ("none" (const #t))
  399. ("simple" report-differing-files)
  400. ("diffoscope" report-differing-files/external)
  401. ((and (? (cut string-prefix? "/" <>)) command)
  402. (cute report-differing-files/external <>
  403. (string-tokenize command)))
  404. (_ (leave (G_ "~a: unknown diff mode~%") arg))))
  405. (apply values
  406. (alist-cons 'difference-report mode result)
  407. rest)))
  408. (option '("substitute-urls") #t #f
  409. (lambda (opt name arg result . rest)
  410. (apply values
  411. (alist-cons 'substitute-urls
  412. (string-tokenize arg)
  413. (alist-delete 'substitute-urls result))
  414. rest)))
  415. (option '("verbose" #\v) #f #f
  416. (lambda (opt name arg result . rest)
  417. (apply values
  418. (alist-cons 'verbose? #t result)
  419. rest)))))
  420. (define %default-options
  421. `((system . ,(%current-system))
  422. (substitute-urls . ,%default-substitute-urls)
  423. (difference-report . ,report-differing-files)))
  424. ;;;
  425. ;;; Entry point.
  426. ;;;
  427. (define-command (guix-challenge . args)
  428. (category packaging)
  429. (synopsis "challenge substitute servers, comparing their binaries")
  430. (with-error-handling
  431. (let* ((opts (parse-command-line args %options (list %default-options)
  432. #:build-options? #f))
  433. (files (filter-map (match-lambda
  434. (('argument . file) file)
  435. (_ #f))
  436. opts))
  437. (system (assoc-ref opts 'system))
  438. (urls (assoc-ref opts 'substitute-urls))
  439. (diff (assoc-ref opts 'difference-report))
  440. (verbose? (assoc-ref opts 'verbose?)))
  441. (leave-on-EPIPE
  442. (with-store store
  443. ;; Disable grafts since substitute servers normally provide only
  444. ;; ungrafted stuff.
  445. (parameterize ((%graft? #f)
  446. (current-terminal-columns (terminal-columns)))
  447. (let ((files (match files
  448. (()
  449. (filter (cut locally-built? store <>)
  450. (live-paths store)))
  451. (x
  452. files))))
  453. (set-build-options store
  454. #:use-substitutes? #f)
  455. (run-with-store store
  456. (mlet* %store-monad ((items (mapm %store-monad
  457. ensure-store-item files))
  458. (reports (compare-contents items urls)))
  459. (for-each (cut summarize-report <> #:verbose? verbose?
  460. #:report-differences diff)
  461. reports)
  462. (report "\n")
  463. (summarize-report-list reports)
  464. (exit (cond ((any comparison-report-mismatch? reports) 2)
  465. ((every comparison-report-match? reports) 0)
  466. (else 1))))
  467. #:system system))))))))
  468. ;;; challenge.scm ends here