1 /*
2  * Copyright (C) 2007 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 #define LOG_TAG "Memory"
18 
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/mman.h>
23 
24 #include <nativehelper/JNIHelp.h>
25 
26 #include <nativehelper/ScopedPrimitiveArray.h>
27 #include <nativehelper/jni_macros.h>
28 
29 #include "JniConstants.h"
30 #include "Portability.h"
31 #include "ScopedBytes.h"
32 
33 // Use packed structures for access to unaligned data on targets with alignment restrictions.
34 // The compiler will generate appropriate code to access these structures without
35 // generating alignment exceptions.
get_unaligned(const T * address)36 template <typename T> static inline T get_unaligned(const T* address) {
37     struct unaligned { T v; } __attribute__ ((packed));
38     const unaligned* p = reinterpret_cast<const unaligned*>(address);
39     return p->v;
40 }
41 
put_unaligned(T * address,T v)42 template <typename T> static inline void put_unaligned(T* address, T v) {
43     struct unaligned { T v; } __attribute__ ((packed));
44     unaligned* p = reinterpret_cast<unaligned*>(address);
45     p->v = v;
46 }
47 
cast(jlong address)48 template <typename T> static T cast(jlong address) {
49     return reinterpret_cast<T>(static_cast<uintptr_t>(address));
50 }
51 
52 // Byte-swap 2 jshort values packed in a jint.
bswap_2x16(jint v)53 static inline jint bswap_2x16(jint v) {
54     // v is initially ABCD
55 #if defined(__mips__) && defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
56     __asm__ volatile ("wsbh %0, %0" : "+r" (v));  // v=BADC
57 #else
58     v = bswap_32(v);                              // v=DCBA
59     v = (v << 16) | ((v >> 16) & 0xffff);         // v=BADC
60 #endif
61     return v;
62 }
63 
swapShorts(jshort * dstShorts,const jshort * srcShorts,size_t count)64 static inline void swapShorts(jshort* dstShorts, const jshort* srcShorts, size_t count) {
65     // Do 32-bit swaps as long as possible...
66     jint* dst = reinterpret_cast<jint*>(dstShorts);
67     const jint* src = reinterpret_cast<const jint*>(srcShorts);
68     for (size_t i = 0; i < count / 2; ++i) {
69         jint v = get_unaligned<jint>(src++);
70         put_unaligned<jint>(dst++, bswap_2x16(v));
71     }
72     if ((count % 2) != 0) {
73       jshort v = get_unaligned<jshort>(reinterpret_cast<const jshort*>(src));
74       put_unaligned<jshort>(reinterpret_cast<jshort*>(dst), bswap_16(v));
75     }
76 }
77 
swapInts(jint * dstInts,const jint * srcInts,size_t count)78 static inline void swapInts(jint* dstInts, const jint* srcInts, size_t count) {
79     for (size_t i = 0; i < count; ++i) {
80         jint v = get_unaligned<int>(srcInts++);
81         put_unaligned<jint>(dstInts++, bswap_32(v));
82     }
83 }
84 
swapLongs(jlong * dstLongs,const jlong * srcLongs,size_t count)85 static inline void swapLongs(jlong* dstLongs, const jlong* srcLongs, size_t count) {
86     jint* dst = reinterpret_cast<jint*>(dstLongs);
87     const jint* src = reinterpret_cast<const jint*>(srcLongs);
88     for (size_t i = 0; i < count; ++i) {
89         jint v1 = get_unaligned<jint>(src++);
90         jint v2 = get_unaligned<jint>(src++);
91         put_unaligned<jint>(dst++, bswap_32(v2));
92         put_unaligned<jint>(dst++, bswap_32(v1));
93     }
94 }
95 
Memory_memmove(JNIEnv * env,jclass,jobject dstObject,jint dstOffset,jobject srcObject,jint srcOffset,jlong length)96 static void Memory_memmove(JNIEnv* env, jclass, jobject dstObject, jint dstOffset, jobject srcObject, jint srcOffset, jlong length) {
97     ScopedBytesRW dstBytes(env, dstObject);
98     if (dstBytes.get() == NULL) {
99         return;
100     }
101     ScopedBytesRO srcBytes(env, srcObject);
102     if (srcBytes.get() == NULL) {
103         return;
104     }
105     memmove(dstBytes.get() + dstOffset, srcBytes.get() + srcOffset, length);
106 }
107 
Memory_peekByte(JNIEnv *,jclass,jlong srcAddress)108 static jbyte Memory_peekByte(JNIEnv*, jclass, jlong srcAddress) {
109     return *cast<const jbyte*>(srcAddress);
110 }
111 
Memory_peekByteArray(JNIEnv * env,jclass,jlong srcAddress,jbyteArray dst,jint dstOffset,jint byteCount)112 static void Memory_peekByteArray(JNIEnv* env, jclass, jlong srcAddress, jbyteArray dst, jint dstOffset, jint byteCount) {
113     env->SetByteArrayRegion(dst, dstOffset, byteCount, cast<const jbyte*>(srcAddress));
114 }
115 
116 // Implements the peekXArray methods:
117 // - For unswapped access, we just use the JNI SetXArrayRegion functions.
118 // - For swapped access, we use GetXArrayElements and our own copy-and-swap routines.
119 //   GetXArrayElements is disproportionately cheap on Dalvik because it doesn't copy (as opposed
120 //   to Hotspot, which always copies). The SWAP_FN copies and swaps in one pass, which is cheaper
121 //   than copying and then swapping in a second pass. Depending on future VM/GC changes, the
122 //   swapped case might need to be revisited.
123 #define PEEKER(SCALAR_TYPE, JNI_NAME, SWAP_TYPE, SWAP_FN) { \
124     if (swap) { \
125         Scoped ## JNI_NAME ## ArrayRW elements(env, dst); \
126         if (elements.get() == NULL) { \
127             return; \
128         } \
129         const SWAP_TYPE* src = cast<const SWAP_TYPE*>(srcAddress); \
130         SWAP_FN(reinterpret_cast<SWAP_TYPE*>(elements.get()) + dstOffset, src, count); /*NOLINT*/ \
131     } else { \
132         const SCALAR_TYPE* src = cast<const SCALAR_TYPE*>(srcAddress); \
133         env->Set ## JNI_NAME ## ArrayRegion(dst, dstOffset, count, src); \
134     } \
135 }
136 
Memory_peekCharArray(JNIEnv * env,jclass,jlong srcAddress,jcharArray dst,jint dstOffset,jint count,jboolean swap)137 static void Memory_peekCharArray(JNIEnv* env, jclass, jlong srcAddress, jcharArray dst, jint dstOffset, jint count, jboolean swap) {
138     PEEKER(jchar, Char, jshort, swapShorts);
139 }
140 
Memory_peekDoubleArray(JNIEnv * env,jclass,jlong srcAddress,jdoubleArray dst,jint dstOffset,jint count,jboolean swap)141 static void Memory_peekDoubleArray(JNIEnv* env, jclass, jlong srcAddress, jdoubleArray dst, jint dstOffset, jint count, jboolean swap) {
142     PEEKER(jdouble, Double, jlong, swapLongs);
143 }
144 
Memory_peekFloatArray(JNIEnv * env,jclass,jlong srcAddress,jfloatArray dst,jint dstOffset,jint count,jboolean swap)145 static void Memory_peekFloatArray(JNIEnv* env, jclass, jlong srcAddress, jfloatArray dst, jint dstOffset, jint count, jboolean swap) {
146     PEEKER(jfloat, Float, jint, swapInts);
147 }
148 
Memory_peekIntArray(JNIEnv * env,jclass,jlong srcAddress,jintArray dst,jint dstOffset,jint count,jboolean swap)149 static void Memory_peekIntArray(JNIEnv* env, jclass, jlong srcAddress, jintArray dst, jint dstOffset, jint count, jboolean swap) {
150     PEEKER(jint, Int, jint, swapInts);
151 }
152 
Memory_peekLongArray(JNIEnv * env,jclass,jlong srcAddress,jlongArray dst,jint dstOffset,jint count,jboolean swap)153 static void Memory_peekLongArray(JNIEnv* env, jclass, jlong srcAddress, jlongArray dst, jint dstOffset, jint count, jboolean swap) {
154     PEEKER(jlong, Long, jlong, swapLongs);
155 }
156 
Memory_peekShortArray(JNIEnv * env,jclass,jlong srcAddress,jshortArray dst,jint dstOffset,jint count,jboolean swap)157 static void Memory_peekShortArray(JNIEnv* env, jclass, jlong srcAddress, jshortArray dst, jint dstOffset, jint count, jboolean swap) {
158     PEEKER(jshort, Short, jshort, swapShorts);
159 }
160 
Memory_pokeByte(JNIEnv *,jclass,jlong dstAddress,jbyte value)161 static void Memory_pokeByte(JNIEnv*, jclass, jlong dstAddress, jbyte value) {
162     *cast<jbyte*>(dstAddress) = value;
163 }
164 
Memory_pokeByteArray(JNIEnv * env,jclass,jlong dstAddress,jbyteArray src,jint offset,jint length)165 static void Memory_pokeByteArray(JNIEnv* env, jclass, jlong dstAddress, jbyteArray src, jint offset, jint length) {
166     env->GetByteArrayRegion(src, offset, length, cast<jbyte*>(dstAddress));
167 }
168 
169 // Implements the pokeXArray methods:
170 // - For unswapped access, we just use the JNI GetXArrayRegion functions.
171 // - For swapped access, we use GetXArrayElements and our own copy-and-swap routines.
172 //   GetXArrayElements is disproportionately cheap on Dalvik because it doesn't copy (as opposed
173 //   to Hotspot, which always copies). The SWAP_FN copies and swaps in one pass, which is cheaper
174 //   than copying and then swapping in a second pass. Depending on future VM/GC changes, the
175 //   swapped case might need to be revisited.
176 #define POKER(SCALAR_TYPE, JNI_NAME, SWAP_TYPE, SWAP_FN) { \
177     if (swap) { \
178         Scoped ## JNI_NAME ## ArrayRO elements(env, src); \
179         if (elements.get() == NULL) { \
180             return; \
181         } \
182         const SWAP_TYPE* src = reinterpret_cast<const SWAP_TYPE*>(elements.get()) + srcOffset; \
183         SWAP_FN(cast<SWAP_TYPE*>(dstAddress), src, count); /*NOLINT*/ \
184     } else { \
185         env->Get ## JNI_NAME ## ArrayRegion(src, srcOffset, count, cast<SCALAR_TYPE*>(dstAddress)); /*NOLINT*/ \
186     } \
187 }
188 
Memory_pokeCharArray(JNIEnv * env,jclass,jlong dstAddress,jcharArray src,jint srcOffset,jint count,jboolean swap)189 static void Memory_pokeCharArray(JNIEnv* env, jclass, jlong dstAddress, jcharArray src, jint srcOffset, jint count, jboolean swap) {
190     POKER(jchar, Char, jshort, swapShorts);
191 }
192 
Memory_pokeDoubleArray(JNIEnv * env,jclass,jlong dstAddress,jdoubleArray src,jint srcOffset,jint count,jboolean swap)193 static void Memory_pokeDoubleArray(JNIEnv* env, jclass, jlong dstAddress, jdoubleArray src, jint srcOffset, jint count, jboolean swap) {
194     POKER(jdouble, Double, jlong, swapLongs);
195 }
196 
Memory_pokeFloatArray(JNIEnv * env,jclass,jlong dstAddress,jfloatArray src,jint srcOffset,jint count,jboolean swap)197 static void Memory_pokeFloatArray(JNIEnv* env, jclass, jlong dstAddress, jfloatArray src, jint srcOffset, jint count, jboolean swap) {
198     POKER(jfloat, Float, jint, swapInts);
199 }
200 
Memory_pokeIntArray(JNIEnv * env,jclass,jlong dstAddress,jintArray src,jint srcOffset,jint count,jboolean swap)201 static void Memory_pokeIntArray(JNIEnv* env, jclass, jlong dstAddress, jintArray src, jint srcOffset, jint count, jboolean swap) {
202     POKER(jint, Int, jint, swapInts);
203 }
204 
Memory_pokeLongArray(JNIEnv * env,jclass,jlong dstAddress,jlongArray src,jint srcOffset,jint count,jboolean swap)205 static void Memory_pokeLongArray(JNIEnv* env, jclass, jlong dstAddress, jlongArray src, jint srcOffset, jint count, jboolean swap) {
206     POKER(jlong, Long, jlong, swapLongs);
207 }
208 
Memory_pokeShortArray(JNIEnv * env,jclass,jlong dstAddress,jshortArray src,jint srcOffset,jint count,jboolean swap)209 static void Memory_pokeShortArray(JNIEnv* env, jclass, jlong dstAddress, jshortArray src, jint srcOffset, jint count, jboolean swap) {
210     POKER(jshort, Short, jshort, swapShorts);
211 }
212 
Memory_peekShortNative(JNIEnv *,jclass,jlong srcAddress)213 static jshort Memory_peekShortNative(JNIEnv*, jclass, jlong srcAddress) {
214     return get_unaligned<jshort>(cast<const jshort*>(srcAddress));
215 }
216 
Memory_pokeShortNative(JNIEnv *,jclass,jlong dstAddress,jshort value)217 static void Memory_pokeShortNative(JNIEnv*, jclass, jlong dstAddress, jshort value) {
218     put_unaligned<jshort>(cast<jshort*>(dstAddress), value);
219 }
220 
Memory_peekIntNative(JNIEnv *,jclass,jlong srcAddress)221 static jint Memory_peekIntNative(JNIEnv*, jclass, jlong srcAddress) {
222     return get_unaligned<jint>(cast<const jint*>(srcAddress));
223 }
224 
Memory_pokeIntNative(JNIEnv *,jclass,jlong dstAddress,jint value)225 static void Memory_pokeIntNative(JNIEnv*, jclass, jlong dstAddress, jint value) {
226     put_unaligned<jint>(cast<jint*>(dstAddress), value);
227 }
228 
Memory_peekLongNative(JNIEnv *,jclass,jlong srcAddress)229 static jlong Memory_peekLongNative(JNIEnv*, jclass, jlong srcAddress) {
230     return get_unaligned<jlong>(cast<const jlong*>(srcAddress));
231 }
232 
Memory_pokeLongNative(JNIEnv *,jclass,jlong dstAddress,jlong value)233 static void Memory_pokeLongNative(JNIEnv*, jclass, jlong dstAddress, jlong value) {
234     put_unaligned<jlong>(cast<jlong*>(dstAddress), value);
235 }
236 
unsafeBulkCopy(jbyte * dst,const jbyte * src,jint byteCount,jint sizeofElement,jboolean swap)237 static void unsafeBulkCopy(jbyte* dst, const jbyte* src, jint byteCount,
238         jint sizeofElement, jboolean swap) {
239     if (!swap) {
240         memcpy(dst, src, byteCount);
241         return;
242     }
243 
244     if (sizeofElement == 2) {
245         jshort* dstShorts = reinterpret_cast<jshort*>(dst);
246         const jshort* srcShorts = reinterpret_cast<const jshort*>(src);
247         swapShorts(dstShorts, srcShorts, byteCount / 2);
248     } else if (sizeofElement == 4) {
249         jint* dstInts = reinterpret_cast<jint*>(dst);
250         const jint* srcInts = reinterpret_cast<const jint*>(src);
251         swapInts(dstInts, srcInts, byteCount / 4);
252     } else if (sizeofElement == 8) {
253         jlong* dstLongs = reinterpret_cast<jlong*>(dst);
254         const jlong* srcLongs = reinterpret_cast<const jlong*>(src);
255         swapLongs(dstLongs, srcLongs, byteCount / 8);
256     }
257 }
258 
Memory_unsafeBulkGet(JNIEnv * env,jclass,jobject dstObject,jint dstOffset,jint byteCount,jbyteArray srcArray,jint srcOffset,jint sizeofElement,jboolean swap)259 static void Memory_unsafeBulkGet(JNIEnv* env, jclass, jobject dstObject, jint dstOffset,
260         jint byteCount, jbyteArray srcArray, jint srcOffset, jint sizeofElement, jboolean swap) {
261     ScopedByteArrayRO srcBytes(env, srcArray);
262     if (srcBytes.get() == NULL) {
263         return;
264     }
265     jarray dstArray = reinterpret_cast<jarray>(dstObject);
266     jbyte* dstBytes = reinterpret_cast<jbyte*>(env->GetPrimitiveArrayCritical(dstArray, NULL));
267     if (dstBytes == NULL) {
268         return;
269     }
270     jbyte* dst = dstBytes + dstOffset*sizeofElement;
271     const jbyte* src = srcBytes.get() + srcOffset;
272     unsafeBulkCopy(dst, src, byteCount, sizeofElement, swap);
273     env->ReleasePrimitiveArrayCritical(dstArray, dstBytes, 0);
274 }
275 
Memory_unsafeBulkPut(JNIEnv * env,jclass,jbyteArray dstArray,jint dstOffset,jint byteCount,jobject srcObject,jint srcOffset,jint sizeofElement,jboolean swap)276 static void Memory_unsafeBulkPut(JNIEnv* env, jclass, jbyteArray dstArray, jint dstOffset,
277         jint byteCount, jobject srcObject, jint srcOffset, jint sizeofElement, jboolean swap) {
278     ScopedByteArrayRW dstBytes(env, dstArray);
279     if (dstBytes.get() == NULL) {
280         return;
281     }
282     jarray srcArray = reinterpret_cast<jarray>(srcObject);
283     jbyte* srcBytes = reinterpret_cast<jbyte*>(env->GetPrimitiveArrayCritical(srcArray, NULL));
284     if (srcBytes == NULL) {
285         return;
286     }
287     jbyte* dst = dstBytes.get() + dstOffset;
288     const jbyte* src = srcBytes + srcOffset*sizeofElement;
289     unsafeBulkCopy(dst, src, byteCount, sizeofElement, swap);
290     env->ReleasePrimitiveArrayCritical(srcArray, srcBytes, 0);
291 }
292 
293 static JNINativeMethod gMethods[] = {
294     NATIVE_METHOD(Memory, memmove, "(Ljava/lang/Object;ILjava/lang/Object;IJ)V"),
295     FAST_NATIVE_METHOD(Memory, peekByte, "(J)B"),
296     NATIVE_METHOD(Memory, peekByteArray, "(J[BII)V"),
297     NATIVE_METHOD(Memory, peekCharArray, "(J[CIIZ)V"),
298     NATIVE_METHOD(Memory, peekDoubleArray, "(J[DIIZ)V"),
299     NATIVE_METHOD(Memory, peekFloatArray, "(J[FIIZ)V"),
300     FAST_NATIVE_METHOD(Memory, peekIntNative, "(J)I"),
301     NATIVE_METHOD(Memory, peekIntArray, "(J[IIIZ)V"),
302     FAST_NATIVE_METHOD(Memory, peekLongNative, "(J)J"),
303     NATIVE_METHOD(Memory, peekLongArray, "(J[JIIZ)V"),
304     FAST_NATIVE_METHOD(Memory, peekShortNative, "(J)S"),
305     NATIVE_METHOD(Memory, peekShortArray, "(J[SIIZ)V"),
306     FAST_NATIVE_METHOD(Memory, pokeByte, "(JB)V"),
307     NATIVE_METHOD(Memory, pokeByteArray, "(J[BII)V"),
308     NATIVE_METHOD(Memory, pokeCharArray, "(J[CIIZ)V"),
309     NATIVE_METHOD(Memory, pokeDoubleArray, "(J[DIIZ)V"),
310     NATIVE_METHOD(Memory, pokeFloatArray, "(J[FIIZ)V"),
311     FAST_NATIVE_METHOD(Memory, pokeIntNative, "(JI)V"),
312     NATIVE_METHOD(Memory, pokeIntArray, "(J[IIIZ)V"),
313     FAST_NATIVE_METHOD(Memory, pokeLongNative, "(JJ)V"),
314     NATIVE_METHOD(Memory, pokeLongArray, "(J[JIIZ)V"),
315     FAST_NATIVE_METHOD(Memory, pokeShortNative, "(JS)V"),
316     NATIVE_METHOD(Memory, pokeShortArray, "(J[SIIZ)V"),
317     NATIVE_METHOD(Memory, unsafeBulkGet, "(Ljava/lang/Object;II[BIIZ)V"),
318     NATIVE_METHOD(Memory, unsafeBulkPut, "([BIILjava/lang/Object;IIZ)V"),
319 };
register_libcore_io_Memory(JNIEnv * env)320 void register_libcore_io_Memory(JNIEnv* env) {
321     jniRegisterNativeMethods(env, "libcore/io/Memory", gMethods, NELEM(gMethods));
322 }
323