-
Notifications
You must be signed in to change notification settings - Fork 3
/
FalsoJNI_ImplBridge.c
590 lines (482 loc) · 16.1 KB
/
FalsoJNI_ImplBridge.c
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
* FalsoJNI_ImplBridge.c
*
* Fake Java Native Interface, providing JavaVM and JNIEnv objects.
*
* Copyright (C) 2021 Andy Nguyen
* Copyright (C) 2021 Rinnegatamante
* Copyright (C) 2022 Volodymyr Atamanenko
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include "FalsoJNI_Impl.h"
#include "FalsoJNI_Logger.h"
#include "FalsoJNI_ImplBridge.h"
#include <string.h>
#include <malloc.h>
#include <pthread.h>
JavaDynArray* javaDynArrays = NULL;
pthread_mutex_t* javaDynArrays_mutex = NULL;
size_t javaDynArrays_free = 0;
size_t javaDynArrays_taken = 0;
JavaDynArray** javaDynArrays_static = NULL;
size_t javaDynArrays_static_count = 0;
size_t javaDynArrays_static_capacity = 16;
jfieldID getFieldIdByName(const char* name) {
for (int i = 0; i < nameToFieldId_size() / sizeof(NameToFieldID); i++) {
if (strcmp(name, nameToFieldId[i].name) == 0) {
return (jfieldID) nameToFieldId[i].id;
}
}
fjni_logv_warn("Unknown field name \"%s\"", name);
return NULL;
}
const char* fieldTypeToStr(FIELD_TYPE t) {
switch (t) {
case FIELD_TYPE_INT:
return "FIELD_TYPE_INT";
case FIELD_TYPE_OBJECT:
return "FIELD_TYPE_OBJECT";
case FIELD_TYPE_BOOLEAN:
return "FIELD_TYPE_BOOLEAN";
case FIELD_TYPE_BYTE:
return "FIELD_TYPE_BYTE";
case FIELD_TYPE_CHAR:
return "FIELD_TYPE_CHAR";
case FIELD_TYPE_SHORT:
return "FIELD_TYPE_SHORT";
case FIELD_TYPE_LONG:
return "FIELD_TYPE_LONG";
case FIELD_TYPE_FLOAT:
return "FIELD_TYPE_FLOAT";
case FIELD_TYPE_DOUBLE:
return "FIELD_TYPE_DOUBLE";
default:
return "FIELD_TYPE_UNKNOWN";
}
}
jsize getFieldTypeSize(FIELD_TYPE fieldType) {
switch (fieldType) {
case FIELD_TYPE_OBJECT:
return sizeof(jobject);
case FIELD_TYPE_BOOLEAN:
return sizeof(jboolean);
case FIELD_TYPE_BYTE:
return sizeof(jbyte);
case FIELD_TYPE_CHAR:
return sizeof(jchar);
case FIELD_TYPE_SHORT:
return sizeof(jshort);
case FIELD_TYPE_INT:
return sizeof(jint);
case FIELD_TYPE_LONG:
return sizeof(jlong);
case FIELD_TYPE_FLOAT:
return sizeof(jfloat);
case FIELD_TYPE_DOUBLE:
return sizeof(jdouble);
default:
return sizeof(void *);
}
}
jobject getObjectFieldValueById(jfieldID id) {
getFieldValueById(jobject, FIELD_TYPE_OBJECT, FieldsObject, fieldsObject, fieldsObject_size, id, (jobject)0x42424242);
}
jint getIntFieldValueById(jfieldID id) {
getFieldValueById(jint, FIELD_TYPE_INT, FieldsInt, fieldsInt, fieldsInt_size, id, 1);
}
jboolean getBooleanFieldValueById(jfieldID id) {
getFieldValueById(jboolean, FIELD_TYPE_BOOLEAN, FieldsBoolean, fieldsBoolean, fieldsBoolean_size, id, JNI_FALSE);
}
jbyte getByteFieldValueById(jfieldID id) {
getFieldValueById(jbyte, FIELD_TYPE_BYTE, FieldsByte, fieldsByte, fieldsByte_size, id, 'a');
}
jchar getCharFieldValueById(jfieldID id) {
getFieldValueById(jchar, FIELD_TYPE_CHAR, FieldsChar, fieldsChar, fieldsChar_size, id, 'b');
}
jshort getShortFieldValueById(jfieldID id) {
getFieldValueById(jshort, FIELD_TYPE_SHORT, FieldsShort, fieldsShort, fieldsShort_size, id, 1);
}
jlong getLongFieldValueById(jfieldID id) {
getFieldValueById(jlong, FIELD_TYPE_LONG, FieldsLong, fieldsLong, fieldsLong_size, id, 1);
}
jfloat getFloatFieldValueById(jfieldID id) {
getFieldValueById(jfloat, FIELD_TYPE_FLOAT, FieldsFloat, fieldsFloat, fieldsFloat_size, id, 1.0f);
}
jdouble getDoubleFieldValueById(jfieldID id) {
getFieldValueById(jdouble, FIELD_TYPE_DOUBLE, FieldsDouble, fieldsDouble, fieldsDouble_size, id, 1);
}
void setObjectFieldValueById(jfieldID id, jobject value) {
setFieldValueById(jobject, FIELD_TYPE_OBJECT, FieldsObject, fieldsObject, fieldsObject_size, id, value);
}
void setIntFieldValueById(jfieldID id, jint value) {
setFieldValueById(jint, FIELD_TYPE_INT, FieldsInt, fieldsInt, fieldsInt_size, id, value);
}
void setBooleanFieldValueById(jfieldID id, jboolean value) {
setFieldValueById(jboolean, FIELD_TYPE_BOOLEAN, FieldsBoolean, fieldsBoolean, fieldsBoolean_size, id, value);
}
void setByteFieldValueById(jfieldID id, jbyte value) {
setFieldValueById(jbyte, FIELD_TYPE_BYTE, FieldsByte, fieldsByte, fieldsByte_size, id, value);
}
void setCharFieldValueById(jfieldID id, jchar value) {
setFieldValueById(jchar, FIELD_TYPE_CHAR, FieldsChar, fieldsChar, fieldsChar_size, id, value);
}
void setShortFieldValueById(jfieldID id, jshort value) {
setFieldValueById(jshort, FIELD_TYPE_SHORT, FieldsShort, fieldsShort, fieldsShort_size, id, value);
}
void setLongFieldValueById(jfieldID id, jlong value) {
setFieldValueById(jlong, FIELD_TYPE_LONG, FieldsLong, fieldsLong, fieldsLong_size, id, value);
}
void setFloatFieldValueById(jfieldID id, jfloat value) {
setFieldValueById(jfloat, FIELD_TYPE_FLOAT, FieldsFloat, fieldsFloat, fieldsFloat_size, id, value);
}
void setDoubleFieldValueById(jfieldID id, jdouble value) {
setFieldValueById(jdouble, FIELD_TYPE_DOUBLE, FieldsDouble, fieldsDouble, fieldsDouble_size, id, value);
}
jmethodID getMethodIdByName(const char* name) {
for (int i = 0; i < nameToMethodId_size() / sizeof(NameToMethodID); i++) {
if (strcmp(name, nameToMethodId[i].name) == 0) {
return (jmethodID) nameToMethodId[i].id;
}
}
return NULL;
}
jobject methodObjectCall(jmethodID id, va_list args) {
for (int i = 0; i < methodsObject_size() / sizeof(MethodsObject); i++) {
if (methodsObject[i].id == (int)id) {
return methodsObject[i].Method(id, args);
}
}
fjni_logv_warn("method ID %i not found!", (int)id);
return NULL;
}
void methodVoidCall(jmethodID id, va_list args) {
for (int i = 0; i < methodsVoid_size() / sizeof(MethodsVoid); i++) {
if (methodsVoid[i].id == (int)id) {
return methodsVoid[i].Method(id, args);
}
}
fjni_logv_warn("method ID %i not found!", (int)id);
}
jboolean methodBooleanCall(jmethodID id, va_list args) {
for (int i = 0; i < methodsBoolean_size() / sizeof(MethodsBoolean); i++) {
if (methodsBoolean[i].id == (int)id) {
return methodsBoolean[i].Method(id, args);
}
}
fjni_logv_warn("method ID %i not found!", (int)id);
return JNI_FALSE;
}
jbyte methodByteCall(jmethodID id, va_list args) {
for (int i = 0; i < methodsByte_size() / sizeof(MethodsByte); i++) {
if (methodsByte[i].id == (int)id) {
return methodsByte[i].Method(id, args);
}
}
fjni_logv_warn("method ID %i not found!", (int)id);
return 0;
}
jshort methodShortCall(jmethodID id, va_list args) {
for (int i = 0; i < methodsShort_size() / sizeof(MethodsShort); i++) {
if (methodsShort[i].id == (int)id) {
return methodsShort[i].Method(id, args);
}
}
fjni_logv_warn("method ID %i not found!", (int)id);
return 0;
}
jdouble methodDoubleCall(jmethodID id, va_list args) {
for (int i = 0; i < methodsDouble_size() / sizeof(MethodsDouble); i++) {
if (methodsDouble[i].id == (int)id) {
return methodsDouble[i].Method(id, args);
}
}
fjni_logv_warn("method ID %i not found!", (int)id);
return 0;
}
jchar methodCharCall(jmethodID id, va_list args) {
for (int i = 0; i < methodsChar_size() / sizeof(MethodsChar); i++) {
if (methodsChar[i].id == (int)id) {
return methodsChar[i].Method(id, args);
}
}
fjni_logv_warn("method ID %i not found!", (int)id);
return 0;
}
jlong methodLongCall(jmethodID id, va_list args) {
for (int i = 0; i < methodsLong_size() / sizeof(MethodsLong); i++) {
if (methodsLong[i].id == (int)id) {
return methodsLong[i].Method(id, args);
}
}
fjni_logv_warn("method ID %i not found!", (int)id);
return -1;
}
jint methodIntCall(jmethodID id, va_list args) {
for (int i = 0; i < methodsInt_size() / sizeof(MethodsInt); i++) {
if (methodsInt[i].id == (int)id) {
return methodsInt[i].Method(id, args);
}
}
fjni_logv_warn("method ID %i not found!", (int)id);
return -1;
}
jfloat methodFloatCall(jmethodID id, va_list args) {
for (int i = 0; i < methodsFloat_size() / sizeof(MethodsFloat); i++) {
if (methodsFloat[i].id == (int)id) {
return methodsFloat[i].Method(id, args);
}
}
fjni_logv_warn("method ID %i not found!", (int)id);
return -1;
}
void jda_lock() {
if (javaDynArrays_mutex == NULL) {
pthread_mutex_t initTmpNormal;
javaDynArrays_mutex = malloc(sizeof(pthread_mutex_t));
memcpy(javaDynArrays_mutex, &initTmpNormal, sizeof(pthread_mutex_t));
if (pthread_mutex_init(javaDynArrays_mutex, NULL) != 0) {
fjni_log_err("Failed to allocate dynarr mutex");
javaDynArrays_mutex = NULL;
return;
}
}
pthread_mutex_lock(javaDynArrays_mutex);
}
void jda_unlock() {
if (javaDynArrays_mutex != NULL) {
pthread_mutex_unlock(javaDynArrays_mutex);
}
}
jboolean jda_tryinit() {
if (javaDynArrays == NULL) {
javaDynArrays = malloc(16 * sizeof(JavaDynArray));
if (!javaDynArrays)
return JNI_FALSE;
for (int i = 0; i < 16; ++i) {
javaDynArrays[i].array = NULL;
javaDynArrays[i].len = -1;
javaDynArrays[i].type = FIELD_TYPE_UNKNOWN;
}
javaDynArrays_free = 16;
javaDynArrays_taken = 0;
}
if (javaDynArrays_static == NULL) {
javaDynArrays_static = malloc(16 * sizeof(JavaDynArray *));
if (!javaDynArrays_static)
return JNI_FALSE;
for (int i = 0; i < 16; ++i) {
javaDynArrays_static[i] = NULL;
}
javaDynArrays_static_capacity = 16;
javaDynArrays_static_count = 0;
}
return JNI_TRUE;
}
jboolean jda_extend() {
if (jda_tryinit() == JNI_FALSE)
return JNI_FALSE;
if (javaDynArrays_free == 0) {
if (javaDynArrays == NULL)
return JNI_FALSE;
javaDynArrays = realloc(javaDynArrays, (sizeof(JavaDynArray) * (javaDynArrays_taken+16)));
if (javaDynArrays == NULL)
return JNI_FALSE;
for (int i = javaDynArrays_taken; i < javaDynArrays_taken+16; ++i) {
javaDynArrays[i].array = NULL;
javaDynArrays[i].len = -1;
javaDynArrays[i].type = FIELD_TYPE_UNKNOWN;
}
javaDynArrays_free = 16;
}
if (javaDynArrays_static_capacity - javaDynArrays_static_count == 0) {
if (javaDynArrays_static == NULL)
return JNI_FALSE;
javaDynArrays_static = realloc(javaDynArrays_static, (sizeof(JavaDynArray *) * (javaDynArrays_static_count+16)));
for (int i = javaDynArrays_static_count; i < javaDynArrays_static_count + 16; ++i) {
javaDynArrays_static[i] = NULL;
}
javaDynArrays_static_capacity += 16;
}
return JNI_TRUE;
}
JavaDynArray * jda_alloc(jsize len, FIELD_TYPE type) {
jda_lock();
void * array = malloc(len * getFieldTypeSize(type));
if (!array) {
jda_unlock();
return NULL;
}
if (jda_extend() == JNI_FALSE) {
jda_unlock();
free(array);
return NULL;
}
int index = -1;
for (int i = 0; i < (javaDynArrays_taken+javaDynArrays_free); ++i) {
if (javaDynArrays[i].array == NULL) {
index = i;
}
}
if (index == -1) {
jda_unlock();
free(array);
return NULL;
}
javaDynArrays[index].array = array;
javaDynArrays[index].len = len;
javaDynArrays[index].type = type;
javaDynArrays_taken++;
javaDynArrays_free--;
JavaDynArray * ret = &javaDynArrays[index];
jda_unlock();
return ret;
}
jboolean jda_alloc_static(JavaDynArray * jda, jsize len, FIELD_TYPE type) {
jda_lock();
void * array = malloc(len * getFieldTypeSize(type));
if (!array) {
jda_unlock();
return JNI_FALSE;
}
if (jda_extend() == JNI_FALSE) {
jda_unlock();
free(array);
return JNI_FALSE;
}
int index = -1;
for (int i = 0; i < javaDynArrays_static_capacity; ++i) {
if (javaDynArrays_static[i] == NULL) {
index = i;
}
}
if (index == -1) {
jda_unlock();
free(array);
return JNI_FALSE;
}
javaDynArrays_static[index] = jda;
jda->array = array;
jda->len = len;
jda->type = type;
javaDynArrays_static_count++;
jda_unlock();
return JNI_TRUE;
}
jsize jda_sizeof(JavaDynArray * jda) {
if (!jda) return -1;
if (!javaDynArrays && !javaDynArrays_static) return -1;
jda_lock();
if (javaDynArrays_taken == 0 && javaDynArrays_static_count == 0) {
jda_unlock();
return -1;
}
for (int i = 0; i < (javaDynArrays_taken+javaDynArrays_free); i++) {
if (jda == &javaDynArrays[i]) {
if (javaDynArrays[i].array == NULL) {
jda_unlock();
return -1;
}
jsize ret = javaDynArrays[i].len;
jda_unlock();
return ret;
}
}
for (int i = 0; i < (javaDynArrays_static_capacity); i++) {
if (jda == javaDynArrays_static[i]) {
if (javaDynArrays_static[i]->array == NULL) {
jda_unlock();
return -1;
}
jsize ret = javaDynArrays_static[i]->len;
jda_unlock();
return ret;
}
}
jda_unlock();
return -1;
}
jboolean jda_free(JavaDynArray * jda) {
if (!jda) return JNI_FALSE;
if (!javaDynArrays && !javaDynArrays_static) return JNI_FALSE;
jda_lock();
if (javaDynArrays_taken == 0 && javaDynArrays_static_count == 0) {
jda_unlock();
return JNI_FALSE;
}
int index = -1;
for (int i = 0; i < (javaDynArrays_taken+javaDynArrays_free); ++i) {
if (jda == &javaDynArrays[i]) {
index = i;
}
}
if (index != -1) {
free(javaDynArrays[index].array);
javaDynArrays[index].array = NULL;
javaDynArrays[index].type = FIELD_TYPE_UNKNOWN;
javaDynArrays[index].len = 0;
javaDynArrays_taken--;
javaDynArrays_free++;
jda_unlock();
return JNI_TRUE;
}
for (int i = 0; i < javaDynArrays_static_capacity; ++i) {
if (jda == javaDynArrays_static[i]) {
index = i;
}
}
if (index != -1) {
free(javaDynArrays_static[index]->array);
javaDynArrays_static[index]->array = NULL;
javaDynArrays_static[index]->type = FIELD_TYPE_UNKNOWN;
javaDynArrays_static[index]->len = 0;
javaDynArrays_static[index] = NULL;
javaDynArrays_static_count--;
jda_unlock();
return JNI_TRUE;
}
jda_unlock();
return JNI_FALSE;
}
JavaDynArray * jda_find(void * arr) {
if (!arr) return NULL;
if (!javaDynArrays && !javaDynArrays_static) return NULL;
jda_lock();
if (javaDynArrays_taken == 0 && javaDynArrays_static_count == 0) {
jda_unlock();
return NULL;
}
for (int i = 0; i < (javaDynArrays_taken+javaDynArrays_free); ++i) {
if (arr == &javaDynArrays[i]) {
if (javaDynArrays[i].array == NULL) {
jda_unlock();
return NULL;
}
JavaDynArray * ret = &javaDynArrays[i];
jda_unlock();
return ret;
}
}
for (int i = 0; i < javaDynArrays_static_capacity; ++i) {
if (arr == javaDynArrays_static[i]) {
if (javaDynArrays_static[i]->array == NULL) {
jda_unlock();
return NULL;
}
JavaDynArray * ret = javaDynArrays_static[i];
jda_unlock();
return ret;
}
}
jda_unlock();
return NULL;
}
va_list _AtoV(int dummy, ...) {
va_list args1;
va_start(args1, dummy);
va_list args2;
va_copy(args2, args1);
va_end(args1);
return args2;
}