MEM: support overaligned types in MEM_cnew_array #120632

Merged
Jacques Lucke merged 11 commits from JacquesLucke/blender:cnew-array-alignment into main 2024-04-15 19:11:17 +02:00
Member

This is similar to the recent change for MEM_cnew: 60a3d85b88.
A new function called MEM_calloc_arrayN_aligned is added that is used by both cnew functions now.

This is similar to the recent change for `MEM_cnew`: 60a3d85b888c70330b7f9e691d8e82d66561d0f1. A new function called `MEM_calloc_arrayN_aligned` is added that is used by both `cnew` functions now.
Jacques Lucke added 3 commits 2024-04-14 12:25:49 +02:00
fix
All checks were successful
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
f013722ced
Author
Member

@blender-bot build

@blender-bot build
Jacques Lucke requested review from Sergey Sharybin 2024-04-14 12:26:13 +02:00
Sergey Sharybin requested changes 2024-04-15 11:56:56 +02:00
Dismissed
@ -305,0 +311,4 @@
if (alignof(T) <= MEM_MIN_CPP_ALIGNMENT) {
return static_cast<T *>(MEM_calloc_arrayN(length, sizeof(T), allocation_name));
}
const size_t bytes_num = length * sizeof(T);

The allocator needs to be consistently robust against the integer overflow on allocation vector of attack, so it needs to be using MEM_size_safe_multiply().

The allocator needs to be consistently robust against the integer overflow on allocation vector of attack, so it needs to be using `MEM_size_safe_multiply()`.
JacquesLucke marked this conversation as resolved
Jacques Lucke added 3 commits 2024-04-15 13:24:40 +02:00
add MEM_calloc_arrayN_aligned
All checks were successful
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
e540b4bf63
Author
Member

Using MEM_size_safe_multiply was a bit more annoying than expected because it was a private inline function and couldn't easily be included in the public header (also because it uses MEM_INLINE and UNLIKELY which are private to the allocator). Instead I added a new MEM_calloc_arrayN_aligned which is implemented in mallocn.cc which is then called by the C++ functions.

Using `MEM_size_safe_multiply` was a bit more annoying than expected because it was a private inline function and couldn't easily be included in the public header (also because it uses `MEM_INLINE` and `UNLIKELY` which are private to the allocator). Instead I added a new `MEM_calloc_arrayN_aligned` which is implemented in `mallocn.cc` which is then called by the C++ functions.
Author
Member

@blender-bot build

@blender-bot build
Sergey Sharybin reviewed 2024-04-15 15:41:23 +02:00
Sergey Sharybin left a comment
Owner

I think having MEM_calloc_arrayN_aligned is useful regardless, so it is probably a better way of achieving the proper multiplication check. There are couple of inlined comments.

I think having `MEM_calloc_arrayN_aligned` is useful regardless, so it is probably a better way of achieving the proper multiplication check. There are couple of inlined comments.
@ -303,2 +313,4 @@
}
/**
* Same as MEM_cnew but for arrays, better alternative to #MEM_calloc_arrayN.

The function can now be moved back below MEM_cnew(), so that the comment does not refer to a function which is not yet "known" ? Also makes diff smaller?

The function can now be moved back below `MEM_cnew()`, so that the comment does not refer to a function which is not yet "known" ? Also makes diff smaller?
JacquesLucke marked this conversation as resolved
@ -104,0 +109,4 @@
{
size_t bytes_num;
if (UNLIKELY(!MEM_size_safe_multiply(len, size, &bytes_num))) {
abort();

The other functions do error print like

    print_error(
        "Calloc array aborted due to integer overflow: "
        "len=" SIZET_FORMAT "x" SIZET_FORMAT " in %s, total " SIZET_FORMAT "\n",
        SIZET_ARG(len),
        SIZET_ARG(size),
        str,
        memory_usage_current());

which could be useful for troubleshooting.

The other functions do error print like ``` print_error( "Calloc array aborted due to integer overflow: " "len=" SIZET_FORMAT "x" SIZET_FORMAT " in %s, total " SIZET_FORMAT "\n", SIZET_ARG(len), SIZET_ARG(size), str, memory_usage_current()); ``` which could be useful for troubleshooting.
Author
Member

Looks like that means that I'll have to duplicate the implementation of the MEM_calloc_arrayN_aligned function because print_error is a separate function for the lockfree and guarded allocator :(

Looks like that means that I'll have to duplicate the implementation of the `MEM_calloc_arrayN_aligned` function because `print_error` is a separate function for the lockfree and guarded allocator :(

What a fun experience :(
Maybe fprintf(stderr, "Calloc array aborted due to integer overflow\n"); will do it for now ? :)

What a fun experience :( Maybe `fprintf(stderr, "Calloc array aborted due to integer overflow\n");` will do it for now ? :)
Author
Member

I just split it up into a lockfree and guarded implementation.

I just split it up into a lockfree and guarded implementation.
JacquesLucke marked this conversation as resolved
@ -104,0 +110,4 @@
size_t bytes_num;
if (UNLIKELY(!MEM_size_safe_multiply(len, size, &bytes_num))) {
abort();
return NULL;

return nullptr;

`return nullptr;`
JacquesLucke marked this conversation as resolved
Jacques Lucke added 3 commits 2024-04-15 17:12:20 +02:00
Jacques Lucke added 2 commits 2024-04-15 17:33:47 +02:00
remove include
All checks were successful
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
e4298eea29
Author
Member

@blender-bot build

@blender-bot build
Sergey Sharybin approved these changes 2024-04-15 17:53:08 +02:00
Sergey Sharybin left a comment
Owner

On a code side seems fine. Could be good to give Windows another retrial. The Internal Compiler Error is unlikely to be caused by this specific fix =\

On a code side seems fine. Could be good to give Windows another retrial. The Internal Compiler Error is unlikely to be caused by this specific fix =\
Author
Member

@blender-bot build windows

@blender-bot build windows
Jacques Lucke merged commit 00648411ea into main 2024-04-15 19:11:17 +02:00
Jacques Lucke deleted branch cnew-array-alignment 2024-04-15 19:11:20 +02:00
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
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, 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
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#120632
No description provided.