Bug 205149 - x11/kde4-workspace: kwin_opengl_test fails with segmentation fault
Summary: x11/kde4-workspace: kwin_opengl_test fails with segmentation fault
Status: Closed FIXED
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: amd64 Any
: --- Affects Some People
Assignee: David Naylor
URL:
Keywords: crash, needs-patch, needs-qa
Depends on:
Blocks:
 
Reported: 2015-12-08 22:06 UTC by Dwayne MacKinnon
Modified: 2016-02-08 17:56 UTC (History)
4 users (show)

See Also:
bugzilla: maintainer-feedback? (kde)
rodrigo.freebsd: merge-quarterly?


Attachments
Fix linking order (457 bytes, patch)
2016-01-18 06:59 UTC, David Naylor
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dwayne MacKinnon 2015-12-08 22:06:22 UTC
Hi. Noticed yesterday that desktop effects weren't working on my FreeBSD 10.2-RELEASE-p8 amd64 box. /usr/local/lib/kde4/libexec/kwin_opengl_test keeps seg-faulting on me. I rebuilt kde4-workspace and nvidia-driver with WITH_DEBUG=yes and managed to get a decent backtrace:

#0  0x000000080697d201 in pthread_mutexattr_setkind_np () from /lib/libthr.so.3
[New LWP 100731]
(gdb) bt
#0  0x000000080697d201 in pthread_mutexattr_setkind_np () from /lib/libthr.so.3
#1  0x0000000801a6c9c7 in glXCreateNewContext () from /usr/local/lib/libGL.so.1
#2  0x0000000804bd958c in _nv021glcore () from /usr/local/lib/libnvidia-glcore.so.1
#3  0x0000000804f4821e in _nv015glcore () from /usr/local/lib/libnvidia-glcore.so.1
#4  0x0000000801a4cefb in glXCreateNewContext () from /usr/local/lib/libGL.so.1
#5  0x0000000801a4da0a in glXCreateNewContext () from /usr/local/lib/libGL.so.1
#6  0x0000000800605a9f in r_debug_state () from /libexec/ld-elf.so.1
#7  0x00000008006050ee in __tls_get_addr () from /libexec/ld-elf.so.1
#8  0x0000000800603439 in .text () from /libexec/ld-elf.so.1
#9  0x0000000000000000 in ?? ()

Now,  pthread_mutexattr_setkind_np reminded me of this bug: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=203372 

For a test, I ran the program this way: LD_PRELOAD=/lib/libthr.so.3 kwin_opengl_test . It worked fine, no seg fault.

I think the issue might be that nvidia's libGL.so.1 doesn't link to libthr.so.3 . Here's a LDD of the file:

/usr/local/lib/.nvidia/libGL.so.1:
        libnvidia-tls.so.1 => /usr/local/lib/libnvidia-tls.so.1 (0x801736000)
        libnvidia-glcore.so.1 => /usr/local/lib/libnvidia-glcore.so.1 (0x801a00000)
        libX11.so.6 => /usr/local/lib/libX11.so.6 (0x80453a000)
        libXext.so.6 => /usr/local/lib/libXext.so.6 (0x804873000)
        libc.so.7 => /lib/libc.so.7 (0x800821000)
        libm.so.5 => /lib/libm.so.5 (0x804a84000)
        libxcb.so.1 => /usr/local/lib/libxcb.so.1 (0x804cad000)
        librpcsvc.so.5 => /usr/lib/librpcsvc.so.5 (0x804ecc000)
        libXau.so.6 => /usr/local/lib/libXau.so.6 (0x8050d5000)
        libpthread-stubs.so.0 => /usr/local/lib/libpthread-stubs.so.0 (0x8052d7000)
        libXdmcp.so.6 => /usr/local/lib/libXdmcp.so.6 (0x8054d8000)

Anyway, I have no idea how to actually fix it. Linking libthr before libGL might do it, (kwin_opengl_test does link to libthr according to ldd) but I have no idea how to make that happen.
Comment 1 Bengt Ahlgren 2015-12-09 09:40:58 UTC
I noticed this too, and I think your analysis is correct.  I think that the proper solution is that nvidia's libGL should link to libthr.

