2014-06-08 17:55:13 +00:00
|
|
|
module Mutant
|
2014-08-07 09:00:31 -07:00
|
|
|
# Module providing isolation
|
2014-06-08 17:55:13 +00:00
|
|
|
module Isolation
|
|
|
|
Error = Class.new(RuntimeError)
|
|
|
|
|
2014-07-06 02:11:31 +00:00
|
|
|
module None
|
|
|
|
|
|
|
|
# Call block in isolation
|
|
|
|
#
|
|
|
|
# @return [Object]
|
|
|
|
#
|
|
|
|
# @raise [Error]
|
|
|
|
# if block terminates abnormal
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def self.call(&block)
|
2014-06-25 00:13:24 +00:00
|
|
|
block.call
|
2014-07-06 02:11:31 +00:00
|
|
|
rescue => exception
|
2014-12-22 01:28:30 +00:00
|
|
|
raise Error, exception
|
2014-07-06 02:11:31 +00:00
|
|
|
end
|
2014-06-08 17:55:13 +00:00
|
|
|
end
|
|
|
|
|
2014-07-06 02:11:31 +00:00
|
|
|
module Fork
|
|
|
|
|
|
|
|
# Call block in isolation
|
|
|
|
#
|
|
|
|
# This isolation implements the fork strategy.
|
|
|
|
# Future strategies will probably use a process pool that can
|
|
|
|
# handle multiple mutation kills, in-isolation at once.
|
|
|
|
#
|
|
|
|
# @return [Object]
|
|
|
|
# returns block execution result
|
|
|
|
#
|
|
|
|
# @raise [Error]
|
|
|
|
# if block terminates abnormal
|
|
|
|
#
|
2014-12-22 01:28:30 +00:00
|
|
|
# rubocop:disable MethodLength
|
|
|
|
#
|
2015-07-02 03:35:54 +00:00
|
|
|
# @api private
|
2014-07-06 02:11:31 +00:00
|
|
|
def self.call(&block)
|
2015-07-23 13:45:43 -07:00
|
|
|
IO.pipe(binmode: true) do |reader, writer|
|
2015-07-29 15:51:39 -07:00
|
|
|
writer.binmode
|
2015-07-23 13:45:43 -07:00
|
|
|
begin
|
|
|
|
pid = Process.fork do
|
2015-07-29 15:52:57 -07:00
|
|
|
File.open(File::NULL, File::WRONLY) do |file|
|
2015-07-23 13:45:43 -07:00
|
|
|
$stderr.reopen(file)
|
|
|
|
reader.close
|
|
|
|
writer.write(Marshal.dump(block.call))
|
|
|
|
writer.close
|
|
|
|
end
|
|
|
|
end
|
2014-10-24 21:34:33 +00:00
|
|
|
|
|
|
|
writer.close
|
2015-07-23 13:45:43 -07:00
|
|
|
Marshal.load(reader.read)
|
|
|
|
ensure
|
|
|
|
Process.waitpid(pid) if pid
|
2014-10-24 21:34:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue => exception
|
2014-12-22 01:28:30 +00:00
|
|
|
raise Error, exception
|
2014-07-06 02:11:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end # Fork
|
|
|
|
|
2014-06-08 17:55:13 +00:00
|
|
|
end # Isolator
|
|
|
|
end # Mutant
|