2 out of 6 bytes produced by BLI_rng_get_char_n() are predictable #91395
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#91395
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
System Information
Operating system: Kubuntu Linux 20.04 LTS
Blender Version
Broken: current master @
9fe6854a93
Worked: didn't check with older versions
Short description of error
BLI_rng_get_char_n()
seems to produce predictable bytes, regardless of the seed used.Exact steps for others to reproduce the error
Run this program a few times, and take note of the fact that the seed changes on every run, but the magenta columns stay the same between runs.
P2390: (An Untitled Masterwork)
I compiled this with
g++ -Wall -std=c++17 -o rng_test rng_test.cc -I ./source/blender/blenlib/ ../build_linux/lib/{libbf_blenlib.a,libbf_intern_guardedalloc.a}
, but I've seen the same behaviour using it to generate random bytes for generating UUIDs within Blender, so this shouldn't be caused by my simplified compilation options.This is a screenshot of the output I get on my machine (screenshotted so that the highlighted colums are maintained)
Proposed Solution
Fixing this issue would break a lot of things in existing files (e.g. layout of particles or hair, or patterns of procedural textures, etc.). Since there is no versioning possible here, this issue could be fixed in two steps:
_deprecated
prefix, but keep using it everywhere it is currently used in code.Added subscriber: @dr.sybren
Added subscriber: @JacquesLucke
4 out of 6 bytes produced by BLI_rng_get_char_n() are predictableto 2 out of 6 bytes produced by BLI_rng_get_char_n() are predictableAdded subscriber: @dfelinto
Changed status from 'Needs Triage' to: 'Confirmed'
Added subscriber: @LazyDodo
The seed function expands a 32 bit value into a 64 bit one with the following code:
0x0000725f1ea2330e
0xXXXX........XXXX
void RandomNumberGenerator::seed_random(uint32_t seed)
{
}```
this does a great job at randomizing bits 16..48 but "fixed" bits will still have their fixed values, which are reflected in the output of the RNG
Given there's many things relying on the behavior of this RNG it's not really recommended to change the seed function, however something like
{
constexpr uint64_t lowseed = 0x330E;
x_ = (static_cast<uint64_t>(seed) << 16) | lowseed;
x_ = x_ ^ static_cast<uint64_t>(seed);
}
would do the trick, The upper 16 bits being zero is not an issue since they are being discarded anyhow.
Unrelated, your initial seed selection based on time has some issues on windows where
sizeof(long) == 4
even on 64 bitshifting a 32 bit variable by 32 bits has some fun undefined behavior (5.8 from the C++ spec) where the result (not in spec, but all compilers do this) is the original value rather than 0 , leading to
ts.tv_nsec^ts.tv_nsec
which is.... 0 :)Added subscriber: @Raimund58