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