-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathscript.vala
3292 lines (2605 loc) · 101 KB
/
script.vala
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
namespace Frida {
private class BareboneScript : Object {
public signal void message (string json, Bytes? data);
public AgentScriptId id {
get;
construct;
}
public Barebone.Services services {
get;
construct;
}
private GDB.Client gdb;
private QuickJS.Runtime rt;
private QuickJS.Context ctx;
private QuickJS.Atom address_key;
private QuickJS.Atom base_key;
private QuickJS.Atom breakpoint_key;
private QuickJS.Atom coalesce_key;
private QuickJS.Atom dependencies_key;
private QuickJS.Atom handle_key;
private QuickJS.Atom invoke_key;
private QuickJS.Atom length_key;
private QuickJS.Atom line_number_key;
private QuickJS.Atom message_key;
private QuickJS.Atom on_complete_key;
private QuickJS.Atom on_enter_key;
private QuickJS.Atom on_error_key;
private QuickJS.Atom on_leave_key;
private QuickJS.Atom on_match_key;
private QuickJS.Atom protection_key;
private QuickJS.Atom prototype_key;
private QuickJS.Atom signum_key;
private QuickJS.Atom size_key;
private QuickJS.Atom thread_key;
private QuickJS.Atom type_key;
private QuickJS.Atom v_key;
private Gee.Queue<QuickJS.Value?> tick_callbacks = new Gee.ArrayQueue<QuickJS.Value?> ();
private Barebone.Allocation? cached_landing_zone; // TODO: Deallocate on teardown.
private Gee.Set<Barebone.Callback> native_callbacks = new Gee.HashSet<Barebone.Callback> ();
private static QuickJS.ClassID cpu_context_class;
private static QuickJS.ClassExoticMethods cpu_context_exotic_methods;
private static QuickJS.ClassID invocation_listener_class;
private Gee.Set<Barebone.InvocationListener> invocation_listeners = new Gee.HashSet<Barebone.InvocationListener> ();
private static QuickJS.ClassID invocation_context_class;
private static QuickJS.ClassID invocation_args_class;
private static QuickJS.ClassExoticMethods invocation_args_exotic_methods;
private static QuickJS.ClassID invocation_retval_class;
private static QuickJS.ClassID rust_module_class;
private Gee.Set<Barebone.RustModule> rust_modules = new Gee.HashSet<Barebone.RustModule> ();
private static QuickJS.ClassID gdb_thread_class;
private static QuickJS.ClassID gdb_breakpoint_class;
private Gee.Map<GDB.Breakpoint, QuickJS.Value?> gdb_breakpoints = new Gee.HashMap<GDB.Breakpoint, QuickJS.Value?> ();
private QuickJS.Value global = QuickJS.Undefined;
private QuickJS.Value runtime_obj = QuickJS.Undefined;
private QuickJS.Value dispatch_exception_func = QuickJS.Undefined;
private QuickJS.Value dispatch_message_func = QuickJS.Undefined;
private QuickJS.Value ptr_func = QuickJS.Undefined;
private QuickJS.Value int64_func = QuickJS.Undefined;
private QuickJS.Value uint64_func = QuickJS.Undefined;
private Gee.ArrayList<QuickJS.Value?> entrypoints = new Gee.ArrayList<QuickJS.Value?> ();
private Gee.Map<string, Asset> assets = new Gee.HashMap<string, Asset> ();
private Cancellable io_cancellable = new Cancellable ();
private const uint64 MAX_ASSET_SIZE = 100 * 1024 * 1024;
private const uint32 MAX_JS_BYTE_ARRAY_LENGTH = 100 * 1024 * 1024;
public static BareboneScript create (AgentScriptId id, string source, Barebone.Services services) throws Error {
var script = new BareboneScript (id, services);
unowned string runtime_js = (string) Frida.Data.Barebone.get_script_runtime_js_blob ().data;
script.add_program (runtime_js, "/_frida.js");
script.add_program (source, "/agent.js");
return script;
}
private BareboneScript (AgentScriptId id, Barebone.Services services) {
Object (id: id, services: services);
}
static construct {
cpu_context_exotic_methods.get_own_property = on_cpu_context_get_own_property;
cpu_context_exotic_methods.get_own_property_names = on_cpu_context_get_own_property_names;
cpu_context_exotic_methods.has_property = on_cpu_context_has_property;
cpu_context_exotic_methods.get_property = on_cpu_context_get_property;
cpu_context_exotic_methods.set_property = on_cpu_context_set_property;
invocation_args_exotic_methods.get_property = on_invocation_args_get_property;
invocation_args_exotic_methods.set_property = on_invocation_args_set_property;
}
construct {
gdb = services.machine.gdb;
rt = QuickJS.Runtime.make ();
rt.set_opaque (this);
ctx = QuickJS.Context.make (rt);
ctx.set_opaque (this);
address_key = ctx.make_atom ("address");
base_key = ctx.make_atom ("base");
breakpoint_key = ctx.make_atom ("breakpoint");
coalesce_key = ctx.make_atom ("coalesce");
dependencies_key = ctx.make_atom ("dependencies");
handle_key = ctx.make_atom ("handle");
invoke_key = ctx.make_atom ("_invoke");
length_key = ctx.make_atom ("length");
line_number_key = ctx.make_atom ("lineNumber");
message_key = ctx.make_atom ("message");
on_complete_key = ctx.make_atom ("onComplete");
on_enter_key = ctx.make_atom ("onEnter");
on_error_key = ctx.make_atom ("onError");
on_leave_key = ctx.make_atom ("onLeave");
on_match_key = ctx.make_atom ("onMatch");
protection_key = ctx.make_atom ("protection");
prototype_key = ctx.make_atom ("prototype");
signum_key = ctx.make_atom ("signum");
size_key = ctx.make_atom ("size");
thread_key = ctx.make_atom ("thread");
type_key = ctx.make_atom ("type");
v_key = ctx.make_atom ("$v");
global = ctx.get_global_object ();
add_cfunc (global, "_send", on_send, 2);
add_cfunc (global, "_invoke", on_invoke, 1);
add_cfunc (global, "_installNativeCallback", on_install_native_callback, 3);
var script_obj = ctx.make_object ();
add_cfunc (script_obj, "evaluate", on_evaluate, 2);
add_cfunc (script_obj, "nextTick", on_next_tick, 1);
global.set_property_str (ctx, "Script", script_obj);
QuickJS.ClassDef cc;
cc.class_name = "CpuContext";
cc.finalizer = on_cpu_context_finalize;
cc.exotic = &cpu_context_exotic_methods;
rt.make_class (QuickJS.make_class_id (ref cpu_context_class), cc);
var memory_obj = ctx.make_object ();
add_cfunc (memory_obj, "alloc", on_memory_alloc, 1);
add_cfunc (memory_obj, "scan", on_memory_scan, 4);
add_cfunc (memory_obj, "scanSync", on_memory_scan_sync, 3);
global.set_property_str (ctx, "Memory", memory_obj);
var process_obj = ctx.make_object ();
process_obj.set_property_str (ctx, "arch", ctx.make_string (gdb.arch.to_nick ()));
process_obj.set_property_str (ctx, "pageSize", ctx.make_uint32 ((uint32) services.allocator.page_size));
process_obj.set_property_str (ctx, "pointerSize", ctx.make_uint32 (gdb.pointer_size));
add_cfunc (process_obj, "enumerateRanges", on_process_enumerate_ranges, 1);
global.set_property_str (ctx, "Process", process_obj);
var file_obj = ctx.make_object ();
add_cfunc (file_obj, "readAllBytes", on_file_read_all_bytes, 1);
add_cfunc (file_obj, "readAllText", on_file_read_all_text, 1);
add_cfunc (file_obj, "writeAllBytes", on_file_write_all_bytes, 2);
add_cfunc (file_obj, "writeAllText", on_file_write_all_text, 2);
global.set_property_str (ctx, "File", file_obj);
var interceptor_obj = ctx.make_object ();
add_property (interceptor_obj, "breakpointKind", on_interceptor_get_breakpoint_kind,
on_interceptor_set_breakpoint_kind);
add_cfunc (interceptor_obj, "attach", on_interceptor_attach, 2);
global.set_property_str (ctx, "Interceptor", interceptor_obj);
QuickJS.ClassDef il;
il.class_name = "InvocationListener";
rt.make_class (QuickJS.make_class_id (ref invocation_listener_class), il);
var il_proto = ctx.make_object ();
add_cfunc (il_proto, "detach", on_invocation_listener_detach, 0);
ctx.set_class_proto (invocation_listener_class, il_proto);
QuickJS.ClassDef ic;
ic.class_name = "InvocationContext";
rt.make_class (QuickJS.make_class_id (ref invocation_context_class), ic);
var ic_proto = ctx.make_object ();
add_getter (ic_proto, "returnAddress", on_invocation_context_get_return_address);
add_getter (ic_proto, "context", on_invocation_context_get_context);
ic_proto.set_property_str (ctx, "errno", ctx.make_int32 (-1));
add_getter (ic_proto, "threadId", on_invocation_context_get_thread_id);
add_getter (ic_proto, "depth", on_invocation_context_get_depth);
ctx.set_class_proto (invocation_context_class, ic_proto);
QuickJS.ClassDef ia;
ia.class_name = "InvocationArguments";
ia.exotic = &invocation_args_exotic_methods;
rt.make_class (QuickJS.make_class_id (ref invocation_args_class), ia);
QuickJS.ClassDef ir;
ir.class_name = "InvocationReturnValue";
rt.make_class (QuickJS.make_class_id (ref invocation_retval_class), ir);
QuickJS.ClassDef rm;
rm.class_name = "RustModule";
rm.finalizer = on_rust_module_finalize;
rt.make_class (QuickJS.make_class_id (ref rust_module_class), rm);
var rm_proto = ctx.make_object ();
add_cfunc (rm_proto, "dispose", on_rust_module_dispose, 0);
ctx.set_class_proto (rust_module_class, rm_proto);
var rm_ctor = ctx.make_cfunction2 (on_rust_module_construct, rm.class_name, 3, constructor, 0);
rm_ctor.set_constructor (ctx, rm_proto);
global.set_property_str (ctx, "RustModule", rm_ctor);
var gdb_obj = ctx.make_object ();
add_getter (gdb_obj, "state", on_gdb_get_state);
add_getter (gdb_obj, "exception", on_gdb_get_exception);
add_cfunc (gdb_obj, "continue", on_gdb_continue, 0);
add_cfunc (gdb_obj, "stop", on_gdb_stop, 0);
add_cfunc (gdb_obj, "restart", on_gdb_restart, 0);
add_cfunc (gdb_obj, "readPointer", on_gdb_read_pointer, 1);
add_cfunc (gdb_obj, "writePointer", on_gdb_write_pointer, 2);
add_cfunc (gdb_obj, "readS8", on_gdb_read_s8, 1);
add_cfunc (gdb_obj, "writeS8", on_gdb_write_s8, 2);
add_cfunc (gdb_obj, "readU8", on_gdb_read_u8, 1);
add_cfunc (gdb_obj, "writeU8", on_gdb_write_u8, 2);
add_cfunc (gdb_obj, "readS16", on_gdb_read_s16, 1);
add_cfunc (gdb_obj, "writeS16", on_gdb_write_s16, 2);
add_cfunc (gdb_obj, "readU16", on_gdb_read_u16, 1);
add_cfunc (gdb_obj, "writeU16", on_gdb_write_u16, 2);
add_cfunc (gdb_obj, "readS32", on_gdb_read_s32, 1);
add_cfunc (gdb_obj, "writeS32", on_gdb_write_s32, 2);
add_cfunc (gdb_obj, "readU32", on_gdb_read_u32, 1);
add_cfunc (gdb_obj, "writeU32", on_gdb_write_u32, 2);
add_cfunc (gdb_obj, "readS64", on_gdb_read_s64, 1);
add_cfunc (gdb_obj, "writeS64", on_gdb_write_s64, 2);
add_cfunc (gdb_obj, "readU64", on_gdb_read_u64, 1);
add_cfunc (gdb_obj, "writeU64", on_gdb_write_u64, 2);
add_cfunc (gdb_obj, "readFloat", on_gdb_read_float, 1);
add_cfunc (gdb_obj, "writeFloat", on_gdb_write_float, 2);
add_cfunc (gdb_obj, "readDouble", on_gdb_read_double, 1);
add_cfunc (gdb_obj, "writeDouble", on_gdb_write_double, 2);
add_cfunc (gdb_obj, "readByteArray", on_gdb_read_byte_array, 2);
add_cfunc (gdb_obj, "writeByteArray", on_gdb_write_byte_array, 2);
add_cfunc (gdb_obj, "readCString", on_gdb_read_c_string, 2);
add_cfunc (gdb_obj, "readUtf8String", on_gdb_read_utf8_string, 2);
add_cfunc (gdb_obj, "writeUtf8String", on_gdb_write_utf8_string, 2);
add_cfunc (gdb_obj, "addBreakpoint", on_gdb_add_breakpoint, 3);
add_cfunc (gdb_obj, "runRemoteCommand", on_gdb_run_remote_command, 1);
add_cfunc (gdb_obj, "execute", on_gdb_execute, 1);
add_cfunc (gdb_obj, "query", on_gdb_query, 1);
global.set_property_str (ctx, "$gdb", gdb_obj);
QuickJS.ClassDef th;
th.class_name = "GDBThread";
th.finalizer = on_gdb_thread_finalize;
rt.make_class (QuickJS.make_class_id (ref gdb_thread_class), th);
var th_proto = ctx.make_object ();
add_getter (th_proto, "id", on_gdb_thread_get_id);
add_getter (th_proto, "name", on_gdb_thread_get_name);
add_cfunc (th_proto, "step", on_gdb_thread_step, 0);
add_cfunc (th_proto, "stepAndContinue", on_gdb_thread_step_and_continue, 0);
add_cfunc (th_proto, "readRegisters", on_gdb_thread_read_registers, 0);
add_cfunc (th_proto, "readRegister", on_gdb_thread_read_register, 1);
add_cfunc (th_proto, "writeRegister", on_gdb_thread_write_register, 2);
ctx.set_class_proto (gdb_thread_class, th_proto);
QuickJS.ClassDef bp;
bp.class_name = "GDBBreakpoint";
bp.finalizer = on_gdb_breakpoint_finalize;
rt.make_class (QuickJS.make_class_id (ref gdb_breakpoint_class), bp);
var bp_proto = ctx.make_object ();
add_getter (bp_proto, "kind", on_gdb_breakpoint_get_kind);
add_getter (bp_proto, "address", on_gdb_breakpoint_get_address);
add_getter (bp_proto, "size", on_gdb_breakpoint_get_size);
add_cfunc (bp_proto, "enable", on_gdb_breakpoint_enable, 0);
add_cfunc (bp_proto, "disable", on_gdb_breakpoint_disable, 0);
add_cfunc (bp_proto, "remove", on_gdb_breakpoint_remove, 0);
ctx.set_class_proto (gdb_breakpoint_class, bp_proto);
}
private void add_cfunc (QuickJS.Value ns, string name, QuickJS.CFunction func, int arity) {
ns.set_property_str (ctx, name, ctx.make_cfunction (func, name, arity));
}
private void add_getter (QuickJS.Value ns, string name, QuickJS.CFunction func) {
add_property (ns, name, func, null);
}
private void add_property (QuickJS.Value ns, string name, QuickJS.CFunction getter_func, QuickJS.CFunction? setter_func) {
QuickJS.Atom prop = ctx.make_atom (name);
var val = QuickJS.Undefined;
QuickJS.PropertyFlags flags = HAS_GET | HAS_ENUMERABLE | ENUMERABLE;
var getter = ctx.make_cfunction (getter_func, name, 0);
QuickJS.Value setter = QuickJS.Undefined;
if (setter_func != null) {
flags |= HAS_SET;
setter = ctx.make_cfunction (setter_func, name, 1);
}
ns.define_property (ctx, prop, val, getter, setter, flags);
ctx.free_value (setter);
ctx.free_value (getter);
ctx.free_atom (prop);
}
~BareboneScript () {
rust_modules.clear ();
native_callbacks.clear ();
QuickJS.Value[] values = {
global,
runtime_obj,
dispatch_exception_func,
dispatch_message_func,
ptr_func,
int64_func,
uint64_func,
};
foreach (var val in values)
ctx.free_value (val);
QuickJS.Atom atoms[] = {
address_key,
base_key,
breakpoint_key,
coalesce_key,
dependencies_key,
handle_key,
invoke_key,
length_key,
line_number_key,
message_key,
on_complete_key,
on_enter_key,
on_error_key,
on_leave_key,
on_match_key,
protection_key,
signum_key,
size_key,
thread_key,
type_key,
v_key,
};
foreach (var atom in atoms)
ctx.free_atom (atom);
ctx = null;
rt = null;
}
public async void destroy (Cancellable? cancellable) throws IOError {
io_cancellable.cancel ();
var interceptor = services.interceptor;
foreach (var listener in invocation_listeners.to_array ()) {
try {
yield interceptor.detach (listener, cancellable);
} catch (Error e) {
}
}
invocation_listeners.clear ();
var source = new IdleSource ();
source.set_callback (destroy.callback);
source.attach (MainContext.get_thread_default ());
yield;
}
public void load () {
foreach (QuickJS.Value? entrypoint in entrypoints) {
var result = ctx.eval_function (entrypoint);
if (result.is_exception ())
catch_and_emit ();
ctx.free_value (result);
if (runtime_obj.is_undefined ()) {
runtime_obj = global.get_property_str (ctx, "$rt");
if (!runtime_obj.is_undefined ()) {
dispatch_exception_func = runtime_obj.get_property_str (ctx, "dispatchException");
assert (!dispatch_exception_func.is_undefined ());
dispatch_message_func = runtime_obj.get_property_str (ctx, "dispatchMessage");
assert (!dispatch_message_func.is_undefined ());
var native_pointer_instance = global.get_property_str (ctx, "NULL");
assert (!native_pointer_instance.is_undefined ());
var native_pointer_proto = native_pointer_instance.get_prototype (ctx);
var ir_proto = ctx.make_object_proto (native_pointer_proto);
add_cfunc (ir_proto, "replace", on_invocation_retval_replace, 1);
ctx.set_class_proto (invocation_retval_class, ir_proto);
ctx.free_value (native_pointer_proto);
ctx.free_value (native_pointer_instance);
ptr_func = global.get_property_str (ctx, "ptr");
assert (!ptr_func.is_undefined ());
int64_func = global.get_property_str (ctx, "int64");
assert (!int64_func.is_undefined ());
uint64_func = global.get_property_str (ctx, "uint64");
assert (!uint64_func.is_undefined ());
}
}
}
perform_pending_io ();
}
public void post (string json, Bytes? data) {
var json_val = ctx.make_string (json);
invoke_void (dispatch_message_func, { json_val }, runtime_obj);
ctx.free_value (json_val);
perform_pending_io ();
}
private void add_program (string source, string name) throws Error {
unowned string package_marker = "📦\n";
unowned string delimiter_marker = "\n✄\n";
unowned string alias_marker = "↻ ";
if (source.has_prefix (package_marker)) {
rt.set_module_loader_func (normalize_module_name, load_module);
string pending = source[package_marker.length:];
while (true) {
string[] pkg_tokens = pending.split (delimiter_marker, 2);
if (pkg_tokens.length != 2)
throw_malformed_package ();
unowned string header = pkg_tokens[0];
unowned string raw_assets = pkg_tokens[1];
uint assets_offset = 0;
uint assets_size = raw_assets.length;
Asset? entrypoint = null;
string[] header_lines = header.split ("\n");
Asset? current_asset = null;
for (uint i = 0; i != header_lines.length && assets_offset != assets_size; i++) {
unowned string header_line = header_lines[i];
if (header_line.has_prefix (alias_marker)) {
if (current_asset == null)
throw_malformed_package ();
string alias = header_line[alias_marker.length:];
assets[alias] = current_asset;
continue;
}
unowned string assets_cursor = (string *) raw_assets + assets_offset;
if (i != 0) {
if (!assets_cursor.has_prefix (delimiter_marker))
throw_malformed_package ();
assets_offset += delimiter_marker.length;
}
string[] tokens = header_line.split (" ", 2);
if (tokens.length != 2)
throw_malformed_package ();
uint64 size = uint64.parse (tokens[0]);
if (size == 0 || size > MAX_ASSET_SIZE || size > assets_size - assets_offset)
throw_malformed_package ();
unowned string asset_name = tokens[1];
string asset_data = raw_assets[assets_offset:assets_offset + (uint) size];
var asset = new Asset (asset_name, (owned) asset_data);
assets[asset_name] = asset;
current_asset = asset;
if (entrypoint == null && asset_name.has_suffix (".js"))
entrypoint = asset;
assets_offset += (uint) size;
}
if (entrypoint == null)
throw_malformed_package ();
var val = compile_module (entrypoint);
entrypoints.add (val);
string rest = raw_assets[assets_offset:];
if (rest.has_prefix (delimiter_marker))
pending = rest[delimiter_marker.length:];
else if (rest.length == 0)
break;
else
throw_malformed_package ();
}
} else {
var val = compile_script (source, name);
entrypoints.add (val);
}
}
[NoReturn]
private static void throw_malformed_package () throws Error {
throw new Error.INVALID_ARGUMENT ("Malformed package");
}
private string * normalize_module_name (QuickJS.Context ctx, string base_name, string name) {
if (name[0] != '.') {
Asset? asset = assets[name];
if (asset != null)
return ctx.strdup (asset.name);
return ctx.strdup (name);
}
var result = new StringBuilder ();
int offset = base_name.last_index_of_char ('/');
if (offset != -1)
result.append (base_name[:offset]);
string * cursor = name;
while (true) {
if (cursor->has_prefix ("./")) {
cursor += 2;
} else if (cursor->has_prefix ("../")) {
if (result.len == 0)
break;
int last_slash_offset = result.str.last_index_of_char ('/');
string * rest;
if (last_slash_offset != -1)
rest = (string *) result.str + last_slash_offset + 1;
else
rest = result.str;
if (rest == "." || rest == "..")
break;
result.truncate ((last_slash_offset != -1) ? last_slash_offset : 0);
cursor += 3;
} else {
break;
}
}
result
.append_c ('/')
.append (cursor);
return ctx.strdup (result.str);
}
private unowned QuickJS.ModuleDef? load_module (QuickJS.Context ctx, string module_name) {
QuickJS.Value val;
try {
Asset? asset = assets[module_name];
if (asset == null)
throw new Error.INVALID_ARGUMENT ("Could not load module '%s'", module_name);
val = compile_module (asset);
} catch (Error e) {
throw_js_error (error_message_to_js (e.message));
return null;
}
unowned QuickJS.ModuleDef mod = (QuickJS.ModuleDef) val.get_ptr ();
ctx.free_value (val);
return mod;
}
private QuickJS.Value compile_module (Asset asset) throws Error {
var val = ctx.eval (asset.data, asset.data.length, asset.name,
QuickJS.EvalType.MODULE |
QuickJS.EvalFlag.STRICT |
QuickJS.EvalFlag.COMPILE_ONLY);
if (val.is_exception ()) {
JSError e = catch_js_error ();
throw new Error.INVALID_ARGUMENT ("Could not parse '%s' line %u: %s", asset.name, e.line, e.message);
}
return val;
}
private QuickJS.Value compile_script (string source, string name) throws Error {
var val = ctx.eval (source, source.length, name,
QuickJS.EvalType.GLOBAL |
QuickJS.EvalFlag.STRICT |
QuickJS.EvalFlag.COMPILE_ONLY);
if (val.is_exception ()) {
JSError e = catch_js_error ();
throw new Error.INVALID_ARGUMENT ("Script(line %u): %s", e.line, e.message);
}
return val;
}
private static QuickJS.Value on_send (QuickJS.Context ctx, QuickJS.Value this_val, QuickJS.Value[] argv) {
BareboneScript * script = ctx.get_opaque ();
string message;
if (!script->unparse_string (argv[0], out message))
return QuickJS.Exception;
Bytes? data = null;
if (!argv[1].is_undefined () && !argv[1].is_null () && !script->unparse_bytes (argv[1], out data))
return QuickJS.Exception;
script->message (message, data);
return QuickJS.Undefined;
}
private static QuickJS.Value on_invoke (QuickJS.Context ctx, QuickJS.Value this_val, QuickJS.Value[] argv) {
BareboneScript * script = ctx.get_opaque ();
uint64 impl;
if (!script->unparse_uint64 (argv[0], out impl))
return QuickJS.Exception;
uint64[] args = {};
for (uint i = 1; i != argv.length; i++) {
uint64 v;
if (!script->unparse_uint64 (argv[i], out v))
return QuickJS.Exception;
args += v;
}
var promise = new Promise<uint64?> ();
script->do_invoke.begin (impl, args, promise);
uint64? retval = script->process_events_until_ready (promise);
if (retval == null)
return QuickJS.Exception;
return ctx.make_biguint64 (retval);
}
private async void do_invoke (uint64 impl, uint64[] args, Promise<uint64?> promise) {
try {
if (cached_landing_zone == null)
cached_landing_zone = yield services.allocator.allocate (4, 1, io_cancellable);
uint64 retval = yield services.machine.invoke (impl, args, cached_landing_zone.virtual_address,
io_cancellable);
promise.resolve (retval);
} catch (GLib.Error e) {
promise.reject (e);
}
}
private static QuickJS.Value on_install_native_callback (QuickJS.Context ctx, QuickJS.Value this_val,
QuickJS.Value[] argv) {
BareboneScript * script = ctx.get_opaque ();
uint64 code;
if (!script->unparse_uint64 (argv[0], out code))
return QuickJS.Exception;
QuickJS.Value wrapper, method;
var scope = new ValueScope (script);
wrapper = scope.retain (argv[1]);
if (!scope.unparse_callback (wrapper, script->invoke_key, out method))
return QuickJS.Exception;
uint arity;
if (!script->unparse_uint (argv[2], out arity))
return QuickJS.Exception;
var handler = new NativeCallbackHandler (script, wrapper, method, arity, scope);
var promise = new Promise<Barebone.Callback> ();
script->do_install_native_callback.begin (code, handler, promise);
Barebone.Callback? callback = script->process_events_until_ready (promise);
if (callback == null)
return QuickJS.Exception;
script->native_callbacks.add (callback);
return QuickJS.Undefined;
}
private async void do_install_native_callback (uint64 code, Barebone.CallbackHandler handler,
Promise<Barebone.Callback> promise) {
try {
var callback = yield new Barebone.Callback (code, handler, services.machine, io_cancellable);
promise.resolve (callback);
} catch (GLib.Error e) {
promise.reject (e);
}
}
private class NativeCallbackHandler : Object, Barebone.CallbackHandler {
public uint arity {
get { return _arity; }
}
private weak BareboneScript script;
private QuickJS.Value wrapper;
private QuickJS.Value method;
private uint _arity;
private ValueScope scope;
public NativeCallbackHandler (BareboneScript script, QuickJS.Value wrapper, QuickJS.Value method, uint arity,
ValueScope scope) {
this.script = script;
this.wrapper = wrapper;
this.method = method;
this._arity = arity;
this.scope = scope;
}
public async uint64 handle_invocation (uint64[] args, Barebone.CallFrame frame, Cancellable? cancellable)
throws Error, IOError {
var scope = new ValueScope (script);
unowned QuickJS.Context ctx = scope.ctx;
var js_args = scope.take (ctx.make_array ());
for (uint32 i = 0; i != args.length; i++)
js_args.set_property_uint32 (ctx, i, ctx.make_biguint64 (args[i]));
var return_address = scope.take (script.make_native_pointer (frame.return_address));
var context = scope.take (script.make_cpu_context (frame.registers));
var js_retval = script.invoke (method, { js_args, return_address, context }, wrapper);
if (js_retval.is_exception ())
return 0;
scope.take (js_retval);
uint64 retval;
if (!script.unparse_uint64 (js_retval, out retval)) {
script.catch_and_emit ();
return 0;
}
return retval;
}
}
private static QuickJS.Value on_evaluate (QuickJS.Context ctx, QuickJS.Value this_val, QuickJS.Value[] argv) {
BareboneScript * script = ctx.get_opaque ();
string name;
if (!script->unparse_string (argv[0], out name))
return QuickJS.Exception;
string source;
if (!script->unparse_string (argv[1], out source))
return QuickJS.Exception;
var func = ctx.eval (source, source.length, name,
QuickJS.EvalType.GLOBAL |
QuickJS.EvalFlag.STRICT |
QuickJS.EvalFlag.COMPILE_ONLY);
if (func.is_exception ()) {
JSError e = script->catch_js_error ();
script->throw_js_error ("could not parse '%s' line %u: %s".printf (name, e.line, e.message));
return QuickJS.Exception;
}
return ctx.eval_function (func);
}
private static QuickJS.Value on_next_tick (QuickJS.Context ctx, QuickJS.Value this_val, QuickJS.Value[] argv) {
BareboneScript * script = ctx.get_opaque ();
var callback = argv[0];
if (!callback.is_function (ctx)) {
script->throw_js_error ("expected a function");
return QuickJS.Exception;
}
script->tick_callbacks.offer (ctx.dup_value (callback));
return QuickJS.Undefined;
}
private QuickJS.Value make_native_pointer (uint64 val) {
var jsval = ctx.make_biguint64 (val);
var result = ptr_func.call (ctx, QuickJS.Undefined, { jsval });
ctx.free_value (jsval);
return result;
}
private QuickJS.Value make_int64 (int64 val) {
var jsval = ctx.make_bigint64 (val);
var result = int64_func.call (ctx, QuickJS.Undefined, { jsval });
ctx.free_value (jsval);
return result;
}
private QuickJS.Value make_uint64 (uint64 val) {
var jsval = ctx.make_biguint64 (val);
var result = uint64_func.call (ctx, QuickJS.Undefined, { jsval });
ctx.free_value (jsval);
return result;
}
private QuickJS.Value make_array_buffer_take (owned uint8[] contents) {
return ctx.make_array_buffer_with_free_func ((owned) contents, free_array_buffer, false);
}
private static void free_array_buffer (QuickJS.Runtime rt, void * ptr) {
free (ptr);
}
private QuickJS.Value make_cpu_context (Gee.Map<string, Variant> regs) {
var wrapper = ctx.make_object_class (cpu_context_class);
wrapper.set_opaque (regs.ref ());
return wrapper;
}
private static void on_cpu_context_finalize (QuickJS.Runtime rt, QuickJS.Value val) {
Gee.Map<string, Variant> * map = val.get_opaque (cpu_context_class);
map->unref ();
}
private static int on_cpu_context_get_own_property (QuickJS.Context ctx, QuickJS.PropertyDescriptor desc, QuickJS.Value obj,
QuickJS.Atom prop) {
BareboneScript * script = ctx.get_opaque ();
var val = script->read_cpu_context_field (obj, prop);
if (val.is_undefined ())
return 0;
desc.flags = ENUMERABLE;
desc.value = val;
desc.getter = QuickJS.Undefined;
desc.setter = QuickJS.Undefined;
return 1;
}
private static int on_cpu_context_get_own_property_names (QuickJS.Context ctx, out QuickJS.PropertyEnum * tab,
out uint32 len, QuickJS.Value obj) {
Gee.Map<string, Variant> * map = obj.get_opaque (cpu_context_class);
var keys = map->keys;
int n = keys.size;
tab = ctx.malloc (n * sizeof (QuickJS.PropertyEnum));
len = n;
int i = 0;
foreach (var key in keys) {
QuickJS.PropertyEnum * p = tab + i;
p->is_enumerable = true;
p->atom = ctx.make_atom (key);
i++;
}
return 0;
}
private static int on_cpu_context_has_property (QuickJS.Context ctx, QuickJS.Value obj, QuickJS.Atom atom) {
Gee.Map<string, Variant> * map = obj.get_opaque (cpu_context_class);
string * name = atom.to_cstring (ctx);
int result = map->has_key (name) ? 1 : 0;
ctx.free_cstring (name);
return result;
}
private static QuickJS.Value on_cpu_context_get_property (QuickJS.Context ctx, QuickJS.Value obj, QuickJS.Atom atom,
QuickJS.Value receiver) {
BareboneScript * script = ctx.get_opaque ();
return script->read_cpu_context_field (obj, atom);
}
private static int on_cpu_context_set_property (QuickJS.Context ctx, QuickJS.Value obj, QuickJS.Atom atom,
QuickJS.Value val, QuickJS.Value receiver, QuickJS.PropertyFlags flags) {
BareboneScript * script = ctx.get_opaque ();
return script->write_cpu_context_field (obj, atom, val) ? 0 : -1;
}
private QuickJS.Value read_cpu_context_field (QuickJS.Value obj, QuickJS.Atom atom) {
Gee.Map<string, Variant> * map = obj.get_opaque (cpu_context_class);
QuickJS.Value result = QuickJS.Undefined;
string * name = atom.to_cstring (ctx);
Variant? val = map->get (name);
if (val != null) {
if (val.is_of_type (VariantType.UINT64)) {
result = make_native_pointer (val.get_uint64 ());
} else if (val.is_of_type (VariantType.UINT32)) {
result = ctx.make_uint32 (val.get_uint32 ());
} else {
unowned uint8[] data = (uint8[]) val.get_data ();
result = ctx.make_array_buffer (data[:val.get_size ()]);
}
}
ctx.free_cstring (name);
return result;
}
private bool write_cpu_context_field (QuickJS.Value obj, QuickJS.Atom atom, QuickJS.Value val) {
Gee.Map<string, Variant> * map = obj.get_opaque (cpu_context_class);
string * name = atom.to_cstring (ctx);
try {
Variant? existing_val = map->get (name);
if (existing_val == null) {
throw_js_error ("invalid register name");
return false;
}
Variant new_val;
if (existing_val.is_of_type (VariantType.UINT64)) {
uint64 raw_val;
if (!unparse_uint64 (val, out raw_val))
return false;
new_val = raw_val;
} else if (existing_val.is_of_type (VariantType.UINT32)) {
uint32 raw_val;
if (!unparse_uint32 (val, out raw_val))
return false;
new_val = raw_val;
} else {
Bytes raw_val;
if (!unparse_bytes (val, out raw_val))
return false;
new_val = Variant.new_from_data (new VariantType ("ay"), raw_val.get_data (), true,
(owned) raw_val);
}
map->set (name, new_val);
map->set_data ("dirty", true);
} finally {
ctx.free_cstring (name);
}
return true;
}
private static QuickJS.Value on_memory_alloc (QuickJS.Context ctx, QuickJS.Value this_val, QuickJS.Value[] argv) {
BareboneScript * script = ctx.get_opaque ();
uint size;
if (!script->unparse_uint (argv[0], out size))
return QuickJS.Exception;
if (size == 0 || size > 0x7fffffff) {
script->throw_js_error ("invalid size");
return QuickJS.Exception;
}
var promise = new Promise<Barebone.Allocation> ();
script->do_memory_alloc.begin (size, promise);
Barebone.Allocation? allocation = script->process_events_until_ready (promise);
if (allocation == null)
return QuickJS.Exception;
// TODO: Monitor lifetime and deallocate().
return script->make_native_pointer (allocation.virtual_address);
}
private async void do_memory_alloc (size_t size, Promise<Barebone.Allocation> promise) {
try {
var allocator = services.allocator;
size_t page_size = allocator.page_size;
size_t alignment = (size % page_size) == 0 ? page_size : 16;
var allocation = yield allocator.allocate (size, alignment, io_cancellable);
Bytes zeroes = gdb.make_buffer_builder ()
.skip (size)
.build ();
yield gdb.write_byte_array (allocation.virtual_address, zeroes, io_cancellable);
promise.resolve (allocation);
} catch (GLib.Error e) {
promise.reject (e);
}
}