Using glutCreateWindow() seeds RNG with predictable value

Bug #421651 reported by James D
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
xf86-video-intel
Fix Released
Medium
mesa (Ubuntu)
Fix Released
Low
Unassigned
xserver-xorg-video-intel (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

Binary package hint: xserver-xorg-video-intel

The bug is what it says on the tin.

When glutCreateWindow() is called in the attached OpenGL program, the random number generator appears to be reseeded with a predictable number. This results in predictable output unless srand(time(NULL)) called again.

This appears to be an Intel driver specific bug; when the program was tested on an Nvidia card in a Linux distro running a similar version of freeglut there was no reseeding.

$ lspci | grep VGA
00:02.0 VGA compatible controller: Intel Corporation Mobile GM965/GL960 Integrated Graphics Controller (rev 0c)

$ lsb_release -rd
Description: Ubuntu karmic (development branch)
Release: 9.10

$ apt-cache policy xserver-xorg-video-intel
xserver-xorg-video-intel:
  Installed: 2:2.8.1-1ubuntu1
  Candidate: 2:2.8.1-1ubuntu1
  Version table:
 *** 2:2.8.1-1ubuntu1 0
        500 http://us.archive.ubuntu.com karmic/main Packages
        100 /var/lib/dpkg/status

ProblemType: Bug
Architecture: amd64
Date: Sun Aug 30 16:43:21 2009
DistroRelease: Ubuntu 9.10
MachineType: Dell Inc. Vostro1510
NonfreeKernelModules: wl
Package: xserver-xorg-video-intel 2:2.8.1-1ubuntu1
PccardctlIdent:

PccardctlStatus:

ProcCmdLine: BOOT_IMAGE=/boot/vmlinuz-2.6.31-8-generic root=UUID=e183b92c-5171-4a2f-8883-fde441cfe962 ro quiet splash
ProcEnviron:
 LANG=en_US.UTF-8
 SHELL=/bin/bash
ProcVersionSignature: Ubuntu 2.6.31-8.28-generic
RelatedPackageVersions:
 xserver-xorg 1:7.4+3ubuntu5
 libgl1-mesa-glx 7.6.0~git20090817.7c422387-0ubuntu3
 libdrm2 2.4.12+git20090801.45078630-0ubuntu1
 xserver-xorg-video-intel 2:2.8.1-1ubuntu1
 xserver-xorg-video-ati 1:6.12.99+git20090629.f39cafc5-0ubuntu6
SourcePackage: xserver-xorg-video-intel
Uname: Linux 2.6.31-8-generic x86_64
XorgConf: Error: [Errno 2] No such file or directory: '/etc/X11/xorg.conf'
dmi.bios.date: 07/10/2008
dmi.bios.vendor: Dell Inc.
dmi.bios.version: A10
dmi.board.name: 0M277C
dmi.board.vendor: Dell Inc.
dmi.chassis.type: 8
dmi.chassis.vendor: Dell Inc.
dmi.chassis.version: N/A
dmi.modalias: dmi:bvnDellInc.:bvrA10:bd07/10/2008:svnDellInc.:pnVostro1510:pvrNull:rvnDellInc.:rn0M277C:rvr:cvnDellInc.:ct8:cvrN/A:
dmi.product.name: Vostro1510
dmi.product.version: Null
dmi.sys.vendor: Dell Inc.
fglrx: Not loaded
system:
 distro: Ubuntu
 architecture: x86_64kernel: 2.6.31-8-generic

Related branches

Revision history for this message
James D (jamesgecko) wrote :
Bryce Harrington (bryce)
Changed in xserver-xorg-video-intel (Ubuntu):
status: New → Confirmed
Revision history for this message
Geir Ove Myhr (gomyhr) wrote :

Thank you for including a test program that demonstrates the problem.

I think the bug probably is in the intel specific part of mesa. Using your program I traced the glutCreateWindow call and added lines like
    printf("Random number 11 (fgOpenWindow): %i\n",rand());
in relevant places in the source code of the freeglut source package. glutCreateWindow (in src/freeglut_window.c) calls fgCreateWindow (in src/freeglut_structure.c) which calls fgOpenWindow (in src/freeglut_window.c) which again calls glXMakeCurrent like this:

    glXMakeCurrent(
        fgDisplay.Display,
        window->Window.Handle,
        window->Window.Context
    );

Before the glXMakeCurrent call, rand() returns a random number, while after the call it returns the same every time.

Changed in mesa (Ubuntu):
status: New → Triaged
importance: Undecided → Low
Geir Ove Myhr (gomyhr)
tags: added: 965gm
Revision history for this message
In , Geir Ove Myhr (gomyhr) wrote :
Download full text (3.3 KiB)

Created an attachment (id=29304)
Demo program using freeglut

Forwarding a bug report from ubuntu user Fred:
https://bugs.launchpad.net/ubuntu/+source/xserver-xorg-video-intel/+bug/421651

[Problem]
After calling glutCreateWindow() from the freeglut package on intel hardware, the output of rand() is the same every time the a test program is run.

[Original description]
Binary package hint: xserver-xorg-video-intel

The bug is what it says on the tin.

When glutCreateWindow() is called in the attached OpenGL program, the random number generator appears to be reseeded with a predictable number. This results in predictable output unless srand(time(NULL)) called again.

This appears to be an Intel driver specific bug; when the program was tested on an Nvidia card in a Linux distro running a similar version of freeglut there was no reseeding.

I think the bug probably is in the intel specific part of mesa. Using the test program I traced the glutCreateWindow call and added lines like
    printf("Random number 11 (fgOpenWindow): %i\n",rand());
in relevant places in the source code of the freeglut source package. glutCreateWindow() (in src/freeglut_window.c) calls fgCreateWindow() (in src/freeglut_structure.c) which calls fgOpenWindow() (in src/freeglut_window.c) which again calls glXMakeCurrent() like this:

    glXMakeCurrent(
        fgDisplay.Display,
        window->Window.Handle,
        window->Window.Context
    );

Before the glXMakeCurrent() call, rand() returns a random number, while after the call it returns the same every time.

$ lspci | grep VGA
00:02.0 VGA compatible controller: Intel Corporation Mobile GM965/GL960 Integrated Graphics Controller (rev 0c)

$ lsb_release -rd
Description: Ubuntu karmic (development branch)
Release: 9.10

$ apt-cache policy xserver-xorg-video-intel
xserver-xorg-video-intel:
  Installed: 2:2.8.1-1ubuntu1
  Candidate: 2:2.8.1-1ubuntu1
  Version table:
 *** 2:2.8.1-1ubuntu1 0
        500 http://us.archive.ubuntu.com karmic/main Packages
        100 /var/lib/dpkg/status

ProblemType: Bug
Architecture: amd64
Date: Sun Aug 30 16:43:21 2009
DistroRelease: Ubuntu 9.10
MachineType: Dell Inc. Vostro1510
NonfreeKernelModules: wl
Package: xserver-xorg-video-intel 2:2.8.1-1ubuntu1
PccardctlIdent:

PccardctlStatus:

ProcCmdLine: BOOT_IMAGE=/boot/vmlinuz-2.6.31-8-generic root=UUID=e183b92c-5171-4a2f-8883-fde441cfe962 ro quiet splash
ProcEnviron:
 LANG=en_US.UTF-8
 SHELL=/bin/bash
ProcVersionSignature: Ubuntu 2.6.31-8.28-generic
RelatedPackageVersions:
 xserver-xorg 1:7.4+3ubuntu5
 libgl1-mesa-glx 7.6.0~git20090817.7c422387-0ubuntu3
 libdrm2 2.4.12+git20090801.45078630-0ubuntu1
 xserver-xorg-video-intel 2:2.8.1-1ubuntu1
 xserver-xorg-video-ati 1:6.12.99+git20090629.f39cafc5-0ubuntu6
SourcePackage: xserver-xorg-video-intel
Uname: Linux 2.6.31-8-generic x86_64
XorgConf: Error: [Errno 2] No such file or directory: '/etc/X11/xorg.conf'
dmi.bios.date: 07/10/2008
dmi.bios.vendor: Dell Inc.
dmi.bios.version: A10
dmi.board.name: 0M277C
dmi.board.vendor: Dell Inc.
dmi.chassis.type: 8
dmi.chassis.vendor: Dell Inc.
dmi.chassis.version: N/A
dmi.modalias: dmi:bvnDellInc.:bvrA10:bd07/10/2008:svnDellInc.:pnVostro1510:pvrNull:rvnDellIn...

Read more...

Revision history for this message
In , Geir Ove Myhr (gomyhr) wrote :

Created an attachment (id=29305)
Output of `lspci -vvnn`

Revision history for this message
In , Geir Ove Myhr (gomyhr) wrote :

Created an attachment (id=29306)
Xorg.0.log

Revision history for this message
In , Geir Ove Myhr (gomyhr) wrote :

Created an attachment (id=29307)
glxinfo output

Revision history for this message
Geir Ove Myhr (gomyhr) wrote :

I have forwarded this bug upstream: https://bugs.freedesktop.org/show_bug.cgi?id=23774.

Please subscribe to the upstream bug report so that you may provide any additional information they might need.

Changed in xserver-xorg-video-intel:
status: Unknown → Confirmed
Revision history for this message
In , Idr (idr) wrote :

It looks like this is caused by the use of srandom in src/glx/x11/glxhash.c. We can fix this by either not calling srandom or by providing our own random number generator. The GLX code always calls srandom with 0xDEADBEEF, which seems almost useless.

Revision history for this message
In , Brian-e-paul (brian-e-paul) wrote :

Ugh! That would explain some weirdness I saw a while back when debugging an app that used random numbers. Can we just get rid of the srandom() call and be done?

Revision history for this message
In , Idr (idr) wrote :

Created an attachment (id=29610)
glx: Use initstate_r / random_r instead of corrupting global random number state

The hash table code actually needs some random numbers. Calling random may not be safe, regardless of the srandom issue, in multithreaded applications. Using initstate_r and random_r should fix these issues.

Geir, could you test this patch?

Revision history for this message
In , Geir Ove Myhr (gomyhr) wrote :

(In reply to comment #6)
> Geir, could you test this patch?

Tested. With the patch, the test program gets new random numbers every new second, so the patch resolves the bug.

I see there is still a number of calls to srandom(0xbeefbeef) in src/glx/x11/glxhash.c , but those are probably not invoked when calling glxMakeCurrent().

Revision history for this message
In , Idr (idr) wrote :

(In reply to comment #7)
> (In reply to comment #6)
> > Geir, could you test this patch?
>
> Tested. With the patch, the test program gets new random numbers every new
> second, so the patch resolves the bug.
>
> I see there is still a number of calls to srandom(0xbeefbeef) in
> src/glx/x11/glxhash.c , but those are probably not invoked when calling
> glxMakeCurrent().
>

Correct. The other srandom calls are in a testing "main" function that is #ifdef'ed out.

I've pushed this patch to mesa_7_5_branch, and it will find its way to mesa_7_6_branch and master soon.

Revision history for this message
Geir Ove Myhr (gomyhr) wrote :

This is now fixed upstream. The patch will find its way to mesa_7_6_branch and master soon (it's already in mesa_7_5_branch).

http://cgit.freedesktop.org/mesa/mesa/commit/?h=mesa_7_5_branch&id=9666529b5a5be1fcde82caadc2fe2efa5ea81e49

Changed in xserver-xorg-video-intel:
status: Confirmed → Fix Released
Revision history for this message
Bryce Harrington (bryce) wrote :

Thanks, looks good

Changed in xserver-xorg-video-intel (Ubuntu):
status: Confirmed → Fix Committed
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package mesa - 7.6.0~git20090817.7c422387-0ubuntu8

---------------
mesa (7.6.0~git20090817.7c422387-0ubuntu8) karmic; urgency=low

  * Add 112_dont_corrupt_random_number_state.patch: Fix predictable
    numbers being seeded into the RNG in OpenGL when glutCreateWindow() is
    called.
    (LP: #421651)

 -- Bryce Harrington <email address hidden> Wed, 07 Oct 2009 00:38:40 -0700

Changed in mesa (Ubuntu):
status: Triaged → Fix Released
Oibaf (oibaf)
Changed in xserver-xorg-video-intel:
importance: Unknown → Undecided
status: Fix Released → New
status: New → Invalid
Oibaf (oibaf)
Changed in xserver-xorg-video-intel:
importance: Undecided → Unknown
status: Invalid → Unknown
Geir Ove Myhr (gomyhr)
Changed in xserver-xorg-video-intel (Ubuntu):
status: Fix Committed → Invalid
Changed in xserver-xorg-video-intel:
status: Unknown → Fix Released
Changed in xserver-xorg-video-intel:
importance: Unknown → Medium
Changed in xserver-xorg-video-intel:
importance: Medium → Unknown
Changed in xserver-xorg-video-intel:
importance: Unknown → Medium
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.