├── .gitignore ├── LICENSE.md ├── README.md ├── src ├── android │ ├── .gitignore │ ├── app │ │ ├── .gitignore │ │ ├── build.gradle │ │ ├── jni │ │ │ ├── NativeTest.c │ │ │ └── xmake.lua │ │ ├── proguard-rules.pro │ │ └── src │ │ │ ├── androidTest │ │ │ └── java │ │ │ │ └── byopen │ │ │ │ └── sample │ │ │ │ └── ExampleInstrumentedTest.java │ │ │ ├── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ │ └── byopen │ │ │ │ │ └── sample │ │ │ │ │ ├── MainActivity.java │ │ │ │ │ └── NativeTest.java │ │ │ └── res │ │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ │ ├── drawable │ │ │ │ └── ic_launcher_background.xml │ │ │ │ ├── layout │ │ │ │ └── activity_main.xml │ │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ └── values │ │ │ │ ├── colors.xml │ │ │ │ ├── strings.xml │ │ │ │ └── styles.xml │ │ │ └── test │ │ │ └── java │ │ │ └── byopen │ │ │ └── sample │ │ │ └── ExampleUnitTest.java │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── lib │ │ ├── .gitignore │ │ ├── build.gradle │ │ ├── consumer-rules.pro │ │ ├── proguard-rules.pro │ │ └── src │ │ │ ├── androidTest │ │ │ └── java │ │ │ │ └── dyopen │ │ │ │ └── lib │ │ │ │ └── ExampleInstrumentedTest.java │ │ │ ├── main │ │ │ ├── AndroidManifest.xml │ │ │ └── java │ │ │ │ └── dyopen │ │ │ │ └── lib │ │ │ │ └── SystemLoader.java │ │ │ └── test │ │ │ └── java │ │ │ └── dyopen │ │ │ └── lib │ │ │ └── ExampleUnitTest.java │ └── settings.gradle ├── demo │ ├── demo.c │ └── xmake.lua └── native │ ├── byopen.h │ ├── byopen_android.c │ ├── byopen_macho.c │ ├── prefix.h │ └── xmake.lua └── xmake.lua /.gitignore: -------------------------------------------------------------------------------- 1 | # build cache 2 | .xmake 3 | .gradle 4 | build/ 5 | 6 | # MacOS cache 7 | .DS_Store 8 | 9 | # Other cache 10 | .idea 11 | local.properties 12 | 13 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright (C) 2020-present, TBOOX Open Source Group 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | 204 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |

dyOpen

3 |

A dlopen library that bypasses mobile system limitation

