1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 <sys/stat.h>
18 #include <stdio.h>
19 #include <string.h>
20 
21 #include <atomic>
22 #include <string>
23 
24 #include <jni.h>
25 #include <nativehelper/JNIHelp.h>
26 #include <nativehelper/ScopedUtfChars.h>
27 
IsX86BuildArch()28 constexpr bool IsX86BuildArch() {
29 #if defined(__i386__)
30   return true;
31 #elif defined(__x86_64__)
32   return true;
33 #else
34   return false;
35 #endif
36 }
37 
IsArmBuildArch()38 constexpr bool IsArmBuildArch() {
39 #if defined(__arm__)
40   return true;
41 #elif defined(__aarch64__)
42   return true;
43 #else
44   return false;
45 #endif
46 }
47 
48 extern "C"
Java_libcore_libcore_util_NativeAllocationRegistryTest_isNativeBridgedABI(JNIEnv *,jclass)49 jboolean Java_libcore_libcore_util_NativeAllocationRegistryTest_isNativeBridgedABI(JNIEnv*, jclass) {
50   FILE* fp = popen("uname -m", "re");
51   char buf[128];
52   memset(buf, '\0', sizeof(buf));
53   char* str = fgets(buf, sizeof(buf), fp);
54   pclose(fp);
55   if (!str) {
56     // Assume no native bridge if cannot do uname.
57     return static_cast<jboolean>(false);
58   }
59 
60   std::string uname_string = buf;
61   bool is_native_bridged_abi;
62   if (IsX86BuildArch()) {
63     is_native_bridged_abi = uname_string.find("86") == std::string::npos;
64   } else if (IsArmBuildArch()) {
65     is_native_bridged_abi = uname_string.find("arm") == std::string::npos &&
66         uname_string.find("aarch64") == std::string::npos;
67   } else {
68     is_native_bridged_abi = false;
69   }
70   return static_cast<jboolean>(is_native_bridged_abi);
71 }
72 
73 std::atomic<uint64_t> gNumNativeBytesAllocated = 0;
74 
finalize(uint64_t * ptr)75 static void finalize(uint64_t* ptr) {
76   gNumNativeBytesAllocated -= *ptr;
77   delete ptr;
78 }
79 
80 extern "C"
Java_libcore_libcore_util_NativeAllocationRegistryTest_getNativeFinalizer(JNIEnv *,jclass)81 jlong Java_libcore_libcore_util_NativeAllocationRegistryTest_getNativeFinalizer(JNIEnv*, jclass) {
82   return static_cast<jlong>(reinterpret_cast<uintptr_t>(&finalize));
83 }
84 
85 extern "C"
Java_libcore_libcore_util_NativeAllocationRegistryTest_doNativeAllocation(JNIEnv *,jclass,jlong size)86 jlong Java_libcore_libcore_util_NativeAllocationRegistryTest_doNativeAllocation(JNIEnv*,
87                                                                         jclass,
88                                                                         jlong size) {
89   gNumNativeBytesAllocated += size;
90 
91   // The actual allocation is a pointer to the pretend size of the allocation.
92   uint64_t* ptr = new uint64_t;
93   *ptr = static_cast<uint64_t>(size);
94   return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr));
95 }
96 
97 extern "C"
Java_libcore_libcore_util_NativeAllocationRegistryTest_getNumNativeBytesAllocated(JNIEnv *,jclass)98 jlong Java_libcore_libcore_util_NativeAllocationRegistryTest_getNumNativeBytesAllocated(JNIEnv*, jclass) {
99   return static_cast<jlong>(gNumNativeBytesAllocated);
100 }
101