saboteur 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +215 -0
  3. data/README.md +44 -6
  4. data/exe/saboteur +6 -2
  5. data/lib/saboteur/attribute_finder.rb +116 -0
  6. data/lib/saboteur/baseline/run_evidence.rb +2 -1
  7. data/lib/saboteur/cli/agent_provenance.rb +59 -0
  8. data/lib/saboteur/cli/dirty_tree_notice.rb +28 -0
  9. data/lib/saboteur/cli/doctor_command.rb +41 -0
  10. data/lib/saboteur/cli/error_cause.rb +40 -0
  11. data/lib/saboteur/cli/execution_options.rb +46 -0
  12. data/lib/saboteur/cli/exit_status.rb +12 -14
  13. data/lib/saboteur/cli/explicit_file_options.rb +21 -0
  14. data/lib/saboteur/cli/explicit_sources.rb +46 -0
  15. data/lib/saboteur/cli/failure_output.rb +35 -0
  16. data/lib/saboteur/cli/help_command.rb +23 -0
  17. data/lib/saboteur/cli/matrix_option.rb +17 -0
  18. data/lib/saboteur/cli/output_options.rb +27 -0
  19. data/lib/saboteur/cli/presets.rb +41 -1
  20. data/lib/saboteur/cli/preview_command.rb +26 -0
  21. data/lib/saboteur/cli/run_budget_options.rb +6 -1
  22. data/lib/saboteur/cli/run_command.rb +33 -0
  23. data/lib/saboteur/cli/run_output.rb +38 -0
  24. data/lib/saboteur/cli/run_reporting.rb +6 -5
  25. data/lib/saboteur/cli/run_validation.rb +54 -0
  26. data/lib/saboteur/cli/source_set.rb +0 -18
  27. data/lib/saboteur/cli.rb +29 -305
  28. data/lib/saboteur/config.rb +76 -7
  29. data/lib/saboteur/config_file.rb +12 -8
  30. data/lib/saboteur/constant_subject_finder.rb +3 -0
  31. data/lib/saboteur/custom_map.rb +24 -5
  32. data/lib/saboteur/deep_source.rb +49 -0
  33. data/lib/saboteur/dirty_tree_warning.rb +6 -4
  34. data/lib/saboteur/doctor.rb +10 -0
  35. data/lib/saboteur/engine/baseline_guard.rb +118 -15
  36. data/lib/saboteur/engine/baseline_parts.rb +51 -0
  37. data/lib/saboteur/engine/boot.rb +11 -3
  38. data/lib/saboteur/engine/bytecode_equivalence.rb +17 -1
  39. data/lib/saboteur/engine/candidate_selection.rb +16 -5
  40. data/lib/saboteur/engine/candidates.rb +4 -1
  41. data/lib/saboteur/engine/coverage_inputs_snapshot.rb +146 -0
  42. data/lib/saboteur/engine/coverage_miss_note.rb +86 -0
  43. data/lib/saboteur/engine/coverage_partial.rb +48 -0
  44. data/lib/saboteur/engine/coverage_parts.rb +50 -0
  45. data/lib/saboteur/engine/coverage_shadow.rb +92 -0
  46. data/lib/saboteur/engine/coverage_store.rb +296 -21
  47. data/lib/saboteur/engine/diagnostics.rb +24 -7
  48. data/lib/saboteur/engine/distribution.rb +2 -2
  49. data/lib/saboteur/engine/gated_run.rb +4 -1
  50. data/lib/saboteur/engine/load_coverage.rb +72 -8
  51. data/lib/saboteur/engine/partial_invalidation.rb +115 -0
  52. data/lib/saboteur/engine/partial_merge.rb +40 -0
  53. data/lib/saboteur/engine/pipelines.rb +11 -3
  54. data/lib/saboteur/engine/reference_specs.rb +71 -0
  55. data/lib/saboteur/engine/result_cache.rb +68 -5
  56. data/lib/saboteur/engine/resume_manifest.rb +110 -0
  57. data/lib/saboteur/engine/run_budget.rb +10 -7
  58. data/lib/saboteur/engine/run_fingerprint.rb +2 -1
  59. data/lib/saboteur/engine/sandbox_policy.rb +26 -5
  60. data/lib/saboteur/engine/scheduler.rb +33 -25
  61. data/lib/saboteur/engine/selection_announcement.rb +9 -3
  62. data/lib/saboteur/engine/shadow_comparison.rb +121 -0
  63. data/lib/saboteur/engine/shadow_run.rb +38 -0
  64. data/lib/saboteur/engine/subject_selection.rb +144 -64
  65. data/lib/saboteur/engine/suite_partition.rb +22 -0
  66. data/lib/saboteur/engine/test_guess.rb +186 -32
  67. data/lib/saboteur/engine/unchanged_syntax.rb +10 -2
  68. data/lib/saboteur/engine.rb +44 -11
  69. data/lib/saboteur/environment_hints.rb +55 -0
  70. data/lib/saboteur/execution/bundle_gemfile_pin.rb +72 -0
  71. data/lib/saboteur/execution/external_runner.rb +123 -1
  72. data/lib/saboteur/execution/fiddle_loader.rb +34 -0
  73. data/lib/saboteur/execution/gemfile_path_sources.rb +166 -0
  74. data/lib/saboteur/execution/marker_sweep.rb +2 -1
  75. data/lib/saboteur/execution/process_table.rb +7 -3
  76. data/lib/saboteur/execution/reevaluator.rb +7 -0
  77. data/lib/saboteur/execution/sandbox.rb +39 -20
  78. data/lib/saboteur/execution/seatbelt.rb +10 -3
  79. data/lib/saboteur/execution/supervisor.rb +42 -8
  80. data/lib/saboteur/execution/supervisor_batch.rb +105 -0
  81. data/lib/saboteur/execution/test_command_shape.rb +149 -0
  82. data/lib/saboteur/execution/worker_pool.rb +48 -4
  83. data/lib/saboteur/frameworks/rails.rb +2 -5
  84. data/lib/saboteur/frameworks/sqlite_memory_database.rb +32 -2
  85. data/lib/saboteur/gates.rb +121 -28
  86. data/lib/saboteur/install.rb +6 -1
  87. data/lib/saboteur/integration/base.rb +19 -2
  88. data/lib/saboteur/integration/rspec.rb +145 -18
  89. data/lib/saboteur/line_hits.rb +81 -0
  90. data/lib/saboteur/method_calls.rb +91 -0
  91. data/lib/saboteur/mutation_source/argument_swap.rb +54 -0
  92. data/lib/saboteur/mutation_source/builtin.rb +33 -21
  93. data/lib/saboteur/mutation_source/condition_negation.rb +3 -12
  94. data/lib/saboteur/mutation_source/hazard_guard.rb +94 -13
  95. data/lib/saboteur/mutation_source/jump_value.rb +59 -0
  96. data/lib/saboteur/mutation_source/literal_replacement.rb +6 -23
  97. data/lib/saboteur/mutation_source/operator_examples.rb +6 -0
  98. data/lib/saboteur/mutation_source/operator_registry.rb +6 -0
  99. data/lib/saboteur/mutation_source/operator_visit.rb +7 -1
  100. data/lib/saboteur/mutation_source/rescue_removal.rb +69 -0
  101. data/lib/saboteur/mutation_source/unary_removal.rb +47 -0
  102. data/lib/saboteur/mutation_source/when_removal.rb +51 -0
  103. data/lib/saboteur/preview.rb +13 -0
  104. data/lib/saboteur/refinement_activations.rb +22 -10
  105. data/lib/saboteur/reporter/agent.rb +2 -2
  106. data/lib/saboteur/reporter/agent_incomplete.rb +31 -5
  107. data/lib/saboteur/reporter/agent_summary.rb +16 -10
  108. data/lib/saboteur/reporter/review_comments.rb +151 -0
  109. data/lib/saboteur/reporter/safe_out.rb +32 -0
  110. data/lib/saboteur/reporter/stryker.rb +8 -6
  111. data/lib/saboteur/reporter/survivor_classification.rb +41 -3
  112. data/lib/saboteur/reporter/survivor_hints.rb +5 -0
  113. data/lib/saboteur/reporter/terminal.rb +45 -5
  114. data/lib/saboteur/result.rb +14 -4
  115. data/lib/saboteur/selection/coverage_map.rb +150 -9
  116. data/lib/saboteur/selection/external_test_map.rb +4 -0
  117. data/lib/saboteur/selection/git_diff.rb +24 -2
  118. data/lib/saboteur/selection/layout_only_lines.rb +45 -1
  119. data/lib/saboteur/selection/recorder.rb +52 -22
  120. data/lib/saboteur/selection/trace_recorder.rb +83 -0
  121. data/lib/saboteur/selection/untracked_changes.rb +1 -2
  122. data/lib/saboteur/subject.rb +16 -0
  123. data/lib/saboteur/subject_finder.rb +91 -6
  124. data/lib/saboteur/text.rb +31 -0
  125. data/lib/saboteur/version.rb +1 -1
  126. data/lib/saboteur/working_tree.rb +7 -2
  127. data/lib/saboteur.rb +9 -0
  128. metadata +48 -16
