debug icon indicating copy to clipboard operation
debug copied to clipboard

DAP Server fails when dealing with some binary data

Open marianosimone opened this issue 3 years ago • 0 comments

Your environment

  • ruby -v: 2.7.2p137
  • rdbg -v: 1.6.2

Describe the bug With certain binary data in a variable, the DAP server fails to dump the frame data and crashes

To Reproduce

I originally detected this when using protobuf, as the inspect method uses interpolates its value (which is binary data) into the string that describes it.

A simple way to reproduce is:

  1. Create a file like:
class PassthroughInspect
  def initialize(data)
    @data = data
  end

  def inspect
    @data
  end
end

with_binary_data = PassthroughInspect.new([8, 200, 1].pack('CCC'))
puts("Put a breakpoint here!")
  1. Use VSCode, add a breakpoint to the last line, and start debugging
  2. Immediately after reaching the breakpoint, the session will end

Expected behavior The debugging session shouldn't crash

Additional context To debug this, you can add something like this to the same file you are debugging:

require 'json'

JSON.instance_eval do
  class << self
    alias unsafe_dump dump
  end

  def dump(obj, *args)
    unsafe_dump(obj, *args)
  rescue StandardError => e
    puts("There was an exception dumping the state: #{e}")
    puts("The backtrace is: #{e.backtrace&.join('\n')}")
    puts("The offending object is #{obj}")
  end
end

This shows that the problem is source sequence is illegal/malformed utf-8, and the trace is:

 <redacted>/json-2.3.1/lib/json/common.rb:430:in `generate'
 <redacted>/json-2.3.1/lib/json/common.rb:430:in `generate'
 <redacted>/json-2.3.1/lib/json/common.rb:629:in `dump'
 target.rb:11:in `dump' # this is the test file
 <redacted>/debug-1.6.2/lib/debug/server_dap.rb:206:in `send'
 <redacted>/debug-1.6.2/lib/debug/server_dap.rb:220:in `send_response'
 <redacted>/debug-1.6.2/lib/debug/server_dap.rb:441:in `respond'
 <redacted>/debug-1.6.2/lib/debug/server_dap.rb:650:in `dap_event'
 <redacted>/debug-1.6.2/lib/debug/session.rb:307:in `process_event'
 <redacted>/debug-1.6.2/lib/debug/session.rb:204:in `session_server_main'
 <redacted>/debug-1.6.2/lib/debug/session.rb:174:in `block in activate'

The problem is that the any variable in my example now has binary data as its value, and when ruby/debug tries to dump it into a String that can send over DAP, JSON complains about it not being a utf-8 string.

marianosimone avatar Sep 21 '22 16:09 marianosimone