| 1 |
|
| 2 |
"""Regression test for issue #217: hash_search() must not blow up to O(N^2). |
| 3 |
|
| 4 |
A disk/VM image can contain a huge number of blocks that share the same weak |
| 5 |
rolling checksum (e.g. long runs of identical bytes). All such blocks land on |
| 6 |
a single hash-table chain. When the sender then rolls across a region whose |
| 7 |
weak checksum keeps landing on that chain but never produces a strong-checksum |
| 8 |
match, the inner loop of hash_search() used to walk the *entire* chain for |
| 9 |
every single byte offset. The transfer would peg one CPU at 100% for hours |
| 10 |
with no visible progress -- "rsync hangs at 100% CPU". |
| 11 |
|
| 12 |
We reproduce a small, deterministic version of that pathology: |
| 13 |
|
| 14 |
* Destination/basis file: many identical "decoy" blocks. Each decoy is an |
| 15 |
all-C block whose first three bytes are perturbed by (+1,-2,+1). That |
| 16 |
perturbation leaves rsync's weak checksum (get_checksum1) unchanged but |
| 17 |
changes the strong checksum, so every decoy shares one weak checksum yet |
| 18 |
never strong-matches the source -> one very long chain, all false alarms. |
| 19 |
|
| 20 |
* Source file: a short run of the constant byte C. Every window has that |
| 21 |
same weak checksum, so without a bound the sender walks the whole chain at |
| 22 |
every offset. |
| 23 |
|
| 24 |
Rather than measure wall-clock time (which is hopelessly machine dependent), |
| 25 |
we count the work directly. rsync's existing `false_alarms` counter -- shown |
| 26 |
by `--debug=deltasum1` -- is incremented once per strong-checksum comparison |
| 27 |
that fails, i.e. exactly once per chain entry examined and rejected. With the |
| 28 |
per-offset cap in match.c the sender examines at most MAX_CHAIN_LEN entries per |
| 29 |
hash hit, so false_alarms / hash_hits stays bounded no matter how long the |
| 30 |
chain is; without the cap that ratio equals the full chain length. We assert |
| 31 |
the bound, which is the same integer on every machine. |
| 32 |
|
| 33 |
The fix is sender-only (it changes no checksum, byte, or protocol field), so |
| 34 |
the transferred result must also still be byte-for-byte correct. |
| 35 |
""" |
| 36 |
|
| 37 |
import re |
| 38 |
|
| 39 |
from rsyncfns import ( |
| 40 |
FROMDIR, TODIR, assert_same, rmtree, run_rsync, test_fail, |
| 41 |
) |
| 42 |
|
| 43 |
BLOCK = 256 |
| 44 |
NBLOCKS = 8000 |
| 45 |
RUN = 3000 |
| 46 |
C = 100 |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
MAX_FALSE_ALARMS_PER_HIT = NBLOCKS // 4 |
| 53 |
|
| 54 |
|
| 55 |
def make_decoy_block() -> bytes: |
| 56 |
b = bytearray([C]) * BLOCK |
| 57 |
|
| 58 |
|
| 59 |
b[0] = C + 1 |
| 60 |
b[1] = C - 2 |
| 61 |
b[2] = C + 1 |
| 62 |
return bytes(b) |
| 63 |
|
| 64 |
|
| 65 |
rmtree(FROMDIR) |
| 66 |
rmtree(TODIR) |
| 67 |
FROMDIR.mkdir(parents=True, exist_ok=True) |
| 68 |
TODIR.mkdir(parents=True, exist_ok=True) |
| 69 |
|
| 70 |
src = FROMDIR / 'image.bin' |
| 71 |
dst = TODIR / 'image.bin' |
| 72 |
|
| 73 |
|
| 74 |
decoy = make_decoy_block() |
| 75 |
with open(dst, 'wb') as f: |
| 76 |
for _ in range(NBLOCKS): |
| 77 |
f.write(decoy) |
| 78 |
|
| 79 |
|
| 80 |
with open(src, 'wb') as f: |
| 81 |
f.write(bytes([C]) * RUN) |
| 82 |
|
| 83 |
proc = run_rsync('-a', '--no-whole-file', f'--block-size={BLOCK}', |
| 84 |
'--no-compress', '--debug=deltasum1', |
| 85 |
f'{src}', f'{dst}', capture_output=True) |
| 86 |
|
| 87 |
out = proc.stdout + proc.stderr |
| 88 |
m = re.search(r'hash_hits=(\d+)\s+false_alarms=(\d+)', out) |
| 89 |
if not m: |
| 90 |
test_fail(f"could not find deltasum stats in rsync output:\n{out}") |
| 91 |
hash_hits = int(m.group(1)) |
| 92 |
false_alarms = int(m.group(2)) |
| 93 |
|
| 94 |
if hash_hits == 0: |
| 95 |
test_fail("expected the source to hit the decoy chain but hash_hits=0") |
| 96 |
|
| 97 |
ratio = false_alarms / hash_hits |
| 98 |
if ratio > MAX_FALSE_ALARMS_PER_HIT: |
| 99 |
test_fail( |
| 100 |
f"hash_search() walked ~{ratio:.0f} chain entries per hash hit " |
| 101 |
f"(false_alarms={false_alarms}, hash_hits={hash_hits}); the chain of " |
| 102 |
f"{NBLOCKS} entries is not being capped -- issue #217 regression") |
| 103 |
|
| 104 |
|
| 105 |
assert_same(dst, src, label='issue #217 chain-cap transfer') |
| 106 |
|
| 107 |
print(f"issue #217: bounded at {ratio:.0f} false alarms/hit " |
| 108 |
f"(chain={NBLOCKS}, cap keeps it under {MAX_FALSE_ALARMS_PER_HIT})") |