forked from memcached/memcached
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy_mutator.c
1140 lines (969 loc) · 33.8 KB
/
proxy_mutator.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
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "proxy.h"
/*
* !!!WARNING!!!
* This is an experimental interface and is not to be used in production until
* after this warning has been removed.
* The req/res mutator system is currently an experimental draft, merged to
* allow code experiments and further information gathering before completing
* the interface.
*
* it currently has bugs, unfinished features, and will not immediately
* release request or result buffers after a request completes.
*/
// space or \r\n
#define MIN_BUF_SPACE 2
enum mcp_mut_type {
MUT_REQ = 1,
MUT_RES,
};
enum mcp_mut_steptype {
mcp_mut_step_none = 0,
mcp_mut_step_cmdset,
mcp_mut_step_cmdcopy,
mcp_mut_step_keycopy,
mcp_mut_step_keyset,
mcp_mut_step_rescodeset,
mcp_mut_step_rescodecopy,
mcp_mut_step_reserr,
mcp_mut_step_flagset,
mcp_mut_step_flagcopy,
mcp_mut_step_valcopy,
mcp_mut_step_final, // not used.
};
enum mcp_mut_step_arg {
mcp_mut_step_arg_none = 0,
mcp_mut_step_arg_request,
mcp_mut_step_arg_response,
mcp_mut_step_arg_string,
mcp_mut_step_arg_int,
};
// START STEP STRUCTS
// struct forward declarations for entry/step function pointers
struct mcp_mut_step;
struct mcp_mutator;
struct mcp_mut_run;
struct mcp_mut_part;
typedef int (*mcp_mut_c)(lua_State *L, int tidx);
typedef int (*mcp_mut_i)(lua_State *L, int tidx, int sc, struct mcp_mutator *mut);
typedef int (*mcp_mut_r)(struct mcp_mut_run *run, struct mcp_mut_step *s, struct mcp_mut_part *p);
struct mcp_mut_entry {
const char *s; // string name
mcp_mut_c c; // argument checker
mcp_mut_i i; // argument initializer
mcp_mut_r n; // runtime length totaller
mcp_mut_r r; // runtime assembly
unsigned int t; // allowed object types
int rc; // number of results to expect
};
struct mcp_mut_string {
unsigned int str; // arena offset for match string.
unsigned int len;
};
struct mcp_mut_flag {
uint64_t bit; // flag converted for bitmask test
char f;
};
#define RESERR_ERROR 1
#define RESERR_ERROR_STR "ERROR"
#define RESERR_CLIENT 2
#define RESERR_CLIENT_STR "CLIENT_ERROR"
#define RESERR_SERVER 3
#define RESERR_SERVER_STR "SERVER_ERROR"
struct mcp_mut_flagval {
struct mcp_mut_flag flag;
struct mcp_mut_string str;
};
struct mcp_mut_step {
enum mcp_mut_steptype type;
unsigned int idx; // common: input argument position
enum mcp_mut_step_arg arg; // common: type of input argument
mcp_mut_r n; // totaller function
mcp_mut_r r; // data copy function
union {
struct mcp_mut_string string;
struct mcp_mut_flag flag;
struct mcp_mut_flagval flagval;
} c;
};
// END STEP STRUCTS
struct mcp_mutator {
enum mcp_mut_type type;
int scount;
unsigned int aused; // arena memory used
unsigned int rcount; // number of results to expect
char *arena; // string/data storage for steps
struct mcp_mut_step steps[];
};
// scratch space for steps between total and execution stages.
struct mcp_mut_part {
const char *src;
size_t slen;
};
// stack scratch variables for mutation execution
struct mcp_mut_run {
lua_State *L;
struct mcp_mutator *mut;
void *arg;
char *numbuf; // stack space for rendering numerics
char *d_pos; // current offset to the write string.
const char *vbuf; // buffer or ptr if a value is being attached
size_t vlen; // length of the actual value buffer.
};
#define mut_step_c(n) static int mcp_mutator_##n##_c(lua_State *L, int tidx)
#define mut_step_i(n) static int mcp_mutator_##n##_i(lua_State *L, int tidx, int sc, struct mcp_mutator *mut)
#define mut_step_r(n) static int mcp_mutator_##n##_r(struct mcp_mut_run *run, struct mcp_mut_step *s, struct mcp_mut_part *p)
#define mut_step_n(n) static int mcp_mutator_##n##_n(struct mcp_mut_run *run, struct mcp_mut_step *s, struct mcp_mut_part *p)
// PRIVATE INTERFACE
// COMMON ARG HANDLERS
static void _mut_check_idx(lua_State *L, int tidx) {
if (lua_getfield(L, tidx, "idx") != LUA_TNIL) {
luaL_checktype(L, -1, LUA_TNUMBER);
int isnum = 0;
lua_Integer i = lua_tointegerx(L, -1, &isnum);
if (!isnum) {
proxy_lua_ferror(L, "mutator step %d: must provide 'idx' argument as an integer", tidx);
}
if (i < 2) {
proxy_lua_ferror(L, "mutator step %d: 'idx' argument must be greater than 1", tidx);
}
} else {
proxy_lua_ferror(L, "mutator step %d: must provide 'idx' argument", tidx);
}
lua_pop(L, 1);
}
static size_t _mut_check_strlen(lua_State *L, int tidx, const char *n) {
size_t len = 0;
if (lua_getfield(L, tidx, n) != LUA_TNIL) {
lua_tolstring(L, -1, &len);
} else {
proxy_lua_ferror(L, "mutator step %d: must provide '%s' argument", tidx, n);
}
lua_pop(L, 1);
return len;
}
static void _mut_check_flag(lua_State *L, int tidx) {
if (lua_getfield(L, tidx, "flag") != LUA_TNIL) {
size_t len = 0;
const char *flag = lua_tolstring(L, -1, &len);
if (len != 1) {
proxy_lua_ferror(L, "mutator step %d: 'flag' must be a single character", tidx);
}
if (mcp_is_flag_invalid(flag[0])) {
proxy_lua_ferror(L, "mutator step %d: 'flag' must be alphanumeric", tidx);
}
} else {
proxy_lua_ferror(L, "mutator step %d: must provide 'flag' argument", tidx);
}
lua_pop(L, 1); // val or nil
}
static void _mut_init_flag(lua_State *L, int tidx, struct mcp_mut_flag *c) {
if (lua_getfield(L, tidx, "flag") != LUA_TNIL) {
const char *flag = lua_tostring(L, -1);
c->f = flag[0];
c->bit = (uint64_t)1 << (c->f - 65);
}
lua_pop(L, 1); // val or nil
}
// END COMMMON ARG HANDLERS
// START STEPS
// FIXME: decide on if this should take an mcp.CMD_etc instead, or at least
// optionally.
// we could also parse this into the int to avoid copying a string here.
// FIXME: track step progress and validate we're the first one.
mut_step_c(cmdset) {
size_t len = 0;
if (lua_getfield(L, tidx, "cmd") != LUA_TNIL) {
lua_tolstring(L, -1, &len);
// TODO: know either exact max length or parse for valid commands
// this small sanity check should help against user error for now.
if (len > 20) {
proxy_lua_ferror(L, "mutator step %d: 'cmd' too long", tidx);
}
} else {
proxy_lua_ferror(L, "mutator step %d: must provide 'cmd' argument", tidx);
}
lua_pop(L, 1);
return len;
}
mut_step_i(cmdset) {
struct mcp_mut_step *s = &mut->steps[sc];
struct mcp_mut_string *c = &s->c.string;
size_t len = 0;
if (lua_getfield(L, tidx, "cmd") != LUA_TNIL) {
const char *cmd = lua_tolstring(L, -1, &len);
c->str = mut->aused;
c->len = len;
char *a = mut->arena + mut->aused;
memcpy(a, cmd, len);
mut->aused += len;
}
lua_pop(L, 1);
return 0;
}
mut_step_n(cmdset) {
struct mcp_mut_string *c = &s->c.string;
return c->len;
}
mut_step_r(cmdset) {
struct mcp_mutator *mut = run->mut;
struct mcp_mut_string *c = &s->c.string;
const char *str = mut->arena + c->str;
memcpy(run->d_pos, str, c->len);
run->d_pos += c->len;
return 0;
}
// TODO: validate we're at the right stage to copy a command (no command set)
mut_step_c(cmdcopy) {
_mut_check_idx(L, tidx);
return 0;
}
mut_step_i(cmdcopy) {
struct mcp_mut_step *s = &mut->steps[sc];
if (lua_getfield(L, tidx, "idx") != LUA_TNIL) {
s->idx = lua_tointeger(L, -1);
}
lua_pop(L, 1);
return 0;
}
mut_step_n(cmdcopy) {
unsigned idx = s->idx;
// TODO: validate metatable matches or pull from cached entry
mcp_request_t *srq = lua_touserdata(run->L, idx);
// command must be at the start
const char *cmd = srq->pr.request;
// command ends at the first token
int clen = srq->pr.tokens[1];
if (cmd[clen] == ' ') {
clen--;
}
p->src = cmd;
p->slen = clen;
return clen;
}
mut_step_r(cmdcopy) {
memcpy(run->d_pos, p->src, p->slen);
run->d_pos += p->slen;
return 0;
}
// TODO: validate a cmd is already slated to be set
// NOTE: we might need to know the integer CMD because key position can move
// with the stupid GAT command.
mut_step_c(keycopy) {
_mut_check_idx(L, tidx);
return 0;
}
// FIXME: idx_i_g?
mut_step_i(keycopy) {
struct mcp_mut_step *s = &mut->steps[sc];
if (lua_getfield(L, tidx, "idx") != LUA_TNIL) {
s->idx = lua_tointeger(L, -1);
}
lua_pop(L, 1);
return 0;
}
mut_step_n(keycopy) {
unsigned idx = s->idx;
// TODO: validate metatable table matches or pull from cached entry
mcp_request_t *srq = lua_touserdata(run->L, idx);
p->src = MCP_PARSER_KEY(srq->pr);
p->slen = srq->pr.klen;
return p->slen;
}
mut_step_r(keycopy) {
memcpy(run->d_pos, p->src, p->slen);
run->d_pos += p->slen;
return 0;
}
// TODO: check we're okay to set a key
mut_step_c(keyset) {
size_t len = 0;
if (lua_getfield(L, tidx, "str") != LUA_TNIL) {
lua_tolstring(L, -1, &len);
if (len < 1) {
proxy_lua_ferror(L, "mutator step %d: 'str' must have nonzero length", tidx);
}
} else {
proxy_lua_ferror(L, "mutator step %d: must provide 'str' argument", tidx);
}
lua_pop(L, 1); // val or nil
return len;
}
mut_step_i(keyset) {
struct mcp_mut_step *s = &mut->steps[sc];
struct mcp_mut_string *c = &s->c.string;
size_t len = 0;
// store our match string in the arena space that we reserved before.
if (lua_getfield(L, tidx, "str") != LUA_TNIL) {
const char *str = lua_tolstring(L, -1, &len);
c->str = mut->aused;
c->len = len;
char *a = mut->arena + mut->aused;
memcpy(a, str, len);
mut->aused += len;
}
lua_pop(L, 1); // val or nil
return len;
}
mut_step_n(keyset) {
struct mcp_mut_string *c = &s->c.string;
return c->len;
}
mut_step_r(keyset) {
struct mcp_mutator *mut = run->mut;
struct mcp_mut_string *c = &s->c.string;
const char *str = mut->arena + c->str;
memcpy(run->d_pos, str, c->len);
run->d_pos += c->len;
return 0;
}
// TODO: ensure step is first
// TODO: pre-validate that it's an accepted code?
mut_step_c(rescodeset) {
return _mut_check_strlen(L, tidx, "str");
}
mut_step_i(rescodeset) {
struct mcp_mut_step *s = &mut->steps[sc];
struct mcp_mut_string *c = &s->c.string;
size_t len = 0;
if (lua_getfield(L, tidx, "str") != LUA_TNIL) {
const char *str = lua_tolstring(L, -1, &len);
c->str = mut->aused;
c->len = len;
char *a = mut->arena + mut->aused;
memcpy(a, str, len);
mut->aused += len;
}
lua_pop(L, 1);
return 0;
}
mut_step_n(rescodeset) {
struct mcp_mut_string *c = &s->c.string;
return c->len;
}
mut_step_r(rescodeset) {
struct mcp_mutator *mut = run->mut;
struct mcp_mut_string *c = &s->c.string;
const char *str = mut->arena + c->str;
memcpy(run->d_pos, str, c->len);
run->d_pos += c->len;
return 0;
}
// TODO: check we're the first step
mut_step_c(rescodecopy) {
_mut_check_idx(L, tidx);
return 0;
}
mut_step_i(rescodecopy) {
struct mcp_mut_step *s = &mut->steps[sc];
if (lua_getfield(L, tidx, "idx") != LUA_TNIL) {
s->idx = lua_tointeger(L, -1);
}
lua_pop(L, 1);
return 0;
}
mut_step_n(rescodecopy) {
unsigned idx = s->idx;
// TODO: validate metatable matches or pull from cached entry
mcp_resp_t *srs = lua_touserdata(run->L, idx);
// TODO: can't recover the exact from the mcmc resp object, so lets make
// sure it's tokenized and copy the first token.
if (srs->resp.type == MCMC_RESP_META) {
mcmc_tokenize_res(srs->buf, srs->resp.reslen, &srs->tok);
} else {
// FIXME: above only does meta responses
assert(1 == 0);
}
int len = 0;
p->src = mcmc_token_get(srs->buf, &srs->tok, 0, &len);
p->slen = len;
return len;
}
// TODO: take a string or number from that position.
mut_step_r(rescodecopy) {
// FIXME: error propagation
// FIXME: can we do all the error handling in the totalling phase?
if (p->slen < 2) {
return -1;
}
memcpy(run->d_pos, p->src, p->slen);
run->d_pos += p->slen;
return 0;
}
// TODO: can be no other steps after an error is set.
mut_step_c(reserr) {
size_t total = 0;
char *code = NULL;
// FIXME: add code length to len
if (lua_getfield(L, tidx, "code") != LUA_TNIL) {
const char *val = lua_tostring(L, -1);
if (strcmp(val, "error") == 0) {
code = RESERR_ERROR_STR;
} else if (strcmp(val, "server") == 0) {
code = RESERR_SERVER_STR;
} else if (strcmp(val, "client") == 0) {
code = RESERR_CLIENT_STR;
} else {
proxy_lua_ferror(L, "mutator step %d: mode must be 'error', server', or 'client'", tidx);
}
total += strlen(code);
} else {
proxy_lua_ferror(L, "mutator step %d: must provide 'mode' argument", tidx);
}
lua_pop(L, 1);
if (lua_getfield(L, tidx, "msg") != LUA_TNIL) {
size_t len = 0;
lua_tolstring(L, -1, &len);
if (len < 1) {
proxy_lua_ferror(L, "mutator step %d: 'msg' must be a nonzero length string", tidx);
}
total += len + 1; // room for space between code and msg
}
lua_pop(L, 1);
return total;
}
mut_step_i(reserr) {
struct mcp_mut_step *s = &mut->steps[sc];
struct mcp_mut_string *c = &s->c.string;
size_t len = 0;
const char *code = NULL;
char *a = mut->arena + mut->aused;
if (lua_getfield(L, tidx, "code") != LUA_TNIL) {
const char *val = lua_tostring(L, -1);
if (strcmp(val, "error") == 0) {
code = RESERR_ERROR_STR;
} else if (strcmp(val, "server") == 0) {
code = RESERR_SERVER_STR;
} else if (strcmp(val, "client") == 0) {
code = RESERR_CLIENT_STR;
} else {
// shouldn't be possible
proxy_lua_ferror(L, "mutator step %d: code must be 'error', server', or 'client'", tidx);
}
size_t clen = strlen(code);
memcpy(a, code, clen);
a += clen;
*a = ' ';
a++;
mut->aused += clen + 1;
} else {
proxy_lua_ferror(L, "mutator step %d: must provide 'code' argument", tidx);
}
lua_pop(L, 1);
if (lua_getfield(L, tidx, "msg") != LUA_TNIL) {
const char *str = lua_tolstring(L, -1, &len);
c->str = mut->aused;
c->len = len;
memcpy(a, str, len);
mut->aused += len;
} else {
// TODO: note no error msg
}
lua_pop(L, 1);
return len;
}
mut_step_n(reserr) {
struct mcp_mut_string *c = &s->c.string;
return c->len;
}
mut_step_r(reserr) {
struct mcp_mutator *mut = run->mut;
struct mcp_mut_string *c = &s->c.string;
const char *str = mut->arena + c->str;
int len = c->len;
// set error code first
memcpy(run->d_pos, str, len);
run->d_pos += len;
return 0;
}
// TODO: track which flags we've already set and error on dupes
mut_step_c(flagset) {
_mut_check_flag(L, tidx);
size_t len = 0;
int vtype = lua_getfield(L, tidx, "val");
if (vtype == LUA_TNUMBER || vtype == LUA_TSTRING) {
// this converts the arg into a string for us.
lua_tolstring(L, -1, &len);
} else if (vtype != LUA_TNIL) {
proxy_lua_ferror(L, "mutator step %d: unsupported type for 'val'", tidx);
}
lua_pop(L, 1);
return len;
}
mut_step_i(flagset) {
struct mcp_mut_step *s = &mut->steps[sc];
struct mcp_mut_flagval *c = &s->c.flagval;
size_t len = 0;
_mut_init_flag(L, tidx, &c->flag);
if (lua_getfield(L, tidx, "val") != LUA_TNIL) {
const char *str = lua_tolstring(L, -1, &len);
c->str.str = mut->aused;
c->str.len = len;
char *a = mut->arena + mut->aused;
memcpy(a, str, len);
mut->aused += len;
}
lua_pop(L, 1);
return len;
}
mut_step_n(flagset) {
struct mcp_mut_flagval *c = &s->c.flagval;
return c->str.len + 1; // room for flag
}
// TODO: validate we're okay to set flags.
// FIXME: triple check that this is actually the same code for req vs res?
// seems like it is.
mut_step_r(flagset) {
struct mcp_mutator *mut = run->mut;
struct mcp_mut_flagval *c = &s->c.flagval;
const char *str = mut->arena + c->str.str;
int len = c->str.len;
*(run->d_pos) = c->flag.f;
run->d_pos++;
if (len > 0) {
memcpy(run->d_pos, str, len);
run->d_pos += len;
}
return 0;
}
mut_step_c(flagcopy) {
_mut_check_flag(L, tidx);
_mut_check_idx(L, tidx);
return 0;
}
// TODO: maybe: optional default val if copy source missing
mut_step_i(flagcopy) {
struct mcp_mut_step *s = &mut->steps[sc];
struct mcp_mut_flag *c = &s->c.flag;
_mut_init_flag(L, tidx, c);
if (lua_getfield(L, tidx, "idx") != LUA_TNIL) {
s->idx = lua_tointeger(L, -1);
}
lua_pop(L, 1);
return 0;
}
// TODO: any reason for a req to pull from a res or vice versa?
// FIXME: handling when source flag doesn't exist might need a special case.
mut_step_n(flagcopy) {
struct mcp_mutator *mut = run->mut;
struct mcp_mut_flag *c = &s->c.flag;
unsigned idx = s->idx;
// TODO: validate idx or use cached entry.
if (mut->type == MUT_REQ) {
mcp_request_t *srq = lua_touserdata(run->L, idx);
if (srq->pr.cmd_type != CMD_TYPE_META) {
return -1;
}
if (srq->pr.t.meta.flags & c->bit) {
const char *tok = NULL;
size_t tlen = 0;
mcp_request_find_flag_token(srq, c->f, &tok, &tlen);
if (tlen > 0) {
p->src = tok;
p->slen = tlen;
} else {
p->slen = 0;
}
}
} else {
mcp_resp_t *srs = lua_touserdata(run->L, idx);
if (srs->resp.type != MCMC_RESP_META) {
// FIXME: error, can't copy flag from non-meta res
return -1;
}
mcmc_tokenize_res(srs->buf, srs->resp.reslen, &srs->tok);
if (mcmc_token_has_flag_bit(&srs->tok, c->bit) == MCMC_OK) {
// flag exists, so copy that in.
// realistically this func is only used if we're also copying a
// token, so we look for that too.
// could add an option to avoid checking for a token?
int len = 0;
const char *tok = mcmc_token_get_flag(srs->buf, &srs->tok, c->f, &len);
if (len > 0) {
p->src = tok;
p->slen = len;
} else {
p->slen = 0;
}
}
}
return 0;
}
mut_step_r(flagcopy) {
struct mcp_mut_flag *c = &s->c.flag;
*(run->d_pos) = c->f;
run->d_pos++;
if (p->slen) {
memcpy(run->d_pos, p->src, p->slen);
run->d_pos += p->slen;
}
return 0;
}
// TODO: check that the value hasn't been set yet.
mut_step_c(valcopy) {
_mut_check_idx(L, tidx);
// TODO: lift to common func
if (lua_getfield(L, tidx, "arg") == LUA_TSTRING) {
const char *a = lua_tostring(L, -1);
if (strcmp(a, "request") != 0 &&
strcmp(a, "response") != 0 &&
strcmp(a, "string") != 0 &&
strcmp(a, "int") != 0) {
proxy_lua_ferror(L, "mutator step %d: 'arg' must be request, response, string or int", tidx);
}
} else {
proxy_lua_ferror(L, "mutator step %d: missing 'arg' for input type", tidx);
}
lua_pop(L, 1);
return 0;
}
mut_step_i(valcopy) {
struct mcp_mut_step *s = &mut->steps[sc];
if (lua_getfield(L, tidx, "idx") != LUA_TNIL) {
s->idx = lua_tointeger(L, -1);
}
lua_pop(L, 1);
// TODO: lift to common func
if (lua_getfield(L, tidx, "arg") == LUA_TSTRING) {
const char *a = lua_tostring(L, -1);
if (strcmp(a, "request") == 0) {
s->arg = mcp_mut_step_arg_request;
} else if (strcmp(a, "response") == 0) {
s->arg = mcp_mut_step_arg_response;
} else if (strcmp(a, "string") == 0) {
s->arg = mcp_mut_step_arg_string;
} else if (strcmp(a, "int") == 0) {
s->arg = mcp_mut_step_arg_int;
} else {
proxy_lua_ferror(L, "mutator step %d: 'arg' must be request, response, string or int", tidx);
}
}
lua_pop(L, 1);
return 0;
}
mut_step_n(valcopy) {
lua_State *L = run->L;
unsigned idx = s->idx;
// extract the string + length we need to copy
mcp_request_t *srq;
//mcp_resp_t *srs;
// TODO: lift to common func?
// parts of it? with callback?
switch (s->arg) {
case mcp_mut_step_arg_none:
// can't get here.
break;
case mcp_mut_step_arg_request:
lua_getmetatable(L, idx);
lua_getiuservalue(L, 1, MUT_REQ);
if (lua_rawequal(L, -1, -2) == 0) {
// FIXME: error propagation
return -1;
}
lua_pop(L, 2);
srq = lua_touserdata(L, idx);
if (srq->pr.vbuf) {
run->vbuf = srq->pr.vbuf;
run->vlen = srq->pr.vlen;
}
break;
case mcp_mut_step_arg_response:
lua_getmetatable(L, idx);
lua_getiuservalue(L, 1, MUT_RES);
if (lua_rawequal(L, -1, -2) == 0) {
// FIXME: error propagation
return -1;
}
lua_pop(L, 2);
assert(1 == 0);
//srs = lua_touserdata(L, idx);
// TODO: need to locally parse the result to get the actual val
// offsets until later refactoring takes place.
break;
case mcp_mut_step_arg_string:
if (lua_isstring(L, idx)) {
// TODO: do we require the user supplies "\r\n"?
// detect it here and add a flag or adjust or etc?
run->vbuf = lua_tolstring(L, idx, &run->vlen);
}
break;
case mcp_mut_step_arg_int:
// TODO: just do number instead of int? meh.
// we want to not auto-convert this to a string.
lua_isnumber(L, idx);
assert(1 == 0); // TODO: unimplemented.
break;
}
// count the number of digits in vlen to reserve space.
//
// oddly algorithms to count digits and write digits are similar (outside
// of hyper optimization via bit math), so just reuse the same code here.
// if I can ever get arena allocations to work and remove the pre-calc
// steps this is moot anyway.
char temp[22];
const char *e = itoa_u64(run->vlen, temp);
return e - temp;
}
// print the vlen into the buffer
// we remove the \r\n from the protocol length
mut_step_r(valcopy) {
run->d_pos = itoa_u64(run->vlen-2, run->d_pos);
return 0;
}
// END STEPS
static const struct mcp_mut_entry mcp_mut_entries[] = {
[mcp_mut_step_none] = {NULL, NULL, NULL, NULL, NULL, 0, 0},
[mcp_mut_step_cmdset] = {"cmdset", mcp_mutator_cmdset_c, mcp_mutator_cmdset_i, mcp_mutator_cmdset_n, mcp_mutator_cmdset_r, MUT_REQ, 0},
[mcp_mut_step_cmdcopy] = {"cmdcopy", mcp_mutator_cmdcopy_c, mcp_mutator_cmdcopy_i, mcp_mutator_cmdcopy_n, mcp_mutator_cmdcopy_r, MUT_REQ, 0},
[mcp_mut_step_keycopy] = {"keycopy", mcp_mutator_keycopy_c, mcp_mutator_keycopy_i, mcp_mutator_keycopy_n, mcp_mutator_keycopy_r, MUT_REQ, 0},
[mcp_mut_step_keyset] = {"keyset", mcp_mutator_keyset_c, mcp_mutator_keyset_i, mcp_mutator_keyset_n, mcp_mutator_keyset_r, MUT_REQ, 0},
[mcp_mut_step_rescodeset] = {"rescodeset", mcp_mutator_rescodeset_c, mcp_mutator_rescodeset_i, mcp_mutator_rescodeset_n, mcp_mutator_rescodeset_r, MUT_RES, 0},
[mcp_mut_step_rescodecopy] = {"rescodecopy", mcp_mutator_rescodecopy_c, mcp_mutator_rescodecopy_i, mcp_mutator_rescodecopy_n, mcp_mutator_rescodecopy_r, MUT_RES, 0},
[mcp_mut_step_reserr] = {"reserr", mcp_mutator_reserr_c, mcp_mutator_reserr_i, mcp_mutator_reserr_n, mcp_mutator_reserr_r, MUT_RES, 0},
[mcp_mut_step_flagset] = {"flagset", mcp_mutator_flagset_c, mcp_mutator_flagset_i, mcp_mutator_flagset_n, mcp_mutator_flagset_r, MUT_REQ|MUT_RES, 0},
[mcp_mut_step_flagcopy] = {"flagcopy", mcp_mutator_flagcopy_c, mcp_mutator_flagcopy_i, mcp_mutator_flagcopy_n, mcp_mutator_flagcopy_r, MUT_REQ|MUT_RES, 0},
[mcp_mut_step_valcopy] = {"valcopy", mcp_mutator_valcopy_c, mcp_mutator_valcopy_i, mcp_mutator_valcopy_n, mcp_mutator_valcopy_r, MUT_REQ|MUT_RES, 0},
[mcp_mut_step_final] = {NULL, NULL, NULL, NULL, NULL, 0, 0},
};
// call with type string on top
static enum mcp_mut_steptype mcp_mutator_steptype(lua_State *L) {
const char *type = luaL_checkstring(L, -1);
for (int x = 0; x < mcp_mut_step_final; x++) {
const struct mcp_mut_entry *e = &mcp_mut_entries[x];
if (e->s && strcmp(type, e->s) == 0) {
return x;
}
}
return mcp_mut_step_none;
}
static int mcp_mutator_new(lua_State *L, enum mcp_mut_type type) {
int argc = lua_gettop(L);
size_t size = 0;
int scount = 0;
// loop argument tables once for validation and pre-calculations.
for (int x = 1; x <= argc; x++) {
luaL_checktype(L, x, LUA_TTABLE);
if (lua_getfield(L, x, "t") != LUA_TNIL) {
enum mcp_mut_steptype st = mcp_mutator_steptype(L);
const struct mcp_mut_entry *e = &mcp_mut_entries[st];
if (!(e->t & type)) {
proxy_lua_ferror(L, "mutator step %d: step incompatible with mutator type", x);
}
if ((st == mcp_mut_step_none) || e->c == NULL) {
proxy_lua_ferror(L, "mutator step %d: unknown step type", x);
}
size += e->c(L, x);
}
lua_pop(L, 1); // drop 't' or nil
scount++;
}
// we now know the size and number of steps. allocate some flat memory.
size_t extsize = sizeof(struct mcp_mut_step) * scount;
struct mcp_mutator *mut = lua_newuserdatauv(L, sizeof(*mut) + extsize, 2);
memset(mut, 0, sizeof(*mut));
mut->arena = malloc(size);
if (mut->arena == NULL) {
proxy_lua_error(L, "mutator_new: failed to allocate memory");
}
luaL_setmetatable(L, "mcp.mutator");
mut->type = type;
// Cache both request and result metatables for arg validation.
// Since a req mutator can take a res argument and vice versa
luaL_getmetatable(L, "mcp.request");
lua_setiuservalue(L, -2, MUT_REQ);
luaL_getmetatable(L, "mcp.response");
lua_setiuservalue(L, -2, MUT_RES);
// loop the arg tables again to fill in the steps
// skip checks since we did that during the first loop.
scount = 0;
for (int x = 1; x <= argc; x++) {
if (lua_getfield(L, x, "t") != LUA_TNIL) {
enum mcp_mut_steptype st = mcp_mutator_steptype(L);
mut->steps[scount].type = st;
mcp_mut_entries[st].i(L, x, scount, mut);
mut->rcount += mcp_mut_entries[st].rc;
// copy function pointers into the step so we don't have to skip
// around the much larger mcp_mut_entries at runtime.
mut->steps[scount].n = mcp_mut_entries[st].n;
mut->steps[scount].r = mcp_mut_entries[st].r;
mut->steps[scount].idx++; // actual args are "self, etc, etc"
}
lua_pop(L, 1); // drop t or nil
scount++;
}
if (size != mut->aused) {
proxy_lua_error(L, "mutator failed to properly initialize, memory not filled correctly");
}
mut->scount = scount;
return 1;
}
static inline int _mcp_mut_run_total(struct mcp_mut_run *run, struct mcp_mut_part *parts) {
int total = 0;
struct mcp_mutator *mut = run->mut;
for (int x = 0; x < mut->scount; x++) {
struct mcp_mut_step *s = &mut->steps[x];
assert(s->type != mcp_mut_step_none);
int len = s->n(run, s, &parts[x]);
if (len < 0) {
assert(1 == 0);
break;
} else {
total += len;
}
}
// account for spaces between "steps" and \r\n
// note that steps that themselves can make multiple tokens _must_ account
// for this on their own.
total += mut->scount + MIN_BUF_SPACE;
return total;
}
static inline int _mcp_mut_run_assemble(struct mcp_mut_run *run, struct mcp_mut_part *parts) {
struct mcp_mutator *mut = run->mut;
// assemble final req/res
for (int x = 0; x < mut->scount; x++) {
struct mcp_mut_step *s = &mut->steps[x];
assert(s->type != mcp_mut_step_none);
// TODO: handle -1 errors, halt.
// TODO: can we structure this so we don't have to check for errors at
// this stage, only the first one? would be nice to cut the branch out
if (s->r(run, s, &parts[x]) < 0) {
assert(1 == 0);
}
*(run->d_pos) = ' ';
run->d_pos++;
}
// TODO: any cases where we need to check if the final char is a space or
// not?
// add the \r\n after all steps
*(run->d_pos-1) = '\r';
*(run->d_pos) = '\n';
run->d_pos++;
return 0;
}
static int mcp_mut_run(struct mcp_mut_run *run) {
struct mcp_mutator *mut = run->mut;
LIBEVENT_THREAD *t = PROXY_GET_THR(run->L);
int ret = 0;
struct mcp_mut_part parts[mut->scount];
// first accumulate the length tally
int total = _mcp_mut_run_total(run, parts);
// FIXME: error handling from total call
// ensure space and/or allocate memory then seed our destination pointer.