# Path: src/funfuzz/util/create_collector.py
# def make_collector():
# def printCrashInfo(crashInfo):  # pylint: disable=invalid-name,missing-docstring
# def printMatchingSignature(match):  # pylint: disable=invalid-name,missing-docstring
# def submit_collector(collector, crash_info, testcase, quality, meta_data=None):
# 
# Path: src/funfuzz/util/lithium_helpers.py
# (HAPPY, LITH_NO_REPRO, LITH_FINISHED, LITH_RETESTED_STILL_INTERESTING, LITH_BUSTED) = range(5)
# def pinpoint(itest, logPrefix, jsEngine, engineFlags, infilename,  # pylint: disable=invalid-name,missing-param-doc
#              bisectRepo, build_options_str, targetTime, suspiciousLevel):
# def run_lithium(lithArgs, logPrefix, targetTime):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc
# def readLithiumResult(lithlogfn):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def reduction_strat(logPrefix, infilename, lithArgs, targetTime, lev):  # pylint: disable=invalid-name
#     def lith_reduce(strategy):
# 
# Path: src/funfuzz/util/os_ops.py
# NO_DUMP_MSG = r"""
# WARNING: Minidumps are not being generated, so all crashes will be uninteresting.
# WARNING: Make sure the following key value exists in this key:
# WARNING: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps
# WARNING: Name: DumpType  Type: REG_DWORD
# WARNING: http://msdn.microsoft.com/en-us/library/windows/desktop/bb787181%28v=vs.85%29.aspx
# """
# def make_cdb_cmd(prog_full_path, crashed_pid):
# def make_gdb_cmd(prog_full_path, crashed_pid):
# def disable_corefile():
# def get_core_limit():
# def make_dbg_cmd(prog_full_path, crashed_pid):
# def grab_crash_log(prog_full_path, crashed_pid, log_prefix, want_stack):
# def grab_mac_crash_log(crash_pid, log_prefix, use_log_files):
# def is_win_dumping_to_default():  # pylint: disable=too-complex
# def make_wtmp_dir(base_dir):


import argparse
import io
import json
import logging
import os
import platform
import subprocess
import sys
import time
import zipfile
from pathlib import Path
from textwrap import dedent
from . import compare_jit
from . import js_interesting
from . import link_fuzzer
from . import shell_flags
from . import with_binaryen
from ..util import create_collector
from ..util import file_manipulation
from ..util import file_system_helpers
from ..util import lithium_helpers
from ..util import os_ops
    js_interesting_opts.jsengineWithArgs = js_interesting_opts.jsengineWithArgs[:-2] + [str(wrapper_file),
                                                                                        str(wasm_file)]
    # Ensure ion flags such as --execute="setJitCompilerOption(\"ion.forceinlineCaches\",1)" are not executed
    # for wasm files
    execute_ion_flags_in_shell = False
    for runtime_flag in js_interesting_opts.jsengineWithArgs:
        if "--execute=" in str(runtime_flag) and "ion." in str(runtime_flag):
            execute_ion_flags_in_shell = True

    if not execute_ion_flags_in_shell:
        res = js_interesting.ShellResult(js_interesting_opts,
                                         js_interesting_opts.jsengineWithArgs, log_prefix, False, env=env)

        if res.lev >= js_interesting.JS_OVERALL_MISMATCH:
            wasm_out_log = (log_prefix.parent / f"{log_prefix.stem}-out").with_suffix(".txt")
            showtail(wasm_out_log)  # wasm_out_log appears again after js_interesting.ShellResult is run
            wasm_err_log = (log_prefix.parent / f"{log_prefix.stem}-err").with_suffix(".txt")
            showtail(wasm_err_log)

            # binaryen integration, we do not yet have pinpoint nor autobisectjs support, so temporarily quality 10
            assert wrapper_file.is_file()
            result_zip = log_prefix.parent / "reduced.zip"
            with zipfile.ZipFile(result_zip, "w") as f:
                f.write(out_log, f"{out_log.name}-binaryen-v{with_binaryen.BINARYEN_VERSION}-seed",
                        compress_type=zipfile.ZIP_DEFLATED)
                f.write(wrapper_file, wrapper_file.name, compress_type=zipfile.ZIP_DEFLATED)
                f.write(wasm_file, wasm_file.name, compress_type=zipfile.ZIP_DEFLATED)

            if not ccoverage:
                # Quality is 10, meta_data {}

