This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
cmd_jdwp.c
4895 lines (4342 loc) · 140 KB
/
cmd_jdwp.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in
* the LICENSE file in the root directory of this source tree. An
* additional grant of patent rights can be found in the PATENTS file
* in the same directory.
*/
#include <ctype.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/queue.h>
#include <assert.h>
#include "util.h"
#include "autocmd.h"
#include "fs.h"
#include "child.h"
#include "constants.h"
#include "adb.h"
#include "net.h"
#include "errcodes.h"
#include "tree.h"
#include "peer.h"
#define JDWP_HANDSHAKE "JDWP-Handshake"
#define JDWP_HANDSHAKE_LENGTH (sizeof (JDWP_HANDSHAKE) - 1)
#define JDWP_COMMANDSET_VIRTUALMACHINE 1
#define JDWP_COMMAND_VM_VERSION 1
#define JDWP_COMMAND_VM_CLASSESBYSIGNATURE 2
#define JDWP_COMMAND_VM_ALLCLASSES 3
#define JDWP_COMMAND_VM_ALLTHREADS 4
#define JDWP_COMMAND_VM_IDSIZES 7
#define JDWP_COMMAND_VM_SUSPEND 8
#define JDWP_COMMAND_VM_RESUME 9
#define JDWP_COMMAND_VM_DISPOSEOBJECTS 14
#define JDWP_COMMAND_VM_ALLCLASSES_GENERIC 20
#define JDWP_COMMANDSET_REFERENCETYPE 2
#define JDWP_COMMAND_RT_SIGNATURE 1
#define JDWP_COMMAND_RT_CLASSLOADER 2
#define JDWP_COMMAND_RT_MODIFIERS 3
#define JDWP_COMMANDSET_CLASSTYPE 3
#define JDWP_COMMANDSET_ARRAYTYPE 4
#define JDWP_COMMANDSET_INTERFACETYPE 5
#define JDWP_COMMANDSET_METHOD 6
#define JDWP_COMMANDSET_FIELD 8
#define JDWP_COMMANDSET_OBJECTREFERENCE 9
#define JDWP_COMMAND_OR_DISABLECOLLECTION 7
#define JDWP_COMMANDSET_STRINGREFERENCE 10
#define JDWP_COMMANDSET_THREADREFERENCE 11
#define JDWP_COMMANDSET_THREADGROUPREFERENCE 12
#define JDWP_COMMANDSET_ARRAYREFERENCE 13
#define JDWP_COMMANDSET_CLASSLOADERREFERENCE 14
#define JDWP_COMMAND_CLR_VISIBLECLASSES 1
#define JDWP_COMMANDSET_EVENTREQUEST 15
#define JDWP_COMMANDSET_STACKFRAME 16
#define JDWP_COMMANDSET_CLASSOBJECTREFERENCE 17
#define JDWP_COMMANDSET_EVENT 64
#define JDWP_COMMANDSET_DDM 199
#define JDWP_COMMAND_DDM_CHUNK 1
#define JDWP_FLAG_REPLY 0x80
#define JDWP_ERROR_NOT_IMPLEMENTED 99
#define MAX_JDWP_TYPE_NESTING 6
#define EVENT_SINGLE_STEP 1
#define EVN_SINGLE_STEP "Ev_SingleStep"
#define EVENT_BREAKPOINT 2
#define EVN_BREAKPOINT "Ev_Breakpoint"
#define EVENT_FRAME_POP 3
#define EVN_FRAME_PO "Ev_FramePop"
#define EVENT_EXCEPTION 4
#define EVN_EXCEPTION "Ev_Exception"
#define EVENT_USER_DEFINED 5
#define EVN_USER_DEFINED "Ev_UserDefined"
#define EVENT_THREAD_START 6
#define EVN_THREAD_START "Ev_ThreadStart"
#define EVENT_THREAD_DEATH 7
#define EVN_THREAD_DEATH "Ev_ThreadDeath"
#define EVENT_CLASS_PREPARE 8
#define EVN_CLASS_PREPARE "Ev_ClassPrepare"
#define EVENT_CLASS_UNLOAD 9
#define EVN_CLASS_UNLOAD "Ev_ClassUnload"
#define EVENT_CLASS_LOAD 10
#define EVN_CLASS_LOAD "Ev_ClassLoad"
#define EVENT_FIELD_ACCESS 20
#define EVN_FIELD_ACCESS "Ev_FieldAccess"
#define EVENT_FIELD_MODIFICATION 21
#define EVN_FIELD_MODIFICATION "Ev_FieldModification"
#define EVENT_EXCEPTION_CATCH 30
#define EVN_EXCEPTION_CATCH "Ev_ExceptionCatch"
#define EVENT_METHOD_ENTRY 40
#define EVN_METHOD_ENTRY "Ev_MethodEntry"
#define EVENT_METHOD_EXIT 41
#define EVN_METHOD_EXIT "Ev_MethodExit"
#define EVENT_METHOD_EXIT_WITH_RETURN_VALUE 42
#define EVN_METHOD_EXIT_WITH_RETURN_VALUE "Ev_MethodExitWithReturnValue"
#define EVENT_MONITOR_CONTENDED_ENTER 43
#define EVN_MONITOR_CONTENDED_ENTER "Ev_MonitorContendedEnter"
#define EVENT_MONITOR_CONTENDED_ENTERED 44
#define EVN_MONITOR_CONTENDED_ENTERED "Ev_MonitorContendedEntered"
#define EVENT_MONITOR_WAIT 45
#define EVN_MONITOR_WAIT "Ev_MonitorWait"
#define EVENT_MONITOR_WAITED 46
#define EVN_MONITOR_WAITED "Ev_MonitorWaited"
#define EVENT_VM_START 90
#define EVN_VM_START "Ev_VmStart"
#define EVENT_VM_DEATH 99
#define EVN_VM_DEATH "Ev_VMDeath"
#define COND_COUNT 1
#define CN_COUNT "Cond_Count"
#define COND_CONDITIONAL 2
#define CN_CONDITIONAL "Cond_Conditional"
#define COND_THREADONLY 3
#define CN_THREADONLY "Cond_ThreadOnly"
#define COND_CLASSONLY 4
#define CN_CLASSONLY "Cond_ClassOnly"
#define COND_CLASSMATCH 5
#define CN_CLASSMATCH "Cond_ClassMatch"
#define COND_CLASSEXCLUDE 6
#define CN_CLASSEXCLUDE "Cond_ClassExclude"
#define COND_LOCATIONONLY 7
#define CN_LOCATIONONLY "Cond_LocationOnly"
#define COND_EXCEPTIONONLY 8
#define CN_EXCEPTIONONLY "Cond_ExceptionOnly"
#define COND_FIELDONLY 9
#define CN_FIELDONLY "Cond_FieldOnly"
#define COND_STEP 10
#define CN_STEP "Cond_Step"
#define COND_INSTANCEONLY 11
#define CN_INSTANCEONLY "Cond_InstanceOnly"
#define COND_SOURCENAMEMATCH 12
#define CN_SOURCENAMEMATCH "Cond_SourceNameMatch"
#define REFKIND_CLASS 1
#define REFKIND_INTERFACE 2
#define REFKIND_ARRAY 3
#define STATUS_VERIFIED 1
#define STATUS_PREPARED 2
#define STATUS_INITIALIZED 4
#define STATUS_ERROR 8
#define ACC_INTERFACE 0x0200
#define ART_HARDCODED_PACKET_MAXIMUM 8192
#define REFERENCE_CACHE_SIZE 5000
#define ENUM_JDWP_ERRORS(x) \
x(JDWP_ERR_NONE, 0), \
x(JDWP_ERR_INVALID_THREAD, 10), \
x(JDWP_ERR_INVALID_THREAD_GROUP, 11), \
x(JDWP_ERR_INVALID_PRIORITY, 12), \
x(JDWP_ERR_THREAD_NOT_SUSPENDED, 13), \
x(JDWP_ERR_THREAD_SUSPENDED, 14), \
x(JDWP_ERR_THREAD_NOT_ALIVE, 15), \
x(JDWP_ERR_INVALID_OBJECT, 20), \
x(JDWP_ERR_INVALID_CLASS, 21), \
x(JDWP_ERR_CLASS_NOT_PREPARED, 22), \
x(JDWP_ERR_INVALID_METHODID, 23), \
x(JDWP_ERR_INVALID_LOCATION, 24), \
x(JDWP_ERR_INVALID_FIELDID, 25), \
x(JDWP_ERR_INVALID_FRAMEID, 30), \
x(JDWP_ERR_NO_MORE_FRAMES, 31), \
x(JDWP_ERR_OPAQUE_FRAME, 32), \
x(JDWP_ERR_NOT_CURRENT_FRAME, 33), \
x(JDWP_ERR_TYPE_MISMATCH, 34), \
x(JDWP_ERR_INVALID_SLOT, 35), \
x(JDWP_ERR_DUPLICATE, 40), \
x(JDWP_ERR_NOT_FOUND, 41), \
x(JDWP_ERR_INVALID_MONITOR, 50), \
x(JDWP_ERR_NOT_MONITOR_OWNED, 51), \
x(JDWP_ERR_INTERRUPT, 52), \
x(JDWP_ERR_INVALID_CLASS_FORMAT, 60), \
x(JDWP_ERR_CIRCULAR_CLASS_DEFINITION, 61), \
x(JDWP_ERR_FAILS_VERIFICATION, 62), \
x(JDWP_ERR_ADD_METHOD_NOT_IMPLEMENTED, 63), \
x(JDWP_ERR_SCHEMA_CHANGE_NOT_IMPLEMENTED, 64), \
x(JDWP_ERR_INVALID_TYPESTATE, 65), \
x(JDWP_ERR_HIERARCHY_CHANGE_NOT_IMPLEMENTED, 66), \
x(JDWP_ERR_DELETE_METHOD_NOT_IMPLEMENTED, 66), \
x(JDWP_ERR_UNSUPPORTED_VERSION, 68), \
x(JDWP_ERR_NAMES_DONT_MATCH, 69), \
x(JDWP_ERR_CLASS_MODIFIERS_CHANGE_NOT_IMPLEMENTED, 70), \
x(JDWP_ERR_METHOD_MODIFIERS_CHANGE_NOT_IMPLEMENTED, 71), \
x(JDWP_ERR_NOT_IMPLEMENTED, 99), \
x(JDWP_ERR_NULL_POINTER, 100), \
x(JDWP_ERR_ABSENT_INFORMATION, 101), \
x(JDWP_ERR_INVALID_EVENT_TYPE, 102), \
x(JDWP_ERR_ILLEGAL_ARGUMENT, 103), \
x(JDWP_ERR_OUT_OF_MEMORY, 110), \
x(JDWP_ERR_ACCESS_DENIED, 111), \
x(JDWP_ERR_VM_READ, 112), \
x(JDWP_ERR_INTERNAL, 113), \
x(JDWP_ERR_UNATTACHED_THREAD, 115), \
x(JDWP_ERR_INVALID_TAG, 500), \
x(JDWP_ERR_ALREADY_INVOKING, 502), \
x(JDWP_ERR_INVALID_INDEX, 503), \
x(JDWP_ERR_INVALID_LENGTH, 504), \
x(JDWP_ERR_INVALID_STRING, 506), \
x(JDWP_ERR_INVALID_CLASS_LOADER, 507), \
x(JDWP_ERR_INVALID_ARRAY, 508), \
x(JDWP_ERR_TRANSPORT_LOAD, 509), \
x(JDWP_ERR_TRANSPORT_INIT, 510), \
x(JDWP_ERR_NATIVE_METHOD, 511), \
x(JDWP_ERR_INVALID_COUNT, 512)
#define JDWP_ERR_MAX JDWP_ERR_INVALID_COUNT
enum jdwp_error {
#define X(name, value) name = value
ENUM_JDWP_ERRORS(X)
#undef X
};
static const struct jdwp_error_names {
uint16_t value;
const char* name;
} jdwp_error_names[] = {
#define X(name, value) { value, #name }
ENUM_JDWP_ERRORS(X)
#undef X
};
struct jdwp_type_table;
struct jdwp_cursor;
struct jdwp_cursor_frame;
struct jdwp_builder;
struct jdwp_builder_frame;
struct jdwp_header;
struct jdwp_type;
struct jdwp_packet;
static unsigned jdwp_scalar_width(struct jdwp_type* this);
static void jdwp_scalar_advance(
struct jdwp_type* this,
struct jdwp_cursor* c,
struct jdwp_cursor_frame* frame);
typedef unsigned long long llu;
static void
swap_bytes(void* bytes_in, unsigned nr_bytes)
{
uint8_t* bytes = bytes_in;
for (unsigned i = 0; i < nr_bytes / 2; ++i) {
uint8_t tmp = bytes[i];
bytes[i] = bytes[nr_bytes - i - 1];
bytes[nr_bytes - i - 1] = tmp;
}
}
#ifndef __unused
# define __unused __attribute__((unused))
#endif
static int
cmp_u64(uint64_t a, uint64_t b)
{
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
static const char*
jdwp_error_to_string(uint16_t errcode)
{
for (unsigned i = 0; i < ARRAYSIZE(jdwp_error_names); ++i)
if (jdwp_error_names[i].value == errcode)
return jdwp_error_names[i].name;
return NULL;
}
__attribute__((noreturn))
static void
die_jdwp(uint16_t errcode)
{
SCOPED_RESLIST(rl);
const char* name = jdwp_error_to_string(errcode);
if (name == NULL)
name = xaprintf("unknown JDWP error %hu", errcode);
die(JDWP_ERR_TO_FBADB_ERR(errcode),
"JDWP error code %hu from app: %s",
errcode, name);
}
static int
jdwp_connect(const struct adb_opts* adb,
const char* to_what)
{
SCOPED_RESLIST(rl);
struct strlist* adb_args_list = strlist_new();
emit_args_adb_opts(adb_args_list, adb);
const char* const* adb_args = strlist_to_argv(adb_args_list);
char* host_socket =
xaprintf("%s/fb-adb-jdwp-%s.sock",
system_tempdir(),
gen_hex_random(ENOUGH_ENTROPY));
const char* local = xaprintf("localfilesystem:%s", host_socket);
const char* remote = xaprintf("jdwp:%s", to_what);
struct remove_forward_cleanup* crf =
remove_forward_cleanup_allocate(local, adb_args);
adb_add_forward(local, remote, adb_args);
remove_forward_cleanup_commit(crf);
dbg("added forward %s -> %s", local, remote);
int scon = xsocket(AF_UNIX, SOCK_STREAM, 0);
xconnect(scon, make_addr_unix_filesystem(host_socket));
WITH_CURRENT_RESLIST(rl->parent);
return xdup(scon);
}
struct write_all_tolerate_epipe_args {
int fd;
const void* buf;
size_t sz;
};
static void
write_all_tolerate_epipe_1(void* arg)
{
struct write_all_tolerate_epipe_args* args = arg;
write_all(args->fd, args->buf, args->sz);
}
static bool
write_all_tolerate_epipe(int fd, const void* buf, size_t sz)
{
struct write_all_tolerate_epipe_args arg = {
.fd = fd,
.buf = buf,
.sz = sz
};
struct errinfo ei = { .want_msg = true };
if (catch_error(write_all_tolerate_epipe_1, &arg, &ei)) {
if (ei.err == EPIPE)
return false;
die_rethrow(&ei);
}
return true;
}
__attribute__((noreturn))
static void
die_early_connection_problem(void)
{
die(ECOMM,
"Could not connect to application. "
"Is app alive? Are you holding the debugger wrong?");
}
static void
do_jdwp_handshake_as_client(int to_app)
{
if (!write_all_tolerate_epipe(
to_app,
JDWP_HANDSHAKE,
JDWP_HANDSHAKE_LENGTH))
{
die_early_connection_problem();
}
char reply_buf[JDWP_HANDSHAKE_LENGTH];
size_t n = read_all(to_app, reply_buf, JDWP_HANDSHAKE_LENGTH);
if (n < JDWP_HANDSHAKE_LENGTH)
die_early_connection_problem();
if (!memcpy(reply_buf, JDWP_HANDSHAKE, JDWP_HANDSHAKE_LENGTH))
die(ECOMM, "illegal JDWP handshake");
}
static void
do_jdwp_handshake_as_server(int to_debugger)
{
char buf[JDWP_HANDSHAKE_LENGTH];
size_t n = read_all(to_debugger, buf, JDWP_HANDSHAKE_LENGTH);
if (n < JDWP_HANDSHAKE_LENGTH)
die(ECOMM, "debugger disconnected prematurely");
if (!memcpy(buf, JDWP_HANDSHAKE, JDWP_HANDSHAKE_LENGTH))
die(ECOMM, "debugger sent bad handshake");
write_all(to_debugger, buf, JDWP_HANDSHAKE_LENGTH);
}
struct jdwp_header {
uint32_t length;
uint32_t id;
uint8_t flags;
union {
struct {
uint8_t group;
uint8_t code;
} command;
struct {
uint16_t error_code;
} reply;
};
uint8_t data[0];
} __attribute__((packed));
_Static_assert(sizeof (struct jdwp_header) == 11, "alignment");
static bool
jdwp_command_p(struct jdwp_header* jh)
{
return (jh->flags & JDWP_FLAG_REPLY) == 0;
}
static void
jdwp_header_to_host(struct jdwp_header* jh)
{
jh->length = ntohl(jh->length);
jh->id = ntohl(jh->id);
if (!jdwp_command_p(jh))
jh->reply.error_code = ntohs(jh->reply.error_code);
}
static void
jdwp_header_to_network(struct jdwp_header* jh)
{
jh->length = htonl(jh->length);
jh->id = htonl(jh->id);
if (!jdwp_command_p(jh))
jh->reply.error_code = htons(jh->reply.error_code);
}
struct jdwp_packet {
struct reslist* rl; // Owns itself
LIST_ENTRY(jdwp_packet) link;
bool on_list;
bool has_rewritten_id;
uint32_t original_id;
struct jdwp_header header;
// struct hack at end of jdwp_header; do not add data here
};
static void
jdwp_send_packet(int fd, const struct jdwp_header* jh)
{
assert(sizeof (*jh) <= jh->length);
struct jdwp_header out_jh = *jh;
jdwp_header_to_network(&out_jh);
struct iovec chunks[] = {
{ &out_jh, sizeof (out_jh) },
{ (void*) &jh->data, jh->length - sizeof (*jh) },
};
write_all_v(fd, &chunks[0], ARRAYSIZE(chunks));
}
static char*
describe_jdwp_message(struct jdwp_header* jh)
{
return !jdwp_command_p(jh)
? xaprintf("[JDWP reply length:%u id:%u flags:%hhu err:%hu]",
jh->length,
jh->id,
jh->flags,
jh->reply.error_code)
: xaprintf(
"[JDWP command length:%u id:%u flags:%hhu cmdset:%hhu cmd:%hhu]",
jh->length,
jh->id,
jh->flags,
jh->command.group,
jh->command.code);
}
struct jdwp_type;
struct jdwp_command;
struct jdwp_type_table {
struct reslist* rl;
struct jdwp_type* types;
struct jdwp_command* commands;
struct {
struct jdwp_type* boolean;
struct jdwp_type* byte;
struct jdwp_type* char_;
struct jdwp_type* double_;
struct jdwp_type* float_;
struct jdwp_type* int_;
struct jdwp_type* long_;
struct jdwp_type* short_;
struct jdwp_type* string;
struct jdwp_type* void_;
struct jdwp_type* object_id;
struct jdwp_type* array_id;
struct jdwp_type* class_loader_id;
struct jdwp_type* class_object_id;
struct jdwp_type* string_id;
struct jdwp_type* thread_group_id;
struct jdwp_type* thread_id;
struct jdwp_type* reference_type_id;
struct jdwp_type* class_id;
struct jdwp_type* interface_id;
struct jdwp_type* array_type_id;
struct jdwp_type* field_id;
struct jdwp_type* method_id;
struct jdwp_type* frame_id;
struct jdwp_type* value;
struct jdwp_type* arrayregion;
} type;
};
enum jdwp_type_kind {
JDWP_TYPE_STRUCT,
JDWP_TYPE_ARRAY,
JDWP_TYPE_OTHER,
};
struct jdwp_type {
struct jdwp_type* next;
struct jdwp_type_table* tt;
enum jdwp_type_kind kind;
char* name;
struct jdwp_type* supertype;
unsigned depth;
void (*init_frame)(
struct jdwp_type* this,
struct jdwp_cursor* c,
struct jdwp_cursor_frame* frame);
size_t (*read)(
struct jdwp_type* this,
struct jdwp_cursor* c,
struct jdwp_cursor_frame* frame,
void* value,
size_t size);
void (*write)(
struct jdwp_type* this,
struct jdwp_cursor* c,
struct jdwp_cursor_frame* frame,
const void* value,
size_t size);
struct jdwp_type* (*next_type)(
struct jdwp_type* this,
struct jdwp_cursor* c,
struct jdwp_cursor_frame* frame);
void (*advance)(
struct jdwp_type* this,
struct jdwp_cursor* c,
struct jdwp_cursor_frame* frame);
union {
struct {
unsigned width;
} scalar;
struct {
unsigned nr_fields;
const char** field_names;
struct jdwp_type** field_types;
} struct_;
struct { // Assume all arrays start with integer count
struct jdwp_type* element_type;
} array;
};
};
static void
noop_init_frame(
struct jdwp_type* this,
struct jdwp_cursor* c,
struct jdwp_cursor_frame* frame)
{}
static void
jdwp_type_set_supertype(
struct jdwp_type* type,
struct jdwp_type* supertype)
{
if (supertype == NULL) {
type->supertype = NULL;
} else {
for (struct jdwp_type* p = supertype; p != NULL; p = p->supertype)
assert(p != type);
type->supertype = supertype;
}
}
static bool
jdwp_type_isinstance(
struct jdwp_type* type,
struct jdwp_type* supertype)
{
while (type != NULL) {
if (type == supertype)
return true;
type = type->supertype;
}
return false;
}
struct jdwp_cursor_frame {
struct jdwp_type* type;
union {
struct {
uint32_t length;
} string;
struct {
unsigned index;
} struct_;
struct {
uint32_t index;
uint32_t count;
struct jdwp_type* element_type;
} array;
};
};
struct jdwp_cursor {
uint8_t* pos;
uint8_t* end;
int depth;
struct jdwp_cursor_frame stack[MAX_JDWP_TYPE_NESTING];
};
static struct jdwp_cursor
jdwp_cursor_create(
struct jdwp_type* type,
void* data,
size_t length)
{
struct jdwp_cursor c;
memset(&c, 0, sizeof (c));
c.pos = data;
c.end = c.pos + length;
c.stack[0].type = type;
type->init_frame(type, &c, &c.stack[0]);
return c;
}
static bool
jdwp_cursor_has_value(struct jdwp_cursor* c)
{
return c->depth >= 0 && c->stack[c->depth].type != NULL;
}
static void
jdwp_cursor_memcpy_out(void* out, struct jdwp_cursor* c, size_t length)
{
if (c->end - c->pos < length)
die(EINVAL, "truncated packet");
memcpy(out, c->pos, length);
}
static void
jdwp_cursor_memcpy_out_byteswapped(
void* out,
struct jdwp_cursor* c,
size_t length)
{
uint8_t* bytes = out;
jdwp_cursor_memcpy_out(bytes, c, length);
swap_bytes(bytes, length);
}
static void
jdwp_cursor_slurp(
void* out,
struct jdwp_cursor* c,
size_t length)
{
jdwp_cursor_memcpy_out_byteswapped(out, c, length);
c->pos += length;
}
static void
jdwp_cursor_memcpy_in(struct jdwp_cursor* c, const void* in, size_t length)
{
if (c->end - c->pos < length)
die(EINVAL, "truncated packet");
memcpy(c->pos, in, length);
}
static struct jdwp_type*
jdwp_cursor_current_type(struct jdwp_cursor* c)
{
if (!jdwp_cursor_has_value(c))
die(EINVAL, "no current value");
return c->stack[c->depth].type;
}
static void
jdwp_cursor_check_type(
struct jdwp_cursor* c,
struct jdwp_type* type)
{
struct jdwp_type* current_type = jdwp_cursor_current_type(c);
if (current_type != type) {
die(EINVAL,
"expected type `%s', but found `%s'",
type->name,
current_type->name);
}
}
static void
jdwp_cursor_check_struct_field(
struct jdwp_cursor* c,
struct jdwp_type* type,
const char* name)
{
jdwp_cursor_check_type(c, type); // XXX
}
static void
jdwp_cursor_next(struct jdwp_cursor* c)
{
if (c->depth < 0)
return;
struct jdwp_cursor_frame* current_frame = &c->stack[c->depth];
struct jdwp_type* current_type = current_frame->type;
if (current_type == NULL)
return;
current_type->advance(current_type, c, current_frame);
memset(current_frame, 0, sizeof (*current_frame));
if (c->depth > 0) {
struct jdwp_cursor_frame* parent_frame = &c->stack[c->depth - 1];
struct jdwp_type* parent_type = parent_frame->type;
current_type = parent_type->next_type(parent_type, c, parent_frame);
current_frame->type = current_type;
if (current_type != NULL)
current_type->init_frame(current_type, c, current_frame);
}
}
static struct jdwp_type* jdwp_array_next_type(
struct jdwp_type* this,
struct jdwp_cursor* c,
struct jdwp_cursor_frame* frame);
static uint32_t
jdwp_cursor_array_length(struct jdwp_cursor*c)
{
if (!jdwp_cursor_has_value(c))
die(EINVAL, "no current value");
struct jdwp_cursor_frame* frame = &c->stack[c->depth];
struct jdwp_type* type = frame->type;
if (type->next_type != jdwp_array_next_type)
die(EINVAL, "not looking at JDWP array");
return frame->array.count;
}
static bool
jdwp_cursor_can_enter(struct jdwp_cursor* c)
{
if (!jdwp_cursor_has_value(c))
die(EINVAL, "no current value");
struct jdwp_cursor_frame* frame = &c->stack[c->depth];
struct jdwp_type* type = frame->type;
return type->next_type != NULL;
}
static void
jdwp_cursor_enter(struct jdwp_cursor* c)
{
if (!jdwp_cursor_has_value(c))
die(EINVAL, "no current value");
struct jdwp_cursor_frame* parent_frame = &c->stack[c->depth];
struct jdwp_type* parent_type = parent_frame->type;
if (parent_type == NULL)
die(EINVAL, "no current value");
if (parent_type->next_type == NULL)
die(EINVAL, "cannot enter type `%s'", parent_type->name);
struct jdwp_cursor_frame* current_frame = &c->stack[c->depth + 1];
memset(current_frame, 0, sizeof (*current_frame));
struct jdwp_type* current_type =
parent_type->next_type(parent_type, c, parent_frame);
current_frame->type = current_type;
if (current_type != NULL)
current_type->init_frame(current_type, c, current_frame);
assert(c->depth < MAX_JDWP_TYPE_NESTING);
c->depth += 1;
}
static bool
jdwp_cursor_has_parent(struct jdwp_cursor* c)
{
return c->depth > 0;
}
static void
jdwp_cursor_leave(struct jdwp_cursor* c)
{
if (!jdwp_cursor_has_parent(c))
die(EINVAL, "return attempted with no parent");
if (jdwp_cursor_has_value(c))
die(EINVAL, "premature return attempt");
c->depth -= 1;
struct jdwp_cursor_frame* current_frame = &c->stack[c->depth];
memset(current_frame, 0, sizeof (*current_frame));
if (c->depth > 0) {
struct jdwp_cursor_frame* parent_frame = &c->stack[c->depth - 1];
struct jdwp_type* parent_type = parent_frame->type;
struct jdwp_type* current_type =
parent_type->next_type(parent_type, c, parent_frame);
if (current_type != NULL) {
current_frame->type = current_type;
current_type->init_frame(current_type, c, current_frame);
}
}
}
static size_t
jdwp_cursor_read(struct jdwp_cursor* c, void* value, size_t size)
{
struct jdwp_type* current = jdwp_cursor_current_type(c);
if (current->read == NULL)
die(EINVAL, "cannot read from type %s", current->name);
return current->read(current, c, &c->stack[c->depth], value, size);
}
static uint64_t
jdwp_cursor_read_id(struct jdwp_cursor* c, struct jdwp_type* id_type)
{
uint64_t id = 0;
jdwp_cursor_read(c, &id, jdwp_scalar_width(id_type));
return id;
}
static uint8_t
jdwp_cursor_read_u8(struct jdwp_cursor* c)
{
uint8_t value;
jdwp_cursor_read(c, &value, sizeof (value));
return value;
}
static int32_t
jdwp_cursor_read_i32(struct jdwp_cursor* c)
{
int32_t value;
jdwp_cursor_read(c, &value, sizeof (value));
return value;
}
static void jdwp_string_init_frame(
struct jdwp_type* this,
struct jdwp_cursor* c,
struct jdwp_cursor_frame* frame);
static char*
jdwp_cursor_read_string(struct jdwp_cursor* c)
{
struct jdwp_type* current = jdwp_cursor_current_type(c);
if (current->init_frame != jdwp_string_init_frame)
die(EINVAL, "not a string value; value is `%s'",
current->name);
uint32_t nbytes = c->stack[c->depth].string.length;
char* value = xalloc(nbytes + 1); // overflow check on stack init
jdwp_cursor_memcpy_out(value, c, nbytes);
value[nbytes] = '\0';
return value;
}
static void
jdwp_cursor_write(struct jdwp_cursor* c, void* value, size_t size)
{
struct jdwp_type* current = jdwp_cursor_current_type(c);
if (current->write == NULL)
die(EINVAL, "cannot update value of type %s", current->name);
current->write(current, c, &c->stack[c->depth], value, size);
}
static uint64_t
jdwp_cursor_write_id(struct jdwp_cursor* c, uint64_t id, struct jdwp_type* id_type)
{
jdwp_cursor_write(c, &id, jdwp_scalar_width(id_type));
return id;
}
static struct jdwp_type*
jdwp_try_find_type(
struct jdwp_type_table* tt,
const char* name)
{
for (struct jdwp_type* type = tt->types;
type != NULL;
type = type->next)
{
if (!strcmp(type->name, name))
return type;
}
return NULL;
}
static struct jdwp_type*
jdwp_find_type(
struct jdwp_type_table* tt,
const char* name)
{
struct jdwp_type* type = jdwp_try_find_type(tt, name);
if (type == NULL)
die(EINVAL, "no type called `%s' defined", name);
return type;
}
static void
generic_advance(
struct jdwp_type* this,
struct jdwp_cursor* c,
struct jdwp_cursor_frame* frame)
{
assert(this->next_type);
jdwp_cursor_enter(c);
while (jdwp_cursor_has_value(c))
jdwp_cursor_next(c);
jdwp_cursor_leave(c);
}
static void
jdwp_commit_new_type(
struct jdwp_type_table* tt,
struct jdwp_type* new_type)
{
assert(new_type->name != NULL);
assert(new_type->next == NULL);
assert(new_type->tt == NULL);
if (new_type->init_frame == NULL)
new_type->init_frame = noop_init_frame;
if (new_type->advance == NULL && new_type->next_type != NULL)
new_type->advance = generic_advance;
assert(new_type->init_frame);
assert(new_type->advance);
if (new_type->next_type != NULL)
assert(new_type->depth > 0);
assert(new_type->depth < MAX_JDWP_TYPE_NESTING);
new_type->next = tt->types;
new_type->tt = tt;
tt->types = new_type;
}