Force utf-8 encoding in load-test
[sbcl.git] / tests / alien.impure.lisp
blobc4667e864d66d1bfc14e4e24bb707b6cfa962a6e
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 (labels ((content (alien length null-terminate)
343 (if null-terminate
344 (cast alien c-string)
345 (let ((buffer (make-array length
346 :element-type '(unsigned-byte 8))))
347 (sb-kernel:copy-ub8-from-system-area
348 (alien-sap alien) 0 buffer 0 length)
349 (sb-ext:octets-to-string buffer))))
350 (test (null-terminate)
351 (let* ((string "This comes from lisp!")
352 (length (length string)))
353 (multiple-value-bind (alien alien-length)
354 (sb-alien::make-alien-string
355 string :null-terminate null-terminate)
356 (assert (= alien-length (+ length (if null-terminate 1 0))))
357 (gc :full t)
358 ;; Copy to make sure STRING did not somehow end up in
359 ;; the alien object.
360 (assert (equal (copy-seq string)
361 (content alien length null-terminate)))
362 (free-alien alien)))))
363 (test nil)
364 (test t)))
366 (with-test (:name :malloc-failure
367 :fails-on :alpha) ;; Alpha has address space to burn
368 (assert (eq :enomem
369 (handler-case
370 (loop repeat 128
371 collect (sb-alien:make-alien char (1- array-total-size-limit)))
372 (storage-condition ()
373 :enomem)))))
375 (with-test (:name :bug-985505)
376 ;; Check that correct octets are reported for a c-string-decoding error.
377 (assert
378 (eq :unibyte
379 (handler-case
380 (let ((c-string (coerce #(70 111 195 182 0)
381 '(vector (unsigned-byte 8)))))
382 (sb-sys:with-pinned-objects (c-string)
383 (sb-alien::c-string-to-string (sb-sys:vector-sap c-string)
384 :ascii 'character)))
385 (sb-int:c-string-decoding-error (e)
386 (assert (equalp #(195) (sb-int:character-decoding-error-octets e)))
387 :unibyte))))
388 (assert
389 (eq :multibyte-4
390 (handler-case
391 ;; KLUDGE, sort of.
393 ;; C-STRING decoding doesn't know how long the string is, and since this
394 ;; looks like a 4-byte sequence, it will grab 4 octets off the end.
396 ;; So we pad the vector for safety's sake.
397 (let ((c-string (coerce #(70 111 246 0 0 0)
398 '(vector (unsigned-byte 8)))))
399 (sb-sys:with-pinned-objects (c-string)
400 (sb-alien::c-string-to-string (sb-sys:vector-sap c-string)
401 :utf-8 'character)))
402 (sb-int:c-string-decoding-error (e)
403 (assert (equalp #(246 0 0 0)
404 (sb-int:character-decoding-error-octets e)))
405 :multibyte-4))))
406 (assert
407 (eq :multibyte-2
408 (handler-case
409 (let ((c-string (coerce #(70 195 1 182 195 182 0) '(vector (unsigned-byte 8)))))
410 (sb-sys:with-pinned-objects (c-string)
411 (sb-alien::c-string-to-string (sb-sys:vector-sap c-string)
412 :utf-8 'character)))
413 (sb-int:c-string-decoding-error (e)
414 (assert (equalp #(195 1)
415 (sb-int:character-decoding-error-octets e)))
416 :multibyte-2)))))
418 (with-test (:name :stream-to-c-string-decoding-restart-leakage)
419 ;; Restarts for stream decoding errors didn't use to be associated with
420 ;; their conditions, so they could get confused with c-string decoding errors.
421 (assert (eq :nesting-ok
422 (catch 'out
423 (handler-bind ((sb-int:character-decoding-error
424 (lambda (stream-condition)
425 (declare (ignore stream-condition))
426 (handler-bind ((sb-int:character-decoding-error
427 (lambda (c-string-condition)
428 (throw 'out
429 (if (find-restart
430 'sb-impl::input-replacement
431 c-string-condition)
432 :bad-restart
433 :nesting-ok)))))
434 (let ((c-string (coerce #(70 195 1 182 195 182 0)
435 '(vector (unsigned-byte 8)))))
436 (sb-sys:with-pinned-objects (c-string)
437 (sb-alien::c-string-to-string
438 (sb-sys:vector-sap c-string)
439 :utf-8 'character)))))))
440 (let ((namestring "alien.impure.tmp"))
441 (unwind-protect
442 (progn
443 (with-open-file (f namestring
444 :element-type '(unsigned-byte 8)
445 :direction :output
446 :if-exists :supersede)
447 (dolist (b '(70 195 1 182 195 182 0))
448 (write-byte b f)))
449 (with-open-file (f namestring
450 :external-format :utf-8)
451 (read-line f)))
452 (delete-file namestring))))))))
454 ;; Previously, the error signaled by (MACROEXPANDing the) redefinition
455 ;; of an alien structure itself signaled an error. Ensure that the
456 ;; error is signaled and prints properly.
457 (sb-alien:define-alien-type nil
458 (sb-alien:struct alien-structure-redefinition (bar sb-alien:int)))
460 (with-test (:name (:alien-structure-redefinition :condition-printable))
461 (handler-case
462 (macroexpand
463 '(sb-alien:define-alien-type nil
464 (sb-alien:struct alien-structure-redefinition (bar sb-alien:c-string))))
465 (error (condition)
466 (princ-to-string condition))
467 (:no-error (&rest values)
468 (declare (ignore values))
469 (error "~@<Alien structure type redefinition failed to signal an ~
470 error~@:>"))))
472 #+largefile
473 (with-test (:name (:64-bit-return-truncation))
474 (with-open-file (stream *load-truename*)
475 (file-position stream 4294967310)
476 (assert (= 4294967310 (file-position stream)))))
478 (with-test (:name :stack-misalignment)
479 (locally (declare (optimize (debug 2)))
480 (labels ((foo ()
481 (declare (optimize speed))
482 (sb-ext:get-time-of-day)))
483 (assert (equal (multiple-value-list
484 (multiple-value-prog1
485 (apply #'values (list 1))
486 (foo)))
487 '(1))))))
489 ;; Parse (ENUM COLOR)
490 (sb-alien-internals:parse-alien-type '(enum color red blue black green) nil)
491 ;; Now reparse it as a different type
492 (with-test (:name :change-enum-type)
493 (handler-bind ((error #'continue))
494 (sb-alien-internals:parse-alien-type '(enum color yellow ochre) nil)))