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