Fix #126108: Crash when EXR image is loaded in image list #126234

Merged
Sergey Sharybin merged 1 commits from Sergey/blender:fix_126108 into main 2024-08-14 11:34:47 +02:00

Technically the regression was caused by #124472 which made it so
duplicating ImBuf allocates the exact amount of memory needed to
hold the pixels, while before IMB_dupImBuf() would leave the float
buffer over-allocated for images that are less than 4 channels per
pixel.

At the same time IMB_scalefastImBuf() was hard-coded to use 4
channels per pixels, for both byte and float buffers. It did not
crash in Blender 4.1 as it was accessing memory that is over-allocated,
but it also did not generate proper preview.

This fix makes the IMB_scalefastImBuf() to operate on an arbitrary
number of channels in the float buffer.

Technically the regression was caused by #124472 which made it so duplicating ImBuf allocates the exact amount of memory needed to hold the pixels, while before IMB_dupImBuf() would leave the float buffer over-allocated for images that are less than 4 channels per pixel. At the same time IMB_scalefastImBuf() was hard-coded to use 4 channels per pixels, for both byte and float buffers. It did not crash in Blender 4.1 as it was accessing memory that is over-allocated, but it also did not generate proper preview. This fix makes the IMB_scalefastImBuf() to operate on an arbitrary number of channels in the float buffer.
Sergey Sharybin added 1 commit 2024-08-12 16:21:59 +02:00
Fix #126108: Crash when EXR image is loaded in image list
Some checks failed
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
86f12008c4
Technically the regression was caused by #124472 which made it so
duplicating ImBuf allocates the exact amount of memory needed to
hold the pixels, while before IMB_dupImBuf() would leave the float
buffer over-allocated for images that are less than 4 channels per
pixel.

At the same time IMB_scalefastImBuf() was hard-coded to use 4
channels per pixels, for both byte and float buffers. It did not
crash in Blender 4.1 as it was accessing memory that is over-allocated,
but it also did not generate proper preview.

This fix makes the IMB_scalefastImBuf() to operate on an arbitrary
number of channels in the float buffer.
Author
Owner

@blender-bot build

@blender-bot build
Sergey Sharybin requested review from Jesse Yurkovich 2024-08-12 16:22:36 +02:00

This fixes the crash for me as well (and build bot failures are not your fault). That said, there's 2 minor issues I have for this code in general:

  • I don't fully understand the use of the 65536 magic number (and related right shifts by 16). Can this be made less obtuse?
  • For any images with an aspect ratio > 32:1, it will technically generate a div-by-0 for the stepx and stepy calculations. Though things happen to still work...

Not sure how far you want to go in this PR to address the initial crash though. Maybe a followup is best.

This fixes the crash for me as well (and build bot failures are not your fault). That said, there's 2 minor issues I have for this code in general: - I don't fully understand the use of the 65536 magic number (and related right shifts by 16). Can this be made less obtuse? - For any images with an aspect ratio > 32:1, it will technically generate a div-by-0 for the `stepx` and `stepy` calculations. Though things happen to still work... Not sure how far you want to go in this PR to address the initial crash though. Maybe a followup is best.
Author
Owner

@deadpin That is a great points you're raising. The magic numbers and such is indeed unclear, and we share the same thoughts. The devision by zero I didn't notice before, nice catch!

I'd like this to go to 4.2.1 scheduled for Thursday, so I wanted to avoid deeper changes, and have the change as safe as possible.
So from my perspective I think doing a followup on top of a fix works better. Unless you have stronger opinion.

The thing I am not sure about is whether doing a followup is something I can do in the near future. But maybe it would be something interesting for @aras_p to help with?

@deadpin That is a great points you're raising. The magic numbers and such is indeed unclear, and we share the same thoughts. The devision by zero I didn't notice before, nice catch! I'd like this to go to 4.2.1 scheduled for Thursday, so I wanted to avoid deeper changes, and have the change as safe as possible. So from my perspective I think doing a followup on top of a fix works better. Unless you have stronger opinion. The thing I am not sure about is whether doing a followup is something I can do in the near future. But maybe it would be something interesting for @aras_p to help with?

@Sergey maybe! Any bets on whether IMB_scalefastImBuf is actually fast? :P

@Sergey maybe! Any bets on whether `IMB_scalefastImBuf` is actually fast? :P
Jesse Yurkovich approved these changes 2024-08-13 18:24:42 +02:00
Jesse Yurkovich left a comment
Member

Approving since the primary bug is fixed. The cosmetic changes to make the code more clear can definitely come later.

Approving since the primary bug is fixed. The cosmetic changes to make the code more clear can definitely come later.
Author
Owner

Thanks for the review and inputs. I'll land the PR now and backport for 4.2.1.

Thanks for the review and inputs. I'll land the PR now and backport for 4.2.1.
Sergey Sharybin merged commit 1fc6a5b9bd into main 2024-08-14 11:34:47 +02:00
Sergey Sharybin deleted branch fix_126108 2024-08-14 11:34:51 +02:00

FYI the fix caused a slight performance regression in using IMB_scalefastImBuf on float images, due to more complex per-pixel work with the channels loop. I'm fixing it as part of #126390 rabbit hole (performance test I've added there on my machine was taking 230ms before this fix, 300ms with this fix, and back to ~220ms in #126390)

FYI the fix caused a slight performance regression in using `IMB_scalefastImBuf` on float images, due to more complex per-pixel work with the channels loop. I'm fixing it as part of #126390 rabbit hole (performance test I've added there on my machine was taking 230ms before this fix, 300ms with this fix, and back to ~220ms in #126390)
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
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
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
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
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
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
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
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
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#126234
No description provided.