From nobody Mon Sep 1 13:15:14 2025 X-Original-To: bugs@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4cFq9Z3vmnz66ShK for ; Mon, 01 Sep 2025 13:15:14 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cFq9Z3BRLz3bQP for ; Mon, 01 Sep 2025 13:15:14 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1756732514; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nnphvTu2yl1Qx+NYj0M2+16RboNI3gocZZWzCeMtOLY=; b=sRF0UQOhu/E2GUunOPWrd+eDJvYNlX50xYsF46e8FVcZCLo3/Q16XTjq0f5UU3jHyLmZBX UgKIFAh9R8FMjOOJMAWlP0al7/MRcrlD8+jGVinqzpo0afq3WekGaW4oSAqNwwkXForrGP ahf6LnRQut4+eX91gQxe8Yp7bJd/sEGljKLmPIHfSW/ZK1Zw5rEkup8QTOhHz5iiKzpaXZ bGfErQ7uHTxAF0oefDk46RHRff6RIajVAPY3FXQqg35upEdUfJl64jbSBGU8licHPH9/CL VVijCyplk+D4P4111kLVYDuQjO9ylHK9RL72Bd39oq8GUqyuZ2wa/leoAUvolQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1756732514; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nnphvTu2yl1Qx+NYj0M2+16RboNI3gocZZWzCeMtOLY=; b=HfxleMlU3fT1X6aFLqXJAqZXJLSZqPt3tFjLVE7+LaHWhdlinAvpFfdt6LAoAuO/XSMJz9 fqO5SLe9VB+9QQrIMBkHprY+LsSJGEENe/cu3Ukjq7gRL9EBs0uTKfWQa7WrAK0CGHNNqG Hvgitf7qtWTkgpA50Q7QAqHlqIQ5vWRqEzG2QztIyo0PDakyDFt0wc6qnqotonn4Zd/537 sNTVZSUor7CLQWe4GlILO+xVrXatsLZlmVu2JJvJs1Qy4YoCHNXuFRdiTpDn9rVc/+1ZpR yCAWvQ216sw0RocNM7p4+CVfjpBhfGOhulsRoxfhRWu7kIiBriP+QgbRqtFYGQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1756732514; a=rsa-sha256; cv=none; b=r4n/fAwiN0sg6cjo/WOZkMZ/UqbOYiHVMDf/pixdsvKSJD6PuqeF6wTe/3FYC1h5gWOPS4 bq/hdo9liDUTB25eF7jcjS75pzfauW1s3bgSrpiZQx5skJK7Xz8ayTTLQuQbt9v0X5ncP0 nNx/igKEahWYgGaEpYzo+kUI0ElL084UeExNO/zOEr7v6AsIPJEJsc5lJeMSvKffR2cg8A v5CIn527LexImHJf4RV9MdwCbD81fauLy23S78THdiZZlsXnwUfcZ3igbb/9kw89RiL3EQ 1WZ5cE7dJYRLvfutnipHWbCsYHaiF2A9LZhqTKmFIir6soWs3ms1VlP5WHVrag== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2610:1c1:1:606c::50:1d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4cFq9Z2MBnz10bF for ; Mon, 01 Sep 2025 13:15:14 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org ([127.0.1.5]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id 581DFEcT078552 for ; Mon, 1 Sep 2025 13:15:14 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id 581DFEuQ078551 for bugs@FreeBSD.org; Mon, 1 Sep 2025 13:15:14 GMT (envelope-from bugzilla-noreply@freebsd.org) X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f From: bugzilla-noreply@freebsd.org To: bugs@FreeBSD.org Subject: [Bug 289232] i386 (x87) signal incorrectly reports FPE_FLTRES over FPE_FLTUND when both are present Date: Mon, 01 Sep 2025 13:15:14 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: kern X-Bugzilla-Version: 13.5-RELEASE X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Many People X-Bugzilla-Who: riverstdr@gmail.com X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: bugs@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="UTF-8" X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated List-Id: Bug reports List-Archive: https://lists.freebsd.org/archives/freebsd-bugs List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-bugs@FreeBSD.org MIME-Version: 1.0 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D289232 Bug ID: 289232 Summary: i386 (x87) signal incorrectly reports FPE_FLTRES over FPE_FLTUND when both are present Product: Base System Version: 13.5-RELEASE Hardware: i386 OS: Any Status: New Severity: Affects Many People Priority: --- Component: kern Assignee: bugs@FreeBSD.org Reporter: riverstdr@gmail.com When an x87 floating-point operation underflows, the result is often also inexact; so both the #U and #P flags are set in the x87 status register. IEEE 754 prioritizes FP exception reporting with a hierarchy. When both UNDERFLOW and INEXACT are present, UNDERFLOW should be reported (as UNDERFL= OW implies INEXACT.) Unfortunately, the signal handling code appears to prefer INEXACT in this c= ase, so that the 'si_code' handed to the signal handler indicates FPE_FLTRES ins= tead of FPE_FLTUND. This makes detecting underflow in a signal handler impossible when FE_INEXA= CT is allowed in conjunction with FE_UNDERFLOW. If you set the floating-pt mask with fpeenableexcept() to be feenableexcept(FE_UNDERFLOW | FE_INEXACT); the= x87 floating-pt status register reported in the machine context only seems to=20 have #P set (something has cleared out #U). But, if you set it to only feenableexcept(FE_UNDERFLOW), the same operation correctly reports FPE_FLTU= ND in si_code, and the x87 status register only has #U. I've tried several FreeBSD versions - the problem appears to be a very old = one all the back to FreeBSD 4.5 a least. Interestingly, when using SSE floating-point, the FPE_FLTUND is properly reported. Here's an example program - compile this with -m64 (using SSE regs) and the proper FPE_FLTUND is reported, compile it with -m32 (which uses x87 operati= ons) and the incorrect FPE_FLTRES is reported. #include #include #include #include #include void sigfpe_handler(int signum, siginfo_t *info, void *context) { ucontext_t *uc =3D (ucontext_t *)context; int si_code =3D info->si_code; printf("Caught SIGFPE (Floating-Point Exception)!\n"); printf("Signal info (si_code): %d", info->si_code); if(si_code =3D=3D FPE_FLTUND) printf(": FPE_FLTUND\n"); else if (si_code =3D=3D FPE_FLTRES) printf(": FPE_FLTRES\n"); else printf("\n"); /* The FP state pointer is within the machine-specific context */ /* This is the structure containing both x87 and SSE state */ struct _fpstate *fpregs =3D (struct _fpstate *)uc->uc_mcontext.mc_fpsta= te; if (fpregs) { // The x87 status word is at the beginning of the _fpstate structure // The fxsave format places the FPU status word (fsw) at offset 2 // // On x86-64, the mc_fpstate points to the FXSAVE area, where the // x87 FPU Status Word is at offset 2. unsigned short x87_status_word =3D *(unsigned short *)((char *)fpre= gs + 2); printf("x87 FPU Status Word (FSW): 0x%04x\n", x87_status_word); if(x87_status_word & 0x01) printf(" INVALID OP\n"); if(x87_status_word & 0x02) printf(" DENORMAL OPERAND\n"); if(x87_status_word & 0x04) printf(" ZERO DIVIDE\n"); if(x87_status_word & 0x08) printf(" OVERFLOW\n"); if(x87_status_word & 0x10) printf(" UNDERFLOW\n"); if(x87_status_word & 0x20) printf(" INEXACT\n"); } _exit(1); } int main() { struct sigaction sa; sa.sa_sigaction =3D sigfpe_handler; sa.sa_flags =3D SA_SIGINFO; sigaction(SIGFPE, &sa, NULL); // Enable traps for floating-point exceptions // The following code is architecture-specific. // It enables traps for underflow and inexact exceptions. // The underflow trap should have priority over the inexact trap. fedisableexcept(FE_INEXACT); feenableexcept(FE_UNDERFLOW ); feenableexcept(FE_UNDERFLOW | FE_INEXACT);=20 double small_num =3D 1.0E-300; double large_num =3D 1.0E+300; // This operation will cause both an underflow and an inexact result. double result =3D small_num / large_num; printf("Result is: %e\n", result); return 0; } --=20 You are receiving this mail because: You are the assignee for the bug.=