diff --git a/.clang-format b/.clang-format index f8c584f7..04f4829b 100644 --- a/.clang-format +++ b/.clang-format @@ -1,22 +1,22 @@ --- Language: Cpp -BasedOnStyle: WebKit -AccessModifierOffset: -4 -AlignAfterOpenBracket: DontAlign +# BasedOnStyle: Microsoft +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align AlignArrayOfStructures: None AlignConsecutiveMacros: None AlignConsecutiveAssignments: None AlignConsecutiveBitFields: None AlignConsecutiveDeclarations: None AlignEscapedNewlines: Right -AlignOperands: DontAlign -AlignTrailingComments: false +AlignOperands: Align +AlignTrailingComments: true AllowAllArgumentsOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true -AllowShortEnumsOnASingleLine: true -AllowShortBlocksOnASingleLine: Empty +AllowShortEnumsOnASingleLine: false +AllowShortBlocksOnASingleLine: Never AllowShortCaseLabelsOnASingleLine: false -AllowShortFunctionsOnASingleLine: All +AllowShortFunctionsOnASingleLine: None AllowShortLambdasOnASingleLine: All AllowShortIfStatementsOnASingleLine: Never AllowShortLoopsOnASingleLine: false @@ -30,40 +30,40 @@ BinPackArguments: true BinPackParameters: true BraceWrapping: AfterCaseLabel: false - AfterClass: false - AfterControlStatement: Never - AfterEnum: false + AfterClass: true + AfterControlStatement: Always + AfterEnum: true AfterFunction: true - AfterNamespace: false - AfterObjCDeclaration: false - AfterStruct: false + AfterNamespace: true + AfterObjCDeclaration: true + AfterStruct: true AfterUnion: false - AfterExternBlock: false - BeforeCatch: false - BeforeElse: false + AfterExternBlock: true + BeforeCatch: true + BeforeElse: true BeforeLambdaBody: false BeforeWhile: false IndentBraces: false SplitEmptyFunction: true SplitEmptyRecord: true SplitEmptyNamespace: true -BreakBeforeBinaryOperators: All +BreakBeforeBinaryOperators: None BreakBeforeConceptDeclarations: true -BreakBeforeBraces: WebKit +BreakBeforeBraces: Custom BreakBeforeInheritanceComma: false BreakInheritanceList: BeforeColon BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false -BreakConstructorInitializers: BeforeComma +BreakConstructorInitializers: BeforeColon BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true -ColumnLimit: 80 +ColumnLimit: 90 # CHANGED from 120 CommentPragmas: '^ IWYU pragma:' QualifierAlignment: Leave CompactNamespaces: false ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 -Cpp11BracedListStyle: false +Cpp11BracedListStyle: true DeriveLineEnding: true DerivePointerAlignment: false DisableFormat: false @@ -71,9 +71,10 @@ EmptyLineAfterAccessModifier: Never EmptyLineBeforeAccessModifier: LogicalBlock ExperimentalAutoDetectBinPacking: false PackConstructorInitializers: BinPack +BasedOnStyle: '' ConstructorInitializerAllOnOneLineOrOnePerLine: false AllowAllConstructorInitializersOnNextLine: true -FixNamespaceComments: false +FixNamespaceComments: true ForEachMacros: - foreach - Q_FOREACH @@ -100,7 +101,8 @@ IndentAccessModifiers: false IndentCaseLabels: false IndentCaseBlocks: false IndentGotoLabels: true -IndentPPDirectives: None +# Changed +IndentPPDirectives: AfterHash IndentExternBlock: AfterExternBlock IndentRequires: false IndentWidth: 4 @@ -113,11 +115,11 @@ LambdaBodyIndentation: Signature MacroBlockBegin: '' MacroBlockEnd: '' MaxEmptyLinesToKeep: 1 -NamespaceIndentation: Inner +NamespaceIndentation: None ObjCBinPackProtocolList: Auto -ObjCBlockIndentWidth: 4 +ObjCBlockIndentWidth: 2 ObjCBreakBeforeNestedBlockParam: true -ObjCSpaceAfterProperty: true +ObjCSpaceAfterProperty: false ObjCSpaceBeforeProtocolList: true PenaltyBreakAssignment: 2 PenaltyBreakBeforeFirstCallParameter: 19 @@ -127,10 +129,10 @@ PenaltyBreakOpenParenthesis: 0 PenaltyBreakString: 1000 PenaltyBreakTemplateDeclaration: 10 PenaltyExcessCharacter: 1000000 -PenaltyReturnTypeOnItsOwnLine: 60 +PenaltyReturnTypeOnItsOwnLine: 1000 PenaltyIndentedWhitespace: 0 -PointerAlignment: Left -PPIndentWidth: -1 +PointerAlignment: Right +PPIndentWidth: 4 ReferenceAlignment: Pointer ReflowComments: true RemoveBracesLLVM: false @@ -144,7 +146,7 @@ SpaceAfterLogicalNot: false SpaceAfterTemplateKeyword: true SpaceBeforeAssignmentOperators: true SpaceBeforeCaseColon: false -SpaceBeforeCpp11BracedList: true +SpaceBeforeCpp11BracedList: false SpaceBeforeCtorInitializerColon: true SpaceBeforeInheritanceColon: true SpaceBeforeParens: ControlStatements @@ -158,7 +160,7 @@ SpaceBeforeParensOptions: BeforeNonEmptyParentheses: false SpaceAroundPointerQualifiers: Default SpaceBeforeRangeBasedForLoopColon: true -SpaceInEmptyBlock: true +SpaceInEmptyBlock: false SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 1 SpacesInAngles: Never @@ -178,7 +180,7 @@ StatementAttributeLikeMacros: StatementMacros: - Q_UNUSED - QT_REQUIRE_VERSION -TabWidth: 8 +TabWidth: 4 UseCRLF: false UseTab: Never WhitespaceSensitiveMacros: @@ -188,4 +190,3 @@ WhitespaceSensitiveMacros: - NS_SWIFT_NAME - CF_SWIFT_NAME ... - diff --git a/include/unicornlua/compat.hpp b/include/unicornlua/compat.hpp index 1f8dac91..d3ce1012 100644 --- a/include/unicornlua/compat.hpp +++ b/include/unicornlua/compat.hpp @@ -6,7 +6,8 @@ #pragma once -extern "C" { +extern "C" +{ #include #include #include @@ -14,14 +15,14 @@ extern "C" { /* Compatibility stuff for Lua < 5.3 */ #if LUA_VERSION_NUM < 503 -LUA_API void lua_seti(lua_State* L, int index, lua_Integer n); -LUA_API int lua_geti(lua_State* L, int index, lua_Integer i); +LUA_API void lua_seti(lua_State *L, int index, lua_Integer n); +LUA_API int lua_geti(lua_State *L, int index, lua_Integer i); #endif /* Compatibility stuff for Lua < 5.2 */ #if LUA_VERSION_NUM < 502 /* Copied and pasted from the 5.3 implementation. */ -LUALIB_API void luaL_setmetatable(lua_State* L, const char* tname); +LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname); /** * A partial replacement for Lua 5.2+ @e lua_len. @@ -29,35 +30,34 @@ LUALIB_API void luaL_setmetatable(lua_State* L, const char* tname); * @warning This DOES NOT invoke the `__len` metamethod, and as of right now * this library doesn't need it so it won't be supported. */ -LUA_API void lua_len(lua_State* L, int index); +LUA_API void lua_len(lua_State *L, int index); -LUA_API int luaL_len(lua_State* L, int index); +LUA_API int luaL_len(lua_State *L, int index); /** * Implementation of Lua 5.2+ `lua_absindex`. */ -LUA_API int lua_absindex(lua_State* L, int index); +LUA_API int lua_absindex(lua_State *L, int index); /* Copied and pasted from the 5.3 implementation. */ -LUALIB_API void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup); +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); +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_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 // luaL_newlib -#endif // LUA_VERSION_NUM < 502 +# ifndef luaL_newlib +# define luaL_newlib(L, l) (luaL_newlibtable((L), (l)), luaL_setfuncs((L), (l), 0)) +# endif // luaL_newlib +#endif // LUA_VERSION_NUM < 502 // http://lua-users.org/lists/lua-l/2011-11/msg01149.html #ifndef IS_LUAJIT -#ifdef LUA_JDIR -#define IS_LUAJIT 1 -#else -#define IS_LUAJIT 0 -#endif +# ifdef LUA_JDIR +# define IS_LUAJIT 1 +# else +# define IS_LUAJIT 0 +# endif #endif diff --git a/include/unicornlua/context.hpp b/include/unicornlua/context.hpp index f711bf2a..9a1af419 100644 --- a/include/unicornlua/context.hpp +++ b/include/unicornlua/context.hpp @@ -11,17 +11,18 @@ #include "unicornlua/engine.hpp" #include "unicornlua/lua.hpp" -extern const char* const kContextMetatableName; +extern const char *const kContextMetatableName; extern const luaL_Reg kContextMetamethods[]; extern const luaL_Reg kContextInstanceMethods[]; -struct Context { - uc_context* context_handle; - UCLuaEngine* engine; +struct Context +{ + uc_context *context_handle; + UCLuaEngine *engine; }; -int ul_context_save(lua_State* L); -int ul_context_restore(lua_State* L); +int ul_context_save(lua_State *L); +int ul_context_restore(lua_State *L); /** Deallocate a context object. * @@ -29,12 +30,12 @@ int ul_context_restore(lua_State* L); * calls `uc_context_free()` on 1.0.2+. In either case, it will behave as * expected. */ -int ul_context_free(lua_State* L); +int ul_context_free(lua_State *L); /** * Like @ref ul_context_free, except if the context is closed, it does nothing * instead of throwing an exception. */ -int ul_context_maybe_free(lua_State* L); +int ul_context_maybe_free(lua_State *L); -Context* ul_toluacontext(lua_State* L, int index); +Context *ul_toluacontext(lua_State *L, int index); diff --git a/include/unicornlua/control_functions.hpp b/include/unicornlua/control_functions.hpp index 16e11455..12887da4 100644 --- a/include/unicornlua/control_functions.hpp +++ b/include/unicornlua/control_functions.hpp @@ -2,38 +2,38 @@ #include "lua.hpp" -[[noreturn]] int ul_crash_unsupported_operation(lua_State* L); +[[noreturn]] int ul_crash_unsupported_operation(lua_State *L); #if UC_API_MAJOR >= 2 -int ul_ctl_exits_disable(lua_State* L); -int ul_ctl_exits_enable(lua_State* L); -int ul_ctl_flush_tlb(lua_State* L); -int ul_ctl_get_arch(lua_State* L); -int ul_ctl_get_cpu_model(lua_State* L); -int ul_ctl_get_exits(lua_State* L); -int ul_ctl_get_exits_cnt(lua_State* L); -int ul_ctl_get_mode(lua_State* L); -int ul_ctl_get_page_size(lua_State* L); -int ul_ctl_get_timeout(lua_State* L); -int ul_ctl_remove_cache(lua_State* L); -int ul_ctl_request_cache(lua_State* L); -int ul_ctl_set_cpu_model(lua_State* L); -int ul_ctl_set_exits(lua_State* L); -int ul_ctl_set_page_size(lua_State* L); +int ul_ctl_exits_disable(lua_State *L); +int ul_ctl_exits_enable(lua_State *L); +int ul_ctl_flush_tlb(lua_State *L); +int ul_ctl_get_arch(lua_State *L); +int ul_ctl_get_cpu_model(lua_State *L); +int ul_ctl_get_exits(lua_State *L); +int ul_ctl_get_exits_cnt(lua_State *L); +int ul_ctl_get_mode(lua_State *L); +int ul_ctl_get_page_size(lua_State *L); +int ul_ctl_get_timeout(lua_State *L); +int ul_ctl_remove_cache(lua_State *L); +int ul_ctl_request_cache(lua_State *L); +int ul_ctl_set_cpu_model(lua_State *L); +int ul_ctl_set_exits(lua_State *L); +int ul_ctl_set_page_size(lua_State *L); #else -#define ul_ctl_exits_disable ul_crash_unsupported_operation -#define ul_ctl_exits_enable ul_crash_unsupported_operation -#define ul_ctl_flush_tlb ul_crash_unsupported_operation -#define ul_ctl_get_arch ul_crash_unsupported_operation -#define ul_ctl_get_cpu_model ul_crash_unsupported_operation -#define ul_ctl_get_exits ul_crash_unsupported_operation -#define ul_ctl_get_exits_cnt ul_crash_unsupported_operation -#define ul_ctl_get_mode ul_crash_unsupported_operation -#define ul_ctl_get_page_size ul_crash_unsupported_operation -#define ul_ctl_get_timeout ul_crash_unsupported_operation -#define ul_ctl_remove_cache ul_crash_unsupported_operation -#define ul_ctl_request_cache ul_crash_unsupported_operation -#define ul_ctl_set_cpu_model ul_crash_unsupported_operation -#define ul_ctl_set_exits ul_crash_unsupported_operation -#define ul_ctl_set_page_size ul_crash_unsupported_operation +# define ul_ctl_exits_disable ul_crash_unsupported_operation +# define ul_ctl_exits_enable ul_crash_unsupported_operation +# define ul_ctl_flush_tlb ul_crash_unsupported_operation +# define ul_ctl_get_arch ul_crash_unsupported_operation +# define ul_ctl_get_cpu_model ul_crash_unsupported_operation +# define ul_ctl_get_exits ul_crash_unsupported_operation +# define ul_ctl_get_exits_cnt ul_crash_unsupported_operation +# define ul_ctl_get_mode ul_crash_unsupported_operation +# define ul_ctl_get_page_size ul_crash_unsupported_operation +# define ul_ctl_get_timeout ul_crash_unsupported_operation +# define ul_ctl_remove_cache ul_crash_unsupported_operation +# define ul_ctl_request_cache ul_crash_unsupported_operation +# define ul_ctl_set_cpu_model ul_crash_unsupported_operation +# define ul_ctl_set_exits ul_crash_unsupported_operation +# define ul_ctl_set_page_size ul_crash_unsupported_operation #endif diff --git a/include/unicornlua/engine.hpp b/include/unicornlua/engine.hpp index 2888a4ac..3bd7e8c3 100644 --- a/include/unicornlua/engine.hpp +++ b/include/unicornlua/engine.hpp @@ -14,14 +14,15 @@ #include "unicornlua/lua.hpp" #include "unicornlua/utils.hpp" -extern const char* const kEngineMetatableName; -extern const char* const kEnginePointerMapName; +extern const char *const kEngineMetatableName; +extern const char *const kEnginePointerMapName; struct Context; -class UCLuaEngine { -public: - UCLuaEngine(lua_State* L, uc_engine* engine); +class UCLuaEngine +{ + public: + UCLuaEngine(lua_State *L, uc_engine *engine); ~UCLuaEngine(); /** @@ -32,10 +33,10 @@ class UCLuaEngine { * together yet. It's used in only one specific case so this function may * eventually be removed. */ - Hook* create_empty_hook(); + Hook *create_empty_hook(); /** Detach and destroy a hook bound to this engine. */ - void remove_hook(Hook* hook); + void remove_hook(Hook *hook); /** * Create a Context object in memory managed by Lua, and push it on the Lua @@ -50,25 +51,25 @@ class UCLuaEngine { * context. Before, it was necessary to call `update()` on the returned * context object. */ - Context* create_context_in_lua(); - void update_context(Context* context) const; - void restore_from_context(Context* context); - void free_context(Context* context); + Context *create_context_in_lua(); + void update_context(Context *context) const; + void restore_from_context(Context *context); + void free_context(Context *context); void start(uint64_t start_addr, uint64_t end_addr, uint64_t timeout = 0, - size_t n_instructions = 0); + size_t n_instructions = 0); void stop(); void close(); size_t query(uc_query_type query_type) const; uc_err get_errno() const; - uc_engine* get_handle() const noexcept; + uc_engine *get_handle() const noexcept; -private: - lua_State* L_; - uc_engine* engine_handle_; - std::set hooks_; - std::set contexts_; + private: + lua_State *L_; + uc_engine *engine_handle_; + std::set hooks_; + std::set contexts_; }; /** @@ -77,7 +78,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_find_lua_engine(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. @@ -86,7 +87,7 @@ void ul_find_lua_engine(lua_State* L, const uc_engine* engine); * * @param L A pointer to the current Lua state. */ -void ul_init_engines_lib(lua_State* L); +void ul_init_engines_lib(lua_State *L); /** * Return the value on the stack at @a index as a uc_engine pointer. @@ -99,7 +100,7 @@ void ul_init_engines_lib(lua_State* L); * * @return The low-level Unicorn engine. */ -uc_engine* ul_toengine(lua_State* L, int index); +uc_engine *ul_toengine(lua_State *L, int index); /** * Return the value on the stack at @a index as a pointer to a @ref UCLuaEngine. @@ -112,10 +113,10 @@ uc_engine* ul_toengine(lua_State* L, int index); * * @return The engine. */ -UCLuaEngine* ul_toluaengine(lua_State* L, int index); +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); +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); diff --git a/include/unicornlua/errors.hpp b/include/unicornlua/errors.hpp index c3f64c5d..ae3cd7ae 100644 --- a/include/unicornlua/errors.hpp +++ b/include/unicornlua/errors.hpp @@ -15,15 +15,16 @@ /** * Exception class for translating Unicorn error codes into C++ exceptions. */ -class UnicornLibraryError : public std::runtime_error { -public: +class UnicornLibraryError : public std::runtime_error +{ + public: explicit UnicornLibraryError(uc_err error); /** Return the Unicorn error code that triggered this exception. */ uc_err get_error() const noexcept; - void rethrow_as_lua_error(lua_State* L); + void rethrow_as_lua_error(lua_State *L); -private: + private: uc_err error_; }; @@ -35,8 +36,9 @@ class UnicornLibraryError : public std::runtime_error { * wrong with the glue code, such as when Lua passes the wrong kind of argument * to a function. */ -class LuaBindingError : public std::runtime_error { -public: - explicit LuaBindingError(const char* message); - void rethrow_as_lua_error(lua_State* L); +class LuaBindingError : public std::runtime_error +{ + public: + explicit LuaBindingError(const char *message); + void rethrow_as_lua_error(lua_State *L); }; diff --git a/include/unicornlua/hooks.hpp b/include/unicornlua/hooks.hpp index 7d47dab0..15ae55e0 100644 --- a/include/unicornlua/hooks.hpp +++ b/include/unicornlua/hooks.hpp @@ -8,19 +8,20 @@ #include "unicornlua/lua.hpp" -class Hook { +class Hook +{ friend class UCLuaEngine; -protected: - Hook(lua_State* L, uc_engine* engine); - Hook(lua_State* L, uc_engine* engine, uc_hook hook_handle, - int callback_func_ref = LUA_NOREF, int user_data_ref = LUA_REFNIL); + protected: + Hook(lua_State *L, uc_engine *engine); + Hook(lua_State *L, uc_engine *engine, uc_hook hook_handle, + int callback_func_ref = LUA_NOREF, int user_data_ref = LUA_REFNIL); -public: + public: ~Hook() noexcept(false); - uc_engine* engine() noexcept; - lua_State* L() noexcept; + uc_engine *engine() noexcept; + lua_State *L() noexcept; uc_hook get_hook_handle() const noexcept; void set_hook_handle(uc_hook hook_handle) noexcept; @@ -32,9 +33,9 @@ class Hook { int get_user_data() const noexcept; void push_user_data(); -private: - lua_State* L_; ///< The Lua state used by this hook. - uc_engine* engine_; ///< The engine this hook is bound to. + private: + lua_State *L_; ///< The Lua state used by this hook. + uc_engine *engine_; ///< The engine this hook is bound to. uc_hook hook_handle_; ///< The hook handle used by Unicorn. /** @@ -80,7 +81,7 @@ class Hook { * my_data) * ``` */ -int ul_hook_add(lua_State* L); +int ul_hook_add(lua_State *L); /** * Delete a hook. Implements `engine:hook_del()`. @@ -92,4 +93,4 @@ int ul_hook_add(lua_State* L); * * Nothing is returned. */ -int ul_hook_del(lua_State* L); +int ul_hook_del(lua_State *L); diff --git a/include/unicornlua/integer_conversions.hpp b/include/unicornlua/integer_conversions.hpp index c6856e10..1fd4e785 100644 --- a/include/unicornlua/integer_conversions.hpp +++ b/include/unicornlua/integer_conversions.hpp @@ -4,7 +4,8 @@ #include #include -// We know lua_Integer is always at least 32 bits, so we don't need any checks here. +// We know lua_Integer is always at least 32 bits, so we don't need any checks +// here. #define ul_lua_int32_t_equiv_type lua_Integer #define ul_push_int32_t_equiv(L, i) lua_pushinteger((L), static_cast(i)) #define ul_to_int32_t_equiv(L, i) static_cast(lua_tointeger((L), (i))) @@ -14,9 +15,10 @@ #define ul_to_int_equiv(L, i) static_cast(lua_tointeger((L), (i))) /* - * The following are default configurations and definitions for Lua integers. Note that - * for Lua 5.1 and 5.2, `lua_Integer` can be redefined to any type. In 5.3+, it must be - * one of `long long`, `long`, or `int`; ptrdiff_t is no longer an option. + * The following are default configurations and definitions for Lua integers. + * Note that for Lua 5.1 and 5.2, `lua_Integer` can be redefined to any type. + * In 5.3+, it must be one of `long long`, `long`, or `int`; ptrdiff_t is no + * longer an option. * * Lua 5.1: * - lua_Integer: ptrdiff_t @@ -30,7 +32,8 @@ * - Defines LUA_MAXUNSIGNED: No * Lua 5.3+: * - lua_Integer: - * - 32-bit builds: `int` or `long`, whichever is smallest and >=32 bits + * - 32-bit builds: `int` or `long`, whichever is smallest and >=32 + * bits * - 64-bit builds: long long * - lua_Unsigned: unsigned of lua_Integer type * - Defines LUA_MAXINTEGER: Yes @@ -59,9 +62,9 @@ # endif # if LUA_MAXINTEGER >= SIZE_MAX -// A regular Lua integer can safely hold a size_t. This will only ever be the case -// on systems where a size_t cannot hold a pointer, e.g. a 64-bit system with a -// 32-bit size_t. This is uncommon but does exist. +// A regular Lua integer can safely hold a size_t. This will only ever be the +// case on systems where a size_t cannot hold a pointer, e.g. a 64-bit system +// with a 32-bit size_t. This is uncommon but does exist. # define ul_lua_size_t_equiv_type lua_Unsigned # define ul_push_size_t_equiv(L, i) \ lua_pushinteger((L), static_cast(i)) @@ -69,20 +72,22 @@ # endif #endif // LUA_MAXINTEGER -// Lua 5.1 and 5.2 use ptrdiff_t as their default integer type. On a 64-bit system this -// *might* be able to hold a 32-bit unsigned integer. Technically, ptrdiff_t only needs to -// be able to hold pointer arithmetic between pointers in the same array. If on a 64-bit -// system malloc() will only let you allocate 4GiB, ptrdiff_t might be a 32-bit type. +// Lua 5.1 and 5.2 use ptrdiff_t as their default integer type. On a 64-bit +// system this *might* be able to hold a 32-bit unsigned integer. Technically, +// ptrdiff_t only needs to be able to hold pointer arithmetic between pointers +// in the same array. If on a 64-bit system malloc() will only let you allocate +// 4GiB, ptrdiff_t might be a 32-bit type. // -// Of course, this assumes that whoever built Lua didn't change it to a type smaller than -// ptrdiff_t. +// Of course, this assumes that whoever built Lua didn't change it to a type +// smaller than ptrdiff_t. #if (LUA_VERSION_NUM == 501 || LUA_VERSION_NUM == 502) && (PTRDIFF_MAX >= UINT32_MAX) # define ul_lua_uint32_t_equiv_type lua_Integer # define ul_push_uint32_t_equiv(L, i) lua_pushinteger((L), static_cast(i)) # define ul_to_uint32_t_equiv(L, i) static_cast(lua_tointeger((L), (i))) #endif -// Fallbacks for anything not defined as an integer -------------------------------------- +// Fallbacks for anything not defined as an integer +// -------------------------------------- #ifndef ul_lua_uint32_t_equiv_type # define ul_lua_uint32_t_equiv_type lua_Number diff --git a/include/unicornlua/lua.hpp b/include/unicornlua/lua.hpp index f3bb2829..5b043c15 100644 --- a/include/unicornlua/lua.hpp +++ b/include/unicornlua/lua.hpp @@ -4,7 +4,8 @@ * @file lua.h */ -extern "C" { +extern "C" +{ #include #include } diff --git a/include/unicornlua/memory.hpp b/include/unicornlua/memory.hpp index bfb37a70..28167ceb 100644 --- a/include/unicornlua/memory.hpp +++ b/include/unicornlua/memory.hpp @@ -11,14 +11,14 @@ /** * Write data to a location in a machine's memory. */ -int ul_mem_write(lua_State* L); +int ul_mem_write(lua_State *L); /** * Read data from a location in a machine's memory. */ -int ul_mem_read(lua_State* L); +int ul_mem_read(lua_State *L); -int ul_mem_map(lua_State* L); -int ul_mem_unmap(lua_State* L); -int ul_mem_protect(lua_State* L); -int ul_mem_regions(lua_State* L); +int ul_mem_map(lua_State *L); +int ul_mem_unmap(lua_State *L); +int ul_mem_protect(lua_State *L); +int ul_mem_regions(lua_State *L); diff --git a/include/unicornlua/register_template_functions.hpp b/include/unicornlua/register_template_functions.hpp index 77d7a726..bb1ede8e 100644 --- a/include/unicornlua/register_template_functions.hpp +++ b/include/unicornlua/register_template_functions.hpp @@ -10,24 +10,26 @@ #include "registers.hpp" template -void integer_array_to_table(lua_State* L, const std::array& arr) +void integer_array_to_table(lua_State *L, const std::array &arr) { static_assert(N > 0 && N <= 128, "Array length must be in [1, 128]"); lua_createtable(L, N, 0); - for (size_t i = 0; i < N; ++i) { + for (size_t i = 0; i < N; ++i) + { lua_pushinteger(L, arr[i]); lua_seti(L, -2, static_cast(i) + 1); } } template -void float_array_to_table(lua_State* L, const std::array& arr) +void float_array_to_table(lua_State *L, const std::array &arr) { static_assert(N > 0 && N <= 128, "Array length must be in [1, 128]"); lua_createtable(L, N, 0); - for (size_t i = 0; i < N; ++i) { + for (size_t i = 0; i < N; ++i) + { lua_pushnumber(L, arr[i]); lua_seti(L, -2, static_cast(i) + 1); } @@ -35,7 +37,7 @@ void float_array_to_table(lua_State* L, const std::array& arr) template std::array Register::array_cast() const { - std::array value {}; + std::array value{}; memcpy(value.data(), data_, sizeof(value)); return value; } @@ -60,8 +62,8 @@ template T try_cast(lua_Integer value) // If we get here then `value` isn't representable as a T. auto buf = std::ostringstream(); - buf << "Numeric value out of range: " << value << " is not within [" << min - << ", " << max << "]."; + buf << "Numeric value out of range: " << value << " is not within [" << min << ", " + << max << "]."; throw std::domain_error(buf.str()); } @@ -74,12 +76,11 @@ template T try_cast(lua_Integer value) * @param value_index * @param buffer */ -template -void write_lua_integer(lua_State* L, int value_index, void* buffer) +template void write_lua_integer(lua_State *L, int value_index, void *buffer) { lua_Integer lua_int = lua_tointeger(L, value_index); T native_value = try_cast(lua_int); - *reinterpret_cast(buffer) = native_value; + *reinterpret_cast(buffer) = native_value; } /** @@ -95,12 +96,12 @@ void write_lua_integer(lua_State* L, int value_index, void* buffer) * @param buffer */ template -void write_lua_integer_array( - lua_State* L, int table_index, int n_elements, void* buffer) +void write_lua_integer_array(lua_State *L, int table_index, int n_elements, void *buffer) { - for (int i = 0; i < n_elements; ++i) { + for (int i = 0; i < n_elements; ++i) + { lua_geti(L, table_index, i + 1); - write_lua_integer(L, -1, reinterpret_cast(buffer) + i); + write_lua_integer(L, -1, reinterpret_cast(buffer) + i); lua_pop(L, 1); } } diff --git a/include/unicornlua/registers.hpp b/include/unicornlua/registers.hpp index 80fff9de..030e2798 100644 --- a/include/unicornlua/registers.hpp +++ b/include/unicornlua/registers.hpp @@ -15,51 +15,52 @@ #include "unicornlua/register_types.hpp" #if FLT_RADIX != 2 -#error "Can't handle floating-point radixes other than 2 right now." +# error "Can't handle floating-point radixes other than 2 right now." #endif #if FLT_MANT_DIG == 24 using uclua_float32 = float; #else -#error "`float` isn't 32 bits. This library can't handle that yet." +# error "`float` isn't 32 bits. This library can't handle that yet." #endif #if DBL_MANT_DIG == 53 using uclua_float64 = double; #else -#error "`double` isn't 64 bits. This library can't handle that yet." +# error "`double` isn't 64 bits. This library can't handle that yet." #endif #if defined(__GNUC__) && defined(__x86__) && defined(USE_FLOAT128) -#define UCLUA_HAVE_GNUFLOAT128 +# define UCLUA_HAVE_GNUFLOAT128 #endif #if defined(LDBL_MANT_DIG) using uclua_float80 = long double; -#if LDBL_MANT_DIG == 64 +# if LDBL_MANT_DIG == 64 // `long double` is 80 bits on this platform. -#define UCLUA_FLOAT80_SIZE 80 -#elif LDBL_MANT_DIG == 113 +# define UCLUA_FLOAT80_SIZE 80 +# elif LDBL_MANT_DIG == 113 // `long double` is 128 bits on this platform. -#define UCLUA_FLOAT80_SIZE 128 -#else +# define UCLUA_FLOAT80_SIZE 128 +# else // No idea how big a `long double` is but it's biiiig. -#define UCLUA_FLOAT80_SIZE 0 -#endif +# define UCLUA_FLOAT80_SIZE 0 +# endif #elif defined(UCLUA_HAVE_GNUFLOAT128) // Platform doesn't support `long double` but does support __float128. -#define UCLUA_FLOAT80_SIZE 128 +# define UCLUA_FLOAT80_SIZE 128 using uclua_float80 = __float128; #else -#error "Platform has no way to represent 80-bit floating-point registers." +# error "Platform has no way to represent 80-bit floating-point registers." #endif -class Register { -public: +class Register +{ + public: Register(); - Register(const void* buffer, RegisterDataType kind); - static Register from_lua(lua_State* L, int value_index, int kind_index); + Register(const void *buffer, RegisterDataType kind); + static Register from_lua(lua_State *L, int value_index, int kind_index); /** * Assign a value to this register from memory. @@ -67,7 +68,7 @@ class Register { * @param buffer The raw data to assign to this register. * @param kind An indication of the type of data this register holds. */ - void assign_value(const void* buffer, RegisterDataType kind); + void assign_value(const void *buffer, RegisterDataType kind); /** * Get the type of the data stored in this register. @@ -279,14 +280,14 @@ class Register { */ std::array as_8xi64() const; - void push_to_lua(lua_State* L) const; + void push_to_lua(lua_State *L) const; /** * The raw data. */ uint8_t data_[64]; -private: + private: template std::array array_cast() const; RegisterDataType kind_; @@ -304,10 +305,10 @@ using register_buffer_type = uint8_t[64]; /** * Write to an architecture register. */ -int ul_reg_write(lua_State* L); -int ul_reg_read(lua_State* L); -int ul_reg_write_batch(lua_State* L); -int ul_reg_read_batch(lua_State* L); +int ul_reg_write(lua_State *L); +int ul_reg_read(lua_State *L); +int ul_reg_write_batch(lua_State *L); +int ul_reg_read_batch(lua_State *L); /** * Read a register from the processor, as something other than as a plain @@ -316,7 +317,7 @@ int ul_reg_read_batch(lua_State* L); * You'll need to use this for reading registers that aren't integers, or for * SSE/AVX/AVX-512 registers that can act as arrays of values. */ -int ul_reg_read_as(lua_State* L); +int ul_reg_read_as(lua_State *L); /** * Like @ref ul_reg_read_as, but reads multiple registers at once. @@ -324,7 +325,7 @@ int ul_reg_read_as(lua_State* L); * The argument to the Lua function is a table mapping the ID of the register to * read to the format it should be read in. */ -int ul_reg_read_batch_as(lua_State* L); +int ul_reg_read_batch_as(lua_State *L); /** * Write to a processor register as something other than as a plain integer. @@ -332,7 +333,7 @@ int ul_reg_read_batch_as(lua_State* L); * You'll need to use this for writing registers that aren't integers, or for * SSE/AVX/AVX-512 registers that can act as arrays of values. */ -int ul_reg_write_as(lua_State* L); +int ul_reg_write_as(lua_State *L); /** * Read an x87 floating-point number as the host machine's native format. @@ -340,7 +341,7 @@ int ul_reg_write_as(lua_State* L); * @warning There's no way to represent a signaling or "indefinite" NaN in C++. * Both of these values are returned as std::NAN. */ -lua_Number read_float80(const uint8_t* data); +lua_Number read_float80(const uint8_t *data); /** * Store a floating-point value into an x87 floating-point number. @@ -352,4 +353,4 @@ lua_Number read_float80(const uint8_t* data); * @warning No distinction is made between quiet and signaling NaNs. All NaNs * are stored in memory as a quiet NaN. */ -void write_float80(lua_Number value, uint8_t* buffer); +void write_float80(lua_Number value, uint8_t *buffer); diff --git a/include/unicornlua/transaction.hpp b/include/unicornlua/transaction.hpp index f06a6c75..e763bd54 100644 --- a/include/unicornlua/transaction.hpp +++ b/include/unicornlua/transaction.hpp @@ -3,7 +3,7 @@ #include #if UC_API_MAJOR >= 2 -#include "unicornlua/lua.hpp" +# include "unicornlua/lua.hpp" /** * Create a Lua table representation of a transaction block and push it to the @@ -12,6 +12,6 @@ * @param L * @param block */ -void create_table_from_transaction_block(lua_State* L, const uc_tb* block); +void create_table_from_transaction_block(lua_State *L, const uc_tb *block); #endif // UC_API_MAJOR diff --git a/include/unicornlua/unicornlua.hpp b/include/unicornlua/unicornlua.hpp index a4f93873..1f14aa1a 100644 --- a/include/unicornlua/unicornlua.hpp +++ b/include/unicornlua/unicornlua.hpp @@ -37,8 +37,7 @@ * // Executes on versions 1.1.0 and above (including 1.1.1) * #endif */ -#define MAKE_VERSION(major, minor, patch) \ - (((major) << 16) | ((minor) << 8) | (patch)) +#define MAKE_VERSION(major, minor, patch) (((major) << 16) | ((minor) << 8) | (patch)) /** * The full version number of this Lua library, as an integer. @@ -48,9 +47,9 @@ * the minor version is the 8 bits below that, and the patch number is below * that. Thus, release 1.10.16 would be represented by 0x010A10. */ -#define UNICORNLUA_VERSION \ - MAKE_VERSION(UNICORNLUA_VERSION_MAJOR, UNICORNLUA_VERSION_MINOR, \ - UNICORNLUA_VERSION_PATCH) +#define UNICORNLUA_VERSION \ + MAKE_VERSION(UNICORNLUA_VERSION_MAJOR, UNICORNLUA_VERSION_MINOR, \ + UNICORNLUA_VERSION_PATCH) /** * The full version number of the Unicorn library this was compiled with, as an @@ -59,5 +58,5 @@ * The construction and semantics of this version number are the same as in * @ref UNICORNLUA_VERSION. */ -#define UNICORNLUA_UNICORN_MAJOR_MINOR_PATCH \ +#define UNICORNLUA_UNICORN_MAJOR_MINOR_PATCH \ MAKE_VERSION(UC_VERSION_MAJOR, UC_VERSION_MINOR, UC_VERSION_EXTRA) diff --git a/include/unicornlua/utils.hpp b/include/unicornlua/utils.hpp index 2fbd1fc9..d4bc1315 100644 --- a/include/unicornlua/utils.hpp +++ b/include/unicornlua/utils.hpp @@ -22,7 +22,7 @@ * @note Like lua_error, this function never returns, and should be treated in * exactly the same way. */ -[[noreturn]] void ul_crash_on_error(lua_State* L, uc_err error); +[[noreturn]] void ul_crash_on_error(lua_State *L, uc_err error); /** * Create a new weak table with the given key mode, and push it onto the stack. @@ -31,14 +31,15 @@ * @param mode The table mode to use. See the Lua documentation for a full * description of valid modes and how they work. */ -void ul_create_weak_table(lua_State* L, const char* mode); +void ul_create_weak_table(lua_State *L, const char *mode); -struct NamedIntConst { - const char* name; +struct NamedIntConst +{ + const char *name; lua_Integer value; }; -void load_int_constants(lua_State* L, const struct NamedIntConst* constants); +void load_int_constants(lua_State *L, const struct NamedIntConst *constants); /** * Count the number of items in the table. @@ -46,25 +47,25 @@ void load_int_constants(lua_State* L, const struct NamedIntConst* constants); * `luaL_len()` only returns the number of entries in the array part of a table, * so this function iterates through the entirety of the table and returns the * result. */ -size_t count_table_elements(lua_State* L, int table_index); +size_t count_table_elements(lua_State *L, int table_index); // Define a cross-platform marker for telling the compiler we're deliberately // falling through to the next case in a switch statement. #if __STDC_VERSION__ >= 201603L -#define UL_FALLTHROUGH_MARKER [[fallthrough]] +# define UL_FALLTHROUGH_MARKER [[fallthrough]] #elif defined(__GNUC__) -#define UL_FALLTHROUGH_MARKER __attribute__((fallthrough)) +# define UL_FALLTHROUGH_MARKER __attribute__((fallthrough)) #else // MSVC -#define UL_FALLTHROUGH_MARKER +# define UL_FALLTHROUGH_MARKER #endif #if defined(__cpp_lib_unreachable) -#define UL_UNREACHABLE_MARKER std::unreachable() +# define UL_UNREACHABLE_MARKER std::unreachable() #elif defined(__GNUC__) // GCC, Clang, ICC -#define UL_UNREACHABLE_MARKER __builtin_unreachable() +# define UL_UNREACHABLE_MARKER __builtin_unreachable() #elif defined(_MSC_VER) // MSVC -#define UL_UNREACHABLE_MARKER __assume(false) +# define UL_UNREACHABLE_MARKER __assume(false) #else -#define UL_UNREACHABLE_MARKER +# define UL_UNREACHABLE_MARKER #endif diff --git a/src/compat.cpp b/src/compat.cpp index f2b837ed..e80f1919 100644 --- a/src/compat.cpp +++ b/src/compat.cpp @@ -7,20 +7,20 @@ * This backport is (probably) functionally equivalent to the 5.3 behavior but * is almost certainly not as fast as the implementation in 5.3. */ -LUA_API void lua_seti(lua_State* L, int index, lua_Integer n) +LUA_API void lua_seti(lua_State *L, int index, lua_Integer n) { int table_index = lua_absindex(L, index); int value_index = lua_gettop(L); // Because lua_settable expects the value on top, we push the key (n) and // then another copy of the value, ignoring the original value. - lua_pushinteger(L, n); // Push key, stack is [... V K] - lua_insert(L, value_index); // Move key under value [... K V] + lua_pushinteger(L, n); // Push key, stack is [... V K] + lua_insert(L, value_index); // Move key under value [... K V] lua_settable(L, table_index); // Set the table value, stack is clean } /* Same caveat as above. */ -LUA_API int lua_geti(lua_State* L, int index, lua_Integer i) +LUA_API int lua_geti(lua_State *L, int index, lua_Integer i) { index = lua_absindex(L, index); @@ -34,28 +34,28 @@ LUA_API int lua_geti(lua_State* L, int index, lua_Integer i) /* Compatibility stuff for Lua < 5.2 */ #if LUA_VERSION_NUM < 502 -#ifdef _MSC_VER +# ifdef _MSC_VER // This complains about potentially-throwing functions being passed to a // function declared with `extern "C"`. I'm loath to modify copied and pasted // code I don't understand, so we're going to ignore this instead of fixing // it. -#pragma warning(push) -#pragma warning(disable : 5039) -#endif +# pragma warning(push) +# pragma warning(disable : 5039) +# endif /* This is an exact copy of the 5.3 implementation. */ -LUALIB_API void luaL_setmetatable(lua_State* L, const char* tname) +LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname) { luaL_getmetatable(L, tname); lua_setmetatable(L, -2); } -LUA_API void lua_len(lua_State* L, int index) +LUA_API void lua_len(lua_State *L, int index) { lua_pushinteger(L, (lua_Integer)lua_objlen(L, index)); } -LUA_API int luaL_len(lua_State* L, int index) +LUA_API int luaL_len(lua_State *L, int index) { lua_Integer length; index = lua_absindex(L, index); @@ -67,10 +67,11 @@ LUA_API int luaL_len(lua_State* L, int index) } /* Copied and pasted from the Lua 5.3 implementation. */ -LUALIB_API void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup) +LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) { luaL_checkstack(L, nup, "too many upvalues"); - for (; l->name != nullptr; l++) { /* fill the table with given functions */ + for (; l->name != nullptr; l++) + { /* fill the table with given functions */ int i; for (i = 0; i < nup; i++) /* copy upvalues to the top */ lua_pushvalue(L, -nup); @@ -80,7 +81,7 @@ LUALIB_API void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup) lua_pop(L, nup); /* remove upvalues */ } -LUA_API int lua_absindex(lua_State* L, int index) +LUA_API int lua_absindex(lua_State *L, int index) { int top = lua_gettop(L); @@ -89,18 +90,18 @@ LUA_API int lua_absindex(lua_State* L, int index) return index + top + 1; } -LUA_API void lua_rawsetp(lua_State* L, int index, const void* p) +LUA_API void lua_rawsetp(lua_State *L, int index, const void *p) { index = lua_absindex(L, index); - lua_pushlightuserdata(L, (void*)p); // Push key, stack is [ ... V K ] - lua_pushvalue(L, -2); // Push value, stack is [ ... V K V ] - lua_rawset(L, index); // Set table, stack is [ ... V ] + lua_pushlightuserdata(L, (void *)p); // Push key, stack is [ ... V K ] + lua_pushvalue(L, -2); // Push value, stack is [ ... V K V ] + lua_rawset(L, index); // Set table, stack is [ ... V ] lua_pop(L, 1); // Remove extra value, stack is back to how it was. } -#ifdef _MSC_VER -#pragma warning(pop) -#endif +# ifdef _MSC_VER +# pragma warning(pop) +# endif #endif diff --git a/src/context.cpp b/src/context.cpp index 105bffbd..e1e821b0 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -3,30 +3,33 @@ #include "unicornlua/errors.hpp" #include "unicornlua/lua.hpp" -const char* const kContextMetatableName = "unicornlua__context_meta"; +const char *const kContextMetatableName = "unicornlua__context_meta"; -constexpr luaL_Reg kContextMetamethods[] = { { "__gc", ul_context_maybe_free }, - { "__close", ul_context_maybe_free }, { nullptr, nullptr } }; +constexpr luaL_Reg kContextMetamethods[] = {{"__gc", ul_context_maybe_free}, + {"__close", ul_context_maybe_free}, + {nullptr, nullptr}}; -constexpr luaL_Reg kContextInstanceMethods[] - = { { "free", ul_context_free }, { nullptr, nullptr } }; +constexpr luaL_Reg kContextInstanceMethods[] = {{"free", ul_context_free}, + {nullptr, nullptr}}; -Context* ul_toluacontext(lua_State* L, int index) +Context *ul_toluacontext(lua_State *L, int index) { - return reinterpret_cast( - luaL_checkudata(L, index, kContextMetatableName)); + return reinterpret_cast(luaL_checkudata(L, index, kContextMetatableName)); } -int ul_context_save(lua_State* L) +int ul_context_save(lua_State *L) { - UCLuaEngine* engine = ul_toluaengine(L, 1); + UCLuaEngine *engine = ul_toluaengine(L, 1); - if (lua_gettop(L) < 2) { + if (lua_gettop(L) < 2) + { // Caller didn't provide a context, create a new one and push it to the // stack so we can return it to the caller. engine->create_context_in_lua(); - } else { - Context* context = ul_toluacontext(L, 2); + } + else + { + Context *context = ul_toluacontext(L, 2); if (context->context_handle == nullptr) throw LuaBindingError("Cannot update a closed context."); @@ -35,17 +38,17 @@ int ul_context_save(lua_State* L) return 1; } -int ul_context_restore(lua_State* L) +int ul_context_restore(lua_State *L) { - UCLuaEngine* engine = ul_toluaengine(L, 1); - Context* context = ul_toluacontext(L, 2); + UCLuaEngine *engine = ul_toluaengine(L, 1); + Context *context = ul_toluacontext(L, 2); engine->restore_from_context(context); return 0; } -int ul_context_free(lua_State* L) +int ul_context_free(lua_State *L) { - Context* context = ul_toluacontext(L, 1); + Context *context = ul_toluacontext(L, 1); if (context->context_handle == nullptr) throw LuaBindingError("Attempted to free the same context twice."); @@ -58,9 +61,9 @@ int ul_context_free(lua_State* L) return 0; } -int ul_context_maybe_free(lua_State* L) +int ul_context_maybe_free(lua_State *L) { - Context* context = ul_toluacontext(L, 1); + Context *context = ul_toluacontext(L, 1); // Do nothing if the context has already been freed. if (context->context_handle != nullptr) diff --git a/src/control_functions.cpp b/src/control_functions.cpp index da70a744..2d98af0a 100644 --- a/src/control_functions.cpp +++ b/src/control_functions.cpp @@ -3,26 +3,25 @@ #include "unicornlua/lua.hpp" -[[noreturn]] int ul_crash_unsupported_operation(lua_State* L) +[[noreturn]] int ul_crash_unsupported_operation(lua_State *L) { (void)L; - throw std::runtime_error( - "Functionality not supported in this version of Unicorn."); + throw std::runtime_error("Functionality not supported in this version of Unicorn."); } #if UC_API_MAJOR >= 2 -#include -#include +# include +# include -#include "unicornlua/control_functions.hpp" -#include "unicornlua/engine.hpp" -#include "unicornlua/transaction.hpp" -#include "unicornlua/utils.hpp" +# include "unicornlua/control_functions.hpp" +# include "unicornlua/engine.hpp" +# include "unicornlua/transaction.hpp" +# include "unicornlua/utils.hpp" -int ul_ctl_get_exits(lua_State* L) +int ul_ctl_get_exits(lua_State *L) { - UCLuaEngine* engine = ul_toluaengine(L, 1); - uc_engine* handle = engine->get_handle(); + UCLuaEngine *engine = ul_toluaengine(L, 1); + uc_engine *handle = engine->get_handle(); size_t count; // Determine how many exit points we have registered. @@ -38,7 +37,8 @@ int ul_ctl_get_exits(lua_State* L) // Put the exit points into a Lua table. lua_createtable(L, static_cast(count), 0); - for (size_t i = 0; i < count; i++) { + for (size_t i = 0; i < count; i++) + { lua_pushinteger(L, static_cast(array.get()[i])); lua_seti(L, -1, static_cast(i)); } @@ -46,10 +46,10 @@ int ul_ctl_get_exits(lua_State* L) return 1; } -int ul_ctl_request_cache(lua_State* L) +int ul_ctl_request_cache(lua_State *L) { - UCLuaEngine* engine = ul_toluaengine(L, 1); - uc_engine* handle = engine->get_handle(); + UCLuaEngine *engine = ul_toluaengine(L, 1); + uc_engine *handle = engine->get_handle(); auto address = static_cast(lua_tointeger(L, 2)); uc_tb tblock; @@ -62,10 +62,10 @@ int ul_ctl_request_cache(lua_State* L) return 1; } -int ul_ctl_set_exits(lua_State* L) +int ul_ctl_set_exits(lua_State *L) { - UCLuaEngine* engine = ul_toluaengine(L, 1); - uc_engine* handle = engine->get_handle(); + UCLuaEngine *engine = ul_toluaengine(L, 1); + uc_engine *handle = engine->get_handle(); auto n_entries = static_cast(luaL_len(L, 2)); if (n_entries < 1) @@ -75,7 +75,8 @@ int ul_ctl_set_exits(lua_State* L) // The table argument lists all the exit points. Iterate over these, putting // them into the array we're about to pass Unicorn. - for (int i = 0; i < static_cast(n_entries); i++) { + for (int i = 0; i < static_cast(n_entries); i++) + { lua_geti(L, 2, i + 1); entries.get()[i] = static_cast(lua_tointeger(L, -1)); lua_pop(L, 1); diff --git a/src/engine.cpp b/src/engine.cpp index 52e5e47f..9749c643 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -11,62 +11,71 @@ #include "unicornlua/unicornlua.hpp" #include "unicornlua/utils.hpp" -const char* const kEngineMetatableName = "unicornlua__engine_meta"; -const char* const kEnginePointerMapName = "unicornlua__engine_ptr_map"; +const char *const kEngineMetatableName = "unicornlua__engine_meta"; +const char *const kEnginePointerMapName = "unicornlua__engine_ptr_map"; // Close the engine only if it hasn't been closed already. -static int maybe_close(lua_State* L) +static int maybe_close(lua_State *L) { - UCLuaEngine* engine_object = ul_toluaengine(L, 1); - uc_engine* engine_handle = engine_object->get_handle(); + UCLuaEngine *engine_object = ul_toluaengine(L, 1); + uc_engine *engine_handle = engine_object->get_handle(); if (engine_handle != nullptr) engine_object->close(); return 0; } -static constexpr luaL_Reg kEngineMetamethods[] = { { "__gc", maybe_close }, - { "__close", maybe_close }, { nullptr, nullptr } }; - -static constexpr luaL_Reg kEngineInstanceMethods[] = { { "close", ul_close }, - { "context_restore", ul_context_restore }, - { "context_save", ul_context_save }, - { "ctl_exits_disable", ul_ctl_exits_disable }, - { "ctl_exits_enable", ul_ctl_exits_enable }, - { "ctl_flush_tlb", ul_ctl_flush_tlb }, { "ctl_get_arch", ul_ctl_get_arch }, - { "ctl_get_cpu_model", ul_ctl_get_cpu_model }, - { "ctl_get_exits", ul_ctl_get_exits }, - { "ctl_get_exits_cnt", ul_ctl_get_exits_cnt }, - { "ctl_get_mode", ul_ctl_get_mode }, - { "ctl_get_page_size", ul_ctl_get_page_size }, - { "ctl_get_timeout", ul_ctl_get_timeout }, - { "ctl_remove_cache", ul_ctl_remove_cache }, - { "ctl_request_cache", ul_ctl_request_cache }, - { "ctl_set_cpu_model", ul_ctl_set_cpu_model }, - { "ctl_set_exits", ul_ctl_set_exits }, - { "ctl_set_page_size", ul_ctl_set_page_size }, - { "emu_start", ul_emu_start }, { "emu_stop", ul_emu_stop }, - { "errno", ul_errno }, { "hook_add", ul_hook_add }, - { "hook_del", ul_hook_del }, { "mem_map", ul_mem_map }, - { "mem_protect", ul_mem_protect }, +static constexpr luaL_Reg kEngineMetamethods[] = { + {"__gc", maybe_close}, {"__close", maybe_close}, {nullptr, nullptr}}; + +static constexpr luaL_Reg kEngineInstanceMethods[] = { + {"close", ul_close}, + {"context_restore", ul_context_restore}, + {"context_save", ul_context_save}, + {"ctl_exits_disable", ul_ctl_exits_disable}, + {"ctl_exits_enable", ul_ctl_exits_enable}, + {"ctl_flush_tlb", ul_ctl_flush_tlb}, + {"ctl_get_arch", ul_ctl_get_arch}, + {"ctl_get_cpu_model", ul_ctl_get_cpu_model}, + {"ctl_get_exits", ul_ctl_get_exits}, + {"ctl_get_exits_cnt", ul_ctl_get_exits_cnt}, + {"ctl_get_mode", ul_ctl_get_mode}, + {"ctl_get_page_size", ul_ctl_get_page_size}, + {"ctl_get_timeout", ul_ctl_get_timeout}, + {"ctl_remove_cache", ul_ctl_remove_cache}, + {"ctl_request_cache", ul_ctl_request_cache}, + {"ctl_set_cpu_model", ul_ctl_set_cpu_model}, + {"ctl_set_exits", ul_ctl_set_exits}, + {"ctl_set_page_size", ul_ctl_set_page_size}, + {"emu_start", ul_emu_start}, + {"emu_stop", ul_emu_stop}, + {"errno", ul_errno}, + {"hook_add", ul_hook_add}, + {"hook_del", ul_hook_del}, + {"mem_map", ul_mem_map}, + {"mem_protect", ul_mem_protect}, // n.b. mem_map_ptr() is irrelevant for Lua - { "mem_read", ul_mem_read }, { "mem_regions", ul_mem_regions }, - { "mem_unmap", ul_mem_unmap }, { "mem_write", ul_mem_write }, - { "query", ul_query }, { "reg_read", ul_reg_read }, - { "reg_read_as", ul_reg_read_as }, { "reg_read_batch", ul_reg_read_batch }, - { "reg_read_batch_as", ul_reg_read_batch_as }, - { "reg_write", ul_reg_write }, { "reg_write_as", ul_reg_write_as }, - { "reg_write_batch", ul_reg_write_batch }, { nullptr, nullptr } }; - -UCLuaEngine* ul_toluaengine(lua_State* L, int index) + {"mem_read", ul_mem_read}, + {"mem_regions", ul_mem_regions}, + {"mem_unmap", ul_mem_unmap}, + {"mem_write", ul_mem_write}, + {"query", ul_query}, + {"reg_read", ul_reg_read}, + {"reg_read_as", ul_reg_read_as}, + {"reg_read_batch", ul_reg_read_batch}, + {"reg_read_batch_as", ul_reg_read_batch_as}, + {"reg_write", ul_reg_write}, + {"reg_write_as", ul_reg_write_as}, + {"reg_write_batch", ul_reg_write_batch}, + {nullptr, nullptr}}; + +UCLuaEngine *ul_toluaengine(lua_State *L, int index) { - return reinterpret_cast( + return reinterpret_cast( luaL_checkudata(L, index, kEngineMetatableName)); } -UCLuaEngine::UCLuaEngine(lua_State* L, uc_engine* engine) - : L_(L) - , engine_handle_(engine) +UCLuaEngine::UCLuaEngine(lua_State *L, uc_engine *engine) : L_(L), engine_handle_(engine) { } @@ -80,24 +89,24 @@ UCLuaEngine::~UCLuaEngine() close(); } -Hook* UCLuaEngine::create_empty_hook() +Hook *UCLuaEngine::create_empty_hook() { - Hook* hook = new Hook(L_, engine_handle_); + Hook *hook = new Hook(L_, engine_handle_); hooks_.insert(hook); return hook; } -void UCLuaEngine::remove_hook(Hook* hook) +void UCLuaEngine::remove_hook(Hook *hook) { hooks_.erase(hook); delete hook; } -void UCLuaEngine::start(uint64_t start_addr, uint64_t end_addr, - uint64_t timeout, size_t n_instructions) +void UCLuaEngine::start(uint64_t start_addr, uint64_t end_addr, uint64_t timeout, + size_t n_instructions) { - uc_err error = uc_emu_start( - engine_handle_, start_addr, end_addr, timeout, n_instructions); + uc_err error = + uc_emu_start(engine_handle_, start_addr, end_addr, timeout, n_instructions); if (error != UC_ERR_OK) throw UnicornLibraryError(error); } @@ -118,7 +127,8 @@ void UCLuaEngine::close() delete hook; hooks_.clear(); - while (!contexts_.empty()) { + while (!contexts_.empty()) + { auto context = *contexts_.begin(); free_context(context); } @@ -140,15 +150,17 @@ size_t UCLuaEngine::query(uc_query_type query_type) const return result; } -uc_err UCLuaEngine::get_errno() const { return uc_errno(engine_handle_); } +uc_err UCLuaEngine::get_errno() const +{ + return uc_errno(engine_handle_); +} -Context* UCLuaEngine::create_context_in_lua() +Context *UCLuaEngine::create_context_in_lua() { // The userdata we pass back to Lua is just a pointer to the context we // created on the normal heap. We can't use a light userdata because light // userdata can't have metatables. - auto context - = reinterpret_cast(lua_newuserdata(L_, sizeof(Context))); + auto context = reinterpret_cast(lua_newuserdata(L_, sizeof(Context))); if (context == nullptr) throw std::bad_alloc(); @@ -166,25 +178,25 @@ Context* UCLuaEngine::create_context_in_lua() return context; } -void UCLuaEngine::restore_from_context(Context* context) +void UCLuaEngine::restore_from_context(Context *context) { if (context->context_handle == nullptr) throw LuaBindingError( "Attempted to use a context object that has already been freed."); if (contexts_.find(context) == contexts_.end()) - throw LuaBindingError( - "Tried to restore engine from a context it doesn't own."); + throw LuaBindingError("Tried to restore engine from a context it doesn't own."); uc_err error = uc_context_restore(engine_handle_, context->context_handle); if (error != UC_ERR_OK) throw UnicornLibraryError(error); } -void UCLuaEngine::update_context(Context* context) const +void UCLuaEngine::update_context(Context *context) const { uc_err error; - if (context->context_handle == nullptr) { + if (context->context_handle == nullptr) + { error = uc_context_alloc(engine_handle_, &context->context_handle); if (error != UC_ERR_OK) throw UnicornLibraryError(error); @@ -195,7 +207,7 @@ void UCLuaEngine::update_context(Context* context) const throw UnicornLibraryError(error); } -void UCLuaEngine::free_context(Context* context) +void UCLuaEngine::free_context(Context *context) { if (context->context_handle == nullptr) throw LuaBindingError("Attempted to remove a context object that has " @@ -221,9 +233,12 @@ void UCLuaEngine::free_context(Context* context) throw UnicornLibraryError(error); } -uc_engine* UCLuaEngine::get_handle() const noexcept { return engine_handle_; } +uc_engine *UCLuaEngine::get_handle() const noexcept +{ + return engine_handle_; +} -void ul_init_engines_lib(lua_State* L) +void ul_init_engines_lib(lua_State *L) { // Create a table with weak values where the engine pointer to engine object // mappings will be stored. @@ -247,13 +262,14 @@ void ul_init_engines_lib(lua_State* L) lua_pop(L, 2); } -void ul_find_lua_engine(lua_State* L, const uc_engine* engine) +void ul_find_lua_engine(lua_State *L, const uc_engine *engine) { lua_getfield(L, LUA_REGISTRYINDEX, kEnginePointerMapName); - lua_pushlightuserdata(L, (void*)engine); + lua_pushlightuserdata(L, (void *)engine); lua_gettable(L, -2); - if (lua_isnil(L, -1)) { + if (lua_isnil(L, -1)) + { // Remove nil and engine pointer map at TOS lua_pop(L, 2); throw LuaBindingError("No engine object is registered for the given " @@ -264,10 +280,10 @@ void ul_find_lua_engine(lua_State* L, const uc_engine* engine) lua_remove(L, -2); } -int ul_close(lua_State* L) +int ul_close(lua_State *L) { - UCLuaEngine* engine_object = ul_toluaengine(L, 1); - uc_engine* engine_handle = engine_object->get_handle(); + UCLuaEngine *engine_object = ul_toluaengine(L, 1); + uc_engine *engine_handle = engine_object->get_handle(); if (engine_handle == nullptr) return 0; @@ -275,7 +291,7 @@ int ul_close(lua_State* L) // Garbage collection should remove the engine object from the pointer map // table, but we might as well do it here anyway. lua_getfield(L, LUA_REGISTRYINDEX, kEnginePointerMapName); - lua_pushlightuserdata(L, (void*)engine_handle); + lua_pushlightuserdata(L, (void *)engine_handle); lua_pushnil(L); lua_settable(L, -3); lua_pop(L, 1); @@ -285,9 +301,9 @@ int ul_close(lua_State* L) return 0; } -int ul_query(lua_State* L) +int ul_query(lua_State *L) { - const UCLuaEngine* engine_object = ul_toluaengine(L, 1); + const UCLuaEngine *engine_object = ul_toluaengine(L, 1); auto query_type = static_cast(luaL_checkinteger(L, 2)); size_t result = engine_object->query(query_type); @@ -295,16 +311,16 @@ int ul_query(lua_State* L) return 1; } -int ul_errno(lua_State* L) +int ul_errno(lua_State *L) { - const UCLuaEngine* engine = ul_toluaengine(L, 1); + const UCLuaEngine *engine = ul_toluaengine(L, 1); lua_pushinteger(L, engine->get_errno()); return 1; } -int ul_emu_start(lua_State* L) +int ul_emu_start(lua_State *L) { - UCLuaEngine* engine = ul_toluaengine(L, 1); + UCLuaEngine *engine = ul_toluaengine(L, 1); auto start = static_cast(luaL_checkinteger(L, 2)); auto end = static_cast(luaL_checkinteger(L, 3)); auto timeout = static_cast(luaL_optinteger(L, 4, 0)); @@ -314,17 +330,17 @@ int ul_emu_start(lua_State* L) return 0; } -int ul_emu_stop(lua_State* L) +int ul_emu_stop(lua_State *L) { - UCLuaEngine* engine = ul_toluaengine(L, 1); + UCLuaEngine *engine = ul_toluaengine(L, 1); engine->stop(); return 0; } -uc_engine* ul_toengine(lua_State* L, int index) +uc_engine *ul_toengine(lua_State *L, int index) { - const UCLuaEngine* engine_object = ul_toluaengine(L, index); - uc_engine* engine_handle = engine_object->get_handle(); + const UCLuaEngine *engine_object = ul_toluaengine(L, index); + uc_engine *engine_handle = engine_object->get_handle(); if (engine_handle == nullptr) throw LuaBindingError("Attempted to use closed engine."); diff --git a/src/errors.cpp b/src/errors.cpp index c492eb95..c45214a4 100644 --- a/src/errors.cpp +++ b/src/errors.cpp @@ -6,24 +6,25 @@ #include "unicornlua/lua.hpp" UnicornLibraryError::UnicornLibraryError(uc_err error) - : std::runtime_error(uc_strerror(error)) - , error_(error) + : std::runtime_error(uc_strerror(error)), error_(error) { } -uc_err UnicornLibraryError::get_error() const noexcept { return error_; } +uc_err UnicornLibraryError::get_error() const noexcept +{ + return error_; +} -void UnicornLibraryError::rethrow_as_lua_error(lua_State* L) +void UnicornLibraryError::rethrow_as_lua_error(lua_State *L) { luaL_error(L, what()); } -LuaBindingError::LuaBindingError(const char* message) - : std::runtime_error(message) +LuaBindingError::LuaBindingError(const char *message) : std::runtime_error(message) { } -void LuaBindingError::rethrow_as_lua_error(lua_State* L) +void LuaBindingError::rethrow_as_lua_error(lua_State *L) { luaL_error(L, what()); } diff --git a/src/hooks.cpp b/src/hooks.cpp index f4b07a98..cf0e828b 100644 --- a/src/hooks.cpp +++ b/src/hooks.cpp @@ -9,24 +9,17 @@ #include "unicornlua/transaction.hpp" #include "unicornlua/utils.hpp" -Hook::Hook(lua_State* L, uc_engine* engine) - : L_(L) - , engine_(engine) - , hook_handle_(0) - , callback_func_ref_(LUA_NOREF) - , user_data_ref_(LUA_REFNIL) - , is_handle_set_(false) +Hook::Hook(lua_State *L, uc_engine *engine) + : L_(L), engine_(engine), hook_handle_(0), callback_func_ref_(LUA_NOREF), + user_data_ref_(LUA_REFNIL), is_handle_set_(false) { } -Hook::Hook(lua_State* L, uc_engine* engine, uc_hook hook_handle, - int callback_func_ref, int user_data_ref) - : L_(L) - , engine_(engine) - , hook_handle_(hook_handle) - , callback_func_ref_(callback_func_ref) - , user_data_ref_(user_data_ref) - , is_handle_set_(true) +Hook::Hook(lua_State *L, uc_engine *engine, uc_hook hook_handle, int callback_func_ref, + int user_data_ref) + : L_(L), engine_(engine), hook_handle_(hook_handle), + callback_func_ref_(callback_func_ref), user_data_ref_(user_data_ref), + is_handle_set_(true) { } @@ -38,18 +31,28 @@ Hook::~Hook() noexcept(false) if ((user_data_ref_ != LUA_NOREF) && (user_data_ref_ != LUA_REFNIL)) luaL_unref(L_, LUA_REGISTRYINDEX, user_data_ref_); - if (is_handle_set_) { + if (is_handle_set_) + { uc_err error = uc_hook_del(engine_, hook_handle_); if (error != UC_ERR_OK) throw UnicornLibraryError(error); } } -uc_engine* Hook::engine() noexcept { return engine_; } +uc_engine *Hook::engine() noexcept +{ + return engine_; +} -lua_State* Hook::L() noexcept { return L_; } +lua_State *Hook::L() noexcept +{ + return L_; +} -uc_hook Hook::get_hook_handle() const noexcept { return hook_handle_; } +uc_hook Hook::get_hook_handle() const noexcept +{ + return hook_handle_; +} void Hook::set_hook_handle(uc_hook hook_handle) noexcept { @@ -68,7 +71,10 @@ int Hook::set_callback(int cb_index) return new_callback_ref; } -int Hook::get_callback() const noexcept { return callback_func_ref_; } +int Hook::get_callback() const noexcept +{ + return callback_func_ref_; +} void Hook::push_callback() { @@ -86,29 +92,34 @@ int Hook::set_user_data(int ud_index) return new_data_ref; } -int Hook::get_user_data() const noexcept { return user_data_ref_; } +int Hook::get_user_data() const noexcept +{ + return user_data_ref_; +} -void Hook::push_user_data() { lua_geti(L_, LUA_REGISTRYINDEX, user_data_ref_); } +void Hook::push_user_data() +{ + lua_geti(L_, LUA_REGISTRYINDEX, user_data_ref_); +} -static void* get_c_callback_for_hook_type(int hook_type, int insn_code); +static void *get_c_callback_for_hook_type(int hook_type, int insn_code); -static void get_callback(Hook* hook) +static void get_callback(Hook *hook) { - lua_State* L = hook->L(); + lua_State *L = hook->L(); hook->push_callback(); - if (lua_isnil(L, -1)) { - luaL_error(L, - "No callback function found for hook %p attached to engine %p", - hook, hook->engine()); + if (lua_isnil(L, -1)) + { + luaL_error(L, "No callback function found for hook %p attached to engine %p", + hook, hook->engine()); } } /* The C wrapper for a code execution hook. */ -static void code_hook( - uc_engine* uc, uint64_t address, uint32_t size, void* user_data) +static void code_hook(uc_engine *uc, uint64_t address, uint32_t size, void *user_data) { - auto hook = reinterpret_cast(user_data); - lua_State* L = hook->L(); + auto hook = reinterpret_cast(user_data); + lua_State *L = hook->L(); /* Push the callback function onto the stack. */ get_callback(hook); @@ -121,10 +132,10 @@ static void code_hook( lua_call(L, 4, 0); } -static void interrupt_hook(uc_engine* uc, uint32_t intno, void* user_data) +static void interrupt_hook(uc_engine *uc, uint32_t intno, void *user_data) { - auto hook = reinterpret_cast(user_data); - lua_State* L = hook->L(); + auto hook = reinterpret_cast(user_data); + lua_State *L = hook->L(); /* Push the callback function onto the stack. */ get_callback(hook); @@ -136,11 +147,10 @@ static void interrupt_hook(uc_engine* uc, uint32_t intno, void* user_data) lua_call(L, 3, 0); } -static uint32_t port_in_hook( - uc_engine* uc, uint32_t port, int size, void* user_data) +static uint32_t port_in_hook(uc_engine *uc, uint32_t port, int size, void *user_data) { - auto hook = reinterpret_cast(user_data); - lua_State* L = hook->L(); + auto hook = reinterpret_cast(user_data); + lua_State *L = hook->L(); /* Push the callback function onto the stack. */ get_callback(hook); @@ -158,11 +168,11 @@ static uint32_t port_in_hook( return return_value; } -static void port_out_hook( - uc_engine* uc, uint32_t port, int size, uint32_t value, void* user_data) +static void port_out_hook(uc_engine *uc, uint32_t port, int size, uint32_t value, + void *user_data) { - auto hook = reinterpret_cast(user_data); - lua_State* L = hook->L(); + auto hook = reinterpret_cast(user_data); + lua_State *L = hook->L(); /* Push the callback function onto the stack. */ get_callback(hook); @@ -176,11 +186,11 @@ static void port_out_hook( lua_call(L, 5, 0); } -static void memory_access_hook(uc_engine* uc, uc_mem_type type, - uint64_t address, int size, int64_t value, void* user_data) +static void memory_access_hook(uc_engine *uc, uc_mem_type type, uint64_t address, + int size, int64_t value, void *user_data) { - auto hook = reinterpret_cast(user_data); - lua_State* L = hook->L(); + auto hook = reinterpret_cast(user_data); + lua_State *L = hook->L(); /* Push the callback function onto the stack. */ get_callback(hook); @@ -195,11 +205,11 @@ static void memory_access_hook(uc_engine* uc, uc_mem_type type, lua_call(L, 6, 0); } -static bool invalid_mem_access_hook(uc_engine* uc, uc_mem_type type, - uint64_t address, int size, int64_t value, void* user_data) +static bool invalid_mem_access_hook(uc_engine *uc, uc_mem_type type, uint64_t address, + int size, int64_t value, void *user_data) { - auto hook = reinterpret_cast(user_data); - lua_State* L = hook->L(); + auto hook = reinterpret_cast(user_data); + lua_State *L = hook->L(); /* Push the callback function onto the stack. */ get_callback(hook); @@ -213,11 +223,12 @@ static bool invalid_mem_access_hook(uc_engine* uc, uc_mem_type type, hook->push_user_data(); lua_call(L, 6, 1); - if (lua_type(L, -1) != LUA_TBOOLEAN) { + if (lua_type(L, -1) != LUA_TBOOLEAN) + { luaL_error(L, - "Error: Handler for invalid memory accesses must return a boolean, " - "got a %s instead.", - lua_typename(L, -1)); + "Error: Handler for invalid memory accesses must return a boolean, " + "got a %s instead.", + lua_typename(L, -1)); // Technically this is unreachable because luaL_error calls longjmp(). // The header doesn't declare this, however, so we have no way of // indicating this to the compiler unless we're on C++20 or higher. @@ -228,10 +239,10 @@ static bool invalid_mem_access_hook(uc_engine* uc, uc_mem_type type, return return_value != 0; } -static void generic_hook_with_no_arguments(uc_engine* uc, void* user_data) +static void generic_hook_with_no_arguments(uc_engine *uc, void *user_data) { - auto hook = reinterpret_cast(user_data); - lua_State* L = hook->L(); + auto hook = reinterpret_cast(user_data); + lua_State *L = hook->L(); ul_find_lua_engine(L, uc); hook->push_user_data(); @@ -239,10 +250,10 @@ static void generic_hook_with_no_arguments(uc_engine* uc, void* user_data) } #if UC_API_MAJOR >= 2 -static bool cpuid_hook(uc_engine* uc, void* user_data) +static bool cpuid_hook(uc_engine *uc, void *user_data) { - auto hook = reinterpret_cast(user_data); - lua_State* L = hook->L(); + auto hook = reinterpret_cast(user_data); + lua_State *L = hook->L(); ul_find_lua_engine(L, uc); hook->push_user_data(); @@ -256,11 +267,11 @@ static bool cpuid_hook(uc_engine* uc, void* user_data) return result != 0; } -static void edge_generated_hook( - uc_engine* uc, uc_tb* cur_tb, uc_tb* prev_tb, void* user_data) +static void edge_generated_hook(uc_engine *uc, uc_tb *cur_tb, uc_tb *prev_tb, + void *user_data) { - auto hook = reinterpret_cast(user_data); - lua_State* L = hook->L(); + auto hook = reinterpret_cast(user_data); + lua_State *L = hook->L(); // Push the callback function onto the stack. get_callback(hook); @@ -274,11 +285,11 @@ static void edge_generated_hook( lua_call(L, 4, 0); } -static void tcg_opcode_hook(uc_engine* uc, uint64_t address, uint64_t arg1, - uint64_t arg2, uint32_t size, void* user_data) +static void tcg_opcode_hook(uc_engine *uc, uint64_t address, uint64_t arg1, uint64_t arg2, + uint32_t size, void *user_data) { - auto hook = reinterpret_cast(user_data); - lua_State* L = hook->L(); + auto hook = reinterpret_cast(user_data); + lua_State *L = hook->L(); // Push the callback function onto the stack. get_callback(hook); @@ -294,7 +305,7 @@ static void tcg_opcode_hook(uc_engine* uc, uint64_t address, uint64_t arg1, lua_call(L, 7, 0); } -static void arm64_cp_reg_to_lua_table(lua_State* L, const uc_arm64_cp_reg* reg) +static void arm64_cp_reg_to_lua_table(lua_State *L, const uc_arm64_cp_reg *reg) { lua_createtable(L, 0, 6); lua_pushinteger(L, reg->crn); @@ -311,11 +322,11 @@ static void arm64_cp_reg_to_lua_table(lua_State* L, const uc_arm64_cp_reg* reg) lua_setfield(L, -1, "val"); } -static uint32_t arm64_sys_hook(uc_engine* uc, uc_arm64_reg reg, - const uc_arm64_cp_reg* cp_reg, void* user_data) +static uint32_t arm64_sys_hook(uc_engine *uc, uc_arm64_reg reg, + const uc_arm64_cp_reg *cp_reg, void *user_data) { - auto hook = reinterpret_cast(user_data); - lua_State* L = hook->L(); + auto hook = reinterpret_cast(user_data); + lua_State *L = hook->L(); // Push the callback function onto the stack. get_callback(hook); @@ -334,36 +345,38 @@ static uint32_t arm64_sys_hook(uc_engine* uc, uc_arm64_reg reg, #endif // UC_API_MAJOR >= 2 -static void* get_c_callback_for_hook_type(int hook_type, int insn_code) +static void *get_c_callback_for_hook_type(int hook_type, int insn_code) { - switch (hook_type) { + switch (hook_type) + { case UC_HOOK_INTR: - return (void*)interrupt_hook; + return (void *)interrupt_hook; case UC_HOOK_BLOCK: case UC_HOOK_CODE: - return (void*)code_hook; + return (void *)code_hook; case UC_HOOK_INSN: - switch (insn_code) { + switch (insn_code) + { case UC_X86_INS_IN: - return reinterpret_cast(port_in_hook); + return reinterpret_cast(port_in_hook); case UC_X86_INS_OUT: - return reinterpret_cast(port_out_hook); + return reinterpret_cast(port_out_hook); case UC_X86_INS_SYSCALL: case UC_X86_INS_SYSENTER: - return reinterpret_cast(generic_hook_with_no_arguments); + return reinterpret_cast(generic_hook_with_no_arguments); #if UC_API_MAJOR >= 2 case UC_X86_INS_CPUID: - return reinterpret_cast(cpuid_hook); + return reinterpret_cast(cpuid_hook); case UC_ARM64_INS_MRS: case UC_ARM64_INS_MSR: case UC_ARM64_INS_SYS: case UC_ARM64_INS_SYSL: - return reinterpret_cast(arm64_sys_hook); + return reinterpret_cast(arm64_sys_hook); #endif default: - return (void*)code_hook; + return (void *)code_hook; } case UC_HOOK_MEM_FETCH: @@ -371,7 +384,7 @@ static void* get_c_callback_for_hook_type(int hook_type, int insn_code) case UC_HOOK_MEM_READ_AFTER: case UC_HOOK_MEM_WRITE: case UC_HOOK_MEM_VALID: - return (void*)memory_access_hook; + return (void *)memory_access_hook; case UC_HOOK_MEM_FETCH_INVALID: case UC_HOOK_MEM_FETCH_PROT: @@ -385,13 +398,13 @@ static void* get_c_callback_for_hook_type(int hook_type, int insn_code) case UC_HOOK_MEM_WRITE_INVALID: case UC_HOOK_MEM_WRITE_PROT: case UC_HOOK_MEM_WRITE_UNMAPPED: - return (void*)invalid_mem_access_hook; + return (void *)invalid_mem_access_hook; #if UC_API_MAJOR >= 2 case UC_HOOK_EDGE_GENERATED: - return (void*)edge_generated_hook; + return (void *)edge_generated_hook; case UC_HOOK_TCG_OPCODE: - return (void*)tcg_opcode_hook; + return (void *)tcg_opcode_hook; #endif // UC_API_MAJOR >= 2 default: @@ -399,7 +412,7 @@ static void* get_c_callback_for_hook_type(int hook_type, int insn_code) } } -int ul_hook_add(lua_State* L) +int ul_hook_add(lua_State *L) { uint64_t start, end; int extra_argument; @@ -407,11 +420,12 @@ int ul_hook_add(lua_State* L) int n_args = lua_gettop(L); - UCLuaEngine* engine_object = ul_toluaengine(L, 1); + UCLuaEngine *engine_object = ul_toluaengine(L, 1); int hook_type = static_cast(luaL_checkinteger(L, 2)); /* Callback function is at position 3 */ - switch (n_args) { + switch (n_args) + { case 3: /* No start or end addresses given, assume hook applies to all of * RAM. */ @@ -435,7 +449,7 @@ int ul_hook_add(lua_State* L) return luaL_error(L, "Expected 3-7 arguments, got %d.", n_args); } - Hook* hook_info = engine_object->create_empty_hook(); + Hook *hook_info = engine_object->create_empty_hook(); hook_info->set_callback(3); /* If the caller gave us a sixth argument, it's data to pass to the @@ -451,23 +465,25 @@ int ul_hook_add(lua_State* L) extra_argument = LUA_NOREF; /* Figure out which C hook we need */ - void* c_callback = get_c_callback_for_hook_type(hook_type, extra_argument); - if (c_callback == nullptr) { + void *c_callback = get_c_callback_for_hook_type(hook_type, extra_argument); + if (c_callback == nullptr) + { engine_object->remove_hook(hook_info); return luaL_error(L, "Unrecognized hook type: %d", hook_type); } uc_hook hook_handle; - uc_engine* engine_handle = engine_object->get_handle(); + uc_engine *engine_handle = engine_object->get_handle(); if (n_args < 6) - error = uc_hook_add(engine_handle, &hook_handle, hook_type, c_callback, - hook_info, start, end); + error = uc_hook_add(engine_handle, &hook_handle, hook_type, c_callback, hook_info, + start, end); else - error = uc_hook_add(engine_handle, &hook_handle, hook_type, c_callback, - hook_info, start, end, extra_argument); + error = uc_hook_add(engine_handle, &hook_handle, hook_type, c_callback, hook_info, + start, end, extra_argument); - if (error != UC_ERR_OK) { + if (error != UC_ERR_OK) + { engine_object->remove_hook(hook_info); ul_crash_on_error(L, error); } @@ -477,14 +493,14 @@ int ul_hook_add(lua_State* L) // Return the hook struct as light userdata. Lua code can use this to remove // a hook before the engine is closed. Hooks will remain attached even if // this handle gets garbage-collected. - lua_pushlightuserdata(L, (void*)hook_info); + lua_pushlightuserdata(L, (void *)hook_info); return 1; } -int ul_hook_del(lua_State* L) +int ul_hook_del(lua_State *L) { - auto hook_info = (Hook*)lua_touserdata(L, 2); - UCLuaEngine* engine = ul_toluaengine(L, 1); + auto hook_info = (Hook *)lua_touserdata(L, 2); + UCLuaEngine *engine = ul_toluaengine(L, 1); engine->remove_hook(hook_info); return 0; diff --git a/src/memory.cpp b/src/memory.cpp index 108f1574..5e640334 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -7,13 +7,13 @@ #include "unicornlua/lua.hpp" #include "unicornlua/utils.hpp" -int ul_mem_write(lua_State* L) +int ul_mem_write(lua_State *L) { size_t length; - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); auto address = static_cast(luaL_checkinteger(L, 2)); - const void* data = luaL_checklstring(L, 3, &length); + const void *data = luaL_checklstring(L, 3, &length); uc_err error = uc_mem_write(engine, address, data, length); if (error != UC_ERR_OK) @@ -21,9 +21,9 @@ int ul_mem_write(lua_State* L) return 0; } -int ul_mem_read(lua_State* L) +int ul_mem_read(lua_State *L) { - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); auto address = static_cast(luaL_checkinteger(L, 2)); auto length = static_cast(luaL_checkinteger(L, 3)); @@ -36,9 +36,9 @@ int ul_mem_read(lua_State* L) return 1; } -int ul_mem_map(lua_State* L) +int ul_mem_map(lua_State *L) { - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); auto address = static_cast(luaL_checkinteger(L, 2)); auto size = static_cast(luaL_checkinteger(L, 3)); auto perms = static_cast(luaL_optinteger(L, 4, UC_PROT_ALL)); @@ -49,9 +49,9 @@ int ul_mem_map(lua_State* L) return 0; } -int ul_mem_unmap(lua_State* L) +int ul_mem_unmap(lua_State *L) { - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); auto address = static_cast(luaL_checkinteger(L, 2)); auto size = static_cast(luaL_checkinteger(L, 3)); @@ -61,9 +61,9 @@ int ul_mem_unmap(lua_State* L) return 0; } -int ul_mem_protect(lua_State* L) +int ul_mem_protect(lua_State *L) { - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); auto address = static_cast(luaL_checkinteger(L, 2)); auto size = static_cast(luaL_checkinteger(L, 3)); auto perms = static_cast(luaL_checkinteger(L, 4)); @@ -74,12 +74,12 @@ int ul_mem_protect(lua_State* L) return 0; } -int ul_mem_regions(lua_State* L) +int ul_mem_regions(lua_State *L) { uint32_t n_regions; - uc_engine* engine = ul_toengine(L, 1); - uc_mem_region* regions = nullptr; + uc_engine *engine = ul_toengine(L, 1); + uc_mem_region *regions = nullptr; n_regions = 0; uc_err error = uc_mem_regions(engine, ®ions, &n_regions); @@ -87,7 +87,8 @@ int ul_mem_regions(lua_State* L) ul_crash_on_error(L, error); lua_createtable(L, static_cast(n_regions), 0); - for (uint32_t i = 0; i < n_regions; ++i) { + for (uint32_t i = 0; i < n_regions; ++i) + { lua_createtable(L, 0, 3); lua_pushinteger(L, static_cast(regions[i].begin)); diff --git a/src/registers_misc.cpp b/src/registers_misc.cpp index eb744c25..7997ee53 100644 --- a/src/registers_misc.cpp +++ b/src/registers_misc.cpp @@ -19,33 +19,35 @@ #include "unicornlua/registers.hpp" #include "unicornlua/utils.hpp" -constexpr uint8_t kFP80PositiveInfinity[] - = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x7f }; -constexpr uint8_t kFP80NegativeInfinity[] - = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0xff }; -constexpr uint8_t kFP80SignalingNaN[] = { 1, 0, 0, 0, 0, 0, 0, 0, 0xf0, 0x7f }; +constexpr uint8_t kFP80PositiveInfinity[] = {0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x7f}; +constexpr uint8_t kFP80NegativeInfinity[] = {0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0xff}; +constexpr uint8_t kFP80SignalingNaN[] = {1, 0, 0, 0, 0, 0, 0, 0, 0xf0, 0x7f}; -lua_Number read_float80(const uint8_t* data) +lua_Number read_float80(const uint8_t *data) { - uint64_t significand = *reinterpret_cast(data); - int exponent = *reinterpret_cast(data + 8) & 0x7fff; - bool sign = (*reinterpret_cast(data + 8) & 0x8000) != 0; + uint64_t significand = *reinterpret_cast(data); + int exponent = *reinterpret_cast(data + 8) & 0x7fff; + bool sign = (*reinterpret_cast(data + 8) & 0x8000) != 0; // Clear errno before starting because we use it to indicate that the return // value is valid on some FPUs but not others, or if the NaN is a signaling // one. errno = 0; - if (exponent == 0) { + if (exponent == 0) + { if (significand == 0) return 0.0; if (sign) return std::ldexp(-static_cast(significand), -16382); return std::ldexp(static_cast(significand), -16382); - } else if (exponent == 0x7fff) { + } + else if (exponent == 0x7fff) + { // Top two bits of the significand will tell us what kind of number this // is and aren't used for storing a value. - switch ((significand >> 62) & 3) { + switch ((significand >> 62) & 3) + { case 0: if (significand == 0) return static_cast(sign ? -INFINITY : +INFINITY); @@ -83,8 +85,7 @@ lua_Number read_float80(const uint8_t* data) // If the high bit of the significand is set, this is a normal value. Ignore // the high bit of the significand and compensate for the exponent bias. - auto f_part - = static_cast(significand & UINT64_C(0x7fffffffffffffff)); + auto f_part = static_cast(significand & UINT64_C(0x7fffffffffffffff)); if (sign) f_part *= -1; @@ -121,12 +122,13 @@ static bool is_snan(lua_Number value) return (fenv_flags & FE_INVALID) != 0; } -void write_float80(lua_Number value, uint8_t* buffer) +void write_float80(lua_Number value, uint8_t *buffer) { int f_type = std::fpclassify(value); int sign_bit = std::signbit(value) ? 0x8000 : 0; - switch (f_type) { + switch (f_type) + { case FP_INFINITE: if (sign_bit) memcpy(buffer, kFP80NegativeInfinity, 10); @@ -171,49 +173,55 @@ void write_float80(lua_Number value, uint8_t* buffer) // 2^63)^exponent = value auto int_significand = static_cast( float_significand * static_cast(UINT64_C(1) << 62)); - if (f_type == FP_NORMAL) { + if (f_type == FP_NORMAL) + { // Normal number, set the high bit. int_significand |= UINT64_C(1) << 63; exponent += 16383; - } else + } + else exponent = 0; - *reinterpret_cast(buffer) = int_significand; - *reinterpret_cast(buffer + UINT16_C(8)) - = static_cast(exponent | sign_bit); + *reinterpret_cast(buffer) = int_significand; + *reinterpret_cast(buffer + UINT16_C(8)) = + static_cast(exponent | sign_bit); } -Register::Register() - : kind_(UL_REG_TYPE_UNKNOWN) +Register::Register() : kind_(UL_REG_TYPE_UNKNOWN) { memset(data_, 0, sizeof(data_)); } -Register::Register(const void* buffer, RegisterDataType kind) - : kind_(kind) +Register::Register(const void *buffer, RegisterDataType kind) : kind_(kind) { assign_value(buffer, kind); } -void Register::assign_value(const void* buffer, RegisterDataType kind) +void Register::assign_value(const void *buffer, RegisterDataType kind) { memcpy(data_, buffer, size_for_register_kind(kind)); kind_ = kind; } -RegisterDataType Register::get_kind() const noexcept { return kind_; } +RegisterDataType Register::get_kind() const noexcept +{ + return kind_; +} -size_t Register::get_size() const { return size_for_register_kind(kind_); } +size_t Register::get_size() const +{ + return size_for_register_kind(kind_); +} -int ul_reg_write(lua_State* L) +int ul_reg_write(lua_State *L) { - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); int register_id = static_cast(luaL_checkinteger(L, 2)); register_buffer_type buffer; memset(buffer, 0, sizeof(buffer)); - *reinterpret_cast(buffer) - = static_cast(luaL_checkinteger(L, 3)); + *reinterpret_cast(buffer) = + static_cast(luaL_checkinteger(L, 3)); uc_err error = uc_reg_write(engine, register_id, buffer); if (error != UC_ERR_OK) @@ -221,9 +229,9 @@ int ul_reg_write(lua_State* L) return 0; } -int ul_reg_write_as(lua_State* L) +int ul_reg_write_as(lua_State *L) { - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); int register_id = static_cast(luaL_checkinteger(L, 2)); Register reg = Register::from_lua(L, 3, 4); @@ -233,18 +241,20 @@ int ul_reg_write_as(lua_State* L) return 0; } -int ul_reg_read(lua_State* L) +int ul_reg_read(lua_State *L) { register_buffer_type value_buffer; memset(value_buffer, 0, sizeof(value_buffer)); - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); int register_id = static_cast(luaL_checkinteger(L, 2)); // When reading an MSR on an x86 processor, Unicorn requires the buffer to // contain the ID of the register to read. - if (register_id == UC_X86_REG_MSR) { - if (lua_gettop(L) < 3) { + if (register_id == UC_X86_REG_MSR) + { + if (lua_gettop(L) < 3) + { throw LuaBindingError( "Reading an x86 model-specific register (MSR) requires" " an additional argument identifying the register to read. You" @@ -252,27 +262,27 @@ int ul_reg_read(lua_State* L) " Developer's Manual\", available as PDFs from their website."); } int msr_id = static_cast(luaL_checkinteger(L, 3)); - *reinterpret_cast(value_buffer) = msr_id; + *reinterpret_cast(value_buffer) = msr_id; } uc_err error = uc_reg_read(engine, register_id, value_buffer); if (error != UC_ERR_OK) ul_crash_on_error(L, error); - lua_pushinteger(L, *reinterpret_cast(value_buffer)); + lua_pushinteger(L, *reinterpret_cast(value_buffer)); return 1; } -int ul_reg_read_as(lua_State* L) +int ul_reg_read_as(lua_State *L) { - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); int register_id = static_cast(luaL_checkinteger(L, 2)); auto read_as_type = static_cast(luaL_checkinteger(L, 3)); - if (register_id == UC_X86_REG_MSR) { - throw LuaBindingError( - "reg_read_as() doesn't support reading x86 model-specific" - " registers."); + if (register_id == UC_X86_REG_MSR) + { + throw LuaBindingError("reg_read_as() doesn't support reading x86 model-specific" + " registers."); } register_buffer_type value_buffer; @@ -288,9 +298,9 @@ int ul_reg_read_as(lua_State* L) return 1; } -int ul_reg_write_batch(lua_State* L) +int ul_reg_write_batch(lua_State *L) { - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); /* Second argument will be a table with key-value pairs, the keys being the * registers to write to and the values being the values to write to the @@ -299,85 +309,87 @@ int ul_reg_write_batch(lua_State* L) std::unique_ptr register_ids(new int[n_registers]); std::unique_ptr values(new int_least64_t[n_registers]); - std::unique_ptr p_values(new void*[n_registers]); + std::unique_ptr p_values(new void *[n_registers]); /* Iterate through the register/value pairs and put them in the * corresponding array positions. */ lua_pushnil(L); - for (size_t i = 0; lua_next(L, 2) != 0; ++i) { + for (size_t i = 0; lua_next(L, 2) != 0; ++i) + { register_ids[i] = static_cast(luaL_checkinteger(L, -2)); values[i] = static_cast(luaL_checkinteger(L, -1)); p_values[i] = &values[i]; lua_pop(L, 1); } - uc_err error = uc_reg_write_batch(engine, register_ids.get(), - p_values.get(), static_cast(n_registers)); + uc_err error = uc_reg_write_batch(engine, register_ids.get(), p_values.get(), + static_cast(n_registers)); if (error != UC_ERR_OK) ul_crash_on_error(L, error); return 0; } static void prepare_batch_buffers(size_t n_registers, - std::unique_ptr& values, - std::unique_ptr& value_pointers) + std::unique_ptr &values, + std::unique_ptr &value_pointers) { values.reset(new register_buffer_type[n_registers]); - value_pointers.reset(new void*[n_registers]); + value_pointers.reset(new void *[n_registers]); for (size_t i = 0; i < n_registers; ++i) value_pointers[i] = &values[i]; memset(values.get(), 0, n_registers * sizeof(register_buffer_type)); } -int ul_reg_read_batch(lua_State* L) +int ul_reg_read_batch(lua_State *L) { - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); auto n_registers = static_cast(lua_gettop(L)) - 1; std::unique_ptr register_ids(new int[n_registers]); std::unique_ptr values; - std::unique_ptr value_pointers; + std::unique_ptr value_pointers; prepare_batch_buffers(n_registers, values, value_pointers); for (size_t i = 0; i < n_registers; ++i) - register_ids[i] - = static_cast(lua_tointeger(L, static_cast(i) + 2)); + register_ids[i] = static_cast(lua_tointeger(L, static_cast(i) + 2)); - uc_err error = uc_reg_read_batch(engine, register_ids.get(), - value_pointers.get(), static_cast(n_registers)); + uc_err error = uc_reg_read_batch(engine, register_ids.get(), value_pointers.get(), + static_cast(n_registers)); if (error != UC_ERR_OK) ul_crash_on_error(L, error); - for (size_t i = 0; i < n_registers; ++i) { - lua_pushinteger(L, *reinterpret_cast(values[i])); + for (size_t i = 0; i < n_registers; ++i) + { + lua_pushinteger(L, *reinterpret_cast(values[i])); } return static_cast(n_registers); } -int ul_reg_read_batch_as(lua_State* L) +int ul_reg_read_batch_as(lua_State *L) { - uc_engine* engine = ul_toengine(L, 1); + uc_engine *engine = ul_toengine(L, 1); size_t n_registers = count_table_elements(L, 2); std::unique_ptr register_ids(new int[n_registers]); std::unique_ptr value_types(new int[n_registers]); std::unique_ptr values; - std::unique_ptr value_pointers; + std::unique_ptr value_pointers; prepare_batch_buffers(n_registers, values, value_pointers); // Iterate through the second argument -- a table mapping register IDs to // the types we want them back as. lua_pushnil(L); - for (size_t i = 0; lua_next(L, 2) != 0; ++i) { + for (size_t i = 0; lua_next(L, 2) != 0; ++i) + { register_ids[i] = (int)luaL_checkinteger(L, -2); value_types[i] = (int)luaL_checkinteger(L, -1); lua_pop(L, 1); } - uc_err error = uc_reg_read_batch(engine, register_ids.get(), - value_pointers.get(), static_cast(n_registers)); + uc_err error = uc_reg_read_batch(engine, register_ids.get(), value_pointers.get(), + static_cast(n_registers)); if (error != UC_ERR_OK) ul_crash_on_error(L, error); @@ -385,13 +397,14 @@ int ul_reg_read_batch_as(lua_State* L) // is a key-value mapping where the keys are the register IDs and the values // are the typecasted values read from the registers. lua_createtable(L, 0, static_cast(n_registers)); - for (size_t i = 0; i < n_registers; ++i) { + for (size_t i = 0; i < n_registers; ++i) + { // Key: register ID lua_pushinteger(L, register_ids[i]); // Value: Deserialized register - auto register_object = Register( - value_pointers[i], static_cast(value_types[i])); + auto register_object = + Register(value_pointers[i], static_cast(value_types[i])); register_object.push_to_lua(L); lua_settable(L, -3); } diff --git a/src/transaction.cpp b/src/transaction.cpp index 2109c6f4..cc477f5f 100644 --- a/src/transaction.cpp +++ b/src/transaction.cpp @@ -1,10 +1,10 @@ #include #if UC_API_MAJOR >= 2 -#include "unicornlua/lua.hpp" -#include "unicornlua/transaction.hpp" +# include "unicornlua/lua.hpp" +# include "unicornlua/transaction.hpp" -void create_table_from_transaction_block(lua_State* L, const uc_tb* block) +void create_table_from_transaction_block(lua_State *L, const uc_tb *block) { lua_createtable(L, 0, 3); diff --git a/src/unicorn.cpp b/src/unicorn.cpp index 94bd66f6..18f26472 100644 --- a/src/unicorn.cpp +++ b/src/unicorn.cpp @@ -6,7 +6,7 @@ #include "unicornlua/unicornlua.hpp" #include "unicornlua/utils.hpp" -static int ul_unicorn_version(lua_State* L) +static int ul_unicorn_version(lua_State *L) { unsigned major, minor; @@ -18,7 +18,7 @@ static int ul_unicorn_version(lua_State* L) // Create a three-element table that indicates the major, minor, and patch // versions of this Lua binding. -static int ul_create_unicornlua_version_table(lua_State* L) +static int ul_create_unicornlua_version_table(lua_State *L) { lua_createtable(L, 3, 0); @@ -33,7 +33,7 @@ static int ul_create_unicornlua_version_table(lua_State* L) return 1; } -static int ul_arch_supported(lua_State* L) +static int ul_arch_supported(lua_State *L) { int is_supported = 0; @@ -41,7 +41,8 @@ static int ul_arch_supported(lua_State* L) // determine if an architecture is supported without needing to check the // Unicorn version AND assume that the Unicorn library was compiled with all // available architectures. - if (!lua_isnil(L, 1)) { + if (!lua_isnil(L, 1)) + { auto architecture = static_cast(luaL_checkinteger(L, 1)); is_supported = uc_arch_supported(architecture) ? 1 : 0; } @@ -50,12 +51,12 @@ static int ul_arch_supported(lua_State* L) return 1; } -static int ul_open(lua_State* L) +static int ul_open(lua_State *L) { auto architecture = static_cast(luaL_checkinteger(L, 1)); auto mode = static_cast(luaL_checkinteger(L, 2)); - uc_engine* engine; + uc_engine *engine; uc_err error = uc_open(architecture, mode, &engine); if (error != UC_ERR_OK) ul_crash_on_error(L, error); @@ -80,19 +81,21 @@ static int ul_open(lua_State* L) return 1; } -static int ul_strerror(lua_State* L) +static int ul_strerror(lua_State *L) { auto error = static_cast(luaL_checkinteger(L, 1)); lua_pushstring(L, uc_strerror(error)); return 1; } -static constexpr luaL_Reg kUnicornLibraryFunctions[] - = { { "arch_supported", ul_arch_supported }, { "open", ul_open }, - { "strerror", ul_strerror }, { "version", ul_unicorn_version }, - { nullptr, nullptr } }; +static constexpr luaL_Reg kUnicornLibraryFunctions[] = { + {"arch_supported", ul_arch_supported}, + {"open", ul_open}, + {"strerror", ul_strerror}, + {"version", ul_unicorn_version}, + {nullptr, nullptr}}; -extern "C" UNICORN_EXPORT int luaopen_unicorn(lua_State* L) +extern "C" UNICORN_EXPORT int luaopen_unicorn(lua_State *L) { // Initialize the engine bits, such as the metatables that engine and // context instances use. diff --git a/src/utils.cpp b/src/utils.cpp index f1fc5ed2..3f26c1c7 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -3,16 +3,16 @@ #include "unicornlua/lua.hpp" #include "unicornlua/utils.hpp" -void ul_crash_on_error(lua_State* L, uc_err error) +void ul_crash_on_error(lua_State *L, uc_err error) { - const char* message = uc_strerror(error); + const char *message = uc_strerror(error); lua_checkstack(L, 1); lua_pushstring(L, message); lua_error(L); UL_UNREACHABLE_MARKER; } -void ul_create_weak_table(lua_State* L, const char* mode) +void ul_create_weak_table(lua_State *L, const char *mode) { lua_newtable(L); lua_createtable(L, 0, 1); @@ -21,15 +21,16 @@ void ul_create_weak_table(lua_State* L, const char* mode) lua_setmetatable(L, -2); } -void load_int_constants(lua_State* L, const struct NamedIntConst* constants) +void load_int_constants(lua_State *L, const struct NamedIntConst *constants) { - for (int i = 0; constants[i].name != nullptr; ++i) { + for (int i = 0; constants[i].name != nullptr; ++i) + { lua_pushinteger(L, constants[i].value); lua_setfield(L, -2, constants[i].name); } } -size_t count_table_elements(lua_State* L, int table_index) +size_t count_table_elements(lua_State *L, int table_index) { size_t count = 0; diff --git a/tests/c/compat.cpp b/tests/c/compat.cpp index 29262771..02edd7e0 100644 --- a/tests/c/compat.cpp +++ b/tests/c/compat.cpp @@ -14,7 +14,7 @@ TEST_CASE_FIXTURE(LuaFixture, "[5.3 compat] lua_seti() basic") lua_seti(L, 1, 5); - CHECK_EQ(lua_gettop(L), 1); // Only the table should be on the stack. + CHECK_EQ(lua_gettop(L), 1); // Only the table should be on the stack. REQUIRE_EQ(lua_type(L, 1), LUA_TTABLE); // Verify it's a table // Retrieve whatever it is at index 5 @@ -23,7 +23,7 @@ TEST_CASE_FIXTURE(LuaFixture, "[5.3 compat] lua_seti() basic") // Should be a string... CHECK_EQ(lua_type(L, -1), LUA_TSTRING); - const char* result = lua_tostring(L, -1); + const char *result = lua_tostring(L, -1); CHECK_EQ(strcmp(result, "This is a string."), 0); // Remove the string and table @@ -42,7 +42,7 @@ TEST_CASE_FIXTURE(LuaFixture, "[5.3 compat] lua_geti() basic") // Don't use lua_seti because it crashes on OSX + LuaJIT. lua_rawset(L, 1); - CHECK_EQ(lua_gettop(L), 1); // Only the table should be on the stack. + CHECK_EQ(lua_gettop(L), 1); // Only the table should be on the stack. CHECK_EQ(lua_type(L, 1), LUA_TTABLE); // Verify it's a table // Retrieve whatever it is at index 1 diff --git a/tests/c/context.cpp b/tests/c/context.cpp index 05c1f4e6..848b44b5 100644 --- a/tests/c/context.cpp +++ b/tests/c/context.cpp @@ -5,21 +5,19 @@ TEST_CASE_FIXTURE(AutoclosingEngineFixture, "Test creating a context") { - Context* context = uclua_engine->create_context_in_lua(); + Context *context = uclua_engine->create_context_in_lua(); CHECK_NE(context, nullptr); - CHECK_MESSAGE( - lua_gettop(L) == 1, "Expecting a context object on the stack."); + CHECK_MESSAGE(lua_gettop(L) == 1, "Expecting a context object on the stack."); CHECK_MESSAGE(lua_type(L, 1) == LUA_TUSERDATA, - "Object at top of stack should be userdata."); + "Object at top of stack should be userdata."); - CHECK_MESSAGE((Context*)lua_touserdata(L, 1) == context, - "TOS isn't the context object we were expecting."); + CHECK_MESSAGE((Context *)lua_touserdata(L, 1) == context, + "TOS isn't the context object we were expecting."); // Metatable of the context is at index 2, the expected metatable is at // index 3. - CHECK_MESSAGE( - lua_getmetatable(L, 1) != 0, "Context object has no metatable."); + CHECK_MESSAGE(lua_getmetatable(L, 1) != 0, "Context object has no metatable."); luaL_getmetatable(L, kContextMetatableName); CHECK(lua_gettop(L) == 3); @@ -27,10 +25,10 @@ TEST_CASE_FIXTURE(AutoclosingEngineFixture, "Test creating a context") #if LUA_VERSION_NUM < 502 // lua_compare() was added in 5.2, so we have to use lua_equal() here. CHECK_MESSAGE(lua_equal(L, 2, 3) == 1, - "Context metatable doesn't match the expected one."); + "Context metatable doesn't match the expected one."); #else CHECK_MESSAGE(lua_compare(L, 2, 3, LUA_OPEQ) == 1, - "Context metatable doesn't match the expected one."); + "Context metatable doesn't match the expected one."); #endif // Clean up the stack lua_pop(L, 3); @@ -38,14 +36,14 @@ TEST_CASE_FIXTURE(AutoclosingEngineFixture, "Test creating a context") TEST_CASE_FIXTURE(AutoclosingEngineFixture, "Test closing a context") { - Context* context = uclua_engine->create_context_in_lua(); + Context *context = uclua_engine->create_context_in_lua(); CHECK_NE(context, nullptr); CHECK_NE(context->context_handle, nullptr); CHECK_NE(context->engine, nullptr); // The pointer in the Lua userdata must be identical to the pointer we got // back from the function. - auto userdata = reinterpret_cast(lua_touserdata(L, -1)); + auto userdata = reinterpret_cast(lua_touserdata(L, -1)); REQUIRE_EQ(userdata, context); ul_context_free(L); @@ -55,10 +53,9 @@ TEST_CASE_FIXTURE(AutoclosingEngineFixture, "Test closing a context") lua_pop(L, 1); } -TEST_CASE_FIXTURE( - AutoclosingEngineFixture, "Closing a closed context explodes.") +TEST_CASE_FIXTURE(AutoclosingEngineFixture, "Closing a closed context explodes.") { - Context* context = uclua_engine->create_context_in_lua(); + Context *context = uclua_engine->create_context_in_lua(); CHECK_NE(context->context_handle, nullptr); CHECK_NE(context->engine, nullptr); @@ -76,15 +73,14 @@ TEST_CASE_FIXTURE( lua_pop(L, 1); } -TEST_CASE_FIXTURE( - AutoclosingEngineFixture, "ul_context_maybe_free is idempotent.") +TEST_CASE_FIXTURE(AutoclosingEngineFixture, "ul_context_maybe_free is idempotent.") { - Context* context = uclua_engine->create_context_in_lua(); + Context *context = uclua_engine->create_context_in_lua(); CHECK_NE(context, nullptr); // The pointer in the Lua userdata must be identical to the pointer we got // back from the function. - auto userdata = reinterpret_cast(lua_touserdata(L, -1)); + auto userdata = reinterpret_cast(lua_touserdata(L, -1)); REQUIRE_EQ(userdata, context); ul_context_maybe_free(L); @@ -101,19 +97,18 @@ TEST_CASE_FIXTURE( } TEST_CASE_FIXTURE(AutoclosingEngineFixture, - "Trying to restore from a closed context explodes.") + "Trying to restore from a closed context explodes.") { - Context* context = uclua_engine->create_context_in_lua(); + Context *context = uclua_engine->create_context_in_lua(); CHECK_NE(context, nullptr); ul_context_free(L); CHECK_EQ(context->context_handle, nullptr); CHECK_EQ(context->engine, nullptr); - auto userdata = reinterpret_cast(lua_touserdata(L, -1)); + auto userdata = reinterpret_cast(lua_touserdata(L, -1)); CHECK_EQ(userdata, context); - CHECK_THROWS_AS( - uclua_engine->restore_from_context(context), LuaBindingError); + CHECK_THROWS_AS(uclua_engine->restore_from_context(context), LuaBindingError); // Remove the context from the stack. lua_pop(L, 1); diff --git a/tests/c/engine.cpp b/tests/c/engine.cpp index 437fffa3..d8fcfe03 100644 --- a/tests/c/engine.cpp +++ b/tests/c/engine.cpp @@ -6,34 +6,30 @@ #include "unicornlua/errors.hpp" #include "unicornlua/lua.hpp" -TEST_CASE_FIXTURE( - EngineFixture, "UCLuaEngine::close() sets engine handle to null") +TEST_CASE_FIXTURE(EngineFixture, "UCLuaEngine::close() sets engine handle to null") { uclua_engine->close(); CHECK_MESSAGE(uclua_engine->get_handle() == nullptr, - "Engine handle should be null after closing."); + "Engine handle should be null after closing."); } -TEST_CASE_FIXTURE( - EngineFixture, "UCLuaEngine::close() crashes if you call it twice") +TEST_CASE_FIXTURE(EngineFixture, "UCLuaEngine::close() crashes if you call it twice") { uclua_engine->close(); CHECK_MESSAGE(uclua_engine->get_handle() == nullptr, - "Engine handle should be null after closing."); + "Engine handle should be null after closing."); CHECK_THROWS_AS(uclua_engine->close(), LuaBindingError); } -TEST_CASE_FIXTURE( - AutoclosingEngineFixture, "UCLuaEngine::query() generally works") +TEST_CASE_FIXTURE(AutoclosingEngineFixture, "UCLuaEngine::query() generally works") { CHECK_EQ(uclua_engine->query(UC_QUERY_PAGE_SIZE), 4096); } TEST_CASE_FIXTURE(AutoclosingEngineFixture, - "UCLuaEngine::query() raises exception when given a bad query type") + "UCLuaEngine::query() raises exception when given a bad query type") { - CHECK_THROWS_AS( - uclua_engine->query((uc_query_type)-123), UnicornLibraryError); + CHECK_THROWS_AS(uclua_engine->query((uc_query_type)-123), UnicornLibraryError); } TEST_CASE_FIXTURE(AutoclosingEngineFixture, "errno() on clean engine works") diff --git a/tests/c/fixtures.cpp b/tests/c/fixtures.cpp index 5e2a6e50..3ae9fc2c 100644 --- a/tests/c/fixtures.cpp +++ b/tests/c/fixtures.cpp @@ -23,11 +23,13 @@ LuaFixture::~LuaFixture() if (L == nullptr) return; - if (lua_gettop(L) > 0) { + if (lua_gettop(L) > 0) + { std::ostringstream buf; buf << "Garbage left on the stack after test exited:\n"; - for (int i = 1; i <= lua_gettop(L); ++i) { - const char* type_name = lua_typename(L, i); + for (int i = 1; i <= lua_gettop(L); ++i) + { + const char *type_name = lua_typename(L, i); buf << "At stack index " << i << ": " << type_name << "\n"; } FAIL(buf.str()); @@ -35,22 +37,20 @@ LuaFixture::~LuaFixture() lua_close(L); } -EngineFixture::EngineFixture() - : LuaFixture() - , uclua_engine(nullptr) +EngineFixture::EngineFixture() : LuaFixture(), uclua_engine(nullptr) { ul_init_engines_lib(L); CHECK_MESSAGE(lua_gettop(L) == 0, - "Garbage left on the stack after initializing the engine system."); + "Garbage left on the stack after initializing the engine system."); - uc_engine* engine_handle; + uc_engine *engine_handle; uc_err error = uc_open(UC_ARCH_X86, UC_MODE_32, &engine_handle); REQUIRE_MESSAGE(error == UC_ERR_OK, "Failed to create an x86-32 engine."); uclua_engine = new UCLuaEngine(L, engine_handle); CHECK_NE(uclua_engine->get_handle(), nullptr); CHECK_MESSAGE(lua_gettop(L) == 0, - "Garbage on the stack after creating the engine object."); + "Garbage on the stack after creating the engine object."); } AutoclosingEngineFixture::~AutoclosingEngineFixture() @@ -59,8 +59,8 @@ AutoclosingEngineFixture::~AutoclosingEngineFixture() // but only on OSX. I'm at my wits' end trying to figure this out. REQUIRE(L != nullptr); CHECK_MESSAGE(lua_gettop(L) == 0, - "Trash on the stack just BEFORE deleting the engine C++ object"); + "Trash on the stack just BEFORE deleting the engine C++ object"); delete uclua_engine; CHECK_MESSAGE(lua_gettop(L) == 0, - "Trash on the stack AFTER deleting the engine C++ object."); + "Trash on the stack AFTER deleting the engine C++ object."); } diff --git a/tests/c/fixtures.hpp b/tests/c/fixtures.hpp index 24f7351e..91d72b29 100644 --- a/tests/c/fixtures.hpp +++ b/tests/c/fixtures.hpp @@ -6,23 +6,26 @@ #include "unicornlua/engine.hpp" #include "unicornlua/lua.hpp" -class LuaFixture { -public: +class LuaFixture +{ + public: LuaFixture(); virtual ~LuaFixture(); - lua_State* L; + lua_State *L; }; -class EngineFixture : public LuaFixture { -public: +class EngineFixture : public LuaFixture +{ + public: EngineFixture(); - UCLuaEngine* uclua_engine; + UCLuaEngine *uclua_engine; }; -class AutoclosingEngineFixture : public EngineFixture { -public: +class AutoclosingEngineFixture : public EngineFixture +{ + public: ~AutoclosingEngineFixture() override; }; diff --git a/tests/c/hooks.cpp b/tests/c/hooks.cpp index ccc98618..16956b7f 100644 --- a/tests/c/hooks.cpp +++ b/tests/c/hooks.cpp @@ -5,12 +5,11 @@ TEST_CASE_FIXTURE(AutoclosingEngineFixture, "Empty Hook created as expected") { - Hook* hook = uclua_engine->create_empty_hook(); + Hook *hook = uclua_engine->create_empty_hook(); CHECK_MESSAGE(hook->get_callback() == LUA_NOREF, - "Hook callback should be undefined."); + "Hook callback should be undefined."); CHECK_MESSAGE(hook->get_user_data() == LUA_REFNIL, - "User data bound to hook should be nil by default."); - CHECK_MESSAGE( - hook->get_hook_handle() == 0, "Hook handle should be 0 by default."); + "User data bound to hook should be nil by default."); + CHECK_MESSAGE(hook->get_hook_handle() == 0, "Hook handle should be 0 by default."); } diff --git a/tests/c/registers.cpp b/tests/c/registers.cpp index 3e059ea6..839e2708 100644 --- a/tests/c/registers.cpp +++ b/tests/c/registers.cpp @@ -9,12 +9,10 @@ #include "unicornlua/registers.hpp" // Copied and pasted from registers.cpp because of linker errors -static const uint8_t kFP80PositiveInfinity[] - = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x7f }; -static const uint8_t kFP80NegativeInfinity[] - = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0xff }; -static const uint8_t kFP80QuietNaN[] - = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; +static const uint8_t kFP80PositiveInfinity[] = {0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x7f}; +static const uint8_t kFP80NegativeInfinity[] = {0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0xff}; +static const uint8_t kFP80QuietNaN[] = {0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff}; // This is deliberate. C++ apparently defaults to all produced NaN being quiet, // so somewhere in here this gets lost in translation, and we can't produce @@ -23,7 +21,7 @@ static const uint8_t kFP80QuietNaN[] TEST_CASE("read_float80(): all zeros = 0") { - const uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + const uint8_t data[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; lua_Number result = read_float80(data); CHECK_EQ(errno, 0); @@ -32,7 +30,7 @@ TEST_CASE("read_float80(): all zeros = 0") TEST_CASE("read_float80(): fp indefinite, sign = 0") { - const uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f }; + const uint8_t data[] = {0, 0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f}; lua_Number result = read_float80(data); CHECK_EQ(errno, 0); @@ -41,7 +39,7 @@ TEST_CASE("read_float80(): fp indefinite, sign = 0") TEST_CASE("read_float80(): fp indefinite, sign = 1") { - const uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0xff }; + const uint8_t data[] = {0, 0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0xff}; lua_Number result = read_float80(data); CHECK_EQ(errno, 0); @@ -50,7 +48,7 @@ TEST_CASE("read_float80(): fp indefinite, sign = 1") TEST_CASE("read_float80(): +INF") { - const uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x7f }; + const uint8_t data[] = {0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x7f}; lua_Number result = read_float80(data); CHECK_EQ(errno, 0); @@ -60,7 +58,7 @@ TEST_CASE("read_float80(): +INF") TEST_CASE("read_float80(): -INF") { - const uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0xff }; + const uint8_t data[] = {0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0xff}; lua_Number result = read_float80(data); CHECK_EQ(errno, 0); @@ -70,7 +68,7 @@ TEST_CASE("read_float80(): -INF") TEST_CASE("read_float80(): qNaN, sign = 0") { - const uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f }; + const uint8_t data[] = {0, 0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f}; lua_Number result = read_float80(data); CHECK_EQ(errno, 0); @@ -79,7 +77,7 @@ TEST_CASE("read_float80(): qNaN, sign = 0") TEST_CASE("read_float80(): qNaN, sign = 1") { - const uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0xff }; + const uint8_t data[] = {0, 0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0xff}; lua_Number result = read_float80(data); CHECK_EQ(errno, 0); @@ -88,7 +86,7 @@ TEST_CASE("read_float80(): qNaN, sign = 1") TEST_CASE("read_float80(): 7FFF4000000000000000 = NaN (invalid 80387+)") { - const uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0x40, 0xff, 0x7f }; + const uint8_t data[] = {0, 0, 0, 0, 0, 0, 0, 0x40, 0xff, 0x7f}; lua_Number result = read_float80(data); CHECK_MESSAGE(errno == EINVAL, "errno should be EINVAL"); CHECK(std::isnan(result)); @@ -96,8 +94,7 @@ TEST_CASE("read_float80(): 7FFF4000000000000000 = NaN (invalid 80387+)") TEST_CASE("read_float80(): 7FFF8BADC0FFEE15DEAD = sNaN") { - const uint8_t data[] - = { 0xad, 0xde, 0x15, 0xee, 0xff, 0xc0, 0xad, 0x8b, 0xff, 0x7f }; + const uint8_t data[] = {0xad, 0xde, 0x15, 0xee, 0xff, 0xc0, 0xad, 0x8b, 0xff, 0x7f}; lua_Number result = read_float80(data); // CHECK_MESSAGE(errno == EDOM, "errno should be EDOM"); // TODO (dargueta): How do we check if this is a signaling NaN? @@ -107,7 +104,7 @@ TEST_CASE("read_float80(): 7FFF8BADC0FFEE15DEAD = sNaN") TEST_CASE("read_float80(): 3FFF8000000000000001 == 1.0") { int exponent; - const uint8_t data[] = { 1, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x3f }; + const uint8_t data[] = {1, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x3f}; lua_Number result = read_float80(data); CHECK_EQ(errno, 0); @@ -121,7 +118,7 @@ TEST_CASE("read_float80(): 3FFF8000000000000001 == 1.0") TEST_CASE("read_float80(): 3FFE8000000000000001 == 0.5") { int exponent; - const uint8_t data[] = { 1, 0, 0, 0, 0, 0, 0, 0x80, 0xfe, 0x3f }; + const uint8_t data[] = {1, 0, 0, 0, 0, 0, 0, 0x80, 0xfe, 0x3f}; lua_Number result = read_float80(data); CHECK_EQ(errno, 0); @@ -135,7 +132,7 @@ TEST_CASE("read_float80(): 3FFE8000000000000001 == 0.5") TEST_CASE("read_float80(): 3FFE8000000000000100 == 1.0") { int exponent; - const uint8_t data[] = { 4, 0, 0, 0, 0, 0, 0, 0x80, 0xfe, 0x3f }; + const uint8_t data[] = {4, 0, 0, 0, 0, 0, 0, 0x80, 0xfe, 0x3f}; lua_Number result = read_float80(data); CHECK_EQ(errno, 0); @@ -178,7 +175,7 @@ TEST_CASE("read_float80(): C000C90FDAA2922A8000 == -3.141592654") { TEST_CASE("write_float80(): 0 -> 00000000000000000000") { - const uint8_t expected[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + const uint8_t expected[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; uint8_t result[10]; write_float80(0, result); @@ -279,14 +276,14 @@ TEST_CASE("Register::as_int64(): 0xCAFEBEEF00000000 == -3819405500257140736") TEST_CASE("Register::as_float32(): 3.141592654") { uclua_float32 value = 3.141592654; - Register reg((const char*)&value, UL_REG_TYPE_FLOAT32); + Register reg((const char *)&value, UL_REG_TYPE_FLOAT32); CHECK_EQ(reg.as_float32(), doctest::Approx(value)); } TEST_CASE("Register::as_float64(): 1.4142135623730951") { uclua_float64 value = 1.4142135623730951; - Register reg((const char*)&value, UL_REG_TYPE_FLOAT64); + Register reg((const char *)&value, UL_REG_TYPE_FLOAT64); CHECK_EQ(reg.as_float64(), doctest::Approx(value)); } @@ -304,8 +301,8 @@ TEST_CASE("Register::as_float80(): 2.71828182845904524") { TEST_CASE("Register::as_8xi8()") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT8_MAX) - INT8_MAX; Register reg(expected.data(), UL_REG_TYPE_INT8_ARRAY_8); @@ -314,8 +311,8 @@ TEST_CASE("Register::as_8xi8()") TEST_CASE("Register::as_4xi16()") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT16_MAX) - INT16_MAX; Register reg(expected.data(), UL_REG_TYPE_INT16_ARRAY_4); @@ -324,8 +321,8 @@ TEST_CASE("Register::as_4xi16()") TEST_CASE("Register::as_2xi32()") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT32_MAX) - INT32_MAX; Register reg(expected.data(), UL_REG_TYPE_INT32_ARRAY_2); @@ -334,8 +331,8 @@ TEST_CASE("Register::as_2xi32()") TEST_CASE("Register::as_1xi64()") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT64_MAX) - INT64_MAX; Register reg(expected.data(), UL_REG_TYPE_INT64_ARRAY_1); @@ -344,8 +341,8 @@ TEST_CASE("Register::as_1xi64()") TEST_CASE("Register::as_8xi16()") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT16_MAX) - INT16_MAX; Register reg(expected.data(), UL_REG_TYPE_INT16_ARRAY_8); @@ -354,8 +351,8 @@ TEST_CASE("Register::as_8xi16()") TEST_CASE("Register::as_4xi32()") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT32_MAX) - INT32_MAX; Register reg(expected.data(), UL_REG_TYPE_INT32_ARRAY_4); @@ -364,8 +361,8 @@ TEST_CASE("Register::as_4xi32()") TEST_CASE("Register::as_2xi64()") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT64_MAX) - INT64_MAX; Register reg(expected.data(), UL_REG_TYPE_INT64_ARRAY_2); @@ -374,8 +371,8 @@ TEST_CASE("Register::as_2xi64()") TEST_CASE("Register::as_32xi8()") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT8_MAX) - INT8_MAX; Register reg(expected.data(), UL_REG_TYPE_INT8_ARRAY_32); @@ -384,8 +381,8 @@ TEST_CASE("Register::as_32xi8()") TEST_CASE("Register::as_16xi16()") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT16_MAX) - INT16_MAX; Register reg(expected.data(), UL_REG_TYPE_INT16_ARRAY_16); @@ -394,8 +391,8 @@ TEST_CASE("Register::as_16xi16()") TEST_CASE("Register::as_8xi32()") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT32_MAX) - INT32_MAX; Register reg(expected.data(), UL_REG_TYPE_INT32_ARRAY_8); @@ -404,8 +401,8 @@ TEST_CASE("Register::as_8xi32()") TEST_CASE("Register::as_4xi64()") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT64_MAX) - INT64_MAX; Register reg(expected.data(), UL_REG_TYPE_INT64_ARRAY_4); @@ -414,22 +411,22 @@ TEST_CASE("Register::as_4xi64()") TEST_CASE("Register::as_4xf32()") { - std::array expected { 3.141592654, 1.23456789, 0, -1 }; + std::array expected{3.141592654, 1.23456789, 0, -1}; Register reg(expected.data(), UL_REG_TYPE_FLOAT32_ARRAY_4); CHECK_EQ(reg.as_4xf32(), expected); } TEST_CASE("Register::as_2xf64()") { - std::array expected { -3.141592654, 0 }; + std::array expected{-3.141592654, 0}; Register reg(expected.data(), UL_REG_TYPE_FLOAT64_ARRAY_2); CHECK_EQ(reg.as_2xf64(), expected); } TEST_CASE("Register::as_4xi64") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT64_MAX) - INT64_MAX; Register reg(expected.data(), UL_REG_TYPE_INT64_ARRAY_4); @@ -438,22 +435,22 @@ TEST_CASE("Register::as_4xi64") TEST_CASE("Register::as_8xf32") { - std::array expected {}; + std::array expected{}; Register reg(expected.data(), UL_REG_TYPE_FLOAT32_ARRAY_8); CHECK_EQ(reg.as_8xf32(), expected); } TEST_CASE("Register::as_4xf64") { - std::array expected { 9.99999, 2, 1, 8.0 }; + std::array expected{9.99999, 2, 1, 8.0}; Register reg(expected.data(), UL_REG_TYPE_FLOAT64_ARRAY_4); CHECK_EQ(reg.as_4xf64(), expected); } TEST_CASE("Register::as_64xi8") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT8_MAX) - INT8_MAX; Register reg(expected.data(), UL_REG_TYPE_INT8_ARRAY_64); @@ -462,8 +459,8 @@ TEST_CASE("Register::as_64xi8") TEST_CASE("Register::as_16xi32") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT32_MAX) - INT32_MAX; Register reg(expected.data(), UL_REG_TYPE_INT32_ARRAY_16); @@ -472,8 +469,8 @@ TEST_CASE("Register::as_16xi32") TEST_CASE("Register::as_8xi64") { - std::array expected {}; - for (auto& value : expected) + std::array expected{}; + for (auto &value : expected) value = (rand() % UINT64_MAX) - INT64_MAX; Register reg(expected.data(), UL_REG_TYPE_INT64_ARRAY_8); @@ -482,15 +479,15 @@ TEST_CASE("Register::as_8xi64") TEST_CASE("Register::as_16xf32") { - std::array expected {}; + std::array expected{}; Register reg(expected.data(), UL_REG_TYPE_FLOAT32_ARRAY_16); CHECK_EQ(reg.as_16xf32(), expected); } TEST_CASE("Register::as_8xf64") { - std::array expected { 0, 1, INFINITY, 123456789, - 0.987654321, 0x80000000, -189357923, -1 }; + std::array expected{0, 1, INFINITY, 123456789, + 0.987654321, 0x80000000, -189357923, -1}; Register reg(expected.data(), UL_REG_TYPE_FLOAT64_ARRAY_8); CHECK_EQ(reg.as_8xf64(), expected); } diff --git a/tests/c/utils.cpp b/tests/c/utils.cpp index 81036a67..9d2b9206 100644 --- a/tests/c/utils.cpp +++ b/tests/c/utils.cpp @@ -53,26 +53,26 @@ TEST_CASE_FIXTURE(LuaFixture, "[ul_create_weak_table] basic test -- weak values" #endif jmp_buf gCrashJmpBuffer; -const char* gExpectedErrorMessage; +const char *gExpectedErrorMessage; -int crash_handler(lua_State* L) +int crash_handler(lua_State *L) { - const char* error_message = lua_tostring(L, -1); + const char *error_message = lua_tostring(L, -1); CHECK_MESSAGE(strcmp(gExpectedErrorMessage, error_message) == 0, - "Error messages don't match."); + "Error messages don't match."); // Error message matches, jump back into the test. longjmp(gCrashJmpBuffer, 123); } -TEST_CASE_FIXTURE( - LuaFixture, "ul_crash_on_error() panics with the right error message") +TEST_CASE_FIXTURE(LuaFixture, "ul_crash_on_error() panics with the right error message") { gExpectedErrorMessage = uc_strerror(UC_ERR_OK); #if !IS_LUAJIT int recover_flag = setjmp(gCrashJmpBuffer); - if (recover_flag == 0) { + if (recover_flag == 0) + { lua_atpanic(L, crash_handler); ul_crash_on_error(L, UC_ERR_OK); // Execution won't continue past here (inside this block) @@ -85,24 +85,25 @@ TEST_CASE_FIXTURE( // Depending on the Lua version, there may be other stuff on the stack aside // from our error message. lua_pop(L, lua_gettop(L)); - CHECK_MESSAGE( - (lua_gettop(L) == 0), "Failed to clear the Lua stack on cleanup."); + CHECK_MESSAGE((lua_gettop(L) == 0), "Failed to clear the Lua stack on cleanup."); #else - try { + try + { ul_crash_on_error(L, UC_ERR_OK); - } catch (...) { + } + catch (...) + { // Some sort of unhandled exception happened. LuaJIT doesn't provide a // way for us to see inside that exception, but we *can* check the error // message. CHECK_MESSAGE(strcmp(lua_tostring(L, -1), uc_strerror(UC_ERR_OK)) == 0, - "Error message doesn't match what's expected."); + "Error message doesn't match what's expected."); // Clear out the stack or the test will fail. The error message will be // at the top of the stack but the interpreter is allowed to put other // stuff beneath it. lua_pop(L, lua_gettop(L)); - CHECK_MESSAGE( - (lua_gettop(L) == 0), "Failed to clear the Lua stack on cleanup."); + CHECK_MESSAGE((lua_gettop(L) == 0), "Failed to clear the Lua stack on cleanup."); return; } // If we get out here then an exception wasn't thrown.