├── app
├── .gitignore
├── src
│ └── main
│ │ ├── .gitignore
│ │ ├── res
│ │ ├── mipmap-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── styles.xml
│ │ │ └── dimens.xml
│ │ ├── values-w820dp
│ │ │ └── dimens.xml
│ │ ├── menu
│ │ │ └── menu_main.xml
│ │ └── layout
│ │ │ └── activity_main.xml
│ │ ├── jni
│ │ ├── plugin_loader.c
│ │ ├── plugin_loader.h
│ │ ├── mainact_native.h
│ │ ├── lib_setup.h
│ │ ├── thread_lookup.h
│ │ ├── elf.c
│ │ ├── lib_setup.c
│ │ ├── art_resolution.h
│ │ ├── elf.h
│ │ ├── statistics.h
│ │ ├── thread_lookup.c
│ │ ├── art_resolution.c
│ │ └── memory_map_lookup.h
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── lukas
│ │ └── ndktest
│ │ └── MemoryAnalysis
│ │ ├── MemoryInfo.java
│ │ ├── MemoryAnalyzer.java
│ │ ├── MappedMemoryRegion.java
│ │ └── MemoryRegionParser.java
└── proguard-rules.pro
├── libAndroidRuntime
├── .gitignore
├── src
│ └── main
│ │ └── jni
│ │ ├── bit_vector_util.h
│ │ ├── jvalue.h
│ │ ├── oat_version_dependent
│ │ ├── VERSION064
│ │ │ ├── entrypoints
│ │ │ │ ├── jni_entrypoints.h
│ │ │ │ ├── quick_entrypoints.h
│ │ │ │ ├── portable_entrypoints.h
│ │ │ │ └── interpreter_entrypoints.h
│ │ │ └── stack.h
│ │ └── VERSION045
│ │ │ ├── stack.h
│ │ │ └── entrypoints
│ │ │ ├── jni_entrypoints.h
│ │ │ ├── portable_entrypoints.h
│ │ │ ├── quick_entrypoints.h
│ │ │ ├── interpreter_entrypoints.h
│ │ │ └── quick_entrypoints_enum.h
│ │ ├── mirror_hacks.h
│ │ ├── throw_location.h
│ │ ├── mem_map.h
│ │ ├── leb128.h
│ │ ├── indirect_reference_table.h
│ │ ├── bit_vector_util.c
│ │ ├── android_utf.h
│ │ ├── oat_dump.h
│ │ ├── dex.h
│ │ ├── runtime_stats.h
│ │ ├── oat.h
│ │ ├── base
│ │ └── mutex.h
│ │ ├── modifiers.h
│ │ └── leb128.c
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── settings.gradle
├── libUtility
├── src
│ └── main
│ │ └── jni
│ │ ├── init.h
│ │ ├── init.c
│ │ ├── system_info.h
│ │ ├── typedefs.h
│ │ ├── error.h
│ │ ├── macros.h
│ │ ├── bit_utils.h
│ │ ├── config.h
│ │ ├── util.h
│ │ ├── exceptions.h
│ │ ├── exceptions.c
│ │ ├── config_constants.h
│ │ ├── error.c
│ │ ├── util.c
│ │ ├── dirty_dirty_std_library_hacks.h
│ │ ├── system_info.c
│ │ ├── memory.h
│ │ ├── concurrent_persistent_list.h
│ │ ├── concurrent_persistent_list.c
│ │ ├── logging.c
│ │ ├── city.h
│ │ ├── logging.h
│ │ └── memory.c
└── build.gradle
├── libHostSystem
├── src
│ ├── main
│ │ └── jni
│ │ │ ├── init.c
│ │ │ ├── init.h
│ │ │ ├── armeabi.h
│ │ │ ├── signal_handling_helper.h
│ │ │ ├── cpsr_util.h
│ │ │ └── abi.h
│ └── armeabi
│ │ └── jni
│ │ ├── signal_handling_helper.c
│ │ └── abi.c
└── build.gradle
├── gradle.properties
├── libHooking
├── src
│ ├── main
│ │ └── jni
│ │ │ ├── hooking_common.h
│ │ │ ├── generate_trap_instruction.h
│ │ │ ├── function_hook.h
│ │ │ ├── breakpoint.h
│ │ │ ├── instruction_bkpt.h
│ │ │ ├── invocation_hook.c
│ │ │ ├── breakpoint.c
│ │ │ ├── invocation_hook.h
│ │ │ ├── trappoint_interface.h
│ │ │ ├── self_patching_trappoint.h
│ │ │ ├── instruction_illegal.h
│ │ │ └── function_hook.c
│ └── armeabi
│ │ └── jni
│ │ ├── instruction_bkpt.c
│ │ ├── generate_trap_instruction.c
│ │ ├── trappoint.h
│ │ └── instruction_illegal.c
└── build.gradle
├── gradlew.bat
├── CRAPL-LICENSE.txt
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build/
2 | *.apk
--------------------------------------------------------------------------------
/app/src/main/.gitignore:
--------------------------------------------------------------------------------
1 | libs/
2 | obj/
--------------------------------------------------------------------------------
/libAndroidRuntime/.gitignore:
--------------------------------------------------------------------------------
1 | build/
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lukas-Dresel/ARTIST/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Remove Android Studio stuff
2 | /.gradle
3 | /local.properties
4 | /.idea
5 | build/
6 | /distribution/
7 | *.iml
8 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lukas-Dresel/ARTIST/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lukas-Dresel/ARTIST/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lukas-Dresel/ARTIST/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lukas-Dresel/ARTIST/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | include ':libAndroidRuntime'
3 | include ':libHostSystem'
4 | include ':libUtility'
5 | include ':libHooking'
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | NdkTest
3 |
4 | Hello world!
5 | Settings
6 |
7 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/init.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Lukas on 8/12/2016.
3 | //
4 |
5 | #ifndef ARTIST_INIT_H
6 | #define ARTIST_INIT_H
7 |
8 | void init_libUtility();
9 | void destroy_libUtility();
10 |
11 | #endif //ARTIST_INIT_H
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 10 15:27:10 PDT 2013
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
7 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/init.c:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Lukas on 8/12/2016.
3 | //
4 |
5 | #include "init.h"
6 | #include "system_info.h"
7 |
8 | void init_libUtility()
9 | {
10 | init_system_info();
11 | }
12 |
13 | void destroy_libUtility()
14 | {
15 | destroy_system_info();
16 | }
17 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/jni/plugin_loader.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "plugin_loader.h"
18 |
19 | #include
20 |
21 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\Lukas\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/jni/plugin_loader.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef NDKTEST_PLUGIN_LOADER_H
18 | #define NDKTEST_PLUGIN_LOADER_H
19 |
20 | #endif //NDKTEST_PLUGIN_LOADER_H
21 |
--------------------------------------------------------------------------------
/app/src/main/jni/mainact_native.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _MAINACT_NATIVE_H
18 | #define _MAINACT_NATIVE_H
19 |
20 | #include
21 |
22 | #endif
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/app/src/main/jni/lib_setup.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _LIB_SETUP_H
18 | #define _LIB_SETUP_H
19 |
20 | void init();
21 |
22 | void destroy();
23 |
24 | #endif
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/libHostSystem/src/main/jni/init.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/12/2016.
19 | //
20 |
21 | #include "init.h"
22 |
23 | void init_libHostSystem()
24 | {
25 |
26 | }
27 |
28 | void destroy_libHostSystem()
29 | {
30 |
31 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
12 |
13 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/jni/thread_lookup.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef NDKTEST_THREAD_LOOKUP_H
18 | #define NDKTEST_THREAD_LOOKUP_H
19 |
20 | #include
21 |
22 | struct Thread* GetCurrentThreadObjectPointer(JNIEnv* env);
23 |
24 | #endif //NDKTEST_THREAD_LOOKUP_H
25 |
--------------------------------------------------------------------------------
/libHostSystem/src/main/jni/init.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/12/2016.
19 | //
20 |
21 | #ifndef ARTIST_INIT_H
22 | #define ARTIST_INIT_H
23 |
24 | void init_libHostSystem();
25 | void destroy_libHostSystem();
26 |
27 | #endif //ARTIST_INIT_H
28 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/system_info.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _SYSTEM_INFO_H
18 | #define _SYSTEM_INFO_H
19 |
20 | void init_system_info(void);
21 | void destroy_system_info(void);
22 | long getSystemPageSize(void);
23 |
24 | #endif
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/libHostSystem/src/main/jni/armeabi.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/11/2016.
19 | //
20 |
21 | #ifndef ARTIST_ARMEABI_H
22 | #define ARTIST_ARMEABI_H
23 |
24 | #include "cpsr_util.h"
25 |
26 | bool IsAddressThumbMode(const void *address);
27 |
28 | #endif //ARTIST_ARMEABI_H
29 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/typedefs.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 9/28/2015.
19 | //
20 |
21 | #ifndef NDKTEST_TYPEDEFS_H
22 | #define NDKTEST_TYPEDEFS_H
23 |
24 | typedef uint8_t byte;
25 |
26 | typedef struct String
27 | {
28 | uint32_t length;
29 | const char* content;
30 | } String;
31 |
32 | #endif //NDKTEST_TYPEDEFS_H
33 |
--------------------------------------------------------------------------------
/libHooking/src/main/jni/hooking_common.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/31/2016.
19 | //
20 |
21 | #ifndef NDKTEST_HOOKING_COMMON_H
22 | #define NDKTEST_HOOKING_COMMON_H
23 |
24 | #include
25 |
26 | typedef void (*HOOKCALLBACK)(void *addr, ucontext_t *ctx, void *additionalArg);
27 |
28 | #endif //NDKTEST_HOOKING_COMMON_H
29 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/error.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/13/2015.
19 | //
20 |
21 | #ifndef NDKTEST_ERROR_H
22 | #define NDKTEST_ERROR_H
23 |
24 | #include
25 |
26 | void set_last_error(const char *c);
27 |
28 | bool error_occurred();
29 |
30 | const char *get_last_error();
31 |
32 | void clear_last_error();
33 |
34 | #endif //NDKTEST_ERROR_H
35 |
--------------------------------------------------------------------------------
/libHooking/src/main/jni/generate_trap_instruction.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/14/2015.
19 | //
20 |
21 | #ifndef NDKTEST_GENERATE_TRAP_INSTRUCTION_H
22 | #define NDKTEST_GENERATE_TRAP_INSTRUCTION_H
23 |
24 | #include
25 |
26 | uint16_t make_thumb_trap_instruction(uint32_t method);
27 | uint32_t make_arm_trap_instruction(uint32_t method);
28 |
29 | #endif //NDKTEST_GENERATE_TRAP_INSTRUCTION_H
30 |
--------------------------------------------------------------------------------
/libHooking/src/main/jni/function_hook.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/31/2016.
19 | //
20 |
21 | #ifndef NDKTEST_FUNCTION_HOOK_H
22 | #define NDKTEST_FUNCTION_HOOK_H
23 |
24 | /*
25 | #include "hooking_common.h"
26 |
27 | struct Handler
28 | {
29 | HOOKCALLBACK OnEntryHandler;
30 | HOOKCALLBACK OnExitHandler;
31 | void* entry_args;
32 | void* exit_args;
33 | };
34 | */
35 |
36 | #endif //NDKTEST_FUNCTION_HOOK_H
37 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/bit_vector_util.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 9/25/2015.
19 | //
20 |
21 | #ifndef NDKTEST_BIT_VECTOR_UTIL_H
22 | #define NDKTEST_BIT_VECTOR_UTIL_H
23 |
24 | #include
25 |
26 | uint32_t bit_vector_NumSetBits ( const uint32_t* data, uint32_t end );
27 | bool bit_vector_IsBitSet ( const uint32_t* data, uint32_t bit_index );
28 |
29 | #endif //NDKTEST_BIT_VECTOR_UTIL_H
30 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/jvalue.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/18/2016.
19 | //
20 |
21 | #ifndef NDKTEST_ART_JVALUE_H
22 | #define NDKTEST_ART_JVALUE_H
23 |
24 | #include
25 | #include
26 |
27 | union PACKED(4) JValue
28 | {
29 | uint8_t z;
30 | int8_t b;
31 | uint16_t c;
32 | int16_t s;
33 | int32_t i;
34 | int64_t j;
35 | float f;
36 | double d;
37 | void* l;
38 | };
39 |
40 | #endif //NDKTEST_ART_JVALUE_H
41 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/macros.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/17/2015.
19 | //
20 |
21 | #ifndef NDKTEST_MACROS_H
22 | #define NDKTEST_MACROS_H
23 |
24 |
25 | #define QUOTE(x) #x
26 | #define STRINGIFY(x) QUOTE(x)
27 |
28 | #define __LINE_STRING__ STRINGIFY(__LINE__)
29 |
30 | #define LIKELY(x) __builtin_expect((x), true)
31 | #define UNLIKELY(x) __builtin_expect((x), false)
32 |
33 | #define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
34 |
35 | #endif //NDKTEST_MACROS_H
36 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
17 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/bit_utils.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef NDKTEST_BIT_UTILS_H
18 | #define NDKTEST_BIT_UTILS_H
19 |
20 | #define COUNT_32BIT_SET_BITS(x) __builtin_popcount(x)
21 | #define COUNT_64BIT_SET_BITS(x) __builtin_popcountll(x)
22 |
23 | #define COUNT_32BIT_LEADING_ZEROS(x) __builtin_clz(x)
24 | #define COUNT_64BIT_LEADING_ZEROS(x) __builtin_clzll(x)
25 |
26 | #define COUNT_32BIT_TAILING_ZEROS(x) __builtin_ctz(x)
27 | #define COUNT_64BIT_TAILING_ZEROS(x) __builtin_ctzll(x)
28 |
29 | #endif //NDKTEST_BIT_UTILS_H
30 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_version_dependent/VERSION064/entrypoints/jni_entrypoints.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/18/2016.
19 | //
20 |
21 | #ifndef NDKTEST_ART_ENTRYPOINTS_JNI_ENTRYPOINTS_H
22 | #define NDKTEST_ART_ENTRYPOINTS_JNI_ENTRYPOINTS_H
23 |
24 | #include
25 | #include
26 |
27 | struct PACKED(4) JniEntryPoints {
28 | // Called when the JNI method isn't registered.
29 | void* (*pDlsymLookup)(JNIEnv* env, jobject);
30 | };
31 |
32 | #endif //NDKTEST_ART_ENTRYPOINTS_JNI_ENTRYPOINTS_H
33 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/mirror_hacks.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 3/11/2016.
19 | //
20 |
21 | #ifndef NDKTEST_MIRROR_HACKS_H
22 | #define NDKTEST_MIRROR_HACKS_H
23 |
24 | #include
25 |
26 | struct MirrorHackStringContent
27 | {
28 | char unknown[12];
29 | uint16_t chars[0];
30 | };
31 | struct MirrorHackString
32 | {
33 | void* some_ref;
34 | void* null;
35 | struct MirrorHackStringContent* str_content;
36 | uint32_t str_len;
37 | uint32_t hash;
38 | };
39 |
40 |
41 | #endif //NDKTEST_MIRROR_HACKS_H
42 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/config.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _CONFIG_H
18 | #define _CONFIG_H
19 |
20 | #include "config_constants.h"
21 |
22 | #define LOG_LEVEL LOG_LEVEL_DEBUG
23 |
24 | #define OAT_VERSION OAT_VERSION_045
25 |
26 | #define TARGET_ARCHITECTURE ARCHITECTURE_ARM
27 |
28 | //#define DEBUG
29 | //#define DEBUG_WAIT
30 |
31 | #define PROC_PID_MAPS_MAX_LINE_LENGTH 3200
32 |
33 | #define DEFAULT_TRAPPOINT_METHOD (TRAP_METHOD_INSTR_KNOWN_ILLEGAL | TRAP_METHOD_SIG_ILL)
34 |
35 | #endif
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/libHostSystem/src/main/jni/signal_handling_helper.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/14/2015.
19 | //
20 |
21 | #ifndef NDKTEST_SIGNAL_HANDLING_HELPER_H
22 | #define NDKTEST_SIGNAL_HANDLING_HELPER_H
23 |
24 | #include
25 | #include
26 |
27 | uint32_t GetArgument(ucontext_t *c, unsigned int index);
28 |
29 | void SetArgument(ucontext_t *c, unsigned int index, uint32_t val);
30 |
31 | void log_siginfo_content(siginfo_t *info);
32 |
33 | void log_mcontext_content(mcontext_t *state_info);
34 |
35 | #endif //NDKTEST_SIGNAL_HANDLING_HELPER_H
36 |
--------------------------------------------------------------------------------
/libHooking/src/main/jni/breakpoint.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 2/9/2016.
19 | //
20 |
21 | #ifndef NDKTEST_BREAKPOINT_H
22 | #define NDKTEST_BREAKPOINT_H
23 |
24 | #include
25 | #include
26 | #include "hooking_common.h"
27 |
28 | void* Breakpoint_Install (void *address, uint32_t trap_method, HOOKCALLBACK handler,
29 | void *arg);
30 | bool Breakpoint_Enable (void *p);
31 | bool Breakpoint_Disable (void *p);
32 | void Breakpoint_Uninstall (void *p);
33 |
34 | #endif //NDKTEST_BREAKPOINT_H
35 |
--------------------------------------------------------------------------------
/app/src/main/jni/elf.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "elf.h"
18 |
19 | #ifdef __cplusplus
20 | extern "C" {
21 | #endif
22 |
23 | bool IsValidElfFileHeader(void* p)
24 | {
25 | Elf32_Ehdr* hdr = p;
26 |
27 | if(hdr->e_ident[EI_MAG0] != ELFMAG0 ||
28 | hdr->e_ident[EI_MAG1] != ELFMAG1 ||
29 | hdr->e_ident[EI_MAG2] != ELFMAG2 ||
30 | hdr->e_ident[EI_MAG3] != ELFMAG3)
31 | {
32 | return false;
33 | }
34 |
35 | // Further checks could be added, but not necessary for our purposes yet
36 |
37 | return true;
38 | }
39 |
40 | #ifdef __cplusplus
41 | }
42 | #endif
--------------------------------------------------------------------------------
/libUtility/src/main/jni/util.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _UTIL_H
18 | #define _UTIL_H
19 |
20 | #include
21 | #include
22 |
23 | typedef bool (*PREDICATE)(void* current, void* additionalArgs);
24 |
25 | uint64_t round_down(uint64_t value, uint64_t size);
26 |
27 | uint64_t round_up(uint64_t value, uint64_t size);
28 |
29 | void *align_address_to_size(const void *addr, uint64_t size);
30 |
31 | void *get_page_base(const void *address);
32 |
33 | uint64_t get_page_offset(const void *address);
34 |
35 | const char *boolToString(bool b);
36 |
37 | #endif
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/libHostSystem/src/main/jni/cpsr_util.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/9/2015.
19 | //
20 |
21 | #ifndef NDKTEST_CPSR_UTIL_H
22 | #define NDKTEST_CPSR_UTIL_H
23 |
24 | #define CPSR_FLAG_THUMB (1 << 5)
25 | #define CPSR_FLAG_DISABLE_FIQ_INTERRUPTS (1 << 6)
26 | #define CPSR_FLAG_DISABLE_IRQ_INTERRUPTS (1 << 7)
27 | #define CPSR_FLAG_JAZELLE (1 << 24)
28 | #define CPSR_FLAG_UNDERFLOW_SATURATION (1 << 27)
29 | #define CPSR_FLAG_SIGNED_OVERFLOW (1 << 28)
30 | #define CPSR_FLAG_CARRY (1 << 29)
31 | #define CPSR_FLAG_ZERO (1 << 30)
32 | #define CPSR_FLAG_NEGATIVE (1 << 31)
33 |
34 | #endif //NDKTEST_CPSR_UTIL_H
35 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/exceptions.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _EXCEPTIONS_H
18 | #define _EXCEPTIONS_H
19 |
20 | #include
21 | #include
22 |
23 | bool hasExceptionOccurred(JNIEnv *env);
24 |
25 | void throwNewJNIException(JNIEnv *env, const char *classNameNotSignature, const char *message);
26 |
27 | #define RETURN_ON_EXCEPTION(env, cleanup, defReturn) \
28 | do { \
29 | if(hasExceptionOccurred((env))) { \
30 | do { \
31 | cleanup \
32 | } while (0); \
33 | return defReturn; \
34 | } } while(0);
35 |
36 |
37 | #endif
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/exceptions.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "exceptions.h"
18 |
19 | #ifdef __cplusplus
20 | extern "C" {
21 | #endif
22 |
23 | bool hasExceptionOccurred(JNIEnv *env)
24 | {
25 | return (*env)->ExceptionCheck(env);
26 | }
27 |
28 | void throwNewJNIException(JNIEnv *env, const char *classNameNotSignature, const char *message)
29 | {
30 | if (!hasExceptionOccurred(env))
31 | {
32 | jclass jClass = (*env)->FindClass(env, classNameNotSignature);
33 | (*env)->ThrowNew(env, jClass, message);
34 | }
35 | }
36 |
37 |
38 | #ifdef __cplusplus
39 | }
40 | #endif
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/app/src/main/jni/lib_setup.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifdef __cplusplus
18 | extern "C"
19 | {
20 | #endif
21 |
22 | #include "lib_setup.h"
23 | #include
24 | #include
25 | #include
26 |
27 | void init()
28 | {
29 | init_system_info();
30 | init_trappoints();
31 | init_self_patching_trappoints();
32 | }
33 |
34 | void destroy()
35 | {
36 | destroy_self_patching_trappoints();
37 | destroy_trappoints();
38 | destroy_system_info();
39 | }
40 |
41 | #ifdef __cplusplus
42 | }
43 | #endif
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_version_dependent/VERSION064/entrypoints/quick_entrypoints.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/18/2016.
19 | //
20 |
21 | #ifndef NDKTEST_ART_ENTRYPOINTS_QUICK_ENTRYPOINTS_H
22 | #define NDKTEST_ART_ENTRYPOINTS_QUICK_ENTRYPOINTS_H
23 |
24 | #include
25 | #include
26 |
27 | struct Thread;
28 |
29 | struct PACKED(4) QuickEntryPoints
30 | {
31 | #define ENTRYPOINT_ENUM(name, rettype, ...) rettype ( * p ## name )( __VA_ARGS__ );
32 |
33 | #include "quick_entrypoints_list.h"
34 | QUICK_ENTRYPOINT_LIST(ENTRYPOINT_ENUM)
35 | #undef QUICK_ENTRYPOINT_LIST
36 | #undef ENTRYPOINT_ENUM
37 | };
38 |
39 | #endif //NDKTEST_ART_ENTRYPOINTS_QUICK_ENTRYPOINTS_H
40 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/config_constants.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _CONFIG_CONSTANTS_H
18 | #define _CONFIG_CONSTANTS_H
19 |
20 | #define ARCHITECTURE_ARM 1
21 | #define ARCHITECTURE_ARM64 2
22 | #define ARCHITECTURE_x86 3
23 | #define ARCHITECTURE_x86_64 4
24 | #define ARCHITECTURE_MIPS 5
25 | #define ARCHITECTURE_MIPS64 6
26 |
27 | #define LOG_LEVEL_ALL 0
28 | #define LOG_LEVEL_VERBOSE 1
29 | #define LOG_LEVEL_DEBUG 2
30 | #define LOG_LEVEL_INFO 3
31 | #define LOG_LEVEL_WARN 4
32 | #define LOG_LEVEL_ERROR 5
33 | #define LOG_LEVEL_FATAL 6
34 | #define LOG_LEVEL_NOLOG 7
35 |
36 |
37 | // Supported Oat versions
38 | #define OAT_VERSION_045 0
39 | #define OAT_VERSION_064 1
40 |
41 |
42 | #endif
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/error.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/13/2015.
19 | //
20 |
21 | #ifndef NDKTEST_ERROR_H
22 | #define NDKTEST_ERROR_H
23 |
24 | #include
25 | #include "logging.h"
26 |
27 | static const char * const no_error = "No Error.";
28 | static const char *last_error = "No Error.";
29 |
30 | void set_last_error(const char * c)
31 | {
32 | LOGE("Setting the last error to %s", c);
33 | last_error = c;
34 | }
35 |
36 | bool error_occurred()
37 | {
38 | return last_error != no_error;
39 | }
40 |
41 | const char * get_last_error()
42 | {
43 | return last_error;
44 | }
45 |
46 | void clear_last_error()
47 | {
48 | LOGE("Clearing the last set error.");
49 | last_error = no_error;
50 | }
51 |
52 | #endif //NDKTEST_ERROR_H
53 |
--------------------------------------------------------------------------------
/app/src/main/jni/art_resolution.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef NDKTEST_ART_RESOLUTION_H
18 | #define NDKTEST_ART_RESOLUTION_H
19 |
20 | #include
21 | #include
22 |
23 | struct ArtClassContext
24 | {
25 | struct OatFile oat_file;
26 | struct OatDexFile oat_dex;
27 | struct OatClass oat_class;
28 | };
29 | struct ArtMethodContext
30 | {
31 | struct ArtClassContext clazz;
32 | struct OatMethod oat_method;
33 | };
34 | bool android_FindLoadedClass(struct ArtClassContext* result, const char* class_name);
35 | bool android_FindLoadedMethod(struct ArtMethodContext* result, const char* class_name,
36 | const char* method_name, const char* method_proto);
37 |
38 |
39 |
40 | #endif //NDKTEST_ART_H
41 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/util.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include
18 | #include
19 |
20 |
21 |
22 | #ifdef __cplusplus
23 | extern "C"
24 | {
25 | #endif
26 |
27 | uint64_t round_down(uint64_t value, uint64_t size)
28 | {
29 | return value - (value % size);
30 | }
31 |
32 | uint64_t round_up(uint64_t value, uint64_t size)
33 | {
34 | uint64_t rounded_down = round_down(value, size);
35 | if (rounded_down == value)
36 | {
37 | return value;
38 | }
39 | return rounded_down + size;
40 | }
41 |
42 | void *align_address_to_size(const void *addr, uint64_t size)
43 | {
44 | return (void *) round_down((uint64_t) addr, size);
45 | }
46 |
47 | const char *boolToString(bool b)
48 | {
49 | return b ? "true" : "false";
50 | }
51 |
52 | #ifdef __cplusplus
53 | }
54 | #endif
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/libHooking/src/main/jni/instruction_bkpt.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/12/2015.
19 | //
20 |
21 | #ifndef NDKTEST_BKPT_H
22 | #define NDKTEST_BKPT_H
23 |
24 |
25 | #include
26 |
27 | static int const ARM_BKPT_SCHEMATIC = 0b11100001001000000000000001110000;
28 | static int const ARM_BKPT_OP_UPPER = 0b00000000000011111111111100000000;
29 | static int const ARM_BKPT_OP_LOWER = 0b00000000000000000000000000001111;
30 |
31 | static int const THUMB_BKPT_SCHEMATIC = 0b1011111000000000;
32 | static int const THUMB_BKPT_OPERAND = 0b0000000011111111;
33 |
34 |
35 | uint32_t make_arm_breakpoint(uint16_t imm16);
36 | uint16_t extract_arm_breakpoint_operand(uint32_t opcode);
37 |
38 | uint16_t make_thumb_breakpoint(uint8_t imm8);
39 | uint8_t extract_thumb_breakpoint_operand(uint16_t opcode);
40 |
41 | #endif //NDKTEST_BKPT_H
42 |
--------------------------------------------------------------------------------
/app/src/main/jni/elf.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _ELF_ANALYZER_H
18 | #define _ELF_ANALYZER_H
19 |
20 | #include
21 | #include
22 |
23 | enum SegmentType
24 | {
25 | Null = PT_NULL,
26 | Load = PT_LOAD,
27 | Dynamic = PT_DYNAMIC,
28 | Interpreter = PT_INTERP,
29 | Note = PT_NOTE,
30 | ShLib_Unused = PT_SHLIB,
31 | ProgramHeader = PT_PHDR,
32 | ThreadLocalStorage = PT_TLS,
33 | OsSpecificLow = PT_LOOS,
34 | OsSpecificHigh = PT_HIOS,
35 | ProcessorSpecificLow = PT_LOPROC,
36 | ProcessorSpecificHigh = PT_HIPROC,
37 | GCC_EHFrameHeader = PT_GNU_EH_FRAME,
38 | GCC_StackExecutability = PT_GNU_STACK
39 | };
40 |
41 | bool IsValidElfFileHeader(void* p);
42 |
43 | #endif
--------------------------------------------------------------------------------
/libUtility/src/main/jni/dirty_dirty_std_library_hacks.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/18/2016.
19 | //
20 |
21 | #ifndef NDKTEST_DIRTY_DIRTY_STD_LIBRARY_HACKS_H
22 | #define NDKTEST_DIRTY_DIRTY_STD_LIBRARY_HACKS_H
23 |
24 | #include
25 |
26 | struct DIRTY_DIRTY_STD_STRING_HACK
27 | {
28 | uint8_t content[32];
29 | };
30 | struct DIRTY_DIRTY_STD_VECTOR_HACK
31 | {
32 | // This is SOOOOO NASTY, NEVER ACTUALLY USE THIS.
33 | // This is simply here, to emulate the contents of a std::vector in c.
34 |
35 | void* mem_begin; // This should be the start-address of the memory
36 | void* in_use_end; // This should point to the first byte after the used entries
37 | void* allocated_end; // This should point to the first byte after the allocated memory
38 | };
39 |
40 | #endif //NDKTEST_DIRTY_DIRTY_STD_LIBRARY_HACKS_H
41 |
--------------------------------------------------------------------------------
/libHooking/src/main/jni/invocation_hook.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 2/9/2016.
19 | //
20 |
21 |
22 | #include "invocation_hook.h"
23 | #include
24 |
25 | void* InvocationHook_Install (void *address, uint32_t trap_method,
26 | HOOKCALLBACK OnEntry, void* OnEntry_Arg,
27 | HOOKCALLBACK OnReturn, void* OnReturn_Arg)
28 | {
29 | return SelfPatchingTrappoint_Install(address, trap_method, OnEntry, OnEntry_Arg, OnReturn, OnReturn_Arg, ExtractReturnAddress);
30 | }
31 | bool InvocationHook_Enable (void *p)
32 | {
33 | return SelfPatchingTrappoint_Enable(p);
34 | }
35 | bool InvocationHook_Disable (void *p)
36 | {
37 | return SelfPatchingTrappoint_Disable(p);
38 | }
39 | void InvocationHook_Uninstall (void *p)
40 | {
41 | return SelfPatchingTrappoint_Uninstall(p);
42 | }
43 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_version_dependent/VERSION064/entrypoints/portable_entrypoints.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/18/2016.
19 | //
20 |
21 | #ifndef NDKTEST_ART_ENTRYPOINTS_PORTABLE_ENTRYPOINTS_H
22 | #define NDKTEST_ART_ENTRYPOINTS_PORTABLE_ENTRYPOINTS_H
23 |
24 | // Pointers to functions that are called by code generated by compiler's adhering to the portable
25 | // compiler ABI.
26 | struct PACKED(4) PortableEntryPoints
27 | {
28 | // Invocation
29 | /*
30 | void (*pPortableImtConflictTrampoline)(mirror::ArtMethod*);
31 | void (*pPortableResolutionTrampoline)(mirror::ArtMethod*);
32 | void (*pPortableToInterpreterBridge)(mirror::ArtMethod*);
33 | */
34 | void (*pPortableImtConflictTrampoline)(void*);
35 | void (*pPortableResolutionTrampoline)(void*);
36 | void (*pPortableToInterpreterBridge)(void*);
37 | };
38 |
39 | #endif //NDKTEST_ART_ENTRYPOINTS_PORTABLE_ENTRYPOINTS_H
40 |
--------------------------------------------------------------------------------
/libHooking/src/main/jni/breakpoint.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 2/9/2016.
19 | //
20 |
21 | #include "breakpoint.h"
22 |
23 | #include
24 | #include "self_patching_trappoint.h"
25 |
26 | // Internally this does nothing but forward to a self-patching trappoint
27 | // which patches at the next instruction to be executed
28 |
29 | void* Breakpoint_Install (void *address, uint32_t trap_method, HOOKCALLBACK handler, void *arg)
30 | {
31 | return SelfPatchingTrappoint_Install(address, trap_method, handler, arg, NULL, NULL, ExtractNextExecutedInstructionPointer);
32 | }
33 | bool Breakpoint_Enable (void *p)
34 | {
35 | return SelfPatchingTrappoint_Enable(p);
36 | }
37 | bool Breakpoint_Disable (void *p)
38 | {
39 | return SelfPatchingTrappoint_Disable(p);
40 | }
41 | void Breakpoint_Uninstall (void *p)
42 | {
43 | SelfPatchingTrappoint_Uninstall(p);
44 | }
45 |
--------------------------------------------------------------------------------
/libUtility/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.model.native'
2 |
3 | model {
4 | android {
5 | compileSdkVersion = 24
6 | buildToolsVersion = '24.0.1'
7 |
8 | defaultConfig {
9 | minSdkVersion.apiLevel = 21
10 | targetSdkVersion.apiLevel = 24
11 | versionCode = 1
12 | versionName = '1.0'
13 | }
14 | ndk {
15 | moduleName = 'Utility'
16 | CFlags.addAll(['--std=gnu99'])
17 | abiFilters.addAll(['armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'])
18 | ldLibs.addAll(['android', 'log'])
19 | }
20 | buildTypes {
21 | debug {
22 | ndk.with {
23 | debuggable = true
24 | }
25 | }
26 | release {
27 | minifyEnabled = false
28 | proguardFiles.add(file('proguard-rules.pro'))
29 | }
30 | }
31 | }
32 | }
33 |
34 | // This is just copy out the header file and built lib into distribution
35 | // directory for clint application to use; it is a small overhead of this sample:
36 | // both lib and app are put inside one project space [save maintenance time]
37 | task(distributeLib, type : Copy) {
38 | // trigger build library
39 | dependsOn assemble
40 |
41 | into '../distribution/'
42 | from('src/main/jni') {
43 | include '**/*.h'
44 | into 'include/utility/'
45 | }
46 | from('build/outputs/native/release/lib') {
47 | into 'lib/utility/'
48 | }
49 | }
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_version_dependent/VERSION064/stack.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/18/2016.
19 | //
20 |
21 | #ifndef NDKTEST_ART_STACK_H
22 | #define NDKTEST_ART_STACK_H
23 |
24 | #include
25 | #include "../util/macros.h"
26 |
27 | // ShadowFrame has 2 possible layouts:
28 | // - interpreter - separate VRegs and reference arrays. References are in the reference array.
29 | // - JNI - just VRegs, but where every VReg holds a reference.
30 | struct ShadowFrame {
31 | const uint32_t number_of_vregs_;
32 | // Link to previous shadow frame or null.
33 | struct ShadowFrame* link_;
34 | void* method_; // used to be ArtMethod*
35 | uint32_t dex_pc_;
36 | uint32_t vregs_[0];
37 | };
38 |
39 | struct PACKED(4) ManagedStack
40 | {
41 | void** top_quick_frame_; // actually a ArtMethod**
42 | struct ManagedStack* link_;
43 | struct ShadowFrame* top_shadow_frame_;
44 | };
45 |
46 | #endif //NDKTEST_ART_STACK_H
47 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/system_info.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | #include "system_info.h"
17 |
18 | #include
19 |
20 | #include "logging.h"
21 | #include "util.h"
22 |
23 |
24 | #include
25 |
26 | static long SYSTEM_PAGE_SIZE = -1;
27 |
28 | void init_system_info()
29 | {
30 | SYSTEM_PAGE_SIZE = sysconf(_SC_PAGE_SIZE);
31 | if (SYSTEM_PAGE_SIZE < 1)
32 | {
33 | LOGE("Error while resolving system page size. Assuming 4k bytes. Err: %s", strerror(errno));
34 | SYSTEM_PAGE_SIZE = 4096;
35 | }
36 | }
37 |
38 | void destroy_system_info()
39 | {
40 | }
41 |
42 | long getSystemPageSize(void)
43 | {
44 | return SYSTEM_PAGE_SIZE;
45 | }
46 |
47 | void *get_page_base(const void *address)
48 | {
49 | return align_address_to_size(address, (uint64_t)getSystemPageSize());
50 | }
51 |
52 | uint64_t get_page_offset(const void *address)
53 | {
54 | return (uint64_t) (address - get_page_base(address));
55 | }
56 |
57 |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/libHooking/src/main/jni/invocation_hook.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 2/9/2016.
19 | //
20 |
21 | #ifndef ARTIST_INVOCATION_HOOK_H
22 | #define ARTIST_INVOCATION_HOOK_H
23 |
24 | // This implements a top-level function hook. This means that it tracks a functions top-most call.
25 | // In practice this means that if a function is called from somewhere inside itself again
26 | // (more general recursion) these calls will not be monitored.
27 |
28 | #include
29 | #include
30 | #include "hooking_common.h"
31 | #include "self_patching_trappoint.h"
32 |
33 | void* InvocationHook_Install (void *address, uint32_t trap_method,
34 | HOOKCALLBACK OnEntry, void* OnEntry_Arg,
35 | HOOKCALLBACK OnReturn, void* OnReturn_Arg);
36 | bool InvocationHook_Enable (void *p);
37 | bool InvocationHook_Disable (void *p);
38 | void InvocationHook_Uninstall (void *p);
39 |
40 | #endif //ARTIST_INVOCATION_HOOK_H
41 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_version_dependent/VERSION064/entrypoints/interpreter_entrypoints.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/18/2016.
19 | //
20 |
21 | #ifndef NDKTEST_ART_ENTRYPOINTS_INTERPRETER_ENTRYPOINTS_H
22 | #define NDKTEST_ART_ENTRYPOINTS_INTERPRETER_ENTRYPOINTS_H
23 |
24 | #include
25 | #include "../../../dex_internal.h"
26 | #include "../stack.h"
27 |
28 | struct Thread;
29 | // Pointers to functions that are called by interpreter trampolines via thread-local storage.
30 | struct PACKED(4) InterpreterEntryPoints {
31 | void (*pInterpreterToInterpreterBridge)(struct Thread* self, const struct CodeItem* code_item,
32 | struct ShadowFrame* shadow_frame, union JValue* result);
33 | void (*pInterpreterToCompiledCodeBridge)(struct Thread* self, const struct CodeItem* code_item,
34 | struct ShadowFrame* shadow_frame, union JValue* result);
35 | };
36 |
37 | #endif //NDKTEST_ART_ENTRYPOINTS_INTERPRETER_ENTRYPOINTS_H
38 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/lukas/ndktest/MemoryAnalysis/MemoryInfo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.example.lukas.ndktest.MemoryAnalysis;
18 |
19 | import com.example.lukas.ndktest.MappedMemoryRegion;
20 | import com.google.common.collect.ImmutableListMultimap;
21 |
22 | /**
23 | * Created by Lukas on 4/20/2015.
24 | */
25 | public class MemoryInfo
26 | {
27 | public final MappedMemoryRegion[] Regions;
28 | public final ImmutableListMultimap DescriptionLookupTable;
29 |
30 | public MemoryInfo(MappedMemoryRegion[] regions)
31 | {
32 | ImmutableListMultimap.Builder builder = ImmutableListMultimap.builder();
33 | this.Regions = regions.clone();
34 | for(MappedMemoryRegion region : this.Regions)
35 | {
36 | if(region.Description != null && !region.Description.isEmpty())
37 | {
38 | builder.put(region.Description, region);
39 | }
40 | }
41 | this.DescriptionLookupTable = builder.build();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/libHooking/src/armeabi/jni/instruction_bkpt.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/12/2015.
19 | //
20 |
21 | #include "../../main/jni/instruction_bkpt.h"
22 |
23 | uint32_t make_arm_breakpoint(uint16_t imm16)
24 | {
25 | uint32_t imm_upper = imm16;
26 | imm_upper <<= 4;
27 | imm_upper &= ARM_BKPT_OP_UPPER;
28 |
29 | uint16_t imm_lower = (imm16 & 0b1111);
30 |
31 | uint32_t opcode = ARM_BKPT_SCHEMATIC | imm_upper | imm_lower;
32 |
33 | return opcode;
34 | }
35 |
36 | uint16_t extract_arm_breakpoint_operand(uint32_t opcode)
37 | {
38 | uint32_t imm_upper = (opcode & ARM_BKPT_OP_UPPER) >> 4;
39 | uint32_t imm_lower = (opcode & ARM_BKPT_OP_LOWER);
40 | uint16_t result = (uint16_t)((imm_upper | imm_lower) & 0xFFFF);
41 | return result;
42 | }
43 |
44 | uint16_t make_thumb_breakpoint(uint8_t imm8)
45 | {
46 | uint16_t opcode = (uint16_t)THUMB_BKPT_SCHEMATIC | imm8;
47 | return opcode;
48 | }
49 |
50 | uint8_t extract_thumb_breakpoint_operand(uint16_t opcode)
51 | {
52 | uint8_t result = (uint8_t)(opcode & THUMB_BKPT_OPERAND);
53 | return result;
54 | }
55 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_version_dependent/VERSION045/stack.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/30/2016.
19 | //
20 |
21 | #ifndef NDKTEST_STACK_H
22 | #define NDKTEST_STACK_H
23 |
24 | #include
25 |
26 | #include
27 |
28 | // ShadowFrame has 3 possible layouts:
29 | // - portable - a unified array of VRegs and references. Precise references need GC maps.
30 | // - interpreter - separate VRegs and reference arrays. References are in the reference array.
31 | // - JNI - just VRegs, but where every VReg holds a reference.
32 | struct ShadowFrame {
33 | // Link to previous shadow frame or NULL.
34 | struct ShadowFrame* link_;
35 | void* method_; // mirror::ArtMethod* method_;
36 | uint32_t dex_pc_;
37 | uint32_t vregs_[0];
38 |
39 | };
40 |
41 | struct ManagedStack;
42 | struct PACKED(4) ManagedStack
43 | {
44 | struct ManagedStack* link_;
45 | struct ShadowFrame* top_shadow_frame_;
46 | void* top_quick_frame_; // StackReference* top_quick_frame_;
47 | uintptr_t top_quick_frame_pc_;
48 | };
49 |
50 |
51 | #endif //NDKTEST_STACK_H
52 |
--------------------------------------------------------------------------------
/app/src/main/jni/statistics.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 |
18 | #ifndef NDKTEST_STATISTICS_H
19 | #define NDKTEST_STATISTICS_H
20 |
21 | #include
22 | #include
23 |
24 | bool stats_NumCompiledMethodsInOatDexClass(struct OatClass* oat_class, uint32_t* result_compiled, uint32_t* result_total);
25 | bool stats_NumCompiledMethodsInOatDexFile(struct OatDexFile* oat_dex, uint32_t* result_compiled, uint32_t* result_total);
26 | bool stats_NumCompiledMethodsInOatFile(struct OatFile* oat, uint32_t* result_compiled, uint32_t* result_total);
27 |
28 | bool stats_logNumCompiledMethodsInOatClass(struct OatClass* oat_class, uint32_t* result_compiled, uint32_t* result_total);
29 | bool stats_logNumCompiledMethodsInOatDexFile(struct OatDexFile* oat_dex, uint32_t* result_compiled, uint32_t* result_total);
30 | bool stats_logNumCompiledMethodsInOatFile(struct OatFile* oat);
31 |
32 | typedef void (*OATDEX_TO_OAT_MERGER)(void* oat, struct OatDexFile* current_oat_dex);
33 | typedef void (*OATCLASS_TO_OATDEX_MERGER)(void* oat_dex, struct OatClass* current_oat_class);
34 | typedef void (*OAT_TO_FINAL_MERGER)(void* final, struct OatFile* oat);
35 |
36 | #endif //NDKTEST_STATISTICS_H
37 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/lukas/ndktest/MemoryAnalysis/MemoryAnalyzer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.example.lukas.ndktest.MemoryAnalysis;
18 |
19 | import android.provider.MediaStore;
20 |
21 | import com.example.lukas.ndktest.MappedMemoryRegion;
22 |
23 | import java.io.BufferedReader;
24 | import java.io.File;
25 | import java.io.FileReader;
26 | import java.io.IOException;
27 | import java.util.ArrayList;
28 |
29 | /**
30 | * Created by Lukas on 4/20/2015.
31 | */
32 | public class MemoryAnalyzer
33 | {
34 | public static MemoryInfo getMemoryInfo() throws IOException
35 | {
36 | return new MemoryInfo(getMemoryRegions());
37 | }
38 | public static MappedMemoryRegion[] getMemoryRegions() throws IOException
39 | {
40 | ArrayList list = new ArrayList<>();
41 | BufferedReader reader = new BufferedReader(new FileReader("/proc/self/maps"));
42 | String line = null;
43 | while ((line = reader.readLine()) != null)
44 | {
45 | list.add(MemoryRegionParser.getMemoryRegionFromMapsFileLine(line));
46 | }
47 | MappedMemoryRegion[] result = new MappedMemoryRegion[list.size()];
48 | return list.toArray(result);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/libHooking/src/armeabi/jni/generate_trap_instruction.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/14/2015.
19 | //
20 | #include
21 | #include
22 |
23 | #include "../../main/jni/generate_trap_instruction.h"
24 | #include "../../main/jni/trappoint_interface.h"
25 | #include "../../main/jni/instruction_bkpt.h"
26 | #include "../../main/jni/instruction_illegal.h"
27 |
28 | uint16_t make_thumb_trap_instruction(uint32_t method)
29 | {
30 | if((method & TRAP_METHOD_INSTR_KNOWN_ILLEGAL) != 0)
31 | {
32 | return make_thumb_illegal_instruction((uint8_t)rand());
33 | }
34 | if((method & TRAP_METHOD_INSTR_BKPT) != 0)
35 | {
36 | return make_thumb_breakpoint((uint8_t)rand());
37 | }
38 | LOGE("INVALID TRAP-GENERATION METHOD(%d).", method);
39 | return NULL;
40 | }
41 | uint32_t make_arm_trap_instruction(uint32_t method)
42 | {
43 | if((method & TRAP_METHOD_INSTR_KNOWN_ILLEGAL) != 0)
44 | {
45 | return make_arm_illegal_instruction(UNCONDITIONAL, (uint8_t)rand());
46 | }
47 | if((method & TRAP_METHOD_INSTR_BKPT) != 0)
48 | {
49 | return make_arm_breakpoint((uint8_t)rand());
50 | }
51 | LOGE("INVALID TRAP-GENERATION METHOD(%d).", method);
52 | return NULL;
53 | }
54 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_version_dependent/VERSION045/entrypoints/jni_entrypoints.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /*
18 | * Copyright (C) 2012 The Android Open Source Project
19 | *
20 | * Licensed under the Apache License, Version 2.0 (the "License");
21 | * you may not use this file except in compliance with the License.
22 | * You may obtain a copy of the License at
23 | *
24 | * http://www.apache.org/licenses/LICENSE-2.0
25 | *
26 | * Unless required by applicable law or agreed to in writing, software
27 | * distributed under the License is distributed on an "AS IS" BASIS,
28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 | * See the License for the specific language governing permissions and
30 | * limitations under the License.
31 | */
32 |
33 | #ifndef ART_RUNTIME_ENTRYPOINTS_JNI_JNI_ENTRYPOINTS_H_
34 | #define ART_RUNTIME_ENTRYPOINTS_JNI_JNI_ENTRYPOINTS_H_
35 |
36 | #include
37 | #include
38 |
39 | // Pointers to functions that are called by JNI trampolines via thread-local storage.
40 | struct PACKED(4) JniEntryPoints {
41 | // Called when the JNI method isn't registered.
42 | void* (*pDlsymLookup)(JNIEnv* env, jobject);
43 | };
44 |
45 | #endif // ART_RUNTIME_ENTRYPOINTS_JNI_JNI_ENTRYPOINTS_H_
46 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/memory.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _MEMORY_H
18 | #define _MEMORY_H
19 |
20 | #ifdef __cplusplus
21 | extern "C"
22 | {
23 | #endif
24 |
25 | #include
26 |
27 | #include
28 | #include
29 |
30 | #include
31 | #include
32 | #include
33 | #include
34 |
35 | typedef bool (*OUTPUT_CALLBACK)(const void *startingAddress, uint64_t currentOffset,
36 | int numCurrentBytes, void *additionalInfo);
37 |
38 | bool set_memory_protection(const void *addr, jlong numBytes, jboolean read, jboolean write,
39 | jboolean execute);
40 |
41 | bool iterate_byte_array_chunks(JNIEnv *env, const void *addr, jlong numBytes, jlong bytesPerLine,
42 | OUTPUT_CALLBACK output, void *additionalInfo);
43 |
44 | bool iterate_byte_array_chunks_primitive(const void *addr, jlong numBytes, jlong bytesPerLine,
45 | OUTPUT_CALLBACK output, void *additionalInfo);
46 |
47 | void dump_process_memory_map(void);
48 |
49 | void *allocate_memory_chunk(size_t size);
50 |
51 | void free_memory_chunk(void *mem);
52 |
53 | #ifdef __cplusplus
54 | }
55 | #endif
56 |
57 |
58 | #endif
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/throw_location.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /*
18 | * Copyright (C) 2013 The Android Open Source Project
19 | *
20 | * Licensed under the Apache License, Version 2.0 (the "License");
21 | * you may not use this file except in compliance with the License.
22 | * You may obtain a copy of the License at
23 | *
24 | * http://www.apache.org/licenses/LICENSE-2.0
25 | *
26 | * Unless required by applicable law or agreed to in writing, software
27 | * distributed under the License is distributed on an "AS IS" BASIS,
28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 | * See the License for the specific language governing permissions and
30 | * limitations under the License.
31 | */
32 |
33 | #ifndef ART_RUNTIME_THROW_LOCATION_H_
34 | #define ART_RUNTIME_THROW_LOCATION_H_
35 |
36 | #include
37 | #include
38 |
39 | struct PACKED(4) ThrowLocation {
40 | // The 'this' reference of the throwing method.
41 | void* this_object_;
42 | // The throwing method.
43 | void* method_;
44 | // The instruction within the throwing method.
45 | uint32_t dex_pc_;
46 | // Ensure 8byte alignment on 64bit.
47 | #ifdef __LP64__
48 | uint32_t pad_;
49 | #endif
50 | };
51 |
52 | #endif // ART_RUNTIME_THROW_LOCATION_H_
53 |
--------------------------------------------------------------------------------
/libHooking/src/armeabi/jni/trappoint.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/6/2015.
19 | //
20 |
21 | #ifndef NDKTEST_TRAPPOINT_H
22 | #define NDKTEST_TRAPPOINT_H
23 |
24 |
25 | #include
26 |
27 | #include
28 |
29 | #include "../../main/jni/trappoint_interface.h"
30 |
31 | #ifdef __cplusplus
32 | extern "C"
33 | {
34 | #endif
35 |
36 | typedef struct ThumbCodeInfo
37 | {
38 | uint16_t preserved;
39 | uint16_t trap_instruction;
40 | } ThumbCodeInfo;
41 |
42 | typedef struct ArmCodeInfo
43 | {
44 | uint32_t preserved;
45 | uint32_t trap_instruction;
46 | } ArmCodeInfo;
47 |
48 | struct TrapPointInfo
49 | {
50 | struct InstructionInfo target_instruction_info;
51 |
52 | struct
53 | {
54 | bool enabled;
55 | HOOKCALLBACK callback;
56 | void* callback_args;
57 | } state;
58 |
59 | struct
60 | {
61 | uint32_t instr_size;
62 | uint32_t trapping_method;
63 | union
64 | {
65 | ThumbCodeInfo thumb;
66 | ArmCodeInfo arm;
67 | };
68 | } hook_info;
69 | };
70 |
71 | #ifdef __cplusplus
72 | }
73 | #endif
74 |
75 | #endif //NDKTEST_TRAPPOINT_H
76 |
77 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/mem_map.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/18/2015.
19 | //
20 |
21 | #ifndef NDKTEST_MEM_MAP_H
22 | #define NDKTEST_MEM_MAP_H
23 |
24 | #include
25 | #include
26 | #include
27 | #include
28 |
29 |
30 | typedef struct MemMap
31 | {
32 | const char *name_;
33 | byte *begin_; // Start of data.
34 | size_t size_; // Length of data.
35 | void *base_begin_; // Page-aligned base address.
36 | size_t base_size_; // Length of mapping. May be changed by RemapAtEnd (ie Zygote).
37 | int prot_; // Protection of the map.
38 | // When reuse_ is true, this is just a view of an existing mapping
39 | // and we do not take ownership and are not responsible for
40 | // unmapping.
41 | bool reuse_;
42 | } MemMap;
43 |
44 | bool mem_map_ForceClear(MemMap *self);
45 |
46 | bool mem_map_Initialize(MemMap *self);
47 |
48 | static bool CheckMapRequest(byte *expected_ptr, void *actual_ptr, size_t byte_count);
49 |
50 | bool mem_map_MapFileAtAddress(MemMap *self, byte *expected_ptr, size_t byte_count,
51 | int prot, int flags, int fd, off_t start, bool reuse,
52 | const char *filename);
53 |
54 | bool mem_map_Unmap(MemMap *self);
55 |
56 | #endif //NDKTEST_MEM_MAP_H
57 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_version_dependent/VERSION045/entrypoints/portable_entrypoints.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /*
18 | * Copyright (C) 2013 The Android Open Source Project
19 | *
20 | * Licensed under the Apache License, Version 2.0 (the "License");
21 | * you may not use this file except in compliance with the License.
22 | * You may obtain a copy of the License at
23 | *
24 | * http://www.apache.org/licenses/LICENSE-2.0
25 | *
26 | * Unless required by applicable law or agreed to in writing, software
27 | * distributed under the License is distributed on an "AS IS" BASIS,
28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 | * See the License for the specific language governing permissions and
30 | * limitations under the License.
31 | */
32 |
33 | #ifndef ART_RUNTIME_ENTRYPOINTS_PORTABLE_PORTABLE_ENTRYPOINTS_H_
34 | #define ART_RUNTIME_ENTRYPOINTS_PORTABLE_PORTABLE_ENTRYPOINTS_H_
35 | #include
36 |
37 |
38 | // Pointers to functions that are called by code generated by compiler's adhering to the portable
39 | // compiler ABI.
40 | struct PACKED(4) PortableEntryPoints {
41 | // Invocation
42 | void (*pPortableImtConflictTrampoline)(void*);
43 | void (*pPortableResolutionTrampoline)(void*);
44 | void (*pPortableToInterpreterBridge)(void*);
45 | };
46 |
47 | #endif // ART_RUNTIME_ENTRYPOINTS_PORTABLE_PORTABLE_ENTRYPOINTS_H_
48 |
--------------------------------------------------------------------------------
/libHooking/src/main/jni/trappoint_interface.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/6/2015.
19 | //
20 |
21 | #ifndef NDKTEST_TRAPPOINT_INTERFACE_H
22 | #define NDKTEST_TRAPPOINT_INTERFACE_H
23 |
24 | #include
25 | #include
26 | #include "hooking_common.h"
27 |
28 | #ifdef __cplusplus
29 | extern "C"
30 | {
31 | #endif
32 |
33 | #define TRAP_METHOD_INSTR_BKPT 1
34 | #define TRAP_METHOD_INSTR_KNOWN_ILLEGAL 2
35 | #define TRAP_METHOD_SIG_TRAP 0x10000
36 | #define TRAP_METHOD_SIG_ILL 0x20000
37 |
38 | struct TrapPointInfo;
39 | typedef struct TrapPointInfo TrapPointInfo;
40 |
41 | typedef bool (*TRAPPOINT_PREDICATE)(TrapPointInfo *trap, void *args);
42 |
43 | void init_trappoints();
44 | void destroy_trappoints();
45 | void dump_installed_trappoints_info();
46 |
47 | TrapPointInfo * trappoint_Install (void *addr, uint32_t method, HOOKCALLBACK handler,
48 | void *additionalArg);
49 | void trappoint_Destroy (TrapPointInfo *trap);
50 |
51 | bool trappoint_Enable (TrapPointInfo *trap);
52 | bool trappoint_Disable (TrapPointInfo *trap);
53 |
54 | TrapPointInfo * trappoint_FindWithPredicate (TRAPPOINT_PREDICATE p, void *args);
55 | bool trappoint_ValidateContents (TrapPointInfo *trap);
56 |
57 | #ifdef __cplusplus
58 | }
59 | #endif
60 |
61 | #endif //NDKTEST_TRAPPOINT_INTERFACE_H
62 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/leb128.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/23/2015.
19 | //
20 |
21 | #ifndef NDKTEST_LEB128_H
22 | #define NDKTEST_LEB128_H
23 |
24 | #include
25 |
26 | // Reads an unsigned LEB128 value, updating the given pointer to point
27 | // just past the end of the read value. This function tolerates
28 | // non-zero high-order bits in the fifth encoded byte.
29 | uint32_t DecodeUnsignedLeb128(const uint8_t **data);
30 |
31 | // Reads an unsigned LEB128 + 1 value. updating the given pointer to point
32 | // just past the end of the read value. This function tolerates
33 | // non-zero high-order bits in the fifth encoded byte.
34 | // It is possible for this function to return -1.
35 | int32_t DecodeUnsignedLeb128P1(const uint8_t **data);
36 |
37 | // Reads a signed LEB128 value, updating the given pointer to point
38 | // just past the end of the read value. This function tolerates
39 | // non-zero high-order bits in the fifth encoded byte.
40 | int32_t DecodeSignedLeb128(const uint8_t **data);
41 |
42 | // Returns the number of bytes needed to encode the value in unsigned LEB128.
43 | uint32_t UnsignedLeb128Size(uint32_t data);
44 |
45 | // Returns the number of bytes needed to encode the value in unsigned LEB128.
46 | uint32_t SignedLeb128Size(int32_t data);
47 |
48 | uint8_t *EncodeUnsignedLeb128(uint8_t *dest, uint32_t value);
49 |
50 | uint8_t *EncodeSignedLeb128(uint8_t *dest, int32_t value);
51 |
52 | #endif //NDKTEST_LEB128_H
53 |
--------------------------------------------------------------------------------
/libHooking/src/main/jni/self_patching_trappoint.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/19/2015.
19 | //
20 |
21 | #ifndef NDKTEST_SELF_PATCHING_TRAPPOINT_H
22 | #define NDKTEST_SELF_PATCHING_TRAPPOINT_H
23 |
24 | #include "hooking_common.h"
25 | #include
26 |
27 | // This is primarily designed to be an internal interface to provide a more general
28 | // breakpoint interface. While accessible for outside use, there is no focus on ease-of-use.
29 | // If that is desired write your own bridge for the functionality you are looking for.
30 | // For reference look at breakpoint.[ch] and invocation_hook.[ch]
31 |
32 | // This is intentionally chosen to fit the signature of the abi_interface.h functions ;)
33 | typedef void* (*PATCH_ADDRESS_EXTRACTOR)(ucontext_t *ctx);
34 |
35 | void init_self_patching_trappoints();
36 | void destroy_self_patching_trappoints();
37 |
38 | void* SelfPatchingTrappoint_Install (void *address, uint32_t trap_method,
39 | HOOKCALLBACK PostTrigger_Handler, void* PostTrigger_Arg,
40 | HOOKCALLBACK PrePatch_Handler, void* PrePatch_Arg,
41 | PATCH_ADDRESS_EXTRACTOR patch_at);
42 | bool SelfPatchingTrappoint_Enable (void *p);
43 | bool SelfPatchingTrappoint_Disable (void *p);
44 | void SelfPatchingTrappoint_Uninstall (void *p);
45 |
46 |
47 |
48 |
49 |
50 | #endif //NDKTEST_SELF_PATCHING_TRAPPOINT_H
51 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/indirect_reference_table.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/18/2016.
19 | //
20 |
21 | #ifndef NDKTEST_ART_INDIRECT_REFERENCE_TABLE_H
22 | #define NDKTEST_ART_INDIRECT_REFERENCE_TABLE_H
23 |
24 | #include
25 |
26 | union IRTSegmentState
27 | {
28 | uint32_t all;
29 | struct
30 | {
31 | uint32_t topIndex:16; /* index of first unused entry */
32 | uint32_t numHoles:16; /* #of holes in entire table */
33 | } parts;
34 | };
35 | enum IndirectRefKind
36 | {
37 | kHandleScopeOrInvalid = 0, // <>
38 | kLocal = 1, // <>
39 | kGlobal = 2, // <>
40 | kWeakGlobal = 3 // <>
41 | };
42 |
43 | struct IndirectReferenceTable
44 | {
45 | /* semi-public - read/write by jni down calls */
46 | union IRTSegmentState segment_state_;
47 |
48 | // Mem map where we store the indirect refs.
49 | void* table_mem_map_; // Used to be std::unique_ptr
50 | // bottom of the stack. Do not directly access the object references
51 | // in this as they are roots. Use Get() that has a read barrier.
52 | void* table_; // used to be IrtEntry
53 | /* bit mask, ORed into all irefs */
54 | const enum IndirectRefKind kind_;
55 | /* max #of entries allowed */
56 | const size_t max_entries_;
57 | };
58 |
59 | #endif //NDKTEST_ART_INDIRECT_REFERENCE_TABLE_H
60 |
--------------------------------------------------------------------------------
/libHooking/src/armeabi/jni/instruction_illegal.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/12/2015.
19 | //
20 |
21 | #include "../../main/jni/instruction_illegal.h"
22 |
23 | uint32_t make_arm_illegal_instruction(uint8_t condition, uint16_t variation)
24 | {
25 | uint32_t cond = (uint32_t)((condition & 0xF) << 28);
26 |
27 | uint32_t variation_upper = (uint32_t)(variation & 0xFFF0);
28 | variation_upper <<= 4;
29 | variation_upper &= ARM_ILLEGAL_INSTR_VAR_UPPER;
30 |
31 | uint32_t variation_lower = (uint32_t)(variation & 0b1111);
32 |
33 | uint32_t opcode = ARM_ILLEGAL_INSTR_SCHEMATIC | cond | variation_upper | variation_lower;
34 |
35 | return opcode;
36 | }
37 |
38 | uint16_t extract_arm_illegal_instruction_variation(uint32_t opcode)
39 | {
40 | uint32_t var_upper = (opcode & ARM_ILLEGAL_INSTR_VAR_UPPER) >> 4;
41 | uint32_t var_lower = (opcode & ARM_ILLEGAL_INSTR_VAR_LOWER);
42 | uint16_t result = (uint16_t)((var_upper | var_lower) & 0xFFFF);
43 | return result;
44 | }
45 | uint8_t extract_arm_illegal_instruction_condition(uint32_t opcode)
46 | {
47 | return (uint8_t)(((opcode & ARM_ILLEGAL_INSTR_CONDITION) >> 28) & 0x0F);
48 | }
49 |
50 | uint16_t make_thumb_illegal_instruction(uint8_t variation)
51 | {
52 | uint16_t opcode = (uint16_t)THUMB_ILLEGAL_INSTRUCTION_SCHEMATIC | variation;
53 | return opcode;
54 | }
55 |
56 | uint8_t extract_thumb_illegal_instruction_variation(uint16_t opcode)
57 | {
58 | uint8_t result = (uint8_t)(opcode & 0xFF);
59 | return result;
60 | }
61 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_version_dependent/VERSION045/entrypoints/quick_entrypoints.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /*
18 | * Copyright (C) 2012 The Android Open Source Project
19 | *
20 | * Licensed under the Apache License, Version 2.0 (the "License");
21 | * you may not use this file except in compliance with the License.
22 | * You may obtain a copy of the License at
23 | *
24 | * http://www.apache.org/licenses/LICENSE-2.0
25 | *
26 | * Unless required by applicable law or agreed to in writing, software
27 | * distributed under the License is distributed on an "AS IS" BASIS,
28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 | * See the License for the specific language governing permissions and
30 | * limitations under the License.
31 | */
32 |
33 | #ifndef ART_RUNTIME_ENTRYPOINTS_QUICK_QUICK_ENTRYPOINTS_H_
34 | #define ART_RUNTIME_ENTRYPOINTS_QUICK_QUICK_ENTRYPOINTS_H_
35 |
36 | #include
37 |
38 | #include
39 | #include "../thread.h"
40 |
41 | #define QUICK_ENTRYPOINT_OFFSET(ptr_size, x) \
42 | Thread::QuickEntryPointOffset(OFFSETOF_MEMBER(QuickEntryPoints, x))
43 |
44 | // Pointers to functions that are called by quick compiler generated code via thread-local storage.
45 | struct PACKED(4) QuickEntryPoints {
46 | #define ENTRYPOINT_ENUM(name, rettype, ...) rettype ( * p ## name )( __VA_ARGS__ );
47 | #include "quick_entrypoints_list.h"
48 | QUICK_ENTRYPOINT_LIST(ENTRYPOINT_ENUM)
49 | #undef QUICK_ENTRYPOINT_LIST
50 | #undef ENTRYPOINT_ENUM
51 | };
52 |
53 | #endif // ART_RUNTIME_ENTRYPOINTS_QUICK_QUICK_ENTRYPOINTS_H_
54 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/bit_vector_util.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 9/25/2015.
19 | //
20 |
21 | #include
22 | #include "bit_vector_util.h"
23 | #include
24 | #include
25 |
26 | // Mostly adopted from AOSP source code.
27 | // platform/art/+/master/runtime/base/bit_vector.cc
28 |
29 | static const uint32_t kWordBytes = sizeof(uint32_t);
30 | static const uint32_t kWordBits = 8 * sizeof(uint32_t);
31 |
32 | static uint32_t WordIndex(uint32_t idx)
33 | {
34 | return idx >> 5;
35 | }
36 | // A bit mask to extract the bit for the given index.
37 | static uint32_t BitMask(uint32_t idx)
38 | {
39 | return (uint32_t)(1 << (idx & 0x1f));
40 | }
41 |
42 | #if defined(__clang__) && defined(__ARM_64BIT_STATE)
43 | // b/19180814 When POPCOUNT is inlined, boot up failed on arm64 devices.
44 | __attribute__((optnone))
45 | #endif
46 | uint32_t bit_vector_NumSetBits(const uint32_t* data, uint32_t end)
47 | {
48 | CHECK(data != NULL);
49 |
50 | uint32_t word_end = WordIndex(end);
51 | uint32_t partial_word_bits = end & 0x1f;
52 |
53 | uint32_t count = 0u;
54 | for(uint32_t word = 0u; word < word_end; word++)
55 | {
56 | count += COUNT_32BIT_SET_BITS(data[word]);
57 | }
58 | if(partial_word_bits != 0u)
59 | {
60 | count += COUNT_32BIT_SET_BITS(data[word_end] & ~(0xffffffffu << partial_word_bits));
61 | }
62 | return count;
63 |
64 | }
65 | bool bit_vector_IsBitSet(const uint32_t* data, uint32_t bit_index)
66 | {
67 | CHECK(data != NULL);
68 |
69 | return (data[WordIndex(bit_index)] & BitMask(bit_index)) != 0;
70 | }
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_version_dependent/VERSION045/entrypoints/interpreter_entrypoints.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /*
18 | * Copyright (C) 2012 The Android Open Source Project
19 | *
20 | * Licensed under the Apache License, Version 2.0 (the "License");
21 | * you may not use this file except in compliance with the License.
22 | * You may obtain a copy of the License at
23 | *
24 | * http://www.apache.org/licenses/LICENSE-2.0
25 | *
26 | * Unless required by applicable law or agreed to in writing, software
27 | * distributed under the License is distributed on an "AS IS" BASIS,
28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 | * See the License for the specific language governing permissions and
30 | * limitations under the License.
31 | */
32 |
33 | #ifndef ART_RUNTIME_ENTRYPOINTS_INTERPRETER_INTERPRETER_ENTRYPOINTS_H_
34 | #define ART_RUNTIME_ENTRYPOINTS_INTERPRETER_INTERPRETER_ENTRYPOINTS_H_
35 |
36 | #include "../thread.h"
37 | #include "../stack.h"
38 |
39 | #include
40 | #include "../../../dex_internal.h"
41 | #include "../../../jvalue.h"
42 |
43 | // Pointers to functions that are called by interpreter trampolines via thread-local storage.
44 | struct PACKED(4) InterpreterEntryPoints {
45 | void (*pInterpreterToInterpreterBridge)(struct Thread* self, void* mh,
46 | struct CodeItem* code_item,
47 | struct ShadowFrame* shadow_frame, union JValue* result);
48 | void (*pInterpreterToCompiledCodeBridge)(struct Thread* self, void* mh,
49 | struct CodeItem* code_item,
50 | struct ShadowFrame* shadow_frame, union JValue* result);
51 | };
52 |
53 | #endif // ART_RUNTIME_ENTRYPOINTS_INTERPRETER_INTERPRETER_ENTRYPOINTS_H_
54 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/lukas/ndktest/MemoryAnalysis/MappedMemoryRegion.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.example.lukas.ndktest;
18 |
19 | import java.util.Collections.*;
20 |
21 | /**
22 | * Created by Lukas on 4/19/2015.
23 | */
24 | public class MappedMemoryRegion
25 | {
26 | public static class FileMapping
27 | {
28 | public final long Offset;
29 | public final long MajorDeviceNumber;
30 | public final long MinorDeviceNumber;
31 | public final long INode;
32 | public FileMapping(long off, long major, long minor, long inode)
33 | {
34 | this.Offset = off;
35 | this.MajorDeviceNumber = major;
36 | this.MinorDeviceNumber = minor;
37 | this.INode = inode;
38 | }
39 | }
40 | private static enum MemoryStatus
41 | {
42 | SHARED,
43 | PRIVATE
44 | }
45 |
46 | public final long StartingAddress;
47 | public final long EndAddress;
48 | public final boolean IsReadable;
49 | public final boolean IsWritable;
50 | public final boolean IsExecutable;
51 | public final boolean IsShared;
52 |
53 | public final FileMapping FileMapInfo;
54 |
55 | public final String Description;
56 |
57 | public MappedMemoryRegion(long start, long end, boolean read, boolean write, boolean exec, boolean shared, long off, long majorDevNum, long minorDevNum, long inode, String desc)
58 | {
59 | this.StartingAddress = start;
60 | this.EndAddress = end;
61 | this.IsReadable = read;
62 | this.IsWritable = write;
63 | this.IsExecutable = exec;
64 | this.IsShared = shared;
65 | this.FileMapInfo = (inode == 0) ? null : new FileMapping(off, majorDevNum, minorDevNum, inode);
66 | this.Description = desc;
67 | }
68 |
69 | public boolean isMappedFromFile()
70 | {
71 | return this.FileMapInfo != null;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/app/src/main/jni/thread_lookup.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 |
18 | #include "thread_lookup.h"
19 | #include
20 | #include "art/oat_version_dependent/VERSION045/thread.h"
21 | #include
22 |
23 | static bool isCurrentThread(struct Thread* t)
24 | {
25 | if(t->tlsPtr_.pthread_self != pthread_self())
26 | {
27 | return false;
28 | }
29 | void* stackpointer = NULL;
30 | asm("mov %0, sp" : "=r" (stackpointer));
31 | if(t->tlsPtr_.stack_begin > stackpointer || stackpointer > t->tlsPtr_.stack_end)
32 | {
33 | return false;
34 | }
35 | if(syscall(SYS_gettid) != t->tls32_.tid)
36 | {
37 | return false;
38 | }
39 | return true;
40 | }
41 | static struct Thread* ScanMemoryForThreadObjectPointer(JNIEnv* env)
42 | {
43 | void** start = (void*)((uint32_t)env & ~0xFFF);
44 | void** end = (void*)start + 0x1000;
45 |
46 | struct Thread* dummy = NULL;
47 | uint32_t diff = (void*)&dummy->tlsPtr_.jni_env - (void*)dummy;
48 |
49 | for(void** p = start; p < end; p ++)
50 | {
51 | if(*p == env)
52 | {
53 | struct Thread* potential_thread = (void*)p - diff;
54 | LOGI("Found JNIEnv reference at: "PRINT_PTR, p);
55 | LOGI("Possible Thread* target at: "PRINT_PTR, (uintptr_t)potential_thread);
56 | if(!isCurrentThread(potential_thread))
57 | {
58 | LOGI("Found current Thread object!");
59 | return potential_thread;
60 | }
61 | LOGI("Turned out to not be the correct Thread object.");
62 | }
63 | }
64 | return NULL;
65 | }
66 | struct Thread* GetCurrentThreadObjectPointer(JNIEnv* env)
67 | {
68 | struct Thread* t;
69 | asm("mov %0, r9" : "=r" (t));
70 | if(t == NULL || t->tlsPtr_.jni_env != env)
71 | {
72 | // Didn't find it by cheating, let's try scanning for it.
73 | return ScanMemoryForThreadObjectPointer(env);;
74 | }
75 | return t;
76 | }
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/android_utf.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 11/14/2015.
19 | //
20 |
21 | #ifndef NDKTEST_ANDROID_UTF_H
22 | #define NDKTEST_ANDROID_UTF_H
23 |
24 | /*
25 | * Copyright (C) 2011 The Android Open Source Project
26 | *
27 | * Licensed under the Apache License, Version 2.0 (the "License");
28 | * you may not use this file except in compliance with the License.
29 | * You may obtain a copy of the License at
30 | *
31 | * http://www.apache.org/licenses/LICENSE-2.0
32 | *
33 | * Unless required by applicable law or agreed to in writing, software
34 | * distributed under the License is distributed on an "AS IS" BASIS,
35 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 | * See the License for the specific language governing permissions and
37 | * limitations under the License.
38 | */
39 |
40 | // Header file created to match android_utf.c
41 |
42 | #include
43 | #include
44 |
45 | uint16_t GetTrailingUtf16Char(uint32_t maybe_pair);
46 | uint16_t GetLeadingUtf16Char(uint32_t maybe_pair);
47 | uint32_t GetUtf16FromUtf8(const char **utf8_data_in);
48 | int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char *utf8_1,
49 | const char *utf8_2);
50 | size_t CountModifiedUtf8Chars(const char *utf8);
51 | void ConvertModifiedUtf8ToUtf16(uint16_t *utf16_data_out, const char *utf8_data_in);
52 | void ConvertUtf16ToModifiedUtf8(char *utf8_out, const uint16_t *utf16_in, size_t char_count);
53 | int32_t ComputeUtf16Hash(const uint16_t *chars, size_t char_count);
54 | size_t ComputeModifiedUtf8Hash(const char *chars);
55 | int CompareModifiedUtf8ToUtf16AsCodePointValues(const char *utf8, const uint16_t *utf16,
56 | size_t utf16_length);
57 | size_t CountUtf8Bytes(const uint16_t *chars, size_t char_count);
58 | #endif //NDKTEST_ANDROID_UTF_H
59 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/concurrent_persistent_list.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef NDKTEST_CONCURRENT_PERSISTENT_LIST_H
18 | #define NDKTEST_CONCURRENT_PERSISTENT_LIST_H
19 |
20 | #include
21 |
22 | /*
23 | * Implementation of a circular inked list that is concurrently accessible for the use in
24 | * trappoints, breakpoints, etc. This list relies on assumptions intrinsic to these, therefore
25 | * caution is required when attempting to use this implementation in a different context.
26 | *
27 | * The important requirements for this list implementation are:
28 | *
29 | * 1. Elements can NEVER be removed. The list can only be destroyed as a whole.
30 | * 2. Manipulation without using the provided functions must be synchronized independently
31 | * 3. External manipulation with correct values should not break this lists internal
32 | * synchronisation
33 | * 4. This list implements non-blocking synchronisation
34 | * 5. The last element has the next pointer as well as the element pointer set to null
35 | * 6. No empty lists exist. Empty lists must instead be represented by NULL pointers.
36 | */
37 |
38 | typedef struct concurrent_persistent_list_entry
39 | {
40 | struct concurrent_persistent_list_entry* next;
41 | void* element;
42 | } concurrent_persistent_list_entry;
43 |
44 | #define LIST(x) concurrent_persistent_list_##x
45 |
46 | typedef void (*CONCURRENT_PERSISTENT_LIST_ITERATION_CALLBACK)(int index,
47 | LIST(entry)* entry,
48 | void* elem, void* args);
49 |
50 |
51 | bool LIST(try_create)(LIST(entry)** list_ptr, void* elem);
52 | struct LIST(entry)* LIST(insert_after)(LIST(entry)* entry_after, void* new_element);
53 |
54 | void LIST(iterate)(LIST(entry) *list,
55 | CONCURRENT_PERSISTENT_LIST_ITERATION_CALLBACK callback,
56 | void* args);
57 |
58 | #undef LIST
59 |
60 | #endif //NDKTEST_CONCURRENT_PERSISTENT_LIST_H
61 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_dump.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 11/18/2015.
19 | //
20 |
21 | #ifndef NDKTEST_OAT_DUMP_H
22 | #define NDKTEST_OAT_DUMP_H
23 |
24 |
25 | #include
26 | #include "oat_internal.h"
27 |
28 | void log_elf_oat_file_info(void *oat_begin, void *oat_end);
29 |
30 | void log_oat_header_info(const struct OatHeader *hdr);
31 |
32 | void log_oat_key_value_storage_contents(const struct OatHeader *hdr);
33 |
34 | void log_oat_dex_file_storage_contents(const struct OatHeader *hdr);
35 |
36 | void log_oat_dex_file_method_offsets_content(const struct OatHeader *oat_header,
37 | const struct OatClassData *oat_class,
38 | uint32_t method_index);
39 |
40 | void log_oat_dex_file_class_def_contents(const uint8_t *oat_class_pointer);
41 |
42 | void log_oat_dex_file_class_defs_contents(const struct OatHeader *oat_header,
43 | const struct DexHeader *hdr,
44 | const uint32_t *class_def_offsets_pointer);
45 |
46 |
47 | void log_dex_file_header_contents(const struct DexHeader *hdr);
48 |
49 | void log_dex_file_string_id_contents(const struct DexHeader *hdr, uint32_t string_id_index);
50 |
51 | void log_dex_file_type_id_contents(const struct DexHeader *hdr, uint32_t type_id_index);
52 |
53 | void log_dex_file_class_def_contents(const struct DexHeader *hdr, uint16_t class_def_index);
54 |
55 | void log_dex_file_class_defs_contents(const struct DexHeader *hdr);
56 |
57 | void log_dex_file_proto_id_contents(const struct DexHeader *hdr, uint32_t proto_id);
58 |
59 | void log_dex_file_method_id_contents(const struct DexHeader *hdr, uint32_t method_index);
60 |
61 | void log_dex_file_method_id_array_contents(const struct DexHeader *hdr);
62 |
63 | void log_dex_file_method_id_array_contents_by_class_def_index(const struct DexHeader *hdr,
64 | uint16_t class_def_index);
65 |
66 | void log_dex_file_class_data_contents(const struct DexHeader *hdr, uint16_t class_def_index);
67 |
68 | #endif //NDKTEST_OAT_DUMP_H
69 |
--------------------------------------------------------------------------------
/libHooking/src/main/jni/instruction_illegal.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/12/2015.
19 | //
20 |
21 | #ifndef NDKTEST_ILLEGAL_INSTRUCTION_H
22 | #define NDKTEST_ILLEGAL_INSTRUCTION_H
23 |
24 |
25 | #include
26 |
27 |
28 | enum CONDITION
29 | {
30 | ZERO_SET = 0b0000, // Z set
31 | ZERO_CLEAR = 0b0001, // Z clear
32 | CARRY_SET = 0b0010, // C set
33 | CARRY_CLEAR = 0b0011, // C clear
34 | NEGATIVE = 0b0100, // N set
35 | NEGATIVE_CLEAR = 0b0101, // N unset
36 | OVERFLOW = 0b0110, // O set
37 | OVERFLOW_CLEAR = 0b0111, // O clear
38 | UNSIGNED_HIGHER = 0b1000, // C set and Z clear
39 | UNSIGNED_LOWER_OR_SAME = 0b1001, // C clear or Z set
40 | SIGNED_GREATER_OR_EQUAL = 0b1010, // N == V
41 | SIGNED_LESS_THAN = 0b1011, // N xor V
42 | SIGNED_GREATER_THAN = 0b1100, // N == V && Z == 0
43 | SIGNED_LESS_OR_EQUAL = 0b1101, // N xor V || Z == 1
44 | UNCONDITIONAL = 0b1110, // Always execute
45 | UNDEFINED_OR_UNCOND_EXT = 0b1111, // see ARM reference
46 | };
47 |
48 | static int const ARM_ILLEGAL_INSTR_SCHEMATIC = 0b00000111111100000000000011110000;
49 | static int const ARM_ILLEGAL_INSTR_CONDITION = (0b1111 << 28);
50 | static int const ARM_ILLEGAL_INSTR_VAR_UPPER = 0b00000000000011111111111100000000;
51 | static int const ARM_ILLEGAL_INSTR_VAR_LOWER = 0b00000000000000000000000000001111;
52 |
53 | static int const THUMB_ILLEGAL_INSTRUCTION_SCHEMATIC = 0b1101111000000000;
54 | static int const THUMB_ILLEGAL_INSTRUCTION_VARIATION = 0b0000000011111111;
55 |
56 |
57 |
58 | uint32_t make_arm_illegal_instruction(uint8_t condition, uint16_t variation);
59 | uint16_t extract_arm_illegal_instruction_variation(uint32_t opcode);
60 | uint8_t extract_arm_illegal_instruction_condition(uint32_t opcode);
61 |
62 | uint16_t make_thumb_illegal_instruction(uint8_t variation);
63 | uint8_t extract_thumb_illegal_instruction_variation(uint16_t opcode);
64 |
65 | #endif //NDKTEST_ILLEGAL_INSTRUCTION_H
66 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat_version_dependent/VERSION045/entrypoints/quick_entrypoints_enum.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /*
18 | * Copyright (C) 2014 The Android Open Source Project
19 | *
20 | * Licensed under the Apache License, Version 2.0 (the "License");
21 | * you may not use this file except in compliance with the License.
22 | * You may obtain a copy of the License at
23 | *
24 | * http://www.apache.org/licenses/LICENSE-2.0
25 | *
26 | * Unless required by applicable law or agreed to in writing, software
27 | * distributed under the License is distributed on an "AS IS" BASIS,
28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 | * See the License for the specific language governing permissions and
30 | * limitations under the License.
31 | */
32 |
33 | #ifndef ART_RUNTIME_ENTRYPOINTS_QUICK_QUICK_ENTRYPOINTS_ENUM_H_
34 | #define ART_RUNTIME_ENTRYPOINTS_QUICK_QUICK_ENTRYPOINTS_ENUM_H_
35 |
36 | #include "quick_entrypoints.h"
37 | #include "../thread.h"
38 |
39 | namespace art {
40 |
41 | // Define an enum for the entrypoints. Names are prepended a 'kQuick'.
42 | enum QuickEntrypointEnum
43 | { // NOLINT(whitespace/braces)
44 | #define ENTRYPOINT_ENUM(name, rettype, ...) kQuick ## name,
45 | #include "quick_entrypoints_list.h"
46 | QUICK_ENTRYPOINT_LIST(ENTRYPOINT_ENUM)
47 | #undef QUICK_ENTRYPOINT_LIST
48 | #undef ENTRYPOINT_ENUM
49 | };
50 |
51 | std::ostream& operator<<(std::ostream& os, const QuickEntrypointEnum& kind);
52 |
53 | // Translate a QuickEntrypointEnum value to the corresponding ThreadOffset.
54 | template
55 | static ThreadOffset GetThreadOffset(QuickEntrypointEnum trampoline) {
56 | switch (trampoline)
57 | { // NOLINT(whitespace/braces)
58 | #define ENTRYPOINT_ENUM(name, rettype, ...) case kQuick ## name : \
59 | return QUICK_ENTRYPOINT_OFFSET(pointer_size, p ## name);
60 | #include "quick_entrypoints_list.h"
61 | QUICK_ENTRYPOINT_LIST(ENTRYPOINT_ENUM)
62 | #undef QUICK_ENTRYPOINT_LIST
63 | #undef ENTRYPOINT_ENUM
64 | };
65 | LOG(FATAL) << "Unexpected trampoline " << static_cast(trampoline);
66 | return ThreadOffset(-1);
67 | }
68 |
69 | } // namespace art
70 |
71 |
72 | #endif // ART_RUNTIME_ENTRYPOINTS_QUICK_QUICK_ENTRYPOINTS_ENUM_H_
73 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/dex.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/20/2015.
19 | //
20 |
21 | #ifndef NDKTEST_DEX_H
22 | #define NDKTEST_DEX_H
23 |
24 | #include "dex_internal.h"
25 |
26 | /*
27 | * Dex Type index sizes:
28 | *
29 | * StringId - uint32_t
30 | * MethodId - uint32_t
31 | * FieldId - uint32_t
32 | *
33 | * TypeId - uint16_t
34 | * ProtoId - uint16_t
35 | * ClassDef - uint16_t
36 | *
37 | */
38 |
39 | struct DexClass
40 | {
41 | const struct DexHeader * dex_header;
42 | const struct ClassDef * class_def;
43 | struct DecodedClassData decoded_class_data;
44 | };
45 |
46 | struct DexMethod
47 | {
48 | const struct DexHeader * dex_header;
49 | const struct DexClass * containing_class;
50 | const struct MethodID * method_id;
51 |
52 | bool is_direct;
53 | uint32_t class_method_idx;
54 | struct DecodedMethod decoded_method_data;
55 | };
56 |
57 | struct DexField
58 | {
59 | const struct DexHeader * dex_header;
60 | const struct DexClass * containing_class;
61 | const struct FieldID * field_id;
62 | uint32_t class_field_idx;
63 | struct DecodedField decoded_field_data;
64 | };
65 |
66 | uint32_t dex_NumberOfStrings ( const struct DexHeader* hdr );
67 | uint32_t dex_NumberOfMethods ( const struct DexHeader* hdr );
68 | uint32_t dex_NumberOfFields ( const struct DexHeader* hdr );
69 | uint32_t dex_NumberOfClassDefs ( const struct DexHeader* hdr );
70 |
71 | bool dex_FindClass(const struct DexHeader *hdr,
72 | struct DexClass *result,
73 | const char *mutf8_descriptor);
74 | bool dex_GetClass(const struct DexHeader* hdr,
75 | struct DexClass* result,
76 | uint16_t index);
77 |
78 |
79 | bool dex_FindVirtualMethod(const struct DexClass *clazz,
80 | struct DexMethod *result,
81 | const char *mutf8_descriptor,
82 | const char *mutf8_Signature);
83 |
84 | bool dex_FindDirectMethod(const struct DexClass *clazz,
85 | struct DexMethod *result,
86 | const char *mutf8_descriptor,
87 | const char *mutf8_signature);
88 |
89 | #endif //NDKTEST_DEX_FILE_H
--------------------------------------------------------------------------------
/libHooking/src/main/jni/function_hook.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/31/2016.
19 | //
20 |
21 | #include
22 |
23 | #ifdef __cplusplus
24 | extern "C" {
25 | #endif
26 |
27 | #include "function_hook.h"
28 | #include "trappoint_interface.h"
29 |
30 | /*
31 | struct FunctionHook
32 | {
33 | uint32_t trap_method;
34 | void* trappoint_entry;
35 | void* trappoint_exit;
36 | struct Handler* handler;
37 | };
38 |
39 | void exit_handler(void* addr, ucontext_t* ctx, void* arg);
40 | void entry_handler(void *addr, ucontext_t *ctx, void *arg);
41 |
42 | void entry_handler(void *addr, ucontext_t *ctx, void *arg)
43 | {
44 | struct FunctionHook* self = (struct FunctionHook*)arg;
45 | trappoint_Disable((TrapPointInfo *) self->trappoint_entry);
46 |
47 | if(self->handler->OnEntryHandler != NULL)
48 | {
49 | // Call the handler for the function entry point.
50 | self->handler->OnEntryHandler(addr, ctx, self->handler->entry_args);
51 | }
52 |
53 | /* Recursive calls of functions can't be monitored by this mechanism. This is because with this
54 | * method the trappoint on the function entry is only reset once the function returned to its
55 | * caller. In the case of a recursive function call this function the OnEntry handler is invoked
56 | * the first time the function is called and the OnExit handler only when the highest instance
57 | * of the function returns. So it does match a call with its corresponding exit but only once.
58 | * Sadly i didn't find a way to do this reliably.
59 | *
60 | * Because of this structure this deallocation should be fine.
61 | *//*
62 | if(self->trappoint_exit != NULL)
63 | {
64 | trappoint_Uninstall(self->trappoint_exit); // Uninstall and free previous exit trappoint
65 | }
66 |
67 | void* ret_addr = ExtractReturnAddress(ctx);
68 | struct TrapPointInfo* trap = trappoint_Install(ret_addr, self->trap_method, exit_handler,
69 | (void *) self);
70 | if(trap == NULL)
71 | {
72 | LOGF("Could not install return address trappoint. THIS IS BAD!.");
73 | LOGF("Last error: %s", get_last_error());
74 | }
75 | self->trappoint_exit = trap;
76 | }*//*
77 | void*FunctionHook_Install(void *entry_point, struct Handler *handler, uint32_t trapping_method)
78 | {
79 |
80 | }*/
81 |
82 | #ifdef __cplusplus
83 | }
84 | #endif
85 |
86 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/runtime_stats.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/18/2016.
19 | //
20 |
21 | #ifndef NDKTEST_ART_RUNTIME_STATS_H
22 | #define NDKTEST_ART_RUNTIME_STATS_H
23 |
24 | #include
25 | #include
26 |
27 | // These must match the values in dalvik.system.VMDebug.
28 | enum StatKinds {
29 | KIND_ALLOCATED_OBJECTS = 1<<0,
30 | KIND_ALLOCATED_BYTES = 1<<1,
31 | KIND_FREED_OBJECTS = 1<<2,
32 | KIND_FREED_BYTES = 1<<3,
33 | KIND_GC_INVOCATIONS = 1<<4,
34 | KIND_CLASS_INIT_COUNT = 1<<5,
35 | KIND_CLASS_INIT_TIME = 1<<6,
36 |
37 | // These values exist for backward compatibility.
38 | KIND_EXT_ALLOCATED_OBJECTS = 1<<12,
39 | KIND_EXT_ALLOCATED_BYTES = 1<<13,
40 | KIND_EXT_FREED_OBJECTS = 1<<14,
41 | KIND_EXT_FREED_BYTES = 1<<15,
42 |
43 | KIND_GLOBAL_ALLOCATED_OBJECTS = KIND_ALLOCATED_OBJECTS,
44 | KIND_GLOBAL_ALLOCATED_BYTES = KIND_ALLOCATED_BYTES,
45 | KIND_GLOBAL_FREED_OBJECTS = KIND_FREED_OBJECTS,
46 | KIND_GLOBAL_FREED_BYTES = KIND_FREED_BYTES,
47 | KIND_GLOBAL_GC_INVOCATIONS = KIND_GC_INVOCATIONS,
48 | KIND_GLOBAL_CLASS_INIT_COUNT = KIND_CLASS_INIT_COUNT,
49 | KIND_GLOBAL_CLASS_INIT_TIME = KIND_CLASS_INIT_TIME,
50 |
51 | KIND_THREAD_ALLOCATED_OBJECTS = KIND_ALLOCATED_OBJECTS << 16,
52 | KIND_THREAD_ALLOCATED_BYTES = KIND_ALLOCATED_BYTES << 16,
53 | KIND_THREAD_FREED_OBJECTS = KIND_FREED_OBJECTS << 16,
54 | KIND_THREAD_FREED_BYTES = KIND_FREED_BYTES << 16,
55 |
56 | KIND_THREAD_GC_INVOCATIONS = KIND_GC_INVOCATIONS << 16,
57 |
58 | // TODO: failedAllocCount, failedAllocSize
59 | };
60 |
61 | /*
62 | * Memory allocation profiler state. This is used both globally and
63 | * per-thread.
64 | */
65 | struct PACKED(4) RuntimeStats {
66 |
67 | // Number of objects allocated.
68 | uint64_t allocated_objects;
69 | // Cumulative size of all objects allocated.
70 | uint64_t allocated_bytes;
71 |
72 | // Number of objects freed.
73 | uint64_t freed_objects;
74 | // Cumulative size of all freed objects.
75 | uint64_t freed_bytes;
76 |
77 | // Number of times an allocation triggered a GC.
78 | uint64_t gc_for_alloc_count;
79 |
80 | // Number of initialized classes.
81 | uint64_t class_init_count;
82 | // Cumulative time spent in class initialization.
83 | uint64_t class_init_time_ns;
84 | };
85 |
86 | #endif //NDKTEST_ART_RUNTIME_STATS_H
87 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/oat.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/16/2015.
19 | //
20 |
21 | #ifndef NDKTEST_OAT_H
22 | #define NDKTEST_OAT_H
23 |
24 | #include
25 | #include "oat_internal.h"
26 | #include "dex.h"
27 |
28 | struct OatFile
29 | {
30 | void* begin;
31 | void* end;
32 |
33 | struct OatHeader* header;
34 | void* key_value_storage_start;
35 | void* dex_file_storage_start;
36 | };
37 |
38 | struct OatDexFile
39 | {
40 | const struct OatFile* oat_file;
41 |
42 | uint32_t index;
43 | struct OatDexFileData data;
44 | };
45 |
46 | struct OatClass
47 | {
48 | const struct OatDexFile* oat_dex_file;
49 | struct DexClass dex_class;
50 | struct OatClassData oat_class_data;
51 | };
52 | struct OatMethod
53 | {
54 | const struct OatClass* oat_class;
55 |
56 | struct DexMethod dex_method;
57 | const struct OatMethodOffsets* oat_method_offsets;
58 | };
59 |
60 | void* oat_PointerFromFileOffset(const struct OatFile* oat_file, uint32_t offset);
61 |
62 | bool oat_Setup(struct OatFile* result, void *mem_begin, void *mem_end);
63 |
64 | bool oat_FindDexFile(struct OatFile* oat_file, struct OatDexFile* result, const char* location);
65 | bool oat_GetOatDexFile(struct OatFile* oat_file, struct OatDexFile* result, uint32_t index);
66 |
67 | bool oat_FindClass(struct OatFile* oat, struct OatDexFile* result_oat_dex_file,
68 | struct OatClass *result_clazz, char *descriptor);
69 | bool oat_FindClassInDex(const struct OatDexFile *oat_dex_file, struct OatClass *clazz,
70 | const char *descriptor);
71 | bool oat_GetClass(const struct OatDexFile* oat_dex_file, struct OatClass* clazz, uint16_t class_def_index);
72 |
73 | bool oat_FindDirectMethod(const struct OatClass * oat_class, struct OatMethod* result, const char* descriptor, const char* signature);
74 | bool oat_FindVirtualMethod(const struct OatClass * oat_class, struct OatMethod* result, const char* descriptor, const char* signature);
75 | bool oat_FindMethod(const struct OatClass* oat_class, struct OatMethod* result, const char* descriptor, const char* signature);
76 |
77 | bool oat_HasQuickCompiledCode(const struct OatMethod* m);
78 | void* oat_GetQuickCompiledEntryPoint(const struct OatMethod* m);
79 | void* oat_GetQuickCompiledMemoryPointer(const struct OatMethod* m);
80 |
81 | #endif //NDKTEST_OAT_H
82 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/lukas/ndktest/MemoryAnalysis/MemoryRegionParser.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.example.lukas.ndktest.MemoryAnalysis;
18 |
19 | import com.example.lukas.ndktest.MappedMemoryRegion;
20 |
21 | import java.util.regex.Matcher;
22 | import java.util.regex.Pattern;
23 |
24 | /**
25 | * Created by Lukas on 4/19/2015.
26 | */
27 | public class MemoryRegionParser
28 | {
29 | /**
30 | * Regular Expression for /proc/self/maps line is
31 | *
32 | * ([0-9a-f]+) - ([0-9a-f]+) \s ([r-]) ([w-]) ([x-]) ([sp]) \s ([0-9a-f]+) \s ([0-9a-f]+) : ([0-9a-f]+) \s (\d+) \s? (.*)
33 | * StartAddress EndAddress Read Write Execute Shared Filemap Offset Major devnum Minor devnum Inode Description
34 | */
35 | public static final String pattern = "([0-9a-f]+)-([0-9a-f]+)\\s([r-])([w-])([x-])([sp])\\s([0-9a-f]+)\\s([0-9a-f]+):([0-9a-f]+)\\s(\\d+)\\s?(.*)";
36 | public final static Pattern MAPS_LINE_PATTERN = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
37 |
38 | private static long parseHex(String s)
39 | {
40 | return Long.parseLong(s, 16);
41 | }
42 |
43 | public static MappedMemoryRegion getMemoryRegionFromMapsFileLine(String line)
44 | {
45 | line = line.trim();
46 | Matcher m = MAPS_LINE_PATTERN.matcher(line);
47 | if(!m.matches())
48 | {
49 | throw new IllegalArgumentException(String.format("The provided line does not match the pattern for /proc/$pid/maps lines. Given: %s", line));
50 | }
51 |
52 | if(m.groupCount() != 11) // group(0) not included in this.
53 | {
54 | throw new InternalError(String.format("Invalid group count: Found %d, but expected %d", m.groupCount(), 12));
55 | }
56 |
57 | long start = parseHex(m.group(1));
58 | long end = parseHex(m.group(2));
59 | boolean read = m.group(3) == "r";
60 | boolean write = m.group(4) == "w";
61 | boolean exec = m.group(5) == "x";
62 | boolean shared = m.group(6) == "s";
63 | long fileOffset = parseHex(m.group(7));
64 | long majorDevNum = parseHex(m.group(8));
65 | long minorDevNum = parseHex(m.group(9));
66 | long inode = parseHex(m.group(10));
67 | String desc = m.group(11);
68 |
69 | return new MappedMemoryRegion(start, end, read, write, exec, shared, fileOffset, majorDevNum, minorDevNum, inode, desc);
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/concurrent_persistent_list.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include
18 | #include "concurrent_persistent_list.h"
19 | #include "logging.h"
20 | #include "memory.h"
21 |
22 |
23 | static concurrent_persistent_list_entry* new_entry()
24 | {
25 | void* mem = allocate_memory_chunk(sizeof(concurrent_persistent_list_entry));
26 | assert(mem != NULL); // if this fails we have a serious problem anyway, so quit
27 | return (concurrent_persistent_list_entry*)mem;
28 | }
29 |
30 | bool concurrent_persistent_list_try_create(concurrent_persistent_list_entry** list_ptr, void* elem)
31 | {
32 | CHECK(list_ptr != NULL);
33 | if(*list_ptr != NULL)
34 | {
35 | return false;
36 | }
37 |
38 | concurrent_persistent_list_entry *new_start = new_entry();
39 | new_start->element = elem;
40 | new_start->next = new_start;
41 |
42 | if(!__sync_bool_compare_and_swap(list_ptr, NULL, new_start))
43 | {
44 | free_memory_chunk(new_start);
45 | return false;
46 | }
47 | return true;
48 | }
49 |
50 | concurrent_persistent_list_entry* concurrent_persistent_list_insert_after(
51 | concurrent_persistent_list_entry* entry_after,
52 | void* new_element)
53 | {
54 | CHECK(entry_after != NULL);
55 |
56 | concurrent_persistent_list_entry* insert_after = entry_after;
57 | concurrent_persistent_list_entry* new_ent = new_entry();
58 | new_ent->element = new_element;
59 |
60 | do // wait until non-blocking sync worked out correctly
61 | {
62 | new_ent->next = insert_after->next;
63 | }
64 | while(!__sync_bool_compare_and_swap(&insert_after->next, new_ent->next, new_ent));
65 |
66 | return new_ent;
67 | }
68 |
69 | /*
70 | * This iteration is possible because we made the assumption that entries can never be deleted
71 | * (see header file). Otherwise we would have to somehow lock the list to avoid getting stuck in
72 | * removed entries.
73 | */
74 |
75 | void concurrent_persistent_list_iterate(concurrent_persistent_list_entry *list,
76 | CONCURRENT_PERSISTENT_LIST_ITERATION_CALLBACK callback,
77 | void *args)
78 | {
79 | CHECK(list != NULL);
80 | CHECK(callback != NULL);
81 |
82 | int index = 0;
83 | concurrent_persistent_list_entry* current_entry = list;
84 | while(true)
85 | {
86 | callback(index++, current_entry, current_entry->element, args);
87 |
88 | current_entry = current_entry->next;
89 | if(current_entry == list)
90 | {
91 | break;
92 | }
93 | }
94 | }
--------------------------------------------------------------------------------
/libHostSystem/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.model.native'
2 |
3 | def distrib_include_dir = file("../distribution/include").absolutePath
4 | def distrib_lib_dir = file("../distribution/lib").absolutePath
5 | model {
6 | android {
7 | compileSdkVersion = 24
8 | buildToolsVersion = '24.0.1'
9 |
10 | defaultConfig {
11 | minSdkVersion.apiLevel = 21
12 | targetSdkVersion.apiLevel = 24
13 | versionCode = 1
14 | versionName = '1.0'
15 | }
16 | ndk {
17 | moduleName = 'HostSystem'
18 |
19 | toolchain = 'clang'
20 | CFlags.addAll(['--std=gnu99'])
21 | ldLibs.addAll(['android', 'log'])
22 |
23 | // Workaround, for some reason I can't figure out a way to make the library dependencies
24 | // apply to the architecture specific source sets
25 | CFlags.addAll(['-I', distrib_include_dir])
26 | }
27 | sources {
28 | main {
29 | jni {
30 | source {
31 | srcDir 'src/main/jni'
32 | }
33 | }
34 | }
35 | armeabi {
36 | jni {
37 | source {
38 | srcDir 'src/armeabi/jni'
39 | }
40 | }
41 | }
42 | armeabi_v7a {
43 | jni {
44 | source {
45 | srcDir 'src/armeabi/jni'
46 | }
47 | }
48 | }
49 | }
50 | buildTypes {
51 | debug {
52 | ndk.with {
53 | debuggable = true
54 | }
55 | }
56 | release {
57 | minifyEnabled = false
58 | proguardFiles.add(file('proguard-rules.pro'))
59 | }
60 | }
61 | productFlavors {
62 | create("armeabi") {
63 | ndk.with {
64 | abiFilters.add("armeabi")
65 | ldFlags.add('-L' + file("${distrib_lib_dir}/utility/armeabi/").absolutePath)
66 | ldFlags.add('-lUtility')
67 | }
68 | }
69 | create("armeabi_v7a") {
70 | ndk.with {
71 | abiFilters.add("armeabi-v7a")
72 | ldFlags.add('-L' + file("${distrib_lib_dir}/utility/armeabi-v7a/").absolutePath)
73 | ldFlags.add('-lUtility')
74 | }
75 | }
76 | }
77 | }
78 | }
79 |
80 | // This is just copy out the header file and built lib into distribution
81 | // directory for clint application to use; it is a small overhead of this sample:
82 | // both lib and app are put inside one project space [save maintenance time]
83 | task distributeLib (type : Copy) {
84 | // trigger build library
85 | dependsOn assemble
86 |
87 | into '../distribution/'
88 | from('src/main/jni') {
89 | include '**/*.h'
90 | into 'include/hostsystem/'
91 | }
92 |
93 | from ('build/outputs/native/release/armeabi/lib/') {
94 | into 'lib/hostsystem'
95 | }
96 | from ('build/outputs/native/release/armeabi_v7a/lib/') {
97 | into 'lib/hostsystem'
98 | }
99 | }
100 | distributeLib.shouldRunAfter ':libUtility:distributeLib'
--------------------------------------------------------------------------------
/libAndroidRuntime/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | apply plugin: 'com.android.model.native'
18 |
19 | model {
20 | android {
21 | compileSdkVersion = 24
22 | buildToolsVersion = '24.0.1'
23 |
24 | defaultConfig {
25 | minSdkVersion.apiLevel = 21
26 | targetSdkVersion.apiLevel = 24
27 | versionCode = 1
28 | versionName = '1.0'
29 | }
30 | ndk {
31 | moduleName = 'AndroidRuntime'
32 | CFlags.addAll(['--std=gnu99'])
33 | abiFilters.addAll(['armeabi', 'armeabi-v7a'])
34 | ldLibs.addAll(['android', 'log'])
35 | }
36 | sources {
37 | main {
38 | jni {
39 | dependencies {
40 | // if it were *.a, change shared --> static
41 | library 'Utility' linkage 'shared'
42 | library 'HostSystem' linkage 'shared'
43 | }
44 | }
45 | }
46 | }
47 | buildTypes {
48 | debug {
49 | ndk.with {
50 | debuggable = true
51 | }
52 | }
53 | release {
54 | minifyEnabled = false
55 | proguardFiles.add(file('proguard-rules.pro'))
56 | }
57 | }
58 | }
59 | repositories{
60 | libs(PrebuiltLibraries) {
61 | Utility {
62 | // Inform Android Studio where the header file dir for this lib is
63 | headers.srcDir "../distribution/include/"
64 |
65 | binaries.withType(SharedLibraryBinary) {
66 | sharedLibraryFile = file("../distribution/lib/utility/${targetPlatform.getName()}/libUtility.so")
67 | }
68 | }
69 | HostSystem {
70 | // Inform Android Studio where the header file dir for this lib is
71 | headers.srcDir "../distribution/include/"
72 |
73 | binaries.withType(SharedLibraryBinary) {
74 | sharedLibraryFile = file("../distribution/lib/hostsystem/${targetPlatform.getName()}/libHostSystem.so")
75 | }
76 | }
77 | }
78 | }
79 | }
80 |
81 | // This is just copy out the header file and built lib into distribution
82 | // directory for clint application to use; it is a small overhead of this sample:
83 | // both lib and app are put inside one project space [save maintenance time]
84 | task distributeLib(type : Copy) {
85 | // trigger build library
86 | dependsOn assemble
87 | into '../distribution/'
88 | // TODO fix this
89 | from('src/main/jni/') {
90 | include '**/*.h'
91 | into 'include/art/'
92 | }
93 | from('build/outputs/native/release/lib') {
94 | into 'lib/art'
95 | }
96 | }
97 | distributeLib.shouldRunAfter ':libUtility:distributeLib', ':libHostSystem:distributeLib'
--------------------------------------------------------------------------------
/libUtility/src/main/jni/logging.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include
18 | #include "logging.h"
19 | #include "memory.h"
20 | #include "exceptions.h"
21 | #include "util.h"
22 |
23 | static bool hexdump_callback(void *addr, uint64_t currentOffset, int numBytes, void *additionalInfo)
24 | {
25 | unsigned char dump[3 * numBytes + 1];
26 | unsigned char ascii[numBytes + 1];
27 | unsigned char *target = (unsigned char *) (addr + currentOffset);
28 | for (int byteInLine = 0; byteInLine < numBytes; byteInLine++)
29 | {
30 | unsigned char c = target[byteInLine];
31 | sprintf(&dump[byteInLine * 3], "%02x ", c);
32 | if (!isprint(c))
33 | {
34 | ascii[byteInLine] = '.';
35 | }
36 | else if (c == '\r' || c == '\n' || c == '\t')
37 | {
38 | ascii[byteInLine] = '.';
39 | }
40 | else
41 | {
42 | ascii[byteInLine] = c;
43 | }
44 | }
45 | ascii[numBytes] = 0;
46 | LOGD("Hexdump: "
47 | PRINT_PTR
48 | " => %s | %s", (uintptr_t) target, dump, ascii);
49 | return true;
50 | }
51 |
52 | bool hexdump(JNIEnv *env, const void *addr, jlong numBytes, jlong bytesPerLine)
53 | {
54 | return iterate_byte_array_chunks(env, addr, numBytes, bytesPerLine,
55 | (OUTPUT_CALLBACK) hexdump_callback, NULL);
56 | }
57 |
58 | bool hexdump_primitive(const void *addr, jlong numBytes, jlong bytesPerLine)
59 | {
60 | return iterate_byte_array_chunks_primitive(addr, numBytes, bytesPerLine,
61 | (OUTPUT_CALLBACK) hexdump_callback, NULL);
62 | }
63 |
64 | bool hexdump_aligned(JNIEnv *env, const void *addr, jlong numBytes, jlong bytesPerLine,
65 | jlong alignment)
66 | {
67 | if (hasExceptionOccurred(env))
68 | {
69 | return false;
70 | }
71 | if (alignment <= 0)
72 | {
73 | throwNewJNIException(env, "java/lang/RuntimeException", "Hexdump: Error alignment == 0");
74 | return false;
75 | }
76 | const unsigned char *aligned = (unsigned char *) align_address_to_size(addr, alignment);
77 | uint64_t size = numBytes + ((unsigned char *) addr - aligned);
78 | return iterate_byte_array_chunks(env, aligned, size, bytesPerLine,
79 | (OUTPUT_CALLBACK) hexdump_callback, NULL);
80 | }
81 |
82 | bool hexdump_aligned_primitive(const void *addr, jlong numBytes, jlong bytesPerLine,
83 | jlong alignment)
84 | {
85 | if (alignment <= 0)
86 | {
87 | return false;
88 | }
89 | const unsigned char *aligned = (unsigned char *) align_address_to_size(addr, alignment);
90 | uint64_t size = numBytes + ((unsigned char *) addr - aligned);
91 | return iterate_byte_array_chunks_primitive(aligned, size, bytesPerLine,
92 | (OUTPUT_CALLBACK) hexdump_callback, NULL);
93 | }
--------------------------------------------------------------------------------
/libHostSystem/src/armeabi/jni/signal_handling_helper.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/14/2015.
19 | //
20 |
21 | #include "../../main/jni/signal_handling_helper.h"
22 |
23 | #include "../../main/jni/cpsr_util.h"
24 |
25 | void log_siginfo_content(siginfo_t* info)
26 | {
27 | LOGD("\nSigInfo: ");
28 | LOGD("\tSignal number: %d", info->si_signo);
29 | LOGD("\tErrno: %d, Error: %s", info->si_errno, strerror(info->si_errno));
30 | LOGD("\tSignal Code: %d", info->si_code);
31 | LOGD("\tFaulting address: "PRINT_PTR"", (uintptr_t)info->si_addr);
32 | }
33 | void log_mcontext_content(mcontext_t* state_info)
34 | {
35 | LOGD("\nContext: ");
36 | LOGD("\tTRAP-Number: "PRINT_PTR, (uintptr_t)state_info->trap_no);
37 | LOGD("\tError-Code: "PRINT_PTR, (uintptr_t)state_info->error_code);
38 | LOGD("\tOld Mask: "PRINT_PTR, (uintptr_t)state_info->oldmask);
39 | LOGD("\tR0: "PRINT_PTR, (uintptr_t)state_info->arm_r0);
40 | LOGD("\tR1: "PRINT_PTR, (uintptr_t)state_info->arm_r1);
41 | LOGD("\tR2: "PRINT_PTR, (uintptr_t)state_info->arm_r2);
42 | LOGD("\tR3: "PRINT_PTR, (uintptr_t)state_info->arm_r3);
43 | LOGD("\tR4: "PRINT_PTR, (uintptr_t)state_info->arm_r4);
44 | LOGD("\tR5: "PRINT_PTR, (uintptr_t)state_info->arm_r5);
45 | LOGD("\tR6: "PRINT_PTR, (uintptr_t)state_info->arm_r6);
46 | LOGD("\tR7: "PRINT_PTR, (uintptr_t)state_info->arm_r7);
47 | LOGD("\tR8: "PRINT_PTR, (uintptr_t)state_info->arm_r8);
48 | LOGD("\tR9: "PRINT_PTR, (uintptr_t)state_info->arm_r9);
49 | LOGD("\tR10: "PRINT_PTR, (uintptr_t)state_info->arm_r10);
50 | LOGD("\tFP: "PRINT_PTR, (uintptr_t)state_info->arm_fp);
51 | LOGD("\tIP: "PRINT_PTR, (uintptr_t)state_info->arm_ip);
52 | LOGD("\tSP: "PRINT_PTR, (uintptr_t)state_info->arm_sp);
53 | LOGD("\tLR: "PRINT_PTR, (uintptr_t)state_info->arm_lr);
54 | LOGD("\tPC: "PRINT_PTR, (uintptr_t)state_info->arm_pc);
55 |
56 | uint32_t cpsr = state_info->arm_cpsr;
57 | LOGD("\tCPSR: "PRINT_PTR, (uintptr_t)cpsr);
58 | LOGI("\t->Thumb State: %d", (cpsr & CPSR_FLAG_THUMB) ? 1 : 0);
59 | LOGI("\t->FIQ Ints disable: %d", (cpsr & CPSR_FLAG_DISABLE_FIQ_INTERRUPTS) ? 1 : 0);
60 | LOGI("\t->IRQ Ints disable: %d", (cpsr & CPSR_FLAG_DISABLE_IRQ_INTERRUPTS) ? 1 : 0);
61 | LOGI("\t->Jazelle State: %d", (cpsr & CPSR_FLAG_JAZELLE) ? 1 : 0);
62 | LOGI("\t->Underflow: %d", (cpsr & CPSR_FLAG_UNDERFLOW_SATURATION) ? 1 : 0);
63 | LOGI("\t->Signed Overflow: %d", (cpsr & CPSR_FLAG_SIGNED_OVERFLOW) ? 1 : 0);
64 | LOGI("\t->Carry: %d", (cpsr & CPSR_FLAG_CARRY) ? 1 : 0);
65 | LOGI("\t->Zero: %d", (cpsr & CPSR_FLAG_ZERO) ? 1 : 0);
66 | LOGI("\t->Negative: %d", (cpsr & CPSR_FLAG_NEGATIVE) ? 1 : 0);
67 | }
68 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/base/mutex.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /*
18 | * Copyright (C) 2011 The Android Open Source Project
19 | *
20 | * Licensed under the Apache License, Version 2.0 (the "License");
21 | * you may not use this file except in compliance with the License.
22 | * You may obtain a copy of the License at
23 | *
24 | * http://www.apache.org/licenses/LICENSE-2.0
25 | *
26 | * Unless required by applicable law or agreed to in writing, software
27 | * distributed under the License is distributed on an "AS IS" BASIS,
28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 | * See the License for the specific language governing permissions and
30 | * limitations under the License.
31 | */
32 |
33 | #ifndef ART_RUNTIME_BASE_MUTEX_H_
34 | #define ART_RUNTIME_BASE_MUTEX_H_
35 |
36 | #if defined(__APPLE__)
37 | #define ART_USE_FUTEXES 0
38 | #else
39 | #define ART_USE_FUTEXES 1
40 | #endif
41 |
42 | // Currently Darwin doesn't support locks with timeouts.
43 | #if !defined(__APPLE__)
44 | #define HAVE_TIMED_RWLOCK 1
45 | #else
46 | #define HAVE_TIMED_RWLOCK 0
47 | #endif
48 |
49 | // LockLevel is used to impose a lock hierarchy [1] where acquisition of a Mutex at a higher or
50 | // equal level to a lock a thread holds is invalid. The lock hierarchy achieves a cycle free
51 | // partial ordering and thereby cause deadlock situations to fail checks.
52 | //
53 | // [1] http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163
54 | enum LockLevel {
55 | kLoggingLock = 0,
56 | kMemMapsLock,
57 | kSwapMutexesLock,
58 | kUnexpectedSignalLock,
59 | kThreadSuspendCountLock,
60 | kAbortLock,
61 | kJdwpSocketLock,
62 | kReferenceQueueSoftReferencesLock,
63 | kReferenceQueuePhantomReferencesLock,
64 | kReferenceQueueFinalizerReferencesLock,
65 | kReferenceQueueWeakReferencesLock,
66 | kReferenceQueueClearedReferencesLock,
67 | kReferenceProcessorLock,
68 | kRosAllocGlobalLock,
69 | kRosAllocBracketLock,
70 | kRosAllocBulkFreeLock,
71 | kAllocSpaceLock,
72 | kDexFileMethodInlinerLock,
73 | kDexFileToMethodInlinerMapLock,
74 | kMarkSweepMarkStackLock,
75 | kTransactionLogLock,
76 | kInternTableLock,
77 | kOatFileSecondaryLookupLock,
78 | kDefaultMutexLevel,
79 | kMarkSweepLargeObjectLock,
80 | kPinTableLock,
81 | kLoadLibraryLock,
82 | kJdwpObjectRegistryLock,
83 | kModifyLdtLock,
84 | kAllocatedThreadIdsLock,
85 | kMonitorPoolLock,
86 | kClassLinkerClassesLock,
87 | kBreakpointLock,
88 | kMonitorLock,
89 | kMonitorListLock,
90 | kThreadListLock,
91 | kBreakpointInvokeLock,
92 | kAllocTrackerLock,
93 | kDeoptimizationLock,
94 | kProfilerLock,
95 | kJdwpEventListLock,
96 | kJdwpAttachLock,
97 | kJdwpStartLock,
98 | kRuntimeShutdownLock,
99 | kTraceLock,
100 | kHeapBitmapLock,
101 | kMutatorLock,
102 | kInstrumentEntrypointsLock,
103 | kThreadListSuspendThreadLock,
104 | kZygoteCreationLock,
105 |
106 | kLockLevelCount // Must come last.
107 | };
108 |
109 | #endif // ART_RUNTIME_BASE_MUTEX_H_
110 |
--------------------------------------------------------------------------------
/app/src/main/jni/art_resolution.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "art_resolution.h"
18 | #include "memory_map_lookup.h"
19 | #include
20 | #include
21 |
22 | /*
23 | *
24 | * This function looks up a java function's native implementation in memory.
25 | *
26 | * The function returns true if the lookup didn't encounter any errors.
27 | * In case the function returns false the contents of the result structure
28 | * are undefined, and cannot be assumed to contain correct values.
29 | *
30 | * If the function succeeds
31 | *
32 | */
33 | bool android_FindLoadedClass(struct ArtClassContext* result, const char* class_name)
34 | {
35 | struct MemoryMapView* mem_map = CreateMemoryMapView();
36 | if(mem_map == NULL)
37 | {
38 | return false;
39 | }
40 |
41 | struct MemoryMappedFile* f;
42 | list_for_each_entry(f, &mem_map->list_files, view_list_files_entry)
43 | {
44 | void* elf_start;
45 | void* elf_oat_start;
46 | void* elf_oat_end;
47 | if(!extractElfOatPointersFromFile(f, &elf_start, &elf_oat_start, &elf_oat_end))
48 | {
49 | continue;
50 | }
51 | if(!oat_Setup(&result->oat_file, elf_oat_start, elf_oat_end))
52 | {
53 | continue;
54 | }
55 |
56 | LOGD("Found oat file: ELF: "PRINT_PTR", OAT: "PRINT_PTR", END: "PRINT_PTR,
57 | (uintptr_t)elf_start, (uintptr_t)elf_oat_start, (uintptr_t)elf_oat_end);
58 | for(uint32_t i = 0; i < NumDexFiles(result->oat_file.header); i++)
59 | {
60 | if(!oat_GetOatDexFile(&result->oat_file, &result->oat_dex, i))
61 | {
62 | continue;
63 | }
64 | if(!oat_FindClassInDex(&result->oat_dex, &result->oat_class, class_name))
65 | {
66 | continue;
67 | }
68 | // Found the class, done here
69 | DestroyMemoryMapView(mem_map);
70 | return true;
71 | }
72 | }
73 | DestroyMemoryMapView(mem_map);
74 | return false;
75 | }
76 | bool android_FindLoadedMethod(struct ArtMethodContext* result, const char* class_name, const char* method_name, const char* method_proto)
77 | {
78 | if(!android_FindLoadedClass(&result->clazz, class_name))
79 | {
80 | return false;
81 | }
82 | LOGI("Found class %s", class_name);
83 | const struct DexHeader* hdr = result->clazz.oat_dex.data.dex_file_pointer;
84 | uint16_t class_def_index = GetIndexForClassDef(hdr, result->clazz.oat_class.dex_class.class_def);
85 | log_dex_file_class_def_contents(hdr, class_def_index);
86 | if(oat_FindDirectMethod(&result->clazz.oat_class, &result->oat_method, method_name, method_proto))
87 | {
88 | LOGI("Found direct method %s", method_name);
89 | return true;
90 | }
91 | if(oat_FindVirtualMethod(&result->clazz.oat_class, &result->oat_method, method_name, method_proto))
92 | {
93 | LOGI("Found virtual method %s", method_name);
94 | return true;
95 | }
96 | LOGI("Could not find method %s", method_name);
97 | // Neither direct, nor virtual => not found
98 | return false;
99 | }
--------------------------------------------------------------------------------
/libUtility/src/main/jni/city.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is in no way part of my work and was used as-is aside from this notice.
3 | * Lukas Dresel
4 | */
5 |
6 | // city.h - cityhash-c
7 | // CityHash on C
8 | // Copyright (c) 2011-2012, Alexander Nusov
9 | //
10 | // - original copyright notice -
11 | // Copyright (c) 2011 Google, Inc.
12 | //
13 | // Permission is hereby granted, free of charge, to any person obtaining a copy
14 | // of this software and associated documentation files (the "Software"), to deal
15 | // in the Software without restriction, including without limitation the rights
16 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 | // copies of the Software, and to permit persons to whom the Software is
18 | // furnished to do so, subject to the following conditions:
19 | //
20 | // The above copyright notice and this permission notice shall be included in
21 | // all copies or substantial portions of the Software.
22 | //
23 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 | // THE SOFTWARE.
30 | //
31 | // CityHash, by Geoff Pike and Jyrki Alakuijala
32 | //
33 | // This file provides a few functions for hashing strings. On x86-64
34 | // hardware in 2011, CityHash64() is faster than other high-quality
35 | // hash functions, such as Murmur. This is largely due to higher
36 | // instruction-level parallelism. CityHash64() and CityHash128() also perform
37 | // well on hash-quality tests.
38 | //
39 | // CityHash128() is optimized for relatively long strings and returns
40 | // a 128-bit hash. For strings more than about 2000 bytes it can be
41 | // faster than CityHash64().
42 | //
43 | // Functions in the CityHash family are not suitable for cryptography.
44 | //
45 | // WARNING: This code has not been tested on big-endian platforms!
46 | // It is known to work well on little-endian platforms that have a small penalty
47 | // for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs.
48 | //
49 | // By the way, for some hash functions, given strings a and b, the hash
50 | // of a+b is easily derived from the hashes of a and b. This property
51 | // doesn't hold for any hash functions in this file.
52 |
53 | #ifndef CITY_HASH_H_
54 | #define CITY_HASH_H_
55 |
56 | #include
57 | #include
58 |
59 | typedef uint8_t uint8;
60 | typedef uint32_t uint32;
61 | typedef uint64_t uint64;
62 |
63 | typedef struct _uint128 uint128;
64 | struct _uint128 {
65 | uint64 first;
66 | uint64 second;
67 | };
68 |
69 | #define Uint128Low64(x) (x).first
70 | #define Uint128High64(x) (x).second
71 |
72 | // Hash function for a byte array.
73 | uint64 CityHash64(const char *buf, size_t len);
74 |
75 | // Hash function for a byte array. For convenience, a 64-bit seed is also
76 | // hashed into the result.
77 | uint64 CityHash64WithSeed(const char *buf, size_t len, uint64 seed);
78 |
79 | // Hash function for a byte array. For convenience, two seeds are also
80 | // hashed into the result.
81 | uint64 CityHash64WithSeeds(const char *buf, size_t len,
82 | uint64 seed0, uint64 seed1);
83 |
84 | // Hash function for a byte array.
85 | uint128 CityHash128(const char *s, size_t len);
86 |
87 | // Hash function for a byte array. For convenience, a 128-bit seed is also
88 | // hashed into the result.
89 | uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed);
90 |
91 | #endif // CITY_HASH_H_
92 |
93 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/logging.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _LOGGING_H
18 | #define _LOGGING_H
19 |
20 | #include
21 | #include
22 |
23 | #include "config.h"
24 | #include "macros.h"
25 |
26 | #include
27 | #include
28 | #include
29 |
30 | #define LOG_TAG "jni"
31 |
32 | #if LOG_LEVEL <= LOG_LEVEL_VERBOSE
33 | #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
34 | #else
35 | #define LOGV(...)
36 | #endif
37 |
38 | #if LOG_LEVEL <= LOG_LEVEL_DEBUG
39 | #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
40 | #else
41 | #define LOGD(...)
42 | #endif
43 |
44 | #if LOG_LEVEL <= LOG_LEVEL_INFO
45 | #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
46 | #else
47 | #define LOGI(...)
48 | #endif
49 |
50 | #if LOG_LEVEL <= LOG_LEVEL_WARN
51 | #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, "["__FILE__":"__LINE_STRING__"]"__VA_ARGS__)
52 | #else
53 | #define LOW(...)
54 | #endif
55 |
56 | #if LOG_LEVEL <= LOG_LEVEL_ERROR
57 | #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "["__FILE__":"__LINE_STRING__"]"__VA_ARGS__)
58 | #else
59 | #define LOGE(...)
60 | #endif
61 |
62 | #if LOG_LEVEL <= LOG_LEVEL_FATAL
63 | #define LOGF(...) __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "["__FILE__":"__LINE_STRING__"]"__VA_ARGS__)
64 | #else
65 | #define LOGF(...)
66 | #endif
67 |
68 | #define DCHECK(x) \
69 | if (UNLIKELY(!(x))) \
70 | LOGD( "Check failed: %s", #x );
71 |
72 | #define DCHECK_RETURN(x, value) \
73 | if (UNLIKELY(!(x))) { \
74 | LOGD( "Check failed: %s", #x ); \
75 | return (value); \
76 | }
77 | #define DCHECK_RETURNNULL(x) DCHECK_RETURN((x), (NULL))
78 | #define DCHECK_RETURNFALSE(x) DCHECK_RETURN((x), (false))
79 |
80 | #define CHECK(x) \
81 | if (UNLIKELY(!(x))) \
82 | LOGF( "Check failed: %s", #x );
83 |
84 | #define CHECK_RETURN(x, value) \
85 | if (UNLIKELY(!(x))) { \
86 | LOGF( "Check failed: %s", #x ); \
87 | return (value); \
88 | }
89 | #define CHECK_RETURNVOID(x) \
90 | if (UNLIKELY(!(x))) { \
91 | LOGF( "Check failed: %s", #x ); \
92 | return; \
93 | }
94 | #define CHECK_RETURNNULL(x) CHECK_RETURN((x), (NULL))
95 | #define CHECK_RETURNFALSE(x) CHECK_RETURN((x), (false))
96 |
97 | #define CHECK_EQ(x, y) CHECK((x) == (y))
98 | #define CHECK_NE(x, y) CHECK((x) != (y))
99 | #define CHECK_LE(x, y) CHECK((x) <= (y))
100 | #define CHECK_LT(x, y) CHECK((x) < (y))
101 | #define CHECK_GE(x, y) CHECK((x) >= (y))
102 | #define CHECK_GT(x, y) CHECK((x) > (y))
103 |
104 | #define PRINT_PTR "0x%08"PRIxPTR
105 |
106 | bool hexdump(JNIEnv *env, const void *addr, jlong numBytes, jlong bytesPerLine);
107 |
108 | bool hexdump_primitive(const void *addr, jlong numBytes, jlong bytesPerLine);
109 |
110 | bool hexdump_aligned(JNIEnv *env, const void *addr, jlong numBytes, jlong bytesPerLine,
111 | jlong alignment);
112 |
113 | bool hexdump_aligned_primitive(const void *addr, jlong numBytes, jlong bytesPerLine,
114 | jlong alignment);
115 |
116 |
117 | #endif
118 |
119 |
--------------------------------------------------------------------------------
/libHooking/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.model.native'
2 |
3 | def distrib_include_dir = file("../distribution/include").absolutePath
4 | def distrib_lib_dir = file("../distribution/lib").absolutePath
5 | model {
6 | android {
7 | compileSdkVersion = 24
8 | buildToolsVersion = '24.0.1'
9 |
10 | defaultConfig {
11 | minSdkVersion.apiLevel = 21
12 | targetSdkVersion.apiLevel = 24
13 | versionCode = 1
14 | versionName = '1.0'
15 | }
16 | ndk {
17 | moduleName = 'Hooking'
18 |
19 | toolchain = 'clang'
20 | CFlags.addAll(['--std=gnu99'])
21 | ldLibs.addAll(['android', 'log'])
22 |
23 | // Workaround, for some reason I can't figure out a way to make the library dependencies
24 | // apply to the architecture specific source sets
25 | CFlags.addAll(['-I', file("${distrib_include_dir}").absolutePath])
26 | }
27 |
28 | sources {
29 | main {
30 | jni {
31 | source {
32 | srcDir 'src/main/jni'
33 | }
34 | }
35 | }
36 | armeabi {
37 | jni {
38 | source {
39 | srcDir 'src/armeabi/jni'
40 | }
41 | }
42 | }
43 | armeabi_v7a {
44 | jni {
45 | source {
46 | srcDir 'src/armeabi/jni'
47 | }
48 | }
49 | }
50 | }
51 | buildTypes {
52 | debug {
53 | ndk.with {
54 | debuggable = true
55 | }
56 | }
57 | release {
58 | minifyEnabled = false
59 | proguardFiles.add(file('proguard-rules.pro'))
60 | }
61 | }
62 |
63 | productFlavors {
64 | create("armeabi") {
65 | ndk.with {
66 | abiFilters.add("armeabi")
67 |
68 | ldFlags.add('-L' + file("${distrib_lib_dir}/utility/armeabi/").absolutePath)
69 | ldFlags.add('-lUtility')
70 |
71 | ldFlags.add('-L' + file("${distrib_lib_dir}/hostsystem/armeabi/").absolutePath)
72 | ldFlags.add('-lHostSystem')
73 | }
74 | }
75 | create("armeabi_v7a") {
76 | ndk.with {
77 | abiFilters.add("armeabi-v7a")
78 |
79 | ldFlags.add('-L' + file("${distrib_lib_dir}/utility/armeabi-v7a/").absolutePath)
80 | ldFlags.add('-lUtility')
81 |
82 | ldFlags.add('-L' + file("${distrib_lib_dir}/hostsystem/armeabi-v7a/").absolutePath)
83 | ldFlags.add('-lHostSystem')
84 | }
85 | }
86 | }
87 | }
88 | }
89 |
90 | // This is just copy out the header file and built lib into distribution
91 | // directory for clint application to use; it is a small overhead of this sample:
92 | // both lib and app are put inside one project space [save maintenance time]
93 | task distributeLib (type : Copy) {
94 | // trigger build library
95 | dependsOn assemble
96 |
97 | into '../distribution/'
98 | from('src/main/jni') {
99 | include '*.h'
100 | into 'include/hooking/'
101 | }
102 | from('src/') {
103 | include '**/jni/*.h'
104 | into 'include/hooking/'
105 | }
106 | from ('build/outputs/native/release/armeabi/lib/') {
107 | into 'lib/hooking'
108 | }
109 | from ('build/outputs/native/release/armeabi_v7a/lib/') {
110 | into 'lib/hooking'
111 | }
112 | }
113 | distributeLib.shouldRunAfter ':libUtility:distributeLib', ':libHostSystem:distributeLib'
--------------------------------------------------------------------------------
/libHostSystem/src/main/jni/abi.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/18/2015.
19 | //
20 |
21 | #ifndef NDKTEST_ABI_INTERFACE_H
22 | #define NDKTEST_ABI_INTERFACE_H
23 |
24 | #ifdef __cplusplus
25 | extern "C"
26 | {
27 | #endif
28 |
29 | #include
30 | #include
31 | #include
32 |
33 | struct InstructionInfo
34 | {
35 | void* call_addr;
36 | void* mem_addr;
37 | bool thumb;
38 | };
39 |
40 | /*
41 | * Returns the actual memory address of the instruction executed when branching to this address.
42 | * On many architectures this might simply return the entrypoint, but there are exceptions.
43 | * For example on ARM processors THUMB mode instructions are addressed with the lowest byte set.
44 | * This has the pointer pointing one byte past the instruction actually being executed.
45 | */
46 | void *InstructionPointerToCodePointer(const void *instruction_pointer);
47 |
48 |
49 | /*
50 | * Returns the length of the instruction at the given InstructionPointer. This must be the
51 | * InstructionPointer and not the memory address, because e.g. on ARM the instruction size can only
52 | * be determined with knowledge of whether the processor is in ARM or THUMB mode. This is determined
53 | * by examining the lowest bit of the InstructionPointer
54 | */
55 | uint32_t GetInstructionLengthAtInstructionPointer(const void *instruction_pointer);
56 |
57 | /*
58 | * Generally only valid on function entry.
59 | * Returns the value of the argument at the specified index. This is generalized to a uint64_t and
60 | * might need to be casted to the appropriate types like uint32_t depending on the target
61 | * architecture.
62 | */
63 | uint64_t GetArgument(ucontext_t *c, unsigned int index);
64 |
65 | /*
66 | * Generally only valid on function entry.
67 | * Sets the value of the argument at the specified index to a specific value.
68 | * This is generalized to a uint64_t and will internally be cast back to the type appropriate for a
69 | * specific architecture, e.g on x86 or armeabi to uint32_t.
70 | */
71 | void SetArgument(ucontext_t *c, unsigned int index, uint64_t val);
72 |
73 | /*
74 | * Ideally it should extract from the current program context the next instruction that will be
75 | * executed. Because of time-constraints however the current implementation simply returns the
76 | * next instruction in memory. This means this will result in wrong results whenever the execution
77 | * branches, like on jumps and calls.
78 | *
79 | * CAUTION: Depending on the architecture this might not actually be a pointer to the first
80 | * instruction of the function in memory. An example of this is the ARM architecture where
81 | * this returns the address + 1 if the processor is operating in THUMB mode.
82 | *
83 | * To get the actual address of the instruction to be executed use the
84 | * @link{InstructionPointerToCodePointer} function.
85 | */
86 | void *ExtractNextExecutedInstructionPointer(ucontext_t *ctx);
87 |
88 | /*
89 | * Generally only valid on function entry. Extracts the return address of a function from the
90 | * calling convention. E.g. on x86 it gets it from the top of the stack,
91 | * or on ARM from the LR register
92 | */
93 | void *ExtractReturnAddress(ucontext_t *ctx);
94 |
95 |
96 |
97 | #ifdef __cplusplus
98 | }
99 | #endif
100 |
101 | #endif //NDKTEST_ABI_H
102 |
--------------------------------------------------------------------------------
/libHostSystem/src/armeabi/jni/abi.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/18/2015.
19 | //
20 |
21 |
22 | #include
23 | #include
24 | #include "../../main/jni/abi.h"
25 |
26 | #include "../../main/jni/cpsr_util.h"
27 |
28 | bool IsAddressThumbMode(const void *address)
29 | {
30 | void* aligned = align_address_to_size(address, 4);
31 | uint32_t offset = (uint32_t)(address - aligned);
32 | return (offset == 1 || offset == 3);
33 | }
34 | void* InstructionPointerToCodePointer(const void *instruction_pointer)
35 | {
36 | return (void*)((uint64_t) instruction_pointer & ~0x1);
37 | }
38 |
39 | static const uint16_t thumb_32bit_mask = 0b1111100000000000; // this one can be a const int.
40 |
41 | // But these have to be constant expressions for the compiler to use them in a switch-case statement
42 | #define THUMB_32BIT_INSTRUCTION_PATTERN1 0b1110100000000000
43 | #define THUMB_32BIT_INSTRUCTION_PATTERN2 0b1111000000000000
44 | #define THUMB_32BIT_INSTRUCTION_PATTERN3 0b1111100000000000
45 |
46 | uint32_t GetInstructionLengthAtInstructionPointer(const void *instruction_pointer)
47 | {
48 | if(!IsAddressThumbMode(instruction_pointer))
49 | {
50 | return 4;
51 | }
52 | uint16_t halfword = *(uint16_t*) InstructionPointerToCodePointer(instruction_pointer);
53 | uint16_t masked = halfword & thumb_32bit_mask;
54 | switch(masked)
55 | {
56 | case THUMB_32BIT_INSTRUCTION_PATTERN1:
57 | case THUMB_32BIT_INSTRUCTION_PATTERN2:
58 | case THUMB_32BIT_INSTRUCTION_PATTERN3:
59 | return 4;
60 | default:
61 | return 2;
62 | }
63 | }
64 |
65 | uint64_t GetArgument(ucontext_t *c, unsigned int index)
66 | {
67 | mcontext_t* state_info = &(c->uc_mcontext);
68 | switch(index)
69 | {
70 | case 0:
71 | return (uint64_t)state_info->arm_r0;
72 | case 1:
73 | return (uint64_t)state_info->arm_r1;
74 | case 2:
75 | return (uint64_t)state_info->arm_r2;
76 | case 3:
77 | return (uint64_t)state_info->arm_r3;
78 |
79 | default:
80 | return (uint64_t)*(((uint32_t*)(state_info->arm_sp)) + (index - 4));
81 | }
82 | }
83 |
84 | void SetArgument(ucontext_t *c, unsigned int index, uint64_t val)
85 | {
86 | mcontext_t* state_info = &(c->uc_mcontext);
87 | switch(index)
88 | {
89 | case 0:
90 | state_info->arm_r0 = (uint32_t)val;
91 | break;
92 | case 1:
93 | state_info->arm_r1 = (uint32_t)val;
94 | break;
95 | case 2:
96 | state_info->arm_r2 = (uint32_t)val;
97 | break;
98 | case 3:
99 | state_info->arm_r3 = (uint32_t)val;
100 | break;
101 |
102 | default:
103 | *(((uint32_t*)(state_info->arm_sp)) + (index - 4)) = (uint32_t)val;
104 | break;
105 | }
106 | }
107 |
108 | void* ExtractNextExecutedInstructionPointer(ucontext_t *ctx)
109 | {
110 | mcontext_t* stateInfo = &ctx->uc_mcontext;
111 |
112 | bool thumb = (stateInfo->arm_cpsr & CPSR_FLAG_THUMB) != 0;
113 |
114 | void* addr = (void*)stateInfo->arm_pc + (thumb ? 1 : 0);
115 | void* next_addr = addr + GetInstructionLengthAtInstructionPointer(addr);
116 |
117 | return next_addr;
118 | }
119 | void* ExtractReturnAddress(ucontext_t *ctx)
120 | {
121 | mcontext_t* stateInfo = &ctx->uc_mcontext;
122 | void* addr = (void*)stateInfo->arm_lr;
123 | return addr;
124 | }
125 |
--------------------------------------------------------------------------------
/CRAPL-LICENSE.txt:
--------------------------------------------------------------------------------
1 | THE CRAPL v0 BETA 1
2 |
3 |
4 | 0. Information about the CRAPL
5 |
6 | If you have questions or concerns about the CRAPL, or you need more
7 | information about this license, please contact:
8 |
9 | Matthew Might
10 | http://matt.might.net/
11 |
12 |
13 | I. Preamble
14 |
15 | Science thrives on openness.
16 |
17 | In modern science, it is often infeasible to replicate claims without
18 | access to the software underlying those claims.
19 |
20 | Let's all be honest: when scientists write code, aesthetics and
21 | software engineering principles take a back seat to having running,
22 | working code before a deadline.
23 |
24 | So, let's release the ugly. And, let's be proud of that.
25 |
26 |
27 | II. Definitions
28 |
29 | 1. "This License" refers to version 0 beta 1 of the Community
30 | Research and Academic Programming License (the CRAPL).
31 |
32 | 2. "The Program" refers to the medley of source code, shell scripts,
33 | executables, objects, libraries and build files supplied to You,
34 | or these files as modified by You.
35 |
36 | [Any appearance of design in the Program is purely coincidental and
37 | should not in any way be mistaken for evidence of thoughtful
38 | software construction.]
39 |
40 | 3. "You" refers to the person or persons brave and daft enough to use
41 | the Program.
42 |
43 | 4. "The Documentation" refers to the Program.
44 |
45 | 5. "The Author" probably refers to the caffeine-addled graduate
46 | student that got the Program to work moments before a submission
47 | deadline.
48 |
49 |
50 | III. Terms
51 |
52 | 1. By reading this sentence, You have agreed to the terms and
53 | conditions of this License.
54 |
55 | 2. If the Program shows any evidence of having been properly tested
56 | or verified, You will disregard this evidence.
57 |
58 | 3. You agree to hold the Author free from shame, embarrassment or
59 | ridicule for any hacks, kludges or leaps of faith found within the
60 | Program.
61 |
62 | 4. You recognize that any request for support for the Program will be
63 | discarded with extreme prejudice.
64 |
65 | 5. The Author reserves all rights to the Program, except for any
66 | rights granted under any additional licenses attached to the
67 | Program.
68 |
69 |
70 | IV. Permissions
71 |
72 | 1. You are permitted to use the Program to validate published
73 | scientific claims.
74 |
75 | 2. You are permitted to use the Program to validate scientific claims
76 | submitted for peer review, under the condition that You keep
77 | modifications to the Program confidential until those claims have
78 | been published.
79 |
80 | 3. You are permitted to use and/or modify the Program for the
81 | validation of novel scientific claims if You make a good-faith
82 | attempt to notify the Author of Your work and Your claims prior to
83 | submission for publication.
84 |
85 | 4. If You publicly release any claims or data that were supported or
86 | generated by the Program or a modification thereof, in whole or in
87 | part, You will release any inputs supplied to the Program and any
88 | modifications You made to the Progam. This License will be in
89 | effect for the modified program.
90 |
91 |
92 | V. Disclaimer of Warranty
93 |
94 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
95 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
96 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
97 | WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
98 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
99 | A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
100 | PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
101 | DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
102 | CORRECTION.
103 |
104 |
105 | VI. Limitation of Liability
106 |
107 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
108 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR
109 | CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
110 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
111 | ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
112 | NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR
113 | LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM
114 | TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER
115 | PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
116 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/modifiers.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 1/26/2016.
19 | //
20 |
21 | #ifndef NDKTEST_ART_MODIFIERS_H
22 | #define NDKTEST_ART_MODIFIERS_H
23 |
24 |
25 | #include
26 |
27 | // class, field, method, ic
28 | #define kAccPublic 0x0001
29 |
30 | // field, method, ic
31 | #define kAccPrivate 0x0002
32 |
33 | // field, method, ic
34 | #define kAccProtected 0x0004
35 |
36 | // field, method, ic
37 | #define kAccStatic 0x0008
38 |
39 | // class, field, method, ic
40 | #define kAccFinal 0x0010
41 |
42 | // method (only allowed on natives)
43 | #define kAccSynchronized 0x0020
44 |
45 | // class (not used in dex)
46 | #define kAccSuper 0x0020
47 |
48 | // field
49 | #define kAccVolatile 0x0040
50 |
51 | // method (1.5)
52 | #define kAccBridge 0x0040
53 |
54 | // field
55 | #define kAccTransient 0x0080
56 |
57 | // method (1.5)
58 | #define kAccVarargs 0x0080
59 |
60 | // method
61 | #define kAccNative 0x0100
62 |
63 | // class, ic
64 | #define kAccInterface 0x0200
65 |
66 | // class, method, ic
67 | #define kAccAbstract 0x0400
68 |
69 | // method
70 | #define kAccStrict 0x0800
71 |
72 | // class, field, method, ic
73 | #define kAccSynthetic 0x1000
74 |
75 | // class, ic (1.5)
76 | #define kAccAnnotation 0x2000
77 |
78 | // class, field, ic (1.5)
79 | #define kAccEnum 0x4000
80 |
81 | // bits set from Java sources (low 16)
82 | #define kAccJavaFlagsMask 0xffff
83 |
84 | // method (dex only) <(cl)init>
85 | #define kAccConstructor 0x00010000
86 |
87 | // method (dex only)
88 | #define kAccDeclaredSynchronized 0x00020000
89 |
90 | // class (dex only)
91 | #define kAccClassIsProxy 0x00040000
92 |
93 | // class (runtime),
94 | #define kAccPreverified 0x00080000
95 |
96 | // method (dex only)
97 | #define kAccFastNative 0x00080000
98 |
99 | // method (dex only)
100 | #define kAccPortableCompiled 0x00100000
101 |
102 | // method (dex only)
103 | #define kAccMiranda 0x00200000
104 |
105 | // Special runtime-only flags.
106 | // Note: if only kAccClassIsReference is set, we have a soft reference.
107 |
108 | // class/ancestor overrides finalize()
109 | #define kAccClassIsFinalizable 0x80000000
110 |
111 | // class is a soft/weak/phantom ref
112 | #define kAccClassIsReference 0x08000000
113 |
114 | // class is a weak reference
115 | #define kAccClassIsWeakReference 0x04000000
116 |
117 | // class is a finalizer reference
118 | #define kAccClassIsFinalizerReference 0x02000000
119 |
120 | // class is a phantom reference
121 | #define kAccClassIsPhantomReference 0x01000000
122 |
123 | #define kAccReferenceFlagsMask (kAccClassIsReference | kAccClassIsWeakReference | kAccClassIsFinalizerReference | kAccClassIsPhantomReference)
124 |
125 | // Valid (meaningful) bits for a field.
126 | #define kAccValidFieldFlags (kAccPublic | kAccPrivate | kAccProtected | kAccStatic | kAccFinal | kAccVolatile | kAccTransient | kAccSynthetic | kAccEnum)
127 |
128 | // Valid (meaningful) bits for a method.
129 | #define kAccValidMethodFlags (kAccPublic | kAccPrivate | kAccProtected | kAccStatic | kAccFinal | kAccSynchronized | kAccBridge | kAccVarargs | kAccNative | kAccAbstract | kAccStrict | kAccSynthetic | kAccMiranda | kAccConstructor | kAccDeclaredSynchronized)
130 |
131 | // Valid (meaningful) bits for a class (not interface).
132 | // Note 1. These are positive bits. Other bits may have to be zero.
133 | // Note 2. Inner classes can expose more access flags to Java programs. That is handled by libcore.
134 | #define kAccValidClassFlags (kAccPublic | kAccFinal | kAccSuper | kAccAbstract | kAccSynthetic | kAccEnum)
135 |
136 | // Valid (meaningful) bits for an interface.
137 | // Note 1. Annotations are interfaces.
138 | // Note 2. These are positive bits. Other bits may have to be zero.
139 | // Note 3. Inner classes can expose more access flags to Java programs. That is handled by libcore.
140 | #define kAccValidInterfaceFlags (kAccPublic | kAccInterface | kAccAbstract | kAccSynthetic | kAccAnnotation)
141 |
142 | #endif //NDKTEST_ART_MODIFIERS_H
143 |
--------------------------------------------------------------------------------
/libUtility/src/main/jni/memory.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "memory.h"
18 | #include "util.h"
19 | #include "exceptions.h"
20 | #include "logging.h"
21 | #include "error.h"
22 |
23 | #ifdef __cplusplus
24 | extern "C" {
25 | #endif
26 |
27 | bool iterate_byte_array_chunks(JNIEnv *env, const void *addr, jlong numBytes, jlong bytesPerLine,
28 | OUTPUT_CALLBACK output, void *additionalInfo)
29 | {
30 | if (hasExceptionOccurred(env))
31 | {
32 | return false;
33 | }
34 | if (addr == NULL)
35 | {
36 | throwNewJNIException(env, "java/lang/RuntimeException", "Hexdump: Error addr == NULL");
37 | return false;
38 | }
39 | if (bytesPerLine <= 0)
40 | {
41 | throwNewJNIException(env, "java/lang/RuntimeException", "Hexdump: Error bytesPerLine <= 0");
42 | return false;
43 | }
44 | for (uint64_t i = 0; i < numBytes; i += bytesPerLine)
45 | {
46 | if (!output(addr, i, bytesPerLine, additionalInfo))
47 | {
48 | throwNewJNIException(env, "java/lang/RuntimeException",
49 | "Hexdump: Error: The callback returned false.");
50 | return false;
51 | }
52 | }
53 | return true;
54 | }
55 |
56 | bool iterate_byte_array_chunks_primitive(const void *addr, jlong numBytes, jlong bytesPerLine,
57 | OUTPUT_CALLBACK output, void *additionalInfo)
58 | {
59 | if (addr == NULL)
60 | {
61 | return false;
62 | }
63 | if (bytesPerLine <= 0)
64 | {
65 | return false;
66 | }
67 | for (uint64_t i = 0; i < numBytes; i += bytesPerLine)
68 | {
69 | if (!output(addr, i, bytesPerLine, additionalInfo))
70 | {
71 | return false;
72 | }
73 | }
74 | return true;
75 | }
76 |
77 | bool set_memory_protection(const void *startingAddress, jlong numBytes, jboolean read, jboolean write,
78 | jboolean execute)
79 | {
80 | /*LOGD("set_memory_protection(addr="
81 | PRINT_PTR
82 | ", numBytes=%lld, read=%s, write=%s, exec=%s", (uintptr_t) startingAddress,
83 | numBytes, read ? "true" : "false", write ? "true" : "false", execute ? "true" : "false");*/
84 | int protections = 0;
85 |
86 | protections |= read ? PROT_READ : 0;
87 | protections |= write ? PROT_WRITE : 0;
88 | protections |= execute ? PROT_EXEC : 0;
89 |
90 | void *addr = get_page_base(startingAddress);
91 | jlong size = numBytes + (startingAddress - addr);
92 |
93 | /*LOGV("Attempting to change memory permissions to %x...", protections);*/
94 | if (mprotect(addr, size, protections) == -1)
95 | {
96 | LOGE("Failed to change protections of %lld bytes from address "
97 | PRINT_PTR
98 | ", errno: %d", size, (uintptr_t) addr, errno);
99 | return false;
100 | }
101 | /*LOGV("Success! Changed protections of %lld bytes from address "
102 | PRINT_PTR
103 | " to full.", size, (uintptr_t) addr);*/
104 | return true;
105 | }
106 |
107 | void dump_process_memory_map(void)
108 | {
109 | char buff[3200];
110 |
111 | FILE *fp = fopen("/proc/self/maps", "r");
112 | while (fgets(buff, sizeof(buff), fp) != NULL)
113 | {
114 | LOGI("MemoryMap: %s", buff);
115 | }
116 | if (feof(fp))
117 | {
118 | LOGI("MemoryMap: %s", "#################### END OF MEMORY MAP ###################");
119 | }
120 | if (ferror(fp))
121 | {
122 | LOGI("MemoryMap: %s",
123 | "#################### ERROR READING /proc/self/maps ###################");
124 | }
125 | fclose(fp);
126 | }
127 |
128 | void *allocate_memory_chunk(size_t size)
129 | {
130 | CHECK(size > 0);
131 | //TODO change to own memory implementation to avoid being fooled by usermode code
132 | void *result = malloc(size);
133 | if (UNLIKELY(result == NULL))
134 | {
135 | set_last_error("Allocation of memory failed.");
136 | LOGF("malloc of size %zd failed: %s", size, strerror(errno));
137 | }
138 | LOGV("Allocation of %d bytes memory returned: "PRINT_PTR, size, result);
139 | return result;
140 | }
141 |
142 | void free_memory_chunk(void *mem)
143 | {
144 | CHECK_NE(mem, NULL);
145 | LOGV("Memory being freed: "PRINT_PTR, mem);
146 | free(mem);
147 | }
148 |
149 | #ifdef __cplusplus
150 | }
151 | #endif
152 |
153 |
154 |
155 |
--------------------------------------------------------------------------------
/app/src/main/jni/memory_map_lookup.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 |
18 | #ifndef NDKTEST_MEMORY_MAP_LOOKUP_H
19 | #define NDKTEST_MEMORY_MAP_LOOKUP_H
20 |
21 | #include
22 | #include
23 | #include
24 |
25 | // TODO This entire class may still contain memory leaks. Not well checked yet.
26 |
27 | struct MemorySegment
28 | {
29 | struct list_head view_list_segments_entry;
30 | struct list_head file_list_segments_entry;
31 | struct list_head region_list_segments_entry;
32 |
33 | void* start;
34 | void* end;
35 | bool flag_readable;
36 | bool flag_writable;
37 | bool flag_executable;
38 | bool flag_shared;
39 | struct FilePath* path;
40 | };
41 | struct FilePath
42 | {
43 | struct list_head view_list_filepaths_entry;
44 |
45 | uint64_t path_hash;
46 | char path[0];
47 | };
48 | struct MemoryRegion
49 | {
50 | struct list_head file_list_regions_entry;
51 | struct list_head list_segments;
52 | uint32_t num_segments;
53 | void* start;
54 | void* end;
55 | };
56 | struct MemoryMappedFile
57 | {
58 | struct list_head view_list_files_entry;
59 |
60 | struct list_head list_segments;
61 | struct list_head list_regions;
62 | uint32_t num_segments;
63 | uint32_t num_regions;
64 | struct FilePath* path;
65 | };
66 | struct MemoryMapView
67 | {
68 | struct list_head list_filepaths;
69 | struct list_head list_files;
70 | struct list_head list_segments;
71 |
72 | uint32_t numFoundFiles;
73 | uint32_t numFoundSegments;
74 | };
75 |
76 | typedef bool (*REGION_PREDICATE)(void* start, void* end, void* arg);
77 | typedef bool (*FILE_PREDICATE)(struct MemoryMapView *, struct MemoryMappedFile *, void* arg);
78 | typedef bool (*FILEPATH_PREDICATE)(struct MemoryMapView *, struct FilePath*, void* arg);
79 | typedef bool (*SEGMENT_PREDICATE)(struct MemoryMapView *, struct MemorySegment*, void* arg);
80 |
81 | bool FilePathByPathStringPredicate(struct MemoryMapView * view, struct FilePath* fp, const char* arg);
82 | bool FileByPathStringPredicate(struct MemoryMapView * view, struct MemoryMappedFile * f, const char* arg);
83 | bool FileByFilePathPredicate(struct MemoryMapView * view, struct MemoryMappedFile * f, const struct FilePath* fp);
84 | bool FileIsElfOatFilePredicate(struct MemoryMapView * view, struct MemoryMappedFile * f, void* unused);
85 | bool MemorySegmentByAddressPredicate(struct MemoryMapView * view, struct MemorySegment* seg, void* addressInQuestion);
86 |
87 | struct MemoryMappedFile* findFileByPredicate(struct MemoryMapView * view, FILE_PREDICATE pred, void* arg);
88 | struct MemoryMappedFile* findFileByPath(struct MemoryMapView * view, char* path);
89 | struct MemorySegment* findMemorySegmentByPredicate(struct MemoryMapView * view, SEGMENT_PREDICATE pred, void* arg);
90 | struct MemoryRegion * findRegionByPredicate(struct MemoryMappedFile * f, REGION_PREDICATE pred, void* arg);
91 |
92 | bool extractElfOatPointersFromFile(struct MemoryMappedFile *f, void **result_elf_start,
93 | void **result_oat_start, void **result_oat_end);
94 |
95 | struct MemoryMapView * CreateMemoryMapView();
96 | void DestroyMemoryMapView(struct MemoryMapView *self);
97 |
98 | void logMemorySegmentContents(struct MemorySegment* seg);
99 | void logMemoryRegionContents(struct MemoryRegion *r);
100 | void logFileContents(struct MemoryMappedFile * f);
101 |
102 |
103 |
104 |
105 | /*
106 | typedef bool IS_START_SEGMENT(struct MemorySegment* current, char* curr_path, void* args);
107 | typedef bool IS_CONTINUATION_SEGMENT(struct MemorySegment* current, struct MemorySegment* previous, char* curr_path, void* args);
108 |
109 | // bool findFileInMemory(void** result_base, void** result_end, const char* path);
110 |
111 | bool findSpecialFileInMemory(void** result_base, void** result_end,
112 | IS_START_SEGMENT isStartSegment,
113 | IS_CONTINUATION_SEGMENT isContinuationSegment,
114 | void* startArgs, void* contArgs);
115 |
116 | bool findElfFile(struct MemoryMappedFile* result, const char* path);
117 | bool findOatFile(struct MemoryMappedFile* result, const char* path);
118 | /*
119 | * Fills in the array of MemorySegment struct with information about all the segments in memory
120 | * containing a file mapping of the file specified by the given path. Fills up to
121 | * max_result_entries elements in the specified array, and returns the absolute number of elements
122 | * filled.
123 |
124 | uint32_t findFileSegmentsInMemory(struct MemorySegment* result_array, uint32_t max_result_entries, const char* path);
125 |
126 | void logFileSegmentsInMemory(const char* path); */
127 |
128 | #endif //NDKTEST_MEMORY_MAP_LOOKUP_H
129 |
--------------------------------------------------------------------------------
/libAndroidRuntime/src/main/jni/leb128.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Lukas Dresel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | //
18 | // Created by Lukas on 8/23/2015.
19 | //
20 |
21 |
22 | #ifdef __cplusplus
23 | extern "C" {
24 | #endif
25 |
26 |
27 | #include "leb128.h"
28 |
29 | #include
30 |
31 | #include
32 | #include
33 |
34 | // Reads an unsigned LEB128 value, updating the given pointer to point
35 | // just past the end of the read value. This function tolerates
36 | // non-zero high-order bits in the fifth encoded byte.
37 | uint32_t DecodeUnsignedLeb128(const uint8_t **data) {
38 | const uint8_t *ptr = *data;
39 | int result = *(ptr++);
40 | if (UNLIKELY(result > 0x7f)) {
41 | int cur = *(ptr++);
42 | result = (result & 0x7f) | ((cur & 0x7f) << 7);
43 | if (cur > 0x7f) {
44 | cur = *(ptr++);
45 | result |= (cur & 0x7f) << 14;
46 | if (cur > 0x7f) {
47 | cur = *(ptr++);
48 | result |= (cur & 0x7f) << 21;
49 | if (cur > 0x7f) {
50 | // Note: We don't check to see if cur is out of range here,
51 | // meaning we tolerate garbage in the four high-order bits.
52 | cur = *(ptr++);
53 | result |= cur << 28;
54 | }
55 | }
56 | }
57 | }
58 | *data = ptr;
59 | return (uint32_t) result;
60 | }
61 |
62 | // Reads an unsigned LEB128 + 1 value. updating the given pointer to point
63 | // just past the end of the read value. This function tolerates
64 | // non-zero high-order bits in the fifth encoded byte.
65 | // It is possible for this function to return -1.
66 | int32_t DecodeUnsignedLeb128P1(const uint8_t **data) {
67 | return DecodeUnsignedLeb128(data) - 1;
68 | }
69 |
70 | // Reads a signed LEB128 value, updating the given pointer to point
71 | // just past the end of the read value. This function tolerates
72 | // non-zero high-order bits in the fifth encoded byte.
73 | int32_t DecodeSignedLeb128(const uint8_t **data) {
74 | const uint8_t *ptr = *data;
75 | int32_t result = *(ptr++);
76 | if (result <= 0x7f) {
77 | result = (result << 25) >> 25;
78 | } else {
79 | int cur = *(ptr++);
80 | result = (result & 0x7f) | ((cur & 0x7f) << 7);
81 | if (cur <= 0x7f) {
82 | result = (result << 18) >> 18;
83 | } else {
84 | cur = *(ptr++);
85 | result |= (cur & 0x7f) << 14;
86 | if (cur <= 0x7f) {
87 | result = (result << 11) >> 11;
88 | } else {
89 | cur = *(ptr++);
90 | result |= (cur & 0x7f) << 21;
91 | if (cur <= 0x7f) {
92 | result = (result << 4) >> 4;
93 | } else {
94 | // Note: We don't check to see if cur is out of range here,
95 | // meaning we tolerate garbage in the four high-order bits.
96 | cur = *(ptr++);
97 | result |= cur << 28;
98 | }
99 | }
100 | }
101 | }
102 | *data = ptr;
103 | return result;
104 | }
105 |
106 | // Returns the number of bytes needed to encode the value in unsigned LEB128.
107 | uint32_t UnsignedLeb128Size(uint32_t data) {
108 | // bits_to_encode = (data != 0) ? 32 - CLZ(x) : 1 // 32 - CLZ(data | 1)
109 | // bytes = ceil(bits_to_encode / 7.0); // (6 + bits_to_encode) / 7
110 | uint32_t x = 6 + 32 - COUNT_32BIT_LEADING_ZEROS(data | 1);
111 | // Division by 7 is done by (x * 37) >> 8 where 37 = ceil(256 / 7).
112 | // This works for 0 <= x < 256 / (7 * 37 - 256), i.e. 0 <= x <= 85.
113 | return (x * 37) >> 8;
114 | }
115 |
116 | // Returns the number of bytes needed to encode the value in unsigned LEB128.
117 | uint32_t SignedLeb128Size(int32_t data) {
118 | // Like UnsignedLeb128Size(), but we need one bit beyond the highest bit that differs from sign.
119 | data = data ^ (data >> 31);
120 | uint32_t x =
121 | 1 /* we need to encode the sign bit */ + 6 + 32 - COUNT_32BIT_LEADING_ZEROS(data | 1);
122 | return (x * 37) >> 8;
123 | }
124 |
125 | uint8_t *EncodeUnsignedLeb128(uint8_t *dest, uint32_t value) {
126 | uint8_t out = (uint8_t) (value & 0x7f);
127 | value >>= 7;
128 | while (value != 0) {
129 | *dest++ = (uint8_t) (out | 0x80);
130 | out = (uint8_t) (value & 0x7f);
131 | value >>= 7;
132 | }
133 | *dest++ = out;
134 | return dest;
135 | }
136 |
137 | uint8_t *EncodeSignedLeb128(uint8_t *dest, int32_t value) {
138 | uint32_t extra_bits = (uint32_t) (value ^ (value >> 31)) >> 6;
139 | uint8_t out = (uint8_t) (value & 0x7f);
140 | while (extra_bits != 0u) {
141 | *dest++ = (uint8_t) (out | 0x80);
142 | value >>= 7;
143 | out = (uint8_t) (value & 0x7f);
144 | extra_bits >>= 7;
145 | }
146 | *dest++ = out;
147 | return dest;
148 | }
149 |
150 |
151 | #ifdef __cplusplus
152 | }
153 | #endif
154 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------