使用 Ruby Nokogiri 模块地址 Sanitizer

Address Sanitizer with Ruby Nokogiri module

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

像这样使用 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++"

并像这样安装 gem:

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 中正确启用 Address Sanitizer?

谢谢,

更新 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

更新二:

使用 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)

好的,它开始工作了。

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

-fsanitize=CFLAGS、CXXFLAGS 和 LDFLAGS 的地址