Module: AuditionHarness
- Defined in:
- lib/audition/dynamic/harness.rb
Defined Under Namespace
Classes: CapClassVar, CapIvar
Constant Summary collapse
- MAX_CONSTS =
5000- EXCLUDED_DIRS =
Directories under the target root that are not the target's own surface. Bundler's deployment mode (and bundler-cache in GitHub Actions) vendors every gem into
/vendor/bundle, and attributing those constants to the target would flip its verdict from blocked to not_ready. Must mirror Audition::Target::EXCLUDED_DIRS (this file is a standalone subprocess script and cannot require the gem); a spec keeps the two lists in sync. %w[ vendor node_modules tmp log coverage pkg .git .bundle ].freeze
- CAP_CONST =
Fixtures for capability probes.
[1, 2]
- NATIVE =
/\.(bundle|so)\z/- DECLARATION =
"rb_ext_ractor_safe"
Class Method Summary collapse
- .base_env ⇒ Object
-
.capabilities ⇒ Object
-- capabilities ------------------------------------------------.
- .capability_probes ⇒ Object
- .declares?(path) ⇒ Boolean
-
.describe_error(error) ⇒ Object
Exception messages can carry arbitrary bytes (C extensions, binary filenames); unscrubbed they blow up JSON.generate inside the rescue and the harness dies without output.
- .entry_candidates(feature, root) ⇒ Object
-
.excluded?(path, root) ⇒ Boolean
Matches the static scanner's exclusion rule: any excluded or dot-prefixed component in the root-relative path means the file is not the target's own code.
- .in_ractor(*args, &block) ⇒ Object
- .inspect_module(full, mod, origin, class_state, class_vars) ⇒ Object
- .jsonable(value) ⇒ Object
-
.library(payload) ⇒ Object
-- libraries ---------------------------------------------------.
- .main(mode, payload, out:) ⇒ Object
-
.native_extensions(before, root, known) ⇒ Object
Compiled extensions the require pulled in, with the one fact that decides their Ractor behavior: whether the file imports rb_ext_ractor_safe.
-
.origin_for(owner, name, root) ⇒ Object
Where was this constant defined, and does that location belong to the audited target (as opposed to a dependency it loaded)? Unknown locations (C extensions, core) count as own so nothing gets silently downgraded.
- .own_path?(path, root) ⇒ Boolean
-
.rack(payload) ⇒ Object
App objects built in config.ru are almost never shareable (the file is instance_eval'd inside Rack::Builder, so every lambda's self is the Builder).
-
.rack_concurrent(config_ru, workers, requests) ⇒ Object
Real failures (races on shared state, require-proxy serialization) only show up under load: boot the app in N Ractors and serve M requests from each.
-
.rack_in_ractor(config_ru) ⇒ Object
Rack::Builder.parse_file cannot run inside a Ractor at all on rack 3.2 (Rack::BUILDER_TOPLEVEL_BINDING holds an unshareable Binding), so the per-Ractor boot rebuilds the app by instance_eval'ing config.ru into a fresh Builder; same DSL, no poisoned constant.
-
.rails(payload) ⇒ Object
-- rails -------------------------------------------------------.
- .realpath(path) ⇒ Object
-
.require_target(feature, root) ⇒ Object
Gem names and entry files diverge in two conventional ways: dashed names ship slashed files (rspec-mocks provides rspec/mocks), and squashed names ship snake_case files (activesupport provides active_support).
- .safe_shareable?(value) ⇒ Boolean
-
.scan(root_names, root: nil) ⇒ Object
Breadth-first walk of every constant the require introduced: plain values get a Ractor.shareable? verdict; classes and modules are inspected for class-level ivars and class variables, then descended into.
-
.script_main(path) ⇒ Object
-- scripts -----------------------------------------------------.
-
.script_ractor(path) ⇒ Object
loadis not proxied to the main Ractor (unlikerequireon Ruby 4.0), so the script body truly executes inside the Ractor. - .scrub(text) ⇒ Object
- .unwrap(error) ⇒ Object
Class Method Details
.base_env ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/audition/dynamic/harness.rb', line 416 def base_env { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/", "QUERY_STRING" => "", "SERVER_NAME" => "localhost", "SERVER_PORT" => "80", "SERVER_PROTOCOL" => "HTTP/1.1", "rack.url_scheme" => "http", "rack.input" => StringIO.new(+""), "rack.errors" => StringIO.new(+"") } end |
.capabilities ⇒ Object
-- capabilities ------------------------------------------------
473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/audition/dynamic/harness.rb', line 473 def capabilities caps = {} capability_probes.each do |label, probe| probe.call caps[label] = {"ok" => true, "error" => nil} rescue Exception => e caps[label] = {"ok" => false, "error" => unwrap(e).class.name} end {"capabilities" => caps} end |
.capability_probes ⇒ Object
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/audition/dynamic/harness.rb', line 485 def capability_probes { "global variable read" => -> { Ractor.new { $audition_cap }.value }, # audition:disable "global variable write" => -> { Ractor.new { $audition_cap = 1 }.value }, # audition:disable "class variable access" => -> { Ractor.new { CapClassVar.read }.value }, "class ivar write" => -> { Ractor.new { CapIvar.write }.value }, "class ivar read (mutable value)" => -> { Ractor.new { CapIvar.read_mutable }.value }, "unshareable constant read" => -> { Ractor.new { CAP_CONST }.value }, "constant set (unshareable value)" => -> { Ractor.new { Object.const_set(:AUDITION_X, +"s") }.value }, "ENV read" => -> { Ractor.new { ENV.fetch("HOME", "none") }.value }, "ENV write" => -> { Ractor.new { ENV["AUD_CAP"] = "1" }.value }, # audition:disable "require inside Ractor" => -> { Ractor.new { require "date" }.value }, # audition:disable "ObjectSpace.each_object" => -> { Ractor.new { ObjectSpace.each_object(Class).first }.value }, "Signal.trap" => -> { Ractor.new { Signal.trap("USR2") {} }.value }, # audition:disable "Thread.current storage" => -> { Ractor.new { Thread.current[:x] = 1 }.value }, "Timeout.timeout" => lambda do require "timeout" # audition:disable runtime-require Ractor.new { Timeout.timeout(2) { :ok } }.value end, "proc copied into Ractor" => lambda do pr = proc { 1 } Ractor.new(pr) { |_p| :ok }.value end, "outer local capture" => lambda do z = [1] Ractor.new { z }.value # audition:disable ractor-isolation end } end |
.declares?(path) ⇒ Boolean
292 293 294 295 296 |
# File 'lib/audition/dynamic/harness.rb', line 292 def declares?(path) File.binread(path).include?(DECLARATION) rescue SystemCallError false end |
.describe_error(error) ⇒ Object
Exception messages can carry arbitrary bytes (C extensions, binary filenames); unscrubbed they blow up JSON.generate inside the rescue and the harness dies without output.
82 83 84 85 86 |
# File 'lib/audition/dynamic/harness.rb', line 82 def describe_error(error) root = unwrap(error) {"class" => scrub(root.class.name.to_s), "message" => scrub(root..to_s)[0, 500]} end |
.entry_candidates(feature, root) ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/audition/dynamic/harness.rb', line 173 def entry_candidates(feature, root) candidates = [] slashed = feature.tr("-", "/") candidates << slashed if slashed != feature files = root ? Dir[File.join(realpath(root), "lib", "*.rb")] : [] candidates << files.first.delete_suffix(".rb") if files.size == 1 candidates end |
.excluded?(path, root) ⇒ Boolean
Matches the static scanner's exclusion rule: any excluded or dot-prefixed component in the root-relative path means the file is not the target's own code.
263 264 265 266 267 268 |
# File 'lib/audition/dynamic/harness.rb', line 263 def excluded?(path, root) relative = path.delete_prefix(root + File::SEPARATOR) relative.split(File::SEPARATOR).any? do |part| EXCLUDED_DIRS.include?(part) || part.start_with?(".") end end |
.in_ractor(*args, &block) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/audition/dynamic/harness.rb', line 102 def in_ractor(*args, &block) ractor = Ractor.new(*args, &block) {"ok" => true, "value" => jsonable(ractor.value)} rescue Exception => e {"ok" => false, "error" => describe_error(e)} end |
.inspect_module(full, mod, origin, class_state, class_vars) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/audition/dynamic/harness.rb', line 313 def inspect_module(full, mod, origin, class_state, class_vars) ivars = mod.instance_variables if ivars.any? shareability = ivars.map do |ivar| value = mod.instance_variable_get(ivar) [ivar.to_s, safe_shareable?(value)] end class_state << origin.merge( "const" => full, "ivars" => shareability.map(&:first), "unshareable" => shareability.reject(&:last).map(&:first) ) end cvars = mod.class_variables(false) if cvars.any? class_vars << origin.merge( "const" => full, "cvars" => cvars.map(&:to_s) ) end 0 rescue Exception 1 end |
.jsonable(value) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/audition/dynamic/harness.rb', line 109 def jsonable(value) case value when Numeric, String, Symbol, true, false, nil then value else value.inspect[0, 200] end end |
.library(payload) ⇒ Object
-- libraries ---------------------------------------------------
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/audition/dynamic/harness.rb', line 136 def library(payload) Array(payload["load_paths"]).each do |lp| $LOAD_PATH.unshift(lp) # audition:disable global-variables end before = Object.constants features = $LOADED_FEATURES.dup # audition:disable global-variables require_target(payload.fetch("feature"), payload["root"]) scan(Object.constants - before, root: payload["root"]).merge( "native_extensions" => native_extensions( features, payload["root"], payload["known_compiled"] ) ) end |
.main(mode, payload, out:) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/audition/dynamic/harness.rb', line 57 def main(mode, payload, out:) result = case mode when "script_main" then script_main(payload.fetch("path")) when "script_ractor" then script_ractor(payload.fetch("path")) when "require" then library(payload) when "rack" then rack(payload) when "rails" then rails(payload) when "capabilities" then capabilities else {"error" => {"class" => "ArgumentError", "message" => "unknown mode #{mode}"}} end out.puts(JSON.generate(result)) rescue Exception => e begin out.puts(JSON.generate("error" => describe_error(e))) rescue Exception out.puts('{"error":{"class":"HarnessFailure",' \ '"message":"unreportable error"}}') end end |
.native_extensions(before, root, known) ⇒ Object
Compiled extensions the require pulled in, with the one fact that decides their Ractor behavior: whether the file imports rb_ext_ractor_safe. Ruby's own extensions (archdir) are flagged so the prober can leave them to Ruby, and files the static check already covers are flagged as known.
278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/audition/dynamic/harness.rb', line 278 def native_extensions(before, root, known) root = realpath(root) known = Array(known).map { |path| realpath(path) } archdir = RbConfig::CONFIG["archdir"] + File::SEPARATOR loaded = $LOADED_FEATURES - before # audition:disable global-variables loaded.grep(NATIVE).map do |path| {"path" => path, "declares" => declares?(path), "ruby" => path.start_with?(archdir), "known" => known.include?(path), "own" => known.include?(path) || own_path?(path, root)} end end |
.origin_for(owner, name, root) ⇒ Object
Where was this constant defined, and does that location belong to the audited target (as opposed to a dependency it loaded)? Unknown locations (C extensions, core) count as own so nothing gets silently downgraded.
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/audition/dynamic/harness.rb', line 247 def origin_for(owner, name, root) path, line = begin owner.const_source_location(name) rescue Exception nil end # The separator matters: /x/app must not claim /x/app-helpers. own = root.nil? || path.nil? || path == root || (path.start_with?(root + File::SEPARATOR) && !excluded?(path, root)) {"path" => path, "line" => line, "own" => own} end |
.own_path?(path, root) ⇒ Boolean
298 299 300 301 302 303 |
# File 'lib/audition/dynamic/harness.rb', line 298 def own_path?(path, root) return false unless root path == root || (path.start_with?(root + File::SEPARATOR) && !excluded?(path, root)) end |
.rack(payload) ⇒ Object
App objects built in config.ru are almost never shareable (the file is instance_eval'd inside Rack::Builder, so every lambda's self is the Builder). Ractor web servers therefore boot the app once per Ractor; the probe mirrors that model: parse config.ru and serve one request entirely inside a Ractor.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/audition/dynamic/harness.rb', line 350 def rack(payload) config_ru = payload.fetch("config_ru") begin require "rack" # audition:disable runtime-require rescue LoadError return {"rack_available" => false} end out = {"rack_available" => true} begin app = Rack::Builder.parse_file(config_ru) app = app.first if app.is_a?(Array) out["app_class"] = app.class.name out["shareable"] = Ractor.shareable?(app) rescue Exception => e out["main_boot_error"] = describe_error(e) end out["ractor_boot_call"] = rack_in_ractor(config_ru) if out["ractor_boot_call"]["ok"] out["concurrency"] = rack_concurrent( config_ru, payload.fetch("ractors", 4), payload.fetch("requests", 25) ) end out end |
.rack_concurrent(config_ru, workers, requests) ⇒ Object
Real failures (races on shared state, require-proxy serialization) only show up under load: boot the app in N Ractors and serve M requests from each.
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/audition/dynamic/harness.rb', line 382 def rack_concurrent(config_ru, workers, requests) ractors = workers.times.map do Ractor.new(config_ru, requests) do |path, n| require "rack" # audition:disable runtime-require require "stringio" # audition:disable runtime-require builder = Rack::Builder.new builder.instance_eval(File.read(path), path, 1) app = builder.to_app statuses = Hash.new(0) n.times do statuses[app.call(AuditionHarness.base_env).first] += 1 end statuses end end results = ractors.map do |ractor| {"ok" => true, "statuses" => ractor.value} rescue Exception => e {"ok" => false, "error" => describe_error(e)} end merged = Hash.new(0) results.each do |result| next unless result["ok"] result["statuses"].each { |code, n| merged[code.to_s] += n } end {"workers" => workers, "requests_per_worker" => requests, "failures" => results.count { |r| !r["ok"] }, "first_error" => results.find { |r| !r["ok"] }&.dig("error"), "statuses" => merged} end |
.rack_in_ractor(config_ru) ⇒ Object
Rack::Builder.parse_file cannot run inside a Ractor at all on rack 3.2 (Rack::BUILDER_TOPLEVEL_BINDING holds an unshareable Binding), so the per-Ractor boot rebuilds the app by instance_eval'ing config.ru into a fresh Builder; same DSL, no poisoned constant.
435 436 437 438 439 440 441 442 443 444 |
# File 'lib/audition/dynamic/harness.rb', line 435 def rack_in_ractor(config_ru) in_ractor(config_ru) do |path| require "rack" # audition:disable runtime-require require "stringio" # audition:disable runtime-require builder = Rack::Builder.new builder.instance_eval(File.read(path), path, 1) app = builder.to_app app.call(AuditionHarness.base_env).first end end |
.rails(payload) ⇒ Object
-- rails -------------------------------------------------------
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/audition/dynamic/harness.rb', line 448 def rails(payload) environment = payload.fetch("environment") before = Object.constants features = $LOADED_FEATURES.dup # audition:disable global-variables started = Time.now require environment # audition:disable runtime-require begin Rails.application.eager_load! rescue Exception nil end boot = {"ok" => true, "seconds" => (Time.now - started).round(1)} scan(Object.constants - before, root: payload["root"]).merge( "boot" => boot, "native_extensions" => native_extensions( features, payload["root"], payload["known_compiled"] ) ) rescue Exception => e {"boot" => {"ok" => false, "error" => describe_error(e)}} end |
.realpath(path) ⇒ Object
305 306 307 308 309 310 311 |
# File 'lib/audition/dynamic/harness.rb', line 305 def realpath(path) return path if path.nil? File.realpath(path) rescue SystemCallError path end |
.require_target(feature, root) ⇒ Object
Gem names and entry files diverge in two conventional ways: dashed names ship slashed files (rspec-mocks provides rspec/mocks), and squashed names ship snake_case files (activesupport provides active_support). The second has no rule to invert, so when the target ships exactly one top-level file under lib/, that file is the entry, required by absolute path. An absolute require keeps the path as given, and scan compares constant origins against the realpathed root, so the candidate is built from the realpath too (a symlinked tmpdir, macOS /var, otherwise turns own findings into dependency ones). The error reported is the last one seen: a candidate that loads but fails inside says more than "cannot load such file".
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/audition/dynamic/harness.rb', line 162 def require_target(feature, root) require feature # audition:disable runtime-require rescue LoadError => error entry_candidates(feature, root).each do |candidate| return require candidate # audition:disable runtime-require rescue LoadError => e error = e end raise error end |
.safe_shareable?(value) ⇒ Boolean
337 338 339 340 341 |
# File 'lib/audition/dynamic/harness.rb', line 337 def safe_shareable?(value) Ractor.shareable?(value) rescue Exception false end |
.scan(root_names, root: nil) ⇒ Object
Breadth-first walk of every constant the require introduced: plain values get a Ractor.shareable? verdict; classes and modules are inspected for class-level ivars and class variables, then descended into. const_get can raise (autoload failures) and anything can lie; every step is rescued and counted.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/audition/dynamic/harness.rb', line 187 def scan(root_names, root: nil) # Loaded features are realpathed by require; the target root # must be too, or symlinked paths (macOS /var vs /private/var) # break the own-vs-dependency comparison. root = realpath(root) unshareable = [] class_state = [] class_vars = [] errors = 0 seen = {} queue = root_names.map { |name| [Object, name.to_s] } visited = 0 until queue.empty? owner, name = queue.shift visited += 1 break if visited > MAX_CONSTS begin value = owner.const_get(name, false) rescue Exception errors += 1 next end full = owner.equal?(Object) ? name : "#{owner}::#{name}" origin = origin_for(owner, name, root) if value.is_a?(Module) next if seen[value.object_id] seen[value.object_id] = true errors += inspect_module(full, value, origin, class_state, class_vars) value.constants(false).each do |child| queue << [value, child.to_s] end else begin unless Ractor.shareable?(value) unshareable << origin.merge( "const" => full, "class" => value.class.name ) end rescue Exception errors += 1 end end end {"unshareable_constants" => unshareable, "class_state" => class_state, "class_variables" => class_vars, "scanned" => visited, "errors" => errors} end |
.script_main(path) ⇒ Object
-- scripts -----------------------------------------------------
118 119 120 121 122 123 |
# File 'lib/audition/dynamic/harness.rb', line 118 def script_main(path) load path # audition:disable runtime-require {"ok" => true} rescue Exception => e {"ok" => false, "error" => describe_error(e)} end |
.script_ractor(path) ⇒ Object
load is not proxied to the main Ractor (unlike require on
Ruby 4.0), so the script body truly executes inside the Ractor.
127 128 129 130 131 132 |
# File 'lib/audition/dynamic/harness.rb', line 127 def script_ractor(path) in_ractor(path) do |p| load p # audition:disable runtime-require :ok end end |
.scrub(text) ⇒ Object
88 89 90 91 92 |
# File 'lib/audition/dynamic/harness.rb', line 88 def scrub(text) text.dup.force_encoding(Encoding::UTF_8).scrub rescue Exception "(unprintable)" end |
.unwrap(error) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/audition/dynamic/harness.rb', line 94 def unwrap(error) if error.is_a?(Ractor::RemoteError) && error.cause error.cause else error end end |