4 |
5 | 6 | ## 简介 7 | 8 | byOpen是一个绕过移动端系统限制的增强版dlfunctions库。 9 | 10 | ## 支持特性 11 | 12 | ### Android 13 | 14 | 支持App中加载和使用Android系统库接口(即使maps中还没有被加载也支持)。 15 | 16 | Android 7以上dlopen, System.load都是被限制调用的,虽然目前网上有[Nougat_dlfunctions](https://github.com/avs333/Nougat_dlfunctions)等库通过从maps中找so库来绕过加载限制。 17 | 18 | 不过对于app中还没被加载到maps的so库,这种方式就不行了。 19 | 20 | 而byOpen不仅支持fake dlopen方式从maps加载,还可以将还没加载到maps的so库绕过系统限制强行加载进来使用,实现更加通用化得dlopen。 21 | 22 | 注:目前的实现方式理论上还是比较通用的,至少我这Android 10上测试ok,但还没完整详细测试过,是否使用请自行评估。 23 | 24 | #### 相关原理 25 | 26 | 具体实现原理还是比较简单的,主要还是借鉴了[一种绕过Android P对非SDK接口限制的简单方法](http://weishu.me/2018/06/07/free-reflection-above-android-p/)的思想和实现方式。 27 | 28 | 虽然这篇文章中主要目的是为了绕过hide api,不过它里面使用的将自己假装成系统调用的方式,一样可以用到`System.loadLibrary`上去,让系统以为是系统自身在调用`System.loadLibrary` 29 | 30 | 从而绕过Android N的classloader-namespace限制,将系统/system/lib中任意so库加载到maps中,然后再通过fake dlopen的方式去dlsym。 31 | 32 | #### 增强版fake dlopen 33 | 34 | 关于fake dlopen的方式实现,网上已有很多实现,比如: 35 | 36 | * [Nougat_dlfunctions](https://github.com/avs333/Nougat_dlfunctions) 37 | * [Enhanced_dlfunctions](https://github.com/turing-technician/Enhanced_dlfunctions) 38 | 39 | byOpen参考了里面的实现,重新实现了一遍,并且做了一些小改进: 40 | 41 | * 不在/proc/self/maps中的系统库,也能绕过限制强行加载进来使用 42 | * 除了从.dynsym中检索符号,还支持从.symtab中检索符号(参考:Enhanced_dlfunctions,顺带修复了里面的一些bug) 43 | * 整个dlopen过程只有一次malloc分配(省去整个符号表的内存分配和copy) 44 | 45 | #### Android例子 46 | 47 | Android相关测试App例子在:[Android Sample](https://github.com/hack0z/byOpen/tree/master/src/android) 48 | 49 | 注:目前自带的App测试例子里面的系统库我写死了,有些系统版本上有可能不存在,请先改成用户自己的库和符号名,再编译测试 50 | 51 | ```java 52 | public class MainActivity extends AppCompatActivity { 53 | private static final String SYSTEM_LIBRARY = "curl"; 54 | private static final String SYMBOL_NAME = "curl_version"; 55 | ``` 56 | 57 | 除了Native版本dlopen接口,byOpen额外提供了java版本的[System.loadLibrary](https://github.com/hack0z/byOpen/blob/master/src/android/lib/src/main/java/dyopen/lib/SystemLoader.java)接口在java层直接绕过系统库加载。 58 | 59 | 关键代码如下: 60 | 61 | ```java 62 | static public boolean loadLibrary(String libraryName) { 63 | Method forName = Class.class.getDeclaredMethod("forName", String.class); 64 | Method getDeclaredMethod = Class.class.getDeclaredMethod("getDeclaredMethod", String.class, Class[].class); 65 | Class systemClass = (Class) forName.invoke(null, "java.lang.System"); 66 | Method loadLibrary = (Method) getDeclaredMethod.invoke(systemClass, "loadLibrary", new Class[]{String.class}); 67 | loadLibrary.invoke(systemClass, libraryName); 68 | } 69 | ``` 70 | 71 | 而native版本的[dlopen_android.c](https://github.com/hack0z/byOpen/blob/master/src/native/byopen_android.c)实现中,我将这段绕过的系统加载的方式,通过jni重新实现了一遍,然后和fake dlopen无缝结合到了一起。 72 | 73 | ### iOS 74 | 75 | 虽然ios可以直接使用dlopen,但是审核上会有风险,苹果有可能会对提交AppStore的app扫描相关dlopen/dlsym等调用,来判断是否存在一些敏感的私有调用。 76 | 77 | 为了在通过调用一些私有接口的时候避免被苹果检测到,byOpen也通过自己实现dlopen/dlsym直接从已经加载进来的images列表里面直接查找对应symbol地址来调用。 78 | 79 | 当然,为了更加安全,相关调用的库符号硬编码字符串等,用户可以自行做层变换加密,不要直接编译进app。 80 | 81 | ## 接口用法 82 | 83 | 相关静态库和接口在:[dlopen.h](https://github.com/hack0z/byOpen/blob/master/src/native/byopen.h) 84 | 85 | 相关使用方式跟原生dlopen完全相同: 86 | 87 | ```c 88 | typedef by_char_t const* (*curl_version_t)(); 89 | by_pointer_t handle = by_dlopen("libcurl.so", BY_RTLD_LAZY); 90 | if (handle) 91 | { 92 | by_pointer_t addr = by_dlsym(handle, "curl_version"); 93 | if (addr) 94 | { 95 | curl_version_t curl_version = (curl_version_t)addr; 96 | by_print("curl_version: %s", curl_version()); 97 | } 98 | by_dlclose(handle); 99 | } 100 | ``` 101 | 102 | ## 编译 103 | 104 | 编译需要先安装:[xmake](https://github.com/xmake-io/xmake) 105 | 106 | ### Android 107 | 108 | #### 直接编译库 109 | 110 | ```console 111 | $ xmake f -p android --ndk=~/file/android-ndk-r20b 112 | $ xmake 113 | ``` 114 | 115 | #### 通过gradle编译测试Apk 116 | 117 | ```console 118 | $ cd src/android 119 | $ ./gradlew app:assembleDebug 120 | ``` 121 | 122 | #### 通过xmake直接编译apk 123 | 124 | ```console 125 | $ xmake apk_build 126 | ``` 127 | 128 | #### 通过xmake直接安装测试apk 129 | 130 | ```console 131 | $ xmake apk_test 132 | ``` 133 | 134 | ### iOS 135 | 136 | #### 直接编译库 137 | 138 | ```console 139 | $ xmake f -p iphoneos -a [armv7|arm64] 140 | $ xmake 141 | ``` 142 | 143 | ### MacOS 144 | 145 | 我们也可以在macOS下编译测试,也是支持的: 146 | 147 | ```console 148 | $ xmake 149 | $ xmake run 150 | ``` 151 | -------------------------------------------------------------------------------- /src/android/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /src/android/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | libs 3 | -------------------------------------------------------------------------------- /src/android/app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'org.tboox.gradle-xmake-plugin' version '1.1.3' 3 | } 4 | apply plugin: 'com.android.application' 5 | 6 | android { 7 | compileSdkVersion 29 8 | 9 | defaultConfig { 10 | applicationId "byopen.sample" 11 | minSdkVersion 19 12 | targetSdkVersion 29 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | 18 | ndk { 19 | abiFilters "armeabi-v7a", "arm64-v8a" 20 | } 21 | } 22 | 23 | buildTypes { 24 | release { 25 | minifyEnabled false 26 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 27 | } 28 | } 29 | 30 | externalNativeBuild { 31 | xmake { 32 | logLevel "warning" 33 | path "../../../xmake.lua" 34 | //buildMode "minsizerel" 35 | buildMode "debug" 36 | } 37 | } 38 | } 39 | 40 | dependencies { 41 | implementation fileTree(dir: "libs", include: ["*.jar"]) 42 | implementation 'androidx.appcompat:appcompat:1.1.0' 43 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 44 | implementation project(path: ':lib') 45 | testImplementation 'junit:junit:4.12' 46 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 47 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/android/app/jni/NativeTest.c: -------------------------------------------------------------------------------- 1 | /*!A dlopen library that bypasses mobile system limitation 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * 15 | * Copyright (C) 2020-present, TBOOX Open Source Group. 16 | * 17 | * @author ruki 18 | * @file NativeTest.c 19 | * 20 | */ 21 | 22 | /* ////////////////////////////////////////////////////////////////////////////////////// 23 | * includes 24 | */ 25 | #include "byopen.h" 26 | 27 | /* ////////////////////////////////////////////////////////////////////////////////////// 28 | * declaration 29 | */ 30 | by_void_t by_jni_javavm_set(JavaVM* jvm, by_int_t jversion); 31 | 32 | /* ////////////////////////////////////////////////////////////////////////////////////// 33 | * interfaces 34 | */ 35 | JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* jvm, by_pointer_t reserved) 36 | { 37 | by_jni_javavm_set(jvm, JNI_VERSION_1_4); 38 | return JNI_VERSION_1_4; 39 | } 40 | 41 | JNIEXPORT jboolean Java_byopen_sample_NativeTest_loadLibrary(JNIEnv* env, jclass jthis, jstring libraryName, jstring symbolName) 42 | { 43 | by_bool_t ok = by_false; 44 | by_char_t const* libraryName_cstr = (*env)->GetStringUTFChars(env, libraryName, by_null); 45 | by_char_t const* symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, by_null); 46 | if (libraryName_cstr && symbolName_cstr) 47 | { 48 | by_pointer_t handle = by_dlopen(libraryName_cstr, BY_RTLD_LAZY); 49 | if (handle) 50 | { 51 | by_pointer_t addr = by_dlsym(handle, symbolName_cstr); 52 | if (addr) 53 | { 54 | #if 0 55 | // test libcurl/curl_version() 56 | typedef char* (*func_t)(); 57 | func_t func = (func_t)addr; 58 | by_trace("curl_version: %s", func()); 59 | #endif 60 | 61 | // load ok 62 | ok = by_true; 63 | } 64 | by_dlclose(handle); 65 | } 66 | (*env)->ReleaseStringUTFChars(env, libraryName, libraryName_cstr); 67 | (*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr); 68 | } 69 | return ok; 70 | } 71 | JNIEXPORT jboolean Java_byopen_sample_NativeTest_validFromMaps(JNIEnv* env, jclass jthis, jstring libraryName) 72 | { 73 | by_bool_t found = by_false; 74 | by_char_t const* libraryName_cstr = (*env)->GetStringUTFChars(env, libraryName, by_null); 75 | if (libraryName_cstr) 76 | { 77 | FILE* f = fopen("/proc/self/maps", "r"); 78 | if (f) 79 | { 80 | char line[512]; 81 | while (fgets(line, sizeof(line), f)) 82 | { 83 | if (strstr(line, libraryName_cstr)) 84 | { 85 | found = by_true; 86 | break; 87 | } 88 | } 89 | fclose(f); 90 | } 91 | (*env)->ReleaseStringUTFChars(env, libraryName, libraryName_cstr); 92 | } 93 | return found; 94 | } 95 | -------------------------------------------------------------------------------- /src/android/app/jni/xmake.lua: -------------------------------------------------------------------------------- 1 | target("testjni") 2 | set_kind("shared") 3 | add_deps("byopen") 4 | add_files("*.c") 5 | 6 | -------------------------------------------------------------------------------- /src/android/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /src/android/app/src/androidTest/java/byopen/sample/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package byopen.sample; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | assertEquals("byopen.sample", appContext.getPackageName()); 25 | } 26 | } -------------------------------------------------------------------------------- /src/android/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/android/app/src/main/java/byopen/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package byopen.sample; 2 | 3 | import androidx.appcompat.app.AppCompatActivity; 4 | 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | import android.view.View; 8 | import android.widget.Button; 9 | 10 | import dyopen.lib.SystemLoader; 11 | 12 | public class MainActivity extends AppCompatActivity { 13 | 14 | private static final String TAG = "byOpen"; 15 | private static final String SYSTEM_LIBRARY = "curl"; 16 | private static final String SYMBOL_NAME = "curl_version"; 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_main); 22 | 23 | final Button origin_loadLibraryBtn = (Button) findViewById(R.id.origin_loadLibrary); 24 | origin_loadLibraryBtn.setOnClickListener(new View.OnClickListener() { 25 | @Override 26 | public void onClick(View v) { 27 | try { 28 | System.loadLibrary(SYSTEM_LIBRARY); 29 | if (NativeTest.validFromMaps(SYSTEM_LIBRARY)) { 30 | origin_loadLibraryBtn.setText("load ok!"); 31 | } else { 32 | origin_loadLibraryBtn.setText("load failed!"); 33 | } 34 | } catch (Throwable e) { 35 | origin_loadLibraryBtn.setText("load failed!"); 36 | } 37 | } 38 | }); 39 | 40 | final Button byopen_loadLibraryBtn = (Button) findViewById(R.id.byopen_loadLibrary); 41 | byopen_loadLibraryBtn.setOnClickListener(new View.OnClickListener() { 42 | @Override 43 | public void onClick(View v) { 44 | if (SystemLoader.loadLibrary(SYSTEM_LIBRARY) && NativeTest.validFromMaps(SYSTEM_LIBRARY)) { 45 | byopen_loadLibraryBtn.setText("load ok!"); 46 | } else { 47 | byopen_loadLibraryBtn.setText("load failed!"); 48 | } 49 | } 50 | }); 51 | 52 | final Button dlopenBtn = (Button) findViewById(R.id.byopen_dlopen); 53 | dlopenBtn.setOnClickListener(new View.OnClickListener() { 54 | @Override 55 | public void onClick(View v) { 56 | if (NativeTest.loadLibrary(SYSTEM_LIBRARY, SYMBOL_NAME) && NativeTest.validFromMaps(SYSTEM_LIBRARY)) { 57 | dlopenBtn.setText("load ok!"); 58 | } else { 59 | dlopenBtn.setText("load failed!"); 60 | } 61 | } 62 | }); 63 | 64 | final Button validBtn = (Button) findViewById(R.id.valid_from_maps); 65 | validBtn.setOnClickListener(new View.OnClickListener() { 66 | @Override 67 | public void onClick(View v) { 68 | if (NativeTest.validFromMaps(SYSTEM_LIBRARY)) { 69 | validBtn.setText("valid ok!"); 70 | } else { 71 | validBtn.setText("valid failed!"); 72 | } 73 | } 74 | }); 75 | } 76 | } -------------------------------------------------------------------------------- /src/android/app/src/main/java/byopen/sample/NativeTest.java: -------------------------------------------------------------------------------- 1 | package byopen.sample; 2 | 3 | public class NativeTest { 4 | static { 5 | System.loadLibrary("testjni"); 6 | } 7 | public static native boolean loadLibrary(String libraryName, String symbolName); 8 | public static native boolean validFromMaps(String libraryName); 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /src/android/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /src/android/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 |