NetBSD Problem Report #55897

From www@netbsd.org  Wed Dec 30 13:10:18 2020
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id B5B8E1A921F
	for <gnats-bugs@gnats.NetBSD.org>; Wed, 30 Dec 2020 13:10:18 +0000 (UTC)
Message-Id: <20201230131017.2A0F51A923A@mollari.NetBSD.org>
Date: Wed, 30 Dec 2020 13:10:17 +0000 (UTC)
From: alexander.i.mukhin@gmail.com
Reply-To: alexander.i.mukhin@gmail.com
To: gnats-bugs@NetBSD.org
Subject: A mess with complex arithmetics in earmv7hf
X-Send-Pr-Version: www-1.0

>Number:         55897
>Category:       lib
>Synopsis:       A mess with complex arithmetics in earmv7hf
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Dec 30 13:15:00 +0000 2020
>Originator:     Alexander Mukhin
>Release:        9.1_STABLE
>Organization:
>Environment:
NetBSD rpi2 9.1_STABLE NetBSD 9.1_STABLE (MY-RPI2-9-v3) #1: Mon Dec 21 12:31:59 UTC 2020  alexandr1@rpi2:/home/alexandr1/build/usr/obj/sys/arch/evbarm/compile/MY-RPI2-9-v3 evbarm
>Description:
I'm developing a Fourier transform library. While testing my software on
NetBSD/earmv7hf, I found, to my surprise, that even simplest complex 
arithmetics cause the programs to dump core or return garbage, if they
are linked against libm.

Tracking the issue down, I found that it is caused by the builtin
complex arithmetic routines, such as muldc3, taken from compiler-rt and
included in libm. These routines use the standard AAPCS calling
convention, while the compiler calls them via the VFP calling convention 
variant, utilizing VFP registers for parameter passing and result
return.

All this is because the complex arithmetic routines in compiler-rt are
defined with the COMPILER_RT_ABI macro, which expands to
__attribute__((pcs("aapcs"))) and overrides the default ABI.
>How-To-Repeat:
$ cat test.c 
#include <complex.h>
int main (void) {
        double complex z,w,u;
        z=1+I;
        w=2+I;
        u=z*w;
        return 0;
}
$ gcc test.c -lm
$ ./a.out
[1]   Bus error (core dumped) ./a.out

>Fix:
A quick and dirty fix I used for myself was to force the macro
COMPILER_RT_ABI to be undefined when compiling these routines for libm:

$ diff -u src/lib/libm/compiler_rt/Makefile.inc.orig src/lib/libm/compiler_rt/Makefile.inc
--- src/lib/libm/compiler_rt/Makefile.inc.orig  2016-02-27 19:06:56.000000000 +0000
+++ src/lib/libm/compiler_rt/Makefile.inc       2020-12-30 10:15:24.865722208 +0000
@@ -36,5 +36,6 @@
 .  else
 SRCS+= ${src}
 COPTS.${src}+= -Wno-error=missing-prototypes
+COPTS.${src}+= -U__ARM_EABI__
 .  endif
 .endfor

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.