Skip to content

Commit 2aa180c

Browse files
committed
feat: major changes
1 parent 642257e commit 2aa180c

File tree

112 files changed

+5304
-3539
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+5304
-3539
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

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

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ByteHook
22

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

@@ -47,7 +47,7 @@ android {
4747
}
4848
4949
dependencies {
50-
implementation 'com.bytedance:bytehook:1.0.10'
50+
implementation 'com.bytedance:bytehook:1.1.0'
5151
}
5252
```
5353

@@ -114,6 +114,12 @@ android {
114114
}
115115
```
116116

117+
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.
118+
119+
```Gradle
120+
apply from: rootProject.file('gradle/prefab_bypass.gradle')
121+
```
122+
117123
### 5. Initialize
118124

119125
```Java
@@ -184,9 +190,6 @@ ByteHook uses the following third-party source code or libraries:
184190
* [queue.h](bytehook/src/main/cpp/third_party/bsd/queue.h)
185191
BSD 3-Clause License
186192
Copyright (c) 1991, 1993 The Regents of the University of California.
187-
* [tree.h](bytehook/src/main/cpp/third_party/bsd/tree.h)
188-
BSD 2-Clause License
189-
Copyright (c) 2002 Niels Provos <[email protected]>
190193
* [linux-syscall-support](https://chromium.googlesource.com/linux-syscall-support/)
191194
BSD 3-Clause License
192195
Copyright (c) 2005-2011 Google Inc.

README.zh-CN.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ByteHook
22

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

@@ -47,7 +47,7 @@ android {
4747
}
4848
4949
dependencies {
50-
implementation 'com.bytedance:bytehook:1.0.10'
50+
implementation 'com.bytedance:bytehook:1.1.0'
5151
}
5252
```
5353

