[racket-dev] FFI and the 2GB limit

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Thu May 31 11:22:22 EDT 2012

Thanks for the report and patch!

I've adjusted the implementation to replace `int' with `intptr_t' and
add checks for arithmetic overflow. I didn't switch to `uintptr_t',
though, because I doubt that the extra bit is useful enough to be worth
reasoning about signed and unsigned conversions.

At Thu, 31 May 2012 16:39:36 +0200, Tobias Hammer wrote:
> Hi,
> 
> 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  
> implementation
> 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
> 
> 
> 
> -- 
> ---------------------------------------------------------
> Tobias Hammer
> DLR / Institute of Robotics and Mechatronics
> Tel.: 08153/28-1487
> Mail: tobias.hammer at dlr.de
> ------------------------------------------------------------------------------
> [application/octet-stream "ffi-2gb.patch"] [~/Desktop & open] [~/Temp & open]
> 
> ------------------------------------------------------------------------------
> [application/octet-stream "ffi-2gb-limit-demo.rkt"] [~/Desktop & open] [~/Temp & 
> open]
> _________________________
>   Racket Developers list:
>   http://lists.racket-lang.org/dev

Posted on the dev mailing list.