@@ -0,0 +1,72 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ require "shellwords"
5
+
6
+ require_relative "test_command_shape"
7
+
8
+ module Saboteur
9
+ module Execution
10
+ # The test command's pinned BUNDLE_GEMFILE, or nil when it does not
11
+ # set one. Every wrapper unwrap strips (VAR= words, env and its
12
+ # flags, timeout, nice, time, bundle exec) keeps assignments in the
13
+ # command's environment, so the prefix it removes is exactly the
14
+ # assignment scope; the LAST BUNDLE_GEMFILE wins like the shell,
15
+ # and an empty last value unsets (#722). A token like
16
+ # A="x"BUNDLE_GEMFILE=g is one assignment to A and sets nothing.
17
+ # Anything BUNDLE_GEMFILE-shaped the scan cannot prove refuses
18
+ # rather than guessing the wrong Gemfile.
19
+ module BundleGemfilePin
20
+ # Raised when BUNDLE_GEMFILE shows up where the scan cannot prove it (#722).
21
+ PIN_UNVERIFIABLE = "the test command sets BUNDLE_GEMFILE where its value cannot be verified; " \
22
+ "pin BUNDLE_GEMFILE as a leading assignment"
23
+
24
+ # A pin value must be a literal: a substitution, tilde, glob, or
25
+ # shell metacharacter makes it unknowable without running the shell (#722).
26
+ PIN_UNVERIFIABLE_VALUE = /[$`\\~;&|<>()*?{}]/
27
+
28
+ module_function
29
+
30
+ def pinned_in(command)
31
+ tokens = Shellwords.split(command)
32
+ rescue ArgumentError
33
+ raise ConfigError, PIN_UNVERIFIABLE if command.include?("BUNDLE_GEMFILE")
34
+ else
35
+ pin = pin_value(tokens)
36
+ raise ConfigError, PIN_UNVERIFIABLE if pin == :unverifiable
37
+
38
+ pin
39
+ end
40
+
41
+ # nil for no pin, :unverifiable for a BUNDLE_GEMFILE the scan
42
+ # cannot prove, the literal value otherwise.
43
+ def pin_value(tokens)
44
+ prefix = tokens.first(tokens.size - TestCommandShape.unwrap(tokens).size)
45
+ value = prefix.reverse.find { |word| word.match?(/\ABUNDLE_GEMFILE=/) }&.split("=", 2)&.last
46
+ return :unverifiable if unverifiable_shape?(tokens, value)
47
+ return nil if value.nil? || value.empty?
48
+ return :unverifiable if value.match?(PIN_UNVERIFIABLE_VALUE)
49
+
50
+ value
51
+ end
52
+
53
+ # BUNDLE_GEMFILE in a shape the scan cannot prove: env rewriting the
54
+ # run's cwd or variables, or the pin outside the leading assignments.
55
+ def unverifiable_shape?(tokens, value)
56
+ mentioned = tokens.any? { |word| word.include?("BUNDLE_GEMFILE") }
57
+ (mentioned && env_unverifiable?(tokens)) ||
58
+ (value.nil? && tokens.any? { |word| word.match?(/(?:\A|[^A-Za-z0-9_])BUNDLE_GEMFILE=/) })
59
+ end
60
+
61
+ # env(1) carrying a cwd-repointing (-C/--chdir), word-reparsing
62
+ # (-S/--split-string), or variable-unsetting (-u/--unset) flag: the
63
+ # pin scan cannot prove the environment the command runs in (#722).
64
+ def env_unverifiable?(tokens)
65
+ tokens.each_index.select { |i| %w[env /usr/bin/env].include?(tokens[i]) }.any? do |i|
66
+ tokens[(i + 1)..].take_while { |word| word.start_with?("-") }
67
+ .any? { |flag| flag.match?(/\A(?:-C|-S|-u|--chdir|--split-string|--unset)/) }
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -74,12 +74,19 @@ module Saboteur
74
74
  # holding the pipe past this no longer blocks the run.
75
75
  DRAIN_GRACE = 0.5
76
76
 
77
- def initialize(command:, root:)
77
+ def initialize(command:, root:, path_sources: nil)
78
78
  @command = command
79
79
  # realpath, not expand_path: the tree must be judged by where it
80
80
  # really is, so a subject path can be checked against the same
81
81
  # canonical root without a symlink making it look outside (#305 QA).
82
82
  @root = File.realpath(root)
83
+ # The Gemfile the command actually loads, root-relative: its own
84
+ # pinned BUNDLE_GEMFILE when it sets one (Appraisal style), else
85
+ # the root Gemfile (#644).
86
+ pinned = pinned_gemfile
87
+ @gemfile_name = pinned || "Gemfile"
88
+ sources = path_sources || GemfilePathSources.resolve(@root, gemfile: pinned)
89
+ @path_sources = classify_path_sources(sources)
83
90
  @copy_root = nil
84
91
  at_exit { close }
85
92
  rescue SystemCallError
@@ -211,9 +218,106 @@ module Saboteur
211
218
  @copy_root = File.realpath(Dir.mktmpdir("saboteur-external-"))
212
219
  FileUtils.cp_r(File.join(@root, "."), @copy_root, preserve: true)
213
220
  rebuild_symlinks
221
+ rebase_path_sources
214
222
  yield
215
223
  end
216
224
 
225
+ # path: sources are judged against the canonical root (#644): a
226
+ # RELATIVE source resolving outside the tree would escape the
227
+ # disposable copy, so it refuses the run at startup naming the
228
+ # source; an ABSOLUTE source pointing into the original tree is
229
+ # rebased into the copy below so bundler loads the copy's (possibly
230
+ # mutated) code, never the original's. Anything unresolved or
231
+ # ambiguous refuses - never a guess that could load unmutated code.
232
+ def classify_path_sources(sources)
233
+ sources.each_with_object(rebase: []) do |src, groups|
234
+ expanded = GemfilePathSources.canonical_path(src.expanded)
235
+ # The root itself counts as inside: a `gemspec` line makes
236
+ # Bundler report the gem's own path source ".", expanded to the
237
+ # project root - refusing it would refuse every gem-style
238
+ # project (#644 security).
239
+ inside = expanded == @root || expanded.start_with?("#{@root}/")
240
+ next if inside && !Pathname.new(src.original).absolute?
241
+ next if !inside && Pathname.new(src.original).absolute?
242
+
243
+ unless inside
244
+ raise ConfigError, "the Gemfile's path: source \"#{src.name}\" (#{src.original}) resolves outside the " \
245
+ "project root (#{expanded}); external test commands need a self-contained tree"
246
+ end
247
+
248
+ groups[:rebase] << [src, expanded]
249
+ end
250
+ end
251
+
252
+ # Rewrites each rebased source in the COPY's Gemfile (quoted option)
253
+ # and Gemfile.lock (PATH remote line). Zero or several matches mean
254
+ # the file computes the path or disagrees with Bundler's resolution -
255
+ # refuse rather than risk loading the original tree (#644).
256
+ def rebase_path_sources
257
+ @path_sources[:rebase].each do |src, expanded|
258
+ copy_path = expanded == @root ? @copy_root : File.join(@copy_root, expanded.delete_prefix("#{@root}/"))
259
+ rewrite_gemfile_source(src, copy_path)
260
+ rewrite_lockfile_source(src, expanded, copy_path)
261
+ end
262
+ end
263
+
264
+ def rewrite_gemfile_source(src, copy_path)
265
+ path = File.join(@copy_root, @gemfile_name)
266
+ text = File.read(path)
267
+ pattern = /(path:\s*["'])#{Regexp.escape(src.original)}(["'])/
268
+ raise ConfigError, ambiguous_source_message(src) unless text.scan(pattern).size == 1
269
+
270
+ File.write(path, text.sub(pattern) { "#{Regexp.last_match(1)}#{copy_path}#{Regexp.last_match(2)}" })
271
+ end
272
+
273
+ def rewrite_lockfile_source(src, expanded, copy_path)
274
+ # Bundler names the lockfile after the Gemfile: <gemfile>.lock.
275
+ path = File.join(@copy_root, "#{@gemfile_name}.lock")
276
+ return unless File.file?(path)
277
+
278
+ text = File.read(path)
279
+ # Bundler spells an in-project absolute path: as the root-relative
280
+ # remote ("vend/g" for /root/vend/g, "." for the root), and that
281
+ # spelling already resolves inside the copy (#722): accept it as
282
+ # is. Only the absolute spelling needs the rewrite, and only a
283
+ # duplicate or a total miss refuses - the lockfile then disagrees
284
+ # with Bundler's own resolution. The relative spelling must come
285
+ # from the CANONICAL expanded: src.expanded is Bundler's textual
286
+ # expand_path, which never matches the realpath'd root behind a
287
+ # symlinked one (macOS /var -> /private/var, #732).
288
+ spellings = ([src.original, expanded == @root ? "." : expanded.delete_prefix("#{@root}/")] - [""]).uniq
289
+ unless text.scan(/^(\s*remote: )(?:#{spellings.map { |s| Regexp.escape(s) }.join('|')})$/).size == 1
290
+ raise ConfigError, "could not rebase the lockfile's PATH remote for path: source \"#{src.name}\" " \
291
+ "(#{src.original}); the lockfile must record it exactly once - run bundle install"
292
+ end
293
+ absolute = /^(\s*remote: )#{Regexp.escape(src.original)}$/
294
+ # Write back only when the absolute spelling matched: a no-match sub
295
+ # would write identical bytes, which still fails on a read-only
296
+ # lockfile carried into the copy (#729 security).
297
+ File.write(path, text.sub(absolute) { "#{Regexp.last_match(1)}#{copy_path}" }) if text.match?(absolute)
298
+ end
299
+
300
+ def ambiguous_source_message(src)
301
+ "could not rebase the Gemfile's path: source \"#{src.name}\" (#{src.original}) into the disposable copy; " \
302
+ "external test commands need a literal path: option they can rewrite"
303
+ end
304
+
305
+ # A command-pinned Gemfile must live inside the tree, or the copy
306
+ # would read a Gemfile - and its path: sources - from outside (#644).
307
+ # Returns its root-relative path, or nil for the root Gemfile.
308
+ def pinned_gemfile
309
+ pinned = BundleGemfilePin.pinned_in(@command)
310
+ return nil unless pinned
311
+
312
+ expanded = GemfilePathSources.canonical_path(File.expand_path(pinned, @root))
313
+ unless expanded == @root || expanded.start_with?("#{@root}/")
314
+ raise ConfigError, "the test command pins BUNDLE_GEMFILE (#{pinned}) outside the project root " \
315
+ "(#{expanded}); external test commands need the Gemfile inside the tree"
316
+ end
317
+
318
+ expanded.delete_prefix("#{@root}/")
319
+ end
320
+
217
321
  # cp_r keeps symlinks, and a write through a link that escapes the
218
322
  # copy would land in the real tree. Links resolving inside the tree
219
323
  # are REBUILT as relative links inside the copy: alias identity is
@@ -335,6 +439,7 @@ module Saboteur
335
439
  if defined?(Bundler)
336
440
  original = Bundler.original_env
337
441
  BUNDLER_PASSTHROUGH_KEYS.each { |key| env[key] = original[key] if original.key?(key) }
442
+ restore_dropped_rubylib_entries(env, original)
338
443
  end
339
444
  if (rubyopt = env["RUBYOPT"])
340
445
  rubyopt = rubyopt.gsub(%r{\s*-rbundler/setup}, "").strip
@@ -343,6 +448,23 @@ module Saboteur
343
448
  env.merge(CHILD_MARKER => "1")
344
449
  end
345
450
 
451
+ # bundler <=2.5 scrubs even shell-exported entries (verified
452
+ # 2.5.22); bundler >=2.6.0 restores the pre-bundler value wholesale
453
+ # (verified 2.6.0/2.6.9), where this restore is a no-op. A
454
+ # relocated/toolcache Ruby needs that entry to find its stdlib
455
+ # (#584). Rebuild the list from the pre-bundler one - RUBYLIB is
456
+ # ordered, and two directories providing the same feature load in
457
+ # path order - then append entries only the scrubbed list has;
458
+ # bundler's own additions (absent from the pre-bundler value) stay
459
+ # dropped, and duplicates collapse to first occurrence.
460
+ def restore_dropped_rubylib_entries(env, original)
461
+ before = original["RUBYLIB"].to_s.split(File::PATH_SEPARATOR)
462
+ return if before.empty?
463
+
464
+ after = env["RUBYLIB"].to_s.split(File::PATH_SEPARATOR)
465
+ env["RUBYLIB"] = (before | after).join(File::PATH_SEPARATOR)
466
+ end
467
+
346
468
  # Only a failing exit proves the command saw the broken file; a
347
469
  # pass is the anchoring or no-coverage case, and a timeout or setup
348
470
  # failure leaves visibility unknown. Both warn and name the file.
@@ -0,0 +1,34 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ module Saboteur
5
+ module Execution
6
+ # The one guarded door for fiddle (#639). Ruby ships fiddle itself - a
7
+ # default gem on 3.x, a bundled gem from 4.0 - so a run never needs to
8
+ # compile it, and a box where it cannot load has a broken Ruby or a
9
+ # missing Gemfile line, not a reason to run mutants without the sandbox
10
+ # layers. Every load goes through here and a failure REFUSES with the
11
+ # fixes, instead of a bundler-time source build dying on missing ffi.h.
12
+ module FiddleLoader
13
+ MESSAGE = "saboteur cannot load fiddle, which Ruby normally includes " \
14
+ "(a default gem on Ruby 3.x, a bundled gem on Ruby 4.0+), " \
15
+ "and refuses to run without its sandbox layers. Fix one of: " \
16
+ "reinstall Ruby so its shipped fiddle returns; " \
17
+ "run `gem install fiddle`; " \
18
+ "add `gem \"fiddle\"` to the Gemfile (required on Ruby 4.0+); " \
19
+ "or install the libffi headers (libffi-dev on Debian/Ubuntu, " \
20
+ "libffi-devel on Fedora/RHEL) and reinstall fiddle."
21
+
22
+ # Loads fiddle (plus fiddle/import for the one caller using its
23
+ # mixin). require is idempotent, so every touchpoint asks again for
24
+ # free. A LoadError means the sandbox layers cannot work: REFUSE -
25
+ # the error propagates, it is never downgraded to a warning.
26
+ def self.require_fiddle!(with_import: false)
27
+ require "fiddle"
28
+ require "fiddle/import" if with_import
29
+ rescue LoadError
30
+ raise Error, MESSAGE
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,166 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require "open3"
6
+
7
+ module Saboteur
8
+ module Execution
9
+ # Resolves a Gemfile's path: sources with Bundler's OWN parser in a
10
+ # short-lived child process (#644). Never a line scan: a wrong guess
11
+ # about a path: source silently loads unmutated code in the disposable
12
+ # copy. Fail closed - a Gemfile that cannot be parsed, a resolver that
13
+ # times out, or a path: source outside the project root refuses the
14
+ # run with exit 2 (ConfigError) naming the source.
15
+ # Resolving EVALUATES the project's Gemfile in that child at
16
+ # ExternalRunner.new - arbitrary Ruby with the caller's permissions,
17
+ # the same trust level as the bundle exec in the test command itself.
18
+ module GemfilePathSources
19
+ Source = Data.define(:name, :original, :expanded)
20
+
21
+ RESOLVE_TIMEOUT = 15
22
+
23
+ # Bundler::Dsl, not Bundler::Definition: the Gemfile must be PARSED by
24
+ # Bundler itself (it is arbitrary Ruby; a line scan can silently miss
25
+ # or invent a path: source and load unmutated code), but resolving the
26
+ # whole bundle is not needed and fails on half-installed projects.
27
+ RESOLVER = <<~RUBY
28
+ dsl = Bundler::Dsl.new
29
+ dsl.eval_gemfile(ENV["BUNDLE_GEMFILE"] || "Gemfile")
30
+ out = dsl.dependencies.filter_map do |dep|
31
+ src = dep.source
32
+ next unless src.is_a?(Bundler::Source::Path)
33
+
34
+ { "name" => dep.name, "original" => src.path.to_s, "expanded" => src.expanded_original_path.to_s }
35
+ end
36
+ puts JSON.generate(out)
37
+ RUBY
38
+
39
+ module_function
40
+
41
+ # Every path: source in the project's Gemfile, resolved by Bundler
42
+ # itself. Raises ConfigError on any failure to parse or resolve.
43
+ # gemfile pins the command's own BUNDLE_GEMFILE (Appraisal style):
44
+ # that file's path: sources are the ones the run loads, so it is
45
+ # the one parsed (#644).
46
+ def resolve(root, env: ENV, timeout: RESOLVE_TIMEOUT, gemfile: nil)
47
+ path = gemfile ? File.expand_path(gemfile, root) : File.join(root, "Gemfile")
48
+ if gemfile && !File.file?(path)
49
+ raise ConfigError, "the test command pins BUNDLE_GEMFILE (#{gemfile}) but #{path} does not exist"
50
+ end
51
+
52
+ return [] unless File.file?(path)
53
+
54
+ stdout, stderr, status = run_resolver(root, env, timeout, gemfile: path)
55
+ unless status.success?
56
+ detail = stderr.to_s.lines.first(3).join.strip
57
+ detail = "resolver exited #{status.exitstatus}" if detail.empty?
58
+ raise ConfigError, "could not parse the Gemfile's path: sources (#{detail}); " \
59
+ "external test commands need the Gemfile to resolve"
60
+ end
61
+
62
+ JSON.parse(stdout).map do |row|
63
+ Source.new(name: row["name"], original: row["original"], expanded: row["expanded"])
64
+ end
65
+ rescue JSON::ParserError
66
+ raise ConfigError, "could not parse the Gemfile's path: sources (resolver returned unreadable output); " \
67
+ "external test commands need the Gemfile to resolve"
68
+ end
69
+
70
+ # realpath, canonicalizing the deepest EXISTING ancestor when the
71
+ # path itself does not exist yet: behind a symlinked root (macOS
72
+ # /var -> /private/var) a textual expand_path can never match the
73
+ # realpath'd root, so an absolute path: source into the tree read
74
+ # as outside and silently skipped the rebase (#644, #721 macOS).
75
+ def canonical_path(path)
76
+ missing = []
77
+ current = path
78
+ until File.exist?(current)
79
+ missing.unshift(File.basename(current))
80
+ parent = File.dirname(current)
81
+ break if parent == current
82
+
83
+ current = parent
84
+ end
85
+ base = File.exist?(current) ? File.realpath(current) : File.expand_path(current)
86
+ File.join(base, *missing)
87
+ rescue SystemCallError
88
+ File.expand_path(path)
89
+ end
90
+
91
+ def run_resolver(root, env, timeout, gemfile: nil)
92
+ # Spawn env semantics: a key ABSENT from the hash is inherited, so
93
+ # the bundler anchors must be explicitly unset with nil, or the
94
+ # child resolves saboteur's own Gemfile instead of the project's.
95
+ # Both prefixes: rubygems itself requires ENV["BUNDLER_SETUP"] in
96
+ # gem_prelude, which would resolve the whole bundle at child boot.
97
+ child_env = env.each_key.with_object({}) do |key, memo|
98
+ memo[key] = nil if key.start_with?("BUNDLE_", "BUNDLER_") || key == "RUBYOPT"
99
+ end
100
+ # The Gemfile to parse, re-pinned after the anchor wipe: the
101
+ # command's own BUNDLE_GEMFILE when it sets one (Appraisal style),
102
+ # else the root Gemfile (#644).
103
+ child_env["BUNDLE_GEMFILE"] = gemfile || File.join(root, "Gemfile")
104
+ # Plain ruby, NOT bundle exec: bundle exec resolves the whole bundle
105
+ # before running, which refuses half-installed projects we are only
106
+ # trying to parse (#644). The child leads its own process group so a
107
+ # timeout can KILL the whole group, not only the ruby pid (#644).
108
+ cmd = ["ruby", "-rbundler", "-rjson", "-e", RESOLVER]
109
+ stdin, stdout, stderr, wait = Open3.popen3(child_env, *cmd, chdir: root, pgroup: true)
110
+ stdin.close
111
+ await_resolver(stdout, stderr, wait, timeout)
112
+ ensure
113
+ [stdin, stdout, stderr].each { |io| io&.close unless io&.closed? }
114
+ end
115
+
116
+ # Drains both pipes while waiting: a child that fills a pipe buffer
117
+ # (~64 KB) blocks on write and never exits, which would read as a
118
+ # fake timeout (#644). The readers accumulate partial chunks, so a
119
+ # still-open pipe (a spawned grandchild that left the process group
120
+ # holds the write end) can be abandoned after a short grace instead
121
+ # of blocking on EOF - on the timeout path the captured output is
122
+ # discarded, and on a clean exit incomplete output cannot be
123
+ # trusted, so the run refuses either way (#722).
124
+ def await_resolver(stdout, stderr, wait, timeout)
125
+ out_buffer = +""
126
+ err_buffer = +""
127
+ readers = [pipe_reader(stdout, out_buffer), pipe_reader(stderr, err_buffer)]
128
+ timed_out = resolver_timed_out?(wait, timeout)
129
+ readers_done = readers.all? { |reader| reader.join(2) }
130
+ readers.each { |reader| reader.kill if reader.alive? }
131
+ if timed_out
132
+ raise ConfigError, "could not parse the Gemfile's path: sources (resolver timed out after #{timeout} s); " \
133
+ "external test commands need the Gemfile to resolve"
134
+ end
135
+ unless readers_done
136
+ raise ConfigError, "could not parse the Gemfile's path: sources (a resolver-spawned process still holds " \
137
+ "its pipes open); external test commands need the Gemfile to resolve"
138
+ end
139
+
140
+ [out_buffer, err_buffer, wait.value]
141
+ end
142
+
143
+ # true when the resolver missed its deadline and its whole process
144
+ # group was KILLed.
145
+ def resolver_timed_out?(wait, timeout)
146
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
147
+ until wait.join(0.05)
148
+ next unless Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
149
+
150
+ Process.kill("KILL", -wait.pid)
151
+ wait.join
152
+ return true
153
+ end
154
+ false
155
+ end
156
+
157
+ def pipe_reader(io, buffer)
158
+ Thread.new do
159
+ loop { buffer << io.readpartial(65_536) }
160
+ rescue IOError # EOFError < IOError
161
+ buffer
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
@@ -1,7 +1,7 @@
1
1
  # SPDX-License-Identifier: MIT
2
2
  # frozen_string_literal: true
3
3
 
4
- require "fiddle"
4
+ require_relative "fiddle_loader"
5
5
 
6
6
  module Saboteur
7
7
  module Execution
@@ -33,6 +33,7 @@ module Saboteur
33
33
  # usable prctl (macOS) the environment marker still names exec'd
34
34
  # descendants and the tree still tracks the rest.
35
35
  def write_comm(marker)
36
+ FiddleLoader.require_fiddle!
36
37
  prctl = Fiddle::Function.new(Fiddle::Handle::DEFAULT["prctl"],
37
38
  [Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP] + ([Fiddle::TYPE_LONG] * 3),
38
39
  Fiddle::TYPE_INT)
@@ -1,7 +1,7 @@
1
1
  # SPDX-License-Identifier: MIT
2
2
  # frozen_string_literal: true
3
3
 
4
- require "fiddle"
4
+ require_relative "fiddle_loader"
5
5
 
6
6
  module Saboteur
7
7
  module Execution
@@ -111,14 +111,18 @@ module Saboteur
111
111
  return @functions if defined?(@functions)
112
112
 
113
113
  @functions = begin
114
- require "fiddle"
114
+ FiddleLoader.require_fiddle!
115
115
  library = Fiddle.dlopen("/usr/lib/libproc.dylib")
116
116
  [Fiddle::Function.new(library["proc_listallpids"], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT],
117
117
  Fiddle::TYPE_INT),
118
118
  Fiddle::Function.new(library["proc_pidinfo"],
119
119
  [Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_LONG_LONG,
120
120
  Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT], Fiddle::TYPE_INT)]