@@ -114,6 +114,12 @@ android {
114114
}
115115
```
116116

117+
注意:如果在 x86 和 x86_64 架构下使用 prefab 依赖 bytehook 时需要在模块的 build.gradle 中添加 [prefab_bypass.gradle](gradle/prefab_bypass.gradle)
118+
119+
```Gradle
120+
apply from: rootProject.file('gradle/prefab_bypass.gradle')
121+
```
122+
117123
### 5. 初始化
118124

119125
```Java
@@ -184,9 +190,6 @@ ByteHook 使用了以下第三方源码或库:
184190
* [queue.h](bytehook/src/main/cpp/third_party/bsd/queue.h)
185191
BSD 3-Clause License
186192
Copyright (c) 1991, 1993 The Regents of the University of California.
187-
* [tree.h](bytehook/src/main/cpp/third_party/bsd/tree.h)
188-
BSD 2-Clause License
189-
Copyright (c) 2002 Niels Provos <[email protected]>
190193
* [linux-syscall-support](https://chromium.googlesource.com/linux-syscall-support/)
191194
BSD 3-Clause License
192195
Copyright (c) 2005-2011 Google Inc.

build.gradle

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plugins {
2-
id 'com.android.application' version '7.4.2' apply false
3-
id 'com.android.library' version '7.4.2' apply false
2+
id 'com.android.application' version '8.7.1' apply false
3+
id 'com.android.library' version '8.7.1' apply false
44
}
55

66
task clean(type: Delete) {
@@ -9,19 +9,21 @@ task clean(type: Delete) {
99

1010
ext {
1111
minSdkVersion = 16
12-
compileSdkVersion = 34
13-
targetSdkVersion = 34
14-
buildToolsVersion = "34.0.0"
12+
compileSdkVersion = 35
13+
targetSdkVersion = 35
14+
buildToolsVersion = "35.0.0"
1515
javaVersion = JavaVersion.VERSION_1_7
1616
ndkVersion = "23.2.8568313"
17-
cmakeVersion = "3.22.1"
17+
cmakeVersion = "3.30.5"
18+
shadowhookVersion = "1.1.1"
19+
// abiFilters = "armeabi-v7a,arm64-v8a,x86,x86_64"
1820
abiFilters = "armeabi-v7a,arm64-v8a,x86,x86_64"
1921
useASAN = false
2022
dependencyOnLocalLibrary = true
2123

2224
POM_GROUP_ID = "com.bytedance"
2325
POM_ARTIFACT_ID = "bytehook"
24-
POM_VERSION_NAME = "1.0.10"
26+
POM_VERSION_NAME = "1.1.0"
2527

2628
POM_NAME = "bytehook"
2729
POM_DESCRIPTION = "ByteHook is an Android PLT hook library which supports armeabi-v7a, arm64-v8a, x86 and x86_64."

bytehook/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ android {
4040
minifyEnabled false
4141
}
4242
}
43+
packagingOptions {
44+
exclude '**/libshadowhook.so'
45+
exclude '**/libshadowhook_nothing.so'
46+
}
4347
buildFeatures {
4448
prefabPublishing true
4549
buildConfig false
50+
prefab true
4651
}
4752
prefab {
4853
bytehook {
@@ -57,5 +62,10 @@ android {
5762
}
5863
}
5964

65+
dependencies {
66+
api 'com.bytedance.android:shadowhook:' + rootProject.ext.shadowhookVersion
67+
}
68+
69+
apply from: rootProject.file('gradle/prefab_bypass.gradle')
6070
apply from: rootProject.file('gradle/check.gradle')
6171
apply from: rootProject.file('gradle/publish.gradle')

bytehook/local_dependency.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# We found that AGP could not generate the correct cmake configuration
2+
# when relying on a prefab package generated by another local project.
3+
# We had to write one ourselves.
4+
#
5+
# Use it like this:
6+
#
7+
# if(DEPENDENCY_ON_LOCAL_LIBRARY)
8+
# include(${CMAKE_CURRENT_SOURCE_DIR}/../../../../bytehook/local_dependency.cmake)
9+
# else()
10+
# find_package(bytehook REQUIRED CONFIG)
11+
# endif()
12+
#
13+
# target_link_libraries(myapp bytehook::bytehook)
14+
#
15+
16+
string(TOLOWER ${CMAKE_BUILD_TYPE} BYTEHOOK_BUILD_TYPE_DIRNAME)
17+
if(${BYTEHOOK_BUILD_TYPE_DIRNAME} STREQUAL "relwithdebinfo")
18+
set(BYTEHOOK_BUILD_TYPE_DIRNAME "release")
19+
endif()
20+
21+
set(BYTEHOOK_BASE ${CMAKE_CURRENT_SOURCE_DIR}/../../../../bytehook)
22+
23+
add_library(bytehook::bytehook SHARED IMPORTED)
24+
set_target_properties(bytehook::bytehook PROPERTIES
25+
IMPORTED_LOCATION "${BYTEHOOK_BASE}/build/intermediates/cmake/${BYTEHOOK_BUILD_TYPE_DIRNAME}/obj/${ANDROID_ABI}/libbytehook.so"
26+
INTERFACE_INCLUDE_DIRECTORIES "${BYTEHOOK_BASE}/src/main/cpp/include"
27+
INTERFACE_LINK_LIBRARIES ""
28+
)

bytehook/src/main/cpp/CMakeLists.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
cmake_minimum_required(VERSION 3.22.1)
1+
cmake_minimum_required(VERSION 3.30.5)
22
project(bytehook)
33

44
enable_language(ASM)
55

6+
if((${ANDROID_ABI} STREQUAL "arm64-v8a") OR (${ANDROID_ABI} STREQUAL "x86_64"))
7+
set(ARCH_LINK_FLAGS "-Wl,-z,max-page-size=16384")
8+
else()
9+
set(ARCH_LINK_FLAGS "")
10+
endif()
11+
612
file(GLOB SRC *.c)
713
add_library(bytehook SHARED ${SRC})
814
target_compile_features(bytehook PUBLIC c_std_17)
@@ -12,8 +18,13 @@ target_link_libraries(bytehook log)
1218

1319
if(USEASAN)
1420
target_compile_options(bytehook PUBLIC -fsanitize=address -fno-omit-frame-pointer)
15-
target_link_options(bytehook PUBLIC -fsanitize=address)
21+
target_link_options(bytehook PUBLIC ${ARCH_LINK_FLAGS} -fsanitize=address)
1622
else()
1723
target_compile_options(bytehook PUBLIC -Oz -ffunction-sections -fdata-sections)
18-
target_link_options(bytehook PUBLIC -Oz -Wl,--exclude-libs,ALL -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/bytehook.map.txt)
24+
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)
1925
endif()
26+
27+
if((${ANDROID_ABI} STREQUAL "arm64-v8a") OR (${ANDROID_ABI} STREQUAL "armeabi-v7a"))
28+
find_package(shadowhook REQUIRED CONFIG)
29+
target_link_libraries(bytehook shadowhook::shadowhook)
30+
endif ()

bytehook/src/main/cpp/bh_array.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2020-2024 ByteDance, Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
//
21+
22+
// Created by Kelun Cai ([email protected]) on 2024-09-24.
23+
24+
#include "bh_array.h"
25+
26+
#include <stdlib.h>
27+
#include <string.h>
28+
29+
int bh_array_push(bh_array_t *self, uintptr_t value) {
30+
if (__predict_false(self->count >= self->cap)) {
31+
self->cap *= 2;
32+
uintptr_t *heap = NULL;
33+
if (self->data == self->stack) {
34+
heap = malloc(sizeof(uintptr_t) * self->cap);
35+
if (NULL == heap) return -1;
36+
memcpy(heap, self->stack, sizeof(self->stack));
37+
} else {
38+
heap = realloc(self->data, sizeof(uintptr_t) * self->cap);
39+
if (NULL == heap) {
40+
free(self->data);
41+
return -1;
42+
}
43+
}
44+
self->data = heap;
45+
}
46+
47+
self->data[self->count] = value;
48+
self->count++;
49+
return 0;
50+
}
51+
52+
void bh_array_free(bh_array_t *self) {
53+
if (__predict_false(self->data != self->stack)) free(self->data);
54+
}

bytehook/src/main/cpp/bh_array.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2020-2024 ByteDance, Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
//
21+
22+
// Created by Kelun Cai ([email protected]) on 2024-09-24.
23+
24+
#pragma once
25+
26+
#include <stdint.h>
27+
28+
#define BH_ARRAY_STACK_CAP 8
29+
30+
typedef struct {
31+
uintptr_t *data;
32+
size_t count;
33+
size_t cap;
34+
uintptr_t stack[BH_ARRAY_STACK_CAP];
35+
} bh_array_t;
36+
37+
#define BH_ARRAY_INITIALIZER(self) {.data = (self)->stack, .count = 0, .cap = BH_ARRAY_STACK_CAP}
38+
39+
int bh_array_push(bh_array_t *self, uintptr_t value);
40+
void bh_array_free(bh_array_t *self);

bytehook/src/main/cpp/bh_cfi.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2022 ByteDance, Inc.
1+
// Copyright (c) 2020-2024 ByteDance, Inc.
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a copy
44
// of this software and associated documentation files (the "Software"), to deal
@@ -30,8 +30,8 @@
3030
#include <sys/mman.h>
3131
#include <unistd.h>
3232

33+
#include "bh_sig.h"
3334
#include "bh_util.h"
34-
#include "bytesig.h"
3535

3636
#define BH_CFI_LIB_DL "libdl.so"
3737
#define BH_CFI_SLOWPATH "__cfi_slowpath"
@@ -61,14 +61,14 @@ int bh_cfi_disable_slowpath(void) {
6161
PROT_READ | PROT_WRITE | PROT_EXEC))
6262
return -1;
6363

64-
BYTESIG_TRY(SIGSEGV, SIGBUS) {
64+
BH_SIG_TRY(SIGSEGV, SIGBUS) {
6565
*((uint32_t *)bh_cfi_slowpath) = BH_CFI_ARM64_RET_INST;
6666
*((uint32_t *)bh_cfi_slowpath_diag) = BH_CFI_ARM64_RET_INST;
6767
}
68-
BYTESIG_CATCH() {
68+
BH_SIG_CATCH() {
6969
return -1;
7070
}
71-
BYTESIG_EXIT
71+
BH_SIG_EXIT
7272

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

0 commit comments

Comments
 (0)