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

Segfaults on Ubuntu Xenial #7

Open
Space-Ed opened this issue Mar 15, 2018 · 10 comments
Open

Segfaults on Ubuntu Xenial #7

Space-Ed opened this issue Mar 15, 2018 · 10 comments

Comments

@Space-Ed
Copy link

libasound2 & libasound2-dev installed

$ ruby --version
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

$ amidi -l
Dir Device    Name
IO  hw:2,0    Virtual Raw MIDI (16 subdevices)
IO  hw:2,1    Virtual Raw MIDI (16 subdevices)
IO  hw:2,2    Virtual Raw MIDI (16 subdevices)
IO  hw:2,3    Virtual Raw MIDI (16 subdevices)

Gemfile

gem "alsa-rawmidi"

alsa-midi.rb

require "alsa-rawmidi"
AlsaRawMIDI::Device.all
$ ruby alsa-midi.rb
alsa-rawmidi/api.rb:407: [BUG] Segmentation fault at 0xffffffff9bf5a098
@lucasluitjes
Copy link

I have the exact same problem, same libraries, ruby version 2.5.1, fresh ubuntu install. My C is really rusty, but if there's anything I can do to help resolve this, please let me know.

Details below:

$ ruby -e "require 'unimidi' ; UniMIDI::Input.gets"

Select a MIDI input...
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/api.rb:407: [BUG] Segmentation fault at 0xffffffffe3328e48
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

-- Control frame information -----------------------------------------------
c:0019 p:---- s:0096 e:000095 CFUNC  :snd_ctl_rawmidi_next_device
c:0018 p:0036 s:0090 e:000089 BLOCK  /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/api.rb:407 [FINISH]
c:0017 p:---- s:0085 e:000084 CFUNC  :select
c:0016 p:0025 s:0081 e:000080 METHOD /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/api.rb:405
c:0015 p:0015 s:0075 e:000074 METHOD /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/soundcard.rb:31
c:0014 p:0022 s:0070 e:000069 METHOD /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/soundcard.rb:14 [FINISH]
c:0013 p:---- s:0065 e:000064 CFUNC  :new
c:0012 p:0064 s:0060 e:000056 METHOD /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/soundcard.rb:23
c:0011 p:0013 s:0052 e:000051 BLOCK  /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/device.rb:45 [FINISH]
c:0010 p:---- s:0047 e:000046 CFUNC  :times
c:0009 p:0021 s:0043 e:000042 METHOD /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/device.rb:44
c:0008 p:0018 s:0037 e:000036 METHOD /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/device.rb:25
c:0007 p:0015 s:0033 e:000032 METHOD /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unimidi-0.4.8/lib/unimidi/adapter/alsa-rawmidi.rb:16
c:0006 p:0018 s:0029 e:000028 METHOD /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unimidi-0.4.8/lib/unimidi/loader.rb:20
c:0005 p:0012 s:0022 e:000021 METHOD /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unimidi-0.4.8/lib/unimidi/input.rb:17
c:0004 p:0004 s:0018 e:000017 METHOD /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unimidi-0.4.8/lib/unimidi/device.rb:19
c:0003 p:0048 s:0014 e:000013 METHOD /home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unimidi-0.4.8/lib/unimidi/device.rb:41
c:0002 p:0019 s:0006 e:000005 EVAL   -e:1 [FINISH]
c:0001 p:0000 s:0003 E:000ca0 (none) [FINISH]