121
- rescue LoadError, Fiddle::DLError
121
+ # Error first: the loader's refusal (#639) must match even when
122
+ # Fiddle is undefined (the DLError constant would not resolve).
123
+ # The process table is not a sandbox layer, so it keeps
124
+ # degrading to the next backend.
125
+ rescue Error, LoadError, Fiddle::DLError
122
126
  nil
123
127
  end
124
128
  end
@@ -191,6 +191,13 @@ module Saboteur
191
191
  source = subject.file_source.dup.force_encoding(Encoding::UTF_8)
192
192
  spliced = source.byteslice(0, subject.start_byte) + text + source.byteslice(subject.end_byte..)
193
193
  silencing_warnings { TOPLEVEL_BINDING.eval(spliced, subject.file, 1) }
194
+ # The eval just loaded the file for this run: a later lazy require
195
+ # of it (dry-rb load_extensions-style) must be a no-op, or its
196
+ # re-assignment of the file's constants fires "already initialized
197
+ # constant" - fatal in --warnings suites with a raising Warning
198
+ # gate, which wrongly reports every mutant as an error (#632).
199
+ loaded = File.expand_path(subject.file)
200
+ $LOADED_FEATURES << loaded unless $LOADED_FEATURES.include?(loaded)
194
201
  end
195
202
 
