Seal the heap: bounded library heaps and zero-allocation program tests

The programs point libuv, c-ares and Mbed TLS (MBEDTLS_PLATFORM_MEMORY in
the bundled build) at their own FixedHeap before anything runs, mark the
end of startup with heap_seal(), and in verbose mode report heap use and
any allocation after it at exit. The library build keeps the host's
allocator.

measure_alloc.py gains --udp, and the alloc_steady_programs_* tests now
fail on any malloc per relayed TCP connection (by name and by address)
or UDP association, for every cipher family.
This commit is contained in:
Max Lv
2026-10-15 00:34:30 +08:00
parent e0047c9cef
commit c69fb4c4d9
12 changed files with 304 additions and 12 deletions
+27 -5
View File
@@ -343,11 +343,33 @@ if(SS_BUILD_EXECUTABLES AND NOT CMAKE_CROSSCOMPILING)
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/alloc_audit/check_steady.py
--shim $<TARGET_FILE:ss_alloc_audit> -- $<TARGET_FILE:test_cache_cpp>)
set_tests_properties(alloc_steady_cache PROPERTIES LABELS "unit" TIMEOUT 60)
add_test(NAME alloc_audit_report
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/alloc_audit/measure_alloc.py
--bin ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
--shim $<TARGET_FILE:ss_alloc_audit>)
set_tests_properties(alloc_audit_report PROPERTIES LABELS "integration" TIMEOUT 120)
# The programs: no heap allocation per relayed connection once
# started, by name (DNS through the bounded c-ares heap) and by
# address, for each cipher family.
set(SS_ALLOC_CASES "aes-256-gcm@localhost" "2022-blake3-aes-256-gcm@127.0.0.1"
"chacha20-ietf-poly1305@127.0.0.1")
if(SS_ENABLE_LEGACY)
list(APPEND SS_ALLOC_CASES "aes-256-cfb@127.0.0.1")
endif()
foreach(case ${SS_ALLOC_CASES})
string(REPLACE "@" ";" parts "${case}")
list(GET parts 0 method)
list(GET parts 1 target)
add_test(NAME alloc_steady_programs_${method}
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/alloc_audit/measure_alloc.py
--bin ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
--shim $<TARGET_FILE:ss_alloc_audit>
--method ${method} --target ${target} --max-per-connection 0)
set_tests_properties(alloc_steady_programs_${method} PROPERTIES LABELS "integration" TIMEOUT 120)
endforeach()
foreach(method aes-256-gcm 2022-blake3-aes-256-gcm)
add_test(NAME alloc_steady_programs_udp_${method}
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/alloc_audit/measure_alloc.py
--bin ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
--shim $<TARGET_FILE:ss_alloc_audit>
--method ${method} --udp --warmup 20 --connections 50 --max-per-connection 0)
set_tests_properties(alloc_steady_programs_udp_${method} PROPERTIES LABELS "integration" TIMEOUT 120)
endforeach()
endif()
add_test(NAME test_printable_salt_interop
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/interop.py
+17 -7
View File
@@ -27,7 +27,8 @@ import threading
import time
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from interop import TCPHandler, TCPOrigin, free_port, peers, receive, socks # noqa: E402
from interop import (TCPHandler, TCPOrigin, UDPHandler, UDPOrigin, free_port, peers, receive, socks, # noqa: E402
udp_case)
def read_snapshot(path, previous_lines, timeout=10):
@@ -59,7 +60,12 @@ def tcp_case(local_port, target, origin_port, size):
upload.result(timeout=10)
def relay(local_port, target, origin_port, connections, concurrency, size):
def relay(local_port, target, origin_port, connections, concurrency, size, udp=False):
if udp:
# Each case is a UDP association with four datagrams.
for _ in range(connections):
udp_case(local_port, origin_port)
return
with concurrent.futures.ThreadPoolExecutor(max_workers=concurrency) as pool:
futures = [pool.submit(tcp_case, local_port, target, origin_port, size) for _ in range(connections)]
for future in futures:
@@ -76,6 +82,7 @@ def main():
parser.add_argument("--concurrency", type=int, default=10)
parser.add_argument("--size", type=int, default=16384, help="bytes echoed per connection")
parser.add_argument("--max-per-connection", type=float, help="fail if any peer exceeds this")
parser.add_argument("--udp", action="store_true", help="relay UDP associations instead of TCP streams")
parser.add_argument("--target", default="localhost",
help="destination host sent to the server; a name makes the server resolve it")
parser.add_argument("--json", action="store_true", help="print the result as JSON")
@@ -87,17 +94,19 @@ def main():
key_size = 16 if args.method == "2022-blake3-aes-128-gcm" else 32
password = base64.b64encode(os.urandom(key_size)).decode("ascii")
with TCPOrigin(("127.0.0.1", 0), TCPHandler) as origin, \
origin_class, handler = (UDPOrigin, UDPHandler) if args.udp else (TCPOrigin, TCPHandler)
with origin_class(("127.0.0.1", 0), handler) as origin, \
tempfile.TemporaryDirectory(prefix="ss-alloc-") as temp:
threading.Thread(target=origin.serve_forever, daemon=True).start()
server_port, local_port = free_port(), free_port()
names = ["ss-server", "ss-local"]
outputs = [Path(temp) / f"{name}.counts" for name in names]
udp = ["-u"] if args.udp else []
commands = [
[str(binary_dir / "ss-server"), "-s", "127.0.0.1", "-p", str(server_port),
"-k", password, "-m", args.method],
"-k", password, "-m", args.method] + udp,
[str(binary_dir / "ss-local"), "-s", "127.0.0.1", "-p", str(server_port),
"-l", str(local_port), "-k", password, "-m", args.method],
"-l", str(local_port), "-k", password, "-m", args.method] + udp,
]
environments = []
for output in outputs:
@@ -112,10 +121,11 @@ def main():
peers([commands[1]], [local_port], environments[1]) as local:
processes = server + local
counts = [0, 0]
relay(local_port, args.target, origin.server_address[1], args.warmup, args.concurrency, args.size)
relay(local_port, args.target, origin.server_address[1], args.warmup, args.concurrency, args.size, args.udp)
time.sleep(.5)
before = snapshot_all(processes, outputs, counts)
relay(local_port, args.target, origin.server_address[1], args.connections, args.concurrency, args.size)
relay(local_port, args.target, origin.server_address[1], args.connections, args.concurrency, args.size,
args.udp)
time.sleep(.5)
after = snapshot_all(processes, outputs, counts)