Improve consistency of MEM_guardedalloc usage in C++ code #124512

Open
opened 2024-07-11 12:48:37 +02:00 by Bastien Montagne · 2 comments

The usage of our guarded allocator code with C++ data creation (new/delete) is confusing at best in current code.

  • MEM_new: Allocates required amount of memory (using guardedalloc code) and calls the default 'placement new' operation on it.
  • MEM_CXX_CLASS_ALLOC_FUNCS: This macro defines new/delete operators for a given C++ type, using the guardedalloc code. Most of its usages are conditioned by the CMake WITH_CXX_GUARDEDALLOC setting.
  • WITH_CXX_GUARDEDALLOC: This CMake setting also conditions the global override of new/delete operators to use guardedalloc code. It is disabled by default.

The fact that WITH_CXX_GUARDEDALLOC is disabled by default means that it is barely ever used. Often enabling it will reveal some memory management issues.

The global overriding of new/delete operators is most likely not worth the risk of inconsistencies (it makes it too easy to run into situations where some data allocated from a linked library with 'normal' new gets deleted by the 'guardedalloc' new e.g.)

Proposal

  • Remove WITH_CXX_GUARDEDALLOC: There should not be an option to disable guardedalloc in most common cases.
    • **Remove the global override of new/delete.
    • **Always use MEM_CXX_CLASS_ALLOC_FUNCS where it is currently used.

Question

Do we want to keep the two current systems (MEM_new/MEM_delete and MEM_CXX_CLASS_ALLOC_FUNCS)?

MEM_new/MEM_delete has the advantage of being 'universal', and allows to control where usage of guardedalloc is desired, and where standard new allocation can be used instead.

MEM_CXX_CLASS_ALLOC_FUNCS allows to use standard new/delete C++ code for Blender C++ types, while automatically using the guardedallocator.

The usage of our guarded allocator code with C++ data creation (`new`/`delete`) is confusing at best in current code. * `MEM_new`: Allocates required amount of memory (using guardedalloc code) and calls the default 'placement new' operation on it. * `MEM_CXX_CLASS_ALLOC_FUNCS`: This macro defines new/delete operators for a given C++ type, using the guardedalloc code. Most of its usages are conditioned by the CMake `WITH_CXX_GUARDEDALLOC` setting. * `WITH_CXX_GUARDEDALLOC`: This CMake setting also conditions the global override of new/delete operators to use guardedalloc code. It is disabled by default. The fact that `WITH_CXX_GUARDEDALLOC` is disabled by default means that it is barely ever used. Often enabling it will reveal some memory management issues. The global overriding of `new`/`delete` operators is most likely not worth the risk of inconsistencies (it makes it too easy to run into situations where some data allocated from a linked library with 'normal' new gets deleted by the 'guardedalloc' new e.g.) ## Proposal * **Remove `WITH_CXX_GUARDEDALLOC`**: There should not be an option to disable guardedalloc in most common cases. * **Remove the global override of `new`/`delete`. * **Always use `MEM_CXX_CLASS_ALLOC_FUNCS` where it is currently used. ## Question Do we want to keep the two current systems (`MEM_new`/`MEM_delete` and `MEM_CXX_CLASS_ALLOC_FUNCS`)? `MEM_new`/`MEM_delete` has the advantage of being 'universal', and allows to control where usage of guardedalloc is desired, and where standard `new` allocation can be used instead. `MEM_CXX_CLASS_ALLOC_FUNCS` allows to use standard new/delete C++ code for Blender C++ types, while automatically using the guardedallocator.
Bastien Montagne added the
Type
Design
label 2024-07-11 12:48:37 +02:00
Bastien Montagne added this to the Module: Core project 2024-07-11 12:48:38 +02:00
Member

Do we want to keep the two current systems (MEM_new/MEM_delete and MEM_CXX_CLASS_ALLOC_FUNCS)?

I'm not fully sure yet. It's nice that with MEM_CXX_CLASS_ALLOC_FUNCS one can just use normal std::make_unique etc. Could make our own version of that though, but that doesn't seem nice. I also still have to check how overriding operator new works with child classes.

Just wanted to mention some downsides of MEM_CXX_CLASS_ALLOC_FUNCS:

  • Currently, that always has to use aligned allocation internally, because our allocator is usually 8 byte, but not 16 byte aligned which new seems to expect in some cases (see __STDCPP_DEFAULT_NEW_ALIGNMENT__).
  • It's not possible to give a custom allocation name at the place of allocation. When trying to find memory leaks, I often find it useful when the allocation name contains information about where the memory was allocated and not just what type it is.
> Do we want to keep the two current systems (MEM_new/MEM_delete and MEM_CXX_CLASS_ALLOC_FUNCS)? I'm not fully sure yet. It's nice that with `MEM_CXX_CLASS_ALLOC_FUNCS` one can just use normal `std::make_unique` etc. Could make our own version of that though, but that doesn't seem nice. I also still have to check how overriding `operator new` works with child classes. Just wanted to mention some downsides of `MEM_CXX_CLASS_ALLOC_FUNCS`: * Currently, that always has to use aligned allocation internally, because our allocator is usually 8 byte, but not 16 byte aligned which `new` seems to expect in some cases (see `__STDCPP_DEFAULT_NEW_ALIGNMENT__`). * It's not possible to give a custom allocation name at the place of allocation. When trying to find memory leaks, I often find it useful when the allocation name contains information about where the memory was allocated and not just what type it is.
Author
Owner

TBH, since we entered the ASAN era, I very rarely use/need our guardedallocation name info anymore?

TBH, since we entered the ASAN era, I very rarely use/need our guardedallocation name info anymore?
Bastien Montagne changed title from WIP: Improve consistency of MEM_guardedalloc usage in C++ code to Improve consistency of MEM_guardedalloc usage in C++ code 2024-11-13 13:28:53 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
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 & 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
Asset System
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline & 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
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#124512
No description provided.