Sanitize size handling in write code #79561

Closed
opened 2020-08-05 18:01:06 +02:00 by Bastien Montagne · 5 comments

Current handling of size (in bytes) in writer code is rather messy, to say the least: different part of the code uses int, uint and size_t. BHead itself stores size in int...

While int should be enough in most cases (it allows chunks of 2GB at most), we are now hitting some rare issues, see e.g. #78529 (Blend file corrupted during save caused by high Cubemap Size).

I think we should at the very least use size_t everywhere in functions, and assert/try to handle the issue when actual size exceeds BHead's int capacity?

Ultimately it might be nice to allow bigger chunks (using int64_t in BHead)? But I am not sure how we could handle that in a compatible way?

Current handling of size (in bytes) in writer code is rather messy, to say the least: different part of the code uses `int`, `uint` and `size_t`. `BHead` itself stores size in `int`... While `int` should be enough in most cases (it allows chunks of 2GB at most), we are now hitting some rare issues, see e.g. #78529 (Blend file corrupted during save caused by high Cubemap Size). I think we should at the very least use `size_t` everywhere in functions, and assert/try to handle the issue when actual size exceeds `BHead`'s `int` capacity? Ultimately it might be nice to allow bigger chunks (using `int64_t` in `BHead`)? But I am not sure how we could handle that in a compatible way?
Author
Owner

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Author
Owner

Added subscribers: @mont29, @brecht, @dfelinto, @ideasman42

Added subscribers: @mont29, @brecht, @dfelinto, @ideasman42

Looked into this and there doesn't seem to be any elegant options seeing as the BHead.len is always used as the final length irrespective of the kind of data written.

Increase limit to 4GB

Firstly, we could make BHead.len unsigned, as long as the new files don't use large allocations, they will load in older Blender versions.

In the case they do, older Blender versions will see this as len < 0 and stop loading the file.

Mentioning this as as far as I can see, all the alternatives are quite involved.


Encode Large Data in the Existing Format

There are some other tricks which could work but will most likely make the code messy/unmaintainable, although if they could be done in a manageable way, we could use them in the case of over 2GB chunks being written.
This would need to be done in way that older Blender versions could read back, skipping the large allocations *. If that can't be properly supported we might as well just make all the BHead variables 64bit and break forwards compatibility for older Blender versions.

We could for example add a new DATA code, only use for >2GB blocks, older Blender versions will skip it, new versions can read/write chunks multiple BHead's that don't exceed INT_MAX.

I considered using some kind of tag to show the BHead should be treated differently, in a way that causes it to be ignored, while there are a few options, BHead.nr can be used for this as it's ignored on read at the moment, so this could hint at different behavior.

However we still need to skip the ignored data for older Blender versions, so unless we do something really strange (writing data after the ENDB chunk for example), I think it's not such a good option.


Other Limits

  • GZip read/write uses int args for size, this would need to handle >INT_MAX operations in multiple chunks.
  • Packed files currently stores size as int's.
  • RNA doesn't currently support numbers larger than int (to access packed file size for example).

Conclusions:

I'm not convinced it's practical to support >2GB BHead support without breaking forward compatibility.

Short term we could make BHead.len unsigned, then update the BHead for a major release (3.0 for e.g) to support 64bit BHead's, with the ability to optionally save blend files for older versions.


* skipping the large allocations might not be stable and cause crashes on load, older Blender versions would need to allow for these struct-members to be NULL.

Looked into this and there doesn't seem to be any elegant options seeing as the `BHead.len` is always used as the final length irrespective of the kind of data written. **Increase limit to 4GB** Firstly, we could make `BHead.len` unsigned, as long as the new files don't use large allocations, they will load in older Blender versions. In the case they do, older Blender versions will see this as `len < 0` and stop loading the file. Mentioning this as as far as I can see, all the alternatives are quite involved. ---- **Encode Large Data in the Existing Format** There are some other tricks which could work but will most likely make the code messy/unmaintainable, although if they could be done in a manageable way, we could use them in the case of over 2GB chunks being written. This would need to be done in way that older Blender versions could read back, skipping the large allocations *. If that can't be properly supported we might as well just make all the `BHead` variables 64bit and break forwards compatibility for older Blender versions. We could for example add a new `DATA` code, only use for >2GB blocks, older Blender versions will skip it, new versions can read/write chunks multiple BHead's that don't exceed `INT_MAX`. I considered using some kind of tag to show the `BHead` should be treated differently, in a way that causes it to be ignored, while there are a few options, `BHead.nr` can be used for this as it's ignored on read at the moment, so this could hint at different behavior. However we still need to skip the ignored data for older Blender versions, so unless we do something really strange (writing data after the `ENDB` chunk for example), I think it's not such a good option. ---- **Other Limits** - GZip read/write uses int args for size, this would need to handle >INT_MAX operations in multiple chunks. - Packed files currently stores size as int's. - RNA doesn't currently support numbers larger than int (to access packed file size for example). ---- **Conclusions:** I'm not convinced it's practical to support >2GB `BHead` support without breaking forward compatibility. Short term we could make `BHead.len` unsigned, then update the `BHead` for a major release (3.0 for e.g) to support 64bit BHead's, with the ability to optionally save blend files for older versions. ---- `*` skipping the large allocations might not be stable and cause crashes on load, older Blender versions would need to allow for these struct-members to be NULL.
Author
Owner

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Bastien Montagne self-assigned this 2020-09-20 21:28:40 +02:00
Author
Owner

Sanitized code a bit in 5ea1049e75, think we'll have to go with that for now.

Sanitized code a bit in 5ea1049e75, think we'll have to go with that for now.
Thomas Dinges added this to the 2.91 milestone 2023-02-08 16:21:41 +01:00
Sign in to join this conversation.
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
2 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#79561
No description provided.