|
| 1 | +#![no_main] |
| 2 | + |
| 3 | +use fuzz::{ |
| 4 | + actions::ActionWrapper, test_multi_sites, |
| 5 | + test_multi_sites_on_one_doc_with_peer_seed_and_targets, Action, FuzzTarget, |
| 6 | +}; |
| 7 | +use libfuzzer_sys::fuzz_target; |
| 8 | +use loro::ContainerType; |
| 9 | + |
| 10 | +fn lca_biased_actions(actions: Vec<Action>) -> Vec<Action> { |
| 11 | + let mut biased = Vec::with_capacity(actions.len().saturating_mul(2).min(128)); |
| 12 | + for (i, action) in actions.into_iter().take(48).enumerate() { |
| 13 | + biased.push(action); |
| 14 | + |
| 15 | + let site = ((i * 37) % 251) as u8; |
| 16 | + let other = site.wrapping_add(1); |
| 17 | + let version = (i as u32).wrapping_mul(97); |
| 18 | + let injected = match i % 8 { |
| 19 | + 0 => Action::Sync { |
| 20 | + from: site, |
| 21 | + to: other, |
| 22 | + }, |
| 23 | + 1 => Action::DiffApply { |
| 24 | + from: site, |
| 25 | + to: other, |
| 26 | + }, |
| 27 | + 2 => Action::Checkout { site, to: version }, |
| 28 | + 3 => Action::ForkAt { site, to: version }, |
| 29 | + 4 => Action::ImportShallow { site, from: other }, |
| 30 | + 5 => Action::ExportShallow { site }, |
| 31 | + 6 => Action::StateOnlyRoundTrip { site }, |
| 32 | + _ => Action::Commit { site }, |
| 33 | + }; |
| 34 | + biased.push(injected); |
| 35 | + } |
| 36 | + |
| 37 | + biased |
| 38 | +} |
| 39 | + |
| 40 | +fn run_text_diff_calc(actions: Vec<Action>) { |
| 41 | + let mut actions = lca_biased_actions(actions); |
| 42 | + test_multi_sites(5, vec![FuzzTarget::Text], &mut actions); |
| 43 | +} |
| 44 | + |
| 45 | +fn run_one_doc_diff_calc(actions: Vec<Action>) { |
| 46 | + let peer_seed = peer_seed_from_actions(&actions); |
| 47 | + let mut actions = lca_biased_actions(actions); |
| 48 | + test_multi_sites_on_one_doc_with_peer_seed_and_targets( |
| 49 | + 5, |
| 50 | + peer_seed, |
| 51 | + vec![ContainerType::Text], |
| 52 | + &mut actions, |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +fn mix_seed(seed: u64, value: u64) -> u64 { |
| 57 | + seed ^ value |
| 58 | + .wrapping_add(0x9E37_79B9_7F4A_7C15) |
| 59 | + .wrapping_add(seed << 6) |
| 60 | + .wrapping_add(seed >> 2) |
| 61 | +} |
| 62 | + |
| 63 | +fn peer_seed_from_actions(actions: &[Action]) -> u64 { |
| 64 | + let mut seed = mix_seed(0xD1FF_CA1C_7E57_0001, actions.len() as u64); |
| 65 | + for action in actions.iter().take(8) { |
| 66 | + seed = match action { |
| 67 | + Action::Handle { |
| 68 | + site, |
| 69 | + target, |
| 70 | + container, |
| 71 | + action, |
| 72 | + } => { |
| 73 | + let mut seed = mix_seed(seed, 0); |
| 74 | + seed = mix_seed(seed, *site as u64); |
| 75 | + seed = mix_seed(seed, *target as u64); |
| 76 | + seed = mix_seed(seed, *container as u64); |
| 77 | + if let ActionWrapper::Generic(g) = action { |
| 78 | + seed = mix_seed(seed, g.bool as u64); |
| 79 | + seed = mix_seed(seed, g.key as u64); |
| 80 | + seed = mix_seed(seed, g.pos as u64); |
| 81 | + seed = mix_seed(seed, g.length as u64); |
| 82 | + seed = mix_seed(seed, g.prop); |
| 83 | + } |
| 84 | + seed |
| 85 | + } |
| 86 | + Action::Checkout { site, to } => mix_seed(mix_seed(seed, 1), ((*site as u64) << 32) | *to as u64), |
| 87 | + Action::Undo { site, op_len } => { |
| 88 | + mix_seed(mix_seed(seed, 2), ((*site as u64) << 32) | *op_len as u64) |
| 89 | + } |
| 90 | + Action::SyncAllUndo { site, op_len } => { |
| 91 | + mix_seed(mix_seed(seed, 3), ((*site as u64) << 32) | *op_len as u64) |
| 92 | + } |
| 93 | + Action::Sync { from, to } => { |
| 94 | + mix_seed(mix_seed(seed, 4), ((*from as u64) << 8) | *to as u64) |
| 95 | + } |
| 96 | + Action::SyncAll => mix_seed(seed, 5), |
| 97 | + Action::ForkAt { site, to } => { |
| 98 | + mix_seed(mix_seed(seed, 6), ((*site as u64) << 32) | *to as u64) |
| 99 | + } |
| 100 | + Action::DiffApply { from, to } => { |
| 101 | + mix_seed(mix_seed(seed, 7), ((*from as u64) << 8) | *to as u64) |
| 102 | + } |
| 103 | + Action::Query { |
| 104 | + site, |
| 105 | + target, |
| 106 | + query_type, |
| 107 | + } => mix_seed( |
| 108 | + mix_seed(seed, 8), |
| 109 | + ((*site as u64) << 16) | ((*target as u64) << 8) | *query_type as u64, |
| 110 | + ), |
| 111 | + Action::ExportShallow { site } => mix_seed(mix_seed(seed, 9), *site as u64), |
| 112 | + Action::ImportShallow { site, from } => { |
| 113 | + mix_seed(mix_seed(seed, 10), ((*site as u64) << 8) | *from as u64) |
| 114 | + } |
| 115 | + Action::StateOnlyRoundTrip { site } => mix_seed(mix_seed(seed, 11), *site as u64), |
| 116 | + Action::Commit { site } => mix_seed(mix_seed(seed, 12), *site as u64), |
| 117 | + Action::SetCommitOptions { site, origin, msg } => mix_seed( |
| 118 | + mix_seed(seed, 13), |
| 119 | + ((*site as u64) << 16) | ((*origin as u64) << 8) | *msg as u64, |
| 120 | + ), |
| 121 | + }; |
| 122 | + } |
| 123 | + seed |
| 124 | +} |
| 125 | + |
| 126 | +fuzz_target!(|actions: Vec<Action>| { |
| 127 | + if actions.is_empty() { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + let use_one_doc = match &actions[0] { |
| 132 | + Action::Handle { site, .. } |
| 133 | + | Action::Checkout { site, .. } |
| 134 | + | Action::Undo { site, .. } |
| 135 | + | Action::SyncAllUndo { site, .. } |
| 136 | + | Action::ForkAt { site, .. } |
| 137 | + | Action::Query { site, .. } |
| 138 | + | Action::ExportShallow { site } |
| 139 | + | Action::ImportShallow { site, .. } |
| 140 | + | Action::StateOnlyRoundTrip { site } |
| 141 | + | Action::Commit { site } |
| 142 | + | Action::SetCommitOptions { site, .. } => site % 2 == 1, |
| 143 | + Action::Sync { from, .. } | Action::DiffApply { from, .. } => from % 2 == 1, |
| 144 | + Action::SyncAll => false, |
| 145 | + }; |
| 146 | + |
| 147 | + if use_one_doc { |
| 148 | + run_one_doc_diff_calc(actions); |
| 149 | + } else { |
| 150 | + run_text_diff_calc(actions); |
| 151 | + } |
| 152 | +}); |
0 commit comments