From 41fd9be898b293b51cfc3e9ff18521fe182725f1 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Tue, 27 Apr 2021 10:10:09 +0800 Subject: [PATCH] [core] Proper way to use attribute (#518) * [core] Proper way to use attribute * Update riru --- core/build.gradle.kts | 2 +- .../main/include/art/runtime/class_linker.h | 9 +- core/src/main/cpp/main/include/base/object.h | 22 ++-- core/src/main/cpp/main/include/macros.h | 117 +----------------- core/src/main/cpp/main/include/native_util.h | 3 +- 5 files changed, 22 insertions(+), 131 deletions(-) diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 4880e503..c614d9eb 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -37,7 +37,7 @@ val authors = "LSPosed Developers" val riruModuleId = "lsposed" val moduleMinRiruApiVersion = 25 -val moduleMinRiruVersionName = "25.0.0" +val moduleMinRiruVersionName = "25.0.1" val moduleMaxRiruApiVersion = 25 val defaultManagerPackageName: String by rootProject.extra diff --git a/core/src/main/cpp/main/include/art/runtime/class_linker.h b/core/src/main/cpp/main/include/art/runtime/class_linker.h index 7cfb15e7..4f4e5a81 100644 --- a/core/src/main/cpp/main/include/art/runtime/class_linker.h +++ b/core/src/main/cpp/main/include/art/runtime/class_linker.h @@ -44,7 +44,8 @@ namespace art { SetEntryPointsToInterpreterSym(thiz, art_method); } - ALWAYS_INLINE static void MaybeDelayHook(void *clazz_ptr) { + [[gnu::always_inline]] + static void MaybeDelayHook(void *clazz_ptr) { art::mirror::Class mirror_class(clazz_ptr); auto class_def = mirror_class.GetClassDef(); bool should_intercept = class_def && lspd::IsClassPending(class_def); @@ -180,13 +181,15 @@ namespace art { } - ALWAYS_INLINE void MakeInitializedClassesVisiblyInitialized(void *self, bool wait) const { + [[gnu::always_inline]] + void MakeInitializedClassesVisiblyInitialized(void *self, bool wait) const { LOGD("MakeInitializedClassesVisiblyInitialized start, thiz=%p, self=%p", thiz_, self); if (LIKELY(thiz_)) MakeInitializedClassesVisiblyInitialized(thiz_, self, wait); } - ALWAYS_INLINE void SetEntryPointsToInterpreter(void *art_method) const { + [[gnu::always_inline]] + void SetEntryPointsToInterpreter(void *art_method) const { LOGD("SetEntryPointsToInterpreter start, thiz=%p, art_method=%p", thiz_, art_method); if (LIKELY(thiz_)) SetEntryPointsToInterpreter(thiz_, art_method); diff --git a/core/src/main/cpp/main/include/base/object.h b/core/src/main/cpp/main/include/base/object.h index 80ab47e1..4c1e1228 100644 --- a/core/src/main/cpp/main/include/base/object.h +++ b/core/src/main/cpp/main/include/base/object.h @@ -79,15 +79,18 @@ namespace lspd { ShadowObject(void *thiz) : thiz_(thiz) { } - ALWAYS_INLINE inline void *Get() { + [[gnu::always_inline]] + inline void *Get() { return thiz_; } - ALWAYS_INLINE inline void Reset(void *thiz) { + [[gnu::always_inline]] + inline void Reset(void *thiz) { thiz_ = thiz; } - ALWAYS_INLINE inline operator bool() const { + [[gnu::always_inline]] + inline operator bool() const { return thiz_ != nullptr; } @@ -100,21 +103,14 @@ namespace lspd { public: HookedObject(void *thiz) : ShadowObject(thiz) {} - - static void SetupSymbols(void *handle) { - - } - - static void SetupHooks(void *handle, HookFunType hook_fun) { - - } }; struct ObjPtr { void *data; }; - ALWAYS_INLINE static void *Dlsym(void *handle, const char *name) { + [[gnu::always_inline]] + static void *Dlsym(void *handle, const char *name) { return dlsym(handle, name); } @@ -127,7 +123,7 @@ namespace lspd { return Dlsym(handle, last...); } - ALWAYS_INLINE inline static void HookFunction(void *original, void *replace, void **backup) { + static void HookFunction(void *original, void *replace, void **backup) { _make_rwx(original, _page_size); hook_func(original, replace, backup); } diff --git a/core/src/main/cpp/main/include/macros.h b/core/src/main/cpp/main/include/macros.h index e4d23cc6..646730e1 100644 --- a/core/src/main/cpp/main/include/macros.h +++ b/core/src/main/cpp/main/include/macros.h @@ -15,84 +15,19 @@ #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(const TypeName&) = delete; \ void operator=(const TypeName&) = delete -// A macro to disallow all the implicit constructors, namely the -// default constructor, copy constructor and operator= functions. -// -// This should be used in the private: declarations for a class -// that wants to prevent anyone from instantiating it. This is -// especially useful for classes containing only static methods. -#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ - TypeName() = delete; \ - DISALLOW_COPY_AND_ASSIGN(TypeName) // The arraysize(arr) macro returns the # of elements in an array arr. // The expression is a compile-time constant, and therefore can be // used in defining new arrays, for example. If you use arraysize on // a pointer by mistake, you will get a compile-time error. -// -// One caveat is that arraysize() doesn't accept any array of an -// anonymous type or a type defined inside a function. In these rare -// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is -// due to a limitation in C++'s template system. The limitation might -// eventually be removed, but it hasn't happened yet. -// This template function declaration is used in defining arraysize. -// Note that the function doesn't need an implementation, as we only -// use its type. -template -char(&ArraySizeHelper(T(&array)[N]))[N]; // NOLINT(readability/casting) -#define arraysize(array) (sizeof(ArraySizeHelper(array))) -#define SIZEOF_MEMBER(t, f) sizeof(std::declval().f) +template +[[gnu::always_inline]] constexpr size_t arraysize(T(&)[N]) { + return N; +} // Changing this definition will cause you a lot of pain. A majority of // vendor code defines LIKELY and UNLIKELY this way, and includes // this header through an indirect path. #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) -#define WARN_UNUSED __attribute__((warn_unused_result)) -// A deprecated function to call to create a false use of the parameter, for -// example: -// int foo(int x) { UNUSED(x); return 10; } -// to avoid compiler warnings. Going forward we prefer ATTRIBUTE_UNUSED. -template -void UNUSED(const T&...) { -} -// An attribute to place on a parameter to a function, for example: -// int foo(int x ATTRIBUTE_UNUSED) { return 10; } -// to avoid compiler warnings. -#define ATTRIBUTE_UNUSED __attribute__((__unused__)) -// The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through -// between switch labels: -// switch (x) { -// case 40: -// case 41: -// if (truth_is_out_there) { -// ++x; -// FALLTHROUGH_INTENDED; // Use instead of/along with annotations in -// // comments. -// } else { -// return x; -// } -// case 42: -// ... -// -// As shown in the example above, the FALLTHROUGH_INTENDED macro should be -// followed by a semicolon. It is designed to mimic control-flow statements -// like 'break;', so it can be placed in most places where 'break;' can, but -// only if there are no statements on the execution path between it and the -// next switch label. -// -// When compiled with clang, the FALLTHROUGH_INTENDED macro is expanded to -// [[clang::fallthrough]] attribute, which is analysed when performing switch -// labels fall-through diagnostic ('-Wimplicit-fallthrough'). See clang -// documentation on language extensions for details: -// http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough -// -// When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no -// effect on diagnostics. -// -// In either case this macro has no effect on runtime behavior and performance -// of code. -#ifndef FALLTHROUGH_INTENDED -#define FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT -#endif // Current ABI string #if defined(__arm__) #define ABI_STRING "arm" @@ -107,50 +42,6 @@ void UNUSED(const T&...) { #elif defined(__mips__) && defined(__LP64__) #define ABI_STRING "mips64" #endif -// A macro to disallow new and delete operators for a class. It goes in the private: declarations. -// NOTE: Providing placement new (and matching delete) for constructing container elements. -#define DISALLOW_ALLOCATION() \ - public: \ - NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \ - ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \ - ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \ - private: \ - void* operator new(size_t) = delete // NOLINT -#define ALIGNED(x) __attribute__ ((__aligned__(x))) -#define PACKED(x) __attribute__ ((__aligned__(x), __packed__)) -// Stringify the argument. -#define QUOTE(x) #x -#define STRINGIFY(x) QUOTE(x) -// Append tokens after evaluating. -#define APPEND_TOKENS_AFTER_EVAL_2(a, b) a ## b -#define APPEND_TOKENS_AFTER_EVAL(a, b) APPEND_TOKENS_AFTER_EVAL_2(a, b) -#ifndef NDEBUG -#define ALWAYS_INLINE -#else -#define ALWAYS_INLINE __attribute__ ((always_inline)) -#endif -// clang doesn't like attributes on lambda functions. It would be nice to say: -// #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE -#define ALWAYS_INLINE_LAMBDA -#define NO_INLINE __attribute__ ((noinline)) -#if defined (__APPLE__) -#define HOT_ATTR -#define COLD_ATTR -#else -#define HOT_ATTR __attribute__ ((hot)) -#define COLD_ATTR __attribute__ ((cold)) -#endif -#define PURE __attribute__ ((__pure__)) -// Define that a position within code is unreachable, for example: -// int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); } -// without the UNREACHABLE a return statement would be necessary. -#define UNREACHABLE __builtin_unreachable -// Add the C++11 noreturn attribute. -#define NO_RETURN [[ noreturn ]] // NOLINT[whitespace/braces] [5] -// Annotalysis thread-safety analysis support. Things that are not in base. -#define LOCKABLE CAPABILITY("mutex") -#define SHARED_LOCKABLE SHARED_CAPABILITY("mutex") - // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. It goes in the private: // declarations in a class. #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ diff --git a/core/src/main/cpp/main/include/native_util.h b/core/src/main/cpp/main/include/native_util.h index b5ca1219..6e7880cf 100644 --- a/core/src/main/cpp/main/include/native_util.h +++ b/core/src/main/cpp/main/include/native_util.h @@ -30,7 +30,8 @@ namespace lspd { - ALWAYS_INLINE inline bool RegisterNativeMethodsInternal(JNIEnv *env, + [[gnu::always_inline]] + inline bool RegisterNativeMethodsInternal(JNIEnv *env, const char *class_name, const JNINativeMethod *methods, jint method_count) {