<s> import logging as log <EOL> import os <EOL> import platform <EOL> import re <EOL> import signal <EOL> import subprocess <EOL> import tempfile <EOL> import time <EOL> from . import _common <EOL> if platform . system ( ) == "<STR_LIT>" : <EOL> import psutil <EOL> import distutils . spawn <EOL> POSSIBLE_PATHS = [ <EOL> os . environ [ '<STR_LIT>' ] , <EOL> os . path . join ( os . environ [ '<STR_LIT>' ] , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> os . path . join ( os . environ [ '<STR_LIT>' ] , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> os . path . join ( _common . PATH_DBG ) , <EOL> ] <EOL> TOOL_CDB = distutils . spawn . find_executable ( '<STR_LIT>' , os . pathsep . join ( POSSIBLE_PATHS ) ) <EOL> TIMEOUT_CHILD_EXIT_WAIT = <NUM_LIT> <EOL> POLL_SLEEP = <NUM_LIT> <EOL> def run_with_windbg ( target_cmd , symbols = None , env = None , callback = None , callback_args = None , <EOL> pageheap = True , timeout = _common . DEFAULT_TIMEOUT , memory_limit = None , <EOL> idle_limit = None ) : <EOL> """<STR_LIT>""" <EOL> target = os . path . basename ( target_cmd [ <NUM_LIT:0> ] ) <EOL> if pageheap and target not in _common . _gflags_enabled : <EOL> _common . _set_gflags ( target ) <EOL> res = _cdb_common ( target_cmd = target_cmd , symbols = symbols , env = env , callback = callback , <EOL> callback_args = callback_args , timeout = timeout , memory_limit = memory_limit , <EOL> idle_limit = idle_limit ) <EOL> return res <EOL> def attach_with_windbg ( target_pid , symbols = None , callback = None , callback_args = None , <EOL> timeout = _common . DEFAULT_TIMEOUT , memory_limit = None ) : <EOL> """<STR_LIT>""" <EOL> res = _cdb_common ( target_pid = target_pid , symbols = symbols , <EOL> callback = callback , callback_args = callback_args , <EOL> timeout = timeout , memory_limit = memory_limit ) <EOL> return res [ <NUM_LIT:1> ] <EOL> def _cdb_common ( target_pid = None , target_cmd = None , symbols = None , env = None , callback = None , <EOL> callback_args = None , timeout = _common . DEFAULT_TIMEOUT , memory_limit = None , <EOL> idle_limit = None ) : <EOL> assert target_pid is not None or target_cmd is not None <EOL> assert target_pid is None or target_cmd is None <EOL> classification = None <EOL> if env is None : <EOL> env = dict ( os . environ ) <EOL> env [ "<STR_LIT>" ] = "<STR_LIT:1>" <EOL> if symbols is not None : <EOL> env [ "<STR_LIT>" ] = symbols <EOL> elif target_cmd : <EOL> env [ "<STR_LIT>" ] = os . path . abspath ( os . path . dirname ( target_cmd [ <NUM_LIT:0> ] ) ) <EOL> cdb_cmd = [ TOOL_CDB , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:-c>" , "<STR_LIT>" "<STR_LIT>" ] <EOL> start_time = _common . prof_timer ( ) <EOL> with tempfile . TemporaryFile ( mode = "<STR_LIT>" ) as f : <EOL> if target_pid is not None : <EOL> cdb_cmd [ - <NUM_LIT:1> ] += "<STR_LIT:d>" <EOL> cdb_cmd += [ "<STR_LIT>" , "<STR_LIT>" % target_pid ] <EOL> p = subprocess . Popen ( cdb_cmd , stderr = f , stdout = f , env = env , <EOL> stdin = subprocess . PIPE , creationflags = _common . POPEN_FLAGS ) <EOL> else : <EOL> p = subprocess . Popen ( cdb_cmd + target_cmd , stderr = f , stdout = f , env = env , <EOL> stdin = subprocess . PIPE , creationflags = _common . POPEN_FLAGS ) <EOL> ps = psutil . Process ( p . pid ) <EOL> bn = os . path . basename ( target_cmd [ <NUM_LIT:0> ] ) <EOL> try : <EOL> while ( _common . prof_timer ( ) - start_time ) < timeout : <EOL> if p . poll ( ) is not None : <EOL> raise psutil . NoSuchProcess ( p . pid ) <EOL> for kid in ps . get_children ( ) : <EOL> if kid . name != bn : <EOL> continue <EOL> assert target_pid is None , "<STR_LIT>" <EOL> target_pid = kid . pid <EOL> if target_pid is not None : <EOL> break <EOL> time . sleep ( POLL_SLEEP ) <EOL> assert target_pid is not None , "<STR_LIT>" <EOL> except psutil . NoSuchProcess : <EOL> log . debug ( "<STR_LIT>" ) <EOL> target_pid = p . pid <EOL> try : <EOL> cb_res = _common . _call_callback ( callback , callback_args , target_pid ) <EOL> if cb_res == _common . CB_ERROR : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if p . poll ( ) is None and target_cmd is None : <EOL> try : <EOL> p . send_signal ( signal . CTRL_BREAK_EVENT ) <EOL> except WindowsError as e : <EOL> f . write ( "<STR_LIT>" % e ) <EOL> log . warn ( "<STR_LIT>" ) <EOL> target_mon = _common . TargetMonitor ( target_pid , idle_limit = idle_limit , <EOL> memory_limit = memory_limit , time_limit = timeout ) <EOL> while p . poll ( ) is None : <EOL> if target_mon . check_memory ( ) : <EOL> f . write ( "<STR_LIT>" % ( memory_limit / <NUM_LIT> ) ) <EOL> classification = _common . EXCESS_MEMORY_USAGE <EOL> break <EOL> if target_mon . check_idle ( ) : <EOL> break <EOL> if target_mon . check_timeout ( ) : <EOL> classification = _common . TIMEOUT <EOL> break <EOL> time . sleep ( POLL_SLEEP ) <EOL> finally : <EOL> if p . poll ( ) is None : <EOL> attempt = <NUM_LIT:0> <EOL> pid = target_pid <EOL> while True : <EOL> if attempt == <NUM_LIT:1> : <EOL> try : <EOL> p . send_signal ( signal . CTRL_BREAK_EVENT ) <EOL> except WindowsError as e : <EOL> f . write ( "<STR_LIT>" <EOL> % e ) <EOL> log . warn ( "<STR_LIT>" ) <EOL> else : <EOL> with open ( os . devnull , "<STR_LIT:w>" ) as fp : <EOL> subprocess . call ( [ "<STR_LIT>" , "<STR_LIT>" , str ( pid ) , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> stdout = fp , stderr = fp ) <EOL> st = _common . prof_timer ( ) <EOL> while p . poll ( ) is None and ( _common . prof_timer ( ) - st ) < TIMEOUT_CHILD_EXIT_WAIT : <EOL> time . sleep ( POLL_SLEEP ) <EOL> if p . poll ( ) is not None : <EOL> break <EOL> if attempt == <NUM_LIT:0> : <EOL> f . write ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( time . time ( ) , pid ) ) <EOL> elif attempt == <NUM_LIT:1> : <EOL> f . write ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( time . time ( ) , pid ) ) <EOL> pid = p . pid <EOL> attempt += <NUM_LIT:1> <EOL> assert attempt <= <NUM_LIT:3> , "<STR_LIT>" <EOL> assert attempt <= <NUM_LIT:1> , "<STR_LIT>" <EOL> exit_code = p . wait ( ) <EOL> f . seek ( <NUM_LIT:0> , os . SEEK_SET ) <EOL> debug_log = f . read ( ) <EOL> backtrace = [ ] <EOL> m = re . search ( r"<STR_LIT>" , <EOL> debug_log , re . MULTILINE ) <EOL> if m is not None : <EOL> raise RuntimeError ( "<STR_LIT>" % m . group ( "<STR_LIT>" ) ) <EOL> debug_class = _common . NOT_AN_EXCEPTION <EOL> if ( "<STR_LIT>" % target_pid ) in debug_log : <EOL> backtrace , debug_class , exit_code = _common . process_exploitable_output ( debug_log ) <EOL> if cb_res == _common . CB_HANG : <EOL> classification = _common . TIMEOUT <EOL> elif classification is None : <EOL> if cb_res == _common . CB_FAIL : <EOL> classification = _common . UNKNOWN <EOL> else : <EOL> classification = debug_class <EOL> debug_log = _common . _limit_output_length ( debug_log ) <EOL> return _common . FuzzResult ( classification , debug_log , backtrace , exit_code = exit_code ) <EOL> def reduce_timeout_with_windbg ( target_cmd , timeout = _common . DEFAULT_TIMEOUT , init_wait = None , <EOL> symbols = None , env = None , callback = None , callback_args = None ) : <EOL> """<STR_LIT>""" <EOL> if callback is not None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if timeout <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if init_wait is None : <EOL> init_wait = timeout / <NUM_LIT:4> <EOL> if env is None : <EOL> env = dict ( os . environ ) <EOL> env [ "<STR_LIT>" ] = "<STR_LIT:1>" <EOL> if symbols is not None : <EOL> env [ "<STR_LIT>" ] = symbols <EOL> elif target_cmd : <EOL> env [ "<STR_LIT>" ] = os . path . abspath ( os . path . dirname ( target_cmd [ <NUM_LIT:0> ] ) ) <EOL> dbg_script = os . path . join ( _common . PATH_DBG , "<STR_LIT>" , "<STR_LIT>" ) <EOL> timeout_time = time . time ( ) + init_wait <EOL> cdb_cmd = [ TOOL_CDB , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:-c>" , "<STR_LIT>" % ( dbg_script , timeout ) ] <EOL> gflags_args = _common . _disable_gflags ( target_cmd [ <NUM_LIT:0> ] ) <EOL> with tempfile . TemporaryFile ( mode = "<STR_LIT>" ) as f : <EOL> f . write ( "<STR_LIT>" % ( timeout ) ) <EOL> if timeout < <NUM_LIT:30> : <EOL> f . write ( "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" % ( init_wait ) ) <EOL> p = subprocess . Popen ( cdb_cmd + target_cmd , stderr = f , stdout = f , env = env , <EOL> stdin = subprocess . PIPE , creationflags = _common . POPEN_FLAGS ) <EOL> kill_debugger = False <EOL> while p . poll ( ) is None : <EOL> if time . time ( ) > timeout_time : <EOL> if not kill_debugger : <EOL> timeout_time = time . time ( ) + ( timeout * <NUM_LIT:5> ) <EOL> kill_debugger = True <EOL> try : <EOL> os . kill ( p . pid , signal . CTRL_C_EVENT ) <EOL> except OSError : <EOL> pass <EOL> else : <EOL> with open ( os . devnull , "<STR_LIT:w>" ) as fp : <EOL> subprocess . call ( [ "<STR_LIT>" , "<STR_LIT>" , str ( p . pid ) , "<STR_LIT>" ] , <EOL> stdout = fp , stderr = fp ) <EOL> p . wait ( ) <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> f . seek ( <NUM_LIT:0> , os . SEEK_SET ) <EOL> output = [ ] <EOL> for line in f . readlines ( ) : <EOL> if line . strip ( ) not in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> output . append ( line ) <EOL> if gflags_args is not None : <EOL> _common . _set_gflags ( target_cmd [ <NUM_LIT:0> ] , ** gflags_args ) <EOL> output = '<STR_LIT>' . join ( output ) <EOL> has_tb = output . find ( "<STR_LIT>" ) <EOL> if has_tb != - <NUM_LIT:1> : <EOL> raise RuntimeError ( "<STR_LIT>" % ( "<STR_LIT:->" * <NUM_LIT> , output [ has_tb : ] ) ) <EOL> call_stack = _common . process_exploitable_output ( output ) [ <NUM_LIT:0> ] <EOL> if not call_stack : <EOL> return _common . FuzzResult ( text = output ) <EOL> return _common . FuzzResult ( classification = _common . TIMEOUT , text = output , backtrace = call_stack ) <EOL> def trace_memory_usage_with_windbg ( target_cmd , init_sym , memory_limit , <EOL> timeout = _common . DEFAULT_TIMEOUT , symbols = None , env = None , <EOL> callback = None , callback_args = None ) : <EOL> """<STR_LIT>""" <EOL> if callback is not None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if not isinstance ( init_sym , str ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( init_sym ) . __name__ ) <EOL> if not isinstance ( memory_limit , int ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( memory_limit ) . __name__ ) <EOL> if env is None : <EOL> env = dict ( os . environ ) <EOL> env [ "<STR_LIT>" ] = "<STR_LIT:1>" <EOL> if symbols is not None : <EOL> env [ "<STR_LIT>" ] = symbols <EOL> elif target_cmd : <EOL> env [ "<STR_LIT>" ] = os . path . abspath ( os . path . dirname ( target_cmd [ <NUM_LIT:0> ] ) ) <EOL> dbg_script = os . path . join ( _common . PATH_DBG , "<STR_LIT>" , "<STR_LIT>" ) <EOL> cdb_cmd = [ TOOL_CDB , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:-c>" , "<STR_LIT>" % ( dbg_script , memory_limit , init_sym ) ] <EOL> gflags_args = _common . _disable_gflags ( target_cmd [ <NUM_LIT:0> ] ) <EOL> with tempfile . TemporaryFile ( mode = "<STR_LIT>" ) as f : <EOL> p = subprocess . Popen ( cdb_cmd + target_cmd , stderr = f , stdout = f , env = env , <EOL> stdin = subprocess . PIPE , creationflags = _common . POPEN_FLAGS ) <EOL> timeout_time = time . time ( ) + timeout <EOL> while p . poll ( ) is None : <EOL> if time . time ( ) > timeout_time : <EOL> with open ( os . devnull , "<STR_LIT:w>" ) as fp : <EOL> subprocess . call ( [ "<STR_LIT>" , "<STR_LIT>" , str ( p . pid ) , "<STR_LIT>" ] , stdout = fp , stderr = fp ) <EOL> p . wait ( ) <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> f . seek ( <NUM_LIT:0> , os . SEEK_SET ) <EOL> output = [ ] <EOL> for line in f . readlines ( ) : <EOL> if not line . strip ( ) . startswith ( "<STR_LIT>" ) : <EOL> output . append ( line ) <EOL> if gflags_args is not None : <EOL> _common . _set_gflags ( target_cmd [ <NUM_LIT:0> ] , ** gflags_args ) <EOL> output = '<STR_LIT>' . join ( output ) <EOL> has_tb = output . find ( "<STR_LIT>" ) <EOL> if has_tb != - <NUM_LIT:1> : <EOL> raise RuntimeError ( "<STR_LIT>" % ( "<STR_LIT:->" * <NUM_LIT> , output [ has_tb : ] ) ) <EOL> call_stack = _common . process_exploitable_output ( output ) [ <NUM_LIT:0> ] <EOL> if not call_stack : <EOL> return _common . FuzzResult ( text = output ) <EOL> return _common . FuzzResult ( classification = _common . EXCESS_MEMORY_USAGE , <EOL> text = output , <EOL> backtrace = call_stack ) </s>
