Log caller for each Thread.new for `rake spec`

MSP-11147
bug/bundler_fix
Luke Imhoff 2014-11-05 15:34:35 -06:00
parent 96990fdc02
commit 097aa330e1
No known key found for this signature in database
GPG Key ID: 5B1FB01FB33356F8
3 changed files with 80 additions and 26 deletions

View File

@ -10,4 +10,5 @@ require 'metasploit/framework/spec/untested_payloads'
Metasploit::Framework::Require.optionally_active_record_railtie Metasploit::Framework::Require.optionally_active_record_railtie
Metasploit::Framework::Application.load_tasks Metasploit::Framework::Application.load_tasks
Metasploit::Framework::Spec::Threads::Suite.define_task
Metasploit::Framework::Spec::UntestedPayloads.define_task Metasploit::Framework::Spec::UntestedPayloads.define_task

View File

@ -0,0 +1,23 @@
require 'metasploit/framework/spec/threads/suite'
original_thread_new = Thread.method(:new)
# Patches `Thread.new` so that if logs `caller` so thread leaks can be traced
Thread.define_singleton_method(:new) { |*args, &block|
lines = ['BEGIN Thread.new caller']
caller.each do |frame|
lines << " #{frame}"
end
lines << 'END Thread.new caller'
Metasploit::Framework::Spec::Threads::Suite::LOG_PATHNAME.parent.mkpath
Metasploit::Framework::Spec::Threads::Suite::LOG_PATHNAME.open('a') { |f|
# single puts so threads can't write in between each other.
f.puts lines.join("\n")
}
original_thread_new.call(*args, &block)
}

View File

@ -1,9 +1,20 @@
module Metasploit::Framework::Spec::Threads::Suite require 'pathname'
# @note needs to use explicit nesting. so this file can be loaded directly without loading 'metasploit/framework' which
# allows for faster loading of rake tasks.
module Metasploit
module Framework
module Spec
module Threads
module Suite
# #
# CONSTANTS # CONSTANTS
# #
# Number of allowed threads when threads are counted in `before(:suite)`
EXPECTED_THREAD_COUNT_BEFORE_SUITE = 1 EXPECTED_THREAD_COUNT_BEFORE_SUITE = 1
# `caller` for all Thread.new calls
LOG_PATHNAME = Pathname.new('log/metasploit/framework/spec/threads/suite.log')
# #
# Module Methods # Module Methods
@ -18,12 +29,15 @@ module Metasploit::Framework::Spec::Threads::Suite
config.before(:suite) do config.before(:suite) do
thread_count = Thread.list.count thread_count = Thread.list.count
expect(thread_count).to( # check with if first so that error message can be constructed lazily
(be <= EXPECTED_THREAD_COUNT_BEFORE_SUITE), if thread_count > EXPECTED_THREAD_COUNT_BEFORE_SUITE
log = LOG_PATHNAME.read()
raise RuntimeError,
"#{thread_count} #{'thread'.pluralize(thread_count)} exist(s) when " \ "#{thread_count} #{'thread'.pluralize(thread_count)} exist(s) when " \
"only #{EXPECTED_THREAD_COUNT_BEFORE_SUITE} #{'thread'.pluralize(EXPECTED_THREAD_COUNT_BEFORE_SUITE)} " \ "only #{EXPECTED_THREAD_COUNT_BEFORE_SUITE} " \
"expected before suite runs" "#{'thread'.pluralize(EXPECTED_THREAD_COUNT_BEFORE_SUITE)} expected before suite runs:\n#{log}"
) end
end end
end end
@ -32,4 +46,20 @@ module Metasploit::Framework::Spec::Threads::Suite
@configured @configured
end end
def self.define_task
Rake::Task.define_task('metasploit:framework:spec:threads:suite') do
parent_pathname = Pathname.new(__FILE__).parent
threads_logger_pathname = parent_pathname.join('logger')
load_pathname = parent_pathname.parent.parent.parent.parent.expand_path
ENV['RUBYOPT'] = "-I#{load_pathname} -r#{threads_logger_pathname} #{ENV['RUBYOPT']}"
end
Rake::Task.define_task(spec: 'metasploit:framework:spec:threads:suite')
end
end
end
end
end
end end