Segmentation fault when using eigen library in addon #93622

Open
opened 2021-12-04 01:44:50 +01:00 by Sergey Morozov · 13 comments

System Information
Operating system: Linux-5.15.5-arch1-1-x86_64-with-glibc2.33 64 Bits
Graphics card: NVIDIA GeForce GTX 1070/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 495.44

Blender Version
Broken: version: 3.0.0, branch: master, commit date: 2021-12-02 18:35, hash: f1cca30557
Worked: -

Short description of error
I'm currently working on an addon that integrates a physics engine, written in c++ that heavily depends on eigen library. This engine also uses swig to create python bindings.

The problem is that this library crashes blender with segmentation fault.

I tried to compile the engine with the eigen version from blender repo, but with no luck.

I can run the same python code without blender with no issues.

I assembled the minimal example that can show the same issue.

I also supplied crash.txt from the original addon; blender.crash.txt from the minimal example and console output from minimal example

Exact steps for others to reproduce the error
Extract cpp_test.zip, it should have the following directory structure

  • cpp_test
    • cpp
      • build.sh
      • example.cpp
      • example.i
    • init.py

Edit paths to includes in build.sh

run build.sh from cpp directory(you should have swig, eigen and python headers installed in your system for this to work)

Install cpp_test as an addon and try to enable it.

It will crash blender with segmentation fault

cpp_test.zip

blender.crash.txt

blender_output.txt

original.crash.txt

**System Information** Operating system: Linux-5.15.5-arch1-1-x86_64-with-glibc2.33 64 Bits Graphics card: NVIDIA GeForce GTX 1070/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 495.44 **Blender Version** Broken: version: 3.0.0, branch: master, commit date: 2021-12-02 18:35, hash: `f1cca30557` Worked: - **Short description of error** I'm currently working on an addon that integrates a physics engine, written in c++ that heavily depends on eigen library. This engine also uses swig to create python bindings. The problem is that this library crashes blender with segmentation fault. I tried to compile the engine with the eigen version from blender repo, but with no luck. I can run the same python code without blender with no issues. I assembled the minimal example that can show the same issue. I also supplied crash.txt from the original addon; blender.crash.txt from the minimal example and console output from minimal example **Exact steps for others to reproduce the error** Extract cpp_test.zip, it should have the following directory structure - cpp_test - cpp - build.sh - example.cpp - example.i - __init__.py Edit paths to includes in build.sh run build.sh from cpp directory(you should have swig, eigen and python headers installed in your system for this to work) Install cpp_test as an addon and try to enable it. It will crash blender with segmentation fault [cpp_test.zip](https://archive.blender.org/developer/F12687981/cpp_test.zip) [blender.crash.txt](https://archive.blender.org/developer/F12687983/blender.crash.txt) [blender_output.txt](https://archive.blender.org/developer/F12687984/blender_output.txt) [original.crash.txt](https://archive.blender.org/developer/F12687993/original.crash.txt)
Author

Added subscriber: @Sergey-Morozov

Added subscriber: @Sergey-Morozov
Author

It also seems that I'm not the first one to experience this problem. I found this thread on stackoverflow with similar issue. https://stackoverflow.com/questions/62470601/c-python-module-crashes-in-blender-but-not-in-python-console

It also seems that I'm not the first one to experience this problem. I found this thread on stackoverflow with similar issue. https://stackoverflow.com/questions/62470601/c-python-module-crashes-in-blender-but-not-in-python-console
Author

I found a workaround, we can just run the target function in subprocess, using 'spawn' start method.

ctx = multiprocessing.get_context('spawn')
p = ctx.Process(target=some_func, args=(arg1, arg2))
p.start()
p.join()

Unfortunately, this way the function won't have access to bpy module and we need to think about inter-process communication, but at least it works.

(The original post problem still should be considered a bug, and this workaround is just for anyone who will stumble upon this issue later)

I found a workaround, we can just run the target function in subprocess, using 'spawn' start method. ``` ctx = multiprocessing.get_context('spawn') p = ctx.Process(target=some_func, args=(arg1, arg2)) p.start() p.join() ``` Unfortunately, this way the function won't have access to `bpy` module and we need to think about inter-process communication, but at least it works. (The original post problem still should be considered a bug, and this workaround is just for anyone who will stumble upon this issue later)
Author

Found another project with similar issue(and the same workaround works for it)
https://github.com/taichi-dev/taichi/issues/1990
https://github.com/taichi-dev/taichi_blend/issues/1

