代码之家  ›  专栏  ›  技术社区  ›  dev

带Ruby Nokogiri模块的地址消毒剂

  •  2
  • dev  · 技术社区  · 6 年前

    我正在尝试在Nokogiri Ruby Gem中编译地址消毒剂。

    使用如下地址消毒剂编译libxml2和libxslt:

    ./configure CFLAGS="-fsanitize=address,undefined -Wformat -Werror=format-security  -Werror=array-bounds -g" CXXFLAGS="-fsanitize=address,undefined -Wformat -Werror=format-security -Werror=array-bounds -g" LDFLAGS="-fsanitize=address,undefined" CC="clang" CXX="clang++"
    

    安装了这样的宝石:

    sudo gem install nokogiri --version 1.8.0 -- --use-system-libraries --with-xml2-include=/home/user/Downloads/libxml2-2.9.3/include/ --with-xml2-lib=/usr/local/lib/ --with-cflags=\"-fsanitize=address\" --with-ldflags=\"-fsanitize=address -lasan -lubsan\" --with-xslt-include=/home/user/Downloads/libxslt-1.1.28/ --with-xslt-lib=/usr/local/lib/
    

    但是,当我使用nokogiri运行示例ruby脚本时

    脚本.rb

    #!/usr/bin/ruby
    require 'nokogiri'
    
    doc = File.open("sample.xml") { |f| Nokogiri::XML(f) }
    

    我收到这个消息:

     ./script.rb 
    ==30473==ASan runtime does not come first in initial library list; you should either link runtime to your application or manually preload it with LD_PRELOAD.
    

    当我进行预加载时:

     LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libasan.so.2 ./script.rb
        /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': /var/lib/gems/2.3.0/gems/nokogiri-1.8.0/lib/nokogiri/nokogiri.so: undefined symbol: __asan_init - /var/lib/gems/2.3.0/gems/nokogiri-1.8.0/lib/nokogiri/nokogiri.so (LoadError)
            from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
            from /var/lib/gems/2.3.0/gems/nokogiri-1.8.0/lib/nokogiri.rb:32:in `rescue in <top (required)>'
            from /var/lib/gems/2.3.0/gems/nokogiri-1.8.0/lib/nokogiri.rb:28:in `<top (required)>'
            from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:127:in `require'
            from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:127:in `rescue in require'
            from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:40:in `require'
            from ./script.rb:2:in `<main>'
    
        =================================================================
        ==30485==ERROR: LeakSanitizer: detected memory leaks
    
        Direct leak of 319176 byte(s) in 4259 object(s) allocated from:
            #0 0x7fce9ea4579a in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x9879a)
            #1 0x7fce9e5b4383 in ruby_xcalloc (/usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3+0x85383)
    
    .
    .
    .
    

    我需要用地址消毒剂来构建Ruby吗?不幸的是失败了…

    cd ruby-2.3.0
    ./configure --disable-shared CFLAGS="-fsanitize=address -ggdb" CXXFLAGS="-fsanitize=address -ggdb"
    make
    

    编译时中断,显示内存泄漏:/

    如何正确启用Ruby宝石上Ruby中的地址消毒剂?

    谢谢,

    更新1:

    如@yugr建议的,尝试传递“undefined”,但没有帮助:

    sudo gem install nokogiri --version 1.8.0 -- --use-system-libraries --with-xml2-include=/home/user/Downloads/libxml2-2.9.3/include/ --with-xml2-lib=/usr/local/lib/ --with-cflags=\"-fsanitize=address,undefined\" --with-ldflags=\"-fsanitize=address,undefined -lasan -lubsan\" --with-xslt-include=/home/user/Downloads/libxslt-1.1.28/
    Building native extensions with: '--use-system-libraries --with-xml2-include=/home/user/Downloads/libxml2-2.9.3/include/ --with-xml2-lib=/usr/local/lib/ --with-cflags="-fsanitize=address,undefined" --with-ldflags="-fsanitize=address,undefined -lasan -lubsan" --with-xslt-include=/home/user/Downloads/libxslt-1.1.28/'
    This could take a while...
    Successfully installed nokogiri-1.8.0
    Parsing documentation for nokogiri-1.8.0
    Done installing documentation for nokogiri after 1 seconds
    1 gem installed
    

    更新2:

    用asan成功地编译了ruby、libxml2、libxst和nokogiri,但是当我运行它时,我仍然得到:

    ./ruby ../script.rb
    Traceback (most recent call last):
        7: from ../script.rb:1:in `<main>'
        6: from /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:39:in `require'
        5: from /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
        4: from /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `require'
        3: from /usr/local/lib/ruby/gems/2.5.0/gems/nokogiri-1.8.0/lib/nokogiri.rb:28:in `<top (required)>'
        2: from /usr/local/lib/ruby/gems/2.5.0/gems/nokogiri-1.8.0/lib/nokogiri.rb:32:in `rescue in <top (required)>'
        1: from /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': /usr/local/lib/libxml2.so.2: undefined symbol: __asan_init - /usr/local/lib/ruby/gems/2.5.0/gems/nokogiri-1.8.0/lib/nokogiri/nokogiri.so (LoadError)
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   dev    6 年前

    好的,开始工作了。

    使用以下标志编译所有内容:

    -fsanitize=指向cflags、cxflags和ldflags的地址