diff --git a/CMakeLists.txt b/CMakeLists.txt index 3401739b..d67c050d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,17 +8,27 @@ project(luzer set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") include(SetClangRTLib) -find_package(Lua 5.1 REQUIRED) -find_package(LLVM REQUIRED CONFIG) - -set(LUA_NAME "lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}") -find_program(LUA_EXECUTABLE "${LUA_NAME}") -if(NOT EXISTS ${LUA_EXECUTABLE}) - message(FATAL_ERROR "${LUA_NAME} is required") +if(LUA_INCLUDE_DIR AND LUA_LIBRARIES) + # When a path to a Lua library is passed outside, we should + # mimic a real CMake library to don't break code that depends on + # LUA_LIBRARIES. + add_library(liblua STATIC IMPORTED GLOBAL) + set_target_properties(liblua PROPERTIES + IMPORTED_LOCATION ${LUA_LIBRARIES}) + set(LUA_LIBRARIES liblua) +else() + find_package(Lua 5.1 REQUIRED) + set(LUA_NAME "lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}") + find_program(LUA_EXECUTABLE "${LUA_NAME}") + if(NOT EXISTS ${LUA_EXECUTABLE}) + message(FATAL_ERROR "${LUA_NAME} is required") + endif() + message(STATUS "Found Lua ${LUA_VERSION_STRING}") + message(STATUS "Found Lua interpreter ${LUA_EXECUTABLE}") endif() -message(STATUS "Found Lua ${LUA_VERSION_STRING}") -message(STATUS "Found Lua interpreter ${LUA_EXECUTABLE}") +find_package(LLVM REQUIRED CONFIG) + message(STATUS "Found LLVM ${LLVM_VERSION}") if(${LLVM_PACKAGE_VERSION} VERSION_LESS 5.0.0) diff --git a/luzer/CMakeLists.txt b/luzer/CMakeLists.txt index ac026f1d..0226fcbd 100644 --- a/luzer/CMakeLists.txt +++ b/luzer/CMakeLists.txt @@ -15,24 +15,24 @@ set(LUZER_SOURCES luzer.c add_library(${CMAKE_PROJECT_NAME} SHARED ${LUZER_SOURCES}) target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE - ${LUA_INCLUDE_DIR} + ${LUA_INCLUDE_DIR} ) target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE - ${LUA_LIBRARIES} - -fsanitize=fuzzer-no-link - ${FUZZER_NO_MAIN_LIBRARY} + ${LUA_LIBRARIES} + -fsanitize=fuzzer-no-link + ${FUZZER_NO_MAIN_LIBRARY} ) target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE - -D_FORTIFY_SOURCE=2 - -fpie - -fPIC - -Wall - -Wextra - -Werror - -Wpedantic - -Wno-unused-parameter - -pedantic - -fsanitize=fuzzer-no-link + -D_FORTIFY_SOURCE=2 + -fpie + -fPIC + -Wall + -Wextra + -Werror + -Wpedantic + -Wno-unused-parameter + -pedantic + -fsanitize=fuzzer-no-link ) set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES PREFIX "") diff --git a/luzer/compat.c b/luzer/compat.c index 2bc398c9..e4e73e0f 100644 --- a/luzer/compat.c +++ b/luzer/compat.c @@ -1,8 +1,18 @@ -#include -#include -#include +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" -#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501 +/* + * PUC-Rio Lua uses `lconfig_h` as include guard for `luaconf.h`, + * LuaJIT uses `luaconf_h`. If you use PUC-Rio's include files + * but LuaJIT's library, you will need to define the macro + * IS_LUAJIT yourself! + */ +#if !defined(IS_LUAJIT) && defined(luaconf_h) +#define IS_LUAJIT +#endif + +#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM == 501 && !defined(IS_LUAJIT)) static int countlevels(lua_State *L) { diff --git a/luzer/compat.h b/luzer/compat.h index 7859dbb4..2fa184f4 100644 --- a/luzer/compat.h +++ b/luzer/compat.h @@ -1,7 +1,11 @@ #ifndef LUZER_COMPAT_H_ #define LUZER_COMPAT_H_ +#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM == 501 && !defined(IS_LUAJIT)) + void luaL_traceback(lua_State *L, lua_State *L1, const char *msg, int level); +#endif + #endif // LUZER_COMPAT_H_ diff --git a/luzer/custom_mutator_lib.c b/luzer/custom_mutator_lib.c index 4895b639..fa3691f9 100644 --- a/luzer/custom_mutator_lib.c +++ b/luzer/custom_mutator_lib.c @@ -7,8 +7,8 @@ #include #include #include -#include -#include +#include "lua.h" +#include "lauxlib.h" #include "luzer.h" diff --git a/luzer/fuzzed_data_provider.cc b/luzer/fuzzed_data_provider.cc index 775d0ba0..2beaa735 100644 --- a/luzer/fuzzed_data_provider.cc +++ b/luzer/fuzzed_data_provider.cc @@ -7,9 +7,9 @@ #ifdef __cplusplus extern "C" { #endif -#include -#include -#include +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" #include #ifdef __cplusplus } /* extern "C" */ diff --git a/luzer/luzer.c b/luzer/luzer.c index a7a4d14b..3ee2124e 100644 --- a/luzer/luzer.c +++ b/luzer/luzer.c @@ -5,9 +5,9 @@ */ #define _GNU_SOURCE -#include -#include -#include +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" #include #include #include diff --git a/luzer/tracer.c b/luzer/tracer.c index dfb4f01f..c62d85ad 100644 --- a/luzer/tracer.c +++ b/luzer/tracer.c @@ -20,7 +20,7 @@ * atheris/src/native/counters.cc */ -#include +#include "lua.h" #include #include /* strlen */