2022-02-16 07:24:35 +08:00
|
|
|
#pragma once
|
|
|
|
|
2022-03-11 19:37:11 +08:00
|
|
|
#include <sys/system_properties.h>
|
|
|
|
|
|
|
|
#include <list>
|
2022-02-16 07:24:35 +08:00
|
|
|
#include <shared_mutex>
|
2022-03-11 19:37:11 +08:00
|
|
|
#include <string_view>
|
2022-02-16 07:24:35 +08:00
|
|
|
#include <unordered_map>
|
2022-03-14 00:07:14 +08:00
|
|
|
#include <unordered_set>
|
2022-03-11 19:37:11 +08:00
|
|
|
|
2022-02-16 07:24:35 +08:00
|
|
|
#include "logging.hpp"
|
|
|
|
#include "lsplant.hpp"
|
2022-03-11 19:37:11 +08:00
|
|
|
#include "utils/hook_helper.hpp"
|
2022-02-16 07:24:35 +08:00
|
|
|
|
|
|
|
namespace lsplant {
|
|
|
|
|
|
|
|
enum class Arch {
|
|
|
|
kArm,
|
|
|
|
kArm64,
|
|
|
|
kX86,
|
|
|
|
kX8664,
|
|
|
|
};
|
|
|
|
|
|
|
|
consteval inline Arch GetArch() {
|
|
|
|
#if defined(__i386__)
|
|
|
|
return Arch::kX86;
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
return Arch::kX8664;
|
|
|
|
#elif defined(__arm__)
|
|
|
|
return Arch::kArm;
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
return Arch::kArm64;
|
|
|
|
#else
|
2022-03-11 19:37:11 +08:00
|
|
|
#error "unsupported architecture"
|
2022-02-16 07:24:35 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static constexpr auto kArch = GetArch();
|
|
|
|
|
2022-03-11 19:37:11 +08:00
|
|
|
template <typename T>
|
2022-02-16 07:24:35 +08:00
|
|
|
constexpr inline auto RoundUpTo(T v, size_t size) {
|
|
|
|
return v + size - 1 - ((v + size - 1) & (size - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto GetAndroidApiLevel() {
|
|
|
|
static auto kApiLevel = []() {
|
|
|
|
std::array<char, PROP_VALUE_MAX> prop_value;
|
|
|
|
__system_property_get("ro.build.version.sdk", prop_value.data());
|
|
|
|
int base = atoi(prop_value.data());
|
|
|
|
__system_property_get("ro.build.version.preview_sdk", prop_value.data());
|
|
|
|
return base + atoi(prop_value.data());
|
|
|
|
}();
|
|
|
|
return kApiLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static constexpr auto kPointerSize = sizeof(void *);
|
|
|
|
|
|
|
|
namespace art {
|
2022-03-12 16:10:55 +08:00
|
|
|
class ArtMethod;
|
2022-03-14 00:07:14 +08:00
|
|
|
namespace dex {
|
|
|
|
class ClassDef;
|
|
|
|
}
|
|
|
|
namespace mirror {
|
|
|
|
class Class;
|
|
|
|
}
|
2022-03-11 19:37:11 +08:00
|
|
|
} // namespace art
|
2022-02-16 07:24:35 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
// target, backup
|
2022-03-12 02:04:58 +08:00
|
|
|
inline std::unordered_map<art::ArtMethod *, std::pair<jobject, art::ArtMethod *>> hooked_methods_;
|
2022-02-16 07:24:35 +08:00
|
|
|
inline std::shared_mutex hooked_methods_lock_;
|
|
|
|
|
|
|
|
inline std::list<std::pair<art::ArtMethod *, art::ArtMethod *>> jit_movements_;
|
|
|
|
inline std::shared_mutex jit_movements_lock_;
|
2022-03-14 00:07:14 +08:00
|
|
|
|
|
|
|
inline std::unordered_map<const art::dex::ClassDef *, std::unordered_set<art::ArtMethod *>>
|
|
|
|
hooked_classes_;
|
|
|
|
inline std::shared_mutex hooked_classes_lock_;
|
2022-03-11 19:37:11 +08:00
|
|
|
} // namespace
|
2022-02-16 07:24:35 +08:00
|
|
|
|
|
|
|
inline bool IsHooked(art::ArtMethod *art_method) {
|
|
|
|
std::shared_lock lk(hooked_methods_lock_);
|
|
|
|
return hooked_methods_.contains(art_method);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::list<std::pair<art::ArtMethod *, art::ArtMethod *>> GetJitMovements() {
|
|
|
|
std::unique_lock lk(jit_movements_lock_);
|
|
|
|
return std::move(jit_movements_);
|
|
|
|
}
|
|
|
|
|
2022-03-14 00:07:14 +08:00
|
|
|
inline void RecordHooked(art::ArtMethod *target, const art::dex::ClassDef *class_def,
|
|
|
|
jobject reflected_backup, art::ArtMethod *backup) {
|
|
|
|
{
|
|
|
|
std::unique_lock lk(hooked_methods_lock_);
|
|
|
|
hooked_methods_[target] = {reflected_backup, backup};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::unique_lock lk(hooked_classes_lock_);
|
|
|
|
hooked_classes_[class_def].emplace(target);
|
|
|
|
}
|
2022-02-16 07:24:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void RecordJitMovement(art::ArtMethod *target, art::ArtMethod *backup) {
|
|
|
|
std::unique_lock lk(jit_movements_lock_);
|
|
|
|
jit_movements_.emplace_back(target, backup);
|
|
|
|
}
|
|
|
|
|
2022-03-11 19:37:11 +08:00
|
|
|
} // namespace lsplant
|