Welcome! Log In Create A New Profile

Advanced

[PATCH] Fix haproxy hangs on FreeBSD >= 11

Posted by Olivier Houchard 
Olivier Houchard
[PATCH] Fix haproxy hangs on FreeBSD >= 11
April 19, 2017 12:00PM
Hi guys,

Thanks to your help, we finally figure out what was happening on FreeBSD,
and the attached patch should fix it.
Problem was, haproxy relies on what is really undefined behavior in C, with
signed integer overflows. gcc and earlier versions of clang behaved as we
expected, but newer versions of clang do not, which is why it started to be
a problem with FreeBSD 11 (and is probably on other BSDs using clang, and
MacOS X).

Thanks again for giving us enough informations to really figure that out, it
would have been _tough_ overwise :)

Regards,

Olivier
From 163be439a8bc6e5aa1cf3fea0f086d518ddad0a9 Mon Sep 17 00:00:00 2001
From: Olivier Houchard <[email protected]>
Date: Wed, 19 Apr 2017 11:34:10 +0200
Subject: [PATCH] BUG/MAJOR: Use -fwrapv.

Haproxy relies on signed integer wraparound on overflow, however this is
really an undefined behavior, so the C compiler is allowed to do whatever
it wants, and clang does exactly that, and that causes problems when the
timer goes from <= INT_MAX to > INT_MAX, and explains the various hangs
reported on FreeBSD every 49.7 days. To make sure we get the intended
behavior, use -fwrapv for now. A proper fix is to switch everything to
unsigned, and it will happen later, but this is simpler, and more likely to
be backported to the stable branches.
Many thanks to David King, Mark S, Dave Cottlehuber, Slawa Olhovchenkov,
Piotr Pawel Stefaniak, and any other I may have forgotten for reporting that
and investigating.
---
Makefile | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index a87e1e2..63db432 100644
--- a/Makefile
+++ b/Makefile
@@ -131,7 +131,10 @@ DEBUG_CFLAGS = -g
#### Compiler-specific flags that may be used to disable some negative over-
# optimization or to silence some warnings. -fno-strict-aliasing is needed with
# gcc >= 4.4.
-SPEC_CFLAGS = -fno-strict-aliasing -Wdeclaration-after-statement
+# We rely on signed integer wraparound on overflow, however clang think it
+# can do whatever it wants since it's an undefined behavior, so use -fwrapv
+# to be sure e get the intended behavior.
+SPEC_CFLAGS = -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv

#### Memory usage tuning
# If small memory footprint is required, you can reduce the buffer size. There
--
2.9.3
Willy Tarreau
Re: [PATCH] Fix haproxy hangs on FreeBSD >= 11
April 19, 2017 12:30PM
On Wed, Apr 19, 2017 at 11:52:22AM +0200, Olivier Houchard wrote:
> Hi guys,
>
> Thanks to your help, we finally figure out what was happening on FreeBSD,
> and the attached patch should fix it.
> Problem was, haproxy relies on what is really undefined behavior in C, with
> signed integer overflows. gcc and earlier versions of clang behaved as we
> expected, but newer versions of clang do not, which is why it started to be
> a problem with FreeBSD 11 (and is probably on other BSDs using clang, and
> MacOS X).
>
> Thanks again for giving us enough informations to really figure that out, it
> would have been _tough_ overwise :)

Thanks Olivier (and to all those giving enough feedback making it possible
to figure the real issue). I've now taken it and already backported to 1.7.
I'll backport to older branches as well since many people will progressively
get hit as they upgrade their compilers :-/

It's really sad that there is no way to get any single warning from Clang
when it purposely decides to "optimize" this check in a way that can be
disabled with specific options.

Willy
Sorry, only registered users may post in this forum.

Click here to login