gremlin.scm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2018, 2020, 2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2022 Chris Marusich <cmmarusich@gmail.com>
  4. ;;; Copyright © 2022 Pierre Langlois <pierre.langlois@gmx.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (test-gremlin)
  21. #:use-module (guix elf)
  22. #:use-module (guix tests)
  23. #:use-module ((guix utils) #:select (call-with-temporary-directory
  24. target-aarch64?))
  25. #:use-module (guix build utils)
  26. #:use-module (guix build gremlin)
  27. #:use-module (gnu packages bootstrap)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-26)
  30. #:use-module (srfi srfi-34)
  31. #:use-module (srfi srfi-64)
  32. #:use-module (rnrs io ports)
  33. #:use-module (ice-9 popen)
  34. #:use-module (ice-9 rdelim)
  35. #:use-module (ice-9 regex)
  36. #:use-module (ice-9 match))
  37. (define %guile-executable
  38. (match (false-if-exception (readlink "/proc/self/exe"))
  39. ((? string? program)
  40. (and (file-exists? program) (elf-file? program)
  41. program))
  42. (_
  43. #f)))
  44. (define read-elf
  45. (compose parse-elf get-bytevector-all))
  46. (define c-compiler
  47. (or (which "gcc") (which "cc") (which "g++")))
  48. (test-begin "gremlin")
  49. (unless %guile-executable (test-skip 1))
  50. (test-assert "elf-dynamic-info-needed, executable"
  51. (let* ((elf (call-with-input-file %guile-executable read-elf))
  52. (dyninfo (elf-dynamic-info elf)))
  53. (or (not dyninfo) ;static executable
  54. (lset<= string=?
  55. (list (string-append "libguile-" (effective-version))
  56. "libc")
  57. (map (lambda (lib)
  58. (string-take lib (string-contains lib ".so")))
  59. (elf-dynamic-info-needed dyninfo))))))
  60. (unless (and %guile-executable (not (getenv "LD_LIBRARY_PATH"))
  61. (file-needed %guile-executable) ;statically linked?
  62. ;; When Guix has been built on a foreign distro, using a
  63. ;; toolchain and libraries from that foreign distro, it is not
  64. ;; unusual for the runpath to be empty.
  65. (pair? (file-runpath %guile-executable)))
  66. (test-skip 1))
  67. (test-assert "file-needed/recursive"
  68. (let* ((needed (file-needed/recursive %guile-executable))
  69. (pipe (dynamic-wind
  70. (lambda ()
  71. ;; Tell ld.so to list loaded objects, like 'ldd' does.
  72. (setenv "LD_TRACE_LOADED_OBJECTS" "yup"))
  73. (lambda ()
  74. (open-pipe* OPEN_READ %guile-executable))
  75. (lambda ()
  76. (unsetenv "LD_TRACE_LOADED_OBJECTS")))))
  77. (define ldd-rx
  78. (make-regexp "^[[:blank:]]+([[:graph:]]+ => )?([[:graph:]]+) .*$"))
  79. (define (read-ldd-output port)
  80. ;; Read from PORT output in GNU ldd format.
  81. (let loop ((result '()))
  82. (match (read-line port)
  83. ((? eof-object?)
  84. (reverse result))
  85. ((= (cut regexp-exec ldd-rx <>) m)
  86. (if m
  87. (loop (cons (match:substring m 2) result))
  88. (loop result))))))
  89. (define ground-truth
  90. (remove (lambda (entry)
  91. ;; See vdso(7) for the list of vDSO names across
  92. ;; architectures.
  93. (or (string-prefix? "linux-vdso.so" entry)
  94. (string-prefix? "linux-vdso32.so" entry) ;32-bit powerpc
  95. (string-prefix? "linux-vdso64.so" entry) ;64-bit powerpc
  96. (string-prefix? "linux-gate.so" entry) ;i386
  97. ;; FIXME: ELF files on aarch64 do not always include a
  98. ;; NEEDED entry for the dynamic linker, and it is unclear
  99. ;; if that is OK. See: https://issues.guix.gnu.org/52943
  100. (and (target-aarch64?)
  101. (string-contains entry (glibc-dynamic-linker)))))
  102. (read-ldd-output pipe)))
  103. (and (zero? (close-pipe pipe))
  104. ;; It's OK if file-needed/recursive returns multiple entries that are
  105. ;; different strings referring to the same file. This appears to be a
  106. ;; benign edge case. See: https://issues.guix.gnu.org/52940
  107. (lset= file=? (pk 'truth ground-truth) (pk 'needed needed)))))
  108. (test-equal "expand-origin"
  109. '("OOO/../lib"
  110. "OOO"
  111. "../OOO/bar/OOO/baz"
  112. "ORIGIN/foo")
  113. (map (cut expand-origin <> "OOO")
  114. '("$ORIGIN/../lib"
  115. "${ORIGIN}"
  116. "../${ORIGIN}/bar/$ORIGIN/baz"
  117. "ORIGIN/foo")))
  118. (unless c-compiler
  119. (test-skip 1))
  120. (test-equal "strip-runpath"
  121. "hello\n"
  122. (call-with-temporary-directory
  123. (lambda (directory)
  124. (with-directory-excursion directory
  125. (call-with-output-file "t.c"
  126. (lambda (port)
  127. (display "int main () { puts(\"hello\"); }" port)))
  128. (invoke c-compiler "t.c"
  129. "-Wl,--enable-new-dtags" "-Wl,-rpath=/foo" "-Wl,-rpath=/bar")
  130. (let* ((dyninfo (elf-dynamic-info
  131. (parse-elf (call-with-input-file "a.out"
  132. get-bytevector-all))))
  133. (old (elf-dynamic-info-runpath dyninfo))
  134. (new (strip-runpath "a.out"))
  135. (new* (strip-runpath "a.out")))
  136. (validate-needed-in-runpath "a.out")
  137. (and (member "/foo" old) (member "/bar" old)
  138. (not (member "/foo" new))
  139. (not (member "/bar" new))
  140. (equal? new* new)
  141. (let* ((pipe (open-input-pipe "./a.out"))
  142. (str (get-string-all pipe)))
  143. (close-pipe pipe)
  144. str)))))))
  145. (unless c-compiler
  146. (test-skip 1))
  147. (test-equal "set-file-runpath + file-runpath"
  148. "hello\n"
  149. (call-with-temporary-directory
  150. (lambda (directory)
  151. (with-directory-excursion directory
  152. (call-with-output-file "t.c"
  153. (lambda (port)
  154. (display "int main () { puts(\"hello\"); }" port)))
  155. (invoke c-compiler "t.c"
  156. "-Wl,--enable-new-dtags" "-Wl,-rpath=/xxxxxxxxx")
  157. (let ((original-runpath (file-runpath "a.out")))
  158. (and (member "/xxxxxxxxx" original-runpath)
  159. (guard (c ((runpath-too-long-error? c)
  160. (string=? "a.out" (runpath-too-long-error-file c))))
  161. (set-file-runpath "a.out" (list (make-string 777 #\y))))
  162. (let ((runpath (delete "/xxxxxxxxx" original-runpath)))
  163. (set-file-runpath "a.out" runpath)
  164. (equal? runpath (file-runpath "a.out")))
  165. (let* ((pipe (open-input-pipe "./a.out"))
  166. (str (get-string-all pipe)))
  167. (close-pipe pipe)
  168. str)))))))
  169. (unless c-compiler
  170. (test-skip 1))
  171. (test-equal "elf-dynamic-info-soname"
  172. "libfoo.so.2"
  173. (call-with-temporary-directory
  174. (lambda (directory)
  175. (with-directory-excursion directory
  176. (call-with-output-file "t.c"
  177. (lambda (port)
  178. (display "// empty file" port)))
  179. (invoke c-compiler "t.c"
  180. "-shared" "-Wl,-soname,libfoo.so.2")
  181. (let* ((dyninfo (elf-dynamic-info
  182. (parse-elf (call-with-input-file "a.out"
  183. get-bytevector-all))))
  184. (soname (elf-dynamic-info-soname dyninfo)))
  185. soname)))))
  186. (test-end "gremlin")