2022-03-03 14:05:26 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-11-18 10:36:23 +00:00
|
|
|
# Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
# This is main driver for gcmole tool. See README for more details.
|
|
|
|
# Usage: CLANG_BIN=clang-bin-dir python tools/gcmole/gcmole.py [arm|arm64|ia32|x64]
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
from multiprocessing import cpu_count
|
2022-04-04 09:56:16 +00:00
|
|
|
from pathlib import Path
|
2022-03-03 14:05:26 +00:00
|
|
|
|
2020-11-26 08:26:39 +00:00
|
|
|
import collections
|
|
|
|
import difflib
|
2022-03-03 21:38:39 +00:00
|
|
|
import json
|
2022-03-03 14:05:26 +00:00
|
|
|
import optparse
|
2020-11-18 10:36:23 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
2020-11-26 08:26:39 +00:00
|
|
|
import sys
|
2020-11-26 13:43:44 +00:00
|
|
|
import threading
|
2022-04-04 09:56:16 +00:00
|
|
|
import queue
|
2022-03-03 14:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
ArchCfg = collections.namedtuple(
|
|
|
|
"ArchCfg", ["name", "cpu", "triple", "arch_define", "arch_options"])
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2022-03-03 21:38:39 +00:00
|
|
|
# TODO(cbruni): use gn desc by default for platform-specific settings
|
|
|
|
OPTIONS_64BIT = [
|
|
|
|
"-DV8_COMPRESS_POINTERS",
|
|
|
|
"-DV8_COMPRESS_POINTERS_IN_SHARED_CAGE",
|
|
|
|
"-DV8_EXTERNAL_CODE_SPACE",
|
|
|
|
"-DV8_SHORT_BUILTIN_CALLS",
|
|
|
|
"-DV8_SHARED_RO_HEAP",
|
|
|
|
]
|
|
|
|
|
2020-11-18 10:36:23 +00:00
|
|
|
ARCHITECTURES = {
|
|
|
|
"ia32":
|
|
|
|
ArchCfg(
|
2022-03-03 14:05:26 +00:00
|
|
|
name="ia32",
|
|
|
|
cpu="x86",
|
2020-11-18 10:36:23 +00:00
|
|
|
triple="i586-unknown-linux",
|
|
|
|
arch_define="V8_TARGET_ARCH_IA32",
|
|
|
|
arch_options=["-m32"],
|
|
|
|
),
|
|
|
|
"arm":
|
|
|
|
ArchCfg(
|
2022-03-03 14:05:26 +00:00
|
|
|
name="arm",
|
|
|
|
cpu="arm",
|
2020-11-18 10:36:23 +00:00
|
|
|
triple="i586-unknown-linux",
|
|
|
|
arch_define="V8_TARGET_ARCH_ARM",
|
|
|
|
arch_options=["-m32"],
|
|
|
|
),
|
2022-03-03 21:38:39 +00:00
|
|
|
# TODO(cbruni): Use detailed settings:
|
|
|
|
# arch_options = OPTIONS_64BIT + [ "-DV8_WIN64_UNWINDING_INFO" ]
|
2020-11-18 10:36:23 +00:00
|
|
|
"x64":
|
|
|
|
ArchCfg(
|
2022-03-03 14:05:26 +00:00
|
|
|
name="x64",
|
|
|
|
cpu="x64",
|
2020-11-18 10:36:23 +00:00
|
|
|
triple="x86_64-unknown-linux",
|
|
|
|
arch_define="V8_TARGET_ARCH_X64",
|
2022-03-03 21:38:39 +00:00
|
|
|
arch_options=[]),
|
2020-11-18 10:36:23 +00:00
|
|
|
"arm64":
|
|
|
|
ArchCfg(
|
2022-03-03 14:05:26 +00:00
|
|
|
name="arm64",
|
|
|
|
cpu="arm64",
|
2020-11-18 10:36:23 +00:00
|
|
|
triple="x86_64-unknown-linux",
|
|
|
|
arch_define="V8_TARGET_ARCH_ARM64",
|
|
|
|
arch_options=[],
|
|
|
|
),
|
|
|
|
}
|
2022-03-03 14:05:26 +00:00
|
|
|
ARCHITECTURES['x86'] = ARCHITECTURES['ia32']
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def log(format, *args, **kwargs):
|
|
|
|
mark = ("#", "=", "-", ".")[kwargs.get("level", 0)]
|
|
|
|
print(mark * 2, str(format).format(*list(map(str, args))))
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def fatal(format):
|
|
|
|
log(format)
|
2020-11-18 10:36:23 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Clang invocation
|
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def make_clang_command_line(plugin, plugin_args, options):
|
|
|
|
arch_cfg = ARCHITECTURES[options.v8_target_cpu]
|
2020-11-18 10:36:23 +00:00
|
|
|
prefixed_plugin_args = []
|
|
|
|
if plugin_args:
|
|
|
|
for arg in plugin_args:
|
|
|
|
prefixed_plugin_args += [
|
|
|
|
"-Xclang",
|
2022-03-03 14:05:26 +00:00
|
|
|
"-plugin-arg-" + plugin,
|
2020-11-18 10:36:23 +00:00
|
|
|
"-Xclang",
|
|
|
|
arg,
|
|
|
|
]
|
2022-03-03 14:05:26 +00:00
|
|
|
log("Using generated files in {}", options.v8_build_dir / 'gen')
|
|
|
|
icu_src_dir = options.v8_root_dir / 'third_party/icu/source'
|
2020-11-18 10:36:23 +00:00
|
|
|
return ([
|
2022-03-03 14:05:26 +00:00
|
|
|
options.clang_bin_dir / "clang++",
|
2022-03-03 21:38:39 +00:00
|
|
|
"-std=c++17",
|
2020-11-18 10:36:23 +00:00
|
|
|
"-c",
|
|
|
|
"-Xclang",
|
|
|
|
"-load",
|
|
|
|
"-Xclang",
|
2022-03-03 14:05:26 +00:00
|
|
|
options.clang_plugins_dir / "libgcmole.so",
|
2020-11-18 10:36:23 +00:00
|
|
|
"-Xclang",
|
|
|
|
"-plugin",
|
|
|
|
"-Xclang",
|
|
|
|
plugin,
|
|
|
|
] + prefixed_plugin_args + [
|
|
|
|
"-Xclang",
|
|
|
|
"-triple",
|
|
|
|
"-Xclang",
|
|
|
|
arch_cfg.triple,
|
|
|
|
"-fno-exceptions",
|
2022-03-03 21:38:39 +00:00
|
|
|
"-Wno-everything",
|
2020-11-18 10:36:23 +00:00
|
|
|
"-D",
|
|
|
|
arch_cfg.arch_define,
|
|
|
|
"-DENABLE_DEBUGGER_SUPPORT",
|
2021-03-01 16:41:31 +00:00
|
|
|
"-DV8_ENABLE_WEBASSEMBLY",
|
2022-03-03 21:38:39 +00:00
|
|
|
"-DV8_GC_MOLE",
|
|
|
|
"-DV8_INTL_SUPPORT",
|
2022-03-03 14:05:26 +00:00
|
|
|
"-I{}".format(options.v8_root_dir),
|
|
|
|
"-I{}".format(options.v8_root_dir / 'include'),
|
|
|
|
"-I{}".format(options.v8_build_dir / 'gen'),
|
|
|
|
"-I{}".format(icu_src_dir / 'common'),
|
|
|
|
"-I{}".format(icu_src_dir / 'i18n'),
|
2020-11-18 10:36:23 +00:00
|
|
|
] + arch_cfg.arch_options)
|
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def invoke_clang_plugin_for_file(filename, cmd_line, verbose):
|
|
|
|
args = cmd_line + [filename]
|
|
|
|
args = list(map(str, args))
|
2020-11-26 13:43:44 +00:00
|
|
|
if verbose:
|
2022-03-03 14:05:26 +00:00
|
|
|
print("popen ", " ".join(args))
|
|
|
|
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2020-11-26 13:43:44 +00:00
|
|
|
stdout, stderr = p.communicate()
|
2022-03-03 14:05:26 +00:00
|
|
|
return p.returncode, stdout.decode("utf-8"), stderr.decode("utf-8")
|
2020-11-26 13:43:44 +00:00
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def invoke_clang_plugin_for_files_in_queue(i, input_queue, output_queue,
|
|
|
|
cancel_event, cmd_line, verbose):
|
2020-11-26 13:43:44 +00:00
|
|
|
success = False
|
2020-11-18 10:36:23 +00:00
|
|
|
try:
|
2020-11-26 13:43:44 +00:00
|
|
|
while not cancel_event.is_set():
|
|
|
|
filename = input_queue.get_nowait()
|
2022-03-03 14:05:26 +00:00
|
|
|
ret, stdout, stderr = invoke_clang_plugin_for_file(
|
|
|
|
filename, cmd_line, verbose)
|
|
|
|
output_queue.put_nowait((filename, ret, stdout, stderr))
|
2020-11-26 13:43:44 +00:00
|
|
|
if ret != 0:
|
|
|
|
break
|
2020-11-18 10:36:23 +00:00
|
|
|
except KeyboardInterrupt:
|
2022-03-03 14:05:26 +00:00
|
|
|
log("[{}] Interrupting", i, level=1)
|
2020-11-26 13:43:44 +00:00
|
|
|
except queue.Empty:
|
|
|
|
success = True
|
|
|
|
finally:
|
|
|
|
# Emit a success bool so that the reader knows that there was either an
|
|
|
|
# error or all files were processed.
|
|
|
|
output_queue.put_nowait(success)
|
|
|
|
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def invoke_clang_plugin_for_each_file(filenames, plugin, plugin_args, options):
|
|
|
|
cmd_line = make_clang_command_line(plugin, plugin_args, options)
|
|
|
|
verbose = options.verbose
|
|
|
|
if options.sequential:
|
|
|
|
log("Sequential execution.")
|
2020-11-18 10:36:23 +00:00
|
|
|
for filename in filenames:
|
2022-03-03 14:05:26 +00:00
|
|
|
log(filename, level=1)
|
|
|
|
returncode, stdout, stderr = invoke_clang_plugin_for_file(
|
2020-11-26 13:43:44 +00:00
|
|
|
filename, cmd_line, verbose)
|
2020-11-18 10:36:23 +00:00
|
|
|
if returncode != 0:
|
|
|
|
sys.stderr.write(stderr)
|
|
|
|
sys.exit(returncode)
|
2020-11-26 13:43:44 +00:00
|
|
|
yield filename, stdout, stderr
|
2020-11-18 10:36:23 +00:00
|
|
|
else:
|
2022-03-03 14:05:26 +00:00
|
|
|
log("Parallel execution.")
|
2020-11-26 13:43:44 +00:00
|
|
|
cpus = cpu_count()
|
|
|
|
input_queue = queue.Queue()
|
|
|
|
output_queue = queue.Queue()
|
|
|
|
threads = []
|
2020-11-18 10:36:23 +00:00
|
|
|
try:
|
|
|
|
for filename in filenames:
|
2020-11-26 13:43:44 +00:00
|
|
|
input_queue.put(filename)
|
|
|
|
|
|
|
|
cancel_event = threading.Event()
|
|
|
|
|
|
|
|
for i in range(min(len(filenames), cpus)):
|
|
|
|
threads.append(
|
|
|
|
threading.Thread(
|
2022-03-03 14:05:26 +00:00
|
|
|
target=invoke_clang_plugin_for_files_in_queue,
|
2020-11-26 13:43:44 +00:00
|
|
|
args=(i, input_queue, output_queue, cancel_event, cmd_line,
|
|
|
|
verbose)))
|
|
|
|
|
|
|
|
for t in threads:
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
num_finished = 0
|
|
|
|
while num_finished < len(threads):
|
|
|
|
output = output_queue.get()
|
|
|
|
if type(output) == bool:
|
|
|
|
if output:
|
|
|
|
num_finished += 1
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
filename, returncode, stdout, stderr = output
|
2022-03-03 21:38:39 +00:00
|
|
|
log(filename, level=2)
|
2020-11-18 10:36:23 +00:00
|
|
|
if returncode != 0:
|
|
|
|
sys.stderr.write(stderr)
|
|
|
|
sys.exit(returncode)
|
2020-11-26 13:43:44 +00:00
|
|
|
yield filename, stdout, stderr
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2020-11-26 13:43:44 +00:00
|
|
|
finally:
|
|
|
|
cancel_event.set()
|
|
|
|
for t in threads:
|
|
|
|
t.join()
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def parse_gn_file(options, for_test):
|
2020-11-18 10:36:23 +00:00
|
|
|
if for_test:
|
2022-03-03 14:05:26 +00:00
|
|
|
return {"all": [options.v8_root_dir / "tools/gcmole/gcmole-test.cc"]}
|
|
|
|
result = {}
|
|
|
|
gn_files = [
|
|
|
|
("BUILD.gn", re.compile('"([^"]*?\.cc)"'), ""),
|
|
|
|
("test/cctest/BUILD.gn", re.compile('"(test-[^"]*?\.cc)"'),
|
|
|
|
Path("test/cctest/")),
|
|
|
|
]
|
2020-11-18 10:36:23 +00:00
|
|
|
for filename, pattern, prefix in gn_files:
|
2022-03-03 14:05:26 +00:00
|
|
|
path = options.v8_root_dir / filename
|
|
|
|
with open(path) as gn_file:
|
2020-11-18 10:36:23 +00:00
|
|
|
gn = gn_file.read()
|
|
|
|
for condition, sources in re.findall("### gcmole\((.*?)\) ###(.*?)\]", gn,
|
|
|
|
re.MULTILINE | re.DOTALL):
|
|
|
|
if condition not in result:
|
|
|
|
result[condition] = []
|
|
|
|
for file in pattern.findall(sources):
|
2022-03-03 14:05:26 +00:00
|
|
|
result[condition].append(options.v8_root_dir / prefix / file)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def evaluate_condition(cond, props):
|
2020-11-18 10:36:23 +00:00
|
|
|
if cond == "all":
|
|
|
|
return True
|
|
|
|
|
|
|
|
m = re.match("(\w+):(\w+)", cond)
|
|
|
|
if m is None:
|
|
|
|
fatal("failed to parse condition: {}", cond)
|
|
|
|
p, v = m.groups()
|
|
|
|
if p not in props:
|
|
|
|
fatal("undefined configuration property: {}", p)
|
|
|
|
|
|
|
|
return props[p] == v
|
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def build_file_list(options, for_test):
|
|
|
|
sources = parse_gn_file(options, for_test)
|
|
|
|
props = {
|
2020-11-18 10:36:23 +00:00
|
|
|
"os": "linux",
|
2022-03-03 14:05:26 +00:00
|
|
|
"arch": options.v8_target_cpu,
|
2020-11-18 10:36:23 +00:00
|
|
|
"mode": "debug",
|
|
|
|
"simulator": ""
|
2022-03-03 14:05:26 +00:00
|
|
|
}
|
|
|
|
ret = []
|
|
|
|
for condition, files in list(sources.items()):
|
|
|
|
if evaluate_condition(condition, props):
|
|
|
|
ret += files
|
|
|
|
return ret
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# GCSuspects Generation
|
|
|
|
|
|
|
|
# Note that the gcsuspects file lists functions in the form:
|
|
|
|
# mangled_name,unmangled_function_name
|
|
|
|
#
|
|
|
|
# This means that we can match just the function name by matching only
|
|
|
|
# after a comma.
|
2020-11-26 13:43:44 +00:00
|
|
|
ALLOWLIST = [
|
2020-11-18 10:36:23 +00:00
|
|
|
# The following functions call CEntryStub which is always present.
|
|
|
|
"MacroAssembler.*,CallRuntime",
|
|
|
|
"CompileCallLoadPropertyWithInterceptor",
|
|
|
|
"CallIC.*,GenerateMiss",
|
|
|
|
# DirectCEntryStub is a special stub used on ARM.
|
|
|
|
# It is pinned and always present.
|
|
|
|
"DirectCEntryStub.*,GenerateCall",
|
|
|
|
# TODO GCMole currently is sensitive enough to understand that certain
|
|
|
|
# functions only cause GC and return Failure simulataneously.
|
|
|
|
# Callsites of such functions are safe as long as they are properly
|
|
|
|
# check return value and propagate the Failure to the caller.
|
|
|
|
# It should be possible to extend GCMole to understand this.
|
|
|
|
"Heap.*,TryEvacuateObject",
|
|
|
|
# Ignore all StateTag methods.
|
|
|
|
"StateTag",
|
|
|
|
# Ignore printing of elements transition.
|
|
|
|
"PrintElementsTransition",
|
|
|
|
# CodeCreateEvent receives AbstractCode (a raw ptr) as an argument.
|
|
|
|
"CodeCreateEvent",
|
|
|
|
"WriteField",
|
2020-11-26 13:43:44 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
GC_PATTERN = ",.*Collect.*Garbage"
|
2021-02-25 16:17:59 +00:00
|
|
|
SAFEPOINT_PATTERN = ",SafepointSlowPath"
|
2022-03-03 14:05:26 +00:00
|
|
|
ALLOWLIST_PATTERN = "|".join("(?:{})".format(p) for p in ALLOWLIST)
|
2020-11-26 13:43:44 +00:00
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def merge_regexp(pattern_dict):
|
|
|
|
return re.compile("|".join("(?P<{}>{})".format(key, value)
|
|
|
|
for (key, value) in list(pattern_dict.items())))
|
2020-11-26 13:43:44 +00:00
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
IS_SPECIAL_WITHOUT_ALLOW_LIST = merge_regexp({
|
2020-11-26 13:43:44 +00:00
|
|
|
"gc": GC_PATTERN,
|
|
|
|
"safepoint": SAFEPOINT_PATTERN
|
|
|
|
})
|
2022-03-03 14:05:26 +00:00
|
|
|
IS_SPECIAL_WITH_ALLOW_LIST = merge_regexp({
|
2020-11-26 13:43:44 +00:00
|
|
|
"gc": GC_PATTERN,
|
|
|
|
"safepoint": SAFEPOINT_PATTERN,
|
|
|
|
"allow": ALLOWLIST_PATTERN
|
|
|
|
})
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GCSuspectsCollector:
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def __init__(self, options):
|
2020-11-18 10:36:23 +00:00
|
|
|
self.gc = {}
|
2022-03-03 14:05:26 +00:00
|
|
|
self.gc_caused = collections.defaultdict(lambda: set())
|
2020-11-18 10:36:23 +00:00
|
|
|
self.funcs = {}
|
2020-11-26 13:43:44 +00:00
|
|
|
self.current_caller = None
|
2022-03-03 14:05:26 +00:00
|
|
|
self.allowlist = options.allowlist
|
2020-11-26 13:43:44 +00:00
|
|
|
self.is_special = IS_SPECIAL_WITH_ALLOW_LIST if self.allowlist else IS_SPECIAL_WITHOUT_ALLOW_LIST
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def add_cause(self, name, cause):
|
|
|
|
self.gc_caused[name].add(cause)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def parse(self, lines):
|
2020-11-18 10:36:23 +00:00
|
|
|
for funcname in lines:
|
|
|
|
if not funcname:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if funcname[0] != "\t":
|
2022-03-03 14:05:26 +00:00
|
|
|
self.resolve(funcname)
|
2020-11-26 13:43:44 +00:00
|
|
|
self.current_caller = funcname
|
2020-11-18 10:36:23 +00:00
|
|
|
else:
|
|
|
|
name = funcname[1:]
|
2022-03-03 14:05:26 +00:00
|
|
|
callers_for_name = self.resolve(name)
|
2020-11-26 13:43:44 +00:00
|
|
|
callers_for_name.add(self.current_caller)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def resolve(self, name):
|
2020-11-18 10:36:23 +00:00
|
|
|
if name not in self.funcs:
|
2020-11-26 13:43:44 +00:00
|
|
|
self.funcs[name] = set()
|
|
|
|
m = self.is_special.search(name)
|
|
|
|
if m:
|
|
|
|
if m.group("gc"):
|
|
|
|
self.gc[name] = True
|
2022-03-03 14:05:26 +00:00
|
|
|
self.add_cause(name, "<GC>")
|
2020-11-26 13:43:44 +00:00
|
|
|
elif m.group("safepoint"):
|
|
|
|
self.gc[name] = True
|
2022-03-03 14:05:26 +00:00
|
|
|
self.add_cause(name, "<Safepoint>")
|
2020-11-26 13:43:44 +00:00
|
|
|
elif m.group("allow"):
|
|
|
|
self.gc[name] = False
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
return self.funcs[name]
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def propagate(self):
|
|
|
|
log("Propagating GC information")
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
def mark(funcname, callers):
|
|
|
|
for caller in callers:
|
|
|
|
if caller not in self.gc:
|
|
|
|
self.gc[caller] = True
|
|
|
|
mark(caller, self.funcs[caller])
|
2022-03-03 14:05:26 +00:00
|
|
|
self.add_cause(caller, funcname)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
for funcname, callers in list(self.funcs.items()):
|
2020-11-18 10:36:23 +00:00
|
|
|
if self.gc.get(funcname, False):
|
|
|
|
mark(funcname, callers)
|
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def generate_gc_suspects(files, options):
|
2020-11-18 10:36:23 +00:00
|
|
|
# Reset the global state.
|
2022-03-03 14:05:26 +00:00
|
|
|
collector = GCSuspectsCollector(options)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
log("Building GC Suspects for {}", options.v8_target_cpu)
|
|
|
|
for _, stdout, _ in invoke_clang_plugin_for_each_file(files, "dump-callees",
|
|
|
|
[], options):
|
|
|
|
collector.parse(stdout.splitlines())
|
|
|
|
collector.propagate()
|
|
|
|
# TODO(cbruni): remove once gcmole.cc is migrated
|
2022-03-03 21:38:39 +00:00
|
|
|
write_gcmole_results(collector, options, options.v8_root_dir)
|
|
|
|
write_gcmole_results(collector, options, options.out_dir)
|
|
|
|
|
|
|
|
|
|
|
|
def write_gcmole_results(collector, options, dst):
|
|
|
|
# gcsuspects contains a list("mangled_full_name,name") of all functions that
|
|
|
|
# could cause a gc (directly or indirectly).
|
|
|
|
#
|
|
|
|
# EXAMPLE
|
|
|
|
# _ZN2v88internal4Heap16CreateApiObjectsEv,CreateApiObjects
|
|
|
|
# _ZN2v88internal4Heap17CreateInitialMapsEv,CreateInitialMaps
|
|
|
|
# ...
|
2022-03-03 14:05:26 +00:00
|
|
|
with open(dst / "gcsuspects", "w") as out:
|
2022-03-03 21:38:39 +00:00
|
|
|
for name, value in list(collector.gc.items()):
|
2020-11-18 10:36:23 +00:00
|
|
|
if value:
|
|
|
|
out.write(name + "\n")
|
2022-03-03 21:38:39 +00:00
|
|
|
# gccauses contains a map["mangled_full_name,name"] => list(inner gcsuspects)
|
|
|
|
# Where the inner gcsuspects are functions directly called in the outer
|
|
|
|
# function that can cause a gc. The format is encoded for simplified
|
|
|
|
# deserialization in gcmole.cc.
|
|
|
|
#
|
|
|
|
# EXAMPLE:
|
|
|
|
# _ZN2v88internal4Heap17CreateHeapObjectsEv,CreateHeapObjects
|
|
|
|
# start,nested
|
|
|
|
# _ZN2v88internal4Heap16CreateApiObjectsEv,CreateApiObjects
|
|
|
|
# _ZN2v88internal4Heap17CreateInitialMapsEv,CreateInitialMaps
|
|
|
|
# ...
|
|
|
|
# end,nested
|
|
|
|
# ...
|
2022-03-03 14:05:26 +00:00
|
|
|
with open(dst / "gccauses", "w") as out:
|
2022-03-03 21:38:39 +00:00
|
|
|
for name, causes in list(collector.gc_caused.items()):
|
|
|
|
out.write("{}\n".format(name))
|
|
|
|
out.write("start,nested\n")
|
2020-11-18 10:36:23 +00:00
|
|
|
for cause in causes:
|
2022-03-03 21:38:39 +00:00
|
|
|
out.write("{}\n".format(cause))
|
|
|
|
out.write("end,nested\n")
|
|
|
|
log("GCSuspects and gccauses generated for {} in '{}'", options.v8_target_cpu,
|
|
|
|
dst)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Analysis
|
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def check_correctness_for_arch(options, for_test):
|
|
|
|
files = build_file_list(options, for_test)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
if not options.reuse_gcsuspects:
|
|
|
|
generate_gc_suspects(files, options)
|
2020-11-18 10:36:23 +00:00
|
|
|
else:
|
2022-03-03 14:05:26 +00:00
|
|
|
log("Reusing GCSuspects for {}", options.v8_target_cpu)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
processed_files = 0
|
|
|
|
errors_found = False
|
|
|
|
output = ""
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
log("Searching for evaluation order problems " +
|
|
|
|
(' and dead variables' if options.dead_vars else '') + "for" +
|
|
|
|
options.v8_target_cpu)
|
2020-11-18 10:36:23 +00:00
|
|
|
plugin_args = []
|
2022-03-03 14:05:26 +00:00
|
|
|
if options.dead_vars:
|
2020-11-18 10:36:23 +00:00
|
|
|
plugin_args.append("--dead-vars")
|
2022-03-03 14:05:26 +00:00
|
|
|
if options.verbose:
|
2020-11-18 10:36:23 +00:00
|
|
|
plugin_args.append("--verbose")
|
2022-03-03 14:05:26 +00:00
|
|
|
if options.verbose_trace:
|
|
|
|
plugin_args.append("--verbose-trace")
|
|
|
|
for _, _, stderr in invoke_clang_plugin_for_each_file(files, "find-problems",
|
|
|
|
plugin_args, options):
|
2020-11-18 10:36:23 +00:00
|
|
|
processed_files = processed_files + 1
|
2020-11-26 13:43:44 +00:00
|
|
|
if not errors_found:
|
|
|
|
errors_found = re.search("^[^:]+:\d+:\d+: (warning|error)", stderr,
|
|
|
|
re.MULTILINE) is not None
|
|
|
|
if for_test:
|
|
|
|
output = output + stderr
|
|
|
|
else:
|
|
|
|
sys.stdout.write(stderr)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
log("Done processing {} files.", processed_files)
|
2022-03-03 21:38:39 +00:00
|
|
|
log("Errors found" if errors_found else "No errors found")
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
return errors_found, output
|
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
def test_run(options):
|
|
|
|
if not options.test_run:
|
|
|
|
return True
|
|
|
|
log("Test Run")
|
|
|
|
errors_found, output = check_correctness_for_arch(options, True)
|
2020-11-18 10:36:23 +00:00
|
|
|
if not errors_found:
|
2022-03-03 14:05:26 +00:00
|
|
|
log("Test file should produce errors, but none were found. Output:")
|
|
|
|
print(output)
|
2020-11-18 10:36:23 +00:00
|
|
|
return False
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
new_file = options.out_dir / "test-expectations-gen.txt"
|
|
|
|
with open(new_file, "w") as f:
|
|
|
|
f.write(output)
|
|
|
|
log("Wrote test-results: {}", new_file)
|
|
|
|
|
|
|
|
expected_file = options.v8_root_dir / "tools/gcmole/test-expectations.txt"
|
|
|
|
with open(expected_file) as exp_file:
|
2020-11-18 10:36:23 +00:00
|
|
|
expectations = exp_file.read()
|
|
|
|
|
|
|
|
if output != expectations:
|
2022-03-03 14:05:26 +00:00
|
|
|
diff_file = options.out_dir / "test_output.diff"
|
|
|
|
print("#" * 79)
|
|
|
|
log("Output mismatch from running tests.")
|
|
|
|
log("Please run gcmole manually with --test-run --verbose.")
|
|
|
|
log("Expected: " + expected_file)
|
|
|
|
log("New: " + new_file)
|
|
|
|
log("*Diff:* " + diff_file)
|
|
|
|
print("#" * 79)
|
2021-01-12 09:35:12 +00:00
|
|
|
for line in difflib.unified_diff(
|
2020-11-26 13:43:44 +00:00
|
|
|
expectations.splitlines(),
|
|
|
|
output.splitlines(),
|
2022-03-03 14:05:26 +00:00
|
|
|
fromfile=str(new_file),
|
|
|
|
tofile=str(diff_file),
|
2020-11-18 10:36:23 +00:00
|
|
|
lineterm="",
|
|
|
|
):
|
2022-03-03 14:05:26 +00:00
|
|
|
print(line)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
print("#" * 79)
|
|
|
|
log("Full output")
|
|
|
|
log("Expected: " + expected_file)
|
|
|
|
log("Diff: " + diff_file)
|
|
|
|
log("*New:* " + new_file)
|
|
|
|
print("#" * 79)
|
|
|
|
print(output)
|
|
|
|
print("#" * 79)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
log("Tests ran successfully")
|
2020-11-18 10:36:23 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-03-03 14:05:26 +00:00
|
|
|
# =============================================================================
|
|
|
|
def relative_parents(path, level=0):
|
|
|
|
return Path(os.path.relpath(str(path.resolve().parents[level])))
|
|
|
|
|
|
|
|
|
2020-11-18 10:36:23 +00:00
|
|
|
def main(args):
|
2022-03-03 14:05:26 +00:00
|
|
|
# Get a clean parent path relative to PWD
|
2022-04-20 14:29:35 +00:00
|
|
|
default_root_dir = relative_parents(Path(__file__), level=2)
|
|
|
|
if len(args) >= 1:
|
|
|
|
default_gcmole_dir = relative_parents(Path(args[0]))
|
|
|
|
if default_gcmole_dir or not default_gcmole_dir.exists():
|
|
|
|
default_gcmole_dir = default_root_dir / 'tools' / 'gcmole'
|
2022-03-03 14:05:26 +00:00
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
archs = list(ARCHITECTURES.keys())
|
2022-04-20 14:29:35 +00:00
|
|
|
parser.add_option(
|
|
|
|
"--v8-root-dir",
|
|
|
|
metavar="DIR",
|
|
|
|
default=default_root_dir,
|
|
|
|
help="V8 checkout directory. Default: '{}'".format(
|
|
|
|
default_root_dir.absolute()))
|
2022-03-03 14:05:26 +00:00
|
|
|
parser.add_option(
|
|
|
|
"--v8-target-cpu",
|
|
|
|
type="choice",
|
2022-04-20 14:29:35 +00:00
|
|
|
default="x64",
|
2022-03-03 14:05:26 +00:00
|
|
|
choices=archs,
|
|
|
|
help="Tested CPU architecture. Choices: {}".format(archs),
|
|
|
|
metavar="CPU")
|
2022-04-20 14:29:35 +00:00
|
|
|
default_clang_bin_dir = default_gcmole_dir / 'gcmole-tools/bin'
|
2022-03-03 14:05:26 +00:00
|
|
|
parser.add_option(
|
|
|
|
"--clang-bin-dir",
|
|
|
|
metavar="DIR",
|
|
|
|
help="Build dir of the custom clang version for gcmole." + \
|
|
|
|
"Default: env['CLANG_DIR'] or '{}'".format(default_clang_bin_dir))
|
|
|
|
parser.add_option(
|
|
|
|
"--clang-plugins-dir",
|
|
|
|
metavar="DIR",
|
|
|
|
help="Containing dir for libgcmole.so."
|
2022-04-20 14:29:35 +00:00
|
|
|
"Default: env['CLANG_PLUGINS'] or '{}'".format(default_gcmole_dir))
|
2022-03-03 14:05:26 +00:00
|
|
|
parser.add_option(
|
|
|
|
"--v8-build-dir",
|
|
|
|
metavar="BUILD_DIR",
|
|
|
|
help="GN build dir for v8. Default: 'out/CPU.Release'. "
|
|
|
|
"Config must match cpu specified by --v8-target-cpu")
|
|
|
|
parser.add_option(
|
|
|
|
"--out-dir",
|
|
|
|
metavar="DIR",
|
2022-03-03 21:38:39 +00:00
|
|
|
help="Output location for the gcsuspect and gcauses file."
|
2022-03-03 14:05:26 +00:00
|
|
|
"Default: BUILD_DIR/gen/tools/gcmole")
|
|
|
|
parser.add_option(
|
|
|
|
"--is-bot",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Flag for setting build bot specific settings.")
|
|
|
|
|
|
|
|
group = optparse.OptionGroup(parser, "GCMOLE options")
|
|
|
|
group.add_option(
|
|
|
|
"--reuse-gcsuspects",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Don't build gcsuspects file and reuse previously generated one.")
|
|
|
|
group.add_option(
|
|
|
|
"--sequential",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Don't use parallel python runner.")
|
|
|
|
group.add_option(
|
|
|
|
"--verbose",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Print commands to console before executing them.")
|
|
|
|
group.add_option(
|
|
|
|
"--no-dead-vars",
|
|
|
|
action="store_false",
|
|
|
|
dest="dead_vars",
|
|
|
|
default=True,
|
|
|
|
help="Don't perform dead variable analysis.")
|
|
|
|
group.add_option(
|
|
|
|
"--verbose-trace",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Enable verbose tracing from the plugin itself."
|
|
|
|
"This can be useful to debug finding dead variable.")
|
|
|
|
group.add_option(
|
|
|
|
"--no-allowlist",
|
|
|
|
action="store_true",
|
|
|
|
default=True,
|
|
|
|
dest="allowlist",
|
2022-03-03 21:38:39 +00:00
|
|
|
help="When building gcsuspects allowlist certain functions as if they can be "
|
|
|
|
"causing GC. Currently used to reduce number of false positives in dead "
|
|
|
|
"variables analysis. See TODO for ALLOWLIST in gcmole.py")
|
2022-03-03 14:05:26 +00:00
|
|
|
group.add_option(
|
|
|
|
"--test-run",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Test gcmole on tools/gcmole/gcmole-test.cc")
|
|
|
|
parser.add_option_group(group)
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if not options.v8_target_cpu:
|
|
|
|
# Backwards compatibility
|
2022-04-20 14:29:35 +00:00
|
|
|
if len(args) > 0 and args[0] in archs:
|
2022-03-03 14:05:26 +00:00
|
|
|
options.v8_target_cpu = args[0]
|
|
|
|
log("Using --v8-target-cpu={}", options.v8_target_cpu)
|
2020-11-18 10:36:23 +00:00
|
|
|
else:
|
2022-03-03 14:05:26 +00:00
|
|
|
parser.error("Missing --v8-target-cpu option")
|
2020-11-18 10:36:23 +00:00
|
|
|
|
2022-04-20 14:29:35 +00:00
|
|
|
verify_and_convert_dirs(parser, options, default_gcmole_dir,
|
|
|
|
default_clang_bin_dir)
|
2022-03-03 14:05:26 +00:00
|
|
|
verify_clang_plugin(parser, options)
|
|
|
|
prepare_gcmole_files(options)
|
|
|
|
verify_build_config(parser, options)
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
any_errors_found = False
|
2022-03-03 14:05:26 +00:00
|
|
|
if not test_run(options):
|
2020-11-18 10:36:23 +00:00
|
|
|
any_errors_found = True
|
|
|
|
else:
|
2022-03-03 14:05:26 +00:00
|
|
|
errors_found, output = check_correctness_for_arch(options, False)
|
|
|
|
any_errors_found = any_errors_found or errors_found
|
2020-11-18 10:36:23 +00:00
|
|
|
|
|
|
|
sys.exit(1 if any_errors_found else 0)
|
|
|
|
|
|
|
|
|
2022-04-20 14:29:35 +00:00
|
|
|
def verify_and_convert_dirs(parser, options, default_tools_gcmole_dir,
|
|
|
|
default_clang_bin_dir):
|
2022-03-03 14:05:26 +00:00
|
|
|
# Verify options for setting directors and convert the input strings to Path
|
|
|
|
# objects.
|
|
|
|
options.v8_root_dir = Path(options.v8_root_dir)
|
|
|
|
|
|
|
|
if not options.clang_bin_dir:
|
2022-04-20 14:29:35 +00:00
|
|
|
# Backwards compatibility
|
2022-03-03 14:05:26 +00:00
|
|
|
if os.getenv("CLANG_BIN"):
|
|
|
|
options.clang_bin_dir = Path(os.getenv("CLANG_BIN"))
|
|
|
|
options.is_bot = True
|
|
|
|
else:
|
|
|
|
options.clang_bin_dir = default_clang_bin_dir
|
|
|
|
if not (options.clang_bin_dir / 'clang++').exists():
|
2022-04-20 14:29:35 +00:00
|
|
|
options.clang_bin_dir = Path(options.v8_root_dir,
|
2022-03-03 14:05:26 +00:00
|
|
|
"tools/gcmole/bootstrap/build/bin")
|
|
|
|
log("Using --clang-bin-dir={}", options.clang_bin_dir)
|
|
|
|
else:
|
|
|
|
options.clang_bin_dir = Path(options.clang_bin_dir)
|
|
|
|
|
|
|
|
if not options.clang_plugins_dir:
|
2022-04-20 14:29:35 +00:00
|
|
|
# Backwards compatibility
|
2022-03-03 14:05:26 +00:00
|
|
|
if os.getenv("CLANG_PLUGINS"):
|
|
|
|
options.clang_plugins_dir = Path(os.getenv("CLANG_PLUGINS"))
|
|
|
|
else:
|
2022-04-20 14:29:35 +00:00
|
|
|
options.clang_plugins_dir = default_tools_gcmole_dir.resolve()
|
2022-03-03 14:05:26 +00:00
|
|
|
log("Using --clang-plugins-dir={}", options.clang_plugins_dir)
|
|
|
|
else:
|
|
|
|
options.clang_plugins_dir = Path(options.clang_plugins_dir)
|
|
|
|
|
|
|
|
if not options.v8_build_dir:
|
|
|
|
config = ARCHITECTURES[options.v8_target_cpu]
|
|
|
|
options.v8_build_dir = options.v8_root_dir / ('out/%s.release' %
|
|
|
|
config.name)
|
|
|
|
# Fallback for build bots.
|
|
|
|
if not options.v8_build_dir.exists() and os.getenv("CLANG_BIN"):
|
|
|
|
options.v8_build_dir = options.v8_root_dir / 'out/build'
|
|
|
|
log("Using --v8-build-dir={}", options.v8_build_dir)
|
|
|
|
else:
|
|
|
|
options.v8_build_dir = Path(options.v8_build_dir)
|
|
|
|
|
|
|
|
if not options.out_dir:
|
|
|
|
options.out_dir = options.v8_build_dir / 'gen/tools/gcmole'
|
|
|
|
if options.v8_build_dir.exists():
|
|
|
|
options.out_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
else:
|
|
|
|
options.out_dir = Path(options.out_dir)
|
|
|
|
|
|
|
|
for flag in [
|
|
|
|
"--v8-root-dir", "--v8-build-dir", "--clang-bin-dir",
|
|
|
|
"--clang-plugins-dir", "--out-dir"
|
|
|
|
]:
|
|
|
|
dir = getattr(options, parser.get_option(flag).dest)
|
|
|
|
if not dir.is_dir():
|
|
|
|
parser.error("{}='{}' does not exist!".format(flag, dir))
|
|
|
|
|
|
|
|
|
|
|
|
def verify_clang_plugin(parser, options):
|
|
|
|
libgcmole_path = options.clang_plugins_dir / "libgcmole.so"
|
|
|
|
if not libgcmole_path.is_file():
|
|
|
|
parser.error("'{}' does not exist. Please build gcmole first.".format(
|
|
|
|
libgcmole_path))
|
|
|
|
clang_path = options.clang_bin_dir / "clang++"
|
|
|
|
if not clang_path.is_file():
|
|
|
|
parser.error(
|
|
|
|
"'{}' does not exist. Please build gcmole first.".format(clang_path))
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_gcmole_files(options):
|
|
|
|
cmd = [
|
|
|
|
"ninja", "-C", options.v8_build_dir, "v8_gcmole_files",
|
|
|
|
"v8_dump_build_config"
|
|
|
|
]
|
|
|
|
cmd = list(map(str, cmd))
|
|
|
|
log("Preparing files: {}", " ".join(cmd))
|
|
|
|
try:
|
|
|
|
subprocess.check_call(cmd)
|
|
|
|
except:
|
|
|
|
# Ignore ninja task errors on the bots
|
|
|
|
if options.is_bot:
|
|
|
|
log("Ninja command failed, ignoring errors.")
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
def verify_build_config(parser, options):
|
|
|
|
if options.is_bot:
|
|
|
|
#TODO(cbruni): Fix, currently not supported on the bots
|
|
|
|
return
|
|
|
|
config_file = options.v8_build_dir / 'v8_build_config.json'
|
|
|
|
with open(config_file) as f:
|
|
|
|
config = json.load(f)
|
|
|
|
found_cpu = None
|
|
|
|
for key in ('v8_target_cpu', 'target_cpu', 'current_cpu'):
|
|
|
|
found_cpu = config.get('v8_target_cpu')
|
|
|
|
if found_cpu == options.v8_target_cpu:
|
|
|
|
return
|
|
|
|
parser.error("Build dir '{}' config doesn't match request cpu. {}: {}".format(
|
|
|
|
options.v8_build_dir, options.v8_target_cpu, found_cpu))
|
|
|
|
|
|
|
|
|
2020-11-18 10:36:23 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv)
|