使用Ruby Nokogiri模块解决Sanitizer问题

时间:2018-05-18 09:46:45

标签: ruby address-sanitizer

我正在尝试在nokogiri Ruby gem中编译Address Sanitizer。

使用Address Sanitizer编译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脚本时

script.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)

.
.
.

我是否还必须使用Address Sanitizer构建Ruby?不幸的是,它失败了......

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

打破编译,显示内存泄漏:/

如何在Ruby Gem上正确启用Ruby Sanitizer?

谢谢,

更新1:

试图通过&#34; undefined&#34;正如@yugr建议的那样,但没有帮助:

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 个答案:

答案 0 :(得分:0)

好的让它发挥作用。

使用这些标志使用GCC编译所有内容:

-fsanitize = CFLAGS,CXXFLAGS和LDFLAGS的地址