[racket-dev] FFI and the 2GB limit

From: Tobias Hammer (tobias.hammer at dlr.de)
Date: Thu May 31 10:39:36 EDT 2012


i stumbled over strange error messages and some unexpected behavior
in the ffi implementation.

The following code raises a very misleading error message:
(define _tm (_array _byte 1024 1024 1200 2))
(malloc _tm)

-> malloc: expects argument of type <non-void-C-type>; given #<ctype>

One dimensional arrays fail eariler and with a slightly better error:
(_array _byte 2147483649)

-> make-array-type: expects type <pointer-sized nonnegative exact integer>
as 2nd argument, given: 2147483649; other arguments were: #<ctype:uint8>

Searching in foreign.c for the cause i found out, that the current  
fails badly with types > 2GB size (not only arrays) because it uses int as
result for ctype_sizeof.
As a consequence it should even fail at the 2GB mark on 64bit  
(gcc-)systems where
int is usually fixed to 32bit for compatibility reasons.

The problem seems to be limited to the foreign.c as libc, libffi and  
rackets gc2 functions
use size_t for all size arguments. I tried to fix it and attached a patch  
vs git
head. But it may be that the use of size_t leads to strange overflow  
behavior whenever
size is multiplied by a potentially negative value to get an offset that  
is stored in
a intptr_t. As far as i can see these overflow cases are not handled in  
the current
implementation so it may be ok to continue to ignore them.

As a side question: What is the sense of foreign.rktc if it is never used  
to generate
the c file from it?


Tobias Hammer
DLR / Institute of Robotics and Mechatronics
Tel.: 08153/28-1487
Mail: tobias.hammer at dlr.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ffi-2gb.patch
Type: application/octet-stream
Size: 5910 bytes
Desc: not available
URL: <http://lists.racket-lang.org/dev/archive/attachments/20120531/030a04e3/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ffi-2gb-limit-demo.rkt
Type: application/octet-stream
Size: 199 bytes
Desc: not available
URL: <http://lists.racket-lang.org/dev/archive/attachments/20120531/030a04e3/attachment-0001.obj>

Posted on the dev mailing list.