Package Peach :: Package Agent :: Module process
[hide private]

Source Code for Module Peach.Agent.process

  1   
  2  ''' 
  3  Process control agent.  This agent is able to start, stop, and monitor 
  4  if a process is running.  If the process exits early a fault will be 
  5  issued to the fuzzer. 
  6   
  7  @author: Michael Eddington 
  8  @version: $Id: Peach.Agent.process-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
  9  ''' 
 10   
 11  # 
 12  # Copyright (c) 2007 Michael Eddington 
 13  # 
 14  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 15  # of this software and associated documentation files (the "Software"), to deal 
 16  # in the Software without restriction, including without limitation the rights  
 17  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 18  # copies of the Software, and to permit persons to whom the Software is  
 19  # furnished to do so, subject to the following conditions: 
 20  # 
 21  # The above copyright notice and this permission notice shall be included in     
 22  # all copies or substantial portions of the Software. 
 23  # 
 24  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 25  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 26  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 27  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 28  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 29  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 30  # SOFTWARE. 
 31  # 
 32   
 33  # Authors: 
 34  #   Michael Eddington (mike@phed.org) 
 35   
 36  # $Id: Peach.Agent.process-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 37   
 38  ''' 
 39  <!-- Start, stop, and monitor a process --> 
 40  <Monitor class="process.Process"> 
 41           
 42          <!-- [Optional] Should we restart the process for each test case? --> 
 43          <!-- Default: False --> 
 44          <Param name="RestartOnEachTest" value="False" /> 
 45           
 46          <!-- [Optional] Should we report an early exit of the process as a fault? --> 
 47          <!-- Default: True --> 
 48          <Param name="FaultOnEarlyExit" value="True" /> 
 49           
 50          <!-- Command to start process --> 
 51          <Param name="Command" value="" /> 
 52           
 53  </Monitor> 
 54  ''' 
 55   
 56  import sys 
 57  sys.path.append("..") 
 58  sys.path.append("../..") 
 59  import os 
 60   
 61  try: 
 62          import win32con 
 63          from win32process import * 
 64  except: 
 65          print "Warning: win32 extensions not found, disabing variouse process monitors." 
 66   
 67  from Peach.agent import Monitor 
 68   
69 -class PageHeap(Monitor):
70 ''' 71 A monitor that will enable/disable pageheap on an executable. 72 ''' 73
74 - def __init__(self, args):
75 ''' 76 Params: Path, Executable 77 ''' 78 79 try: 80 self._path = args['Path'] + "\\gflags.exe" 81 82 except: 83 self._path = 'c:\\Program Files\\Debugging Tools for Windows\\gflags.exe' 84 85 self._exe = args['Executable'] 86 87 self._onParams = [ 'gflags.exe', '/p', '/full', '/enable', self._exe ] 88 self._offParams = [ 'gflags.exe', '/p', '/disable', self._exe ] 89 90 os.spawnv(os.P_WAIT, self._path, self._onParams )
91
92 - def OnShutdown(self):
93 os.spawnv(os.P_WAIT, self._path, self._offParams )
94 95
96 -class Process(Monitor):
97 ''' 98 Process control agent. This agent is able to start, stop, and monitor 99 if a process is running. If the process exits early a fault will be 100 issued to the fuzzer. 101 ''' 102
103 - def __init__(self, args):
104 if args.has_key('RestartOnEachTest'): 105 if args['RestartOnEachTest'].lower() == 'true': 106 self.restartOnTest = True 107 else: 108 self.restartOnTest = False 109 else: 110 self.restartOnTest = False 111 112 if args.has_key('FaultOnEarlyExit'): 113 if args['FaultOnEarlyExit'].lower() == 'true': 114 self.faultOnEarlyExit = True 115 else: 116 self.faultOnEarlyExit = False 117 else: 118 self.faultOnEarlyExit = True 119 120 self.strangeExit = False 121 self.command = args["Command"] 122 self.args = None 123 self.pid = None 124 self.hProcess = None 125 self.hThread = None 126 self.dwProcessId = None 127 self.dwThreadId = None
128
129 - def _StopProcess(self):
130 131 if self.hProcess == None: 132 return 133 134 if self._IsProcessRunning(): 135 TerminateProcess(self.hProcess, 0) 136 137 self.hProcess = None 138 self.hThread = None 139 self.dwProcessId = None 140 self.dwThreadId = None
141
142 - def _StartProcess(self):
143 if self.hProcess != None: 144 self._StopProcess() 145 146 (hProcess, hThread, dwProcessId, dwThreadId) = CreateProcess(None, self.command, 147 None, None, 0, 0, None, None, STARTUPINFO()) 148 149 self.hProcess = hProcess 150 self.hThread = hThread 151 self.dwProcessId = dwProcessId 152 self.dwThreadId = dwThreadId
153
154 - def _IsProcessRunning(self):
155 if self.hProcess == None: 156 return False 157 158 ret = GetExitCodeProcess(self.hProcess) 159 if ret != win32con.STILL_ACTIVE: 160 return False 161 162 ret = GetExitCodeThread(self.hThread) 163 if ret != win32con.STILL_ACTIVE: 164 return False 165 166 return True
167
168 - def OnTestStarting(self):
169 ''' 170 Called right before start of test. 171 ''' 172 self.strangeExit = False 173 if self.restartOnTest or not self._IsProcessRunning(): 174 self._StopProcess() 175 self._StartProcess()
176
177 - def OnTestFinished(self):
178 ''' 179 Called right after a test. 180 ''' 181 if not self._IsProcessRunning(): 182 self.strangeExit = True 183 184 if self.restartOnTest: 185 self._StopProcess()
186
187 - def GetMonitorData(self):
188 ''' 189 Get any monitored data. 190 ''' 191 if self.strangeExit: 192 return "Process exited early" 193 194 return None
195
196 - def DetectedFault(self):
197 ''' 198 Check if a fault was detected. If the process exits 199 with out our help we will report it as a fault. 200 ''' 201 if self.faultOnEarlyExit: 202 return not self._IsProcessRunning() 203 204 else: 205 return False
206
207 - def OnFault(self):
208 ''' 209 Called when a fault was detected. 210 ''' 211 self._StopProcess()
212
213 - def OnShutdown(self):
214 ''' 215 Called when Agent is shutting down. 216 ''' 217 self._StopProcess()
218 219 # end 220