challenge.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2016, 2017, 2019 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)
  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 dump-port* ;FIXME: deduplicate
  174. (@@ (guix serialization) dump))
  175. (define (port-sha256* port size)
  176. ;; Like 'port-sha256', but limited to SIZE bytes.
  177. (let-values (((out get) (open-sha256-port)))
  178. (dump-port* port out size)
  179. (close-port out)
  180. (get)))
  181. (define (archive-contents port)
  182. "Return a list representing the files contained in the nar read from PORT."
  183. (fold-archive (lambda (file type contents result)
  184. (match type
  185. ((or 'regular 'executable)
  186. (match contents
  187. ((port . size)
  188. (cons `(,file ,type ,(port-sha256* port size))
  189. result))))
  190. ('directory 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 (guix-challenge . args)
  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