def trace_memory_usage_with_windbg(target_cmd, init_sym, memory_limit,
                                   timeout=_common.DEFAULT_TIMEOUT, symbols=None, env=None,
                                   callback=None, callback_args=None):
    """
    This can be used to help pin point the call that causes the target
    application to exceed the defined memory limit.  It returns a
    :class:`FuzzResult`.

    init_sym is the symbol used for the initial breakpoint used to
    configure the run.  For example: <target>!main

    memory_limit is the memory limit in bytes that the application
    should not exceed.

    TODO: Add support for callback

    Availability: Windows.
    """

    if callback is not None: #TODO: Add support for callback
        raise RuntimeError("callback support not implemented")
    if not isinstance(init_sym, str):
        raise TypeError(type(init_sym).__name__ % "init_sym must be of type str not %s")
    if not isinstance(memory_limit, int):
        raise TypeError("memory_limit be of type int not %s" % type(memory_limit).__name__)

    if env is None:
        env = dict(os.environ)
    env["LIBC_FATAL_STDERR_"] = "1"
    if symbols is not None:
        env["_NT_ALT_SYMBOL_PATH"] = symbols
    elif target_cmd:
        env["_NT_ALT_SYMBOL_PATH"] = os.path.abspath(os.path.dirname(target_cmd[0]))

    dbg_script = os.path.join(_common.PATH_DBG, "scripts", "WinDBGMemoryLimit.py")
    cdb_cmd = [TOOL_CDB, "-x", "-y",
               "SRV*c:\\websymbols*http://msdl.microsoft.com/download/symbols",
               "-c", ".load pykd.pyd;!py %s -l %d -s %s;q" % (dbg_script, memory_limit, init_sym)]

    gflags_args = _common._disable_gflags(target_cmd[0])
    with tempfile.TemporaryFile(mode="w+t") as f:
        p = subprocess.Popen(cdb_cmd + target_cmd, stderr=f, stdout=f, env=env,
                             stdin=subprocess.PIPE, creationflags=_common.POPEN_FLAGS)
        timeout_time = time.time() + timeout
        while p.poll() is None:
            if time.time() > timeout_time:
                with open(os.devnull, "w") as fp:
                    subprocess.call(["taskkill", "/pid", str(p.pid), "/f"], stdout=fp, stderr=fp)
                p.wait()
            time.sleep(0.5) # no hurry, less cpu hogging is better

        f.seek(0, os.SEEK_SET)
        output = []
        for line in f.readlines():
            if not line.strip().startswith("Breakpoint"):
                output.append(line)
    if gflags_args is not None:
        _common._set_gflags(target_cmd[0], **gflags_args)
    output = ''.join(output)
    has_tb = output.find("Traceback (most recent call last):")
    if has_tb != -1:
        raise RuntimeError("CDB Python Failure\n%s\n%s" % ("-"*80, output[has_tb:]))
    call_stack = _common.process_exploitable_output(output)[0]
    if not call_stack:
        return _common.FuzzResult(text=output)
    return _common.FuzzResult(classification=_common.EXCESS_MEMORY_USAGE,
                              text=output,
                              backtrace=call_stack)
dataset/ETHPy150Open blackberry/ALF/alf/debug/_cdb.py/trace_memory_usage_with_windbg/swapped operands of `%`