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,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through class_builders_spec.rb, which records the readers of built classes.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Installer
8
+ module BuiltClasses
9
+ def on_built(klass, names, site)
10
+ return unless for_the_app?(klass, site)
11
+
12
+ ask_host([]) { fields_of(klass, names) }.each { |name| adopt(klass, name, site) }
13
+ end
14
+
15
+ private
16
+
17
+ def for_the_app?(klass, site)
18
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
19
+ Module === klass && @inclusion.include?(site.path) && keyable?(klass)
20
+ # rubocop:enable Style/CaseEquality
21
+ end
22
+
23
+ def adopt(klass, name, site)
24
+ sites[[klass, name]] = [site.path, site.lineno]
25
+ on_defined(klass, name, class_method: false)
26
+ end
27
+
28
+ def fields_of(klass, names)
29
+ readers = names.grep(Symbol) + names.grep(String).map(&:to_sym)
30
+ readers.flat_map { |name| [name, :"#{name}="] }.select { |name| klass.method_defined?(name) }
31
+ end
32
+
33
+ def location_of(target, name, method)
34
+ sites[[target, name]] || method&.source_location
35
+ end
36
+
37
+ def sites
38
+ @sites ||= {}
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/ruby_parsing"
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Installer
8
+ class CalledNames < RubyParsing.visitor
9
+ def self.in(directories)
10
+ files = directories.flat_map { |directory| Dir.glob(File.join(directory, "**", "*.rb")) }
11
+ files.each_with_object(new) { |file, names| names.read(file) }.found
12
+ end
13
+
14
+ def found = @found ||= Set.new
15
+
16
+ def read(file)
17
+ RubyParsing.tree_of_file(file).accept(self)
18
+ rescue SystemCallError
19
+ nil
20
+ end
21
+
22
+ def visit_call_node(node)
23
+ found << node.name
24
+ super
25
+ end
26
+
27
+ def visit_symbol_node(node)
28
+ found << node.unescaped.to_sym
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Trace
5
+ class Installer
6
+ module ClassBuilders
7
+ DEFAULT = [[::Data, :define], [::Struct, :new]].freeze
8
+
9
+ @watchers = {}
10
+ @watched = []
11
+
12
+ class << self
13
+ def default = DEFAULT
14
+
15
+ def watch(builders, listener)
16
+ @listener = listener
17
+ @watched = Array(builders)
18
+ @watched.each { |builder| @watchers[builder] ||= place(builder) }
19
+ end
20
+
21
+ def watcher?(mod)
22
+ @watchers.value?(mod)
23
+ end
24
+
25
+ def built(builder, klass, names, asked_from)
26
+ return unless Trace.enabled? && @watched.include?(builder)
27
+
28
+ asked_from.each { |site| @listener.on_built(klass, names, site) }
29
+ end
30
+
31
+ private
32
+
33
+ def place(builder)
34
+ Module.new.tap do |watcher|
35
+ report_from(watcher, builder)
36
+ builder.first.singleton_class.prepend(watcher)
37
+ end
38
+ end
39
+
40
+ def report_from(watcher, builder)
41
+ watcher.define_method(builder.last) do |*args, **kwargs, &block|
42
+ asked_from = caller_locations(1, 1)
43
+ super(*args, **kwargs, &block).tap { |built| ClassBuilders.built(builder, built, args, asked_from) }
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through installer_spec.rb, which pauses discovery and resumes it.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Installer
8
+ module Deferral
9
+ BACKLOG_EMPTY = ThreadError
10
+
11
+ def pause
12
+ @paused = true
13
+ end
14
+
15
+ def resume
16
+ @paused = false
17
+ flush
18
+ end
19
+
20
+ def flush
21
+ loop { wrap_deferred(*@pending.deq(true)) }
22
+ rescue BACKLOG_EMPTY
23
+ nil
24
+ end
25
+
26
+ private
27
+
28
+ def init_deferral
29
+ @pending = Queue.new
30
+ end
31
+
32
+ def enqueue(target, name, class_method)
33
+ @pending << [target, name, class_method]
34
+ end
35
+
36
+ def wrap_deferred(target, name, class_method)
37
+ Failsafe.attempt { discover(target, name, class_method) }
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace_spec.rb, whose examples define, copy and remove methods while
4
+ # recording, and installer_spec.rb, which also closes class bodies and defines methods nobody hears of.
5
+
6
+ module RuntimeAnalysis
7
+ module Trace
8
+ class Installer
9
+ module Hook
10
+ PROBE = :__runtime_analysis_probe
11
+
12
+ def self.hears_singletons?
13
+ return @hears_singletons unless @hears_singletons.nil?
14
+
15
+ @heard = false
16
+ Class.new { def self.__runtime_analysis_probe = nil }
17
+ @hears_singletons = @heard
18
+ end
19
+
20
+ def self.heard(name)
21
+ @heard = true if name == PROBE
22
+ end
23
+
24
+ def initialize_copy(source)
25
+ Installer.copying { super }
26
+ end
27
+
28
+ def dup
29
+ Installer.copying { super }
30
+ end
31
+
32
+ def clone(**)
33
+ Installer.copying { super }
34
+ end
35
+
36
+ COPIERS = ["Kernel#dup", "Kernel#clone"].freeze
37
+
38
+ def self.defining?(heard_from) = !COPIERS.include?(heard_from.label)
39
+
40
+ NAMES = %i[method_added singleton_method_added].freeze
41
+ METHOD = ::Kernel.instance_method(:method)
42
+ FROZEN = ::Kernel.instance_method(:frozen?)
43
+ SINGLETON_CLASS = ::Kernel.instance_method(:singleton_class)
44
+
45
+ def self.kept_back_from?(mod)
46
+ !FROZEN.bind_call(mod) && NAMES.any? { |name| !Definitions.equal?(METHOD.bind_call(mod, name).owner) }
47
+ end
48
+
49
+ def self.keeper_of(mod, class_method:)
50
+ name = NAMES.fetch(class_method ? 1 : 0)
51
+ keeper = METHOD.bind_call(mod, name).owner
52
+ return if Definitions.equal?(keeper)
53
+
54
+ keeper.singleton_class? ? "#{keeper.attached_object}.#{name}" : "#{keeper}##{name}"
55
+ end
56
+
57
+ def self.go_ahead_in(mod) = SINGLETON_CLASS.bind_call(mod).prepend(Definitions)
58
+
59
+ module Definitions
60
+ def method_added(name)
61
+ super
62
+ return unless Trace.enabled? && Hook.defining?(*caller_locations(1, 1))
63
+
64
+ Trace.on_defined(self, name, class_method: false)
65
+ end
66
+
67
+ def singleton_method_added(name)
68
+ super
69
+ Hook.heard(name)
70
+ Hook.go_ahead_in(self) if NAMES.include?(name)
71
+ Trace.on_defined(self, name, class_method: true) if Trace.enabled?
72
+ end
73
+ end
74
+
75
+ include Definitions
76
+
77
+ module Unheard
78
+ NAME = ::Module.instance_method(:name)
79
+
80
+ KINDS = [false, true].freeze
81
+
82
+ def never_wrapped
83
+ closed_bodies.flat_map do |owner, keepers|
84
+ targets_of(owner).flat_map { |target, kind| never_wrapped_in(owner, target, keepers.assoc(kind)) }
85
+ end
86
+ end
87
+
88
+ def overhear(mod)
89
+ Hook.go_ahead_in(mod) if ask_host(false) { Hook.kept_back_from?(mod) }
90
+ end
91
+
92
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
93
+ def one_object?(target) = target.singleton_class? && !(Module === target.attached_object)
94
+ # rubocop:enable Style/CaseEquality
95
+
96
+ def sweep_body(mod)
97
+ owner = mod.singleton_class? ? mod.attached_object : mod
98
+ return if one_object?(owner.singleton_class)
99
+
100
+ body_closed_of(owner)
101
+ overhear(owner)
102
+ targets_of(owner).each { |target, class_method| discover_own(owner, target, class_method) }
103
+ end
104
+
105
+ private
106
+
107
+ def settled?(target, name, method)
108
+ @originals[[target, name]] == method && @registry.site(target, name) == location_of(target, name, method)
109
+ end
110
+
111
+ def closed_bodies
112
+ @closed_bodies ||= {}
113
+ end
114
+
115
+ def body_closed_of(owner)
116
+ return if untouchable?(owner, owner)
117
+
118
+ closed_bodies[owner] ||= KINDS.to_h { |class_method| [class_method, Hook.keeper_of(owner, class_method:)] }
119
+ end
120
+
121
+ def never_wrapped_in(owner, target, kind_and_keeper)
122
+ own_names(target).filter_map { |name| missed(owner, target, name, kind_and_keeper) }
123
+ end
124
+
125
+ def missed(owner, target, name, (class_method, keeper))
126
+ file, line = body_location(what_ruby_would_call_which_is_the_wrapper_of_a_wrapped_method(target, name))
127
+ return unless @inclusion.include?(file)
128
+
129
+ { class: NAME.bind_call(owner), method: name, class_method: class_method, file: file, line: line,
130
+ kept_by: keeper }
131
+ end
132
+
133
+ def what_ruby_would_call_which_is_the_wrapper_of_a_wrapped_method(target, name) = raw_method(target, name)
134
+
135
+ def targets_of(owner) = [[owner, false], [owner.singleton_class, true]]
136
+
137
+ def discover_own(owner, target, class_method)
138
+ own_names(target).each { |name| on_defined(owner, name, class_method: class_method) }
139
+ end
140
+
141
+ def own_names(target)
142
+ ask_host([]) { target.instance_methods(false) + target.private_instance_methods(false) }
143
+ end
144
+ end
145
+
146
+ def self.unheard = Unheard
147
+
148
+ def method_removed(name)
149
+ super
150
+ Trace.on_removed(self, name, class_method: false)
151
+ end
152
+
153
+ def method_undefined(name)
154
+ super
155
+ Trace.on_removed(self, name, class_method: false)
156
+ end
157
+
158
+ def singleton_method_removed(name)
159
+ super
160
+ Trace.on_removed(self, name, class_method: true)
161
+ end
162
+
163
+ def singleton_method_undefined(name)
164
+ super
165
+ Trace.on_removed(self, name, class_method: true)
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace_spec.rb and installer_spec.rb, which record calls into gems.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Installer
8
+ module IncludedGems
9
+ def sweep
10
+ gems = @called && @inclusion.gems?
11
+ ObjectSpace.each_object(Module) do |mod|
12
+ overhear(mod)
13
+ sweep_module(mod) if gems
14
+ end
15
+ end
16
+
17
+ def sweep_singletons(mod)
18
+ owner = mod.singleton_class? ? mod.attached_object : mod
19
+ ask_host([]) { owner.singleton_methods(false) }.each { |name| on_defined(owner, name, class_method: true) }
20
+ end
21
+
22
+ def body_closed(mod, file_of_the_body = caller_locations(2, 1).last.path)
23
+ sweep_singletons(mod) unless Hook.hears_singletons?
24
+ sweep_body(mod) if @inclusion.include?(file_of_the_body)
25
+ resync(mod)
26
+ end
27
+
28
+ private
29
+
30
+ def reach_of(name, file)
31
+ return :app if @inclusion.include?(file)
32
+
33
+ @inclusion.gem_depth(file) if @called&.include?(name)
34
+ end
35
+
36
+ def sweep_module(mod)
37
+ return if mod.frozen?
38
+
39
+ named_in(mod).each { |name| on_defined(mod, name, class_method: false) }
40
+ sweep_singleton(mod) unless mod.singleton_class?
41
+ end
42
+
43
+ def sweep_singleton(mod)
44
+ named_in(mod.singleton_class).each { |name| on_defined(mod, name, class_method: true) }
45
+ end
46
+
47
+ def named_in(target)
48
+ own_names(target).select { |name| @called.include?(name) }
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Trace
5
+ class Installer
6
+ class Probe
7
+ EVERY = "*"
8
+ NAME = ::Module.instance_method(:name)
9
+
10
+ def initialize(identities)
11
+ @identities = Set.new(identities)
12
+ @every = @identities.include?(EVERY)
13
+ end
14
+
15
+ def covers?(target, name, class_method)
16
+ @every || @identities.include?(identity(target, name, class_method))
17
+ end
18
+
19
+ private
20
+
21
+ def identity(target, name, class_method)
22
+ owner = NAME.bind_call(class_method ? target.attached_object : target)
23
+ "#{owner}#{class_method ? "." : "#"}#{name}" if owner
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace_spec.rb, which reflects on wrapped methods while recording.
4
+
5
+ require "runtime_analysis/native"
6
+
7
+ module RuntimeAnalysis
8
+ module Trace
9
+ class Installer
10
+ module Reflection
11
+ SOURCE_LOCATION = ::UnboundMethod.instance_method(:source_location)
12
+ INSTANCE_METHOD = ::Module.instance_method(:instance_method)
13
+
14
+ module Lookup
15
+ def instance_method(name) = Reflection.beneath(super)
16
+ def public_instance_method(name) = Reflection.beneath(super)
17
+ end
18
+
19
+ Native.wrapper.lookups_on(Lookup) if Native.loaded?
20
+
21
+ module SingletonLookup
22
+ def define_singleton_method(name, *body)
23
+ super(name, *Trace.unwrapped(body))
24
+ end
25
+ end
26
+
27
+ def arity = Reflection.answer(self, :arity) { super }
28
+ def parameters = Reflection.answer(self, :parameters) { super }
29
+ def source_location = Reflection.answer(self, :source_location) { super }
30
+
31
+ def self.source_location(method) = SOURCE_LOCATION.bind_call(method)
32
+ def self.raw_instance_method(mod, name) = INSTANCE_METHOD.bind_call(mod, name)
33
+
34
+ def self.beneath(method) = Trace.wrapped_original(method) || method
35
+
36
+ def self.answer(method, reader)
37
+ original = Trace.wrapped_original(method)
38
+ original ? original.public_send(reader) : yield
39
+ end
40
+
41
+ def self.install
42
+ Method.prepend(self)
43
+ UnboundMethod.prepend(self)
44
+ Module.prepend(Lookup)
45
+ Object.prepend(SingletonLookup)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Trace
5
+ class Installer
6
+ class Registry
7
+ GONE = [nil, false, nil, nil, nil, false, nil, false].freeze
8
+ FILE = 2
9
+ DEPTH = 4
10
+ KEYWORDS_AS_HASH = 5
11
+ SALT = 6
12
+ PROBED = 7
13
+
14
+ def self.after(earlier)
15
+ new(first: earlier ? earlier.size : 0)
16
+ end
17
+
18
+ def initialize(first: 0)
19
+ @ids = {}
20
+ @entries = Array.new(first, GONE)
21
+ end
22
+
23
+ def size = @entries.size
24
+
25
+ def register(target, name, class_method:, file:, line:)
26
+ entry = [name, class_method, file, line, nil, false, nil, false]
27
+ id = @ids[[target, name]]
28
+ return replace(id, entry) if id
29
+
30
+ add([target, name], entry)
31
+ end
32
+
33
+ def limit(id, depth)
34
+ fetch(id)[DEPTH] = depth
35
+ end
36
+
37
+ def keywords_as_hash(id)
38
+ fetch(id)[KEYWORDS_AS_HASH] = true
39
+ end
40
+
41
+ def probe(id)
42
+ fetch(id)[PROBED] = true
43
+ end
44
+
45
+ def probed?(id) = fetch(id).fetch(PROBED)
46
+
47
+ def salt(id, value)
48
+ fetch(id)[SALT] = value
49
+ end
50
+
51
+ def fetch(id)
52
+ @entries.at(id)
53
+ end
54
+
55
+ def site(target, name)
56
+ id = @ids[[target, name]]
57
+ fetch(id)[FILE, 2] if id
58
+ end
59
+
60
+ def files
61
+ @entries.filter_map { |entry| entry.fetch(FILE) }.uniq
62
+ end
63
+
64
+ private
65
+
66
+ def replace(id, entry)
67
+ @entries[id] = entry
68
+ id
69
+ end
70
+
71
+ def add(key, entry)
72
+ @entries << entry
73
+ @ids[key] = size - 1
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through sorbet_spec.rb, which records a class with sigs under sorbet-runtime.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Installer
8
+ module Sorbet
9
+ def self.install
10
+ return unless defined?(T::Private::Methods) && T::Private::Methods.respond_to?(:_on_method_added)
11
+
12
+ T::Private::Methods.singleton_class.prepend(self)
13
+ end
14
+
15
+ def self.defined(args, options)
16
+ owner = args.size == 3 ? args.fetch(1) : args.first
17
+ singleton = owner.singleton_class? || options[:is_singleton_method]
18
+ owner = owner.attached_object if owner.singleton_class?
19
+ Trace.on_defined(owner, args.last, class_method: singleton)
20
+ end
21
+
22
+ def _on_method_added(*args, **options)
23
+ Sorbet.defined(args, options) if Trace.enabled?
24
+ super
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/ruby_parsing"
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Installer
8
+ module Wrapping
9
+ class IvarWrites
10
+ RETURNING = RubyParsing.kinds(:instance_variable_write_node, :instance_variable_operator_write_node,
11
+ :instance_variable_or_write_node, :instance_variable_and_write_node)
12
+ WRITES = RubyParsing.kinds(*RETURNING, :instance_variable_target_node)
13
+
14
+ def initialize
15
+ @defs = {}
16
+ end
17
+
18
+ def of(file, line)
19
+ return [] unless file && File.file?(file) && RubyParsing.available?
20
+
21
+ defs_in(file).fetch(line, [])
22
+ end
23
+
24
+ private
25
+
26
+ def defs_in(file)
27
+ @defs[file] ||= Failsafe.attempt({}) { by_line(RubyParsing.tree_of_file(file)) }
28
+ end
29
+
30
+ def by_line(tree, found = {})
31
+ tree.compact_child_nodes.each do |node|
32
+ found[node.location.start_line] ||= watched(node) if RubyParsing.definition?(node)
33
+ by_line(node, found)
34
+ end
35
+ found
36
+ end
37
+
38
+ def watched(definition)
39
+ writes = own_writes(definition)
40
+ returned = write_the_call_returns(definition)
41
+ writes -= [returned.name] if returned && writes.count(returned.name) == 1
42
+ writes.uniq
43
+ end
44
+
45
+ def write_the_call_returns(definition)
46
+ body = definition.body
47
+ last = body.body.last if RubyParsing.statements?(body)
48
+ last if RETURNING.include?(RubyParsing.kind(last))
49
+ end
50
+
51
+ def own_writes(node)
52
+ node.compact_child_nodes.flat_map do |child|
53
+ next [] if RubyParsing.definition?(child)
54
+
55
+ WRITES.include?(RubyParsing.kind(child)) ? [child.name, *own_writes(child)] : own_writes(child)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end