196
203
  def eval_in_owner(owner, subject, text, extra_lines)
@@ -1,8 +1,7 @@
1
1
  # SPDX-License-Identifier: MIT
2
2
  # frozen_string_literal: true
3
3
 
4
- require "fiddle"
5
- require "fiddle/import"
4
+ require_relative "fiddle_loader"
6
5
 
7
6
  module Saboteur
8
7
  module Execution
@@ -57,37 +56,47 @@ module Saboteur
57
56
  return @abi_version if defined?(@abi_version)
58
57
  return @abi_version = 0 unless linux?
59
58
 
60
- # Build the errno reader BEFORE the probe: its lazy dlopen/dlsym run
61
- # libc calls that can set errno, so building it between the probe and
62
- # the read can replace the real answer with a stale 0.
63
- LibC.errno_location_fn
64
- version, errno = probe_once
65
- # A failed syscall always sets errno, so (failure, errno 0) is never
66
- # the kernel's real answer - the read was clobbered between the call
67
- # and here. Probe once more, now with every Fiddle function warm.
68
- version, errno = probe_once if !version.positive? && errno.zero?
69
- @abi_version = probe_abi(version, errno)
59
+ # Refuses when fiddle cannot load (#639): the sandbox is mandatory,
60
+ # so this error must reach the run, not the DLError degrade below.
61
+ FiddleLoader.require_fiddle!(with_import: true)
62
+ @abi_version = probed_abi_version
63
+ end
64
+
65
+ # Fiddle holds the GVL for the call (need_gvl: true) and saves errno in
66
+ # Fiddle.last_error in the same C function, right after ffi_call, so no
67
+ # Ruby code or GVL hand-off runs between the failed syscall and its
68
+ # errno. Without need_gvl, Fiddle releases the GVL for the call and reads
69
+ # errno only after reacquiring it, and the old second Fiddle call to
70
+ # __errno_location came later still: a stale errno could replace the
71
+ # real answer, first 0 (#350), then EINVAL, which the kernel never
72
+ # returns for a valid version probe (it answers EINVAL only for a
73
+ # non-NULL attr or a non-zero size).
74
+ def probed_abi_version
75
+ version = LibC.probe_fn.call(SYS_CREATE_RULESET, 0, 0, CREATE_RULESET_VERSION, 0)
76
+ probe_abi(version, Fiddle.last_error)
70
77
  rescue Fiddle::DLError
