challenge.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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-26)
  38. #:use-module (srfi srfi-34)
  39. #:use-module (srfi srfi-37)
  40. #:use-module (srfi srfi-71)
  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 ((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. The list is sorted in canonical order--i.e., the order in which entries appear
  183. in the nar."
  184. (reverse
  185. (fold-archive (lambda (file type contents result)
  186. (match type
  187. ((or 'regular 'executable)
  188. (match contents
  189. ((port . size)
  190. (cons `(,file ,type ,(port-sha256* port size))
  191. result))))
  192. ('directory result)
  193. ('directory-complete result)
  194. ('symlink
  195. (cons `(,file ,type ,contents) result))))
  196. '()
  197. port
  198. "")))
  199. (define (store-item-contents item)
  200. "Return a list of files and contents for ITEM in the same format as
  201. 'archive-contents'."
  202. (let loop ((file item))
  203. (define stat
  204. (lstat file))
  205. (define short
  206. (string-drop file (string-length item)))
  207. (match (stat:type stat)
  208. ('regular
  209. (let ((size (stat:size stat))
  210. (type (if (zero? (logand (stat:mode stat)
  211. #o100))
  212. 'regular
  213. 'executable)))
  214. `((,short ,type
  215. ,(call-with-input-file file
  216. (cut port-sha256* <> size))))))
  217. ('symlink
  218. `((,short symlink ,(readlink file))))
  219. ('directory
  220. (append-map (match-lambda
  221. ((or "." "..")
  222. '())
  223. (entry
  224. (loop (string-append file "/" entry))))
  225. ;; Traverse entries in canonical order, the same as the
  226. ;; order of entries in nars.
  227. (scandir file (const #t) string<?))))))
  228. (define (call-with-nar narinfo proc)
  229. "Call PROC with an input port from which it can read the nar pointed to by
  230. NARINFO."
  231. (let* ((uri compression size (narinfo-best-uri narinfo))
  232. (port actual-size (http-fetch uri)))
  233. (define reporter
  234. (progress-reporter/file (narinfo-path narinfo)
  235. (and size
  236. (max size (or actual-size 0))) ;defensive
  237. #:abbreviation (const (uri-host uri))))
  238. (define result
  239. (call-with-decompressed-port (string->symbol compression)
  240. (progress-report-port reporter port)
  241. proc))
  242. (close-port port)
  243. (erase-current-line (current-output-port))
  244. result))
  245. (define (narinfo-contents narinfo)
  246. "Fetch the nar described by NARINFO and return a list representing the file
  247. it contains."
  248. (call-with-nar narinfo archive-contents))
  249. (define (differing-files comparison-report)
  250. "Return a list of files that differ among the nars and possibly the local
  251. store item specified in COMPARISON-REPORT."
  252. (define contents
  253. (map narinfo-contents
  254. (comparison-report-narinfos comparison-report)))
  255. (define local-contents
  256. (and (comparison-report-local-sha256 comparison-report)
  257. (store-item-contents (comparison-report-item comparison-report))))
  258. (match (apply lset-difference equal?
  259. (take (delete-duplicates
  260. (if local-contents
  261. (cons local-contents contents)
  262. contents))
  263. 2))
  264. (((files _ ...) ...)
  265. files)))
  266. (define (report-differing-files comparison-report)
  267. "Report differences among the nars and possibly the local store item
  268. specified in COMPARISON-REPORT."
  269. (match (differing-files comparison-report)
  270. (()
  271. #t)
  272. ((files ...)
  273. (format #t (N_ " differing file:~%"
  274. " differing files:~%"
  275. (length files)))
  276. (format #t "~{ ~a~%~}" files))))
  277. (define (call-with-mismatches comparison-report proc)
  278. "Call PROC with two directories containing the mismatching store items."
  279. (define local-hash
  280. (comparison-report-local-sha256 comparison-report))
  281. (define narinfos
  282. (comparison-report-narinfos comparison-report))
  283. (call-with-temporary-directory
  284. (lambda (directory1)
  285. (call-with-temporary-directory
  286. (lambda (directory2)
  287. (define narinfo1
  288. (if local-hash
  289. (find (lambda (narinfo)
  290. (not (bytevector=? (narinfo-hash->sha256
  291. (narinfo-hash narinfo))
  292. local-hash)))
  293. narinfos)
  294. (first (comparison-report-narinfos comparison-report))))
  295. (define narinfo2
  296. (and (not local-hash)
  297. (find (lambda (narinfo)
  298. (not (eq? narinfo narinfo1)))
  299. narinfos)))
  300. (rmdir directory1)
  301. (call-with-nar narinfo1 (cut restore-file <> directory1))
  302. (when narinfo2
  303. (rmdir directory2)
  304. (call-with-nar narinfo2 (cut restore-file <> directory2)))
  305. (proc directory1
  306. (if local-hash
  307. (comparison-report-item comparison-report)
  308. directory2)))))))
  309. (define %diffoscope-command
  310. ;; Default external diff command. Pass "--exclude-directory-metadata" so
  311. ;; that the mtime/ctime differences are ignored.
  312. '("diffoscope" "--exclude-directory-metadata=yes"))
  313. (define* (report-differing-files/external comparison-report
  314. #:optional
  315. (command %diffoscope-command))
  316. "Run COMMAND to show the file-level differences for the mismatches in
  317. COMPARISON-REPORT."
  318. (call-with-mismatches comparison-report
  319. (lambda (directory1 directory2)
  320. (apply system*
  321. (append command
  322. (list directory1 directory2))))))
  323. (define* (summarize-report comparison-report
  324. #:key
  325. (report-differences (const #f))
  326. (hash->string bytevector->nix-base32-string)
  327. verbose?)
  328. "Write to the current error port a summary of COMPARISON-REPORT, a
  329. <comparison-report> object. When VERBOSE?, display matches in addition to
  330. mismatches and inconclusive reports. Upon mismatch, call REPORT-DIFFERENCES
  331. with COMPARISON-REPORT."
  332. (define (report-hashes item local narinfos)
  333. (if local
  334. (report (G_ " local hash: ~a~%") (hash->string local))
  335. (report (G_ " no local build for '~a'~%") item))
  336. (for-each (lambda (narinfo)
  337. (report (G_ " ~50a: ~a~%")
  338. (uri->string (narinfo-best-uri narinfo))
  339. (hash->string
  340. (narinfo-hash->sha256 (narinfo-hash narinfo)))))
  341. narinfos))
  342. (match comparison-report
  343. (($ <comparison-report> item 'mismatch local (narinfos ...))
  344. (report (G_ "~a contents differ:~%") item)
  345. (report-hashes item local narinfos)
  346. (report-differences comparison-report))
  347. (($ <comparison-report> item 'inconclusive #f narinfos)
  348. (warning (G_ "could not challenge '~a': no local build~%") item))
  349. (($ <comparison-report> item 'inconclusive locals ())
  350. (warning (G_ "could not challenge '~a': no substitutes~%") item))
  351. (($ <comparison-report> item 'match local (narinfos ...))
  352. (when verbose?
  353. (report (G_ "~a contents match:~%") item)
  354. (report-hashes item local narinfos)))))
  355. (define (summarize-report-list reports)
  356. "Display the overall summary of REPORTS."
  357. (let ((total (length reports))
  358. (inconclusive (count comparison-report-inconclusive? reports))
  359. (matches (count comparison-report-match? reports))
  360. (discrepancies (count comparison-report-mismatch? reports)))
  361. (report (G_ "~h store items were analyzed:~%") total)
  362. (report (G_ " - ~h (~,1f%) were identical~%")
  363. matches (* 100. (/ matches total)))
  364. (report (G_ " - ~h (~,1f%) differed~%")
  365. discrepancies (* 100. (/ discrepancies total)))
  366. (report (G_ " - ~h (~,1f%) were inconclusive~%")
  367. inconclusive (* 100. (/ inconclusive total)))))
  368. ;;;
  369. ;;; Command-line options.
  370. ;;;
  371. (define (show-help)
  372. (display (G_ "Usage: guix challenge [PACKAGE...]
  373. Challenge the substitutes for PACKAGE... provided by one or more servers.\n"))
  374. (display (G_ "
  375. --substitute-urls=URLS
  376. compare build results with those at URLS"))
  377. (display (G_ "
  378. -v, --verbose show details about successful comparisons"))
  379. (display (G_ "
  380. --diff=MODE show differences according to MODE"))
  381. (newline)
  382. (display (G_ "
  383. -h, --help display this help and exit"))
  384. (display (G_ "
  385. -V, --version display version information and exit"))
  386. (newline)
  387. (show-bug-report-information))
  388. (define %options
  389. (list (option '(#\h "help") #f #f
  390. (lambda args
  391. (show-help)
  392. (exit 0)))
  393. (option '(#\V "version") #f #f
  394. (lambda args
  395. (show-version-and-exit "guix challenge")))
  396. (option '("diff") #t #f
  397. (lambda (opt name arg result . rest)
  398. (define mode
  399. (match arg
  400. ("none" (const #t))
  401. ("simple" report-differing-files)
  402. ("diffoscope" report-differing-files/external)
  403. ((and (? (cut string-prefix? "/" <>)) command)
  404. (cute report-differing-files/external <>
  405. (string-tokenize command)))
  406. (_ (leave (G_ "~a: unknown diff mode~%") arg))))
  407. (apply values
  408. (alist-cons 'difference-report mode result)
  409. rest)))
  410. (option '("substitute-urls") #t #f
  411. (lambda (opt name arg result . rest)
  412. (apply values
  413. (alist-cons 'substitute-urls
  414. (string-tokenize arg)
  415. (alist-delete 'substitute-urls result))
  416. rest)))
  417. (option '("verbose" #\v) #f #f
  418. (lambda (opt name arg result . rest)
  419. (apply values
  420. (alist-cons 'verbose? #t result)
  421. rest)))))
  422. (define %default-options
  423. `((system . ,(%current-system))
  424. (substitute-urls . ,%default-substitute-urls)
  425. (difference-report . ,report-differing-files)))
  426. ;;;
  427. ;;; Entry point.
  428. ;;;
  429. (define-command (guix-challenge . args)
  430. (category packaging)
  431. (synopsis "challenge substitute servers, comparing their binaries")
  432. (with-error-handling
  433. (let* ((opts (parse-command-line args %options (list %default-options)
  434. #:build-options? #f))
  435. (files (filter-map (match-lambda
  436. (('argument . file) file)
  437. (_ #f))
  438. opts))
  439. (system (assoc-ref opts 'system))
  440. (urls (assoc-ref opts 'substitute-urls))
  441. (diff (assoc-ref opts 'difference-report))
  442. (verbose? (assoc-ref opts 'verbose?)))
  443. (leave-on-EPIPE
  444. (with-store store
  445. ;; Disable grafts since substitute servers normally provide only
  446. ;; ungrafted stuff.
  447. (parameterize ((%graft? #f)
  448. (current-terminal-columns (terminal-columns)))
  449. (let ((files (match files
  450. (()
  451. (filter (cut locally-built? store <>)
  452. (live-paths store)))
  453. (x
  454. files))))
  455. (set-build-options store
  456. #:use-substitutes? #f)
  457. (run-with-store store
  458. (mlet* %store-monad ((items (mapm %store-monad
  459. ensure-store-item files))
  460. (reports (compare-contents items urls)))
  461. (for-each (cut summarize-report <> #:verbose? verbose?
  462. #:report-differences diff)
  463. reports)
  464. (report "\n")
  465. (summarize-report-list reports)
  466. (exit (cond ((any comparison-report-mismatch? reports) 2)
  467. ((every comparison-report-match? reports) 0)
  468. (else 1))))
  469. #:system system))))))))
  470. ;;; challenge.scm ends here