[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
new module 'rand'
|
From: |
Bruno Haible |
|
Subject: |
new module 'rand' |
|
Date: |
Thu, 09 Nov 2023 16:16:27 +0100 |
On CheriBSD, I see these test failures:
FAIL: test-pthread-tss
FAIL: test-thread_local
FAIL: test-tls
FAIL: test-tss
In the debugger I see this:
(gdb) where
#0 random_r (estate=0x42319000 [rwRW,0x42319000-0x423190d0])
at
/local/scratch/jenkins/workspace/CheriBSD-pipeline_releng_22.12@2/cheribsd/lib/libc/stdlib/random.c:471
#1 0x0000000040394578 in rand () at
/local/scratch/jenkins/workspace/CheriBSD-pipeline_releng_22.12@2/cheribsd/lib/libc/stdlib/rand.c:85
#2 0x0000000000111468 in perhaps_yield () at
../../gltests/test-thread_local.c:79
#3 0x00000000001113dc in worker_thread (arg=0x8) at
../../gltests/test-thread_local.c:127
#4 0x00000000401a19d4 in thrd_entry (arg=<optimized out>)
at
/local/scratch/jenkins/workspace/CheriBSD-pipeline_releng_22.12@2/cheribsd/lib/libstdthreads/thrd.c:52
#5 0x00000000401e3de4 in thread_start (curthread=0x408f4a00
[rwRW,0x408f4a00-0x408f53d0])
at
/local/scratch/jenkins/workspace/CheriBSD-pipeline_releng_22.12@2/cheribsd/lib/libthr/thread/thr_create.c:319
#6 0x00000000401e38ec in _pthread_create (thread=0xfffffff7fe90
[rwRW,0xfffffff7fe10-0xfffffff7ff10], attr=<optimized out>,
start_routine=<optimized out>,
arg=<optimized out>) at
/local/scratch/jenkins/workspace/CheriBSD-pipeline_releng_22.12@2/cheribsd/lib/libthr/thread/thr_create.c:214
(gdb) info locals
r = 0x423190d0 [rwRW,0x42319000-0x423190d0]
f = 0x42319054 [rwRW,0x42319000-0x423190d0]
i = <optimized out>
So, rand() is crashing in multithreaded tests. But it works fine in the many
single-threaded test programs. Could it be that rand() is not multithread-
safe?
According to the standards, rand() should be multithread-safe.
When I add a 'rand' module and force it to invoke the gnulib implementation
of random_r (via random()), I see a crash inside gnulib's random_r instead.
But wait! That's because gnulib's 'random' implementation is also not
multithread-safe:
#ifdef _LIBC
# include <libc-lock.h>
#else
/* Allow memory races; that's random enough. */
# define __libc_lock_define_initialized(class, name)
# define __libc_lock_lock(name) ((void) 0)
# define __libc_lock_unlock(name) ((void) 0)
#endif
Memory races are not "random"; they are undefined behaviour. They crash on
CheriBSD and they may crash (or return low-quality random numbers) on other
systems as well.
With the patches below, rand() becomes multithread-safe, and the 4 test failures
disappear.
2023-11-09 Bruno Haible <bruno@clisp.org>
rand: Add tests.
* tests/test-rand.c: New file.
* modules/rand-tests: New file.
rand: New module.
* lib/rand.c: New file, based on glibc/stdlib/rand.c.
* m4/rand.m4: New file.
* modules/rand: New file.
* doc/posix-functions/rand.texi: Mention the new module.
2023-11-09 Bruno Haible <bruno@clisp.org>
random: Fix multithread-safety bug on CheriBSD.
* m4/random.m4 (gl_FUNC_RANDOM): Override on CheriBSD.
* lib/random.c: Include glthread/lock.h.
(__libc_lock_define_initialized, __libc_lock_lock, __libc_lock_unlock):
Define to do real locking.
* modules/random (Depends-on): Add lock.
* doc/posix-functions/random.texi: Mention the multithread-safety
problem.
0001-random-Fix-multithread-safety-bug-on-CheriBSD.patch
Description: Text Data
0002-rand-New-module.patch
Description: Text Data
0003-rand-Add-tests.patch
Description: Text Data
- new module 'rand',
Bruno Haible <=