runtime_analysis 1.0.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 (267) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +115 -0
  3. data/LICENSE.txt +96 -0
  4. data/README.md +164 -0
  5. data/editor/nvim/README.md +278 -0
  6. data/editor/nvim/lua/runtime_analysis/connected.lua +565 -0
  7. data/editor/nvim/lua/runtime_analysis/go_to_class.lua +63 -0
  8. data/editor/nvim/lua/runtime_analysis/inline.lua +138 -0
  9. data/editor/nvim/lua/runtime_analysis/lens.lua +50 -0
  10. data/editor/nvim/lua/runtime_analysis/lsp.lua +92 -0
  11. data/editor/nvim/lua/runtime_analysis/menu.lua +98 -0
  12. data/editor/nvim/lua/runtime_analysis/preview.lua +70 -0
  13. data/editor/nvim/lua/runtime_analysis/project.lua +28 -0
  14. data/editor/nvim/lua/runtime_analysis/rerun.lua +39 -0
  15. data/editor/nvim/lua/runtime_analysis/text.lua +23 -0
  16. data/editor/nvim/lua/runtime_analysis/value_popup.lua +45 -0
  17. data/editor/nvim/plugin/runtime_analysis.lua +71 -0
  18. data/examples/trackers/PROMPT.md +86 -0
  19. data/examples/trackers/honeybadger.rb +19 -0
  20. data/examples/trackers/sentry.rb +16 -0
  21. data/ext/runtime_analysis/extconf.rb +68 -0
  22. data/ext/runtime_analysis/fast.c +775 -0
  23. data/ext/runtime_analysis/fast.h +59 -0
  24. data/ext/runtime_analysis/hold.c +560 -0
  25. data/ext/runtime_analysis/init.c +36 -0
  26. data/ext/runtime_analysis/names.c +78 -0
  27. data/ext/runtime_analysis/names.h +28 -0
  28. data/ext/runtime_analysis/novelty.c +139 -0
  29. data/ext/runtime_analysis/novelty.h +22 -0
  30. data/ext/runtime_analysis/os.c +73 -0
  31. data/ext/runtime_analysis/os.h +32 -0
  32. data/ext/runtime_analysis/ring.c +445 -0
  33. data/ext/runtime_analysis/rows.c +304 -0
  34. data/ext/runtime_analysis/sink.c +485 -0
  35. data/ext/runtime_analysis/sink.h +240 -0
  36. data/ext/runtime_analysis/state.c +254 -0
  37. data/ext/runtime_analysis/state.h +57 -0
  38. data/ext/runtime_analysis/wrapper.c +278 -0
  39. data/ext/runtime_analysis/wrapper.h +24 -0
  40. data/lib/runtime_analysis/errors.rb +34 -0
  41. data/lib/runtime_analysis/failsafe.rb +123 -0
  42. data/lib/runtime_analysis/host/controller_entry_point.rb +18 -0
  43. data/lib/runtime_analysis/host/declared.rb +152 -0
  44. data/lib/runtime_analysis/host/inject.rb +90 -0
  45. data/lib/runtime_analysis/host/job_runners.rb +27 -0
  46. data/lib/runtime_analysis/host/middleware.rb +23 -0
  47. data/lib/runtime_analysis/host/minitest_integration.rb +22 -0
  48. data/lib/runtime_analysis/host/railtie.rb +42 -0
  49. data/lib/runtime_analysis/host/reports.rb +54 -0
  50. data/lib/runtime_analysis/host/rspec_integration.rb +17 -0
  51. data/lib/runtime_analysis/host/web.rb +22 -0
  52. data/lib/runtime_analysis/host.rb +34 -0
  53. data/lib/runtime_analysis/inject.rb +5 -0
  54. data/lib/runtime_analysis/minitest.rb +9 -0
  55. data/lib/runtime_analysis/native/build.rb +17 -0
  56. data/lib/runtime_analysis/native/byte_size.rb +27 -0
  57. data/lib/runtime_analysis/native/call_site_logger/definitions.rb +51 -0
  58. data/lib/runtime_analysis/native/call_site_logger/held.rb +35 -0
  59. data/lib/runtime_analysis/native/call_site_logger/rows.rb +110 -0
  60. data/lib/runtime_analysis/native/call_site_logger/segments.rb +100 -0
  61. data/lib/runtime_analysis/native/call_site_logger/streams.rb +110 -0
  62. data/lib/runtime_analysis/native/call_site_logger.rb +80 -0
  63. data/lib/runtime_analysis/native/rotation_policy.rb +28 -0
  64. data/lib/runtime_analysis/native/sanitizer_runtime.rb +50 -0
  65. data/lib/runtime_analysis/native/sink.rb +12 -0
  66. data/lib/runtime_analysis/native.rb +74 -0
  67. data/lib/runtime_analysis/platform.rb +30 -0
  68. data/lib/runtime_analysis/query/index/anonymise.rb +98 -0
  69. data/lib/runtime_analysis/query/index/graph.rb +107 -0
  70. data/lib/runtime_analysis/query/index/graph_cache.rb +32 -0
  71. data/lib/runtime_analysis/query/index/reading/declared.rb +42 -0
  72. data/lib/runtime_analysis/query/index/reading/dict.rb +43 -0
  73. data/lib/runtime_analysis/query/index/reading/frame.rb +27 -0
  74. data/lib/runtime_analysis/query/index/reading/node.rb +45 -0
  75. data/lib/runtime_analysis/query/index/reading/path.rb +21 -0
  76. data/lib/runtime_analysis/query/index/reading/reruns.rb +30 -0
  77. data/lib/runtime_analysis/query/index/reading/rows.rb +128 -0
  78. data/lib/runtime_analysis/query/index/reading/stream.rb +82 -0
  79. data/lib/runtime_analysis/query/index/reading/tree.rb +66 -0
  80. data/lib/runtime_analysis/query/index/reading/tree_builder.rb +135 -0
  81. data/lib/runtime_analysis/query/index/reading/values.rb +49 -0
  82. data/lib/runtime_analysis/query/index/reading.rb +80 -0
  83. data/lib/runtime_analysis/query/index/shapes.rb +142 -0
  84. data/lib/runtime_analysis/query/index/signatures/active_support.rb +31 -0
  85. data/lib/runtime_analysis/query/index/signatures/core.json +1 -0
  86. data/lib/runtime_analysis/query/index/signatures/layers.rb +29 -0
  87. data/lib/runtime_analysis/query/index/signatures/lineage.rb +57 -0
  88. data/lib/runtime_analysis/query/index/signatures/overloads.rb +32 -0
  89. data/lib/runtime_analysis/query/index/signatures/rbs/entry.rb +95 -0
  90. data/lib/runtime_analysis/query/index/signatures/rbs/export.rb +76 -0
  91. data/lib/runtime_analysis/query/index/signatures/rbs/library.rb +43 -0
  92. data/lib/runtime_analysis/query/index/signatures/rbs/overload.rb +66 -0
  93. data/lib/runtime_analysis/query/index/signatures/rbs/project.rb +69 -0
  94. data/lib/runtime_analysis/query/index/signatures/rbs/types.rb +79 -0
  95. data/lib/runtime_analysis/query/index/signatures/rbs.rb +57 -0
  96. data/lib/runtime_analysis/query/index/signatures/sorbet/sig.rb +48 -0
  97. data/lib/runtime_analysis/query/index/signatures/sorbet/signed.rb +69 -0
  98. data/lib/runtime_analysis/query/index/signatures/sorbet/table.rb +43 -0
  99. data/lib/runtime_analysis/query/index/signatures/sorbet/types.rb +96 -0
  100. data/lib/runtime_analysis/query/index/signatures/sorbet.rb +150 -0
  101. data/lib/runtime_analysis/query/index/signatures/sources.rb +80 -0
  102. data/lib/runtime_analysis/query/index/signatures/written.rb +45 -0
  103. data/lib/runtime_analysis/query/index/signatures.rb +93 -0
  104. data/lib/runtime_analysis/query/index/source_named_graph.rb +84 -0
  105. data/lib/runtime_analysis/query/index/static/change_set.rb +20 -0
  106. data/lib/runtime_analysis/query/index/static/changes.rb +80 -0
  107. data/lib/runtime_analysis/query/index/static/code_visitor.rb +144 -0
  108. data/lib/runtime_analysis/query/index/static/klass.rb +13 -0
  109. data/lib/runtime_analysis/query/index/static/method.rb +19 -0
  110. data/lib/runtime_analysis/query/index/static/params.rb +52 -0
  111. data/lib/runtime_analysis/query/index/static/source.rb +69 -0
  112. data/lib/runtime_analysis/query/index/static/stub.rb +14 -0
  113. data/lib/runtime_analysis/query/index/static/stub_visitor.rb +133 -0
  114. data/lib/runtime_analysis/query/index/static/value.rb +17 -0
  115. data/lib/runtime_analysis/query/index/static/values.rb +71 -0
  116. data/lib/runtime_analysis/query/index/static/visibility.rb +40 -0
  117. data/lib/runtime_analysis/query/index/static.rb +85 -0
  118. data/lib/runtime_analysis/query/index.rb +32 -0
  119. data/lib/runtime_analysis/query/nodes.rb +60 -0
  120. data/lib/runtime_analysis/query/project/config.rb +108 -0
  121. data/lib/runtime_analysis/query/project.rb +40 -0
  122. data/lib/runtime_analysis/query/report/document.rb +84 -0
  123. data/lib/runtime_analysis/query/report/finding.rb +15 -0
  124. data/lib/runtime_analysis/query/report/json_renderer.rb +51 -0
  125. data/lib/runtime_analysis/query/report/list.rb +15 -0
  126. data/lib/runtime_analysis/query/report/location.rb +14 -0
  127. data/lib/runtime_analysis/query/report/one_row.rb +14 -0
  128. data/lib/runtime_analysis/query/report/outline.rb +15 -0
  129. data/lib/runtime_analysis/query/report/paint.rb +34 -0
  130. data/lib/runtime_analysis/query/report/related.rb +11 -0
  131. data/lib/runtime_analysis/query/report/table.rb +15 -0
  132. data/lib/runtime_analysis/query/report/text.rb +13 -0
  133. data/lib/runtime_analysis/query/report/text_renderer.rb +102 -0
  134. data/lib/runtime_analysis/query/report.rb +35 -0
  135. data/lib/runtime_analysis/query/surfaces/class_value.rb +17 -0
  136. data/lib/runtime_analysis/query/surfaces/cli/answer.rb +32 -0
  137. data/lib/runtime_analysis/query/surfaces/cli/command_line.rb +77 -0
  138. data/lib/runtime_analysis/query/surfaces/cli/usage.rb +32 -0
  139. data/lib/runtime_analysis/query/surfaces/cli.rb +101 -0
  140. data/lib/runtime_analysis/query/surfaces/crystallise/lexical.rb +44 -0
  141. data/lib/runtime_analysis/query/surfaces/crystallise/notation.rb +33 -0
  142. data/lib/runtime_analysis/query/surfaces/crystallise/observed.rb +102 -0
  143. data/lib/runtime_analysis/query/surfaces/crystallise/outline.rb +111 -0
  144. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_file.rb +100 -0
  145. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_member.rb +50 -0
  146. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_notation.rb +44 -0
  147. data/lib/runtime_analysis/query/surfaces/crystallise/seen.rb +74 -0
  148. data/lib/runtime_analysis/query/surfaces/crystallise/sig_text.rb +49 -0
  149. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_edit.rb +106 -0
  150. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_notation.rb +48 -0
  151. data/lib/runtime_analysis/query/surfaces/crystallise/subject.rb +55 -0
  152. data/lib/runtime_analysis/query/surfaces/crystallise.rb +94 -0
  153. data/lib/runtime_analysis/query/surfaces/mcp.rb +89 -0
  154. data/lib/runtime_analysis/query/surfaces/record/commands.rb +45 -0
  155. data/lib/runtime_analysis/query/surfaces/record/environment.rb +48 -0
  156. data/lib/runtime_analysis/query/surfaces/record/ignore.rb +62 -0
  157. data/lib/runtime_analysis/query/surfaces/record/question.rb +55 -0
  158. data/lib/runtime_analysis/query/surfaces/record/run.rb +30 -0
  159. data/lib/runtime_analysis/query/surfaces/record.rb +122 -0
  160. data/lib/runtime_analysis/query/surfaces/rerun.rb +87 -0
  161. data/lib/runtime_analysis/query/surfaces/setup/agents.rb +76 -0
  162. data/lib/runtime_analysis/query/surfaces/setup/changes.rb +107 -0
  163. data/lib/runtime_analysis/query/surfaces/setup/project.rb +80 -0
  164. data/lib/runtime_analysis/query/surfaces/setup/steps.md +205 -0
  165. data/lib/runtime_analysis/query/surfaces/setup.rb +98 -0
  166. data/lib/runtime_analysis/query/surfaces.rb +14 -0
  167. data/lib/runtime_analysis/query/tools/base.rb +86 -0
  168. data/lib/runtime_analysis/query/tools/method_lookup.rb +37 -0
  169. data/lib/runtime_analysis/query/tools/namespaces.rb +36 -0
  170. data/lib/runtime_analysis/query/tools/origins.rb +47 -0
  171. data/lib/runtime_analysis/query/tools/stubs.rb +53 -0
  172. data/lib/runtime_analysis/query/tools/unknown_method.rb +11 -0
  173. data/lib/runtime_analysis/query/tools/verify_mock.rb +145 -0
  174. data/lib/runtime_analysis/query/tools/what_calls.rb +67 -0
  175. data/lib/runtime_analysis/query/tools/what_it_receives.rb +142 -0
  176. data/lib/runtime_analysis/query/tools/where_nil_comes_from.rb +105 -0
  177. data/lib/runtime_analysis/query/tools/who_actually_answered.rb +82 -0
  178. data/lib/runtime_analysis/query/tools.rb +34 -0
  179. data/lib/runtime_analysis/query.rb +33 -0
  180. data/lib/runtime_analysis/recording/blob.rb +13 -0
  181. data/lib/runtime_analysis/recording/format.rb +32 -0
  182. data/lib/runtime_analysis/recording/opcodes.rb +30 -0
  183. data/lib/runtime_analysis/recording/raise_ids.rb +15 -0
  184. data/lib/runtime_analysis/recording/ruby_sink.rb +176 -0
  185. data/lib/runtime_analysis/recording/run_manifest.rb +110 -0
  186. data/lib/runtime_analysis/recording/seen/nothing.rb +15 -0
  187. data/lib/runtime_analysis/recording/seen/tally.rb +48 -0
  188. data/lib/runtime_analysis/recording/seen.rb +63 -0
  189. data/lib/runtime_analysis/recording/sink.rb +19 -0
  190. data/lib/runtime_analysis/recording/source_blob.rb +20 -0
  191. data/lib/runtime_analysis/recording.rb +52 -0
  192. data/lib/runtime_analysis/rspec.rb +8 -0
  193. data/lib/runtime_analysis/ruby_parsing/packages.rb +17 -0
  194. data/lib/runtime_analysis/ruby_parsing.rb +98 -0
  195. data/lib/runtime_analysis/trace/configuration/capturing.rb +32 -0
  196. data/lib/runtime_analysis/trace/configuration/per_run.rb +31 -0
  197. data/lib/runtime_analysis/trace/configuration/tier.rb +27 -0
  198. data/lib/runtime_analysis/trace/configuration.rb +115 -0
  199. data/lib/runtime_analysis/trace/entries.rb +47 -0
  200. data/lib/runtime_analysis/trace/hold/raises.rb +28 -0
  201. data/lib/runtime_analysis/trace/hold/requests.rb +47 -0
  202. data/lib/runtime_analysis/trace/hold/telling.rb +45 -0
  203. data/lib/runtime_analysis/trace/hold/unwound.rb +30 -0
  204. data/lib/runtime_analysis/trace/hold.rb +142 -0
  205. data/lib/runtime_analysis/trace/inclusion.rb +46 -0
  206. data/lib/runtime_analysis/trace/installer/alias_repair.rb +68 -0
  207. data/lib/runtime_analysis/trace/installer/answers.rb +24 -0
  208. data/lib/runtime_analysis/trace/installer/body_sync.rb +25 -0
  209. data/lib/runtime_analysis/trace/installer/built_classes.rb +43 -0
  210. data/lib/runtime_analysis/trace/installer/called_names.rb +33 -0
  211. data/lib/runtime_analysis/trace/installer/class_builders.rb +50 -0
  212. data/lib/runtime_analysis/trace/installer/deferral.rb +42 -0
  213. data/lib/runtime_analysis/trace/installer/hook.rb +170 -0
  214. data/lib/runtime_analysis/trace/installer/included_gems.rb +53 -0
  215. data/lib/runtime_analysis/trace/installer/probe.rb +28 -0
  216. data/lib/runtime_analysis/trace/installer/reflection.rb +50 -0
  217. data/lib/runtime_analysis/trace/installer/registry.rb +78 -0
  218. data/lib/runtime_analysis/trace/installer/sorbet.rb +29 -0
  219. data/lib/runtime_analysis/trace/installer/wrapping/ivar_writes.rb +62 -0
  220. data/lib/runtime_analysis/trace/installer/wrapping.rb +66 -0
  221. data/lib/runtime_analysis/trace/installer.rb +149 -0
  222. data/lib/runtime_analysis/trace/mechanism/block_entries.rb +29 -0
  223. data/lib/runtime_analysis/trace/mechanism/frame_readers.rb +61 -0
  224. data/lib/runtime_analysis/trace/mechanism/native_asks.rb +36 -0
  225. data/lib/runtime_analysis/trace/mechanism/native_sync.rb +28 -0
  226. data/lib/runtime_analysis/trace/mechanism/pure_wrapper.rb +41 -0
  227. data/lib/runtime_analysis/trace/mechanism/ruby_wrapper.rb +141 -0
  228. data/lib/runtime_analysis/trace/mechanism/wrapper.rb +79 -0
  229. data/lib/runtime_analysis/trace/mechanism/wrapper_shape.rb +40 -0
  230. data/lib/runtime_analysis/trace/mechanism.rb +28 -0
  231. data/lib/runtime_analysis/trace/nil_origin/answers.rb +24 -0
  232. data/lib/runtime_analysis/trace/nil_origin/beyond.rb +182 -0
  233. data/lib/runtime_analysis/trace/nil_origin/index.rb +164 -0
  234. data/lib/runtime_analysis/trace/nil_origin/lifetime.rb +29 -0
  235. data/lib/runtime_analysis/trace/nil_origin/named.rb +48 -0
  236. data/lib/runtime_analysis/trace/nil_origin/objects.rb +78 -0
  237. data/lib/runtime_analysis/trace/nil_origin/provenance.rb +136 -0
  238. data/lib/runtime_analysis/trace/nil_origin/receiver.rb +121 -0
  239. data/lib/runtime_analysis/trace/nil_origin/rows.rb +43 -0
  240. data/lib/runtime_analysis/trace/nil_origin/source.rb +315 -0
  241. data/lib/runtime_analysis/trace/nil_origin/stored.rb +159 -0
  242. data/lib/runtime_analysis/trace/nil_origin/told.rb +152 -0
  243. data/lib/runtime_analysis/trace/nil_origin/values.rb +47 -0
  244. data/lib/runtime_analysis/trace/nil_origin/weighed.rb +70 -0
  245. data/lib/runtime_analysis/trace/nil_origin/writers.rb +79 -0
  246. data/lib/runtime_analysis/trace/nil_origin.rb +278 -0
  247. data/lib/runtime_analysis/trace/novelty.rb +17 -0
  248. data/lib/runtime_analysis/trace/pure/novelty.rb +43 -0
  249. data/lib/runtime_analysis/trace/pure/state.rb +94 -0
  250. data/lib/runtime_analysis/trace/pure.rb +15 -0
  251. data/lib/runtime_analysis/trace/recorder/class_name.rb +63 -0
  252. data/lib/runtime_analysis/trace/recorder/fingerprint.rb +39 -0
  253. data/lib/runtime_analysis/trace/recorder/frame_key.rb +27 -0
  254. data/lib/runtime_analysis/trace/recorder/paths.rb +89 -0
  255. data/lib/runtime_analysis/trace/recorder/raise_site.rb +36 -0
  256. data/lib/runtime_analysis/trace/recorder/reachability.rb +46 -0
  257. data/lib/runtime_analysis/trace/recorder/row_emission.rb +137 -0
  258. data/lib/runtime_analysis/trace/recorder/safe_steps.rb +84 -0
  259. data/lib/runtime_analysis/trace/recorder/tree_steps.rb +42 -0
  260. data/lib/runtime_analysis/trace/recorder/unwinding.rb +23 -0
  261. data/lib/runtime_analysis/trace/recorder.rb +149 -0
  262. data/lib/runtime_analysis/trace/state.rb +16 -0
  263. data/lib/runtime_analysis/trace.rb +142 -0
  264. data/lib/runtime_analysis/version.rb +6 -0
  265. data/lib/runtime_analysis/web.rb +6 -0
  266. data/lib/runtime_analysis.rb +125 -0
  267. metadata +330 -0
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Host
5
+ module Inject
6
+ CODE = %w[app lib].freeze
7
+ LIB = File.expand_path("../..", __dir__)
8
+ FRAMEWORKS = { "rspec" => -> { defined?(RSpec.configure) },
9
+ "minitest" => -> { defined?(Minitest) && Minitest.const_defined?(:Test) } }.freeze
10
+
11
+ module_function
12
+
13
+ def arm(env)
14
+ root = env["RUNTIME_ANALYSIS_ROOT"] or return
15
+ roots = [root, File.realpath(root)].uniq
16
+ finish_last_at_exit
17
+ watch = TracePoint.new(:class) { |opened| watch.disable if settled?(roots, env, opened.path) }
18
+ watch.enable
19
+ end
20
+
21
+ def finish_last_at_exit
22
+ at_exit { @recorder&.finish! }
23
+ end
24
+
25
+ def settled?(roots, env, path)
26
+ @recorder ||= (record(roots, env) if ours?(roots, path))
27
+ @recorder && tests_named?
28
+ rescue StandardError, ScriptError => e
29
+ warn "ra: the recorder did not start as it should: #{e} (#{e.class})"
30
+ true
31
+ end
32
+
33
+ def ours?(roots, path)
34
+ roots.any? { |root| path.start_with?("#{root}/") && !path.start_with?("#{root}/vendor/") }
35
+ end
36
+
37
+ def record(roots, env)
38
+ $LOAD_PATH.push(LIB) unless $LOAD_PATH.include?(LIB)
39
+ require "#{LIB}/runtime_analysis"
40
+ RuntimeAnalysis.configure { |config| settings(config, roots, env) }
41
+ RuntimeAnalysis.start!
42
+ RuntimeAnalysis
43
+ end
44
+
45
+ def tests_named?
46
+ @tests_named ||= FRAMEWORKS.any? { |file, loaded| loaded.call && name_with(file) }
47
+ end
48
+
49
+ def name_with(file)
50
+ require_relative "../#{file}"
51
+ true
52
+ end
53
+
54
+ def settings(config, roots, env)
55
+ config.included = code(roots)
56
+ config.recording_dir = output(roots.first, env)
57
+ capture(config, env)
58
+ describe(config, env)
59
+ end
60
+
61
+ def capture(config, env)
62
+ config.tier = env.fetch("RUNTIME_ANALYSIS_TIER", "raises")
63
+ config.adaptive = asked?(env, "RUNTIME_ANALYSIS_ADAPTIVE")
64
+ config.mechanism = env["RUNTIME_ANALYSIS_MECHANISM"]&.to_sym
65
+ config.record_ivars = asked?(env, "RUNTIME_ANALYSIS_IVARS")
66
+ end
67
+
68
+ def asked?(env, name) = !env[name].to_s.empty?
69
+
70
+ def describe(config, env)
71
+ config.label = env["RUNTIME_ANALYSIS_LABEL"]
72
+ config.environment = rails_environment
73
+ end
74
+
75
+ def rails_environment
76
+ Rails.env.to_str if defined?(Rails.env)
77
+ end
78
+
79
+ def code(roots)
80
+ roots.product(CODE).map { |root, dir| File.join(root, dir) }
81
+ end
82
+
83
+ def output(root, env)
84
+ env.fetch("RUNTIME_ANALYSIS_OUTPUT") { File.join(root, "tmp/runtime_analysis") }
85
+ end
86
+
87
+ arm(ENV)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Host
5
+ module JobRunners
6
+ module ActiveJobExecution
7
+ def perform_now = RuntimeAnalysis.job("#{self.class.name}#perform") { super }
8
+ end
9
+
10
+ module RakeTask
11
+ def execute(*) = RuntimeAnalysis.job("rake #{name}") { super }
12
+ end
13
+
14
+ module_function
15
+
16
+ def install
17
+ rake_task&.prepend(RakeTask)
18
+ end
19
+
20
+ def active_job(base) = base.prepend(ActiveJobExecution)
21
+
22
+ def rake_task
23
+ Rake::Task if defined?(Rake::Task)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Host
5
+ class Middleware
6
+ def initialize(app, entry_point: true)
7
+ @app = app
8
+ @entry_point = entry_point
9
+ end
10
+
11
+ def call(env)
12
+ identity = Failsafe.attempt { identity(env) }
13
+ RuntimeAnalysis.request(identity) { @app.call(env) }
14
+ end
15
+
16
+ private
17
+
18
+ def identity(env)
19
+ "#{env.fetch("REQUEST_METHOD")} #{env.fetch("PATH_INFO")}" if @entry_point
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+
5
+ module RuntimeAnalysis
6
+ module Host
7
+ module MinitestIntegration
8
+ def self.follow = Minitest::Test.prepend(self)
9
+
10
+ def run
11
+ RuntimeAnalysis.with_entry_point(runtime_analysis_entry_point) { super }
12
+ end
13
+
14
+ private
15
+
16
+ def runtime_analysis_entry_point
17
+ file, line = method(name).source_location
18
+ "./#{Pathname(File.expand_path(file)).relative_path_from(Pathname.pwd)}:#{line}"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ return unless defined?(Rails::Railtie)
4
+
5
+ require_relative "controller_entry_point"
6
+ require_relative "declared"
7
+
8
+ module RuntimeAnalysis
9
+ module Host
10
+ class Railtie < Rails::Railtie
11
+ initializer "runtime_analysis.reload_pause" do
12
+ next unless defined?(ActiveSupport::Reloader)
13
+
14
+ ActiveSupport::Reloader.to_run { RuntimeAnalysis.pause_discovery }
15
+ ActiveSupport::Reloader.to_complete { RuntimeAnalysis.resume_discovery }
16
+ end
17
+
18
+ initializer "runtime_analysis.environment" do
19
+ RuntimeAnalysis.configuration.environment ||= Rails.env.to_s
20
+ end
21
+
22
+ initializer "runtime_analysis.requests", after: :load_config_initializers do |app|
23
+ next unless RuntimeAnalysis.configuration.errors_only
24
+
25
+ app.config.middleware.insert(0, Middleware, entry_point: false)
26
+ RuntimeAnalysis.finish_at_exit
27
+ end
28
+
29
+ initializer "runtime_analysis.declared" do |app|
30
+ Declared.write_at_exit(app) unless Rails.env == "test"
31
+ end
32
+
33
+ initializer "runtime_analysis.entry_points" do
34
+ ActiveSupport.on_load(:action_controller) { include ControllerEntryPoint }
35
+ end
36
+
37
+ config.after_initialize do
38
+ ActiveSupport.on_load(:active_job) { JobRunners.active_job(self) }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../failsafe"
4
+
5
+ module RuntimeAnalysis
6
+ module Host
7
+ module Reports
8
+ RECENT = 64
9
+
10
+ @weakly_remembered = ObjectSpace::WeakMap.new
11
+ @held_strongly = Queue.new
12
+
13
+ module_function
14
+
15
+ def reported(error)
16
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
17
+ return unless Exception === error
18
+ # rubocop:enable Style/CaseEquality
19
+
20
+ origin = Failsafe.attempt { kept(error) }
21
+ Failsafe.host { yield origin } if origin && block_given?
22
+ nil
23
+ end
24
+
25
+ def remembering
26
+ # rubocop:disable Style/SpecialGlobalVars -- host program: $ERROR_INFO needs English, which is not loaded into it
27
+ handled = $!
28
+ # rubocop:enable Style/SpecialGlobalVars
29
+ yield
30
+ ensure
31
+ error = Trace.raised_since(handled)
32
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
33
+ remember(error) if StandardError === error || ScriptError === error
34
+ # rubocop:enable Style/CaseEquality
35
+ end
36
+
37
+ def remember(error)
38
+ RuntimeAnalysis.keep_request
39
+ @weakly_remembered[error] = held_strongly(RuntimeAnalysis.nil_origin(error))
40
+ end
41
+
42
+ def held_strongly(origin)
43
+ @held_strongly << origin
44
+ @held_strongly.deq if @held_strongly.size > RECENT
45
+ origin
46
+ end
47
+
48
+ def kept(error)
49
+ Trace.reported(@weakly_remembered.key?(error))
50
+ RuntimeAnalysis.nil_origin(error) || @weakly_remembered[error]
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Host
5
+ module RSpecIntegration
6
+ module_function
7
+
8
+ def follow = RSpec.configure { |config| install(config) }
9
+
10
+ def install(config)
11
+ config.around do |example|
12
+ RuntimeAnalysis.with_entry_point(example.location) { example.run }
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module RuntimeAnalysis
6
+ module Host
7
+ module Web
8
+ STATS = ["", "/", "/stats"].freeze
9
+ READS = %w[GET HEAD].freeze
10
+ JSON_HEADERS = { "content-type" => "application/json", "cache-control" => "no-store" }.freeze
11
+
12
+ module_function
13
+
14
+ def call(env)
15
+ return [404, { "content-type" => "text/plain" }, ["Not found\n"]] unless STATS.include?(env.fetch("PATH_INFO"))
16
+ return [405, { "allow" => READS.join(", ") }, []] unless READS.include?(env.fetch("REQUEST_METHOD"))
17
+
18
+ [200, JSON_HEADERS.dup, env.fetch("REQUEST_METHOD") == "HEAD" ? [] : [JSON.generate(RuntimeAnalysis.stats)]]
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through the root's calls by runtime_analysis_spec.rb, reports_spec.rb,
4
+ # rspec_integration_spec.rb, middleware_spec.rb and web_spec.rb
5
+
6
+ require_relative "host/middleware"
7
+ require_relative "host/reports"
8
+ require_relative "host/job_runners"
9
+ require_relative "host/web"
10
+ require_relative "host/declared"
11
+ require_relative "host/railtie"
12
+
13
+ module RuntimeAnalysis
14
+ module Host
15
+ module_function
16
+
17
+ def middleware = Middleware
18
+
19
+ def web = Web
20
+
21
+ def reported(error, &) = Reports.reported(error, &)
22
+
23
+ def remembering(&) = Reports.remembering(&)
24
+
25
+ def install_job_runners = JobRunners.install
26
+
27
+ def follow(framework)
28
+ case framework
29
+ when :rspec then RSpecIntegration.follow
30
+ when :minitest then MinitestIntegration.follow
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: the path -rruntime_analysis/inject names; inject_spec.rb loads the recorder through it
4
+
5
+ require_relative "host/inject"
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: one-line installer; the hook it installs is covered by minitest_integration_spec.rb
4
+
5
+ require "minitest"
6
+ require "runtime_analysis"
7
+ require_relative "host/minitest_integration"
8
+
9
+ RuntimeAnalysis.follow(:minitest)
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Native
5
+ module Build
6
+ module_function
7
+
8
+ def tag(version: RUBY_VERSION, platform: RUBY_PLATFORM)
9
+ "#{version}-#{platform}"
10
+ end
11
+
12
+ def feature(**build)
13
+ "runtime_analysis/native_ext/#{tag(**build)}/native_ext"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Native
5
+ module ByteSize
6
+ UNITS = { "" => 1, "k" => 1024, "m" => 1024**2, "g" => 1024**3, "t" => 1024**4 }.freeze
7
+ PATTERN = /\A\s*(?<number>\d+(?:\.\d+)?)\s*(?<unit>[kmgt]?)b?\s*\z/i
8
+
9
+ module_function
10
+
11
+ def parse(value)
12
+ return nil if value.nil? || value == false
13
+ return value if value.instance_of?(Integer)
14
+
15
+ to_bytes(size_match(value))
16
+ end
17
+
18
+ def size_match(value)
19
+ PATTERN.match(value.to_s) or raise ArgumentError, "invalid byte size: #{value.inspect}"
20
+ end
21
+
22
+ def to_bytes(match)
23
+ (match[:number].to_f * UNITS.fetch(match[:unit].downcase)).to_int
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through call_site_logger_spec.rb, which reads back the strings and routes a segment defines.
4
+
5
+ module RuntimeAnalysis
6
+ module Native
7
+ class CallSiteLogger
8
+ module Definitions
9
+ private
10
+
11
+ def define(stream, path)
12
+ parent = path.parent
13
+ return if undefined?(stream, parent) && !define(stream, parent)
14
+
15
+ stream.paths[path.id] = taken?(stream) { stream.sink.write(PATH, path_row(stream, path, parent)) }
16
+ end
17
+
18
+ def taken?(stream)
19
+ dropped = stream.sink.dropped
20
+ yield
21
+ stream.sink.dropped == dropped
22
+ end
23
+
24
+ def undefined?(stream, path)
25
+ path && !stream.paths.at(path.id)
26
+ end
27
+
28
+ def path_row(stream, path, parent)
29
+ [path.id, parent&.id || 0, *frame_fields(stream, path.frame),
30
+ intern(stream, path.call_file), int(path.call_line)]
31
+ end
32
+
33
+ def frame_fields(stream, (klass, name, file, line, class_method))
34
+ [intern(stream, klass), class_method, intern(stream, name), intern(stream, file), int(line)]
35
+ end
36
+
37
+ # hot-path: no lock, which would be taken for every string of every recorded call. One thread
38
+ # owns a stream's dictionary, and an entry is handed to the sink before its id is published.
39
+ def intern(stream, string)
40
+ stream.dict.fetch(string) { intern_new(stream, string) }
41
+ end
42
+
43
+ def intern_new(stream, string)
44
+ id = (stream.ids[string] ||= stream.ids.size)
45
+ stream.dict[string] = id if taken?(stream) { stream.sink.define(id, string&.to_s) }
46
+ id
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through call_site_logger_hold_spec.rb, which keeps and discards requests.
4
+
5
+ module RuntimeAnalysis
6
+ module Native
7
+ class CallSiteLogger
8
+ module Held
9
+ def keep_held
10
+ stream = cached_stream
11
+ return false unless stream&.sink&.keep(free_segment_path(stream))
12
+
13
+ stream.seq += 1
14
+ enforce_retain!
15
+ true
16
+ end
17
+
18
+ def discard_held
19
+ cached_stream&.sink&.discard
20
+ end
21
+
22
+ def held(most)
23
+ stream = cached_stream or return nil
24
+ words, left_out = stream.sink.held(most)
25
+ { rows: words.unpack("q*"), left_out: left_out, ids: stream.ids,
26
+ segment: File.basename(free_segment_path(stream)) }
27
+ end
28
+
29
+ def largest_request
30
+ @mutex.synchronize { [@largest, *@streams.map { |stream| stream.sink.largest_request }].max }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through the rows a recording is read back as in call_site_logger_spec.rb.
4
+
5
+ module RuntimeAnalysis
6
+ module Native
7
+ class CallSiteLogger
8
+ module Rows
9
+ # rubocop:disable Metrics/ParameterLists -- hot path: a row object would be allocated for every recorded call
10
+ def log_call(stack_uuid:, step:, path:, call_file:, call_line:, arg_classes: nil, receiver_id: nil,
11
+ arg_ids: nil)
12
+ stream = stream_for(step)
13
+ dropped_before = stream.sink.dropped
14
+ define(stream, path) if undefined?(stream, path)
15
+ write_call(stream, dropped_before, [stack_uuid, step, path.id, intern(stream, call_file), int(call_line),
16
+ *arg_tail(stream, arg_classes, receiver_id, arg_ids)])
17
+ end
18
+ # rubocop:enable Metrics/ParameterLists
19
+
20
+ def log_return(stack_uuid:, step:, return_class:, return_id:, elements: nil)
21
+ stream = ensure_stream
22
+ write(stream, RETURN, [stack_uuid, step, intern(stream, return_class), return_id,
23
+ *element_tail(stream, elements)])
24
+ end
25
+
26
+ def element_tail(stream, elements)
27
+ return nil unless elements
28
+
29
+ count, pairs = elements
30
+ [count, *pairs.first(MAX_ARGS).flat_map { |name, id| [intern(stream, name), id] }]
31
+ end
32
+
33
+ def log_kwargs(stack_uuid:, step:, kwarg_classes:, kwarg_ids: nil)
34
+ stream = ensure_stream
35
+ write(stream, KWARGS, kwarg_row(stream, stack_uuid, step, kwarg_classes, kwarg_ids))
36
+ end
37
+
38
+ def kwarg_row(stream, stack_uuid, step, kwarg_classes, kwarg_ids)
39
+ [stack_uuid, step, kwarg_ids ? 3 : 2, kwarg_classes.size,
40
+ *kwarg_entries(stream, kwarg_classes, kwarg_ids)]
41
+ end
42
+
43
+ def kwarg_entries(stream, kwarg_classes, kwarg_ids)
44
+ kwarg_classes.first(MAX_ARGS).flat_map do |name, klass|
45
+ pair = [intern(stream, name.to_s), intern(stream, klass)]
46
+ kwarg_ids ? pair << kwarg_ids.fetch(name) : pair
47
+ end
48
+ end
49
+
50
+ def log_unwind(stack_uuid:, step:, exception_class:, raised_at: nil, missing: nil)
51
+ stream = ensure_stream
52
+ site = RAISE_IDS.of(raised_at, missing) { |string| intern(stream, string) }
53
+ write(stream, UNWIND, [stack_uuid, step, intern(stream, exception_class), *site])
54
+ end
55
+
56
+ def log_entry(stack_uuid:, entry_point:)
57
+ stream = ensure_stream
58
+ write(stream, ENTRY, [stack_uuid, intern(stream, entry_point)])
59
+ end
60
+
61
+ def log_inner_entry(stack_uuid:, identity:)
62
+ stream = ensure_stream
63
+ write(stream, INNER_ENTRY, [stack_uuid, intern(stream, identity)])
64
+ end
65
+
66
+ # rubocop:disable Metrics/ParameterLists -- hot path: a row object would be allocated for every probed value
67
+ def log_value(stack_uuid:, step:, role:, key:, how:, print:)
68
+ stream = ensure_stream
69
+ write(stream, VALUE,
70
+ [stack_uuid, step, role, role == KEYWORD_VALUE ? intern(stream, key.to_s) : key, how, print])
71
+ end
72
+ # rubocop:enable Metrics/ParameterLists
73
+
74
+ def log_answered(stack_uuid:, step:, name:)
75
+ stream = ensure_stream
76
+ write(stream, ANSWERED, [stack_uuid, step, intern(stream, name)])
77
+ end
78
+
79
+ def name_id(handle, name)
80
+ stream = ensure_stream
81
+ return unless stream.sink.handle == handle
82
+
83
+ intern(stream, name)
84
+ stream.dict[name]
85
+ end
86
+
87
+ private
88
+
89
+ def arg_tail(stream, arg_classes, receiver_id, arg_ids)
90
+ return tier3_tail(stream, arg_classes.to_a, receiver_id, arg_ids) unless receiver_id.nil?
91
+
92
+ tier2_tail(stream, arg_classes) if arg_classes && !arg_classes.empty?
93
+ end
94
+
95
+ def tier2_tail(stream, classes)
96
+ [2, classes.size, *classes.first(MAX_ARGS).map { |name| intern(stream, name) }]
97
+ end
98
+
99
+ def tier3_tail(stream, classes, receiver_id, arg_ids)
100
+ stored = classes.first(MAX_ARGS).zip(arg_ids)
101
+ [3, classes.size, receiver_id, *stored.flat_map { |name, id| [intern(stream, name), id] }]
102
+ end
103
+
104
+ def int(value)
105
+ value || -1
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end