Skip to content

Commit

Permalink
feat: major changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Colibrow committed Nov 5, 2024
1 parent 642257e commit 2aa180c
Show file tree
Hide file tree
Showing 112 changed files with 5,304 additions and 3,539 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020-2022 ByteDance, Inc.
Copyright (c) 2020-2024 ByteDance, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ByteHook

![](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)
![](https://img.shields.io/badge/release-1.0.10-red.svg?style=flat)
![](https://img.shields.io/badge/release-1.1.0-red.svg?style=flat)
![](https://img.shields.io/badge/Android-4.1%20--%2014-blue.svg?style=flat)
![](https://img.shields.io/badge/arch-armeabi--v7a%20%7C%20arm64--v8a%20%7C%20x86%20%7C%20x86__64-blue.svg?style=flat)

Expand Down Expand Up @@ -47,7 +47,7 @@ android {
}
dependencies {
implementation 'com.bytedance:bytehook:1.0.10'
implementation 'com.bytedance:bytehook:1.1.0'
}
```

Expand Down Expand Up @@ -114,6 +114,12 @@ android {
}
```

Note: If you use prefab dependency bytehook under x86 and x86_64 architectures, you need to add [prefab_bypass.gradle](gradle/prefab_bypass.gradle) to the module's build.gradle.

```Gradle
apply from: rootProject.file('gradle/prefab_bypass.gradle')
```

### 5. Initialize

```Java
Expand Down Expand Up @@ -184,9 +190,6 @@ ByteHook uses the following third-party source code or libraries:
* [queue.h](bytehook/src/main/cpp/third_party/bsd/queue.h)
BSD 3-Clause License
Copyright (c) 1991, 1993 The Regents of the University of California.
* [tree.h](bytehook/src/main/cpp/third_party/bsd/tree.h)
BSD 2-Clause License
Copyright (c) 2002 Niels Provos <[email protected]>
* [linux-syscall-support](https://chromium.googlesource.com/linux-syscall-support/)
BSD 3-Clause License
Copyright (c) 2005-2011 Google Inc.
13 changes: 8 additions & 5 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ByteHook

![](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)
![](https://img.shields.io/badge/release-1.0.10-red.svg?style=flat)
![](https://img.shields.io/badge/release-1.1.0-red.svg?style=flat)
![](https://img.shields.io/badge/Android-4.1%20--%2014-blue.svg?style=flat)
![](https://img.shields.io/badge/arch-armeabi--v7a%20%7C%20arm64--v8a%20%7C%20x86%20%7C%20x86__64-blue.svg?style=flat)

Expand Down Expand Up @@ -47,7 +47,7 @@ android {
}
dependencies {
implementation 'com.bytedance:bytehook:1.0.10'
implementation 'com.bytedance:bytehook:1.1.0'
}
```

Expand Down Expand Up @@ -114,6 +114,12 @@ android {
}
```

注意:如果在 x86 和 x86_64 架构下使用 prefab 依赖 bytehook 时需要在模块的 build.gradle 中添加 [prefab_bypass.gradle](gradle/prefab_bypass.gradle)

```Gradle
apply from: rootProject.file('gradle/prefab_bypass.gradle')
```

### 5. 初始化

```Java
Expand Down Expand Up @@ -184,9 +190,6 @@ ByteHook 使用了以下第三方源码或库:
* [queue.h](bytehook/src/main/cpp/third_party/bsd/queue.h)
BSD 3-Clause License
Copyright (c) 1991, 1993 The Regents of the University of California.
* [tree.h](bytehook/src/main/cpp/third_party/bsd/tree.h)
BSD 2-Clause License
Copyright (c) 2002 Niels Provos <[email protected]>
* [linux-syscall-support](https://chromium.googlesource.com/linux-syscall-support/)
BSD 3-Clause License
Copyright (c) 2005-2011 Google Inc.
16 changes: 9 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
id 'com.android.application' version '8.7.1' apply false
id 'com.android.library' version '8.7.1' apply false
}

task clean(type: Delete) {
Expand All @@ -9,19 +9,21 @@ task clean(type: Delete) {

ext {
minSdkVersion = 16
compileSdkVersion = 34
targetSdkVersion = 34
buildToolsVersion = "34.0.0"
compileSdkVersion = 35
targetSdkVersion = 35
buildToolsVersion = "35.0.0"
javaVersion = JavaVersion.VERSION_1_7
ndkVersion = "23.2.8568313"
cmakeVersion = "3.22.1"
cmakeVersion = "3.30.5"
shadowhookVersion = "1.1.1"
// abiFilters = "armeabi-v7a,arm64-v8a,x86,x86_64"
abiFilters = "armeabi-v7a,arm64-v8a,x86,x86_64"
useASAN = false
dependencyOnLocalLibrary = true

POM_GROUP_ID = "com.bytedance"
POM_ARTIFACT_ID = "bytehook"
POM_VERSION_NAME = "1.0.10"
POM_VERSION_NAME = "1.1.0"

POM_NAME = "bytehook"
POM_DESCRIPTION = "ByteHook is an Android PLT hook library which supports armeabi-v7a, arm64-v8a, x86 and x86_64."
Expand Down
10 changes: 10 additions & 0 deletions bytehook/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ android {
minifyEnabled false
}
}
packagingOptions {
exclude '**/libshadowhook.so'
exclude '**/libshadowhook_nothing.so'
}
buildFeatures {
prefabPublishing true
buildConfig false
prefab true
}
prefab {
bytehook {
Expand All @@ -57,5 +62,10 @@ android {
}
}

dependencies {
api 'com.bytedance.android:shadowhook:' + rootProject.ext.shadowhookVersion
}

apply from: rootProject.file('gradle/prefab_bypass.gradle')
apply from: rootProject.file('gradle/check.gradle')
apply from: rootProject.file('gradle/publish.gradle')
28 changes: 28 additions & 0 deletions bytehook/local_dependency.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# We found that AGP could not generate the correct cmake configuration
# when relying on a prefab package generated by another local project.
# We had to write one ourselves.
#
# Use it like this:
#
# if(DEPENDENCY_ON_LOCAL_LIBRARY)
# include(${CMAKE_CURRENT_SOURCE_DIR}/../../../../bytehook/local_dependency.cmake)
# else()
# find_package(bytehook REQUIRED CONFIG)
# endif()
#
# target_link_libraries(myapp bytehook::bytehook)
#

string(TOLOWER ${CMAKE_BUILD_TYPE} BYTEHOOK_BUILD_TYPE_DIRNAME)
if(${BYTEHOOK_BUILD_TYPE_DIRNAME} STREQUAL "relwithdebinfo")
set(BYTEHOOK_BUILD_TYPE_DIRNAME "release")
endif()

set(BYTEHOOK_BASE ${CMAKE_CURRENT_SOURCE_DIR}/../../../../bytehook)

add_library(bytehook::bytehook SHARED IMPORTED)
set_target_properties(bytehook::bytehook PROPERTIES
IMPORTED_LOCATION "${BYTEHOOK_BASE}/build/intermediates/cmake/${BYTEHOOK_BUILD_TYPE_DIRNAME}/obj/${ANDROID_ABI}/libbytehook.so"
INTERFACE_INCLUDE_DIRECTORIES "${BYTEHOOK_BASE}/src/main/cpp/include"
INTERFACE_LINK_LIBRARIES ""
)
17 changes: 14 additions & 3 deletions bytehook/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
cmake_minimum_required(VERSION 3.22.1)
cmake_minimum_required(VERSION 3.30.5)
project(bytehook)

enable_language(ASM)

if((${ANDROID_ABI} STREQUAL "arm64-v8a") OR (${ANDROID_ABI} STREQUAL "x86_64"))
set(ARCH_LINK_FLAGS "-Wl,-z,max-page-size=16384")
else()
set(ARCH_LINK_FLAGS "")
endif()

file(GLOB SRC *.c)
add_library(bytehook SHARED ${SRC})
target_compile_features(bytehook PUBLIC c_std_17)
Expand All @@ -12,8 +18,13 @@ target_link_libraries(bytehook log)

if(USEASAN)
target_compile_options(bytehook PUBLIC -fsanitize=address -fno-omit-frame-pointer)
target_link_options(bytehook PUBLIC -fsanitize=address)
target_link_options(bytehook PUBLIC ${ARCH_LINK_FLAGS} -fsanitize=address)
else()
target_compile_options(bytehook PUBLIC -Oz -ffunction-sections -fdata-sections)
target_link_options(bytehook PUBLIC -Oz -Wl,--exclude-libs,ALL -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/bytehook.map.txt)
target_link_options(bytehook PUBLIC ${ARCH_LINK_FLAGS} -Oz -Wl,--exclude-libs,ALL -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/bytehook.map.txt)
endif()

if((${ANDROID_ABI} STREQUAL "arm64-v8a") OR (${ANDROID_ABI} STREQUAL "armeabi-v7a"))
find_package(shadowhook REQUIRED CONFIG)
target_link_libraries(bytehook shadowhook::shadowhook)
endif ()
54 changes: 54 additions & 0 deletions bytehook/src/main/cpp/bh_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2020-2024 ByteDance, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

// Created by Kelun Cai ([email protected]) on 2024-09-24.

#include "bh_array.h"

#include <stdlib.h>
#include <string.h>

int bh_array_push(bh_array_t *self, uintptr_t value) {
if (__predict_false(self->count >= self->cap)) {
self->cap *= 2;
uintptr_t *heap = NULL;
if (self->data == self->stack) {
heap = malloc(sizeof(uintptr_t) * self->cap);
if (NULL == heap) return -1;
memcpy(heap, self->stack, sizeof(self->stack));
} else {
heap = realloc(self->data, sizeof(uintptr_t) * self->cap);
if (NULL == heap) {
free(self->data);
return -1;
}
}
self->data = heap;
}

self->data[self->count] = value;
self->count++;
return 0;
}

void bh_array_free(bh_array_t *self) {
if (__predict_false(self->data != self->stack)) free(self->data);
}
40 changes: 40 additions & 0 deletions bytehook/src/main/cpp/bh_array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2020-2024 ByteDance, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

// Created by Kelun Cai ([email protected]) on 2024-09-24.

#pragma once

#include <stdint.h>

#define BH_ARRAY_STACK_CAP 8

typedef struct {
uintptr_t *data;
size_t count;
size_t cap;
uintptr_t stack[BH_ARRAY_STACK_CAP];
} bh_array_t;

#define BH_ARRAY_INITIALIZER(self) {.data = (self)->stack, .count = 0, .cap = BH_ARRAY_STACK_CAP}

int bh_array_push(bh_array_t *self, uintptr_t value);
void bh_array_free(bh_array_t *self);
10 changes: 5 additions & 5 deletions bytehook/src/main/cpp/bh_cfi.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 ByteDance, Inc.
// Copyright (c) 2020-2024 ByteDance, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -30,8 +30,8 @@
#include <sys/mman.h>
#include <unistd.h>

#include "bh_sig.h"
#include "bh_util.h"
#include "bytesig.h"

#define BH_CFI_LIB_DL "libdl.so"
#define BH_CFI_SLOWPATH "__cfi_slowpath"
Expand Down Expand Up @@ -61,14 +61,14 @@ int bh_cfi_disable_slowpath(void) {
PROT_READ | PROT_WRITE | PROT_EXEC))
return -1;

BYTESIG_TRY(SIGSEGV, SIGBUS) {
BH_SIG_TRY(SIGSEGV, SIGBUS) {
*((uint32_t *)bh_cfi_slowpath) = BH_CFI_ARM64_RET_INST;
*((uint32_t *)bh_cfi_slowpath_diag) = BH_CFI_ARM64_RET_INST;
}
BYTESIG_CATCH() {
BH_SIG_CATCH() {
return -1;
}
BYTESIG_EXIT
BH_SIG_EXIT

__builtin___clear_cache(start, (void *)((size_t)end + sizeof(uint32_t)));

Expand Down
2 changes: 1 addition & 1 deletion bytehook/src/main/cpp/bh_cfi.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 ByteDance, Inc.
// Copyright (c) 2020-2024 ByteDance, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
42 changes: 42 additions & 0 deletions bytehook/src/main/cpp/bh_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2020-2024 ByteDance Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

// Created by Kelun Cai ([email protected]) on 2024-09-12.

#pragma once

// Global debugging.
//
// Note that in some cases these logs themselves can cause a crash.
//
// #define BH_CONFIG_DEBUG

// Operation record of hook and unhook.
//
// Disabling it can reduce hook/unhook latency, memory footprint and file size.
//
#define BH_CONFIG_OPERATION_RECORDS

// Crash signal protection.
//
// Do not disable it in a production environment.
//
#define BH_CONFIG_SIG_PROT
Loading

0 comments on commit 2aa180c

Please sign in to comment.