From c77d364905bc02b02c3dd70fb397ceba2084c7d4 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 12 Dec 2018 06:19:42 +0100 Subject: [PATCH] MINOR: config: round up global.tune.bufsize to the next multiple of 2 void* Since HTX casts the buffer to a struct and stores relative pointers at the end, it is mandatory that its end is properly aligned. This patch enforces a buffer size rounding up to the next multiple of two void*, thus 8 on 32-bit and 16 on 64-bit, to match what malloc() already does on the beginning of the buffer. In pratice it will never be really noticeable since default sizes already are such multiples. --- doc/configuration.txt | 4 +++- src/cfgparse-global.c | 2 ++ src/haproxy.c | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/doc/configuration.txt b/doc/configuration.txt index 400ec6402..d13222331 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -1532,7 +1532,9 @@ tune.bufsize addition, use of HTTP/2 mandates that this value must be 16384 or more. If an HTTP request is larger than (tune.bufsize - tune.maxrewrite), haproxy will return HTTP 400 (Bad Request) error. Similarly if an HTTP response is larger - than this size, haproxy will return HTTP 502 (Bad Gateway). + than this size, haproxy will return HTTP 502 (Bad Gateway). Note that the + value set using this parameter will automatically be rounded up to the next + multiple of 8 on 32-bit machines and 16 on 64-bit machines. tune.chksize Sets the check buffer size to this size (in bytes). Higher values may help diff --git a/src/cfgparse-global.c b/src/cfgparse-global.c index 4303ef987..5a92d9b31 100644 --- a/src/cfgparse-global.c +++ b/src/cfgparse-global.c @@ -206,6 +206,8 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm) goto out; } global.tune.bufsize = atol(args[1]); + /* round it up to support a two-pointer alignment at the end */ + global.tune.bufsize = (global.tune.bufsize + 2 * sizeof(void *) - 1) & -(2 * sizeof(void *)); if (global.tune.bufsize <= 0) { ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n", file, linenum, args[0]); err_code |= ERR_ALERT | ERR_FATAL; diff --git a/src/haproxy.c b/src/haproxy.c index 6c96794e2..4af888073 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -149,9 +149,9 @@ struct global global = { } }, .tune = { - .bufsize = BUFSIZE, + .bufsize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)), .maxrewrite = -1, - .chksize = BUFSIZE, + .chksize = (BUFSIZE + 2*sizeof(void *) - 1) & -(2*sizeof(void *)), .reserved_bufs = RESERVED_BUFS, .pattern_cache = DEFAULT_PAT_LRU_SIZE, #ifdef USE_OPENSSL