A workaround is to set KWIN_DIRECT_GL=1 in the environment so that kwin never runs kwin_opengl_test.
Comment 2 Quentin Schwerkolt 2015-12-09 10:10:46 UTC
Hi, I had this issue a long time ago (it was in 2013 on 9.x, I think) but I never solved it. I think you can enforce nvidia's libGL to link with libthr.so by adding "LDFLAGS+= -pthread" in the x11/nvidia-driver's Makefile. I can make a patch if you need but I cannot test since I am not running FreeBSD with NVidia's graphic card anymore...
Comment 3 Dwayne MacKinnon 2015-12-09 16:50:57 UTC
(In reply to Bengt Ahlgren from comment #1)

Thanks for the tip. A bit more elegant then my workaround, which was to put in place a dummy kwin_opengl_test program that does nothing except return 0.
Comment 4 Dwayne MacKinnon 2015-12-09 16:52:18 UTC
(In reply to Quentin Schwerkolt from comment #2)

I'm not sure if that's possible. nvidia ships a precompiled libGL.so.1. Still though, it's not like linkers are my strong suit. (I'm a few years removed from my coding days.) If you make a patch, I'll be happy to try it out for you.
Comment 5 David Naylor freebsd_committer freebsd_triage 2016-01-18 06:56:22 UTC
To add some more information:

# gdb /usr/local/lib/kde4/libexec/kwin_opengl_test kwin_opengl_test.core
GNU gdb 6.1.1 [FreeBSD]
<snip/>
#0  0x000000080060c2a2 in _rtld_is_dlopened () from /libexec/ld-elf.so.1
[New LWP 100463]
(gdb) bt
#0  0x000000080060c2a2 in _rtld_is_dlopened () from /libexec/ld-elf.so.1
#1  0x0000000800605c8d in _r_debug_postinit () from /libexec/ld-elf.so.1
#2  0x000000080060346d in .text () from /libexec/ld-elf.so.1
#3  0x0000000804f3a365 in _nv003glcore () from /usr/local/lib/libnvidia-glcore.so.1
#4  0x0000000804b9e690 in _nv023glcore () from /usr/local/lib/libnvidia-glcore.so.1
#5  0x0000000804f1effc in _nv015glcore () from /usr/local/lib/libnvidia-glcore.so.1
#6  0x0000000801a414eb in glXCreateNewContext () from /usr/local/lib/libGL.so.1
#7  0x0000000801a09e5a in .init () from /usr/local/lib/libGL.so.1
#8  0x0000000800605a9f in r_debug_state () from /libexec/ld-elf.so.1
#9  0x00000008006050ee in __tls_get_addr () from /libexec/ld-elf.so.1
#10 0x0000000800603439 in .text () from /libexec/ld-elf.so.1
#11 0x0000000000000000 in ?? ()

One workaround is:

cat > /usr/local/lib/kde4/libexec/kwin_opengl_test
#!/bin/sh
exit 0
^D
Comment 6 David Naylor freebsd_committer freebsd_triage 2016-01-18 06:59:32 UTC
Created attachment 165745 [details]
Fix linking order

The cause of this error is libXft linking before libGL, if the linking order is reversed (as in this patch) then no error happens.  I'm not sure why the linking order is giving issues.
Comment 7 commit-hook freebsd_committer freebsd_triage 2016-02-08 04:21:44 UTC
A commit references this bug:

Author: dbn
Date: Mon Feb  8 04:20:50 UTC 2016
New revision: 408463
URL: https://svnweb.freebsd.org/changeset/ports/408463

Log:
  x11/kde4-workspace: fix OpenGL tests with nVidia's libGL

  nVidia's libGL causes a segfault in ld-elf if the application that is linking
  to libGL also links to another library (ordered before linking to libGL) and
  the second library links to libthr.  For example:

  kwin_opengl_test
   -> libXft
     -> libthr
   -> libGL

  Two workarounds are known:
   1) Change the linking order to have libGL linked first, or
   2) Also link to libthr in the (base) application.

  This patch implements the latter fix.

  PR:	205149, 206899
  MFH:	2016Q1

Changes:
  head/x11/kde4-workspace/Makefile
  head/x11/kde4-workspace/files/patch-kwin_opengltest_CMakeLists.txt
Comment 8 David Naylor freebsd_committer freebsd_triage 2016-02-08 04:48:03 UTC
Fixed in r408463.  Thank you for the PR.
Comment 9 commit-hook freebsd_committer freebsd_triage 2016-02-08 17:56:59 UTC
A commit references this bug:

Author: dbn
Date: Mon Feb  8 17:56:13 UTC 2016
New revision: 408489
URL: https://svnweb.freebsd.org/changeset/ports/408489

Log:
  MFH: r408463

  x11/kde4-workspace: fix OpenGL tests with nVidia's libGL

  nVidia's libGL causes a segfault in ld-elf if the application that is linking
  to libGL also links to another library (ordered before linking to libGL) and
  the second library links to libthr.  For example:

  kwin_opengl_test
   -> libXft
     -> libthr
   -> libGL

  Two workarounds are known:
   1) Change the linking order to have libGL linked first, or
   2) Also link to libthr in the (base) application.

  This patch implements the latter fix.

  PR:		205149, 206899
  Approved by:	ports-secteam (feld@)

Changes:
_U  branches/2016Q1/
  branches/2016Q1/x11/kde4-workspace/Makefile
  branches/2016Q1/x11/kde4-workspace/files/patch-kwin_opengltest_CMakeLists.txt