challenge.scm 20 KB

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