Skip to content

Commit

Permalink
Land #9744, Add synchronization around public module metadata cache m…
Browse files Browse the repository at this point in the history
…ethods
  • Loading branch information
jmartin-tech committed Mar 22, 2018
2 parents 880f8ee + 70c9a43 commit b9fc786
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 40 deletions.
79 changes: 43 additions & 36 deletions lib/msf/core/modules/metadata/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,69 @@ class Cache
# Refreshes cached module metadata as well as updating the store
#
def refresh_metadata_instance(module_instance)
dlog "Refreshing #{module_instance.refname} of type: #{module_instance.type}"
refresh_metadata_instance_internal(module_instance)
update_store
@mutex.synchronize {
dlog "Refreshing #{module_instance.refname} of type: #{module_instance.type}"
refresh_metadata_instance_internal(module_instance)
update_store
}
end

#
# Returns the module data cache, but first ensures all the metadata is loaded
#
def get_metadata
wait_for_load
@module_metadata_cache.values
@mutex.synchronize {
wait_for_load
@module_metadata_cache.values
}
end

#
# Checks for modules loaded that are not a part of the cache and updates the underlying store
# if there are changes.
#
def refresh_metadata(module_sets)
unchanged_module_references = get_unchanged_module_references
has_changes = false
module_sets.each do |mt|
unchanged_reference_name_set = unchanged_module_references[mt[0]]

mt[1].keys.sort.each do |mn|
next if unchanged_reference_name_set.include? mn

begin
module_instance = mt[1].create(mn)
rescue Exception => e
elog "Unable to create module: #{mn}. #{e.message}"
end
@mutex.synchronize {
unchanged_module_references = get_unchanged_module_references
has_changes = false
module_sets.each do |mt|
unchanged_reference_name_set = unchanged_module_references[mt[0]]

mt[1].keys.sort.each do |mn|
next if unchanged_reference_name_set.include? mn

begin
module_instance = mt[1].create(mn)
rescue Exception => e
elog "Unable to create module: #{mn}. #{e.message}"
end

unless module_instance
wlog "Removing invalid module reference from cache: #{mn}"
existed = remove_from_cache(mn)
if existed
has_changes = true
unless module_instance
wlog "Removing invalid module reference from cache: #{mn}"
existed = remove_from_cache(mn)
if existed
has_changes = true
end
next
end
next
end

begin
refresh_metadata_instance_internal(module_instance)
has_changes = true
rescue Exception => e
elog("Error updating module details for #{module_instance.fullname}: #{$!.class} #{$!} : #{e.message}")
begin
refresh_metadata_instance_internal(module_instance)
has_changes = true
rescue Exception => e
elog("Error updating module details for #{module_instance.fullname}: #{$!.class} #{$!} : #{e.message}")
end
end
end
end

update_store if has_changes
update_store if has_changes
}
end

#######
private
#######

#
# Returns a hash(type->set) which references modules that have not changed.
#
Expand All @@ -102,10 +112,6 @@ def get_unchanged_module_references
return skip_reference_name_set_by_module_type
end

#######
private
#######

def remove_from_cache(module_name)
old_cache_size = @module_metadata_cache.size
@module_metadata_cache.delete_if {|_, module_metadata|
Expand Down Expand Up @@ -140,6 +146,7 @@ def get_cache_key(module_instance)
end

def initialize
@mutex = Mutex.new
@module_metadata_cache = {}
@store_loaded = false
@console = Rex::Ui::Text::Output::Stdio.new
Expand Down
8 changes: 4 additions & 4 deletions lib/msf/core/modules/metadata/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def init_store
load_metadata
end

#######
private
#######

#
# Update the module meta cache disk store
#
Expand All @@ -32,10 +36,6 @@ def update_store
end
end

#######
private
#######

def load_metadata
begin
retries ||= 0
Expand Down

0 comments on commit b9fc786

Please sign in to comment.