Segfault in lrintf() and similar functions

Frank Wille <[email protected]>
Newsgroups gmane.os.netbsd.ports.m68k
Organization Privates Internet Ostwestfalen/Lippe e.V.
Message-ID <[email protected]>
Hi,

it seems we have a serious problem in some new C99 math functions, which
use fenv_t to save/restore the FPU environment. It is also responsible for
X-server segfaults, when moving the mouse (at least on NetBSD/amiga).

The following source reproduces the problem:

---8<---
typedef unsigned long uint32_t;
typedef struct {
        uint32_t fpcr;
        uint32_t fpsr;
        uint32_t fppc;
} fenv_t;

#define __fgetenv(__envp) \
    __asm__ __volatile__ ("fmovem%.l %/fpcr/%/fpsr/%/fpiar,%0" : "=m"
(*__envp))
#define __set_fpsr(__fpsr) \
    __asm__ __volatile__ ("fmove%.l %0,%/fpsr" : : "dm" (__fpsr))  
#define FE_ALL_EXCEPT (0xf8)

static inline int
feholdexcept(fenv_t *__envp)
{
        int __fpsr;
        __fgetenv(__envp);
        __fpsr = __envp->fpsr & ~FE_ALL_EXCEPT;
        __set_fpsr(__fpsr);     /* clear all */
        return 0;
}

int main(void)
{
    fenv_t env;
    return feholdexcept(&env);
}
---8<---

I am currently testing this modification to sys/arch/m68k/include/fenv.h,
which seems to fix it. I would commit that, when there are no objections:

--- fenv.h      22 Mar 2017 23:11:09 -0000      1.6
+++ fenv.h      9 Sep 2017 15:46:17 -0000
@@ -210,7 +210,7 @@
 __fenv_static inline int
 fegetenv(fenv_t *__envp)
 {
-       __fgetenv(__envp);
+       __fgetenv(*__envp);
 
        return 0;
 }
@@ -220,7 +220,7 @@
 {
        fexcept_t __fpcr, __fpsr;
 
-       __fgetenv(__envp);
+       __fgetenv(*__envp);
        __fpsr = __envp->fpsr & ~FE_ALL_EXCEPT;
        __set_fpsr(__fpsr);     /* clear all */
        __fpcr = __envp->fpcr & ~(FE_ALL_EXCEPT << 6);


-- 
Frank Wille
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.