f8d3b6204a2cfabf62a5cc2a0ef67e38f71300bc
[profile/ivi/qtjsbackend.git] / src / 3rdparty / v8 / tools / gcmole / gcmole.lua
1 -- 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
31 local DIR = arg[0]:match("^(.+)/[^/]+$")
32
33 local 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 }
50 local ARGS = {}
51
52 for 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
67 end
68
69 local ARCHS = ARGS[1] and { ARGS[1] } or { 'ia32', 'arm', 'x64' }
70
71 local io = require "io"
72 local os = require "os"
73
74 function log(...)
75    io.stderr:write(string.format(...))
76    io.stderr:write "\n"
77 end
78
79 -------------------------------------------------------------------------------
80 -- Clang invocation
81
82 local CLANG_BIN = os.getenv "CLANG_BIN"
83
84 if not CLANG_BIN or CLANG_BIN == "" then
85    error "CLANG_BIN not set"
86 end
87
88 local 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"
96       .. " -plugin "  .. plugin
97       .. (plugin_args or "")
98       .. " -triple " .. triple
99       .. " -D" .. arch_define
100       .. " -DENABLE_DEBUGGER_SUPPORT"
101       .. " -Isrc"
102 end
103
104 function InvokeClangPluginForEachFile(filenames, cfg, func)
105    local cmd_line = MakeClangCommandLine(cfg.plugin,
106                                          cfg.plugin_args,
107                                          cfg.triple,
108                                          cfg.arch_define)
109
110    for _, filename in ipairs(filenames) do
111       log("-- %s", filename)
112       local action = cmd_line .. " src/" .. filename .. " 2>&1"
113       if FLAGS.verbose then print('popen ', action) end
114       local pipe = io.popen(action)
115       func(filename, pipe:lines())
116       pipe:close()
117    end
118 end
119
120 -------------------------------------------------------------------------------
121 -- SConscript parsing
122
123 local function ParseSConscript()
124    local f = assert(io.open("src/SConscript"), "failed to open SConscript")
125    local sconscript = f:read('*a')
126    f:close()
127
128    local SOURCES = sconscript:match "SOURCES = {(.-)}";
129
130    local sources = {}
131
132    for condition, list in
133       SOURCES:gmatch "'([^']-)': Split%(\"\"\"(.-)\"\"\"%)" do
134       local files = {}
135       for file in list:gmatch "[^%s]+" do table.insert(files, file) end
136       sources[condition] = files
137    end
138
139    for condition, list in SOURCES:gmatch "'([^']-)': %[(.-)%]" do
140       local files = {}
141       for file in list:gmatch "'([^']-)'" do table.insert(files, file) end
142       sources[condition] = files
143    end
144
145    return sources
146 end
147
148 local function EvaluateCondition(cond, props)
149    if cond == 'all' then return true end
150
151    local p, v = cond:match "(%w+):(%w+)"
152
153    assert(p and v, "failed to parse condition: " .. cond)
154    assert(props[p] ~= nil, "undefined configuration property: " .. p)
155
156    return props[p] == v
157 end
158
159 local function BuildFileList(sources, props)
160    local list = {}
161    for condition, files in pairs(sources) do
162       if EvaluateCondition(condition, props) then
163          for i = 1, #files do table.insert(list, files[i]) end
164       end
165    end
166    return list
167 end
168
169 local sources = ParseSConscript()
170
171 local function FilesForArch(arch)
172    return BuildFileList(sources, { os = 'linux',
173                                    arch = arch,
174                                    mode = 'debug',
175                                    simulator = ''})
176 end
177
178 local mtConfig = {}
179
180 mtConfig.__index = mtConfig
181
182 local function config (t) return setmetatable(t, mtConfig) end
183
184 function mtConfig:extend(t)
185    local e = {}
186    for k, v in pairs(self) do e[k] = v end
187    for k, v in pairs(t) do e[k] = v end
188    return config(e)
189 end
190
191 local ARCHITECTURES = {
192    ia32 = config { triple = "i586-unknown-linux",
193                    arch_define = "V8_TARGET_ARCH_IA32" },
194    arm = config { triple = "i586-unknown-linux",
195                   arch_define = "V8_TARGET_ARCH_ARM" },
196    x64 = config { triple = "x86_64-unknown-linux",
197                   arch_define = "V8_TARGET_ARCH_X64" }
198 }
199
200 -------------------------------------------------------------------------------
201 -- GCSuspects Generation
202
203 local gc, gc_caused, funcs
204
205 local WHITELIST = {
206    -- The following functions call CEntryStub which is always present.
207    "MacroAssembler.*CallExternalReference",
208    "MacroAssembler.*CallRuntime",
209    "CompileCallLoadPropertyWithInterceptor",
210    "CallIC.*GenerateMiss",
211
212    -- DirectCEntryStub is a special stub used on ARM. 
213    -- It is pinned and always present.
214    "DirectCEntryStub.*GenerateCall",  
215
216    -- TODO GCMole currently is sensitive enough to understand that certain 
217    --      functions only cause GC and return Failure simulataneously. 
218    --      Callsites of such functions are safe as long as they are properly 
219    --      check return value and propagate the Failure to the caller.
220    --      It should be possible to extend GCMole to understand this.
221    "Heap.*AllocateFunctionPrototype"
222 };
223
224 local function AddCause(name, cause)
225    local t = gc_caused[name]
226    if not t then
227       t = {}
228       gc_caused[name] = t
229    end
230    table.insert(t, cause)
231 end
232
233 local function resolve(name)
234    local f = funcs[name]
235
236    if not f then
237       f = {}
238       funcs[name] = f
239
240       if name:match "Collect.*Garbage" then
241          gc[name] = true
242          AddCause(name, "<GC>")
243       end
244
245       if FLAGS.whitelist then
246          for i = 1, #WHITELIST do
247             if name:match(WHITELIST[i]) then
248                gc[name] = false
249             end
250          end
251       end
252    end
253
254     return f
255 end
256
257 local function parse (filename, lines)
258    local scope
259
260    for funcname in lines do
261       if funcname:sub(1, 1) ~= '\t' then
262          resolve(funcname)
263          scope = funcname
264       else
265          local name = funcname:sub(2)
266          resolve(name)[scope] = true
267       end
268    end
269 end
270
271 local function propagate ()
272    log "** Propagating GC information"
273
274    local function mark(from, callers)
275       for caller, _ in pairs(callers) do
276          if gc[caller] == nil then
277             gc[caller] = true
278             mark(caller, funcs[caller])
279          end
280          AddCause(caller, from)
281       end
282    end
283
284    for funcname, callers in pairs(funcs) do
285       if gc[funcname] then mark(funcname, callers) end
286    end
287 end
288
289 local function GenerateGCSuspects(arch, files, cfg)
290    -- Reset the global state.
291    gc, gc_caused, funcs = {}, {}, {}
292
293    log ("** Building GC Suspects for %s", arch)
294    InvokeClangPluginForEachFile (files,
295                                  cfg:extend { plugin = "dump-callees" },
296                                  parse)
297
298    propagate()
299
300    local out = assert(io.open("gcsuspects", "w"))
301    for name, value in pairs(gc) do if value then out:write (name, '\n') end end
302    out:close()
303
304    local out = assert(io.open("gccauses", "w"))
305    out:write "GC = {"
306    for name, causes in pairs(gc_caused) do
307       out:write("['", name, "'] = {")
308       for i = 1, #causes do out:write ("'", causes[i], "';") end
309       out:write("};\n")
310    end
311    out:write "}"
312    out:close()
313
314    log ("** GCSuspects generated for %s", arch)
315 end
316
317 --------------------------------------------------------------------------------
318 -- Analysis
319
320 local function CheckCorrectnessForArch(arch)
321    local files = FilesForArch(arch)
322    local cfg = ARCHITECTURES[arch]
323
324    if not FLAGS.reuse_gcsuspects then
325       GenerateGCSuspects(arch, files, cfg)
326    end
327
328    local processed_files = 0
329    local errors_found = false
330    local function SearchForErrors(filename, lines)
331       processed_files = processed_files + 1
332       for l in lines do
333          errors_found = errors_found or
334             l:match "^[^:]+:%d+:%d+:" or
335             l:match "error" or
336             l:match "warning"
337          print(l)
338       end
339    end
340
341    log("** Searching for evaluation order problems%s for %s",
342        FLAGS.dead_vars and " and dead variables" or "",
343        arch)
344    local plugin_args
345    if FLAGS.dead_vars then plugin_args = { "--dead-vars" } end
346    InvokeClangPluginForEachFile(files,
347                                 cfg:extend { plugin = "find-problems",
348                                              plugin_args = plugin_args },
349                                 SearchForErrors)
350    log("** Done processing %d files. %s",
351        processed_files,
352        errors_found and "Errors found" or "No errors found")
353
354    return errors_found
355 end
356
357 local function SafeCheckCorrectnessForArch(arch)
358    local status, errors = pcall(CheckCorrectnessForArch, arch)
359    if not status then
360       print(string.format("There was an error: %s", errors))
361       errors = true
362    end
363    return errors
364 end
365
366 local errors = false
367
368 for _, arch in ipairs(ARCHS) do
369    if not ARCHITECTURES[arch] then
370       error ("Unknown arch: " .. arch)
371    end
372
373    errors = SafeCheckCorrectnessForArch(arch, report) or errors
374 end
375
376 os.exit(errors and 1 or 0)