2019-05-16 17:26:57 +03:00
|
|
|
#!/usr/bin/ruby
|
2019-07-15 02:54:34 +03:00
|
|
|
require_relative 'lib/colorize'
|
2019-05-16 17:26:57 +03:00
|
|
|
|
|
|
|
until ARGV.empty?
|
|
|
|
case ARGV[0]
|
2023-07-07 17:33:04 +03:00
|
|
|
when /\A SYMBOL_PREFIX=(.*)/x
|
2019-05-16 17:26:57 +03:00
|
|
|
SYMBOL_PREFIX = $1
|
2023-07-07 17:33:04 +03:00
|
|
|
when /\A NM=(.*)/x # may be multiple words
|
2019-05-16 17:26:57 +03:00
|
|
|
NM = $1
|
2023-07-07 17:33:04 +03:00
|
|
|
when /\A PLATFORM=(.+)?/x
|
2020-12-27 08:58:00 +03:00
|
|
|
platform = $1
|
2023-07-07 17:33:04 +03:00
|
|
|
when /\A SOEXT=(.+)?/x
|
|
|
|
soext = $1
|
2023-10-14 10:51:11 +03:00
|
|
|
when /\A SYMBOLS_IN_EMPTYLIB=(.*)/x
|
|
|
|
SYMBOLS_IN_EMPTYLIB = $1.split(" ")
|
2019-05-16 17:26:57 +03:00
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
ARGV.shift
|
|
|
|
end
|
2023-10-14 10:51:11 +03:00
|
|
|
SYMBOLS_IN_EMPTYLIB ||= nil
|
2019-05-16 17:26:57 +03:00
|
|
|
|
|
|
|
config = ARGV.shift
|
|
|
|
count = 0
|
|
|
|
col = Colorize.new
|
2023-04-02 12:11:59 +03:00
|
|
|
|
2020-12-25 18:53:51 +03:00
|
|
|
config_code = File.read(config)
|
2020-12-26 15:47:57 +03:00
|
|
|
REPLACE = config_code.scan(/\bAC_(?:REPLACE|CHECK)_FUNCS?\((\w+)/).flatten
|
2020-12-27 08:05:24 +03:00
|
|
|
# REPLACE << 'memcmp' if /\bAC_FUNC_MEMCMP\b/ =~ config_code
|
2020-12-26 15:46:27 +03:00
|
|
|
REPLACE.push('main', 'DllMain')
|
2020-12-27 08:58:00 +03:00
|
|
|
if platform and !platform.empty?
|
|
|
|
begin
|
|
|
|
h = File.read(platform)
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
else
|
|
|
|
REPLACE.concat(
|
|
|
|
h .gsub(%r[/\*.*?\*/]m, " ") # delete block comments
|
2023-01-21 13:25:45 +03:00
|
|
|
.gsub(%r[//.*], " ") # delete oneline comments
|
2020-12-27 08:58:00 +03:00
|
|
|
.gsub(/^\s*#.*(?:\\\n.*)*/, "") # delete preprocessor directives
|
2023-01-21 13:25:45 +03:00
|
|
|
.gsub(/(?:\A|;)\K\s*typedef\s.*?;/m, "")
|
2020-12-27 08:58:00 +03:00
|
|
|
.scan(/\b((?!rb_|DEPRECATED|_)\w+)\s*\(.*\);/)
|
|
|
|
.flatten)
|
|
|
|
end
|
|
|
|
end
|
2020-12-27 08:05:24 +03:00
|
|
|
missing = File.dirname(config) + "/missing/"
|
|
|
|
ARGV.reject! do |n|
|
|
|
|
unless (src = Dir.glob(missing + File.basename(n, ".*") + ".[cS]")).empty?
|
2023-04-02 11:56:12 +03:00
|
|
|
puts "Ignore #{col.skip(n)} because of #{src.map {|s| File.basename(s)}.join(', ')} under missing"
|
2020-12-27 08:05:24 +03:00
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2023-07-07 06:59:47 +03:00
|
|
|
|
2023-01-18 23:58:35 +03:00
|
|
|
# darwin's ld64 seems to require exception handling personality functions to be
|
|
|
|
# extern, so we allow the Rust one.
|
|
|
|
REPLACE.push("rust_eh_personality") if RUBY_PLATFORM.include?("darwin")
|
|
|
|
|
2019-05-16 17:26:57 +03:00
|
|
|
print "Checking leaked global symbols..."
|
|
|
|
STDOUT.flush
|
2023-07-07 17:33:04 +03:00
|
|
|
soext = /\.#{soext}(?:$|\.)/ if soext
|
|
|
|
so = soext =~ ARGV.first if ARGV.size == 1
|
2023-04-08 06:47:27 +03:00
|
|
|
IO.foreach("|#{NM} #{ARGV.join(' ')}") do |line|
|
2023-07-07 17:33:04 +03:00
|
|
|
line.chomp!
|
|
|
|
next so = nil if line.empty?
|
|
|
|
if so.nil? and line.chomp!(":")
|
|
|
|
so = soext =~ line || false
|
|
|
|
next
|
|
|
|
end
|
2019-05-16 17:26:57 +03:00
|
|
|
n, t, = line.split
|
2020-06-04 10:24:45 +03:00
|
|
|
next unless /[A-TV-Z]/ =~ t
|
2019-05-16 17:26:57 +03:00
|
|
|
next unless n.sub!(/^#{SYMBOL_PREFIX}/o, "")
|
|
|
|
next if n.include?(".")
|
2023-07-07 17:33:04 +03:00
|
|
|
next if !so and n.start_with?("___asan_")
|
|
|
|
case n
|
2023-09-27 19:39:53 +03:00
|
|
|
when /\A(?:Init_|InitVM_|pm_|[Oo]nig|dln_|coroutine_)/
|
2023-07-07 17:33:04 +03:00
|
|
|
next
|
|
|
|
when /\Aruby_static_id_/
|
|
|
|
next unless so
|
|
|
|
when /\A(?:RUBY_|ruby_|rb_)/
|
|
|
|
next unless so and /_(threadptr|ec)_/ =~ n
|
2023-10-14 10:51:11 +03:00
|
|
|
when *SYMBOLS_IN_EMPTYLIB
|
|
|
|
next
|
2023-07-07 17:33:04 +03:00
|
|
|
end
|
2019-05-16 17:26:57 +03:00
|
|
|
next if REPLACE.include?(n)
|
|
|
|
puts col.fail("leaked") if count.zero?
|
|
|
|
count += 1
|
|
|
|
puts " #{n}"
|
|
|
|
end
|
|
|
|
case count
|
|
|
|
when 0
|
|
|
|
puts col.pass("none")
|
|
|
|
when 1
|
|
|
|
abort col.fail("1 un-prefixed symbol leaked")
|
|
|
|
else
|
|
|
|
abort col.fail("#{count} un-prefixed symbols leaked")
|
|
|
|
end
|