gcc/libstdc++-v3/testsuite/util
Jonathan Wakely 5f070ba298 libstdc++: Add PRNG fallback to std::random_device
This makes std::random_device usable on VxWorks when running on older
x86 hardware. Since the r10-728 fix for PR libstdc++/85494 the library
will use the new code unconditionally on x86, but the cpuid checks for
RDSEED and RDRAND can fail at runtime, depending on the hardware where
the code is executing. If the OS does not provide /dev/urandom then this
means the std::random_device constructor always fails. In previous
releases if /dev/urandom is unavailable then std::mt19937 was used
unconditionally.

This patch adds a fallback for the case where the runtime cpuid checks
for x86 hardware instructions fail, and no /dev/urandom is available.
When this happens a std::linear_congruential_engine object will be used,
with a seed based on hashing the engine's address and the current time.
Distinct std::random_device objects will use different seeds, unless an
object is created and destroyed and a new object created at the same
memory location within the clock tick. This is not great, but is better
than always throwing from the constructor, and better than always using
std::mt19937 with the same seed (as GCC 9 and earlier do).

libstdc++-v3/ChangeLog:

	* src/c++11/random.cc (USE_LCG): Define when a pseudo-random
	fallback is needed.
	[USE_LCG] (bad_seed, construct_lcg_at, destroy_lcg_at, __lcg):
	New helper functions and callback.
	(random_device::_M_init): Add 'prng' and 'all' enumerators.
	Replace switch with fallthrough with a series of 'if' statements.
	[USE_LCG]: Construct an lcg_type engine and use __lcg when cpuid
	checks fail.
	(random_device::_M_init_pretr1) [USE_MT19937]: Accept "prng"
	token.
	(random_device::_M_getval): Check for callback unconditionally
	and always pass _M_file pointer.
	* testsuite/26_numerics/random/random_device/85494.cc: Remove
	effective-target check. Use new random_device_available helper.
	* testsuite/26_numerics/random/random_device/94087.cc: Likewise.
	* testsuite/26_numerics/random/random_device/cons/default-cow.cc:
	Remove effective-target check.
	* testsuite/26_numerics/random/random_device/cons/default.cc:
	Likewise.
	* testsuite/26_numerics/random/random_device/cons/token.cc: Use
	new random_device_available helper. Test "prng" token.
	* testsuite/util/testsuite_random.h (random_device_available):
	New helper function.
2021-03-26 19:12:12 +00:00
..
atomic Update copyright years. 2021-01-04 10:26:59 +01:00
common_type Update copyright years. 2021-01-04 10:26:59 +01:00
debug Update copyright years. 2021-01-04 10:26:59 +01:00
exception libstdc++: Fix and complete __gnu_debug::basic_string implementation 2021-03-25 21:56:14 +01:00
hash_fn Update copyright years. 2021-01-04 10:26:59 +01:00
io Update copyright years. 2021-01-04 10:26:59 +01:00
native_type Update copyright years. 2021-01-04 10:26:59 +01:00
performance Update copyright years. 2021-01-04 10:26:59 +01:00
pstl
regression Update copyright years. 2021-01-04 10:26:59 +01:00
statistic Update copyright years. 2021-01-04 10:26:59 +01:00
thread Update copyright years. 2021-01-04 10:26:59 +01:00
replacement_memory_operators.h Update copyright years. 2021-01-04 10:26:59 +01:00
slow_clock.h Update copyright years. 2021-01-04 10:26:59 +01:00
specfun_testcase.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_abi_check.cc Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_abi.cc Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_abi.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_allocator.cc Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_allocator.h libstdc++: Make test memory_resource work without exceptions and RTTI 2021-02-12 14:30:13 +00:00
testsuite_api.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_character.cc Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_character.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_common_types.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_container_traits.h libstdc++: Fix and complete __gnu_debug::basic_string implementation 2021-03-25 21:56:14 +01:00
testsuite_containergen.h libstdc++: Allow seeding random engines in testsuite 2021-03-25 18:21:19 +00:00
testsuite_containers.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_counter_type.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_error.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_fs.h libstdc++: Restore <unistd.h> in testsuite_fs.h header [PR 99096] 2021-02-14 20:38:32 +00:00
testsuite_greedy_ops.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_hooks.cc Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_hooks.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_io.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_iterators.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_new_operators.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_performance.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_random.h libstdc++: Add PRNG fallback to std::random_device 2021-03-26 19:12:12 +00:00
testsuite_regex.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_rng.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_rvalref.h Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_shared.cc Update copyright years. 2021-01-04 10:26:59 +01:00
testsuite_tr1.h Update copyright years. 2021-01-04 10:26:59 +01:00