challenge.scm 21 KB

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