|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""No undo list leaks out of readline's history walk or its aborted search. |
| 3 | +
|
| 4 | +Two places where readline 8.1 (Ubuntu 22.04's) drops an undo list it owns, |
| 5 | +each once per line, reported by LeakSanitizer when the shell exits: |
| 6 | +
|
| 7 | + saved line type, then walk the history with previous-history (Ctrl-P, or |
| 8 | + Up with prefix search off) and end the line on the recalled |
| 9 | + entry: Enter, ^C or ^G. The next read freed readline's saved |
| 10 | + copy of the typed line, but not its undo list. |
| 11 | + Fixed by hist_rl_reclaim_saved() (history_rl.c). |
| 12 | + search Alt-P / Alt-N (stock non-incremental search), type a string, |
| 13 | + abort with ^G or Rubout on an empty string: readline restored |
| 14 | + the line over the search string's undo list. |
| 15 | + Fixed by rl_nsearch_reclaim() (rl_nsearch.c). |
| 16 | +
|
| 17 | +Each case is a fresh shell (in-process reader: the forked one discards its |
| 18 | +heap with the child) run with detect_leaks=1; it must exit without a |
| 19 | +report. The `works/` cases check the keys still do their job. On a binary |
| 20 | +built without ASan nothing can be measured: SKIP, not pass. |
| 21 | +""" |
| 22 | +import os |
| 23 | +import sys |
| 24 | +import time |
| 25 | + |
| 26 | +sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), |
| 27 | + "helpers")) |
| 28 | +from ptyexpect import Tty # noqa: E402 |
| 29 | + |
| 30 | +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 31 | +SHELL = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else |
| 32 | + os.path.join(ROOT, "build", "bin", "hellish")) |
| 33 | +FAILS = [] |
| 34 | +UP, DOWN = "\x1b[A", "\x1b[B" |
| 35 | +ALT_P, ALT_N = "\x1bp", "\x1bn" |
| 36 | +HISTORY = ["echo alpha", "echo beta"] |
| 37 | + |
| 38 | + |
| 39 | +def check(name, ok, detail=""): |
| 40 | + print(("ok " if ok else "FAIL ") + name) |
| 41 | + if not ok: |
| 42 | + FAILS.append(name) |
| 43 | + print(" " + detail) |
| 44 | + |
| 45 | + |
| 46 | +def session(name, steps, want=None): |
| 47 | + """A shell with HISTORY run, then each key group of `steps` sent and |
| 48 | + given time to land, then exit. No leak report allowed; `want`, when |
| 49 | + given, must be printed by the line the keys ran.""" |
| 50 | + t = Tty(SHELL, rc="PS1='P$ '\nHISTFILE=/dev/null\n", |
| 51 | + env={"ASAN_OPTIONS": "detect_leaks=1", |
| 52 | + "HELLISH_HISTORY_PREFIX": "0"}) |
| 53 | + ok = t.expect(b"P$ ") |
| 54 | + for c in HISTORY: |
| 55 | + t.send(c + "\r") |
| 56 | + ok = ok and t.expect(b"P$ ") |
| 57 | + mark = len(t.out) |
| 58 | + for keys in steps: |
| 59 | + t.send(keys) |
| 60 | + t._read(0.25) |
| 61 | + if want is not None: |
| 62 | + ok = ok and t.expect(b"\n" + want.encode() + b"\r\n") |
| 63 | + t.send("\x03") |
| 64 | + ok = ok and t.expect(b"P$ ") |
| 65 | + t.send("exit\r") |
| 66 | + # LeakSanitizer reports as the shell exits: read until the pty closes, |
| 67 | + # so the report is in t.out before anything is decided. |
| 68 | + end = time.time() + 10 |
| 69 | + while t._read(0.2) and time.time() < end: |
| 70 | + pass |
| 71 | + st = t.close() |
| 72 | + leak = b"LeakSanitizer" in t.out |
| 73 | + check(name, ok and not leak and os.WIFEXITED(st), |
| 74 | + repr(t.since(mark)[-400:])) |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + with open(SHELL, "rb") as f: |
| 79 | + if b"AddressSanitizer" not in f.read(): |
| 80 | + print("SKIP %s has no ASan: leaks cannot be seen" % SHELL) |
| 81 | + return 0 |
| 82 | + session("saved/up-then-enter", ["echo", UP, "\r"]) |
| 83 | + session("saved/up-then-ctrl-c", ["echo", UP, "\x03"]) |
| 84 | + session("saved/up-then-ctrl-g", ["ec", UP, "\x07"]) |
| 85 | + session("saved/ctrl-p-then-enter", ["echo", "\x10", "\r"]) |
| 86 | + session("saved/ctrl-p-walk-back-and-forth", |
| 87 | + ["echo", "\x10", "\x10", "\x0e", "\x10", "\r"]) |
| 88 | + session("search/alt-p-abort-ctrl-g", [ALT_P, "ech", "\x07"]) |
| 89 | + session("search/alt-n-abort-ctrl-g", [ALT_N, "ech", "\x07"]) |
| 90 | + session("search/alt-p-rubout-past-empty", [ALT_P, "e", "\x7f", "\x7f"]) |
| 91 | + session("search/typed-line-then-abort", ["ls", ALT_P, "ech", "\x07"]) |
| 92 | + session("search/typed-then-alt-p-enter", ["ech", ALT_P, "\r"]) |
| 93 | + session("works/up-down-brings-the-typed-line-back", |
| 94 | + ["echo typed", UP, DOWN, "\r"], want="typed") |
| 95 | + session("works/alt-p-search-runs-the-match", |
| 96 | + [ALT_P, "alph", "\r", "\r"], want="alpha") |
| 97 | + session("works/aborted-search-restores-the-line", |
| 98 | + ["echo kept", ALT_P, "x", "\x07", "\r"], want="kept") |
| 99 | + print("%d failed" % len(FAILS)) |
| 100 | + return 1 if FAILS else 0 |
| 101 | + |
| 102 | + |
| 103 | +sys.exit(main()) |
0 commit comments