2011-04-07 12:38:16 +00:00
|
|
|
DESCRIPTION -------------------------------------------------------------------
|
|
|
|
|
|
|
|
gcmole is a simple static analysis tool used to find possible evaluation order
|
2019-11-26 13:06:29 +00:00
|
|
|
dependent GC-unsafe places in the V8 codebase and "stale" pointers to the heap
|
|
|
|
(ones whose addresses got invalidated by the GC).
|
2011-04-07 12:38:16 +00:00
|
|
|
|
|
|
|
For example the following code is GC-unsafe:
|
|
|
|
|
2019-03-08 09:25:57 +00:00
|
|
|
Handle<Object> Foo(); // Assume Foo can trigger a GC.
|
2019-11-26 13:06:29 +00:00
|
|
|
void Bar(Object, Object);
|
2011-04-07 12:38:16 +00:00
|
|
|
|
2019-03-08 09:25:57 +00:00
|
|
|
Handle<Object> baz;
|
|
|
|
baz->Qux(*Foo()); // (a)
|
2019-11-26 13:06:29 +00:00
|
|
|
Bar(*Foo(), *baz); // (b)
|
2011-04-07 12:38:16 +00:00
|
|
|
|
|
|
|
Both in cases (a) and (b) compiler is free to evaluate call arguments (that
|
|
|
|
includes receiver) in any order. That means it can dereference baz before
|
|
|
|
calling to Foo and save a raw pointer to a heap object in the register or
|
2019-11-26 13:06:29 +00:00
|
|
|
on the stack.
|
|
|
|
|
|
|
|
In terms of the AST analysis that gcmole does, it warns about places in the
|
|
|
|
code which result in 2 subtrees, the order of execution of which is undefined
|
|
|
|
by C++, one of which causes a GC and the other dereferences a Handle to a raw
|
|
|
|
Object (or its subclasses).
|
|
|
|
|
|
|
|
The following code triggers a stale variable warning (assuming that the Foo
|
|
|
|
function was detected as potentially allocating, as in the previous example):
|
|
|
|
|
|
|
|
JSObject raw_obj = ...;
|
|
|
|
Foo();
|
|
|
|
raw_obj.Print();
|
|
|
|
|
|
|
|
Since Foo can trigger a GC, it might have moved the raw_obj. The solution is
|
|
|
|
simply to store it as a Handle.
|
2011-04-07 12:38:16 +00:00
|
|
|
|
|
|
|
PREREQUISITES -----------------------------------------------------------------
|
|
|
|
|
2021-01-12 09:35:12 +00:00
|
|
|
(1) Install Python
|
2011-04-07 12:38:16 +00:00
|
|
|
|
2021-01-12 09:35:12 +00:00
|
|
|
$ sudo apt-get install python
|
2011-04-07 12:38:16 +00:00
|
|
|
|
2019-03-08 09:25:57 +00:00
|
|
|
(2) Get LLVM 8.0 and Clang 8.0 sources and build them.
|
2011-04-07 12:38:16 +00:00
|
|
|
|
2019-03-08 09:25:57 +00:00
|
|
|
Follow the instructions on http://clang.llvm.org/get_started.html.
|
2011-04-07 12:38:16 +00:00
|
|
|
|
2019-03-08 09:25:57 +00:00
|
|
|
Make sure to pass -DCMAKE_BUILD_TYPE=Release to cmake to get Release build
|
|
|
|
instead of a Debug one.
|
2011-04-07 12:38:16 +00:00
|
|
|
|
2019-03-08 09:25:57 +00:00
|
|
|
(3) Build gcmole Clang plugin (libgcmole.so)
|
2011-04-07 12:38:16 +00:00
|
|
|
|
2019-03-08 09:25:57 +00:00
|
|
|
In the tools/gcmole directory execute the following command:
|
|
|
|
|
|
|
|
$ BUILD_ROOT=<path> LLVM_SRC_ROOT=<path> CLANG_SRC_ROOT=<path> make
|
|
|
|
|
|
|
|
(*) Note that steps (2) and (3) can also be achieved by just using the included
|
|
|
|
bootstrapping script in this directory:
|
|
|
|
|
|
|
|
$ ./tools/gcmole/bootstrap.sh
|
|
|
|
|
|
|
|
This will use "third_party/llvm+clang-build" as a build directory and checkout
|
|
|
|
required sources in the "third_party" directory.
|
2011-04-07 12:38:16 +00:00
|
|
|
|
|
|
|
USING GCMOLE ------------------------------------------------------------------
|
|
|
|
|
2021-01-12 09:35:12 +00:00
|
|
|
gcmole consists of driver script written in Python and Clang plugin that does
|
2011-04-07 12:38:16 +00:00
|
|
|
C++ AST processing. Plugin (libgcmole.so) is expected to be in the same
|
2021-01-12 09:35:12 +00:00
|
|
|
folder as driver (gcmole.py).
|
2011-04-07 12:38:16 +00:00
|
|
|
|
|
|
|
To start analysis cd into the root of v8 checkout and execute the following
|
|
|
|
command:
|
|
|
|
|
2021-01-12 09:35:12 +00:00
|
|
|
CLANG_BIN=<path-to-clang-bin-folder> python tools/gcmole/gcmole.py [<arch>]
|
2011-04-07 12:38:16 +00:00
|
|
|
|
|
|
|
where arch should be one of architectures supported by V8 (arm, ia32, x64).
|
|
|
|
|
|
|
|
Analysis will be performed in 2 stages:
|
|
|
|
|
|
|
|
- on the first stage driver will parse all files and build a global callgraph
|
|
|
|
approximation to find all functions that might potentially cause GC, list
|
|
|
|
of this functions will be written into gcsuspects file.
|
|
|
|
|
|
|
|
- on the second stage driver will parse all files again and will locate all
|
|
|
|
callsites that might be GC-unsafe based on the list of functions causing GC.
|
|
|
|
Such places are marked with a "Possible problem with evaluation order."
|
|
|
|
warning. Messages "Failed to resolve v8::internal::Object" are benign and
|
|
|
|
can be ignored.
|
|
|
|
|
|
|
|
If any errors were found driver exits with non-zero status.
|
2019-03-07 14:43:43 +00:00
|
|
|
|
2019-08-29 14:37:43 +00:00
|
|
|
TESTING -----------------------------------------------------------------------
|
|
|
|
|
2021-01-12 09:35:12 +00:00
|
|
|
Tests are automatically run by the main python runner. Expectations are in
|
2019-08-29 14:37:43 +00:00
|
|
|
test-expectations.txt and need to be updated whenever the sources of the tests
|
|
|
|
in gcmole-test.cc are modified (line numbers also count).
|
|
|
|
|
2019-03-14 13:17:48 +00:00
|
|
|
PACKAGING ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
gcmole is deployed on V8's buildbot infrastructure to run it as part of the
|
|
|
|
continuous integration. A pre-built package of gcmole together with Clang is
|
|
|
|
hosted on Google Cloud Storage for this purpose. To update this package to a
|
|
|
|
newer version, use the provided packaging script:
|
|
|
|
|
|
|
|
$ ./tools/gcmole/package.sh
|
|
|
|
|
|
|
|
This will create a new "tools/gcmole/gcmole-tools.tar.gz" package with the
|
|
|
|
corresponding SHA1 sum suitable to be used for this purpose. It assumes that
|
|
|
|
Clang was built in "third_party/llvm+clang-build" (e.g. by the bootstrapping
|
|
|
|
script "bootstrap.sh" mentioned above).
|
|
|
|
|
|
|
|
TROUBLESHOOTING ---------------------------------------------------------------
|
2019-03-07 14:43:43 +00:00
|
|
|
|
|
|
|
gcmole is tighly coupled with the AST structure that Clang produces. Therefore
|
|
|
|
when upgrading to a newer Clang version, it might start producing bogus output
|
|
|
|
or completely stop outputting warnings. In such occasion, one might start the
|
|
|
|
debugging process by checking weather a new AST node type is introduced which
|
|
|
|
is currently not supported by gcmole. Insert the following code at the end of
|
|
|
|
the FunctionAnalyzer::VisitExpr method to see the unsupported AST class(es)
|
|
|
|
and the source position which generates them:
|
|
|
|
|
|
|
|
if (expr) {
|
|
|
|
clang::Stmt::StmtClass stmtClass = expr->getStmtClass();
|
|
|
|
d_.Report(clang::FullSourceLoc(expr->getExprLoc(), sm_),
|
|
|
|
d_.getCustomDiagID(clang::DiagnosticsEngine::Remark, "%0")) << stmtClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
For instance, gcmole currently doesn't support AtomicExprClass statements
|
|
|
|
introduced for atomic operations.
|
|
|
|
|
|
|
|
A convenient way to observe the AST generated by Clang is to pass the following
|
|
|
|
flags when invoking clang++
|
|
|
|
|
|
|
|
-Xclang -ast-dump -fsyntax-only
|