diff --git a/doc/ext_ffi_semantics.html b/doc/ext_ffi_semantics.html index 9b7cac70..f48c6406 100644 --- a/doc/ext_ffi_semantics.html +++ b/doc/ext_ffi_semantics.html @@ -57,18 +57,159 @@

-TODO +This page describes the detailed semantics underlying the FFI library +and its interaction with both Lua and C code. +

+

+Given that the FFI library is designed to interface with C code +and that declarations can be written in plain C syntax, it +closely follows the C language semantics wherever possible. Some +concessions are needed for smoother interoperation with Lua language +semantics. But it should be straightforward to write applications +using the LuaJIT FFI for developers with a C or C++ background.

C Language Support

-TODO +The FFI library has a built-in C parser with a minimal memory +footprint. It's used by the ffi.* library +functions to declare C types or external symbols.

+

+It's only purpose is to parse C declarations, as found e.g. in +C header files. Although it does evaluate constant expressions, +it's not a C compiler. The body of inline +C function definitions is simply ignored. +

+

+Also, this is not a validating C parser. It expects and +accepts correctly formed C declarations, but it may choose to +ignore bad declarations or show rather generic error messages. If in +doubt, please check the input against your favorite C compiler. +

+

+The C parser complies to the C99 language standard plus +the following extensions: +

+ +

+The following C types are pre-defined by the C parser (like +a typedef, except re-declarations will be ignored): +

+ +

+You're encouraged to use these types in preference to the +compiler-specific extensions or the target-dependent standard types. +E.g. char differs in signedness and long differs in +size, depending on the target architecture and platform ABI. +

+

+The following C features are not supported: +

+

C Type Conversion Rules

TODO

+

Conversions from C types to Lua objects

+

Conversions from Lua objects to C types

+

Conversions between C types

Initializers

@@ -81,8 +222,8 @@ initializers and the C types involved:

  • If no initializers are given, the object is filled with zero bytes.
  • Scalar types (numbers and pointers) accept a single initializer. -The standard C type conversion rules -apply.
  • +The Lua object is converted to the scalar +C type.
  • Valarrays (complex numbers and vectors) are treated like scalars when a single initializer is given. Otherwise they are treated like @@ -111,16 +252,6 @@ initializer or a compatible aggregate, of course.
  • -

    C Library Namespaces

    -

    -A C library namespace is a special kind of object which allows -access to the symbols contained in libraries. Indexing it with a -symbol name (a Lua string) automatically binds it to the library. -

    -

    -TODO -

    -

    Operations on cdata Objects

    TODO @@ -158,9 +289,9 @@ Similar rules apply for Lua strings which are implicitly converted to "const char *": the string object itself must be referenced somewhere or it'll be garbage collected eventually. The pointer will then point to stale data, which may have already beeen -overwritten. Note that string literals are automatically kept alive as -long as the function containing it (actually its prototype) is not -garbage collected. +overwritten. Note that string literals are automatically kept +alive as long as the function containing it (actually its prototype) +is not garbage collected.

    Objects which are passed as an argument to an external C function @@ -181,6 +312,121 @@ indistinguishable from pointers returned by C functions (which is one of the reasons why the GC cannot follow them).

    +

    C Library Namespaces

    +

    +A C library namespace is a special kind of object which allows +access to the symbols contained in shared libraries or the default +symbol namespace. The default +ffi.C namespace is +automatically created when the FFI library is loaded. C library +namespaces for specific shared libraries may be created with the +ffi.load() API +function. +

    +

    +Indexing a C library namespace object with a symbol name (a Lua +string) automatically binds it to the library. First the symbol type +is resolved — it must have been declared with +ffi.cdef. Then the +symbol address is resolved by searching for the symbol name in the +associated shared libraries or the default symbol namespace. Finally, +the resulting binding between the symbol name, the symbol type and its +address is cached. Missing symbol declarations or nonexistent symbol +names cause an error. +

    +

    +This is what happens on a read access for the different kinds of +symbols: +

    + +

    +This is what happens on a write access: +

    + +

    +C library namespaces themselves are garbage collected objects. If +the last reference to the namespace object is gone, the garbage +collector will eventually release the shared library reference and +remove all memory associated with the namespace. Since this may +trigger the removal of the shared library from the memory of the +running process, it's generally not safe to use function +cdata objects obtained from a library if the namespace object may be +unreferenced. +

    +

    +Performance notice: the JIT compiler specializes to the identity of +namespace objects and to the strings used to index it. This +effectively turns function cdata objects into constants. It's not +useful and actually counter-productive to explicitly cache these +function objects, e.g. local strlen = ffi.C.strlen. OTOH it +is useful to cache the namespace itself, e.g. local C = +ffi.C. +

    + +

    No Hand-holding!

    +

    +The FFI library has been designed as a low-level library. The +goal is to interface with C code and C data types with a +minimum of overhead. This means you can do anything you can do +from C: access all memory, overwrite anything in memory, call +machine code at any memory address and so on. +

    +

    +The FFI library provides no memory safety, unlike regular Lua +code. It will happily allow you to dereference a NULL +pointer, to access arrays out of bounds or to misdeclare +C functions. If you make a mistake, your application might crash, +just like equivalent C code would. +

    +

    +This behavior is inevitable, since the goal is to provide full +interoperability with C code. Adding extra safety measures, like +bounds checks, would be futile. There's no way to detect +misdeclarations of C functions, since shared libraries only +provide symbol names, but no type information. Likewise there's no way +to infer the valid range of indexes for a returned pointer. +

    +

    +Again: the FFI library is a low-level library. This implies it needs +to be used with care, but it's flexibility and performance often +outweigh this concern. If you're a C or C++ developer, it'll be easy +to apply your existing knowledge. OTOH writing code for the FFI +library is not for the faint of heart and probably shouldn't be the +first exercise for someone with little experience in Lua, C or C++. +

    +

    +As a corollary of the above, the FFI library is not safe for use by +untrusted Lua code. If you're sandboxing untrusted Lua code, you +definitely don't want to give this code access to the FFI library or +to any cdata object (except 64 bit integers or complex +numbers). Any properly engineered Lua sandbox needs to provide safety +wrappers for many of the standard Lua library functions — +similar wrappers need to be written for high-level operations on FFI +data types, too. +

    +

    Current Status

    The initial release of the FFI library has some limitations and is @@ -200,18 +446,15 @@ obscure constructs.

  • static const declarations only work for integer types up to 32 bits. Neither declaring string constants nor floating-point constants is supported.
  • -
  • The long double C type is parsed correctly, but -there's no support for the related conversions, accesses or -arithmetic operations.
  • Packed struct bitfields that cross container boundaries are not implemented.
  • -
  • Native vector types may be defined with the GCC mode and -vector_size attributes. But no operations other than loading, +
  • Native vector types may be defined with the GCC mode or +vector_size attribute. But no operations other than loading, storing and initializing them are supported, yet.
  • The volatile type qualifier is currently ignored by compiled code.
  • -
  • ffi.cdef silently ignores -all redeclarations.
  • +
  • ffi.cdef silently +ignores all redeclarations.
  • The JIT compiler already handles a large subset of all FFI operations. @@ -238,6 +481,7 @@ two. value.

  • Calls to C functions with 64 bit arguments or return values on 32 bit CPUs.
  • +
  • Accesses to external variables in C library namespaces.
  • tostring() for cdata types.
  • The following ffi.* API functions: ffi.sizeof(), ffi.alignof(), ffi.offsetof().