1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* java.lang.Class
*/
#include "Dalvik.h"
#include "native/InternalNativePriv.h"
/*
* public static void arraycopy(Object src, int srcPos, Object dest,
* int destPos, int length)
*
* The description of this function is long, and describes a multitude
* of checks and exceptions.
*/
static void Dalvik_java_lang_System_arraycopy(const u4* args, JValue* pResult)
{
void* (*copyFunc)(void *dest, const void *src, size_t n);
ArrayObject* srcArray;
ArrayObject* dstArray;
ClassObject* srcClass;
ClassObject* dstClass;
int srcPos, dstPos, length;
char srcType, dstType;
bool srcPrim, dstPrim;
srcArray = (ArrayObject*) args[0];
srcPos = args[1];
dstArray = (ArrayObject*) args[2];
dstPos = args[3];
length = args[4];
if (srcArray == dstArray)
copyFunc = memmove; /* might overlap */
else
copyFunc = memcpy; /* can't overlap, use faster func */
/* check for null or bad pointer */
if (!dvmValidateObject((Object*)srcArray) ||
!dvmValidateObject((Object*)dstArray))
{
assert(dvmCheckException(dvmThreadSelf()));
RETURN_VOID();
}
/* make sure it's an array */
if (!dvmIsArray(srcArray) || !dvmIsArray(dstArray)) {
dvmThrowException("Ljava/lang/ArrayStoreException;", NULL);
RETURN_VOID();
}
// avoid int overflow
if (srcPos < 0 || dstPos < 0 || length < 0 ||
srcPos > (int) srcArray->length - length ||
dstPos > (int) dstArray->length - length)
{
dvmThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", NULL);
RETURN_VOID();
}
srcClass = srcArray->obj.clazz;
dstClass = dstArray->obj.clazz;
srcType = srcClass->descriptor[1];
dstType = dstClass->descriptor[1];
/*
* If one of the arrays holds a primitive type, the other array must
* hold the same type.
*/
srcPrim = (srcType != '[' && srcType != 'L');
dstPrim = (dstType != '[' && dstType != 'L');
if (srcPrim || dstPrim) {
int width;
if (srcPrim != dstPrim || srcType != dstType) {
dvmThrowException("Ljava/lang/ArrayStoreException;", NULL);
RETURN_VOID();
}
switch (srcClass->descriptor[1]) {
case 'B':
case 'Z':
width = 1;
break;
case 'C':
case 'S':
width = 2;
break;
case 'F':
case 'I':
width = 4;
break;
case 'D':
case 'J':
width = 8;
break;
default: /* 'V' or something weird */
LOGE("Weird array type '%s'\n", srcClass->descriptor);
assert(false);
width = 0;
break;
}
if (false) LOGVV("arraycopy prim dst=%p %d src=%p %d len=%d\n",
dstArray->contents, dstPos * width,
srcArray->contents, srcPos * width,
length * width);
(*copyFunc)((u1*)dstArray->contents + dstPos * width,
(const u1*)srcArray->contents + srcPos * width,
length * width);
} else {
/*
* Neither class is primitive. See if elements in "src" are instances
* of elements in "dst" (e.g. copy String to String or String to
* Object).
*/
int width = sizeof(Object*);
if (srcClass->arrayDim == dstClass->arrayDim &&
dvmInstanceof(srcClass, dstClass))
{
/*
* "dst" can hold "src"; copy the whole thing.
*/
if (false) LOGVV("arraycopy ref dst=%p %d src=%p %d len=%d\n",
dstArray->contents, dstPos * width,
srcArray->contents, srcPos * width,
length * width);
(*copyFunc)((u1*)dstArray->contents + dstPos * width,
(const u1*)srcArray->contents + srcPos * width,
length * width);
} else {
/*
* The arrays are not fundamentally compatible. However, we may
* still be able to do this if the destination object is compatible
* (e.g. copy Object to String, but the Object being copied is
* actually a String). We need to copy elements one by one until
* something goes wrong.
*
* Because of overlapping moves, what we really want to do is
* compare the types and count up how many we can move, then call
* memmove() to shift the actual data. If we just start from the
* front we could do a smear rather than a move.
*/
Object** srcObj;
Object** dstObj;
int copyCount;
ClassObject* clazz = NULL;
srcObj = ((Object**) srcArray->contents) + srcPos;
dstObj = ((Object**) dstArray->contents) + dstPos;
if (length > 0 && srcObj[0] != NULL)
{
clazz = srcObj[0]->clazz;
if (!dvmCanPutArrayElement(clazz, dstClass))
clazz = NULL;
}
for (copyCount = 0; copyCount < length; copyCount++)
{
if (srcObj[copyCount] != NULL &&
srcObj[copyCount]->clazz != clazz &&
!dvmCanPutArrayElement(srcObj[copyCount]->clazz, dstClass))
{
/* can't put this element into the array */
break;
}
}
if (false) LOGVV("arraycopy iref dst=%p %d src=%p %d count=%d of %d\n",
dstArray->contents, dstPos * width,
srcArray->contents, srcPos * width,
copyCount, length);
(*copyFunc)((u1*)dstArray->contents + dstPos * width,
(const u1*)srcArray->contents + srcPos * width,
copyCount * width);
if (copyCount != length) {
dvmThrowException("Ljava/lang/ArrayStoreException;", NULL);
RETURN_VOID();
}
}
}
RETURN_VOID();
}
/*
* static long currentTimeMillis()
*
* Current time, in miliseconds. This doesn't need to be internal to the
* VM, but we're already handling java.lang.System here.
*/
static void Dalvik_java_lang_System_currentTimeMillis(const u4* args,
JValue* pResult)
{
struct timeval tv;
UNUSED_PARAMETER(args);
gettimeofday(&tv, (struct timezone *) NULL);
long long when = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
RETURN_LONG(when);
}
/*
* static long nanoTime()
*
* Current monotonically-increasing time, in nanoseconds. This doesn't
* need to be internal to the VM, but we're already handling
* java.lang.System here.
*/
static void Dalvik_java_lang_System_nanoTime(const u4* args, JValue* pResult)
{
UNUSED_PARAMETER(args);
u8 when = dvmGetRelativeTimeNsec();
RETURN_LONG(when);
}
/*
* static int identityHashCode(Object x)
*
* Returns that hash code that the default hashCode()
* method would return for "x", even if "x"s class
* overrides hashCode().
*/
static void Dalvik_java_lang_System_identityHashCode(const u4* args,
JValue* pResult)
{
Object* thisPtr = (Object*) args[0];
RETURN_INT(dvmGetObjectHashCode(thisPtr));
}
/*
* public static String mapLibraryName(String libname)
*/
static void Dalvik_java_lang_System_mapLibraryName(const u4* args,
JValue* pResult)
{
StringObject* nameObj = (StringObject*) args[0];
StringObject* result = NULL;
char* name;
char* mappedName;
if (nameObj == NULL) {
dvmThrowException("Ljava/lang/NullPointerException;", NULL);
RETURN_VOID();
}
name = dvmCreateCstrFromString(nameObj);
mappedName = dvmCreateSystemLibraryName(name);
if (mappedName != NULL) {
result = dvmCreateStringFromCstr(mappedName, ALLOC_DEFAULT);
dvmReleaseTrackedAlloc((Object*) result, NULL);
}
free(name);
free(mappedName);
RETURN_PTR(result);
}
const DalvikNativeMethod dvm_java_lang_System[] = {
{ "arraycopy", "(Ljava/lang/Object;ILjava/lang/Object;II)V",
Dalvik_java_lang_System_arraycopy },
{ "currentTimeMillis", "()J",
Dalvik_java_lang_System_currentTimeMillis },
{ "nanoTime", "()J",
Dalvik_java_lang_System_nanoTime },
{ "identityHashCode", "(Ljava/lang/Object;)I",
Dalvik_java_lang_System_identityHashCode },
{ "mapLibraryName", "(Ljava/lang/String;)Ljava/lang/String;",
Dalvik_java_lang_System_mapLibraryName },
{ NULL, NULL, NULL },
};
|