blob: 09db54754f8e9099bad568610f29018ea85f31db [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
37 -- Print commands to console before executing them.
38 verbose = false;
39
40 -- Perform dead variable analysis (generates many false positives).
41 -- TODO add some sort of whiteliste to filter out false positives.
42 dead_vars = false;
43
44 -- When building gcsuspects whitelist certain functions as if they
45 -- can be causing GC. Currently used to reduce number of false
46 -- positives in dead variables analysis. See TODO for WHITELIST
47 -- below.
48 whitelist = true;
49}
50local ARGS = {}
51
52for i = 1, #arg do
53 local flag = arg[i]:match "^%-%-([%w_-]+)$"
54 if flag then
55 local no, real_flag = flag:match "^(no)([%w_-]+)$"
56 if real_flag then flag = real_flag end
57
58 flag = flag:gsub("%-", "_")
59 if FLAGS[flag] ~= nil then
60 FLAGS[flag] = (no ~= "no")
61 else
62 error("Unknown flag: " .. flag)
63 end
64 else
65 table.insert(ARGS, arg[i])
66 end
67end
68
69local ARCHS = ARGS[1] and { ARGS[1] } or { 'ia32', 'arm', 'x64' }
Ben Murdoch8b112d22011-06-08 16:22:53 +010070
71local io = require "io"
72local os = require "os"
73
74function log(...)
75 io.stderr:write(string.format(...))
76 io.stderr:write "\n"
77end
78
79-------------------------------------------------------------------------------
80-- Clang invocation
81
Ben Murdoch257744e2011-11-30 15:57:28 +000082local CLANG_BIN = os.getenv "CLANG_BIN"
Ben Murdoch8b112d22011-06-08 16:22:53 +010083
84if not CLANG_BIN or CLANG_BIN == "" then
85 error "CLANG_BIN not set"
Ben Murdoch257744e2011-11-30 15:57:28 +000086end
Ben Murdoch8b112d22011-06-08 16:22:53 +010087
Ben Murdoch257744e2011-11-30 15:57:28 +000088local function MakeClangCommandLine(plugin, plugin_args, triple, arch_define)
89 if plugin_args then
90 for i = 1, #plugin_args do
91 plugin_args[i] = "-plugin-arg-" .. plugin .. " " .. plugin_args[i]
92 end
93 plugin_args = " " .. table.concat(plugin_args, " ")
94 end
95 return CLANG_BIN .. "/clang -cc1 -load " .. DIR .. "/libgcmole.so"
Ben Murdoch8b112d22011-06-08 16:22:53 +010096 .. " -plugin " .. plugin
Ben Murdoch257744e2011-11-30 15:57:28 +000097 .. (plugin_args or "")
98 .. " -triple " .. triple
Ben Murdoch8b112d22011-06-08 16:22:53 +010099 .. " -D" .. arch_define
Ben Murdoch8b112d22011-06-08 16:22:53 +0100100 .. " -DENABLE_DEBUGGER_SUPPORT"
101 .. " -Isrc"
102end
103
104function InvokeClangPluginForEachFile(filenames, cfg, func)
105 local cmd_line = MakeClangCommandLine(cfg.plugin,
Ben Murdoch257744e2011-11-30 15:57:28 +0000106 cfg.plugin_args,
107 cfg.triple,
108 cfg.arch_define)
Ben Murdoch257744e2011-11-30 15:57:28 +0000109 for _, filename in ipairs(filenames) do
Ben Murdoch8b112d22011-06-08 16:22:53 +0100110 log("-- %s", filename)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100111 local action = cmd_line .. " src/" .. filename .. " 2>&1"
Ben Murdoch257744e2011-11-30 15:57:28 +0000112 if FLAGS.verbose then print('popen ', action) end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100113 local pipe = io.popen(action)
114 func(filename, pipe:lines())
115 pipe:close()
116 end
117end
118
119-------------------------------------------------------------------------------
120-- SConscript parsing
121
122local function ParseSConscript()
123 local f = assert(io.open("src/SConscript"), "failed to open SConscript")
124 local sconscript = f:read('*a')
125 f:close()
126
Ben Murdoch257744e2011-11-30 15:57:28 +0000127 local SOURCES = sconscript:match "SOURCES = {(.-)}";
Ben Murdoch8b112d22011-06-08 16:22:53 +0100128
129 local sources = {}
130
131 for condition, list in
132 SOURCES:gmatch "'([^']-)': Split%(\"\"\"(.-)\"\"\"%)" do
133 local files = {}
134 for file in list:gmatch "[^%s]+" do table.insert(files, file) end
135 sources[condition] = files
Ben Murdoch257744e2011-11-30 15:57:28 +0000136 end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100137
138 for condition, list in SOURCES:gmatch "'([^']-)': %[(.-)%]" do
139 local files = {}
140 for file in list:gmatch "'([^']-)'" do table.insert(files, file) end
141 sources[condition] = files
Ben Murdoch257744e2011-11-30 15:57:28 +0000142 end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100143
144 return sources
145end
146
147local function EvaluateCondition(cond, props)
148 if cond == 'all' then return true end
149
150 local p, v = cond:match "(%w+):(%w+)"
151
152 assert(p and v, "failed to parse condition: " .. cond)
153 assert(props[p] ~= nil, "undefined configuration property: " .. p)
154
155 return props[p] == v
156end
157
158local function BuildFileList(sources, props)
159 local list = {}
160 for condition, files in pairs(sources) do
161 if EvaluateCondition(condition, props) then
Ben Murdoch257744e2011-11-30 15:57:28 +0000162 for i = 1, #files do table.insert(list, files[i]) end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100163 end
164 end
165 return list
166end
167
168local sources = ParseSConscript()
169
170local function FilesForArch(arch)
171 return BuildFileList(sources, { os = 'linux',
Ben Murdoch257744e2011-11-30 15:57:28 +0000172 arch = arch,
173 mode = 'debug',
174 simulator = ''})
Ben Murdoch8b112d22011-06-08 16:22:53 +0100175end
176
177local mtConfig = {}
178
179mtConfig.__index = mtConfig
180
181local function config (t) return setmetatable(t, mtConfig) end
182
183function mtConfig:extend(t)
184 local e = {}
185 for k, v in pairs(self) do e[k] = v end
186 for k, v in pairs(t) do e[k] = v end
187 return config(e)
188end
189
190local ARCHITECTURES = {
191 ia32 = config { triple = "i586-unknown-linux",
Ben Murdoch257744e2011-11-30 15:57:28 +0000192 arch_define = "V8_TARGET_ARCH_IA32" },
Ben Murdoch8b112d22011-06-08 16:22:53 +0100193 arm = config { triple = "i586-unknown-linux",
Ben Murdoch257744e2011-11-30 15:57:28 +0000194 arch_define = "V8_TARGET_ARCH_ARM" },
Ben Murdoch8b112d22011-06-08 16:22:53 +0100195 x64 = config { triple = "x86_64-unknown-linux",
Ben Murdoch257744e2011-11-30 15:57:28 +0000196 arch_define = "V8_TARGET_ARCH_X64" }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100197}
198
199-------------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000200-- GCSuspects Generation
Ben Murdoch8b112d22011-06-08 16:22:53 +0100201
Ben Murdoch257744e2011-11-30 15:57:28 +0000202local gc, gc_caused, funcs
203
204local WHITELIST = {
205 -- The following functions call CEntryStub which is always present.
206 "MacroAssembler.*CallExternalReference",
207 "MacroAssembler.*CallRuntime",
208 "CompileCallLoadPropertyWithInterceptor",
209 "CallIC.*GenerateMiss",
210
211 -- DirectCEntryStub is a special stub used on ARM.
212 -- It is pinned and always present.
213 "DirectCEntryStub.*GenerateCall",
214
215 -- TODO GCMole currently is sensitive enough to understand that certain
216 -- functions only cause GC and return Failure simulataneously.
217 -- Callsites of such functions are safe as long as they are properly
218 -- check return value and propagate the Failure to the caller.
219 -- It should be possible to extend GCMole to understand this.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100220 "Heap.*AllocateFunctionPrototype",
221
222 -- Ignore all StateTag methods.
223 "StateTag",
224
225 -- Ignore printing of elements transition.
226 "PrintElementsTransition"
Ben Murdoch257744e2011-11-30 15:57:28 +0000227};
228
229local function AddCause(name, cause)
230 local t = gc_caused[name]
231 if not t then
232 t = {}
233 gc_caused[name] = t
234 end
235 table.insert(t, cause)
236end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100237
238local function resolve(name)
239 local f = funcs[name]
Ben Murdoch257744e2011-11-30 15:57:28 +0000240
241 if not f then
Ben Murdoch8b112d22011-06-08 16:22:53 +0100242 f = {}
243 funcs[name] = f
Ben Murdoch257744e2011-11-30 15:57:28 +0000244
245 if name:match "Collect.*Garbage" then
246 gc[name] = true
247 AddCause(name, "<GC>")
248 end
249
250 if FLAGS.whitelist then
251 for i = 1, #WHITELIST do
252 if name:match(WHITELIST[i]) then
253 gc[name] = false
254 end
255 end
256 end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100257 end
Ben Murdoch257744e2011-11-30 15:57:28 +0000258
Ben Murdoch8b112d22011-06-08 16:22:53 +0100259 return f
260end
261
262local function parse (filename, lines)
263 local scope
264
265 for funcname in lines do
266 if funcname:sub(1, 1) ~= '\t' then
Ben Murdoch257744e2011-11-30 15:57:28 +0000267 resolve(funcname)
268 scope = funcname
Ben Murdoch8b112d22011-06-08 16:22:53 +0100269 else
Ben Murdoch257744e2011-11-30 15:57:28 +0000270 local name = funcname:sub(2)
271 resolve(name)[scope] = true
Ben Murdoch8b112d22011-06-08 16:22:53 +0100272 end
273 end
274end
275
276local function propagate ()
277 log "** Propagating GC information"
278
Ben Murdoch257744e2011-11-30 15:57:28 +0000279 local function mark(from, callers)
280 for caller, _ in pairs(callers) do
281 if gc[caller] == nil then
282 gc[caller] = true
283 mark(caller, funcs[caller])
284 end
285 AddCause(caller, from)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100286 end
287 end
288
289 for funcname, callers in pairs(funcs) do
Ben Murdoch257744e2011-11-30 15:57:28 +0000290 if gc[funcname] then mark(funcname, callers) end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100291 end
292end
293
294local function GenerateGCSuspects(arch, files, cfg)
Ben Murdoch257744e2011-11-30 15:57:28 +0000295 -- Reset the global state.
296 gc, gc_caused, funcs = {}, {}, {}
297
Ben Murdoch8b112d22011-06-08 16:22:53 +0100298 log ("** Building GC Suspects for %s", arch)
299 InvokeClangPluginForEachFile (files,
300 cfg:extend { plugin = "dump-callees" },
301 parse)
Ben Murdoch257744e2011-11-30 15:57:28 +0000302
Ben Murdoch8b112d22011-06-08 16:22:53 +0100303 propagate()
304
305 local out = assert(io.open("gcsuspects", "w"))
Ben Murdoch257744e2011-11-30 15:57:28 +0000306 for name, value in pairs(gc) do if value then out:write (name, '\n') end end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100307 out:close()
Ben Murdoch257744e2011-11-30 15:57:28 +0000308
309 local out = assert(io.open("gccauses", "w"))
310 out:write "GC = {"
311 for name, causes in pairs(gc_caused) do
312 out:write("['", name, "'] = {")
313 for i = 1, #causes do out:write ("'", causes[i], "';") end
314 out:write("};\n")
315 end
316 out:write "}"
317 out:close()
318
Ben Murdoch8b112d22011-06-08 16:22:53 +0100319 log ("** GCSuspects generated for %s", arch)
320end
321
Ben Murdoch257744e2011-11-30 15:57:28 +0000322--------------------------------------------------------------------------------
Ben Murdoch8b112d22011-06-08 16:22:53 +0100323-- Analysis
324
Ben Murdoch257744e2011-11-30 15:57:28 +0000325local function CheckCorrectnessForArch(arch)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100326 local files = FilesForArch(arch)
327 local cfg = ARCHITECTURES[arch]
328
Ben Murdoch257744e2011-11-30 15:57:28 +0000329 if not FLAGS.reuse_gcsuspects then
330 GenerateGCSuspects(arch, files, cfg)
331 end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100332
333 local processed_files = 0
334 local errors_found = false
335 local function SearchForErrors(filename, lines)
336 processed_files = processed_files + 1
337 for l in lines do
Ben Murdoch257744e2011-11-30 15:57:28 +0000338 errors_found = errors_found or
339 l:match "^[^:]+:%d+:%d+:" or
340 l:match "error" or
341 l:match "warning"
Ben Murdoch8b112d22011-06-08 16:22:53 +0100342 print(l)
343 end
344 end
345
Ben Murdoch257744e2011-11-30 15:57:28 +0000346 log("** Searching for evaluation order problems%s for %s",
347 FLAGS.dead_vars and " and dead variables" or "",
348 arch)
349 local plugin_args
350 if FLAGS.dead_vars then plugin_args = { "--dead-vars" } end
Ben Murdoch8b112d22011-06-08 16:22:53 +0100351 InvokeClangPluginForEachFile(files,
Ben Murdoch257744e2011-11-30 15:57:28 +0000352 cfg:extend { plugin = "find-problems",
353 plugin_args = plugin_args },
354 SearchForErrors)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100355 log("** Done processing %d files. %s",
356 processed_files,
357 errors_found and "Errors found" or "No errors found")
358
359 return errors_found
360end
361
362local function SafeCheckCorrectnessForArch(arch)
363 local status, errors = pcall(CheckCorrectnessForArch, arch)
364 if not status then
365 print(string.format("There was an error: %s", errors))
366 errors = true
367 end
368 return errors
369end
370
371local errors = false
372
373for _, arch in ipairs(ARCHS) do
374 if not ARCHITECTURES[arch] then
375 error ("Unknown arch: " .. arch)
376 end
377
378 errors = SafeCheckCorrectnessForArch(arch, report) or errors
379end
380
381os.exit(errors and 1 or 0)