Debian Codegraph @

tool/leaked-globals

raw
1 #!/usr/bin/ruby
2 require_relative 'lib/colorize'
3
4 until ARGV.empty?
5 case ARGV[0]
6 when /\A SYMBOL_PREFIX=(.*)/x
7 SYMBOL_PREFIX = $1
8 when /\A NM=(.*)/x # may be multiple words
9 NM = $1
10 when /\A PLATFORM=(.+)?/x
11 platform = $1
12 when /\A SOEXT=(.+)?/x
13 soext = $1
14 when /\A SYMBOLS_IN_EMPTYLIB=(.*)/x
15 SYMBOLS_IN_EMPTYLIB = $1.split(" ")
16 else
17 break
18 end
19 ARGV.shift
20 end
21 SYMBOLS_IN_EMPTYLIB ||= nil
22
23 config = ARGV.shift
24 count = 0
25 col = Colorize.new
26
27 config_code = File.read(config)
28 REPLACE = config_code.scan(/\bAC_(?:REPLACE|CHECK)_FUNCS?\((\w+)/).flatten
29 # REPLACE << 'memcmp' if /\bAC_FUNC_MEMCMP\b/ =~ config_code
30 REPLACE.push('main', 'DllMain')
31 if platform and !platform.empty?
32 begin
33 h = File.read(platform)
34 rescue Errno::ENOENT
35 else
36 REPLACE.concat(
37 h .gsub(%r[/\*.*?\*/]m, " ") # delete block comments
38 .gsub(%r[//.*], " ") # delete oneline comments
39 .gsub(/^\s*#.*(?:\\\n.*)*/, "") # delete preprocessor directives
40 .gsub(/(?:\A|;)\K\s*typedef\s.*?;/m, "")
41 .scan(/\b((?!rb_|DEPRECATED|_)\w+)\s*\(.*\);/)
42 .flatten)
43 end
44 end
45 missing = File.dirname(config) + "/missing/"
46 ARGV.reject! do |n|
47 unless (src = Dir.glob(missing + File.basename(n, ".*") + ".[cS]")).empty?
48 puts "Ignore #{col.skip(n)} because of #{src.map {|s| File.basename(s)}.join(', ')} under missing"
49 true
50 end
51 end
52
53 # darwin's ld64 seems to require exception handling personality functions to be
54 # extern, so we allow the Rust one.
55 REPLACE.push("rust_eh_personality") if RUBY_PLATFORM.include?("darwin")
56
57 print "Checking leaked global symbols..."
58 STDOUT.flush
59 soext = /\.#{soext}(?:$|\.)/ if soext
60 so = soext =~ ARGV.first if ARGV.size == 1
61 IO.foreach("|#{NM} #{ARGV.join(' ')}") do |line|
62 line.chomp!
63 next so = nil if line.empty?
64 if so.nil? and line.chomp!(":")
65 so = soext =~ line || false
66 next
67 end
68 n, t, = line.split
69 next unless /[A-TV-Z]/ =~ t
70 next unless n.sub!(/^#{SYMBOL_PREFIX}/o, "")
71 next if n.include?(".")
72 next if !so and n.start_with?("___asan_")
73 case n
74 when /\A(?:Init_|InitVM_|pm_|[Oo]nig|dln_|coroutine_)/
75 next
76 when /\Aruby_static_id_/
77 next unless so
78 when /\A(?:RUBY_|ruby_|rb_)/
79 next unless so and /_(threadptr|ec)_/ =~ n
80 when *SYMBOLS_IN_EMPTYLIB
81 next
82 end
83 next if REPLACE.include?(n)
84 puts col.fail("leaked") if count.zero?
85 count += 1
86 puts " #{n}"
87 end
88 case count
89 when 0
90 puts col.pass("none")
91 when 1
92 abort col.fail("1 un-prefixed symbol leaked")
93 else
94 abort col.fail("#{count} un-prefixed symbols leaked")
95 end