Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault importing pytrellis.so on Apple M1 #185

Open
ccattuto opened this issue Mar 9, 2022 · 5 comments
Open

Segfault importing pytrellis.so on Apple M1 #185

ccattuto opened this issue Mar 9, 2022 · 5 comments

Comments

@ccattuto
Copy link

ccattuto commented Mar 9, 2022

Apple Macbook Pro M1
OSX Monterey 12.2.1
Native arm64 Python 3.9.10 via Miniforge
Xcode 13.2.1
Apple clang version 13.0.0 (clang-1300.0.29.30)
boost 1.78.0_1 (Homebrew)

The library and executables build cleanly. No warnings of notice except for a few deprecations:

/opt/homebrew/include/boost/format/group.hpp:59:12: warning: definition of implicit copy constructor for 'group1<boost::io::detail::group0>' is deprecated because it has a user-declared copy assignment operator [-Wdeprecated-copy]
   group1& operator=(const group1&);

pytrellis.so is very big (~8 Mb). On importing it from Python, it segfaults:

Python 3.9.10 | packaged by conda-forge | (main, Feb  1 2022, 21:27:43) 
[Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytrellis.so
zsh: segmentation fault  python

Digging a bit deeper shows that this happens when calling take_gil() when importing pytrellis.so.

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
  * frame #0: 0x000000010489cad8 libpython3.9.dylib`take_gil + 84
    frame #1: 0x00000001048ffce0 libpython3.9.dylib`PyGILState_Ensure + 56
    frame #2: 0x0000000103ff57ec pytrellis.so`pybind11::detail::get_internals() + 64
    frame #3: 0x0000000103ff567c pytrellis.so`PyInit_pytrellis + 120
@Tigralt
Copy link

Tigralt commented Mar 10, 2022

Same issue here.

OSX Monterey 12.2.1
Native arm64
Python 3.10.2 (installed via Homebrew)
Boost 1.78.0
AppleClang 13.0.0.13000029

@ccattuto
Copy link
Author

ccattuto commented Mar 10, 2022

There's also an issue with dynamic linking of the executables:

% ./ecppack 
dyld[87459]: Library not loaded: @rpath/libtrellis.dylib
  Referenced from: /Users/ciro/prjtrellis/libtrellis/ecppack
  Reason: tried: '/Users/ciro/prjtrellis/libtrellis/../lib/trellis/libtrellis.dylib' (no such file), '/Users/ciro/prjtrellis/libtrellis/../lib/trellis/libtrellis.dylib' (no such file), '/usr/local/lib/libtrellis.dylib' (no such file), '/usr/lib/libtrellis.dylib' (no such file)
zsh: abort      ./ecppack

Using otool -L I get:

ecppack:
	@rpath/libtrellis.dylib (compatibility version 0.0.0, current version 0.0.0)
	/opt/homebrew/opt/boost/lib/libboost_filesystem-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
	/opt/homebrew/opt/boost/lib/libboost_program_options-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
	/opt/homebrew/opt/boost/lib/libboost_system-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
	/opt/homebrew/opt/boost/lib/libboost_thread-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
	/opt/homebrew/opt/boost/lib/libboost_chrono-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
	/opt/homebrew/opt/boost/lib/libboost_atomic-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libpython3.9.dylib (compatibility version 3.9.0, current version 3.9.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1200.3.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.0.0)

Hence this can be fixed using install_name_tool to specify the path of libpython3.9.dylib:

install_name_tool ./ecppack -add_rpath MINIFORGE_LIB_PATH

Yielding:

% ecppack 
Error: input file is mandatory.

Project Trellis - Open Source Tools for ECP5 FPGAs
Version 1.2.1
ecppack: ECP5 bitstream packer

Copyright (C) 2018 gatecat <gatecat@ds0.me>

Usage: ecppack input.config [output.bit] [options]
Allowed options:
  -h [ --help ]         show help
  -v [ --verbose ]      verbose output
  --db arg              Trellis database folder location
  --usercode arg        USERCODE to set in bitstream
  --idcode arg          IDCODE to override in bitstream
  --freq arg            config frequency in MHz
  --svf arg             output SVF file
  --svf-rowsize arg     SVF row size in bits (default 8000)
  --compress            compress bitstream to reduce size
  --spimode arg         SPI Mode to use (fast-read, dual-spi, qspi)
  --background          enable background reconfiguration in bitstream
  --delta arg           create a delta partial bitstream given a reference 
                        config
  --bootaddr arg        set next BOOTADDR in bitstream and enable multi-boot
  --input arg           input textual configuration
  --bit arg             output bitstream file
  --version             show current version and exit

@ccattuto
Copy link
Author

ccattuto commented Mar 12, 2022

Update: manually removing libpython3.9.dylib from the linker script for pytrellis.so (prjtrellis/libtrellis/CMakeFiles/pytrellis.dir/link.txt) fixes the segfault.

@qcabanes
Copy link

qcabanes commented Mar 18, 2022

Works like a charm! I could finally install nextpnr for ECP5

@JamesTimothyMeech
Copy link

Has anyone else tried the above solution without success? Could me using python3.11 instead of 3.9 prevent the fix from working?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants