From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from omta002.cacentral1.a.cloudfilter.net (omta002.cacentral1.a.cloudfilter.net [3.97.99.33]) by sourceware.org (Postfix) with ESMTPS id D553B3857702 for ; Wed, 25 Oct 2023 22:39:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D553B3857702 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=Shaw.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=shaw.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D553B3857702 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=3.97.99.33 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698273548; cv=none; b=HC1GahjtlABL2cRUrcHdmGLzvrcyvTXGZ00TxbK0t16QZS4V/+hxefN974Oz8iuIqJt1NFgZruiAmK7Zm11bhCUd1QLvLrFKOgK96bxGW7WJ5ffpdAA+ma17qg7r4NrszsIrVl6PaBFEBLXI9CiWzD2qayUYqZeQyH4oKYb7nN0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698273548; c=relaxed/simple; bh=Iubn7K5H3Ol1rY9Fu0rd8ESTyKYtZUGZaR5KwnPJMvU=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=d+SOXz9sDfBHf45rYME2usmJzsnZqyvUY7DdhszhAvus20IELVOxa/JYD3BKopwG/cfTulwFDWjO9NeBVHdNJTlGkDrj3jlZBsrsoX0losvbdqd0Mo0cNvIcApKw/dKASOvUektfPUEH0P9pq4ZsyXO8NQBPnuVNQuT0VmZ5d+o= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from shw-obgw-4003a.ext.cloudfilter.net ([10.228.9.183]) by cmsmtp with ESMTPS id vd6NqjzS3B0n0vmWnqmyXF; Wed, 25 Oct 2023 22:39:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=shaw.ca; s=s20180605; t=1698273545; bh=Iubn7K5H3Ol1rY9Fu0rd8ESTyKYtZUGZaR5KwnPJMvU=; h=Date:Reply-To:Subject:To:References:Cc:From:In-Reply-To; b=OKBEzZKkNRsK0wDLHi7qH/DixzqYgmhxvRQ6SXJfPV/GkfxulqQO1hLtAw5o7E+EA li0rCqvPrWiiWHaGy0z8pAvlffQRsoHecIjkat+KiBj/tOkpZs3cEK7D2mf57ls/jY 5VF3WZDKFLlIrWiono5OYGXfH+hpCGe/3gGRkdJzJvbHvHmRTWHJTKbS3Pm8lt0VFO 9XEVohxxkHTBcyfmouO1Rm28FvjbDZnrzX0eUAkUl9hPCNb+Prn7DtHhn+Q0bDOVFN AE9dBqlmG3yXimz488hYN2Ef6xrpUGygiaQUh60llxFNYicyE8r0+3uO/NAmpOxTGH 1JQmdt7t6Giqw== Received: from [10.0.0.5] ([184.64.102.149]) by cmsmtp with ESMTP id vmWmqRsXIMsNfvmWmqaBYK; Wed, 25 Oct 2023 22:39:05 +0000 X-Authority-Analysis: v=2.4 cv=KJNJsXJo c=1 sm=1 tr=0 ts=65399909 a=DxHlV3/gbUaP7LOF0QAmaA==:117 a=DxHlV3/gbUaP7LOF0QAmaA==:17 a=IkcTkHD0fZMA:10 a=CCpqsmhAAAAA:8 a=6I5d2MoRAAAA:8 a=NEAV23lmAAAA:8 a=ILxlH8vaY-4WpylZPdIA:9 a=QEXdDO2ut3YA:10 a=ul9cdbp4aOFLsgKbc677:22 a=IjZwj45LgO3ly-622nXo:22 Message-ID: <4a4c944f-7246-4d5f-9775-2906d27b301a@Shaw.ca> Date: Wed, 25 Oct 2023 16:39:04 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Reply-To: newlib@sourceware.org Subject: Re: feraiseexcept does not raise traps in newlib on x86/_64 [was Cygwin] Content-Language: en-CA To: cygwin@cygwin.com, newlib@sourceware.org References: <39807966.XM6RcZxFsP@nimes> Cc: Bruno Haible From: Brian Inglis Organization: Inglis In-Reply-To: <39807966.XM6RcZxFsP@nimes> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CMAE-Envelope: MS4xfDEZbC53aHjGUn+KgAYmapJWr4mT1C0loOJjxD843z7CWQtDju+TsP2KRGqDXJc5QJCHUppagTf4tAFJ/BGJqOjR8vXH7TQenki4rBWr0DAyfs5sP8lC OgX54yYwfLqnKaCwc2ZL3AdfOmllnxE6YJpC+s1qUNlVH5MJYcyd2pHHC61Ird6YMjQe3CVUKP9jleURLayREujG89R4qvUpC3XioM/INi1SahyGZuk18yxq J6GStc/3nPc29OCZRcnZEA== X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_ASCII_DIVIDERS,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 2023-10-25 03:21, Bruno Haible via Cygwin wrote: > Hi, > > Seen on > - Cygwin 3.4.6 or 2.9.0 on x86_64. > - Cygwin 2.9.0 on i386. > > According to ISO C 23 § 7.6.4.3 > "The feraiseexcept function attempts to raise the supported floating-point > exceptions represented by its argument. 266) > Footnote 266) The effect is intended to be similar to that of floating-point > exceptions raised by arithmetic operations. Hence, implementation extensions > associated with raising a floating-point exception (for example, enabled > traps or IEC 60559 alternate exception handling) should be honored." > > This does not work. How to reproduce: > =================================== foo.c =================================== > #define _GNU_SOURCE 1 > #include > #include > > int > main () > { > /* Clear FE_INVALID exceptions from past operations. */ > if (feclearexcept (FE_INVALID)) > return 1; > > /* An FE_INVALID exception shall trigger a SIGFPE signal, which by default > terminates the program. */ > if (feenableexcept (FE_INVALID) == -1) > return 2; > > if (feraiseexcept (FE_INVALID)) > return 3; > > return 0; > } > ============================================================================= > > For x86_64: > > $ x86_64-pc-cygwin-gcc -Wall foo.c > $ ./a.exe; echo $? > > For i386: > > $ i686-pc-cygwin-gcc -Wall foo.c > $ ./a.exe; echo $? > > Expected result (like seen e.g. on Linux/glibc): > Floating-point exception (core dumped) > 136 > > Actual result: > 0 > > The workaround for x86_64 is to redefine feeraiseexcept in the same way > as glibc does. This modified test program includes the workaround: > > =================================== foo.c =================================== > #define _GNU_SOURCE 1 > #include > #include #ifdef REDEFFE > /* The floating-point environment of the 387 unit. */ > typedef struct > { > /* 7 32-bit words: */ > unsigned short __control_word; /* fctrl register */ > unsigned short __reserved1; > unsigned short __status_word; /* fstat register */ > unsigned short __reserved2; > unsigned int more[5]; > } > x86_387_fenv_t; > > int > feraiseexcept (int exceptions) > { > exceptions &= FE_ALL_EXCEPT; > if ((exceptions & ~(FE_INVALID | FE_DIVBYZERO)) == 0 && 0) > { > /* First: invalid exception. */ > if (exceptions & FE_INVALID) > { > double volatile a; > double volatile b; > a = 0; b = a / a; > (void) b; > } > /* Next: division by zero. */ > if (exceptions & FE_DIVBYZERO) > { > double volatile a, b; > double volatile c; > a = 1; b = 0; c = a / b; > (void) c; > } > } > else > { > /* The general case. */ > > /* Set the bits in the 387 unit. */ > x86_387_fenv_t env; > unsigned short orig_status_word; > __asm__ __volatile__ ("fnstenv %0" : "=m" (*&env)); > orig_status_word = env.__status_word; > env.__status_word |= exceptions; > if (env.__status_word != orig_status_word) > { > __asm__ __volatile__ ("fldenv %0" : : "m" (*&env)); > /* A trap (if enabled) is triggered only at the next floating-point > instruction. Force it to occur here. */ > __asm__ __volatile__ ("fwait"); > } > } > return 0; > } #endif /* REDEFFE */ > int > main () > { > /* Clear FE_INVALID exceptions from past operations. */ > if (feclearexcept (FE_INVALID)) > return 1; > > /* An FE_INVALID exception shall trigger a SIGFPE signal, which by default > terminates the program. */ > if (feenableexcept (FE_INVALID) == -1) > return 2; > > if (feraiseexcept (FE_INVALID)) > return 3; > > return 0; > } > ============================================================================= > > (This workaround *should* also work on i386, but it doesn't. I don't know > why.) Thanks for the report Bruno, Confirmed on Cygwin current stable with #ifdef added for ease of rebuild: $ uname -srvmo CYGWIN_NT-10.0-19045 3.4.9-1.x86_64 2023-09-06 11:19 UTC x86_64 Cygwin $ gcc -UREDEFFE -o feraiseexcept{,.c} && ./feraiseexcept; echo $? 0 $ gcc -DREDEFFE -o feraiseexcept{,.c} && ./feraiseexcept; echo $? Floating point exception (core dumped) 136 Cygwin no longer supports x86/i686 32 bit. Anyone have a Cygwin 3.5.0 test release environment available, just in case it might be different in some way, and could confirm? The code is in newlib, so redirecting there: https://sourceware.org/cgit/newlib-cygwin/tree/newlib/libm/machine/shared_x86/fenv.c#n253 That only has similar common code as in glibc, not the specific exceptions. Please, could anyone test on a non-Cygwin newlib x86/_64 build? The BSDs store, add exceptions, then load first the X87 then SSE MXCSR environments: https://cgit.freebsd.org/src/tree/lib/msun/amd64/fenv.c#n32 https://github.com/NetBSD/src/blob/trunk/lib/libm/arch/x86_64/fenv.c#L195 https://github.com/openbsd/src/blob/master/lib/libm/arch/amd64/fenv.c#L116 which is handled in newlib fenv.c functions by fgetenv/fsetenv *except* in feraiseexcept! -- Take care. Thanks, Brian Inglis Calgary, Alberta, Canada La perfection est atteinte Perfection is achieved non pas lorsqu'il n'y a plus rien à ajouter not when there is no more to add mais lorsqu'il n'y a plus rien à retirer but when there is no more to cut -- Antoine de Saint-Exupéry