71
78
  # No usable libc (an unusual musl/ldso layout): the sandbox cannot
72
79
  # be built here, so degrade to the unsandboxed note, not a crash.
73
- @abi_version = 0
80
+ 0
74
81
  end
75
82
 
76
83
  # A positive answer is the ABI. ENOSYS/EOPNOTSUPP mean the kernel has
77
84
  # no Landlock to give: run unsandboxed, noted once. Anything else
78
85
  # (EPERM under a seccomp policy, ...) means the kernel is there but
79
86
  # refused - fail closed rather than run without the promised sandbox.
80
- # One create-ruleset version probe with its errno read paired to it.
81
- def probe_once
82
- version = LibC.syscall_fn.call(SYS_CREATE_RULESET, 0, 0, CREATE_RULESET_VERSION, 0)
83
- [version, last_errno]
84
- end
85
-
87
+ # The message names the kernel so a run on an unexpected runner says
88
+ # what it ran on.
86
89
  def probe_abi(version, errno)
87
90
  return version if version.positive?
88
91
  return 0 if UNSUPPORTED_ERRNOS.include?(errno)
89
92
 
90
- raise SystemCallError.new("landlock_create_ruleset probe", errno)
93
+ raise SystemCallError.new("landlock_create_ruleset probe (kernel #{kernel_release})", errno)
94
+ end
95
+
96
+ def kernel_release
97
+ File.read("/proc/sys/kernel/osrelease").strip
98
+ rescue SystemCallError
99
+ "unknown"
91
100
  end
92
101
 
93
102
  def available?
@@ -249,6 +258,16 @@ module Saboteur
249
258
  nil
250
259
  end
251
260
 
261
+ # The same syscall, built to run with the GVL held: see probed_abi_version.
262
+ def probe_fn
263
+ @probe_fn ||= Fiddle::Function.new(
264
+ handle["syscall"],
265
+ [Fiddle::TYPE_LONG, Fiddle::TYPE_LONG, Fiddle::TYPE_LONG, Fiddle::TYPE_LONG, Fiddle::TYPE_LONG],
266
+ Fiddle::TYPE_LONG,
267
+ need_gvl: true
268
+ )
269
+ end
270
+
252
271
  def syscall_fn
253
272
  @syscall_fn ||= Fiddle::Function.new(
254
273
  handle["syscall"],
@@ -1,7 +1,7 @@
1
1
  # SPDX-License-Identifier: MIT
2
2
  # frozen_string_literal: true
3
3
 
4
- require "fiddle"
4
+ require_relative "fiddle_loader"
5
5
 
6
6
  module Saboteur
7
7
  module Execution
@@ -50,9 +50,16 @@ module Saboteur
50
50
  return @probe if defined?(@probe)
51
51
  return @probe = :unsupported_platform unless darwin?
52
52
 
53
- @probe = run_probe
53
+ # Refuses when fiddle cannot load (#639), before the DLError
54
+ # degrade below can turn it into a silent :unavailable.
55
+ FiddleLoader.require_fiddle!
56
+ @probe = probed
57
+ end
58
+
59
+ def probed
60
+ run_probe
54
61
  rescue Fiddle::DLError
55
- @probe = :unavailable
62
+ :unavailable
56
63
  end
57
64
 
58
65
  def available?