Skip to content

Commit

Permalink
Add LuaJIT 2.1 (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
dargueta authored Apr 14, 2023
1 parent 4fb5210 commit 70da3f0
Show file tree
Hide file tree
Showing 38 changed files with 319 additions and 166 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ jobs:
- "5.3"
- "5.4"
- "luajit-2.0.5"
- "luajit-2.1.0-beta3"
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install Lua
uses: leafo/gh-actions-lua@v10
with:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tests/c/doctest.h
src/constants/
src/registers.cpp
src/registers_const.cpp
include/unicornlua/register_types.h
include/unicornlua/register_types.hpp

# Other garbage
core*
Expand Down
16 changes: 13 additions & 3 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
Changes
=======

Unreleased
----------
2.2.0 (Unreleased)
------------------

New Features
~~~~~~~~~~~~

Official support for LuaJIT 2.1.

Other Changes
~~~~~~~~~~~~~

* Autogenerate a bunch of register-related files from templates.
* Add clang-format, use WebKit's style (more or less).
* Autogenerate a bunch of register-related files from templates. **Note:** Some
register type enums values have changed. If you use the symbolic constants
provided in ``registers_const`` this won't affect you.

2.1.0 (2023-04-08)
------------------
Expand Down
55 changes: 39 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ LIB_EXTENSION ?= so
LUA ?= lua
LUA_DIR ?= /usr/local
LUAROCKS ?= luarocks
MKDIR ?= mkdir
OBJ_EXTENSION ?= o
UNICORN_INCDIR ?= /usr/include

Expand All @@ -32,8 +33,10 @@ endif
IS_LUAJIT = $(shell $(LUA) -e 'if _G.jit ~= nil then print(1) else print(0) end')
ifeq ($(IS_LUAJIT),1)
DEFAULT_LUA_LIB_NAME := luajit-5.1
# FIXME (dargueta): This will break on LuaJIT 2.1
FALLBACK_LUA_INCDIR := $(LUA_DIR)/include/luajit-2.0
LUAJIT_VERSION := $(shell \
$(LUA) -e 'print(string.format("%d.%d", jit.version_num / 10000, (jit.version_num / 100) % 100))' \
)
FALLBACK_LUA_INCDIR := $(LUA_DIR)/include/luajit-$(LUAJIT_VERSION)
else
DEFAULT_LUA_LIB_NAME := lua
FALLBACK_LUA_INCDIR := $(LUA_DIR)/include
Expand All @@ -49,12 +52,11 @@ ARCHITECTURE_SLUGS = $(filter-out platform,$(basename $(notdir $(ARCHITECTURE_HE
CONSTS_DIR = src/constants
CONSTANT_FILES = $(foreach s,$(ARCHITECTURE_SLUGS),$(CONSTS_DIR)/$(s)_const.cpp)

CPP_TEMPLATE_SOURCES := src/registers_const.template \
src/registers.template
CPP_TEMPLATE_SOURCES := $(wildcard src/*.template)
AUTOGENERATED_CPP_FILES = $(CPP_TEMPLATE_SOURCES:.template=.cpp)

HEADER_TEMPLATE_SOURCES := include/unicornlua/register_types.template
AUTOGENERATED_HEADER_FILES = $(HEADER_TEMPLATE_SOURCES:.template=.h)
HEADER_TEMPLATE_SOURCES := $(wildcard include/unicornlua/*.template)
AUTOGENERATED_HPP_FILES = $(HEADER_TEMPLATE_SOURCES:.template=.hpp)

LIB_BUILD_TARGET := $(BUILD_DIR)/unicorn.$(LIB_EXTENSION)
LIB_CPP_SOURCES = $(wildcard src/*.cpp) $(CONSTANT_FILES) $(AUTOGENERATED_CPP_FILES)
Expand All @@ -63,7 +65,7 @@ LIB_OBJECT_FILES = $(LIB_CPP_SOURCES:.cpp=.$(OBJ_EXTENSION))
TEST_EXECUTABLE := $(BUILD_DIR)/cpp_test
TEST_CPP_SOURCES = $(wildcard tests/c/*.cpp)
TEST_LUA_SOURCES = $(wildcard tests/lua/*.lua)
TEST_HEADERS = $(wildcard tests/c/*.h)
TEST_HEADERS = $(wildcard tests/c/*.hpp)
TEST_CPP_OBJECT_FILES = $(TEST_CPP_SOURCES:.cpp=.$(OBJ_EXTENSION))

LIBRARY_DIRECTORIES := $(strip $(LUA_LIBDIR) $(FALLBACK_LUA_LIBDIR) $(UNICORN_LIBDIR) $(PTHREAD_LIBDIR) /usr/lib64 /usr/local/lib)
Expand All @@ -86,7 +88,10 @@ LINK_TO_LUA_FLAG := $(if $(LUALIB),-l:$(LUALIB),-l$(DEFAULT_LUA_LIB_NAME))
OS ?= $(shell uname -s)
ifeq ($(OS),Darwin)
ifeq ($(IS_LUAJIT),1)
LINK_TO_LUA_FLAG += -pagezero_size 10000 -image_base 100000000
# This workaround isn't needed for LuaJIT 2.1+
ifeq ($(LUAJIT_VERSION),2.0)
LINK_TO_LUA_FLAG += -pagezero_size 10000 -image_base 100000000
endif
endif
endif

Expand All @@ -109,7 +114,7 @@ DOCTEST_TAG := v2.4.11
DOCTEST_HEADER := tests/c/doctest.h

# Uncomment for debugging autogenerated files
# .PRECIOUS: $(AUTOGENERATED_CPP_FILES) $(AUTOGENERATED_HEADER_FILES) $(CONSTANT_FILES)
# .PRECIOUS: $(AUTOGENERATED_CPP_FILES) $(AUTOGENERATED_HPP_FILES) $(CONSTANT_FILES)

.PHONY: all
all: $(LIB_BUILD_TARGET) $(TEST_EXECUTABLE)
Expand All @@ -124,7 +129,7 @@ install: $(LIB_BUILD_TARGET)
clean:
$(RM) $(LIB_OBJECT_FILES) $(CONSTANT_FILES) $(LIB_BUILD_TARGET)
$(RM) $(TEST_EXECUTABLE) $(TEST_CPP_OBJECT_FILES) $(DOCTEST_HEADER)
$(RM) -r $(BUILD_DIR) $(CONSTS_DIR) $(AUTOGENERATED_CPP_FILES) $(AUTOGENERATED_HEADER_FILES)
$(RM) -r $(BUILD_DIR) $(CONSTS_DIR) $(AUTOGENERATED_CPP_FILES) $(AUTOGENERATED_HPP_FILES)


.PHONY: test
Expand All @@ -139,6 +144,21 @@ test: $(TEST_EXECUTABLE) $(TEST_LUA_SOURCES)
tests/lua


.PHONY: autoformat
autoformat:
@clang-format --Werror -i --verbose \
$(filter-out $(AUTOGENERATED_CPP_FILES),$(wildcard src/*.cpp)) \
$(filter-out $(AUTOGENERATED_HPP_FILES),$(wildcard include/unicornlua/*.hpp)) \
$(wildcard tests/c/*.cpp) \
$(wildcard tests/c/*.hpp)


# Convenience target for generating all templated files. This is mostly for
# making IDEs and linters shut up about "missing" files.
.PHONY: autogen-files
autogen-files: $(AUTOGENERATED_CPP_FILES) $(AUTOGENERATED_HPP_FILES) $(CONSTANT_FILES)


$(DOCTEST_HEADER):
$(CURL) -sSo $@ https://raw.githubusercontent.com/doctest/doctest/$(DOCTEST_TAG)/doctest/doctest.h

Expand All @@ -148,11 +168,12 @@ $(LIB_BUILD_TARGET): $(LIB_OBJECT_FILES) | $(BUILD_DIR)


$(TEST_EXECUTABLE): $(DOCTEST_HEADER) $(TEST_CPP_OBJECT_FILES) $(LIB_OBJECT_FILES) $(TEST_HEADERS)
$(LINK_CMD) -o $@ $(filter-out %.h,$^) $(REQUIRED_LIBS_FLAGS) $(LINK_TO_LUA_FLAG) -lm
$(LINK_CMD) -o $@ $(filter %.$(OBJ_EXTENSION),$^) $(REQUIRED_LIBS_FLAGS) $(LINK_TO_LUA_FLAG) -lm


$(CONSTS_DIR)/%_const.cpp: $(UNICORN_INCDIR)/unicorn/%.h | $(CONSTS_DIR)
$(SET_SEARCH_PATHS); $(LUA) tools/generate_constants.lua $< $@
@echo "Generating $@"
@$(SET_SEARCH_PATHS); $(LUA) tools/generate_constants.lua $< $@


# We're deliberately omitting CXXFLAGS as provided by LuaRocks because it includes
Expand All @@ -161,16 +182,18 @@ tests/c/%.$(OBJ_EXTENSION): tests/c/%.cpp
$(CXX_CMD) -c -o $@ $^


src/%.$(OBJ_EXTENSION): src/%.cpp $(AUTOGENERATED_HEADER_FILES)
src/%.$(OBJ_EXTENSION): src/%.cpp $(AUTOGENERATED_HPP_FILES)
$(CXX_CMD) $(CXXFLAGS) -c -o $@ $<


%.cpp: %.template src/register_types.lua
$(LUA) tools/render_template.lua -o $@ $^
@echo "Generating $@"
@$(SET_SEARCH_PATHS); $(LUA) tools/render_template.lua -o $@ $^


%.h: %.template src/register_types.lua
$(LUA) tools/render_template.lua -o $@ $^
%.hpp: %.template src/register_types.lua
@echo "Generating $@"
@$(SET_SEARCH_PATHS); $(LUA) tools/render_template.lua -o $@ $^


$(CONSTS_DIR) $(BUILD_DIR):
Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ unicorn-lua
:alt: Build status
:target: https://travis-ci.com/dargueta/unicorn-lua

.. |lua-versions| image:: https://img.shields.io/badge/lua-5.1%20%7C%205.2%20%7C%205.3%20%7C%205.4%20%7C%20LuaJIT2.0-blue
.. |lua-versions| image:: https://img.shields.io/badge/lua-5.1%20%7C%205.2%20%7C%205.3%20%7C%205.4%20%7C%20LuaJIT2.020%7C%20LuaJIT2.1-blue-blue
:alt: Lua versions
:target: https://www.lua.org

Expand All @@ -16,8 +16,8 @@ unicorn-lua

Lua bindings for the `Unicorn CPU Emulator`_.

I'm currently testing this on vanilla Lua 5.1 - 5.4, and LuaJIT 2.0 on both Linux
and OSX.
I'm currently testing this on vanilla Lua 5.1 - 5.4, LuaJIT 2.0, and LuaJIT 2.1 on
both Linux and MacOS.

License Change
--------------
Expand Down
9 changes: 7 additions & 2 deletions include/unicornlua/compat.h → include/unicornlua/compat.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Compatibility shims for differences between Lua versions.
*
* @file compat.h
* @file compat.hpp
*/

#pragma once
Expand Down Expand Up @@ -42,10 +42,15 @@ LUALIB_API void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup);

LUALIB_API void lua_rawsetp(lua_State* L, int index, const void* p);

#ifndef luaL_newlibtable
#define luaL_newlibtable(L, l) lua_createtable((L), 0, sizeof(l) / sizeof(*(l)))
#endif // luaL_newlibtable

#ifndef luaL_newlib
#define luaL_newlib(L, l) \
(luaL_newlibtable((L), (l)), luaL_setfuncs((L), (l), 0))
#endif
#endif // luaL_newlib
#endif // LUA_VERSION_NUM < 502

// http://lua-users.org/lists/lua-l/2011-11/msg01149.html
#ifndef IS_LUAJIT
Expand Down
10 changes: 4 additions & 6 deletions include/unicornlua/context.h → include/unicornlua/context.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* Lua bindings for Unicorn context operations.
*
* @file context.h
* @file context.hpp
*/

#pragma once

#include <unicorn/unicorn.h>

#include "unicornlua/engine.h"
#include "unicornlua/lua.h"
#include "unicornlua/engine.hpp"
#include "unicornlua/lua.hpp"

extern const char* const kContextMetatableName;
extern const luaL_Reg kContextMetamethods[];
Expand Down Expand Up @@ -37,6 +37,4 @@ int ul_context_free(lua_State* L);
*/
int ul_context_maybe_free(lua_State* L);

#define get_context_struct(L, index) \
(reinterpret_cast<Context*>( \
luaL_checkudata((L), (index), kContextMetatableName)))
Context* ul_toluacontext(lua_State* L, int index);
28 changes: 19 additions & 9 deletions include/unicornlua/engine.h → include/unicornlua/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

#include <unicorn/unicorn.h>

#include "unicornlua/hooks.h"
#include "unicornlua/lua.h"
#include "unicornlua/utils.h"
#include "unicornlua/hooks.hpp"
#include "unicornlua/lua.hpp"
#include "unicornlua/utils.hpp"

extern const char* const kEngineMetatableName;
extern const char* const kEnginePointerMapName;
extern "C" {
extern const luaL_Reg kEngineInstanceMethods[];
extern const luaL_Reg kEngineMetamethods[];
}

struct Context;

Expand Down Expand Up @@ -79,7 +81,7 @@ class UCLuaEngine {
* @param L A pointer to the current Lua state.
* @param engine A pointer to the engine we want to get the Lua object for.
*/
void ul_get_engine_object(lua_State* L, const uc_engine* engine);
void ul_find_lua_engine(lua_State* L, const uc_engine* engine);

/**
* Initialize the engine object internals, such as registering metatables.
Expand All @@ -99,17 +101,25 @@ void ul_init_engines_lib(lua_State* L);
* @param L A pointer to the current Lua state.
* @param index The index on the Lua stack of the value to convert.
*
* @return The engine.
* @return The low-level Unicorn engine.
*/
uc_engine* ul_toengine(lua_State* L, int index);

#define get_engine_struct(L, index) \
reinterpret_cast<UCLuaEngine*>( \
luaL_checkudata((L), (index), kEngineMetatableName))
/**
* Return the value on the stack at @a index as a pointer to a @ref UCLuaEngine.
*
* If the value at @a index is @e not a @ref UCLuaEngine, a Lua error will be
* thrown.
*
* @param L A pointer to the current Lua state.
* @param index The index on the Lua stack of the value to convert.
*
* @return The engine.
*/
UCLuaEngine* ul_toluaengine(lua_State* L, int index);

int ul_close(lua_State* L);
int ul_query(lua_State* L);
int ul_errno(lua_State* L);
int ul_emu_start(lua_State* L);
int ul_emu_stop(lua_State* L);
uc_engine* ul_toengine(lua_State* L, int index);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <unicorn/unicorn.h>

#include "unicornlua/lua.h"
#include "unicornlua/lua.hpp"

/**
* Exception class for translating Unicorn error codes into C++ exceptions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <unicorn/unicorn.h>

#include "unicornlua/lua.h"
#include "unicornlua/lua.hpp"

class Hook {
friend class UCLuaEngine;
Expand Down
2 changes: 1 addition & 1 deletion include/unicornlua/lua.h → include/unicornlua/lua.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ extern "C" {
#include <lua.h>
}

#include "compat.h"
#include "compat.hpp"
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#pragma once

#include "unicornlua/lua.h"
#include "unicornlua/lua.hpp"

/**
* Write data to a location in a machine's memory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <sstream>

#include "lua.h"
#include "registers.h"
#include "registers.hpp"

template <class T, size_t N>
void integer_array_to_table(lua_State* L, const std::array<T, N>& arr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include <climits>
#include <cstdint>

#include "unicornlua/lua.h"
#include "unicornlua/register_types.h"
#include "unicornlua/lua.hpp"
#include "unicornlua/register_types.hpp"

#if FLT_RADIX != 2
#error "Can't handle floating-point radixes other than 2 right now."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#include <unicorn/unicorn.h>

#include "unicornlua/compat.h"
#include "unicornlua/lua.h"
#include "unicornlua/compat.hpp"
#include "unicornlua/lua.hpp"

#if UC_VERSION_MAJOR != 1
#error "Library must be compiled against version 1.x of Unicorn."
Expand All @@ -23,7 +23,7 @@
/**
* The minor version number of this Lua library (second part, x.1.x).
*/
#define UNICORNLUA_VERSION_MINOR 1
#define UNICORNLUA_VERSION_MINOR 2

/**
* The patch version number of this Lua library (third part, x.x.1).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include <unicorn/unicorn.h>

#include "unicornlua/lua.h"
#include "unicornlua/lua.hpp"

/**
* Throw a Lua error with a message derived from the given Unicorn error code.
Expand Down
File renamed without changes.
Loading

0 comments on commit 70da3f0

Please sign in to comment.