Ver código fonte

add dobby. (#5920)

* add dobby.

* dobby: follow config style suggestions.

* dobby: remove comments.

* dobby: update.

* dobby: fix syntax error.

* dobby: add homepage.

* dobby: add android & iphoneos platform.

* dobby: fix missing import.

* dobby: fix link under android.

* dobby: re-add cmake system processor defs to fix build under macos.

* dobby: try fix build under android ndk r27.

* dobby: disable shared build under macosx.

* Update xmake.lua

* Update xmake.lua

---------

Co-authored-by: ruki <[email protected]>
RedbeanW 9 meses atrás
pai
commit
3553f714b2

+ 130 - 0
packages/d/dobby/patches/fix-compile-on-lower-version-of-gcc.patch

@@ -0,0 +1,130 @@
+From 24c79d845bcc1d3de35ea9052665a68f89ff6a02 Mon Sep 17 00:00:00 2001
+From: Redbeanw44602 <[email protected]>
+Date: Sat, 31 Aug 2024 01:33:04 +0800
+Subject: [PATCH] fix compile on lower version of gcc.
+
+---
+ .../ImportTableReplace/dobby_import_replace.cc        |  4 ++--
+ examples/socket_example.cc                            |  4 +---
+ .../ClosureTrampolineBridge/common_bridge_handler.cc  |  4 +---
+ source/dobby/pac_kit.h                                |  8 +++++---
+ source/dobby/platform_features.h                      | 11 ++++++++++-
+ 5 files changed, 19 insertions(+), 12 deletions(-)
+
+diff --git a/builtin-plugin/ImportTableReplace/dobby_import_replace.cc b/builtin-plugin/ImportTableReplace/dobby_import_replace.cc
+index eab1af9..1b7b573 100644
+--- a/builtin-plugin/ImportTableReplace/dobby_import_replace.cc
++++ b/builtin-plugin/ImportTableReplace/dobby_import_replace.cc
+@@ -172,13 +172,13 @@ PUBLIC int DobbyImportTableReplace(char *image_name, char *symbol_name, void *fa
+     if (stub) {
+       void *orig_func;
+       orig_func = *(void **)stub;
+-#if __has_feature(ptrauth_calls)
++#if __has_ptrauth_calls
+       orig_func = ptrauth_strip(orig_func, ptrauth_key_asia);
+       orig_func = ptrauth_sign_unauthenticated(orig_func, ptrauth_key_asia, 0);
+ #endif
+       *orig_func_ptr = orig_func;
+ 
+-#if __has_feature(ptrauth_calls)
++#if __has_ptrauth_calls
+       fake_func = (void *)ptrauth_strip(fake_func, ptrauth_key_asia);
+       fake_func = ptrauth_sign_unauthenticated(fake_func, ptrauth_key_asia, stub);
+ #endif
+diff --git a/examples/socket_example.cc b/examples/socket_example.cc
+index 1377fa3..07e2dcc 100644
+--- a/examples/socket_example.cc
++++ b/examples/socket_example.cc
+@@ -43,12 +43,10 @@ const char *func_short_array[] = {
+ // clang-format on
+ 
+ #define pac_strip(symbol)
+-#if defined(__APPLE__) && __arm64e__
+-#if __has_feature(ptrauth_calls)
++#if __has_ptrauth_calls
+ #define pac_strip(symbol)
+ //#define pac_strip(symbol) *(void **)&symbol = (void *)ptrauth_sign_unauthenticated((void *)symbol, ptrauth_key_asia, 0)
+ #endif
+-#endif
+ 
+ #define install_hook(name, fn_ret_t, fn_args_t...)                                                                     \
+   fn_ret_t (*orig_##name)(fn_args_t);                                                                                  \
+diff --git a/source/TrampolineBridge/ClosureTrampolineBridge/common_bridge_handler.cc b/source/TrampolineBridge/ClosureTrampolineBridge/common_bridge_handler.cc
+index b1be02a..b7757e0 100644
+--- a/source/TrampolineBridge/ClosureTrampolineBridge/common_bridge_handler.cc
++++ b/source/TrampolineBridge/ClosureTrampolineBridge/common_bridge_handler.cc
+@@ -9,13 +9,11 @@ PUBLIC void common_closure_bridge_handler(DobbyRegisterContext *ctx, ClosureTram
+   typedef void (*routing_handler_t)(InterceptEntry *, DobbyRegisterContext *);
+   auto routing_handler = (routing_handler_t)entry->carry_handler;
+ 
+-#if defined(__APPLE__) && __arm64e__
+-#if __has_feature(ptrauth_calls)
++#if __has_ptrauth_calls
+   uint64_t discriminator = 0;
+   // discriminator = __builtin_ptrauth_type_discriminator(__typeof(routing_handler));
+   routing_handler = (__typeof(routing_handler))__builtin_ptrauth_sign_unauthenticated((void *)routing_handler,
+                                                                                       ptrauth_key_asia, discriminator);
+-#endif
+ #endif
+ 
+   routing_handler((InterceptEntry *)entry->carry_data, ctx);
+diff --git a/source/dobby/pac_kit.h b/source/dobby/pac_kit.h
+index 12bf097..c8c3dbb 100644
+--- a/source/dobby/pac_kit.h
++++ b/source/dobby/pac_kit.h
+@@ -2,9 +2,11 @@
+ 
+ #include <stdint.h>
+ 
++#include "platform_features.h"
++
+ #ifndef PAC_KIT
+ #define PAC_KIT
+-#if defined(__arm64e__) && __has_feature(ptrauth_calls)
++#if __has_ptrauth_calls
+ #include <ptrauth.h>
+ #endif
+ 
+@@ -12,7 +14,7 @@ static inline void *pac_strip(void *addr) {
+   if (addr == NULL) {
+     return NULL;
+   }
+-#if __has_feature(ptrauth_calls)
++#if __has_ptrauth_calls
+   addr = ptrauth_strip(addr, ptrauth_key_asia);
+ #endif
+   return addr;
+@@ -22,7 +24,7 @@ static inline void *pac_sign(void *addr) {
+   if (addr == NULL) {
+     return NULL;
+   }
+-#if __has_feature(ptrauth_calls)
++#if __has_ptrauth_calls
+   addr = ptrauth_sign_unauthenticated((void *)addr, ptrauth_key_asia, 0);
+ #endif
+   return addr;
+diff --git a/source/dobby/platform_features.h b/source/dobby/platform_features.h
+index fb9076a..8e9a5c5 100644
+--- a/source/dobby/platform_features.h
++++ b/source/dobby/platform_features.h
+@@ -1,7 +1,16 @@
+ #pragma once
+ 
++// __has_feature is only supported in clang or gcc >= 14.0. To avoid compilation failure in lower versions
++// of gcc, please should not use this extension directly in project.
++#if defined(__has_feature)
++// ptrauth_calls is only implemented in Apple Clang.
++#if __has_feature(ptrauth_calls) // Do not use && directly, it will cause gcc < 14.0 to fail to compile
++#define __has_ptrauth_calls (1)
++#endif
++#endif
++
+ #if defined(__APPLE__) && __arm64e__
+-#if __has_feature(ptrauth_calls)
++#if __has_ptrauth_calls
+ #include <ptrauth.h>
+ #endif
+ #endif
+-- 
+2.47.1
+

+ 65 - 0
packages/d/dobby/xmake.lua

@@ -0,0 +1,65 @@
+package("dobby")
+    set_homepage("https://github.com/jmpews/Dobby")
+    set_description("a lightweight, multi-platform, multi-architecture hook framework.")
+    set_license("Apache-2.0")
+
+    add_urls("https://github.com/jmpews/Dobby.git")
+
+    add_versions("2023.4.14", "0932d69c320e786672361ab53825ba8f4245e9d3")
+    
+    add_patches("2023.4.14", path.join(os.scriptdir(), "patches", "fix-compile-on-lower-version-of-gcc.patch"), "632aad7d79e2afd9587089a39c3eb2b64a3750ab3c8954f04672c13abcddbbae")
+
+    add_configs("symbol_resolver", {description = "Enable symbol resolver plugin.", default = true,  type = "boolean"})
+    add_configs("import_table_replacer", {description = "Enable import table replacer plugin.", default = false, type = "boolean"})
+    add_configs("android_bionic_linker_utils", {description = "Enable android bionic linker utils.",  default = false, type = "boolean"})
+
+    add_configs("near_branch", {description = "Enable near branch trampoline.", default = true,  type = "boolean"})
+    add_configs("full_floating_point_register_pack", {description = "Enables saving and packing of all floating-point registers.", default = false, type = "boolean"})
+
+    if is_plat("macosx") then
+        -- @see https://github.com/xmake-io/xmake-repo/pull/5920#issuecomment-2522876049
+        add_configs("shared", {description = "Build shared library.", default = false, type = "boolean", readonly = true})
+    end
+
+    if is_plat("linux", "bsd") then
+        add_syslinks("pthread")
+    end
+
+    add_deps("cmake")
+
+    on_install("linux", "macosx", "android", "iphoneos", function (package)
+        local configs = {"-DDOBBY_BUILD_EXAMPLE=OFF", "-DDOBBY_BUILD_TEST=OFF"}
+        table.insert(configs, "-DDOBBY_DEBUG=" .. (package:debug() and "ON" or "OFF"))
+        table.insert(configs, "-DPlugin.SymbolResolver=" .. (package:config("symbol_resolver") and "ON" or "OFF"))
+        table.insert(configs, "-DPlugin.ImportTableReplace=" .. (package:config("import_table_replacer") and "ON" or "OFF"))
+        table.insert(configs, "-DPlugin.Android.BionicLinkerUtil=" .. (package:config("android_bionic_linker_utils") and "ON" or "OFF"))
+        table.insert(configs, "-DNearBranch=" .. (package:config("near_branch") and "ON" or "OFF"))
+        table.insert(configs, "-DFullFloatingPointRegisterPack=" .. (package:config("full_floating_point_register_pack") and "ON" or "OFF"))
+        table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
+
+        if package:is_plat("android") then
+            local ndk = package:toolchain("ndk")
+            table.insert(configs, "-DCMAKE_ANDROID_NDK=" .. ndk:config("ndk"))
+            table.insert(configs, "-DCMAKE_ANDROID_ARCH_ABI=" .. package:arch())
+            local sdkver = "21"
+            if package:is_arch("armeabi-v7a", "x86") then
+                sdkver = "19"
+            end
+            table.insert(configs, "-DCMAKE_SYSTEM_VERSION=" .. sdkver)
+        elseif package:is_plat("iphoneos") then
+            table.insert(configs, "-DCMAKE_SYSTEM_PROCESSOR=" .. package:arch())
+            table.insert(configs, "-DCMAKE_OSX_DEPLOYMENT_TARGET=9.3") -- @from scripts/platform_builder.py:158
+        end
+
+        import("package.tools.cmake").install(package, configs, {buildir = "build"})
+        os.trycp("include", package:installdir())
+        os.trycp(package:config("shared") and "build/**.so" or "build/**.a", package:installdir("lib"))
+    end)
+
+    on_test(function (package)
+        assert(package:check_cxxsnippets({test = [[
+            void test() {
+                DobbyGetVersion();
+            }
+        ]]}, {configs = {languages = "c++11"}, includes = "dobby.h"}))
+    end)