foreign.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. ;;;; foreign.test --- FFI. -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2010, 2011, 2012, 2013, 2017, 2021 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;
  19. ;;; See also ../standalone/test-ffi for FFI tests.
  20. ;;;
  21. (define-module (test-foreign)
  22. #:use-module (system foreign-library)
  23. #:use-module (system foreign)
  24. #:use-module (rnrs bytevectors)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-26)
  27. #:use-module (ice-9 format)
  28. #:use-module (test-suite lib))
  29. (with-test-prefix "foreign-library-pointer"
  30. (pass-if-exception
  31. "error message"
  32. ;; The error comes from dlsym, which is system-dependent.
  33. '(misc-error . "")
  34. (foreign-library-pointer #f "does_not_exist___")))
  35. (with-test-prefix "null pointer"
  36. (pass-if "pointer?"
  37. (pointer? %null-pointer))
  38. (pass-if "zero"
  39. (= 0 (pointer-address %null-pointer)))
  40. (pass-if "null pointer identity"
  41. (eq? %null-pointer (make-pointer 0)))
  42. (pass-if "null-pointer? %null-pointer"
  43. (null-pointer? %null-pointer))
  44. (pass-if-exception "dereference-pointer %null-pointer"
  45. exception:null-pointer-error
  46. (dereference-pointer %null-pointer))
  47. (pass-if-exception "pointer->bytevector %null-pointer"
  48. exception:null-pointer-error
  49. (pointer->bytevector %null-pointer 7)))
  50. (with-test-prefix "make-pointer"
  51. (pass-if "pointer?"
  52. (pointer? (make-pointer 123)))
  53. (pass-if "address preserved"
  54. (= 123 (pointer-address (make-pointer 123))))
  55. (pass-if "equal?"
  56. (equal? (make-pointer 123) (make-pointer 123)))
  57. (pass-if "equal? modulo finalizer"
  58. (let ((finalizer (false-if-exception
  59. (foreign-library-pointer #f "scm_is_pair"))))
  60. (if (not finalizer)
  61. (throw 'unresolved) ; Windows or a static build
  62. (equal? (make-pointer 123)
  63. (make-pointer 123 finalizer)))))
  64. (pass-if "equal? modulo finalizer (set-pointer-finalizer!)"
  65. (let ((finalizer (false-if-exception
  66. (foreign-library-pointer #f "scm_is_pair")))
  67. (ptr (make-pointer 123)))
  68. (if (not finalizer)
  69. (throw 'unresolved) ; Windows or a static build
  70. (begin
  71. (set-pointer-finalizer! ptr finalizer)
  72. (equal? (make-pointer 123) ptr)))))
  73. (pass-if "not equal?"
  74. (not (equal? (make-pointer 123) (make-pointer 456)))))
  75. (with-test-prefix "pointer<->scm"
  76. (pass-if "immediates"
  77. (equal? (pointer->scm (scm->pointer #\newline))
  78. #\newline))
  79. (pass-if "non-immediates"
  80. (equal? (pointer->scm (scm->pointer "Hello, world!"))
  81. "Hello, world!")))
  82. (define-wrapped-pointer-type foo
  83. foo?
  84. wrap-foo unwrap-foo
  85. (lambda (x p)
  86. (format p "#<foo! ~a>" (pointer-address (unwrap-foo x)))))
  87. (with-test-prefix "define-wrapped-pointer-type"
  88. (pass-if "foo?"
  89. (foo? (wrap-foo %null-pointer)))
  90. (pass-if "unwrap-foo"
  91. (let ((p (make-pointer 123)))
  92. (eq? p (unwrap-foo (wrap-foo p)))))
  93. (pass-if "identity"
  94. (let ((p1 (make-pointer 123))
  95. (p2 (make-pointer 123)))
  96. (eq? (wrap-foo p1)
  97. (wrap-foo p2))))
  98. (pass-if "printer"
  99. (string=? "#<foo! 123>"
  100. (with-output-to-string
  101. (lambda ()
  102. (write (wrap-foo (make-pointer 123))))))))
  103. (with-test-prefix "pointer<->bytevector"
  104. (pass-if "bijection"
  105. (let ((bv #vu8(0 1 2 3 4 5 6 7)))
  106. (equal? (pointer->bytevector (bytevector->pointer bv)
  107. (bytevector-length bv))
  108. bv)))
  109. (pass-if "pointer from bits"
  110. (let* ((bytes (iota (sizeof '*)))
  111. (bv (u8-list->bytevector bytes))
  112. (fold (case (native-endianness)
  113. ((little) fold-right)
  114. ((big) fold)
  115. (else (error "unsupported endianness")))))
  116. (= (pointer-address
  117. (make-pointer (bytevector-uint-ref bv 0 (native-endianness)
  118. (sizeof '*))))
  119. (fold (lambda (byte address)
  120. (+ byte (* 256 address)))
  121. 0
  122. bytes))))
  123. (pass-if "dereference-pointer"
  124. (let* ((bytes (iota (sizeof '*)))
  125. (bv (u8-list->bytevector bytes))
  126. (fold (case (native-endianness)
  127. ((little) fold-right)
  128. ((big) fold)
  129. (else (error "unsupported endianness")))))
  130. (= (pointer-address
  131. (dereference-pointer (bytevector->pointer bv)))
  132. (fold (lambda (byte address)
  133. (+ byte (* 256 address)))
  134. 0
  135. bytes)))))
  136. (with-test-prefix "pointer<->string"
  137. (pass-if-exception "%default-port-conversion-strategy is error"
  138. exception:encoding-error
  139. (let ((s "χαοσ"))
  140. (with-fluids ((%default-port-conversion-strategy 'error))
  141. (string->pointer s "ISO-8859-1"))))
  142. (pass-if "%default-port-conversion-strategy is escape"
  143. (let ((s "teĥniko"))
  144. (equal? (with-fluids ((%default-port-conversion-strategy 'escape))
  145. (pointer->string (string->pointer s "ISO-8859-1")))
  146. (format #f "te\\u~4,'0xniko"
  147. (char->integer #\ĥ)))))
  148. (pass-if "%default-port-conversion-strategy is substitute"
  149. (let ((s "teĥniko")
  150. (member (negate (negate member))))
  151. (member (with-fluids ((%default-port-conversion-strategy 'substitute))
  152. (pointer->string (string->pointer s "ISO-8859-1")))
  153. '("te?niko"
  154. ;; This form is found on FreeBSD 8.2 and Darwin 10.8.0.
  155. "te^hniko"))))
  156. (pass-if "bijection"
  157. (let ((s "hello, world"))
  158. (string=? s (pointer->string (string->pointer s)))))
  159. (pass-if "bijection [latin1]"
  160. (with-latin1-locale
  161. (let ((s "Szép jó napot!"))
  162. (string=? s (pointer->string (string->pointer s))))))
  163. (pass-if "bijection, utf-8"
  164. (let ((s "hello, world"))
  165. (string=? s (pointer->string (string->pointer s "utf-8")
  166. -1 "utf-8"))))
  167. (pass-if "bijection, utf-8 [latin1]"
  168. (let ((s "Szép jó napot!"))
  169. (string=? s (pointer->string (string->pointer s "utf-8")
  170. -1 "utf-8")))))
  171. (with-test-prefix "pointer->procedure"
  172. (pass-if-exception "object instead of pointer"
  173. exception:wrong-type-arg
  174. (let ((p (pointer->procedure '* %null-pointer '(*))))
  175. (p #t))))
  176. (with-test-prefix "procedure->pointer"
  177. (define qsort
  178. ;; Bindings for libc's `qsort' function.
  179. ;; On some platforms, such as MinGW, `qsort' is visible only if
  180. ;; linking with `-export-dynamic'. Just skip these tests when it's
  181. ;; not visible.
  182. (false-if-exception
  183. (foreign-library-function
  184. (cond
  185. ((string-contains %host-type "cygwin")
  186. ;; On Cygwin, load-foreign-library does not search recursively
  187. ;; into linked DLLs. Thus, one needs to link to the core C
  188. ;; library DLL explicitly.
  189. "cygwin1")
  190. (else #f))
  191. "qsort" #:arg-types (list '* size_t size_t '*))))
  192. (define (dereference-pointer-to-byte ptr)
  193. (let ((b (pointer->bytevector ptr 1)))
  194. (bytevector-u8-ref b 0)))
  195. (define input
  196. '(7 1 127 3 5 4 77 2 9 0))
  197. (pass-if "qsort"
  198. (if (and qsort (defined? 'procedure->pointer))
  199. (let* ((called? #f)
  200. (cmp (lambda (x y)
  201. (set! called? #t)
  202. (- (dereference-pointer-to-byte x)
  203. (dereference-pointer-to-byte y))))
  204. (ptr (procedure->pointer int cmp (list '* '*)))
  205. (bv (u8-list->bytevector input)))
  206. (qsort (bytevector->pointer bv) (bytevector-length bv) 1
  207. (procedure->pointer int cmp (list '* '*)))
  208. (and called?
  209. (equal? (bytevector->u8-list bv)
  210. (sort input <))))
  211. (throw 'unresolved)))
  212. (pass-if-exception "qsort, wrong return type"
  213. exception:wrong-type-arg
  214. (if (and qsort (defined? 'procedure->pointer))
  215. (let* ((cmp (lambda (x y) #f)) ; wrong return type
  216. (ptr (procedure->pointer int cmp (list '* '*)))
  217. (bv (u8-list->bytevector input)))
  218. (qsort (bytevector->pointer bv) (bytevector-length bv) 1
  219. (procedure->pointer int cmp (list '* '*)))
  220. #f)
  221. (throw 'unresolved)))
  222. (pass-if-exception "qsort, wrong arity"
  223. exception:wrong-num-args
  224. (if (and qsort (defined? 'procedure->pointer))
  225. (let* ((cmp (lambda (x y z) #f)) ; wrong arity
  226. (ptr (procedure->pointer int cmp (list '* '*)))
  227. (bv (u8-list->bytevector input)))
  228. (qsort (bytevector->pointer bv) (bytevector-length bv) 1
  229. (procedure->pointer int cmp (list '* '*)))
  230. #f)
  231. (throw 'unresolved)))
  232. (pass-if "bijection"
  233. (if (defined? 'procedure->pointer)
  234. (let* ((proc (lambda (x y z)
  235. (+ x y z 0.0)))
  236. (ret double)
  237. (args (list float int16 double))
  238. (proc* (pointer->procedure ret
  239. (procedure->pointer ret proc args)
  240. args))
  241. (arg1 (map (cut / <> 2.0) (iota 123)))
  242. (arg2 (iota 123 32000))
  243. (arg3 (map (cut / <> 4.0) (iota 123 100 4))))
  244. (equal? (map proc arg1 arg2 arg3)
  245. (map proc* arg1 arg2 arg3)))
  246. (throw 'unresolved)))
  247. (pass-if "procedures returning a pointer"
  248. (if (defined? 'procedure->pointer)
  249. (let* ((called? #f)
  250. (proc (lambda (i) (set! called? #t) (make-pointer i)))
  251. (pointer (procedure->pointer '* proc (list int)))
  252. (proc* (pointer->procedure '* pointer (list int)))
  253. (result (proc* 777)))
  254. (and called? (equal? result (make-pointer 777))))
  255. (throw 'unresolved)))
  256. (pass-if "procedures returning void"
  257. (if (defined? 'procedure->pointer)
  258. (let* ((called? #f)
  259. (proc (lambda () (set! called? #t)))
  260. (pointer (procedure->pointer void proc '()))
  261. (proc* (pointer->procedure void pointer '())))
  262. (proc*)
  263. called?)
  264. (throw 'unresolved)))
  265. (pass-if "procedure is retained"
  266. ;; The lambda passed to `procedure->pointer' must remain live.
  267. (if (defined? 'procedure->pointer)
  268. (let* ((ptr (procedure->pointer int
  269. (lambda (x) (+ x 7))
  270. (list int)))
  271. (procs (unfold (cut >= <> 10000)
  272. (lambda (i)
  273. (pointer->procedure int ptr (list int)))
  274. 1+
  275. 0)))
  276. (gc) (gc) (gc)
  277. (every (cut = <> 9)
  278. (map (lambda (f) (f 2)) procs)))
  279. (throw 'unresolved)))
  280. (pass-if "arity"
  281. (if (and qsort (defined? 'procedure->pointer))
  282. (equal? '(4 0 #f) (procedure-minimum-arity qsort))
  283. (throw 'unresolved))))
  284. (with-test-prefix "structs"
  285. (pass-if "sizeof { int8, double }"
  286. (= (sizeof (list int8 double))
  287. (+ (alignof double) (sizeof double))))
  288. (pass-if "sizeof { double, int8 }"
  289. (= (sizeof (list double int8))
  290. (+ (alignof double) (sizeof double))))
  291. (pass-if "sizeof { short, int, long, pointer }"
  292. (let ((layout (list short int long '*)))
  293. (>= (sizeof layout)
  294. (reduce + 0.0 (map sizeof layout)))))
  295. (pass-if "alignof { int8, double, int8 }"
  296. ;; alignment of the most strictly aligned component
  297. (let ((layout (list int8 double int8)))
  298. (= (alignof layout) (alignof double))))
  299. (pass-if "parse-c-struct"
  300. (let ((layout (list int64 uint8))
  301. (data (list -300 43)))
  302. (equal? (parse-c-struct (make-c-struct layout data)
  303. layout)
  304. data)))
  305. (pass-if "alignment constraints honored"
  306. (let ((layout (list int8 double))
  307. (data (list -7 3.14)))
  308. (equal? (parse-c-struct (make-c-struct layout data)
  309. layout)
  310. data)))
  311. (pass-if "int8, pointer"
  312. (let ((layout (list uint8 '*))
  313. (data (list 222 (make-pointer 7777))))
  314. (equal? (parse-c-struct (make-c-struct layout data)
  315. layout)
  316. data)))
  317. (pass-if "unsigned-long, int8, size_t"
  318. (let ((layout (list unsigned-long int8 size_t))
  319. (data (list (expt 2 17) -128 (expt 2 18))))
  320. (equal? (parse-c-struct (make-c-struct layout data)
  321. layout)
  322. data)))
  323. (pass-if "long, int, pointer"
  324. (let ((layout (list long int '*))
  325. (data (list (- (expt 2 17)) -222 (make-pointer 777))))
  326. (equal? (parse-c-struct (make-c-struct layout data)
  327. layout)
  328. data)))
  329. (pass-if "int8, pointer, short, double"
  330. (let ((layout (list int8 '* short double))
  331. (data (list 77 %null-pointer -42 3.14)))
  332. (equal? (parse-c-struct (make-c-struct layout data)
  333. layout)
  334. data)))
  335. (pass-if "int8, { int8, double, int8 }, int16"
  336. (let ((layout (list int8 (list int8 double int8) int16))
  337. (data (list 77 (list 42 4.2 55) 88)))
  338. (equal? (parse-c-struct (make-c-struct layout data)
  339. layout)
  340. data))))
  341. (with-test-prefix "lib->cyg"
  342. (pass-if "name is #f"
  343. (equal? #f (lib->cyg #f)))
  344. (pass-if "name too short"
  345. (string=? (lib->cyg "a") "a"))
  346. (pass-if "name starts with 'lib'"
  347. (string=? (lib->cyg "libfoo.dll") "cygfoo.dll"))
  348. (pass-if "name contains 'lib'"
  349. (string=? (lib->cyg "foolib.dll") "foolib.dll"))
  350. (pass-if "name doesn't contain 'lib'"
  351. (string=? (lib->cyg "foobar.dll") "foobar.dll"))
  352. (pass-if "name in path too short"
  353. (string=? (lib->cyg "/lib/a") "/lib/a"))
  354. (pass-if "name in path starts with 'lib'"
  355. (string=? (lib->cyg "/lib/libfoo.dll") "/lib/cygfoo.dll"))
  356. (pass-if "name in path contains 'lib'"
  357. (string=? (lib->cyg "/lib/foolib.dll") "/lib/foolib.dll"))
  358. (pass-if "name in path doesn't contain 'lib'"
  359. (string=? (lib->cyg "/lib/foobar.dll") "/lib/foobar.dll"))
  360. (pass-if "name in windows path starts with 'lib'"
  361. (string=? (lib->cyg "c:\\lib\\libfoo.dll") "c:\\lib\\cygfoo.dll"))
  362. (pass-if "name in windows path doesn't contain 'lib'"
  363. (string=? (lib->cyg "c:\\lib\\foobar.dll") "c:\\lib\\foobar.dll")))