OSL Memory Corruption (Use after free) #47008
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
3 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#47008
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
Windows x64
Blender Version
Broken: Latest from git
Worked: (optional)
Short description of error
Tricky one but I'll try to give a short tour.
in Session::~Session() we have this piece of code
Now here's the setup:
Exact steps for others to reproduce the error
[With visual studio]
Tracking it down is a little harder, you have to enable the PageHeap for the process using gflags, once enabled, setup vs.net to catch exception c0000005 (access violation) (something is eating is so it doesn't break by default) and you should hit the exception in blender-app.exe!OSL::pvt::PeakCounter::operator+=(int sz)
suggested fix:
But given i'm unfamiliar with this piece of code i'm unsure if this will create other problems. (doesn't seem like it, but a sober second look never hurts)
Changed status to: 'Open'
Added subscriber: @LazyDodo
Added subscriber: @Sergey
Session is expected to be destroyed only when all the rendering is done, it should not really be destroyed prior to that. This could be just missing de-initialization happening somewhere else in the code, but it's hard to tell without being able to reproduce the issue (don't see anything running blender with )
What are the exact steps reproducing the issue? Does it happen with any scene?
no problem there, the session is destroyed at an appropriate moment in time, when all rendering is completed.
The repro steps i posted should be detailed enough to reproduce the issue on windows. Step 2 should have been 2) Render any scene with osl enabled . I noticed my mistake 2 minutes seconds after i posted but it wouldn't allow me to change it.
so more detailed recap for msvc
If you're not on windows it'll have to go a little bit more on faith but i think this should prove the problem exists:
Ok, see what's happening now. Even tho we're doing all proper ownership management OSL is being lazy and uses some per-thread variables. This is happening, for example, when performing shader group optimization.
Those variables will only be freed when thread is actually exiting and there seems to be no way to force clear the storage neither from the per-thread-variable wrapper (meaning, ShadingSystemImpl can't ensure there's no handling per-thread-variables from it's destructor) and there's also no way to force such storage to clear without actually destroying the thread.
Now, your patch will work around the problem in the case when we've got single render at a time, but it'll totally fail when doing several viewport renderings or when mixing viewport and final render.
This needs some further investigation, maybe even extending OSL/boost API?
One idea which might work (and in fact it seems to work on Linux, judging from debug prints and clang's asan there's no heisenbugs here) is to perform jit optimization ahead of a time using
ShadingSystem->optimize_all_groups()
with couple of therads used. This will make it so OSL creates threads pool and optimzation will use it's TLS. Once optimization is done, all TLS used by optimization will be freed. After that render threads will not create ShadingContext in TLS anymore.While it's working as expected on Linux, it's still failing on Windows. Could be so OSL is still storing something in TLS for the case of Windows platform. What's also weird, disablign
greedyjit
does not solve memory access issues on WIndows, so it's probably something else is happening with threads.Ok, had a mistake in original greedyjut change i was talking about yesterday. This patch should solve the bad memory access:
P304: Fix #47008
It's not totally optimal from the performance point of view, but unless i'm wrong it'll only be some constant delay before the rendering actually starts.
This issue was referenced by
a3df65dea8
Changed status from 'Open' to: 'Resolved'
This issue was referenced by blender/cycles@626e479945