Fix atomic builtins detection

Jeremy Allison jra at samba.org
Mon Apr 25 18:15:42 UTC 2016


On Mon, Apr 25, 2016 at 04:24:28PM +0200, Jeremie Courreges-Anglas wrote:
> 
> Hi,
> 
> lib/replace/wscript has this bunch of code:
> 
>     # Check for atomic builtins. */
>     conf.CHECK_CODE('''
>                     int main(void) {
>                         int i;
>                         (void)__sync_fetch_and_add(&i, 1);
>                         return 0;
>                     }
>                     ''',
>                     'HAVE___SYNC_FETCH_AND_ADD',
>                     msg='Checking for __sync_fetch_and_add compiler builtin')
> 
> 
> It is incorrect, since CHECK_CODE already wraps the code fragment in
> main().  Here's what gets compiled:
> 
> 
> [includes]
>  int main(void) { 
>                     int main(void) {
>                         int i;
>                         (void)__sync_fetch_and_add(&i, 1);
>                         return 0;
>                     }
>                     ; return 0; }
> 
> 
> Now the funny part is when you run this where __sync_fetch_and_add is
> actually not available.  The inner main() function isn't called
> anywhere, and at cc -O2 it is simply optimized out, thus the compile
> test wrongly succeeds.  This can be seen for example on OpenBSD/hppa
> (gcc-4.2.1).
> 
> Please find attached a patch to fix this issue for __sync_fetch_and_add
> and atomic_add_32.

Looks good to me. Can I get a second Team reviewer ?

> From cced95b0de2d749aa5d5b85588f2c8f7656fe3d6 Mon Sep 17 00:00:00 2001
> From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Courr=C3=A8ges-Anglas?= <jca at wxcvbn.org>
> Date: Mon, 25 Apr 2016 16:10:03 +0200
> Subject: [PATCH] Fix CHECK_CODE usage in atomics builtin detection
> MIME-Version: 1.0
> Content-Type: text/plain; charset=UTF-8
> Content-Transfer-Encoding: 8bit
> 
> CHECK_CODE already wraps the code with main().  Adding another layer
> results in a nested function, eg
> 
>   int main(void) { int main(void) { __sync_fetch_and_add(); } }
> 
> Since the inner function isn't called it is optimized out at cc -O2,
> thus the linker doesn't fail if __sync_fetch_and_add() isn't available.
> 
> Issue noticed on OpenBSD/hppa.
> 
> Signed-off-by: Jérémie Courrèges-Anglas <jca at wxcvbn.org>
> ---
>  lib/replace/wscript | 16 ++++------------
>  1 file changed, 4 insertions(+), 12 deletions(-)
> 
> diff --git a/lib/replace/wscript b/lib/replace/wscript
> index 37cbbb7..5efd86c 100644
> --- a/lib/replace/wscript
> +++ b/lib/replace/wscript
> @@ -202,23 +202,15 @@ def configure(conf):
>  
>      # Check for atomic builtins. */
>      conf.CHECK_CODE('''
> -                    int main(void) {
> -                        int i;
> -                        (void)__sync_fetch_and_add(&i, 1);
> -                        return 0;
> -                    }
> +                    int i;
> +                    (void)__sync_fetch_and_add(&i, 1);
>                      ''',
>                      'HAVE___SYNC_FETCH_AND_ADD',
>                      msg='Checking for __sync_fetch_and_add compiler builtin')
>  
>      conf.CHECK_CODE('''
> -                    #include <stdint.h>
> -                    #include <sys/atomic.h>
> -                    int main(void) {
> -                        int32_t i;
> -                        atomic_add_32(&i, 1);
> -                        return 0;
> -                    }
> +                    int32_t i;
> +                    atomic_add_32(&i, 1);
>                      ''',
>                      'HAVE_ATOMIC_ADD_32',
>                      headers='stdint.h sys/atomic.h',
> -- 
> 2.8.1
> 

> 
> -- 
> jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE




More information about the samba-technical mailing list