Skip to content

Commit

Permalink
leaked-globals: check leaked symbols in libruby.so if enable-shared
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Jul 8, 2023
1 parent 28ae4e4 commit cceb410
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions cygwin/GNUmakefile.in
Expand Up @@ -41,6 +41,7 @@ SOLIBS := $(DLL_BASE_NAME).res.$(OBJEXT) $(SOLIBS)
override EXTOBJS += $(if $(filter-out $(RUBYW_INSTALL_NAME),$(@:$(EXEEXT)=)),$(RUBY_INSTALL_NAME),$(@:$(EXEEXT)=)).res.$(OBJEXT)
RCFILES = $(RUBY_INSTALL_NAME).rc $(RUBYW_INSTALL_NAME).rc $(DLL_BASE_NAME).rc
RUBYDEF = $(DLL_BASE_NAME).def
override LIBRUBY_FOR_LEAKED_GLOBALS := # DLL shows symbols from import library

ruby: $(PROGRAM)
rubyw: $(WPROGRAM)
Expand Down
8 changes: 6 additions & 2 deletions template/Makefile.in
Expand Up @@ -637,6 +637,10 @@ un-runnable:
$(ECHO) cannot make runnable, configure with --enable-load-relative.
$(Q) exit 1

LIBRUBY_FOR_LEAKED_GLOBALS = $(enable_shared:no=)
yes-test-basic: $(DOT_WAIT) leaked-globals
leaked-globals: $(COMMONOBJS) prog $(tooldir)/leaked-globals PHONY
$(Q) $(XRUBY) $(tooldir)/leaked-globals NM="$(NM) -Pgp" SYMBOL_PREFIX=$(SYMBOL_PREFIX) PLATFORM=$(hdrdir)/ruby/$(PLATFORM_DIR).h $(srcdir)/configure.ac $(COMMONOBJS)
leaked-globals: $(COMMONOBJS) prog $(tooldir)/leaked-globals
$(Q) $(XRUBY) $(tooldir)/leaked-globals \
SOEXT=$(SOEXT) NM="$(NM) -Pgp" SYMBOL_PREFIX=$(SYMBOL_PREFIX) \
PLATFORM=$(hdrdir)/ruby/$(PLATFORM_DIR).h $(srcdir)/configure.ac \
$(COMMONOBJS) $(LIBRUBY_FOR_LEAKED_GLOBALS:yes=$(LIBRUBY_SO))
27 changes: 22 additions & 5 deletions tool/leaked-globals
Expand Up @@ -3,12 +3,14 @@ require_relative 'lib/colorize'

until ARGV.empty?
case ARGV[0]
when /\ASYMBOL_PREFIX=(.*)/
when /\A SYMBOL_PREFIX=(.*)/x
SYMBOL_PREFIX = $1
when /\ANM=(.*)/ # may be multiple words
when /\A NM=(.*)/x # may be multiple words
NM = $1
when /\APLATFORM=(.+)?/
when /\A PLATFORM=(.+)?/x
platform = $1
when /\A SOEXT=(.+)?/x
soext = $1
else
break
end
Expand Down Expand Up @@ -51,13 +53,28 @@ REPLACE.push("rust_eh_personality") if RUBY_PLATFORM.include?("darwin")

print "Checking leaked global symbols..."
STDOUT.flush
soext = /\.#{soext}(?:$|\.)/ if soext
so = soext =~ ARGV.first if ARGV.size == 1
IO.foreach("|#{NM} #{ARGV.join(' ')}") do |line|
line.chomp!
next so = nil if line.empty?
if so.nil? and line.chomp!(":")
so = soext =~ line || false
next
end
n, t, = line.split
next unless /[A-TV-Z]/ =~ t
next unless n.sub!(/^#{SYMBOL_PREFIX}/o, "")
next if n.include?(".")
next if n.start_with?("___asan_")
next if /\A(?:Init_|InitVM_|RUBY_|ruby_|rb_|yp_|[Oo]nig|dln_|coroutine_)/ =~ n
next if !so and n.start_with?("___asan_")
case n
when /\A(?:Init_|InitVM_|yp_|[Oo]nig|dln_|coroutine_)/
next
when /\Aruby_static_id_/
next unless so
when /\A(?:RUBY_|ruby_|rb_)/
next unless so and /_(threadptr|ec)_/ =~ n
end
next if REPLACE.include?(n)
puts col.fail("leaked") if count.zero?
count += 1
Expand Down

0 comments on commit cceb410

Please sign in to comment.