Skip to content

Commit

Permalink
tests: introduce ffi_cdef_proto_test
Browse files Browse the repository at this point in the history
LuaJIT has a FFI library which allows calling external C functions
and using C data structures from a pure Lua code, see [1].

According to a FFI API documentation [2] the Lua function
`ffi.cdef(def)` adds multiple C declarations for types or
external symbols (named variables or functions). `def` must be
a Lua string. The contents of the string `def` must be a sequence
of C declarations, separated by semicolons. The C parser complies
to the C99 language standard plus the extensions described in [3].

Note, LuaJIT C parser is not a validating C parser. It expects and
accepts correctly formed C declarations. Therefore without
grammar-aware fuzzing we will face with a false-positive crashes.

The patch adds a grammar-aware test, where C declarations
generated automatically using Protobuf grammar and
LibProtoBuf-mutator and then serialize Protobuf structure to
a string. Note, an implementation is not finished yet.

An example of bug in `src/lj_cparse.c` is LJ#1114.

1. https://luajit.org/ext_ffi.html
2. https://luajit.org/ext_ffi_api.html
3. https://luajit.org/ext_ffi_semantics.html#clang
4. https://luajit.org/ext_ffi_semantics.html#status
  • Loading branch information
ligurio committed Oct 18, 2024
1 parent 0e60f36 commit 8903286
Show file tree
Hide file tree
Showing 6 changed files with 1,747 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/capi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,6 @@ endforeach()

include(ProtobufMutator)
add_subdirectory(luaL_loadbuffer_proto)
if(USE_LUAJIT)
add_subdirectory(ffi_cdef_proto)
endif ()
22 changes: 22 additions & 0 deletions tests/capi/ffi_cdef_proto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
set(test_name ffi_cdef_proto_test)

add_library(cdef-proto)

foreach(lib ${LPM_LIBRARIES})
find_library(${lib} REQUIRED_FILES)
endforeach(lib)

protobuf_generate(LANGUAGE cpp
TARGET cdef-proto
PROTOS cdef.proto)

target_link_libraries(cdef-proto
${PROTOBUF_LIBRARIES})

create_test(FILENAME ${test_name}
SOURCES ffi_cdef_proto_test.cc cdef_print.cc
LIBRARIES cdef-proto ${LPM_LIBRARIES})

target_include_directories(${test_name}
PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${LUA_INCLUDE_DIR})
add_dependencies(${test_name} ${LPM_LIBRARIES} cdef-proto)
Loading

0 comments on commit 8903286

Please sign in to comment.