Page MenuHome

Fix T94760: Crashe building BMesh when opening file
ClosedPublic

Authored by Hans Goudey (HooglyBoogly) on Jan 21 2022, 10:46 PM.

Details

Summary

The BMesh code relies heavily on alloca which can cause problems when the required space exceeds the reserved space.

This caused the file on T94760 to crash when a polygon with a huge number of sides enters edit mode.

As stated by @Jesse Yurkovich (deadpin) in T94760#1292932:

It's crashing on a call to BLI_array_alloca which allocates memory on the stack. An incoming mesh has mp->totloop of 98658 which causes 2 significant stack allocations of size 789264 bytes each to occur. So you are probably running out of stack space (as alluded to in the ___chkstk_darwin above).

One solution would be to always use malloc in bm_face_create_from_mpoly. But to avoid penalties in performanse, a stack for polygons of up to 64 sides is reserved.

Diff Detail

Repository
rB Blender

Event Timeline

Germano Cavalcante (mano-wii) requested review of this revision.Jan 21 2022, 10:46 PM
Germano Cavalcante (mano-wii) created this revision.
Germano Cavalcante (mano-wii) planned changes to this revision.Jan 21 2022, 11:53 PM

Error in multiplying the ARRAY_SIZE by 2. The right thing was to divide it by 2.

In principle this is fine, could this use: BLI_array_staticdeclare though? It handles the logic to allocate a a little more cleanly.

Campbell Barton (campbellbarton) requested changes to this revision.Jan 22 2022, 4:19 AM

Mentioned the possibility of using Array to @Germano Cavalcante (mano-wii) in chat. Without arc I can't update the diff without commandeering it.

Hans Goudey (HooglyBoogly) retitled this revision from Fix T94760: Crashes when opening files to Fix T94760: Crashe building BMesh when opening file.
Hans Goudey (HooglyBoogly) edited the summary of this revision. (Show Details)

Use blender::Array with an inline buffer instead

Campbell Barton (campbellbarton) added inline comments.
source/blender/bmesh/intern/bmesh_mesh_convert.cc
184–186

Use BM_DEFAULT_NGON_STACK_SIZE, otherwise this seems fine.

This revision is now accepted and ready to land.Jan 22 2022, 11:13 AM