-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaaf_mutator.cc
More file actions
780 lines (697 loc) · 29.2 KB
/
Copy pathlaaf_mutator.cc
File metadata and controls
780 lines (697 loc) · 29.2 KB
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
#include "laaf_mutator.h"
#include <algorithm>
#include <cstring>
// ============================================================
// Boundary data pools — expanded to full random ranges
// ============================================================
static re2::Rune random_rune(RNG& rng) {
// Full Unicode range with bias toward boundary values
int choice = rng.uniform(10);
if (choice == 0) return 0;
if (choice == 1) return rng.uniform(128); // ASCII
if (choice == 2) return 0x7F + rng.uniform(129); // Latin-1
if (choice == 3) return 0xD7FF + rng.uniform(1024); // surrogate range
return static_cast<re2::Rune>(rng.next() % 0x110000); // full Unicode
}
static int random_min(RNG& rng) {
int choice = rng.uniform(8);
if (choice == 0) return 0;
if (choice == 1) return 1;
if (choice == 2) return rng.uniform(11); // 0-10
if (choice == 3) return rng.uniform(101); // 0-100
return rng.next() % 65536; // full range
}
static int random_max(RNG& rng) {
int choice = rng.uniform(10);
if (choice == 0) return -1; // unlimited
if (choice == 1) return rng.uniform(11); // 0-10
if (choice == 2) return rng.uniform(101); // 0-100
return rng.next() % 65536; // full range
}
static re2::Rune random_cc_boundary(RNG& rng) {
int choice = rng.uniform(8);
if (choice == 0) return '0';
if (choice == 1) return '9';
if (choice == 2) return 'A';
if (choice == 3) return 'a';
if (choice == 4) return 'z';
if (choice == 5) return 0x00;
if (choice == 6) return 0xFF;
return static_cast<re2::Rune>(rng.next() % 0x110000);
}
// ============================================================
// Helper: create a simple Literal node
// ============================================================
static re2::Regexp* make_literal(re2::Rune r, re2::Regexp::ParseFlags flags) {
return re2::Regexp::NewLiteral(r, flags);
}
// Helper: check if quantifier op
static bool is_quantifier(re2::RegexpOp op) {
return op == re2::kRegexpStar ||
op == re2::kRegexpPlus ||
op == re2::kRegexpQuest ||
op == re2::kRegexpRepeat;
}
// Helper: make a quantifier wrapping a sub
static re2::Regexp* make_quantifier(re2::RegexpOp op, re2::Regexp* sub,
re2::Regexp::ParseFlags flags, int min = 0, int max = -1) {
switch (op) {
case re2::kRegexpStar: return re2::Regexp::Star(sub, flags);
case re2::kRegexpPlus: return re2::Regexp::Plus(sub, flags);
case re2::kRegexpQuest: return re2::Regexp::Quest(sub, flags);
case re2::kRegexpRepeat: return re2::Regexp::Repeat(sub, flags, min, max);
default: return sub;
}
}
// ============================================================
// M1: Literal Boundary Mutation
// ============================================================
bool LiteralMutator::can_mutate(const Slot& slot) const {
return slot.kind == re2::kRegexpLiteral ||
slot.kind == re2::kRegexpLiteralString ||
slot.kind == re2::kRegexpAnyChar ||
slot.kind == re2::kRegexpAnyByte;
}
bool LiteralMutator::mutate(re2::Regexp* /*root*/, const std::vector<Slot>& slots, RNG& rng) {
auto candidates = filter(slots, [](const Slot& s) {
return s.kind == re2::kRegexpLiteral ||
s.kind == re2::kRegexpLiteralString ||
s.kind == re2::kRegexpAnyChar ||
s.kind == re2::kRegexpAnyByte;
});
if (candidates.empty()) return false;
const auto& target = candidates[rng.uniform(candidates.size())];
re2::Regexp* node = *target.ptr;
re2::Regexp::ParseFlags flags = node->parse_flags();
switch (node->op()) {
case re2::kRegexpLiteral: {
// Replace the literal rune with a boundary value
re2::Rune new_rune = random_rune(rng);
re2::Regexp* newnode = make_literal(new_rune, flags);
return laaf_replace(nullptr, target, newnode);
}
case re2::kRegexpLiteralString: {
int n = node->nrunes();
if (n == 0) {
// Empty string -> replace with a boundary literal
re2::Rune r = random_rune(rng);
re2::Regexp* newnode = make_literal(r, flags);
return laaf_replace(nullptr, target, newnode);
}
// Modify one character in the string
re2::Rune* newrunes = new re2::Rune[n];
std::memcpy(newrunes, node->runes(), n * sizeof(re2::Rune));
int pos = rng.uniform(n);
newrunes[pos] = random_rune(rng);
re2::Regexp* newnode = re2::Regexp::LiteralString(newrunes, n, flags);
delete[] newrunes; // LiteralString copies the array
return laaf_replace(nullptr, target, newnode);
}
case re2::kRegexpAnyChar:
case re2::kRegexpAnyByte: {
// Replace AnyChar/AnyByte with a specific literal
re2::Rune r = random_rune(rng);
re2::RegexpOp newop = (node->op() == re2::kRegexpAnyByte && r > 255)
? re2::kRegexpAnyChar : re2::kRegexpLiteral;
re2::Regexp* newnode;
if (newop == re2::kRegexpLiteral)
newnode = make_literal(r & 0xFF, flags);
else
newnode = make_literal(r, flags);
return laaf_replace(nullptr, target, newnode);
}
default:
return false;
}
}
// ============================================================
// M2: Operator Morphing
// ============================================================
bool OperatorMorphMutator::can_mutate(const Slot& slot) const {
return is_quantifier(slot.kind) ||
slot.kind == re2::kRegexpCapture ||
slot.kind == re2::kRegexpConcat ||
slot.kind == re2::kRegexpAlternate;
}
bool OperatorMorphMutator::mutate(re2::Regexp* /*root*/, const std::vector<Slot>& slots, RNG& rng) {
auto candidates = filter(slots, [](const Slot& s) {
return is_quantifier(s.kind) ||
s.kind == re2::kRegexpCapture ||
s.kind == re2::kRegexpConcat ||
s.kind == re2::kRegexpAlternate;
});
if (candidates.empty()) return false;
const auto& target = candidates[rng.uniform(candidates.size())];
re2::Regexp* node = *target.ptr;
re2::Regexp::ParseFlags flags = node->parse_flags();
if (is_quantifier(node->op()) && node->nsub() == 1) {
// Morph between Star/Plus/Quest and to/from Repeat
re2::Regexp* child = node->sub()[0];
child->Incref();
int mode = rng.uniform(5);
re2::Regexp* newnode = nullptr;
switch (mode) {
case 0: newnode = re2::Regexp::Star(child, flags); break;
case 1: newnode = re2::Regexp::Plus(child, flags); break;
case 2: newnode = re2::Regexp::Quest(child, flags); break;
case 3: newnode = re2::Regexp::Repeat(child, flags, 0, 5); break;
case 4: newnode = re2::Regexp::Repeat(child, flags, 1, -1); break;
}
return laaf_replace(nullptr, target, newnode);
}
if (node->op() == re2::kRegexpCapture && node->nsub() == 1) {
// Unwrap: remove capture, expose child
re2::Regexp* child = node->sub()[0];
child->Incref();
return laaf_replace(nullptr, target, child);
}
if ((node->op() == re2::kRegexpConcat || node->op() == re2::kRegexpAlternate) && node->nsub() > 1) {
// Swap Concat <-> Alternate
re2::RegexpOp newop = (node->op() == re2::kRegexpConcat)
? re2::kRegexpAlternate : re2::kRegexpConcat;
int n = node->nsub();
re2::Regexp** subs = new re2::Regexp*[n];
for (int i = 0; i < n; i++) { subs[i] = node->sub()[i]; subs[i]->Incref(); }
re2::Regexp* newnode = (newop == re2::kRegexpConcat)
? re2::Regexp::Concat(subs, n, flags)
: re2::Regexp::Alternate(subs, n, flags);
delete[] subs;
return laaf_replace(nullptr, target, newnode);
}
// Wrap any non-capture node with Capture
if (node->op() != re2::kRegexpCapture &&
node->op() != re2::kRegexpConcat &&
node->op() != re2::kRegexpAlternate) {
node->Incref();
re2::Regexp* newnode = re2::Regexp::Capture(node, flags, 1);
return laaf_replace(nullptr, target, newnode);
}
return false;
}
// ============================================================
// M3: Same-Kind Subtree Replacement
// ============================================================
bool SubtreeReplaceMutator::can_mutate(const Slot& slot) const {
(void)slot;
return true; // any slot can be replaced if a compatible donor exists
}
bool SubtreeReplaceMutator::mutate(re2::Regexp* /*root*/, const std::vector<Slot>& slots, RNG& rng) {
if (slots.size() < 2) return false;
// Level 1: try exact same kind first (safe)
for (int attempt = 0; attempt < 10; attempt++) {
const auto& target = slots[rng.uniform(slots.size())];
for (int d = 0; d < 5; d++) {
const auto& donor = slots[rng.uniform(slots.size())];
if (&target == &donor) continue;
// Donor must not be inside target's subtree
// (simple check: if both are from same parent, skip if target.index < donor.index for list parents)
if (target.kind == donor.kind) {
re2::Regexp* replacement = laaf_clone(*donor.ptr);
return laaf_replace(nullptr, target, replacement);
}
}
}
// Level 2: try compatible quantifier kinds
for (int attempt = 0; attempt < 5; attempt++) {
const auto& target = slots[rng.uniform(slots.size())];
const auto& donor = slots[rng.uniform(slots.size())];
if (&target == &donor) continue;
if (is_quantifier(target.kind) && is_quantifier(donor.kind)) {
re2::Regexp* replacement = laaf_clone(*donor.ptr);
return laaf_replace(nullptr, target, replacement);
}
}
// Level 3: any node with same child count
for (int attempt = 0; attempt < 5; attempt++) {
const auto& target = slots[rng.uniform(slots.size())];
const auto& donor = slots[rng.uniform(slots.size())];
if (&target == &donor) continue;
bool target_haschild = ((*target.ptr)->nsub() > 0);
bool donor_haschild = ((*donor.ptr)->nsub() > 0);
if (target_haschild == donor_haschild) {
re2::Regexp* replacement = laaf_clone(*donor.ptr);
return laaf_replace(nullptr, target, replacement);
}
}
return false;
}
// ============================================================
// M4: List Mutation
// ============================================================
bool ListMutator::can_mutate(const Slot& slot) const {
return slot.in_list; // parent is Concat or Alternate
}
// Build Concat or Alternate from subs array (null-safe)
static re2::Regexp* make_list_node(re2::RegexpOp op, re2::Regexp** raw_subs, int count,
re2::Regexp::ParseFlags flags) {
// Filter out null entries
std::vector<re2::Regexp*> valid;
for (int i = 0; i < count; i++) {
if (raw_subs[i]) valid.push_back(raw_subs[i]);
}
if (valid.empty()) {
// Fallback: return an empty match
return re2::Regexp::NewLiteral(re2::Rune('x'), flags);
}
if (valid.size() == 1) {
valid[0]->Incref();
return valid[0];
}
if (op == re2::kRegexpConcat)
return re2::Regexp::Concat(valid.data(), static_cast<int>(valid.size()), flags);
else
return re2::Regexp::Alternate(valid.data(), static_cast<int>(valid.size()), flags);
}
bool ListMutator::mutate(re2::Regexp* /*root*/, const std::vector<Slot>& slots, RNG& rng) {
auto candidates = filter(slots, [](const Slot& s) { return s.in_list; });
if (candidates.empty()) return false;
// Group slots by parent
std::vector<re2::Regexp*> parents;
for (const auto& s : candidates) {
if (!s.parent) continue;
if (std::find(parents.begin(), parents.end(), s.parent) == parents.end())
parents.push_back(s.parent);
}
if (parents.empty()) return false;
re2::Regexp* parent = parents[rng.uniform(parents.size())];
if (!parent) return false;
int n = parent->nsub();
if (n == 0) return false;
// Verify all subs are valid
bool all_valid = true;
for (int i = 0; i < n; i++) {
if (!parent->sub()[i]) { all_valid = false; break; }
}
if (!all_valid) return false;
re2::Regexp::ParseFlags flags = parent->parse_flags();
int op = rng.uniform(5); // 5 operations: dup, swap, insert, shuffle, truncate
// Find a slot that points TO this parent (from grandparent)
Slot parent_slot = candidates[rng.uniform(candidates.size())];
// Try to find a slot whose child IS this parent
std::vector<Slot> all_slots = laaf_collect_slots(const_cast<re2::Regexp*>(parent)); // not really needed
(void)all_slots;
if (op == 0 && n < 100) {
// Duplicate a random child
int dup = rng.uniform(n);
int new_n = n + 1;
re2::Regexp** subs = new re2::Regexp*[new_n];
for (int i = 0; i < new_n; i++) {
int src = (i <= dup) ? i : i - 1;
subs[i] = parent->sub()[src];
if (subs[i]) subs[i]->Incref();
}
// Insert the duplicate at position dup
// subs[dup] is already set to parent->sub()[dup] from the loop above
// but we need another copy at dup+1 ... actually let me re-do this correctly
// Correctly: original at positions 0..n-1; insert duplicate of element 'dup' right after it
for (int i = 0; i < new_n; i++) {
int src;
if (i <= dup) src = i;
else src = i - 1;
subs[i] = parent->sub()[src];
if (subs[i]) subs[i]->Incref();
}
// Now also add a duplicate of dup at position dup+1... no wait, we already have the original at dup
// We want: [0..dup, dup, dup+1..n-1]
// The loop above gives: [0..dup, dup+1..n-1] with dup element appearing once
// Let's do it more clearly:
delete[] subs;
subs = new re2::Regexp*[new_n];
for (int i = 0, j = 0; i < n; i++, j++) {
subs[j] = parent->sub()[i];
if (subs[j]) subs[j]->Incref();
if (i == dup) {
j++;
subs[j] = parent->sub()[i]; // duplicate
if (subs[j]) subs[j]->Incref();
}
}
re2::Regexp* newnode = make_list_node(parent->op(), subs, new_n, flags);
delete[] subs;
// Replace the parent itself by finding the right slot
for (const auto& s : slots) {
if (*s.ptr == parent) {
const_cast<Slot&>(s).ptr = const_cast<re2::Regexp**>(s.ptr);
return laaf_replace(nullptr, s, newnode);
}
}
// If we can't find the parent's slot, free the new node and fail
// laaf_free(newnode); // skip to avoid use-after-free of shared children
return false;
}
if (op == 1 && n >= 2) {
// Swap two adjacent children
int idx = rng.uniform(n - 1);
re2::Regexp** subs = new re2::Regexp*[n];
for (int i = 0; i < n; i++) {
subs[i] = parent->sub()[i];
if (subs[i]) subs[i]->Incref();
}
std::swap(subs[idx], subs[idx + 1]);
re2::Regexp* newnode = make_list_node(parent->op(), subs, n, flags);
delete[] subs;
for (const auto& s : slots) {
if (*s.ptr == parent) {
return laaf_replace(nullptr, s, newnode);
}
}
// laaf_free(newnode); // skip to avoid use-after-free of shared children
return false;
}
if (op == 2 && n < 100) {
// Insert a random literal at a random position
int pos = rng.uniform(n + 1);
int new_n = n + 1;
re2::Regexp** subs = new re2::Regexp*[new_n];
for (int i = 0, src = 0; i < new_n; i++) {
if (i == pos) {
re2::Rune r = random_rune(rng);
subs[i] = make_literal(r, flags);
} else {
subs[i] = parent->sub()[src++];
if (subs[i]) subs[i]->Incref();
}
}
re2::Regexp* newnode = make_list_node(parent->op(), subs, new_n, flags);
delete[] subs;
for (const auto& s : slots) {
if (*s.ptr == parent) {
return laaf_replace(nullptr, s, newnode);
}
}
// laaf_free(newnode); // skip to avoid use-after-free of shared children
return false;
}
if (op == 3 && n >= 2) {
// Shuffle (random reorder)
re2::Regexp** subs = new re2::Regexp*[n];
for (int i = 0; i < n; i++) {
subs[i] = parent->sub()[i];
if (subs[i]) subs[i]->Incref();
}
// Fisher-Yates shuffle
for (int i = n - 1; i > 0; i--) {
int j = rng.uniform(i + 1);
std::swap(subs[i], subs[j]);
}
re2::Regexp* newnode = make_list_node(parent->op(), subs, n, flags);
delete[] subs;
for (const auto& s : slots) {
if (*s.ptr == parent) {
return laaf_replace(nullptr, s, newnode);
}
}
// laaf_free(newnode); // skip to avoid use-after-free of shared children
return false;
}
if (op == 4 && n >= 3) {
// Truncate: keep first half
int keep = std::max(2, n / 2);
int new_n = keep;
re2::Regexp** subs = new re2::Regexp*[new_n];
for (int i = 0; i < new_n; i++) {
subs[i] = parent->sub()[i];
if (subs[i]) subs[i]->Incref();
}
re2::Regexp* newnode = make_list_node(parent->op(), subs, new_n, flags);
delete[] subs;
for (const auto& s : slots) {
if (*s.ptr == parent) {
return laaf_replace(nullptr, s, newnode);
}
}
// laaf_free(newnode); // skip to avoid use-after-free of shared children
return false;
}
return false;
}
// ============================================================
// M5: Repetition Boundary Mutation
// ============================================================
bool RepetitionMutator::can_mutate(const Slot& slot) const {
return slot.kind == re2::kRegexpRepeat;
}
bool RepetitionMutator::mutate(re2::Regexp* /*root*/, const std::vector<Slot>& slots, RNG& rng) {
auto candidates = filter(slots, [](const Slot& s) {
return s.kind == re2::kRegexpRepeat;
});
if (candidates.empty()) return false;
const auto& target = candidates[rng.uniform(candidates.size())];
re2::Regexp* node = *target.ptr;
re2::Regexp* child = node->sub()[0];
re2::Regexp::ParseFlags flags = node->parse_flags();
int old_min = node->min();
int old_max = node->max();
int mode = rng.uniform(4);
int new_min = old_min, new_max = old_max;
switch (mode) {
case 0: new_min = random_min(rng); break;
case 1: new_max = random_max(rng); break;
case 2: { int d = rng.uniform(5) - 2; new_min = std::max(0, old_min + d); break; }
case 3: { int d = rng.uniform(7) - 3; new_max = std::max(-1, (old_max == -1) ? 10 : old_max + d); break; }
}
// Don't mutate if nothing changed
if (new_min == old_min && new_max == old_max) return false;
// Enforce validity: max must be -1 (unlimited) or >= min
if (new_max != -1 && new_max < new_min) {
if (new_max < 0) new_max = new_min;
else std::swap(new_min, new_max);
}
// Clamp to reasonable range for RE2
if (new_min < 0) new_min = 0;
if (new_max > 10000) new_max = std::max(new_min, 100);
child->Incref();
re2::Regexp* newnode = re2::Regexp::Repeat(child, flags, new_min, new_max);
return laaf_replace(nullptr, target, newnode);
}
// ============================================================
// M6: CharClass Mutation
// ============================================================
bool CharClassMutator::can_mutate(const Slot& slot) const {
return slot.kind == re2::kRegexpCharClass;
}
bool CharClassMutator::mutate(re2::Regexp* /*root*/, const std::vector<Slot>& slots, RNG& rng) {
auto candidates = filter(slots, [](const Slot& s) {
return s.kind == re2::kRegexpCharClass;
});
if (candidates.empty()) return false;
const auto& target = candidates[rng.uniform(candidates.size())];
re2::Regexp* node = *target.ptr;
re2::Regexp::ParseFlags flags = node->parse_flags();
re2::CharClass* cc = node->cc();
if (!cc || cc->empty()) {
// Replace empty charclass with a literal
re2::Rune r = random_cc_boundary(rng);
re2::Regexp* newnode = make_literal(r, flags);
return laaf_replace(nullptr, target, newnode);
}
// Clone existing ranges and mutate
int nranges = 0;
for (auto it = cc->begin(); it != cc->end(); ++it) nranges++;
int mode = rng.uniform(5);
if (mode == 0) {
// Expand a range's hi bound
int idx = rng.uniform(nranges);
auto it = cc->begin();
for (int i = 0; i < idx; i++) ++it;
re2::Rune new_hi = random_cc_boundary(rng);
if (new_hi < it->lo) new_hi = static_cast<re2::Rune>(it->lo + rng.uniform(256));
// Build new ranges
re2::RuneRange* ranges = new re2::RuneRange[nranges];
int j = 0;
for (auto it2 = cc->begin(); it2 != cc->end(); ++it2, ++j) {
ranges[j].lo = it2->lo;
ranges[j].hi = (j == idx) ? new_hi : it2->hi;
}
re2::Regexp* newnode = make_literal('x', flags); // Fallback: replace with a literal
delete[] ranges;
return laaf_replace(nullptr, target, newnode);
}
// mode 1-4: replace charclass with a Perl shorthand pattern
// This exercises the \d \w \s \D \W \S parsing paths
// Strategy: dump as text, then create a new seed with a Perl shorthand in it
if (rng.one_in(3)) {
const char* shorthands[] = {"\\d", "\\w", "\\s", ".", "\\D", "\\W", "\\S", "\\N"};
const char* sc = shorthands[rng.uniform(8)];
std::string scs(sc);
re2::RegexpStatus status;
re2::Regexp* newnode = re2::Regexp::Parse(scs, flags, &status);
if (newnode) {
return laaf_replace(nullptr, target, newnode);
}
}
// Fallback: replace charclass with boundary literal
re2::Rune r = random_cc_boundary(rng);
re2::Regexp* newnode = make_literal(r, flags);
return laaf_replace(nullptr, target, newnode);
}
// ============================================================
// M7: Wrap / Unwrap
// ============================================================
bool WrapUnwrapMutator::can_mutate(const Slot& slot) const {
(void)slot;
return true; // any slot
}
bool WrapUnwrapMutator::mutate(re2::Regexp* /*root*/, const std::vector<Slot>& slots, RNG& rng) {
if (slots.empty()) return false;
const auto& target = slots[rng.uniform(slots.size())];
re2::Regexp* node = *target.ptr;
re2::Regexp::ParseFlags flags = node->parse_flags();
int mode = rng.uniform(6);
if (mode == 0 && node->op() == re2::kRegexpCapture && node->nsub() == 1) {
// Unwrap: remove Capture
re2::Regexp* child = node->sub()[0];
child->Incref();
return laaf_replace(nullptr, target, child);
}
if (mode == 1 && is_quantifier(node->op()) && node->nsub() == 1) {
// Unwrap: remove quantifier, expose child
re2::Regexp* child = node->sub()[0];
child->Incref();
return laaf_replace(nullptr, target, child);
}
if (mode == 2) {
// Wrap with Star
node->Incref();
re2::Regexp* newnode = re2::Regexp::Star(node, flags);
return laaf_replace(nullptr, target, newnode);
}
if (mode == 3) {
// Wrap with Quest
node->Incref();
re2::Regexp* newnode = re2::Regexp::Quest(node, flags);
return laaf_replace(nullptr, target, newnode);
}
if (mode == 4) {
// Wrap with Capture
node->Incref();
re2::Regexp* newnode = re2::Regexp::Capture(node, flags, 1);
return laaf_replace(nullptr, target, newnode);
}
// mode == 5: Wrap with Concat (add empty prefix)
re2::Regexp** subs = new re2::Regexp*[2];
subs[0] = re2::Regexp::NewLiteral(L'a', flags); // simple anchor
node->Incref();
subs[1] = node;
re2::Regexp* newnode = re2::Regexp::Concat(subs, 2, flags);
delete[] subs;
return laaf_replace(nullptr, target, newnode);
}
// ============================================================
// M8: Syntax Injection Mutator
// ============================================================
bool SyntaxInjectMutator::can_mutate(const Slot& slot) const {
(void)slot;
return true; // can inject syntax around any slot
}
bool SyntaxInjectMutator::mutate(re2::Regexp* /*root*/, const std::vector<Slot>& slots, RNG& rng) {
if (slots.empty()) return false;
int op = rng.uniform(8);
re2::Regexp::ParseFlags flags = re2::Regexp::LikePerl;
if (op == 0) {
// Inject ^ prefix: ToString → "^" + text → Parse → replace
const auto& target = slots[rng.uniform(slots.size())];
re2::Regexp* node = *target.ptr;
std::string inner = node->ToString();
std::string full = "^" + inner;
re2::RegexpStatus st;
re2::Regexp* nn = re2::Regexp::Parse(full, flags, &st);
if (nn) return laaf_replace(nullptr, target, nn);
return false;
}
if (op == 1) {
// Toggle FoldCase: ToString → add/remove (?i) prefix → Parse → replace
for (const auto& s : slots) {
if (*s.ptr && (*s.ptr)->nsub() > 0) {
re2::Regexp* node = *s.ptr;
std::string inner = node->ToString();
bool has_fold = (node->parse_flags() & re2::Regexp::FoldCase) != 0;
std::string full = has_fold ? "(?-i:" + inner + ")" : "(?i:" + inner + ")";
re2::RegexpStatus st;
re2::Regexp* nn = re2::Regexp::Parse(full, flags, &st);
if (nn) return laaf_replace(nullptr, s, nn);
}
}
return false;
}
if (op == 2) {
// Replace a literal with a Perl shorthand pattern (\d, \w, \s, etc.)
for (const auto& s : slots) {
if (s.kind == re2::kRegexpLiteral || s.kind == re2::kRegexpLiteralString) {
const char* sh[] = {"\\d", "\\w", "\\s", ".", "\\D", "\\W", "\\S"};
const char* sc = sh[rng.uniform(7)];
re2::RegexpStatus st;
re2::Regexp* nn = re2::Regexp::Parse(std::string(sc), flags, &st);
if (nn) return laaf_replace(nullptr, s, nn);
}
}
return false;
}
if (op == 3) {
// Replace literal with \b (word boundary)
for (const auto& s : slots) {
if (s.kind == re2::kRegexpLiteral) {
re2::RegexpStatus st;
re2::Regexp* nn = re2::Regexp::Parse("\\b", flags, &st);
if (nn) return laaf_replace(nullptr, s, nn);
}
}
return false;
}
if (op == 4) {
// Replace literal with Unicode property \p{L}
for (const auto& s : slots) {
if (s.kind == re2::kRegexpLiteral) {
re2::RegexpStatus st;
re2::Regexp* nn = re2::Regexp::Parse("\\p{L}", flags, &st);
if (nn) return laaf_replace(nullptr, s, nn);
}
}
return false;
}
if (op == 5) {
// Replace literal with POSIX class [[:alpha:]]
for (const auto& s : slots) {
if (s.kind == re2::kRegexpLiteral) {
re2::RegexpStatus st;
re2::Regexp* nn = re2::Regexp::Parse("[[:alpha:]]", flags, &st);
if (nn) return laaf_replace(nullptr, s, nn);
}
}
return false;
}
if (op == 6) {
// Wrap node in Concat with ^ prefix and $ suffix
const auto& target = slots[rng.uniform(slots.size())];
re2::Regexp* node = *target.ptr;
node->Incref();
// Parse "^X$" pattern by replacing the middle
std::string inner = node->ToString();
std::string full = "^" + inner + "$";
re2::RegexpStatus st;
re2::Regexp* nn = re2::Regexp::Parse(full, flags, &st);
if (nn) return laaf_replace(nullptr, target, nn);
node->Decref();
return false;
}
// op == 7: Negate charclass (safe Parse-based approach)
for (const auto& s : slots) {
if (s.kind == re2::kRegexpCharClass) {
re2::Regexp* node = *s.ptr;
std::string orig = node->ToString();
// If it starts with [^, remove the ^ ; if it starts with [, add ^
std::string modified;
if (orig.size() > 2 && orig[1] == '^') {
modified = "[" + orig.substr(2); // remove ^
} else if (orig.size() > 1 && orig[0] == '[') {
modified = "[^" + orig.substr(1); // add ^
} else {
modified = orig; // fallback, shouldn't happen
}
re2::RegexpStatus st;
re2::Regexp* nn = re2::Regexp::Parse(modified, flags, &st);
if (nn) return laaf_replace(nullptr, s, nn);
}
}
return false;
}