blob: 9739684629e7e990b93fcd09280c8c042e17b911 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001-- Copyright 2011 the V8 project authors. All rights reserved.
2-- Redistribution and use in source and binary forms, with or without
3-- modification, are permitted provided that the following conditions are
4-- met:
5--
6-- * Redistributions of source code must retain the above copyright
7-- notice, this list of conditions and the following disclaimer.
8-- * Redistributions in binary form must reproduce the above
9-- copyright notice, this list of conditions and the following
10-- disclaimer in the documentation and/or other materials provided
11-- with the distribution.
12-- * Neither the name of Google Inc. nor the names of its
13-- contributors may be used to endorse or promote products derived
14-- from this software without specific prior written permission.
15--
16-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20-- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28-- This is main driver for gcmole tool. See README for more details.
29-- Usage: CLANG_BIN=clang-bin-dir lua tools/gcmole/gcmole.lua [arm|ia32|x64]
30
31local DIR = arg[0]:match("^(.+)/[^/]+$")
Ben Murdoch257744e2011-11-30 15:57:28 +000032
33local FLAGS = {
34 -- Do not build gcsuspects file and reuse previously generated one.
35 reuse_gcsuspects = false;
36
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037 -- Don't use parallel python runner.
38 sequential = false;
39
Ben Murdoch257744e2011-11-30 15:57:28 +000040 -- Print commands to console before executing them.
41 verbose = false;
42
43 -- Perform dead variable analysis (generates many false positives).
44 -- TODO add some sort of whiteliste to filter out false positives.
45 dead_vars = false;
46
47 -- When building gcsuspects whitelist certain functions as if they
48 -- can be causing GC. Currently used to reduce number of false
49 -- positives in dead variables analysis. See TODO for WHITELIST
50 -- below.
51 whitelist = true;
52}
53local ARGS = {}
54
55for i = 1, #arg do
56 local flag = arg[i]:match "^%-%-([%w_-]+)$"
57 if flag then
58 local no, real_flag = flag:match "^(no)([%w_-]+)$"
59 if real_flag then flag = real_flag end
60
61 flag = flag:gsub("%-", "_")
62 if FLAGS[flag] ~= nil then
63 FLAGS[flag] = (no ~= "no")
64 else
65 error("Unknown flag: " .. flag)
66 end
67 else
68 table.insert(ARGS, arg[i])
69 end
70end
71
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072local ARCHS = ARGS[1] and { ARGS[1] } or { 'ia32', 'arm', 'x64', 'arm64' }
Ben Murdoch8b112d22011-06-08 16:22:53 +010073
74local io = require "io"
75local os = require "os"
76
77function log(...)
78 io.stderr:write(string.format(...))
79 io.stderr:write "\n"
80end
81
82-------------------------------------------------------------------------------
83-- Clang invocation
84
Ben Murdoch257744e2011-11-30 15:57:28 +000085local CLANG_BIN = os.getenv "CLANG_BIN"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086local CLANG_PLUGINS = os.getenv "CLANG_PLUGINS"
Ben Murdoch8b112d22011-06-08 16:22:53 +010087
88if not CLANG_BIN or CLANG_BIN == "" then
89 error "CLANG_BIN not set"
Ben Murdoch257744e2011-11-30 15:57:28 +000090end
Ben Murdoch8b112d22011-06-08 16:22:53 +010091
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092if not CLANG_PLUGINS or CLANG_PLUGINS == "" then
93 CLANG_PLUGINS = DIR
94end
95
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096local function MakeClangCommandLine(
97 plugin, plugin_args, triple, arch_define, arch_options)
Ben Murdoch257744e2011-11-30 15:57:28 +000098 if plugin_args then
99 for i = 1, #plugin_args do
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 plugin_args[i] = "-Xclang -plugin-arg-" .. plugin
101 .. " -Xclang " .. plugin_args[i]
Ben Murdoch257744e2011-11-30 15:57:28 +0000102 end
103 plugin_args = " " .. table.concat(plugin_args, " ")
104 end
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 return CLANG_BIN .. "/clang++ -std=c++11 -c "
106 .. " -Xclang -load -Xclang " .. CLANG_PLUGINS .. "/libgcmole.so"
107 .. " -Xclang -plugin -Xclang " .. plugin
Ben Murdoch257744e2011-11-30 15:57:28 +0000108 .. (plugin_args or "")
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 .. " -Xclang -triple -Xclang " .. triple
Ben Murdoch8b112d22011-06-08 16:22:53 +0100110 .. " -D" .. arch_define
Ben Murdoch8b112d22011-06-08 16:22:53 +0100111 .. " -DENABLE_DEBUGGER_SUPPORT"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 .. " -DV8_I18N_SUPPORT"
113 .. " -I./"
114 .. " -Ithird_party/icu/source/common"
115 .. " -Ithird_party/icu/source/i18n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116 .. " " .. arch_options
117end
118
119local function IterTable(t)
120 return coroutine.wrap(function ()
121 for i, v in ipairs(t) do
122 coroutine.yield(v)
123 end
124 end)
125end
126
127local function SplitResults(lines, func)
128 -- Splits the output of parallel.py and calls func on each result.
129 -- Bails out in case of an error in one of the executions.
130 local current = {}
131 local filename = ""
132 for line in lines do
133 local new_file = line:match "^______________ (.*)$"
134 local code = line:match "^______________ finish (%d+) ______________$"
135 if code then
136 if tonumber(code) > 0 then
137 log(table.concat(current, "\n"))
138 log("Failed to examine " .. filename)
139 return false
140 end
141 log("-- %s", filename)
142 func(filename, IterTable(current))
143 elseif new_file then
144 filename = new_file
145 current = {}
146 else
147 table.insert(current, line)
148 end
149 end
150 return true
Ben Murdoch8b112d22011-06-08 16:22:53 +0100151end
152
153function InvokeClangPluginForEachFile(filenames, cfg, func)
154 local cmd_line = MakeClangCommandLine(cfg.plugin,
Ben Murdoch257744e2011-11-30 15:57:28 +0000155 cfg.plugin_args,
156 cfg.triple,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000157 cfg.arch_define,
158 cfg.arch_options)
159 if FLAGS.sequential then
160 log("** Sequential execution.")
161 for _, filename in ipairs(filenames) do
162 log("-- %s", filename)
163 local action = cmd_line .. " " .. filename .. " 2>&1"
164 if FLAGS.verbose then print('popen ', action) end
165 local pipe = io.popen(action)
166 func(filename, pipe:lines())
167 local success = pipe:close()
168 if not success then error("Failed to run: " .. action) end
169 end
170 else
171 log("** Parallel execution.")
172 local action = "python tools/gcmole/parallel.py \""
173 .. cmd_line .. "\" " .. table.concat(filenames, " ")
Ben Murdoch257744e2011-11-30 15:57:28 +0000174 if FLAGS.verbose then print('popen ', action) end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100175 local pipe = io.popen(action)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000176 local success = SplitResults(pipe:lines(), func)
177 local closed = pipe:close()
178 if not (success and closed) then error("Failed to run: " .. action) end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100179 end
180end
181
182-------------------------------------------------------------------------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000183-- GYP file parsing
Ben Murdoch8b112d22011-06-08 16:22:53 +0100184
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185local function ParseGYPFile()
186 local gyp = ""
187 local gyp_files = { "tools/gyp/v8.gyp", "test/cctest/cctest.gyp" }
188 for i = 1, #gyp_files do
189 local f = assert(io.open(gyp_files[i]), "failed to open GYP file")
190 local t = f:read('*a')
191 gyp = gyp .. t
192 f:close()
Ben Murdoch257744e2011-11-30 15:57:28 +0000193 end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100194
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000195 local result = {}
196
197 for condition, sources in
198 gyp:gmatch "'sources': %[.-### gcmole%((.-)%) ###(.-)%]" do
199 if result[condition] == nil then result[condition] = {} end
200 for file in sources:gmatch "'%.%./%.%./src/([^']-%.cc)'" do
201 table.insert(result[condition], "src/" .. file)
202 end
203 for file in sources:gmatch "'(test-[^']-%.cc)'" do
204 table.insert(result[condition], "test/cctest/" .. file)
205 end
Ben Murdoch257744e2011-11-30 15:57:28 +0000206 end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100207
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000208 return result
Ben Murdoch8b112d22011-06-08 16:22:53 +0100209end
210
211local function EvaluateCondition(cond, props)
212 if cond == 'all' then return true end
213
214 local p, v = cond:match "(%w+):(%w+)"
215
216 assert(p and v, "failed to parse condition: " .. cond)
217 assert(props[p] ~= nil, "undefined configuration property: " .. p)
218
219 return props[p] == v
220end
221
222local function BuildFileList(sources, props)
223 local list = {}
224 for condition, files in pairs(sources) do
225 if EvaluateCondition(condition, props) then
Ben Murdoch257744e2011-11-30 15:57:28 +0000226 for i = 1, #files do table.insert(list, files[i]) end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100227 end
228 end
229 return list
230end
231
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000232local sources = ParseGYPFile()
Ben Murdoch8b112d22011-06-08 16:22:53 +0100233
234local function FilesForArch(arch)
235 return BuildFileList(sources, { os = 'linux',
Ben Murdoch257744e2011-11-30 15:57:28 +0000236 arch = arch,
237 mode = 'debug',
238 simulator = ''})
Ben Murdoch8b112d22011-06-08 16:22:53 +0100239end
240
241local mtConfig = {}
242
243mtConfig.__index = mtConfig
244
245local function config (t) return setmetatable(t, mtConfig) end
246
247function mtConfig:extend(t)
248 local e = {}
249 for k, v in pairs(self) do e[k] = v end
250 for k, v in pairs(t) do e[k] = v end
251 return config(e)
252end
253
254local ARCHITECTURES = {
255 ia32 = config { triple = "i586-unknown-linux",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000256 arch_define = "V8_TARGET_ARCH_IA32",
257 arch_options = "-m32" },
Ben Murdoch8b112d22011-06-08 16:22:53 +0100258 arm = config { triple = "i586-unknown-linux",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000259 arch_define = "V8_TARGET_ARCH_ARM",
260 arch_options = "-m32" },
Ben Murdoch8b112d22011-06-08 16:22:53 +0100261 x64 = config { triple = "x86_64-unknown-linux",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000262 arch_define = "V8_TARGET_ARCH_X64",
263 arch_options = "" },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264 arm64 = config { triple = "x86_64-unknown-linux",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000265 arch_define = "V8_TARGET_ARCH_ARM64",
266 arch_options = "" },
Ben Murdoch8b112d22011-06-08 16:22:53 +0100267}
268
269-------------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000270-- GCSuspects Generation
Ben Murdoch8b112d22011-06-08 16:22:53 +0100271
Ben Murdoch257744e2011-11-30 15:57:28 +0000272local gc, gc_caused, funcs
273
274local WHITELIST = {
275 -- The following functions call CEntryStub which is always present.
276 "MacroAssembler.*CallExternalReference",
277 "MacroAssembler.*CallRuntime",
278 "CompileCallLoadPropertyWithInterceptor",
279 "CallIC.*GenerateMiss",
280
281 -- DirectCEntryStub is a special stub used on ARM.
282 -- It is pinned and always present.
283 "DirectCEntryStub.*GenerateCall",
284
285 -- TODO GCMole currently is sensitive enough to understand that certain
286 -- functions only cause GC and return Failure simulataneously.
287 -- Callsites of such functions are safe as long as they are properly
288 -- check return value and propagate the Failure to the caller.
289 -- It should be possible to extend GCMole to understand this.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100290 "Heap.*AllocateFunctionPrototype",
291
292 -- Ignore all StateTag methods.
293 "StateTag",
294
295 -- Ignore printing of elements transition.
296 "PrintElementsTransition"
Ben Murdoch257744e2011-11-30 15:57:28 +0000297};
298
299local function AddCause(name, cause)
300 local t = gc_caused[name]
301 if not t then
302 t = {}
303 gc_caused[name] = t
304 end
305 table.insert(t, cause)
306end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100307
308local function resolve(name)
309 local f = funcs[name]
Ben Murdoch257744e2011-11-30 15:57:28 +0000310
311 if not f then
Ben Murdoch8b112d22011-06-08 16:22:53 +0100312 f = {}
313 funcs[name] = f
Ben Murdoch257744e2011-11-30 15:57:28 +0000314
315 if name:match "Collect.*Garbage" then
316 gc[name] = true
317 AddCause(name, "<GC>")
318 end
319
320 if FLAGS.whitelist then
321 for i = 1, #WHITELIST do
322 if name:match(WHITELIST[i]) then
323 gc[name] = false
324 end
325 end
326 end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100327 end
Ben Murdoch257744e2011-11-30 15:57:28 +0000328
Ben Murdoch8b112d22011-06-08 16:22:53 +0100329 return f
330end
331
332local function parse (filename, lines)
333 local scope
334
335 for funcname in lines do
336 if funcname:sub(1, 1) ~= '\t' then
Ben Murdoch257744e2011-11-30 15:57:28 +0000337 resolve(funcname)
338 scope = funcname
Ben Murdoch8b112d22011-06-08 16:22:53 +0100339 else
Ben Murdoch257744e2011-11-30 15:57:28 +0000340 local name = funcname:sub(2)
341 resolve(name)[scope] = true
Ben Murdoch8b112d22011-06-08 16:22:53 +0100342 end
343 end
344end
345
346local function propagate ()
347 log "** Propagating GC information"
348
Ben Murdoch257744e2011-11-30 15:57:28 +0000349 local function mark(from, callers)
350 for caller, _ in pairs(callers) do
351 if gc[caller] == nil then
352 gc[caller] = true
353 mark(caller, funcs[caller])
354 end
355 AddCause(caller, from)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100356 end
357 end
358
359 for funcname, callers in pairs(funcs) do
Ben Murdoch257744e2011-11-30 15:57:28 +0000360 if gc[funcname] then mark(funcname, callers) end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100361 end
362end
363
364local function GenerateGCSuspects(arch, files, cfg)
Ben Murdoch257744e2011-11-30 15:57:28 +0000365 -- Reset the global state.
366 gc, gc_caused, funcs = {}, {}, {}
367
Ben Murdoch8b112d22011-06-08 16:22:53 +0100368 log ("** Building GC Suspects for %s", arch)
369 InvokeClangPluginForEachFile (files,
370 cfg:extend { plugin = "dump-callees" },
371 parse)
Ben Murdoch257744e2011-11-30 15:57:28 +0000372
Ben Murdoch8b112d22011-06-08 16:22:53 +0100373 propagate()
374
375 local out = assert(io.open("gcsuspects", "w"))
Ben Murdoch257744e2011-11-30 15:57:28 +0000376 for name, value in pairs(gc) do if value then out:write (name, '\n') end end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100377 out:close()
Ben Murdoch257744e2011-11-30 15:57:28 +0000378
379 local out = assert(io.open("gccauses", "w"))
380 out:write "GC = {"
381 for name, causes in pairs(gc_caused) do
382 out:write("['", name, "'] = {")
383 for i = 1, #causes do out:write ("'", causes[i], "';") end
384 out:write("};\n")
385 end
386 out:write "}"
387 out:close()
388
Ben Murdoch8b112d22011-06-08 16:22:53 +0100389 log ("** GCSuspects generated for %s", arch)
390end
391
Ben Murdoch257744e2011-11-30 15:57:28 +0000392--------------------------------------------------------------------------------
Ben Murdoch8b112d22011-06-08 16:22:53 +0100393-- Analysis
394
Ben Murdoch257744e2011-11-30 15:57:28 +0000395local function CheckCorrectnessForArch(arch)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100396 local files = FilesForArch(arch)
397 local cfg = ARCHITECTURES[arch]
398
Ben Murdoch257744e2011-11-30 15:57:28 +0000399 if not FLAGS.reuse_gcsuspects then
400 GenerateGCSuspects(arch, files, cfg)
401 end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100402
403 local processed_files = 0
404 local errors_found = false
405 local function SearchForErrors(filename, lines)
406 processed_files = processed_files + 1
407 for l in lines do
Ben Murdoch257744e2011-11-30 15:57:28 +0000408 errors_found = errors_found or
409 l:match "^[^:]+:%d+:%d+:" or
410 l:match "error" or
411 l:match "warning"
Ben Murdoch8b112d22011-06-08 16:22:53 +0100412 print(l)
413 end
414 end
415
Ben Murdoch257744e2011-11-30 15:57:28 +0000416 log("** Searching for evaluation order problems%s for %s",
417 FLAGS.dead_vars and " and dead variables" or "",
418 arch)
419 local plugin_args
420 if FLAGS.dead_vars then plugin_args = { "--dead-vars" } end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100421 InvokeClangPluginForEachFile(files,
Ben Murdoch257744e2011-11-30 15:57:28 +0000422 cfg:extend { plugin = "find-problems",
423 plugin_args = plugin_args },
424 SearchForErrors)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100425 log("** Done processing %d files. %s",
426 processed_files,
427 errors_found and "Errors found" or "No errors found")
428
429 return errors_found
430end
431
432local function SafeCheckCorrectnessForArch(arch)
433 local status, errors = pcall(CheckCorrectnessForArch, arch)
434 if not status then
435 print(string.format("There was an error: %s", errors))
436 errors = true
437 end
438 return errors
439end
440
441local errors = false
442
443for _, arch in ipairs(ARCHS) do
444 if not ARCHITECTURES[arch] then
445 error ("Unknown arch: " .. arch)
446 end
447
448 errors = SafeCheckCorrectnessForArch(arch, report) or errors
449end
450
451os.exit(errors and 1 or 0)