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