Skip to content

Commit

Permalink
Disable strict-aliasing in clang by default
Browse files Browse the repository at this point in the history
Aliasing is essentially when you access the same memory via different
types.

If the compiler knows this doesn't happen it can make some
optimisations.

There is however code in Unit, for example in the wasm language module
and the websocket code that may fall foul of strict-aliasing rules.

(For the wasm module I explicitly disable it there)

In auto/cc/test for GCC we have

  NXT_CFLAGS="$NXT_CFLAGS -O"
  ...
  # -O2 enables -fstrict-aliasing and -fstrict-overflow.
  #NXT_CFLAGS="$NXT_CFLAGS -O2"
  #NXT_CFLAGS="$NXT_CFLAGS -Wno-strict-aliasing"

So with GCC by default we effectively compile with -fno-strict-aliasing.
It seems to also be the case that the intention is to keep it disabled
if we compile at an optimisation level that would enable it.

For clang we have this

  NXT_CFLAGS="$NXT_CFLAGS -O"
  ...
  #NXT_CFLAGS="$NXT_CFLAGS -O2"
  ...
  NXT_CFLAGS="$NXT_CFLAGS -fstrict-aliasing"

(In _clang_, -fstrict-aliasing is always enabled by default)

So in clang we always build with -fstrict-aliasing. I don't think this
is the best idea, building with something as fundamental as this
disabled in one compiler and enabled in another.

This patch adjusts the Clang side of things to match that of GCC. I.e
compile with -fno-strict-aliasing. It also explicitly sets
-fno-strict-aliasing for GCC, which is what we were getting anyway but
lets be explicit about it.

Cc: Dan Callahan <d.callahan@f5.com>
Cc: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
  • Loading branch information
ac000 committed Mar 3, 2024
1 parent 535107e commit 1728813
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions auto/cc/test
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ case $NXT_CC_NAME in

# -O2 enables -fstrict-aliasing and -fstrict-overflow.
#NXT_CFLAGS="$NXT_CFLAGS -O2"
#NXT_CFLAGS="$NXT_CFLAGS -Wno-strict-aliasing"
NXT_CFLAGS="$NXT_CFLAGS -Wno-strict-aliasing"

#NXT_CFLAGS="$NXT_CFLAGS -fomit-frame-pointer"
#NXT_CFLAGS="$NXT_CFLAGS -momit-leaf-frame-pointer"
Expand Down Expand Up @@ -116,8 +116,10 @@ case $NXT_CC_NAME in
#NXT_CFLAGS="$NXT_CFLAGS -Wshorten-64-to-32"
NXT_CFLAGS="$NXT_CFLAGS -Wwrite-strings"
#NXT_CFLAGS="$NXT_CFLAGS -O2"
# strict-aliasing is always enabled by default in clang
NXT_CFLAGS="$NXT_CFLAGS -fno-strict-aliasing"

#NXT_CFLAGS="$NXT_CFLAGS -fomit-frame-pointer"
NXT_CFLAGS="$NXT_CFLAGS -fstrict-aliasing"
NXT_CFLAGS="$NXT_CFLAGS -Wstrict-overflow=5"

NXT_CFLAGS="$NXT_CFLAGS -Wmissing-prototypes"
Expand Down

0 comments on commit 1728813

Please sign in to comment.