From: Alex Xu (Hello71) Date: Mon, 15 Sep 2025 23:39:08 +0000 (-0400) Subject: Fix test_parseconf on MaxHSDirCacheBytes default X-Git-Tag: tor-0.4.8.18~3^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=599a9374a3de6534594910c38228e954d5221d57;p=thirdparty%2Ftor.git Fix test_parseconf on MaxHSDirCacheBytes default Change the logic to evaluate the default when fetching the value, matching other default options like ExtORPortCookieAuthFile. --- diff --git a/src/app/config/config.c b/src/app/config/config.c index 6fb892748f..a1520820a9 100644 --- a/src/app/config/config.c +++ b/src/app/config/config.c @@ -3536,13 +3536,6 @@ options_validate_cb(const void *old_options_, void *options_, char **msg) server_mode(options)); options->MaxMemInQueues_low_threshold = (options->MaxMemInQueues / 4) * 3; - /* Process MaxHSDirCacheBytes. If not set (0), use MaxMemInQueues / 5 as - * default. */ - if (options->MaxHSDirCacheBytes == 0) { - /* Default to MaxMemInQueues / 5 for HS directory cache (20%) */ - options->MaxHSDirCacheBytes = options->MaxMemInQueues / 5; - } - if (!options->SafeLogging || !strcasecmp(options->SafeLogging, "0")) { options->SafeLogging_ = SAFELOG_SCRUB_NONE; diff --git a/src/core/or/relay.c b/src/core/or/relay.c index 6fa19e0fa0..c3a6ff0985 100644 --- a/src/core/or/relay.c +++ b/src/core/or/relay.c @@ -2934,9 +2934,9 @@ cell_queues_check_size(void) /* If we're spending over the configured limit on hidden service * descriptors, free them until we're down to 50% of the limit. */ - if (hs_cache_total > get_options()->MaxHSDirCacheBytes) { + if (hs_cache_total > hs_cache_get_max_bytes()) { const size_t bytes_to_remove = - hs_cache_total - (size_t)(get_options()->MaxHSDirCacheBytes / 2); + hs_cache_total - (size_t)(hs_cache_get_max_bytes() / 2); removed = hs_cache_handle_oom(bytes_to_remove); oom_stats_n_bytes_removed_hsdir += removed; alloc -= removed; @@ -2944,7 +2944,7 @@ cell_queues_check_size(void) log_fn_ratelim(&hs_cache_oom_ratelim, LOG_NOTICE, LD_REND, "HSDir cache exceeded limit (%zu > %"PRIu64" bytes). " "Pruned %zu bytes during cell_queues_check_size.", - hs_cache_total, get_options()->MaxHSDirCacheBytes, removed); + hs_cache_total, hs_cache_get_max_bytes(), removed); } if (geoip_client_cache_total > get_options()->MaxMemInQueues / 5) { const size_t bytes_to_remove = diff --git a/src/feature/hs/hs_cache.c b/src/feature/hs/hs_cache.c index bd8f888bb0..a4dec7b54b 100644 --- a/src/feature/hs/hs_cache.c +++ b/src/feature/hs/hs_cache.c @@ -155,7 +155,7 @@ cache_store_v3_as_dir(hs_cache_dir_descriptor_t *desc) /* Check if we've exceeded the MaxHSDirCacheBytes limit after adding * this descriptor. If so, prune excess bytes leaving room for more. */ - const size_t max_cache_bytes = get_options()->MaxHSDirCacheBytes; + const size_t max_cache_bytes = hs_cache_get_max_bytes(); const size_t current_cache_bytes = hs_cache_get_total_allocation(); if (max_cache_bytes > 0 && current_cache_bytes > max_cache_bytes) { /* We prune only 1000 descriptors worth of memory here because @@ -1254,6 +1254,14 @@ hs_cache_free_all(void) hs_cache_total_allocation = 0; } +/** Get the configured maximum cache size. */ +uint64_t +hs_cache_get_max_bytes(void) +{ + uint64_t opt = get_options()->MaxHSDirCacheBytes; + return opt != 0 ? opt : get_options()->MaxMemInQueues / 5; +} + /* Return total size of the cache. */ size_t hs_cache_get_total_allocation(void) diff --git a/src/feature/hs/hs_cache.h b/src/feature/hs/hs_cache.h index 2f78f8479a..9cf73c1ba7 100644 --- a/src/feature/hs/hs_cache.h +++ b/src/feature/hs/hs_cache.h @@ -122,6 +122,7 @@ void hs_cache_client_intro_state_purge(void); bool hs_cache_client_new_auth_parse(const ed25519_public_key_t *service_pk); +uint64_t hs_cache_get_max_bytes(void); size_t hs_cache_get_total_allocation(void); void hs_cache_decrement_allocation(size_t n); void hs_cache_increment_allocation(size_t n);