-- Ruby level backtrace information ----------------------------------------
-e:1:in `<main>'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unimidi-0.4.8/lib/unimidi/device.rb:41:in `gets'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unimidi-0.4.8/lib/unimidi/device.rb:19:in `list'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unimidi-0.4.8/lib/unimidi/input.rb:17:in `all'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unimidi-0.4.8/lib/unimidi/loader.rb:20:in `devices'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unimidi-0.4.8/lib/unimidi/adapter/alsa-rawmidi.rb:16:in `inputs'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/device.rb:25:in `all_by_type'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/device.rb:44:in `get_devices'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/device.rb:44:in `times'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/device.rb:45:in `block in get_devices'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/soundcard.rb:23:in `find'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/soundcard.rb:23:in `new'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/soundcard.rb:14:in `initialize'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/soundcard.rb:31:in `populate_subdevices'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/api.rb:405:in `get_device_ids'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/api.rb:405:in `select'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/api.rb:407:in `block in get_device_ids'
/home/lucas/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/alsa-rawmidi-0.3.2/lib/alsa-rawmidi/api.rb:407:in `snd_ctl_rawmidi_next_device'

@arirusso
Copy link
Owner

arirusso commented Jul 3, 2018

What does your RawMIDI setup look like?

Some info here that might be helpful

@lucasluitjes
Copy link

lucasluitjes commented Jul 3, 2018

Edited: premature posting, formatting fixes

$ aconnect -i
client 0: 'System' [type=kernel]
    0 'Timer           '
    1 'Announce        '
client 14: 'Midi Through' [type=kernel]
    0 'Midi Through Port-0'
client 20: 'USB2MIDI' [type=kernel]
    0 'USB2MIDI MIDI 1 '
$ amidi -l
Dir Device    Name
IO  hw:1,0,0  USB2MIDI MIDI 1

I tested roughly the same script using python/mido/rtmidi, so it doesn't look like the hardware is the problem.

@arirusso
Copy link
Owner

arirusso commented Jul 3, 2018

I think the issue is that you need a RawMIDI device listed there. This should be handled better by the gem as to not segfault. See this guide and let me know if that helps. Unfortunately, I don't have an Ubuntu computer handy to try this all on, but I should in August

@lucasluitjes
Copy link

Using that guide I do see my rawmidi device in there, the USB2MIDI hardware device. In case it helps, all that's connected is a FCB1010 control surface, just an input device, not a synth or other output.

The same USB2MIDI + FCB1010 setup works well with alsa-rawmidi on ubuntu 14, by the way. Thanks for looking into it so far!

$ cat /proc/asound/cards
 0 [PCH            ]: HDA-Intel - HDA Intel PCH
                      HDA Intel PCH at 0xdc220000 irq 129
 1 [USB2MIDI       ]: USB-Audio - USB2MIDI
                      Alesis INC. USB2MIDI at usb-0000:00:14.0-7, full speed
$ cat /proc/asound/devices 
  1:        : sequencer
  2: [ 0]   : control
  3: [ 0- 0]: digital audio playback
  4: [ 0- 0]: digital audio capture
  5: [ 0- 3]: digital audio playback
  6: [ 0- 7]: digital audio playback
  7: [ 0- 8]: digital audio playback
  8: [ 0- 9]: digital audio playback
  9: [ 0-10]: digital audio playback
 10: [ 0- 0]: hardware dependent
 11: [ 0- 2]: hardware dependent
 12: [ 1]   : control
 13: [ 1- 0]: raw midi
 33:        : timer

@fukuchi
Copy link

fukuchi commented Feb 15, 2019

Hi,

Here's a quick-and-dirty hack to avoid the segmentation fault:

--- ./api.rb	2019-02-15 16:50:37.218105977 +0900
+++ /tmp/api.rb	2019-02-15 16:50:30.766584839 +0900
@@ -330,9 +330,9 @@
       # @param [Proc] block
       # @return [Integer]
       def open(id, &block)
-        handle_pointer = FFI::MemoryPointer.new(FFI.type_size(:int))
+        handle_pointer = FFI::MemoryPointer.new(FFI.type_size(:ulong))
         yield(handle_pointer)
-        handle_pointer.read_int
+        handle_pointer.read_ulong
       end
 
     end
@@ -424,9 +424,9 @@
       # @param [Integer] soundcard_id
       # @return [Integer]
       def get_handle(soundcard_id)
-        handle_pointer = FFI::MemoryPointer.new(FFI.type_size(:int))
+        handle_pointer = FFI::MemoryPointer.new(FFI.type_size(:ulong))
         API.snd_ctl_open(handle_pointer, get_name(soundcard_id), 0)
-        handle_pointer.read_int
+        handle_pointer.read_ulong
       end
 
     end

This works with Ubuntu 18.04 as far as I tested.

While I'm not familiar with the implementation of Ruby's FFI, I found that the problem behind this issue is that the latest alsa-rawmidi uses 'int' to store the address value.

The patch above may have a problem on 32-bit environment and/or old Linux distributions because probably they uses 32-bit runtimes instead 64-bit.

@chrisbrasington
Copy link

Thank you @fukuchi, that ulong patch was exactly what I needed to communicate with my USBMIDI device!

@NoraCodes
Copy link

@arirusso I'm also getting this error, on Ubuntu 20.04; any chance you could apply this patch and update the package?

@dromer
Copy link

dromer commented Mar 22, 2021

This patch also works for me! (debian buster amd64)

Would be great to not have to hack this in manually.

@najati
Copy link

najati commented Aug 18, 2023

Fantastic. This patch unstuck me on Raspbian. Is it not applied because it's waiting for a PR?

Repository owner deleted a comment from H4wk-eye Feb 21, 2024
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

8 participants