Subject: [clisp-list] FFI:CAST segfault with gcc 3.2.2

From: Aurelio Bignoli <[email-masked]>
Date: Fri Apr 11 11:45:06 2003

Recently I switched from gcc 2.95.3 to gcc 3.2.2. After recompiling
CLISP, I discovered that, in some circumstances, FFI:CAST causes a
segmentation fault.

Test case:

define the following foreign function:

(def-call-out make-foreign-string
  (:name "make_foreign_string")
  (:arguments (s c-string :in))
  (:return-type c-pointer :malloc-free))

void *
make_foreign_string (const char *s)
{
  int len = strlen(s);
  char *r = malloc (len);
  memcpy (r, s, len);
  return r;
}

try to convert its result to a '(c-ptr (c-array character ...)):

[2]> (use-package :ffi)
T
[3]> (setf *x* (make-foreign-string "abcd"))
#<FOREIGN-ADDRESS #x081B6680>
[4]> (with-c-var (p 'c-pointer *x*) (cast p '(c-ptr (c-array uint8 4))))
#(97 98 99 100)

OK, with bytes it works.

[5]>(with-c-var (p 'c-pointer *x*) (cast p '(c-ptr (c-array character 4))))
Segmentation fault

Any idea?

---

From: Sam Steingold <[email-masked]>
Date: Wed Apr 16 07:13:20 2003

> * In message <[email-masked]>
> * On the subject of "[clisp-list] FFI:CAST segfault with gcc 3.2.2"
> * Sent on Fri, 11 Apr 2003 20:18:43 +0200
> * Honorable Aurelio Bignoli <[email-masked]> writes:
>

I do not observe this with
gcc (GCC) 3.2.1 20021207 (Red Hat Linux 8.0 3.2.1-2)

could you please get 3.2.1 and check that this is indeed a regression in
gcc?

(def-call-out make-foreign-string (:arguments (s c-string :in :malloc-free))
  (:name "ffi_identity") (:language :stdc)
  (:return-type c-pointer))
(setf *x* (make-foreign-string "abcd"))

(with-c-var (p 'c-pointer *x*) (cast p '(c-ptr (c-array uint8 4))))
==> #(97 98 99 100)

(with-c-var (p 'c-pointer *x*) (cast p '(c-ptr (c-array character 4))))
==> "abcd"

(CLISP CVS head)

-- 
Sam Steingold (http://www.podval.org/~sds) running RedHat8 GNU/Linux
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.palestine-central.com/links.html>
I don't like cats! -- Come on, you just don't know how to cook them!

---

From: Sam Steingold <[email-masked]>
Date: Wed Apr 16 16:14:05 2003

> * In message <[email-masked]>
> * On the subject of "Re: [clisp-list] FFI:CAST segfault with gcc 3.2.2"
> * Sent on 16 Apr 2003 10:11:04 -0400
> * I write:
>

I _do_ observe this crash with gcc 3.2.1 when compiled with -O2.
I do _not_ observe it when compiled with -g -O0.

this is yet another instance where -O2 vs -g makes a difference.
(see also <https://sourceforge.net/tracker/?func=detail&atid=101355&aid=710737&group_id=1355>)

-- 
Sam Steingold (http://www.podval.org/~sds) running RedHat8 GNU/Linux
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.palestine-central.com/links.html>
Winword 6.0 UNinstall: Not enough disk space to uninstall WinWord

---

From: Aurelio Bignoli <[email-masked]>
Date: Sun Apr 20 11:55:01 2003

> I do _not_ observe it when compiled with -g -O0.

I had also to add -falign-functions=4 to CFLAGS. Without it, lisp.run
prints the following error message when it is executed by make:

./lisp.run -B . -N locale -Efile UTF-8 -Eterminal UTF-8 -norc -m 750KW -x "(and (load \"init.lisp\") (sys::%saveinitmem) (ext::exit))"
C_CODE_ALIGNMENT is wrong. &EVAL-WHEN = 0x806abd2.
Add -falign-functions=4 to CFLAGS in the Makefile.
make: *** [interpreted.mem] Aborted

 > this is yet another instance where -O2 vs -g makes a difference.

the problem seems to be in the code generated by gcc for foreign.c,
probably for the function C_cast. I tried to compile it without "-O2
-fexpensive-optimizations" and all the other C modules with the usual
CFLAGS, and the resulting lisp.run passed the testsuite and the CAST
test. I'm currently using it.