Module: AuditionHarness
- Defined in:
- lib/audition/dynamic/harness.rb
Defined Under Namespace
Classes: CapClassVar, CapIvar
Constant Summary collapse
- MAX_CONSTS =
5000- CAP_CONST =
Fixtures for capability probes.
[1, 2]
Class Method Summary collapse
- .base_env ⇒ Object
-
.capabilities ⇒ Object
-- capabilities ------------------------------------------------.
- .capability_probes ⇒ Object
- .describe_error(error) ⇒ Object
- .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
-
.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.
-
.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 -------------------------------------------------------.
- .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. - .unwrap(error) ⇒ Object
Class Method Details
.base_env ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/audition/dynamic/harness.rb', line 302 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 ------------------------------------------------
354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/audition/dynamic/harness.rb', line 354 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
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 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 |
# File 'lib/audition/dynamic/harness.rb', line 366 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 |
.describe_error(error) ⇒ Object
61 62 63 64 65 |
# File 'lib/audition/dynamic/harness.rb', line 61 def describe_error(error) root = unwrap(error) {"class" => root.class.name, "message" => root..to_s[0, 500]} end |
.in_ractor(*args, &block) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/audition/dynamic/harness.rb', line 75 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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/audition/dynamic/harness.rb', line 199 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
82 83 84 85 86 87 |
# File 'lib/audition/dynamic/harness.rb', line 82 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 ---------------------------------------------------
109 110 111 112 113 114 115 116 |
# File 'lib/audition/dynamic/harness.rb', line 109 def library(payload) Array(payload["load_paths"]).each do |lp| $LOAD_PATH.unshift(lp) # audition:disable global-variables end before = Object.constants require payload.fetch("feature") # audition:disable runtime-require scan(Object.constants - before, root: payload["root"]) end |
.main(mode, payload, out:) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/audition/dynamic/harness.rb', line 44 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 out.puts(JSON.generate("error" => describe_error(e))) 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.
189 190 191 192 193 194 195 196 197 |
# File 'lib/audition/dynamic/harness.rb', line 189 def origin_for(owner, name, root) path, line = begin owner.const_source_location(name) rescue Exception nil end own = root.nil? || path.nil? || path.start_with?(root) {"path" => path, "line" => line, "own" => own} 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.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/audition/dynamic/harness.rb', line 236 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.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/audition/dynamic/harness.rb', line 268 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.
321 322 323 324 325 326 327 328 329 330 |
# File 'lib/audition/dynamic/harness.rb', line 321 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 -------------------------------------------------------
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/audition/dynamic/harness.rb', line 334 def rails(payload) environment = payload.fetch("environment") before = Object.constants 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) rescue Exception => e {"boot" => {"ok" => false, "error" => describe_error(e)}} end |
.safe_shareable?(value) ⇒ Boolean
223 224 225 226 227 |
# File 'lib/audition/dynamic/harness.rb', line 223 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.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/audition/dynamic/harness.rb', line 123 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. if root root = begin File.realpath(root) rescue root end end 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 -----------------------------------------------------
91 92 93 94 95 96 |
# File 'lib/audition/dynamic/harness.rb', line 91 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.
100 101 102 103 104 105 |
# File 'lib/audition/dynamic/harness.rb', line 100 def script_ractor(path) in_ractor(path) do |p| load p # audition:disable runtime-require :ok end end |
.unwrap(error) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/audition/dynamic/harness.rb', line 67 def unwrap(error) if error.is_a?(Ractor::RemoteError) && error.cause error.cause else error end end |