Signal floating-point-overflow from bignum-to-float.
[sbcl.git] / tests / alien.impure.lisp
blob613fcc67422b56f21e9db5fbb28839ee968be3fb
1 ;;;; This file is for compiler tests which have side effects (e.g.
2 ;;;; executing DEFUN) but which don't need any special side-effecting
3 ;;;; environmental stuff (e.g. DECLAIM of particular optimization
4 ;;;; settings). Similar tests which *do* expect special settings may
5 ;;;; be in files compiler-1.impure.lisp, compiler-2.impure.lisp, etc.
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; While most of SBCL is derived from the CMU CL system, the test
11 ;;;; files (like this one) were written from scratch after the fork
12 ;;;; from CMU CL.
13 ;;;;
14 ;;;; This software is in the public domain and is provided with
15 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
16 ;;;; more information.
18 (cl:in-package :cl-user)
20 ;;; In sbcl-0.6.10, Douglas Brebner reported that (SETF EXTERN-ALIEN)
21 ;;; was messed up so badly that trying to execute expressions like
22 ;;; this signalled an error.
23 (with-test (:name (setf extern-alien))
24 (setf (sb-alien:extern-alien "thread_control_stack_size" sb-alien:unsigned)
25 (sb-alien:extern-alien "thread_control_stack_size" sb-alien:unsigned)))
27 ;;; bug 133, fixed in 0.7.0.5: Somewhere in 0.pre7.*, C void returns
28 ;;; were broken ("unable to use values types here") when
29 ;;; auto-PROCLAIM-of-return-value was added to DEFINE-ALIEN-ROUTINE.
30 (with-test (:name (define-alien-routine :bug-133))
31 (sb-alien:define-alien-routine ("free" free) void (ptr (* t) :in)))
33 ;;; Types of alien functions were being incorrectly DECLAIMED when
34 ;;; docstrings were included in the definition until sbcl-0.7.6.15.
35 (sb-alien:define-alien-routine ("getenv" ftype-correctness) c-string
36 "docstring"
37 (name c-string))
39 (with-test (:name (define-alien-routine ftype :correctness))
40 (multiple-value-bind (function failure-p warnings)
41 (checked-compile '(lambda () (ftype-correctness))
42 :allow-warnings t)
43 (declare (ignore function failure-p))
44 (assert (= (length warnings) 1)))
46 (checked-compile '(lambda () (ftype-correctness "FOO")))
48 (multiple-value-bind (function failure-p warnings)
49 (checked-compile '(lambda () (ftype-correctness "FOO" "BAR"))
50 :allow-warnings t)
51 (declare (ignore function failure-p))
52 (assert (= (length warnings) 1))))
54 ;;; This used to break due to too eager auxiliary type twiddling in
55 ;;; parse-alien-record-type.
56 (defparameter *maybe* nil)
57 (with-test (:name (with-alien struct :and alien-funcall))
58 (checked-compile
59 '(lambda ()
60 (with-alien ((x (struct bar (x unsigned) (y unsigned)))
61 ;; bogus definition, but we just need the symbol
62 (f (function int (* (struct bar))) :extern "printf"))
63 (when *maybe*
64 (alien-funcall f (addr x)))))))
66 ;;; Mutually referent structures
67 (define-alien-type struct.1 (struct struct.1 (x (* (struct struct.2))) (y int)))
68 (define-alien-type struct.2 (struct struct.2 (x (* (struct struct.1))) (y int)))
69 (with-test (:name (define-alien-type :mutually-reference-structures))
70 (let ((s1 (make-alien struct.1))
71 (s2 (make-alien struct.2)))
72 (setf (slot s1 'x) s2
73 (slot s2 'x) s1
74 (slot (slot s1 'x) 'y) 1
75 (slot (slot s2 'x) 'y) 2)
76 (assert (= 1 (slot (slot s1 'x) 'y)))
77 (assert (= 2 (slot (slot s2 'x) 'y)))))
79 ;;; "Alien bug" on sbcl-devel 2004-10-11 by Thomas F. Burdick caused
80 ;;; by recursive struct definition.
81 (with-test (:name (compile-file :nested define-alien-type struct))
82 (let ((fname "alien-bug-2004-10-11.tmp.lisp"))
83 (unwind-protect
84 (progn
85 (with-open-file (f fname :direction :output :if-exists :supersede)
86 (mapc (lambda (form) (print form f))
87 '((defpackage :alien-bug
88 (:use :cl :sb-alien))
89 (in-package :alien-bug)
90 (define-alien-type objc-class
91 (struct objc-class
92 (protocols
93 (* (struct protocol-list
94 (list (array (* (struct objc-class))))))))))))
95 (load fname)
96 (load fname)
97 (load (compile-file fname))
98 (load (compile-file fname)))
99 (delete-file (compile-file-pathname fname))
100 (delete-file fname))))
102 ;;; enumerations with only one enum resulted in division-by-zero
103 ;;; reported on sbcl-help 2004-11-16 by John Morrison
104 (define-alien-type enum.1 (enum nil (:val0 0)))
106 (define-alien-type enum.2 (enum nil (zero 0) (one 1) (two 2) (three 3)
107 (four 4) (five 5) (six 6) (seven 7)
108 (eight 8) (nine 9)))
109 (with-test (:name (define-alien-type enum array cast))
110 (with-alien ((integer-array (array int 3)))
111 (let ((enum-array (cast integer-array (array enum.2 3))))
112 (setf (deref enum-array 0) 'three
113 (deref enum-array 1) 'four)
114 (setf (deref integer-array 2) (+ (deref integer-array 0)
115 (deref integer-array 1)))
116 (assert (eql (deref enum-array 2) 'seven)))))
118 ;; The code that is used for mapping from integers to symbols depends
119 ;; on the `density' of the set of used integers, so test with a sparse
120 ;; set as well.
121 (define-alien-type enum.3 (enum nil (zero 0) (one 1) (k-one 1001) (k-two 1002)))
122 (with-test (:name (define-alien-type enum :sparse-integers))
123 (with-alien ((integer-array (array int 3)))
124 (let ((enum-array (cast integer-array (array enum.3 3))))
125 (setf (deref enum-array 0) 'one
126 (deref enum-array 1) 'k-one)
127 (setf (deref integer-array 2) (+ (deref integer-array 0)
128 (deref integer-array 1)))
129 (assert (eql (deref enum-array 2) 'k-two)))))
131 ;; enums used to allow values to be used only once
132 ;; C enums allow for multiple tags to point to the same value
133 (define-alien-type enum.4
134 (enum nil (:key1 1) (:key2 2) (:keytwo 2)))
135 (with-test (:name (define-alien-type enum :repeated-integers))
136 (with-alien ((enum-array (array enum.4 3)))
137 (setf (deref enum-array 0) :key1)
138 (setf (deref enum-array 1) :key2)
139 (setf (deref enum-array 2) :keytwo)
140 (assert (and (eql (deref enum-array 1) (deref enum-array 2))
141 (eql (deref enum-array 1) :key2)))))
143 ;;; As reported by Baughn on #lisp, ALIEN-FUNCALL loops forever when
144 ;;; compiled with (DEBUG 3).
145 (with-test (:name (alien-funcall debug 3))
146 (sb-kernel::values-specifier-type-cache-clear)
147 (checked-compile-and-assert ()
148 '(lambda (v)
149 (sb-alien:alien-funcall (sb-alien:extern-alien "getenv"
150 (function (c-string) c-string))
152 (("HOME") '(or string null) :test (lambda (values expected)
153 (every #'typep values expected)))))
155 ;;; CLH: Test for non-standard alignment in alien structs
156 (sb-alien:define-alien-type align-test-struct
157 (sb-alien:union align-test-union
158 (s (sb-alien:struct nil
159 (s1 sb-alien:unsigned-char)
160 (c1 sb-alien:unsigned-char :alignment 16)
161 (c2 sb-alien:unsigned-char :alignment 32)
162 (c3 sb-alien:unsigned-char :alignment 32)
163 (c4 sb-alien:unsigned-char :alignment 8)))
164 (u (sb-alien:array sb-alien:unsigned-char 16))))
165 (with-test (:name (define-alien-type struct :alignment))
166 (let ((a1 (sb-alien:make-alien align-test-struct)))
167 (declare (type (sb-alien:alien (* align-test-struct)) a1))
168 (setf (sb-alien:slot (sb-alien:slot a1 's) 's1) 1)
169 (setf (sb-alien:slot (sb-alien:slot a1 's) 'c1) 21)
170 (setf (sb-alien:slot (sb-alien:slot a1 's) 'c2) 41)
171 (setf (sb-alien:slot (sb-alien:slot a1 's) 'c3) 61)
172 (setf (sb-alien:slot (sb-alien:slot a1 's) 'c4) 81)
173 (assert (equal '(1 21 41 61 81)
174 (list (sb-alien:deref (sb-alien:slot a1 'u) 0)
175 (sb-alien:deref (sb-alien:slot a1 'u) 2)
176 (sb-alien:deref (sb-alien:slot a1 'u) 4)
177 (sb-alien:deref (sb-alien:slot a1 'u) 8)
178 (sb-alien:deref (sb-alien:slot a1 'u) 9))))))
180 (with-test (:name (make-alien :no-note))
181 (checked-compile-and-assert (:allow-notes nil)
182 '(lambda () (sb-alien:make-alien sb-alien:int))
183 (() 'sb-alien:alien :test #'typep)))
185 ;;; Test case for unwinding an alien (Win32) exception frame
187 ;;; The basic theory here is that failing to honor a win32
188 ;;; exception frame during stack unwinding breaks the chain.
189 ;;; "And if / You don't love me now / You will never love me
190 ;;; again / I can still hear you saying / You would never break
191 ;;; the chain." If the chain is broken and another exception
192 ;;; occurs (such as an error trap caused by an OBJECT-NOT-TYPE
193 ;;; error), the system will kill our process. No mercy, no
194 ;;; appeal. So, to check that we have done our job properly, we
195 ;;; need some way to put an exception frame on the stack and then
196 ;;; unwind through it, then trigger another exception. (FUNCALL
197 ;;; 0) will suffice for the latter, and a simple test shows that
198 ;;; CallWindowProc() establishes a frame and calls a function
199 ;;; passed to it as an argument.
200 #+win32
201 (progn
202 (load-shared-object "USER32")
203 (assert
204 (eq :ok
205 (handler-case
206 (tagbody
207 (alien-funcall
208 (extern-alien "CallWindowProcW"
209 (function unsigned-int
210 (* (function int)) unsigned-int
211 unsigned-int unsigned-int unsigned-int))
212 (alien-sap
213 (sb-alien::alien-callback (function unsigned-int)
214 #'(lambda () (go up))))
215 0 0 0 0)
217 (funcall 0))
218 (error ()
219 :ok)))))
221 ;;; Unused local alien caused a compiler error
222 (with-test (:name (sb-alien:with-alien :unused :no error))
223 (checked-compile-and-assert ()
224 `(lambda ()
225 (sb-alien:with-alien ((alien1923 (array (sb-alien:unsigned 8) 72)))
226 (values)))
227 (() (values))))
229 ;;; Non-local exit from WITH-ALIEN caused alien stack to be leaked.
230 (defvar *sap-int*)
231 (defun try-to-leak-alien-stack (x)
232 (with-alien ((alien (array (sb-alien:unsigned 8) 72)))
233 (let ((sap-int (sb-sys:sap-int (alien-sap alien))))
234 (if *sap-int*
235 (assert (= *sap-int* sap-int))
236 (setf *sap-int* sap-int)))
237 (when x
238 (return-from try-to-leak-alien-stack 'going))
239 (locally (declare (muffle-conditions style-warning))
240 (never))))
242 ;;; Can't stack allocate aliens with an interpreter
243 (compile 'try-to-leak-alien-stack)
245 (with-test (:name :nlx-causes-alien-stack-leak)
246 (let ((*sap-int* nil))
247 (loop repeat 1024
248 do (try-to-leak-alien-stack t))))
250 (with-test (:name (define-alien-type struct :redefinition :bug-431)
251 :fails-on :interpreter)
252 (eval '(progn
253 (define-alien-type nil (struct mystruct (myshort short) (mychar char)))
254 (with-alien ((myst (struct mystruct)))
255 (with-alien ((mysh short (slot myst 'myshort)))
256 (assert (integerp mysh))))))
257 (let ((restarted 0))
258 (handler-bind ((error (lambda (e)
259 (let ((cont (find-restart 'continue e)))
260 (when cont
261 (incf restarted)
262 (invoke-restart cont))))))
263 (eval '(define-alien-type nil (struct mystruct (myint int) (mychar char)))))
264 (assert (= 1 restarted)))
265 (eval '(with-alien ((myst (struct mystruct)))
266 (with-alien ((myin int (slot myst 'myint)))
267 (assert (integerp myin))))))
269 ;;; void conflicted with derived type
270 (declaim (inline bug-316075))
271 ;; KLUDGE: This win32 reader conditional masks a bug, but allows the
272 ;; test to fail cleanly. The linkage-table reader conditional
273 ;; accomodates the little fact that the function doesn't exist, and
274 ;; non-linkage-table systems resolve such things immediately and
275 ;; signal errors.
276 #-(or win32 (not linkage-table))
277 (locally (declare (muffle-conditions style-warning))
278 (sb-alien:define-alien-routine bug-316075 void (result char :out)))
279 (with-test (:name :bug-316075 :fails-on :win32
280 :broken-on (not :linkage-table))
281 #+win32 (error "fail")
282 #-linkage-table (error "unable to set up test precondition")
283 ;; The interpreter gives you a style-warning because the "undefined alien"
284 ;; first occurs here during compilation of the test case. But if compiling
285 ;; by default, then the warning already happened above at DEFINE-ALIEN-ROUTINE
286 ;; because when that got compiled, it warned, which inhibited further
287 ;; warnings for the same foreign symbol.
288 (checked-compile '(lambda () (multiple-value-list (bug-316075)))
289 :allow-style-warnings t))
291 ;;; Bug #316325: "return values of alien calls assumed truncated to
292 ;;; correct width on x86"
293 #+x86-64
294 (sb-alien::define-alien-callback truncation-test (unsigned 64)
295 ((foo (unsigned 64)))
296 foo)
297 #+x86
298 (sb-alien::define-alien-callback truncation-test (unsigned 32)
299 ((foo (unsigned 32)))
300 foo)
302 (with-test (:name :bug-316325 :skipped-on (not (or :x86-64 :x86))
303 :fails-on :interpreter)
304 ;; This test works by defining a callback function that provides an
305 ;; identity transform over a full-width machine word, then calling
306 ;; it as if it returned a narrower type and checking to see if any
307 ;; noise in the high bits of the result are properly ignored.
308 (macrolet ((verify (type input output)
309 `(with-alien ((fun (* (function ,type
310 #+x86-64 (unsigned 64)
311 #+x86 (unsigned 32)))
312 :local (alien-sap truncation-test)))
313 (let ((result (alien-funcall fun ,input)))
314 (assert (= result ,output))))))
315 #+x86-64
316 (progn
317 (verify (unsigned 64) #x8000000000000000 #x8000000000000000)
318 (verify (signed 64) #x8000000000000000 #x-8000000000000000)
319 (verify (signed 64) #x7fffffffffffffff #x7fffffffffffffff)
320 (verify (unsigned 32) #x0000000180000042 #x80000042)
321 (verify (signed 32) #x0000000180000042 #x-7fffffbe)
322 (verify (signed 32) #xffffffff7fffffff #x7fffffff))
323 #+x86
324 (progn
325 (verify (unsigned 32) #x80000042 #x80000042)
326 (verify (signed 32) #x80000042 #x-7fffffbe)
327 (verify (signed 32) #x7fffffff #x7fffffff))
328 (verify (unsigned 16) #x00018042 #x8042)
329 (verify (signed 16) #x003f8042 #x-7fbe)
330 (verify (signed 16) #x003f7042 #x7042)))
332 (with-test (:name :bug-654485)
333 ;; DEBUG 2 used to prevent let-conversion of the open-coded ALIEN-FUNCALL body,
334 ;; which in turn led the dreaded %SAP-ALIEN note.
335 (checked-compile
336 `(lambda (program argv)
337 (declare (optimize (debug 2)))
338 (with-alien ((sys-execv1 (function int c-string (* c-string)) :extern
339 "exit"))
340 (values (alien-funcall sys-execv1 program argv))))
341 :allow-notes nil))
343 (with-test (:name :bug-721087 :fails-on :win32)
344 (assert (typep nil '(alien c-string)))
345 (assert (not (typep nil '(alien (c-string :not-null t)))))
346 (assert (eq :ok
347 (handler-case
348 (posix-getenv nil)
349 (type-error (e)
350 (when (and (null (type-error-datum e))
351 (equal (type-error-expected-type e)
352 '(alien (c-string :not-null t))))
353 :ok))))))
355 (with-test (:name :make-alien-string)
356 (labels ((content (alien length null-terminate)
357 (if null-terminate
358 (cast alien c-string)
359 (let ((buffer (make-array length
360 :element-type '(unsigned-byte 8))))
361 (sb-kernel:copy-ub8-from-system-area
362 (alien-sap alien) 0 buffer 0 length)
363 (sb-ext:octets-to-string buffer))))
364 (test (null-terminate)
365 (let* ((string "This comes from lisp!")
366 (length (length string)))
367 (multiple-value-bind (alien alien-length)
368 (sb-alien::make-alien-string
369 string :null-terminate null-terminate)
370 (assert (= alien-length (+ length (if null-terminate 1 0))))
371 (gc :full t)
372 ;; Copy to make sure STRING did not somehow end up in
373 ;; the alien object.
374 (assert (equal (copy-seq string)
375 (content alien length null-terminate)))
376 (free-alien alien)))))
377 (test nil)
378 (test t)))
380 (with-test (:name :malloc-failure
381 :fails-on :alpha) ;; Alpha has address space to burn
382 (assert (eq :enomem
383 (handler-case
384 (loop repeat 128
385 collect (sb-alien:make-alien char (1- array-total-size-limit)))
386 (storage-condition ()
387 :enomem)))))
389 (with-test (:name :bug-985505)
390 ;; Check that correct octets are reported for a c-string-decoding error.
391 (assert
392 (eq :unibyte
393 (handler-case
394 (let ((c-string (coerce #(70 111 195 182 0)
395 '(vector (unsigned-byte 8)))))
396 (sb-sys:with-pinned-objects (c-string)
397 (sb-alien::c-string-to-string (sb-sys:vector-sap c-string)
398 :ascii 'character)))
399 (sb-int:c-string-decoding-error (e)
400 (assert (equalp #(195) (sb-int:character-decoding-error-octets e)))
401 :unibyte))))
402 (assert
403 (eq :multibyte-4
404 (handler-case
405 ;; KLUDGE, sort of.
407 ;; C-STRING decoding doesn't know how long the string is, and since this
408 ;; looks like a 4-byte sequence, it will grab 4 octets off the end.
410 ;; So we pad the vector for safety's sake.
411 (let ((c-string (coerce #(70 111 246 0 0 0)
412 '(vector (unsigned-byte 8)))))
413 (sb-sys:with-pinned-objects (c-string)
414 (sb-alien::c-string-to-string (sb-sys:vector-sap c-string)
415 :utf-8 'character)))
416 (sb-int:c-string-decoding-error (e)
417 (assert (equalp #(246 0 0 0)
418 (sb-int:character-decoding-error-octets e)))
419 :multibyte-4))))
420 (assert
421 (eq :multibyte-2
422 (handler-case
423 (let ((c-string (coerce #(70 195 1 182 195 182 0) '(vector (unsigned-byte 8)))))
424 (sb-sys:with-pinned-objects (c-string)
425 (sb-alien::c-string-to-string (sb-sys:vector-sap c-string)
426 :utf-8 'character)))
427 (sb-int:c-string-decoding-error (e)
428 (assert (equalp #(195 1)
429 (sb-int:character-decoding-error-octets e)))
430 :multibyte-2)))))
432 (with-test (:name :stream-to-c-string-decoding-restart-leakage)
433 ;; Restarts for stream decoding errors didn't use to be associated with
434 ;; their conditions, so they could get confused with c-string decoding errors.
435 (assert (eq :nesting-ok
436 (catch 'out
437 (handler-bind ((sb-int:character-decoding-error
438 (lambda (stream-condition)
439 (declare (ignore stream-condition))
440 (handler-bind ((sb-int:character-decoding-error
441 (lambda (c-string-condition)
442 (throw 'out
443 (if (find-restart
444 'sb-impl::input-replacement
445 c-string-condition)
446 :bad-restart
447 :nesting-ok)))))
448 (let ((c-string (coerce #(70 195 1 182 195 182 0)
449 '(vector (unsigned-byte 8)))))
450 (sb-sys:with-pinned-objects (c-string)
451 (sb-alien::c-string-to-string
452 (sb-sys:vector-sap c-string)
453 :utf-8 'character)))))))
454 (let ((namestring "alien.impure.tmp"))
455 (unwind-protect
456 (progn
457 (with-open-file (f namestring
458 :element-type '(unsigned-byte 8)
459 :direction :output
460 :if-exists :supersede)
461 (dolist (b '(70 195 1 182 195 182 0))
462 (write-byte b f)))
463 (with-open-file (f namestring
464 :external-format :utf-8)
465 (read-line f)))
466 (delete-file namestring))))))))
468 ;; Previously, the error signaled by (MACROEXPANDing the) redefinition
469 ;; of an alien structure itself signaled an error. Ensure that the
470 ;; error is signaled and prints properly.
471 (sb-alien:define-alien-type nil
472 (sb-alien:struct alien-structure-redefinition (bar sb-alien:int)))
474 (with-test (:name (:alien-structure-redefinition :condition-printable))
475 (handler-case
476 (macroexpand
477 '(sb-alien:define-alien-type nil
478 (sb-alien:struct alien-structure-redefinition (bar sb-alien:c-string))))
479 (error (condition)
480 (princ-to-string condition))
481 (:no-error (&rest values)
482 (declare (ignore values))
483 (error "~@<Alien structure type redefinition failed to signal an ~
484 error~@:>"))))
486 #+largefile
487 (with-test (:name (:64-bit-return-truncation))
488 (with-open-file (stream *load-truename*)
489 (file-position stream 4294967310)
490 (assert (= 4294967310 (file-position stream)))))
492 (with-test (:name :stack-misalignment)
493 (locally (declare (optimize (debug 2)))
494 (labels ((foo ()
495 (declare (optimize speed))
496 (sb-ext:get-time-of-day)))
497 (assert (equal (multiple-value-list
498 (multiple-value-prog1
499 (apply #'values (list 1))
500 (foo)))
501 '(1))))))
503 ;; Parse (ENUM COLOR)
504 (sb-alien-internals:parse-alien-type '(enum color red blue black green) nil)
505 ;; Now reparse it as a different type
506 (with-test (:name :change-enum-type)
507 (handler-bind ((error #'continue))
508 (sb-alien-internals:parse-alien-type '(enum color yellow ochre) nil)))
510 (with-test (:name :note-local-alien-type)
511 (let ((type (sb-alien::make-local-alien-info :type
512 (sb-alien-internals:parse-alien-type 'c-string nil))))
513 (checked-compile-and-assert ()
514 `(lambda (x)
515 (let ((alien (sb-alien-internals:make-local-alien ',type)))
516 (sb-alien-internals:note-local-alien-type ',type alien)
517 (flet ((x ()
518 (setf alien x)))
519 (x))
520 alien))
521 ((31) 31))))
523 (with-test (:name :memoize-coerce-to-interpreted-fun)
524 (let* ((form1 '(lambda (x) x))
525 (form2 (copy-tree form1)))
526 (assert (eq (sb-alien::coerce-to-interpreted-function form1)
527 (sb-alien::coerce-to-interpreted-function form2)))))
529 (with-test (:name :undefined-alien-name
530 :skipped-on (not (and :linkage-table
531 (or :x86-64 :arm :arm64))))
532 (handler-case (funcall (checked-compile `(lambda ()
533 (alien-funcall (extern-alien "bar" (function (values)))))
534 :allow-style-warnings t))
535 (t (c)
536 (assert (typep c 'sb-kernel::undefined-alien-function-error))
537 (assert (equal (cell-error-name c) "bar")))))