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
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
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
39 (with-test (:name
(define-alien-routine ftype
:correctness
))
40 (multiple-value-bind (function failure-p warnings
)
41 (checked-compile '(lambda () (ftype-correctness))
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"))
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
))
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"))
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)))
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 (scratch-file-name "lisp")))
85 (with-open-file (f fname
:direction
:output
:if-exists
:supersede
)
86 (mapc (lambda (form) (print form f
))
87 '((defpackage :alien-bug
89 (in-package :alien-bug
)
90 (define-alien-type objc-class
93 (* (struct protocol-list
94 (list (array (* (struct objc-class
))))))))))))
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)
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
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 ()
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.
202 (load-shared-object "USER32")
208 (extern-alien "CallWindowProcW"
209 (function unsigned-int
210 (* (function int
)) unsigned-int
211 unsigned-int unsigned-int unsigned-int
))
213 (sb-alien::alien-callback
(function unsigned-int
)
214 #'(lambda () (go up
))))
221 ;;; Unused local alien caused a compiler error
222 (with-test (:name
(sb-alien:with-alien
:unused
:no error
))
223 (checked-compile-and-assert ()
225 (sb-alien:with-alien
((alien1923 (array (sb-alien:unsigned
8) 72)))
229 ;;; Non-local exit from WITH-ALIEN caused alien stack to be leaked.
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
))))
235 (assert (= *sap-int
* sap-int
))
236 (setf *sap-int
* sap-int
)))
238 (return-from try-to-leak-alien-stack
'going
))
239 (locally (declare (muffle-conditions style-warning
))
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
))
248 do
(try-to-leak-alien-stack t
))))
250 (with-test (:name
(define-alien-type struct
:redefinition
:bug-431
)
251 :fails-on
:interpreter
)
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
))))))
258 (handler-bind ((error (lambda (e)
259 (let ((cont (find-restart 'continue e
)))
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.
274 (locally (declare (muffle-conditions style-warning
))
275 (sb-alien:define-alien-routine bug-316075 void
(result char
:out
)))
276 (with-test (:name
:bug-316075
:fails-on
:win32
)
277 #+win32
(error "fail")
278 (checked-compile '(lambda () (multiple-value-list (bug-316075)))))
280 ;;; Bug #316325: "return values of alien calls assumed truncated to
281 ;;; correct width on x86"
283 (define-alien-callable truncation-test
(unsigned 64)
284 ((foo (unsigned 64)))
287 (define-alien-callable truncation-test
(unsigned 32)
288 ((foo (unsigned 32)))
291 (with-test (:name
:bug-316325
:skipped-on
(not (or :x86-64
:x86
))
292 :fails-on
:interpreter
)
293 ;; This test works by defining a callback function that provides an
294 ;; identity transform over a full-width machine word, then calling
295 ;; it as if it returned a narrower type and checking to see if any
296 ;; noise in the high bits of the result are properly ignored.
297 (macrolet ((verify (type input output
)
298 `(with-alien ((fun (* (function ,type
299 #+x86-64
(unsigned 64)
300 #+x86
(unsigned 32)))
301 :local
(alien-sap (alien-callable-function 'truncation-test
))))
302 (let ((result (alien-funcall fun
,input
)))
303 (assert (= result
,output
))))))
306 (verify (unsigned 64) #x8000000000000000
#x8000000000000000
)
307 (verify (signed 64) #x8000000000000000
#x-8000000000000000
)
308 (verify (signed 64) #x7fffffffffffffff
#x7fffffffffffffff
)
309 (verify (unsigned 32) #x0000000180000042
#x80000042
)
310 (verify (signed 32) #x0000000180000042
#x-7fffffbe
)
311 (verify (signed 32) #xffffffff7fffffff
#x7fffffff
))
314 (verify (unsigned 32) #x80000042
#x80000042
)
315 (verify (signed 32) #x80000042
#x-7fffffbe
)
316 (verify (signed 32) #x7fffffff
#x7fffffff
))
317 (verify (unsigned 16) #x00018042
#x8042
)
318 (verify (signed 16) #x003f8042
#x-7fbe
)
319 (verify (signed 16) #x003f7042
#x7042
)))
321 (with-test (:name
:bug-654485
)
322 ;; DEBUG 2 used to prevent let-conversion of the open-coded ALIEN-FUNCALL body,
323 ;; which in turn led the dreaded %SAP-ALIEN note.
325 `(lambda (program argv
)
326 (declare (optimize (debug 2)))
327 (with-alien ((sys-execv1 (function int c-string
(* c-string
)) :extern
329 (values (alien-funcall sys-execv1 program argv
))))
332 (with-test (:name
:bug-721087
:fails-on
:win32
)
333 (assert (typep nil
'(alien c-string
)))
334 (assert (not (typep nil
'(alien (c-string :not-null t
)))))
339 (when (and (null (type-error-datum e
))
340 (equal (type-error-expected-type e
)
341 '(alien (c-string :not-null t
))))
344 (with-test (:name
:make-alien-string
)
345 (labels ((content (alien length null-terminate
)
347 (cast alien c-string
)
348 (let ((buffer (make-array length
349 :element-type
'(unsigned-byte 8))))
350 (sb-kernel:copy-ub8-from-system-area
351 (alien-sap alien
) 0 buffer
0 length
)
352 (sb-ext:octets-to-string buffer
))))
353 (test (null-terminate)
354 (let* ((string "This comes from lisp!")
355 (length (length string
)))
356 (multiple-value-bind (alien alien-length
)
357 (sb-alien::make-alien-string
358 string
:null-terminate null-terminate
)
359 (assert (= alien-length
(+ length
(if null-terminate
1 0))))
361 ;; Copy to make sure STRING did not somehow end up in
363 (assert (equal (copy-seq string
)
364 (content alien length null-terminate
)))
365 (free-alien alien
)))))
369 ;;; Skip for MSAN. Instead of returning 0, the intercepted malloc is configured
370 ;;; to cause process termination by default on failure to allocate memory.
371 ;;; Skip also for UBSAN which has a smaller ARRAY-TOTAL-SIZE-LIMIT
372 ;;; and so doesn't get ENOMEM.
373 (with-test (:name
:malloc-failure
374 :skipped-on
(or :ubsan
:msan
))
378 collect
(sb-alien:make-alien char
(1- array-total-size-limit
)))
379 (storage-condition ()
382 (with-test (:name
:bug-985505
)
383 ;; Check that correct octets are reported for a c-string-decoding error.
387 (let ((c-string (coerce #(70 111 195 182 0)
388 '(vector (unsigned-byte 8)))))
389 (sb-sys:with-pinned-objects
(c-string)
390 (sb-alien::c-string-to-string
(sb-sys:vector-sap c-string
)
392 (sb-int:c-string-decoding-error
(e)
393 (assert (equalp #(195) (sb-int:character-decoding-error-octets e
)))
400 ;; C-STRING decoding doesn't know how long the string is, and since this
401 ;; looks like a 4-byte sequence, it will grab 4 octets off the end.
403 ;; So we pad the vector for safety's sake.
404 (let ((c-string (coerce #(70 111 246 0 0 0)
405 '(vector (unsigned-byte 8)))))
406 (sb-sys:with-pinned-objects
(c-string)
407 (sb-alien::c-string-to-string
(sb-sys:vector-sap c-string
)
409 (sb-int:c-string-decoding-error
(e)
410 (assert (equalp #(246 0 0 0)
411 (sb-int:character-decoding-error-octets e
)))
416 (let ((c-string (coerce #(70 195 1 182 195 182 0) '(vector (unsigned-byte 8)))))
417 (sb-sys:with-pinned-objects
(c-string)
418 (sb-alien::c-string-to-string
(sb-sys:vector-sap c-string
)
420 (sb-int:c-string-decoding-error
(e)
421 (assert (equalp #(195 1)
422 (sb-int:character-decoding-error-octets e
)))
425 (with-test (:name
:stream-to-c-string-decoding-restart-leakage
)
426 ;; Restarts for stream decoding errors didn't use to be associated with
427 ;; their conditions, so they could get confused with c-string decoding errors.
428 (assert (eq :nesting-ok
430 (handler-bind ((sb-int:character-decoding-error
431 (lambda (stream-condition)
432 (declare (ignore stream-condition
))
433 (handler-bind ((sb-int:character-decoding-error
434 (lambda (c-string-condition)
437 'sb-impl
::input-replacement
441 (let ((c-string (coerce #(70 195 1 182 195 182 0)
442 '(vector (unsigned-byte 8)))))
443 (sb-sys:with-pinned-objects
(c-string)
444 (sb-alien::c-string-to-string
445 (sb-sys:vector-sap c-string
)
446 :utf-8
'character
)))))))
447 (let ((namestring (scratch-file-name)))
450 (with-open-file (f namestring
451 :element-type
'(unsigned-byte 8)
453 :if-exists
:supersede
)
454 (dolist (b '(70 195 1 182 195 182 0))
456 (with-open-file (f namestring
457 :external-format
:utf-8
)
459 (delete-file namestring
))))))))
461 ;; Previously, the error signaled by (MACROEXPANDing the) redefinition
462 ;; of an alien structure itself signaled an error. Ensure that the
463 ;; error is signaled and prints properly.
464 (sb-alien:define-alien-type nil
465 (sb-alien:struct alien-structure-redefinition
(bar sb-alien
:int
)))
467 (with-test (:name
(:alien-structure-redefinition
:condition-printable
))
470 '(sb-alien:define-alien-type nil
471 (sb-alien:struct alien-structure-redefinition
(bar sb-alien
:c-string
))))
473 (princ-to-string condition
))
474 (:no-error
(&rest values
)
475 (declare (ignore values
))
476 (error "~@<Alien structure type redefinition failed to signal an ~
480 (with-test (:name
(:64-bit-return-truncation
))
481 (with-open-file (stream *load-truename
*)
482 (file-position stream
4294967310)
483 (assert (= 4294967310 (file-position stream
)))))
485 (with-test (:name
:stack-misalignment
)
486 (locally (declare (optimize (debug 2)))
488 (declare (optimize speed
))
489 (sb-ext:get-time-of-day
)))
490 (assert (equal (multiple-value-list
491 (multiple-value-prog1
492 (apply #'values
(list 1))
496 ;; Parse (ENUM COLOR)
497 (sb-alien-internals:parse-alien-type
'(enum color red blue black green
) nil
)
498 ;; Now reparse it as a different type
499 (with-test (:name
:change-enum-type
)
500 (handler-bind ((error #'continue
))
501 (sb-alien-internals:parse-alien-type
'(enum color yellow ochre
) nil
)))
503 (with-test (:name
:note-local-alien-type
)
504 (let ((type (sb-alien::make-local-alien-info
:type
505 (sb-alien-internals:parse-alien-type
'c-string nil
))))
506 (checked-compile-and-assert ()
508 (let ((alien (sb-alien-internals:make-local-alien
',type
)))
509 (sb-alien-internals:note-local-alien-type
',type alien
)
516 (with-test (:name
:memoize-coerce-to-interpreted-fun
)
517 (let* ((form1 '(lambda (x) x
))
518 (form2 (copy-tree form1
)))
519 (assert (eq (sb-alien::coerce-to-interpreted-function form1
)
520 (sb-alien::coerce-to-interpreted-function form2
)))))
522 (with-test (:name
:undefined-alien-name
523 :skipped-on
(not (or :x86-64
:arm64
)))
524 (dolist (memspace '(:dynamic
#+immobile-space
:immobile
))
526 (let ((sb-c::*compile-to-memory-space
* memspace
))
527 (checked-compile `(lambda ()
528 (alien-funcall (extern-alien "bar" (function (values)))))
529 :allow-style-warnings t
))))
530 (handler-case (funcall lispfun
)
532 (assert (typep c
'sb-kernel
::undefined-alien-function-error
))
533 (assert (equal (cell-error-name c
) "bar")))))))
535 (with-test (:name
:undefined-alien-name-via-linkage-table-trampoline
536 :skipped-on
(not (or :x86-64
:arm64
)))
537 (dolist (memspace '(:dynamic
#+immobile-space
:immobile
))
539 (let ((sb-c::*compile-to-memory-space
* memspace
))
542 (with-alien ((fn (* (function (values)))
543 (sb-sys:int-sap
(sb-sys:foreign-symbol-address
"baz"))))
544 (alien-funcall fn
)))))))
545 (handler-case (funcall lispfun
)
547 (assert (typep c
'sb-kernel
::undefined-alien-function-error
))
548 (assert (equal (cell-error-name c
) "baz")))))))
550 (defconstant fleem
3)
551 ;; We used to expand into
552 ;; (SYMBOL-MACROLET ((FLEEM (SB-ALIEN-INTERNALS:%ALIEN-VALUE
553 ;; which conflicted with the symbol as global variable.
554 (with-test (:name
:def-alien-rtn-use-gensym
)
555 (checked-compile '(lambda () (define-alien-routine "fleem" int
(x int
)))
556 :allow-style-warnings
(or #-
(or :x86-64
:arm
:arm64
) t
)))
558 (with-test (:name
:no-vector-sap-of-array-nil
)
559 (assert-error (sb-sys:vector-sap
(opaque-identity (make-array 5 :element-type nil
)))))
561 (cl:in-package
"SB-KERNEL")
562 (test-util:with-test
(:name
:hash-consing
)
563 (assert (eq (parse-alien-type '(integer 9) nil
)
564 (parse-alien-type '(integer 9) nil
)))
565 (assert (eq (parse-alien-type '(* (struct nil
(x int
) (y int
))) nil
)
566 (parse-alien-type '(* (struct nil
(x int
) (y int
))) nil
))))