# Path: src/funfuzz/util/create_collector.py
# def make_collector():
# def printCrashInfo(crashInfo):  # pylint: disable=invalid-name,missing-docstring
# def printMatchingSignature(match):  # pylint: disable=invalid-name,missing-docstring
# def submit_collector(collector, crash_info, testcase, quality, meta_data=None):
# 
# Path: src/funfuzz/util/lithium_helpers.py
# (HAPPY, LITH_NO_REPRO, LITH_FINISHED, LITH_RETESTED_STILL_INTERESTING, LITH_BUSTED) = range(5)
# def pinpoint(itest, logPrefix, jsEngine, engineFlags, infilename,  # pylint: disable=invalid-name,missing-param-doc
#              bisectRepo, build_options_str, targetTime, suspiciousLevel):
# def run_lithium(lithArgs, logPrefix, targetTime):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc
# def readLithiumResult(lithlogfn):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def reduction_strat(logPrefix, infilename, lithArgs, targetTime, lev):  # pylint: disable=invalid-name
#     def lith_reduce(strategy):
# 
# Path: src/funfuzz/util/os_ops.py
# NO_DUMP_MSG = r"""
# WARNING: Minidumps are not being generated, so all crashes will be uninteresting.
# WARNING: Make sure the following key value exists in this key:
# WARNING: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps
# WARNING: Name: DumpType  Type: REG_DWORD
# WARNING: http://msdn.microsoft.com/en-us/library/windows/desktop/bb787181%28v=vs.85%29.aspx
# """
# def make_cdb_cmd(prog_full_path, crashed_pid):
# def make_gdb_cmd(prog_full_path, crashed_pid):
# def disable_corefile():
# def get_core_limit():
# def make_dbg_cmd(prog_full_path, crashed_pid):
# def grab_crash_log(prog_full_path, crashed_pid, log_prefix, want_stack):
# def grab_mac_crash_log(crash_pid, log_prefix, use_log_files):
# def is_win_dumping_to_default():  # pylint: disable=too-complex
# def make_wtmp_dir(base_dir):


# Path: src/funfuzz/js/loop.py
import argparse
import io
import json
import logging
import os
import platform
import subprocess
import sys
import time
import zipfile
from pathlib import Path
from textwrap import dedent
from . import compare_jit
from . import js_interesting
from . import link_fuzzer
from . import shell_flags
from . import with_binaryen
from ..util import create_collector
from ..util import file_manipulation
from ..util import file_system_helpers
from ..util import lithium_helpers
from ..util import os_ops

    js_interesting_opts.jsengineWithArgs = js_interesting_opts.jsengineWithArgs[:-2] + [str(wrapper_file),
                                                                                        str(wasm_file)]
    # Ensure ion flags such as --execute="setJitCompilerOption(\"ion.forceinlineCaches\",1)" are not executed
    # for wasm files
    execute_ion_flags_in_shell = False
    for runtime_flag in js_interesting_opts.jsengineWithArgs:
        if "--execute=" in str(runtime_flag) and "ion." in str(runtime_flag):
            execute_ion_flags_in_shell = True

    if not execute_ion_flags_in_shell:
        res = js_interesting.ShellResult(js_interesting_opts,
                                         js_interesting_opts.jsengineWithArgs, log_prefix, False, env=env)

        if res.lev >= js_interesting.JS_OVERALL_MISMATCH:
            wasm_out_log = (log_prefix.parent / f"{log_prefix.stem}-out").with_suffix(".txt")
            showtail(wasm_out_log)  # wasm_out_log appears again after js_interesting.ShellResult is run
            wasm_err_log = (log_prefix.parent / f"{log_prefix.stem}-err").with_suffix(".txt")
            showtail(wasm_err_log)

            # binaryen integration, we do not yet have pinpoint nor autobisectjs support, so temporarily quality 10
            assert wrapper_file.is_file()
            result_zip = log_prefix.parent / "reduced.zip"
            with zipfile.ZipFile(result_zip, "w") as f:
                f.write(out_log, f"{out_log.name}-binaryen-v{with_binaryen.BINARYEN_VERSION}-seed",
                        compress_type=zipfile.ZIP_DEFLATED)
                f.write(wrapper_file, wrapper_file.name, compress_type=zipfile.ZIP_DEFLATED)
                f.write(wasm_file, wasm_file.name, compress_type=zipfile.ZIP_DEFLATED)

            if not ccoverage:
                # Quality is 10, meta_data {}

                create_collector.submit_collector(collector, res.crashInfo, str(result_zip), 10, meta_data={})