Esempio n. 1
0
def JavaRewrite(text: str) -> str:
    """Run the Java rewriter on the text.

  Args:
    text: The source code to rewrite.

  Returns:
    Source code with identifier names normalized.

  Raises:
    RewriterError: If rewriter found nothing to rewrite.
    ClangTimeout: If rewriter fails to complete within timeout_seconds.
  """
    cmd = ["timeout", "-s9", "60", str(JAVA_REWRITER)]
    process = subprocess.Popen(
        cmd,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        universal_newlines=True,
    )
    app.Log(2, "$ %s", " ".join(cmd))
    stdout, stderr = process.communicate(text)
    if process.returncode == 9:
        raise errors.RewriterException(
            "JavaRewriter failed to complete after 60s")
    elif process.returncode:
        raise errors.RewriterException(stderr)
    return stdout.strip() + "\n"
Esempio n. 2
0
def JavaRewrite(text: str) -> str:
    """Run the Java rewriter on the text.

  Args:
    text: The source code to rewrite.

  Returns:
    Source code with identifier names normalized.

  Raises:
    RewriterError: If rewriter found nothing to rewrite.
    ClangTimeout: If rewriter fails to complete within timeout_seconds.
  """
    cmd = ['timeout', '-s9', '60', str(JAVA_REWRITER)]
    process = subprocess.Popen(cmd,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               universal_newlines=True)
    logging.debug('$ %s', ' '.join(cmd))
    stdout, stderr = process.communicate(text)
    if process.returncode == 9:
        raise errors.RewriterException(
            'JavaRewriter failed to complete after 60s')
    elif process.returncode:
        raise errors.RewriterException(stderr)
    return stdout.strip() + '\n'
Esempio n. 3
0
def NormalizeIdentifiers(text: str,
                         suffix: str,
                         cflags: typing.List[str],
                         timeout_seconds: int = 60) -> str:
    """Normalize identifiers in source code.

  An LLVM rewriter pass which renames all functions and variables with short,
  unique names. The variables and functions defined within the input text
  are rewritten, with the sequence 'A', 'B', ... 'AA', 'AB'... being used for
  function names, and the sequence 'a', 'b', ... 'aa', 'ab'... being used for
  variable names. Functions and variables which are defined in #include files
  are not renamed. Undefined function and variable names are not renamed.

  Args:
    text: The source code to rewrite.
    suffix: The suffix to append to the source code temporary file. E.g. '.c'
      for a C program.
    cflags: A list of flags to be passed to clang.
    timeout_seconds: The number of seconds to allow before killing the rewriter.

  Returns:
    Source code with identifier names normalized.

  Raises:
    RewriterException: If rewriter found nothing to rewrite.
    ClangTimeout: If rewriter fails to complete within timeout_seconds.
  """
    with tempfile.NamedTemporaryFile('w', suffix=suffix) as f:
        f.write(text)
        f.flush()
        cmd = [
            "timeout", "-s9",
            str(timeout_seconds),
            str(CLGEN_REWRITER), f.name
        ] + ['-extra-arg=' + x for x in cflags] + ['--']
        logging.debug(
            '$ %s%s', f'LD_PRELOAD={CLGEN_REWRITER_ENV["LD_PRELOAD"]} '
            if 'LD_PRELOAD' in CLGEN_REWRITER_ENV else '', ' '.join(cmd))
        process = subprocess.Popen(cmd,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   universal_newlines=True,
                                   env=CLGEN_REWRITER_ENV)
        stdout, stderr = process.communicate()
        logging.debug(stderr)
    # If there was nothing to rewrite, rewriter exits with error code:
    EUGLY_CODE = 204
    if process.returncode == EUGLY_CODE:
        # Propagate the error:
        raise errors.RewriterException(stderr)
    elif process.returncode == 9:
        raise errors.ClangTimeout(
            f'clang_rewriter failed to complete after {timeout_seconds}s')
    # The rewriter process can still fail because of some other compilation
    # problem, e.g. for some reason the 'enable 64bit support' pragma which should
    # be included in the shim isn't being propogated correctly to the rewriter.
    # However, the rewriter will still correctly process the input, so we ignore
    # all error codes except the one we care about (EUGLY_CODE).
    return stdout