Re: Possibly FFI bug on ARM

  • From: Justin Cormack <justin@xxxxxxxxxxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 7 Aug 2012 16:54:17 +0100

On 7 Aug 2012 15:38, "mike" <mikhaeldanilov@xxxxxxxxx> wrote:
>
> Hi,
>
> FFI can lead to unaligned access exception, when working with packed
structures on ARM ( in my case OMAP3 ( beagleBoard xm), ubuntu 12.04 )
platform. ( on x86|x64 everything works fine )
>
> Should this be considered FFI bug, or it is up to user to take care about
alignment issues on alignment sensitive platforms?
>
> Code to reproduce case:
>
> ffi = require "ffi"
>
> ffi.cdef[[
>    typedef __attribute__ ((packed)) struct _tst
>    {
>       int8_t   _byte;
>       int16_t _short;
>       int32_t _int;
>    }tst;
> ]]
>
>    tmp = ffi.new("tst");
>
>    tmp._byte  = math.random() * 0xff;        -- ok
>    tmp._short = math.random() * 0xffff;      -- ok
>    tmp._int     = math.random() * 0xffffffff;  --  got SIGBUS here (
tmp._int = const works fine ( ?gets optimized out? ) )
>
>    print(tmp._byte);    -- ok
>    print(tmp._short);   -- ok
>    print(tmp._int);        -- ok, ?or just optimized out?
>
> P.S.
> echo 2 > /proc/cpu/alignment
> (tell linux kernel to fix alignment errors, more info:
http://www.mjmwired.net/kernel/Documentation/arm/mem_alignment )
> do not helps, not sure is this a FFI, or linux kernel issue here...
>

That's your problem. You asked for a non aligned structure by using packed.
You should be able to use ffi.copy to set them in this case rather than
assignment. Ffi is a low level interface...

Justin

Other related posts: