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

Class Method Details

.base_envObject



328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/audition/dynamic/harness.rb', line 328

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

.capabilitiesObject

-- capabilities ------------------------------------------------



380
381
382
383
384
385
386
387
388
389
390
# File 'lib/audition/dynamic/harness.rb', line 380

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_probesObject



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/audition/dynamic/harness.rb', line 392

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

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.



69
70
71
72
73
# File 'lib/audition/dynamic/harness.rb', line 69

def describe_error(error)
  root = unwrap(error)
  {"class" => scrub(root.class.name.to_s),
   "message" => scrub(root.message.to_s)[0, 500]}
end

.in_ractor(*args, &block) ⇒ Object



89
90
91
92
93
94
# File 'lib/audition/dynamic/harness.rb', line 89

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



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/audition/dynamic/harness.rb', line 225

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



96
97
98
99
100
101
# File 'lib/audition/dynamic/harness.rb', line 96

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 ---------------------------------------------------



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/audition/dynamic/harness.rb', line 123

def library(payload)
  Array(payload["load_paths"]).each do |lp|
    $LOAD_PATH.unshift(lp) # audition:disable global-variables
  end
  before = Object.constants
  feature = payload.fetch("feature")
  begin
    require feature # audition:disable runtime-require
  rescue LoadError
    # Dashed gem names conventionally ship slashed entry files
    # (rspec-mocks provides rspec/mocks).
    slashed = feature.tr("-", "/")
    raise if slashed == feature

    require slashed # audition:disable runtime-require
  end
  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
60
61
62
63
64
# 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
  begin
    out.puts(JSON.generate("error" => describe_error(e)))
  rescue Exception
    out.puts('{"error":{"class":"HarnessFailure",' \
             '"message":"unreportable error"}}')
  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.



213
214
215
216
217
218
219
220
221
222
223
# File 'lib/audition/dynamic/harness.rb', line 213

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)
  {"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.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/audition/dynamic/harness.rb', line 262

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.



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/audition/dynamic/harness.rb', line 294

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.



347
348
349
350
351
352
353
354
355
356
# File 'lib/audition/dynamic/harness.rb', line 347

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 -------------------------------------------------------



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/audition/dynamic/harness.rb', line 360

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

Returns:

  • (Boolean)


249
250
251
252
253
# File 'lib/audition/dynamic/harness.rb', line 249

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.



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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/audition/dynamic/harness.rb', line 147

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 -----------------------------------------------------



105
106
107
108
109
110
# File 'lib/audition/dynamic/harness.rb', line 105

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.



114
115
116
117
118
119
# File 'lib/audition/dynamic/harness.rb', line 114

def script_ractor(path)
  in_ractor(path) do |p|
    load p # audition:disable runtime-require
    :ok
  end
end

.scrub(text) ⇒ Object



75
76
77
78
79
# File 'lib/audition/dynamic/harness.rb', line 75

def scrub(text)
  text.dup.force_encoding(Encoding::UTF_8).scrub
rescue Exception
  "(unprintable)"
end

.unwrap(error) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/audition/dynamic/harness.rb', line 81

def unwrap(error)
  if error.is_a?(Ractor::RemoteError) && error.cause
    error.cause
  else
    error
  end
end