Found another project with similar issue(and the same workaround works for it) https://github.com/taichi-dev/taichi/issues/1990 https://github.com/taichi-dev/taichi_blend/issues/1
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

Changed status from 'Needs Triage' to: 'Needs User Info'

Changed status from 'Needs Triage' to: 'Needs User Info'
Member

This is our guidleline on cases like these:

Crashes or errors involving your own C/C++ code (Python C/API for example) will only be accepted as bugs if you are able to identify the error in Blender's code. Finding that your code isn't working as expected in Blender isn't sufficient to consider it a Blender bug as there may be other reasons that aren't necessarily caused by errors in Blender's code.

So if you can identify the error in Blender's code that would be good.

This is our guidleline on cases like these: > Crashes or errors involving your own C/C++ code (Python C/API for example) will only be accepted as bugs if you are able to identify the error in Blender's code. Finding that your code isn't working as expected in Blender isn't sufficient to consider it a Blender bug as there may be other reasons that aren't necessarily caused by errors in Blender's code. So if you can identify the error in Blender's code that would be good.
Member

Poke ^^
(otherwise will be closed next week I am afraid)

Poke ^^ (otherwise will be closed next week I am afraid)
Author

Sorry for late answer, I was rather busy lately.

I'm not very good at c++ development, but after some debugging on master branch it seems like I found a problem.

At some point eigen library which is compiled into an _example.so tries to call IOFormat() constructor, but instead of calling this method from an _example.so it finds it in blender executable which causes memory corruption and segmentation fault when accessing a rowSpacer member of a resulting object.

Here is the simplified version of an example.cpp which causes the issue.

- include <Eigen/Dense>
- include <iostream>
using namespace Eigen;
using namespace std;
void matrix_test(int n) {
   cout << IOFormat().rowSpacer;
}

If I understand correctly the issue is that by default g++(and other compilers) make most symbols weak so they can be overridden by other symbols. This can be solved by just compiling the shared library with -fvisibility=hidden flag like this
g++ -g -ggdb -fPIC example.cpp example_wrap.c -I /usr/include/python3.10/ -I /usr/include/eigen3/ -shared -fvisibility=hidden -o _example.so
Then the symbols in the resulting library will be created as text.

I tried this on blender 3.0.1 and 3.2.0 Alpha(master branch) with this example and taichi library and it looks like it's working.

I don't know if this is a proper solution, but It still might be considered a blender related bug, because it might not be always possible to recompile shared libraries(for example, when integrating something proprietary).

Sorry for late answer, I was rather busy lately. I'm not very good at c++ development, but after some debugging on master branch it seems like I found a problem. At some point eigen library which is compiled into an `_example.so` tries to call `IOFormat()` constructor, but instead of calling this method from an `_example.so` it finds it in blender executable which causes memory corruption and segmentation fault when accessing a `rowSpacer` member of a resulting object. Here is the simplified version of an `example.cpp` which causes the issue. ``` - include <Eigen/Dense> - include <iostream> using namespace Eigen; using namespace std; void matrix_test(int n) { cout << IOFormat().rowSpacer; } ``` If I understand correctly the issue is that by default g++(and other compilers) make most symbols weak so they can be overridden by other symbols. This can be solved by just compiling the shared library with `-fvisibility=hidden` flag like this `g++ -g -ggdb -fPIC example.cpp example_wrap.c -I /usr/include/python3.10/ -I /usr/include/eigen3/ -shared -fvisibility=hidden -o _example.so` Then the symbols in the resulting library will be created as text. I tried this on blender 3.0.1 and 3.2.0 Alpha(master branch) with this example and taichi library and it looks like it's working. I don't know if this is a proper solution, but It still might be considered a blender related bug, because it might not be always possible to recompile shared libraries(for example, when integrating something proprietary).
Member

Changed status from 'Needs User Info' to: 'Needs Triage'

Changed status from 'Needs User Info' to: 'Needs Triage'
Member

Added subscriber: @OmarEmaraDev

Added subscriber: @OmarEmaraDev
Member

Changed status from 'Needs Triage' to: 'Needs Developer To Reproduce'

Changed status from 'Needs Triage' to: 'Needs Developer To Reproduce'
Member

Not sure if there is something Blender can do here, I can't find similar reports, so this needs a look from a developer.

Not sure if there is something Blender can do here, I can't find similar reports, so this needs a look from a developer.
Philipp Oeser removed the
Interest
Python API
label 2023-02-10 09:04:29 +01:00
Bart van der Braak added
Type
Bug
and removed
Type
Report
labels 2024-08-14 13:22:39 +02: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
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#93622
No description provided.