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,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "mechanism/wrapper"
4
+ require_relative "mechanism/ruby_wrapper"
5
+ require_relative "mechanism/pure_wrapper"
6
+ require_relative "mechanism/native_sync"
7
+ require_relative "mechanism/native_asks"
8
+ require_relative "mechanism/block_entries"
9
+
10
+ module RuntimeAnalysis
11
+ module Trace
12
+ module Mechanism
13
+ WRAPPERS = { native: ->(_tier) { Wrapper.new }, ruby: ->(tier) { RubyWrapper.new(tier: tier) },
14
+ pure: ->(tier) { PureWrapper.new(tier: tier) } }.freeze
15
+ NAMES = WRAPPERS.keys.freeze
16
+
17
+ def self.wrapper(name, tier:)
18
+ WRAPPERS.fetch(name) { raise ArgumentError, unknown(name) }.call(tier)
19
+ end
20
+
21
+ def self.entries = [NativeSync, NativeAsks, BlockEntries]
22
+
23
+ def self.unknown(name)
24
+ "mechanism must be one of #{NAMES.map(&:inspect).join(", ")}, not #{name.inspect}"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace/nil_origin_spec.rb, which asks NilOrigin for each of these.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class NilOrigin
8
+ module Answers
9
+ def configure(configuration)
10
+ self.index = Index.configured(configuration)
11
+ end
12
+
13
+ def facts(error, logger, paths, root:)
14
+ error = nil_error(error) or return nil
15
+ held = logger.held(most) or return nil
16
+ new(error, held, paths, root: root).facts(Lifetime.public_method(:of))
17
+ end
18
+
19
+ def most = MOST
20
+ def hops = HOPS
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,182 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace/nil_origin/stored_spec.rb.
4
+
5
+ require_relative "stored"
6
+
7
+ module RuntimeAnalysis
8
+ module Trace
9
+ class NilOrigin
10
+ class Beyond
11
+ def initialize(world, source)
12
+ @world = world
13
+ @source = source
14
+ @stored = Stored.new(world.index)
15
+ @receiver = Receiver.new(world, source)
16
+ end
17
+
18
+ def of(node, call, raised)
19
+ found = call ? returned(call) : raised_on(node, raised)
20
+ (found || {}).merge(index: @world.index.status)
21
+ end
22
+
23
+ def received(call)
24
+ job?(call) ? job(call).given(call.nil_arg) : {}
25
+ end
26
+
27
+ def behind(set)
28
+ found = handed(set) or return {}
29
+ return found.merge(index: @world.index.status) if only_carried_by_request?(found)
30
+
31
+ { upstream: { kind: found[:kind], reason: found.fetch(:because) }.compact, index: @world.index.status }
32
+ end
33
+
34
+ def returned_to(set)
35
+ kind, name = argument(set)
36
+ call = @world.tree.call(set.made.tree, set.made.step)
37
+ nil_before(call, name) if kind == :call && call.parent
38
+ end
39
+
40
+ private
41
+
42
+ def only_carried_by_request?(found) = found[:kind] == "stored_null"
43
+
44
+ def raised_on(node, raised)
45
+ shape = @source.receiver_at(*raised)
46
+ local = shape.fetch(1) if shape&.first == :local
47
+ (local ? of_local(node, local) : unrecorded_attribute(shape, node)) || site(raised, node, shape)
48
+ end
49
+
50
+ def of_local(node, local)
51
+ _klass, name, file, line = frame(node)
52
+ left_out(node, local) || unrecorded_attribute(@source.local_shape(file, line, name, local), node)
53
+ end
54
+
55
+ def left_out(node, local)
56
+ job(node).left_out(local, node) if job?(node)
57
+ end
58
+
59
+ def job(call) = Job.new(@source, frame(call), @world)
60
+
61
+ def job?(call)
62
+ klass, name = frame(call)
63
+ call.parent.nil? && @world.entry == "#{klass}##{name}"
64
+ end
65
+
66
+ def site(raised, node, shape)
67
+ found = @world.index.sites(*raised)
68
+ indexed_site(found.first, node, shape) if found.map { |one| one.fetch("kind") }.uniq.size == 1
69
+ end
70
+
71
+ def indexed_site(one, node, shape)
72
+ return way(one) unless one.key?("model")
73
+
74
+ @stored.cause(read_from_self(one.fetch("model"), node, shape), one["attr"]) unless read_from(shape, node)
75
+ end
76
+
77
+ def read_from_self(model, node, shape)
78
+ Receiver.known(model, (node.receiver if shape.nil? && frame(node).first == model))
79
+ end
80
+
81
+ def read_from(shape, node)
82
+ owner(shape, node)&.klass if shape&.first == :call
83
+ end
84
+
85
+ def owner(shape, call) = @receiver.of(shape.fetch(2), call, nil_passes: shape.include?(:safe))
86
+
87
+ def returned(call)
88
+ _klass, name, file, line = frame(call)
89
+ unrecorded_attribute(@source.returned_shape(file, line, name), call) || indexed(Named.called(frame(call)))
90
+ end
91
+
92
+ def unrecorded_attribute(shape, call)
93
+ return unless shape && shape.first == :call
94
+
95
+ found = owner(shape, call) or return nil
96
+ @stored.cause(found, shape.fetch(1))
97
+ end
98
+
99
+ def indexed(identity)
100
+ found = @world.index.returns(identity)
101
+ kept = found.find { |one| one.fetch("kind") == "process_memory" } || (found.first if one_kind?(found))
102
+ way(kept) if kept
103
+ end
104
+
105
+ def one_kind?(found)
106
+ found.map { |one| one.values_at("kind", "sub_kind") }.uniq.size == 1
107
+ end
108
+
109
+ def way(found)
110
+ says = found.fetch("says")
111
+ { kind: found.fetch("kind"), sub_kind: found["sub_kind"], because: says,
112
+ origin: { file: found.fetch("file"), line: found["at"] || found.fetch("line"), reason: says } }
113
+ end
114
+
115
+ class Job
116
+ KIND = { kind: "outside_taxonomy", sub_kind: "crossed_boundary" }.freeze
117
+
118
+ def initialize(source, frame, world)
119
+ @source = source
120
+ @frame = frame
121
+ @world = world
122
+ end
123
+
124
+ def given(index)
125
+ _klass, name, file, line = @frame
126
+ param = @source.param_at(file, line, name, index) or return {}
127
+ KIND.merge(because: "the job was given nil for `#{param.name}`: it was nil when the job was enqueued, " \
128
+ "in another request or process")
129
+ end
130
+
131
+ def left_out(local, call)
132
+ _klass, name, file, line = @frame
133
+ param = @source.param(file, line, name, local)
134
+ unsent(local, file, line) if param&.default_nil && !passed?(param, call)
135
+ end
136
+
137
+ private
138
+
139
+ def unsent(local, file, line)
140
+ KIND.merge(because: "the job was run without `#{local}`, which defaults to nil: " \
141
+ "it was enqueued before the parameter was there",
142
+ origin: { file: relative(file), line: line, reason: "`#{local}` defaults to nil" })
143
+ end
144
+
145
+ def relative(file) = file.delete_prefix("#{@world.root}/")
146
+
147
+ def passed?(param, call)
148
+ return @world.given.keywords(call).key?(param.name.to_s) if param.keyword
149
+
150
+ param.index < @world.given.arguments(call).size
151
+ end
152
+ end
153
+
154
+ def handed(set)
155
+ by = returned_to(set)
156
+ by ? returned(by) : read_by_caller(set)
157
+ end
158
+
159
+ def read_by_caller(set)
160
+ caller = @world.tree.call(set.made.tree, set.made.step).parent
161
+ unrecorded_attribute(argument(set), caller) if caller
162
+ end
163
+
164
+ def argument(set)
165
+ called = set.builder? ? "new" : set.name.to_s
166
+ @source.argument_at(set.made.file, set.made.line, called, set.assigned.param)
167
+ end
168
+
169
+ def nil_before(call, name)
170
+ first = call.parent.children.select { |made| nil_for?(made, call, name) }.last
171
+ Trail.descent(first).last.last if first
172
+ end
173
+
174
+ def nil_for?(made, call, name)
175
+ made.returned_nil && made.step < call.step && made.line == call.line && frame(made).fetch(1).to_s == name
176
+ end
177
+
178
+ def frame(call) = @world.paths.frame_of(call.path)
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "runtime_analysis/recording"
5
+ require "runtime_analysis/errors"
6
+
7
+ module RuntimeAnalysis
8
+ module Trace
9
+ class NilOrigin
10
+ class Index
11
+ VERSION = 1
12
+ BYTES = 4 << 20
13
+ DEFAULT_PATH = "config/runtime_analysis/nil_index.json"
14
+ EMPTY = { "models" => {}, "writes" => {}, "returns" => {}, "files" => {}, "sites" => {} }.freeze
15
+
16
+ MODEL_KEYS = %w[table columns].freeze
17
+ ASSOCIATION_KEYS = %w[kind model key].freeze
18
+ WRITE_KEYS = %w[file line how].freeze
19
+ RETURN_KEYS = %w[file line kind says].freeze
20
+ SITE_KEYS = %w[called kind file line says].freeze
21
+
22
+ def self.load(path, root: Dir.pwd, revision: nil)
23
+ new(Read.new(path).contents, root, revision)
24
+ rescue Refused => e
25
+ refused(e)
26
+ end
27
+
28
+ def self.refused(why) = new(EMPTY, nil, nil, "none: #{why}")
29
+
30
+ def self.configured(configuration)
31
+ load(configuration.nil_index || DEFAULT_PATH, revision: configuration.revision)
32
+ end
33
+
34
+ class Refused < StandardError; end
35
+
36
+ class Read
37
+ def initialize(path)
38
+ @path = path
39
+ @name = File.basename(path)
40
+ end
41
+
42
+ def contents
43
+ raise Refused, "run `ra nil_index` to build #{@name}" unless File.file?(@path)
44
+ raise Refused, "#{@name} is larger than #{BYTES} bytes" if File.size(@path) > BYTES
45
+
46
+ known(parsed)
47
+ end
48
+
49
+ private
50
+
51
+ def parsed
52
+ JSON.parse(File.read(@path))
53
+ rescue Errors.unreadable_json
54
+ raise Refused, "#{@name} does not read as JSON"
55
+ end
56
+
57
+ def known(contents)
58
+ version = contents["version"] if contents.instance_of?(Hash)
59
+ return contents if version == VERSION
60
+
61
+ raise Refused, "#{@name} is of version #{version}, and this recorder reads #{VERSION}"
62
+ end
63
+ end
64
+
65
+ def initialize(contents, root, revision, refusal = nil)
66
+ @contents = contents
67
+ @root = root
68
+ @revision = revision
69
+ @refusal = refusal
70
+ @fresh = {}
71
+ end
72
+
73
+ def model(name) = whole([part("models")[name]], MODEL_KEYS).first
74
+
75
+ def association(model, name) = whole([part("models").dig(model, "assocs", name)], ASSOCIATION_KEYS).first
76
+
77
+ def model_named(word)
78
+ names = part("models").keys
79
+ ended = names.select { |name| name.end_with?("::#{word}") }
80
+ names.include?(word) ? word : (ended.first if ended.size == 1)
81
+ end
82
+
83
+ def models_with(name)
84
+ part("models").select { |_model, facts| facts && named?(facts, name) }.keys.sort
85
+ end
86
+
87
+ def writers(model, column)
88
+ whole(part("writes").dig(model, column), WRITE_KEYS).select { |site| fresh?(site.fetch("file")) }
89
+ end
90
+
91
+ def returns(identity)
92
+ whole(part("returns")[identity], RETURN_KEYS).select { |found| fresh?(found.fetch("file")) }
93
+ end
94
+
95
+ def sites(file, line, called)
96
+ file = relative(file)
97
+ found = whole(part("sites")["#{file}:#{line}"], SITE_KEYS)
98
+ found.select { |site| site.fetch("called") == called && fresh?(file) }
99
+ end
100
+
101
+ def relative(file) = file.delete_prefix("#{@root}/")
102
+
103
+ def stale_files = @fresh.reject { |_file, fresh| fresh }.keys
104
+
105
+ def status
106
+ return @refusal if @refusal
107
+
108
+ built = @contents["commit"].to_s
109
+ running = @revision || Revision.of(@root) or return "#{built}, running commit not known"
110
+ same?(built, running) ? built : "stale: built from #{built}, running #{running}"
111
+ end
112
+
113
+ private
114
+
115
+ def part(name) = @contents[name] || {}
116
+
117
+ def whole(entries, keys)
118
+ entries.to_a.select { |entry| entry && keys.all? { |key| entry.key?(key) } }
119
+ end
120
+
121
+ def named?(facts, name)
122
+ %w[columns assocs].any? { |part| facts[part]&.key?(name) }
123
+ end
124
+
125
+ def same?(built, running)
126
+ !built.empty? && (built.start_with?(running) || running.start_with?(built))
127
+ end
128
+
129
+ def fresh?(file)
130
+ @fresh[file] = unchanged?(file)
131
+ end
132
+
133
+ def unchanged?(file)
134
+ indexed = part("files")[file] or return true
135
+ text = File.binread(File.join(@root, file))
136
+ Recording.blob_id(text) == indexed
137
+ rescue SystemCallError
138
+ false
139
+ end
140
+
141
+ module Revision
142
+ module_function
143
+
144
+ def of(root)
145
+ read(root, "REVISION") || head(root)
146
+ end
147
+
148
+ def head(root)
149
+ head = read(root, ".git/HEAD") or return nil
150
+ ref = head.delete_prefix("ref: ")
151
+ ref == head ? head : read(root, File.join(".git", ref))
152
+ end
153
+
154
+ def read(root, file)
155
+ text = File.read(File.join(root, file)).rstrip
156
+ text unless text.empty?
157
+ rescue SystemCallError
158
+ nil
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace/nil_origin/provenance_spec.rb, which builds objects in and
4
+ # before a request, and stubbed there for the lifetimes Rails gives.
5
+
6
+ module RuntimeAnalysis
7
+ module Trace
8
+ class NilOrigin
9
+ module Lifetime
10
+ KNOWN = { "AbstractController::Base" => :request, "Singleton" => :process }.freeze
11
+
12
+ module_function
13
+
14
+ def of(class_name)
15
+ above = NilOrigin.constant(class_name)&.ancestors or return nil
16
+ recorded?(above) ? :recorded : named(above)
17
+ end
18
+
19
+ def recorded?(above)
20
+ above.any? { |mod| Trace.wraps?(mod) && mod.private_method_defined?(:initialize) }
21
+ end
22
+
23
+ def named(above)
24
+ KNOWN.find { |name, _life| above.any? { |mod| mod.name == name } }&.last
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace/nil_origin_spec.rb, which names inherited methods and templates.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class NilOrigin
8
+ module Named
9
+ TEMPLATES = %w[.erb .haml .slim .builder .jbuilder .rabl].freeze
10
+
11
+ module_function
12
+
13
+ def identity(frame, root)
14
+ file = frame.fetch(2)
15
+ TEMPLATES.include?(File.extname(file)) ? file.delete_prefix("#{root}/") : called(frame)
16
+ end
17
+
18
+ def called((klass, name, _file, _line, class_method))
19
+ const = NilOrigin.constant(klass)
20
+ class_method ? of_class(const, name, klass) : "#{instance_owner(const, name, klass)}##{name}"
21
+ end
22
+
23
+ def instance_owner(const, name, klass)
24
+ const.instance_method(name).owner.name || klass
25
+ rescue NameError
26
+ klass
27
+ end
28
+
29
+ def of_class(const, name, klass)
30
+ definer = class_definer(const, name)
31
+ return "#{Recorder.class_names.of(definer.attached_object)}.#{name}" if definer&.singleton_class?
32
+
33
+ owner = definer&.name
34
+ owner ? "#{owner}##{name}" : "#{klass}.#{name}"
35
+ end
36
+
37
+ def class_definer(const, name)
38
+ above = const ? const.singleton_class.ancestors : []
39
+ above.find { |mod| !Trace.wraps?(mod) && defines?(mod, name) }
40
+ end
41
+
42
+ def defines?(mod, name)
43
+ mod.method_defined?(name, false) || mod.private_method_defined?(name, false)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace/nil_origin/provenance_spec.rb, which records requests and
4
+ # reads them through this.
5
+
6
+ require_relative "rows"
7
+
8
+ module RuntimeAnalysis
9
+ module Trace
10
+ class NilOrigin
11
+ class Objects
12
+ Made = Struct.new(:tree, :step, :path, :file, :line, :given, :stored, :nil_args, :keywords, :raised)
13
+
14
+ def initialize(held)
15
+ @words = held.fetch(:rows)
16
+ @ids = held.fetch(:ids)
17
+ @nil = @ids[NIL]
18
+ @whole = held.fetch(:left_out).zero?
19
+ end
20
+
21
+ def whole? = @whole
22
+
23
+ def calls_on(object, kind, tree, step)
24
+ @object = object
25
+ @made = {}
26
+ @last = [kind, tree, step]
27
+ Rows.each(@words) { |row_kind, at| last?(row_kind, at) ? break : read(row_kind, at) }
28
+ @made.values
29
+ end
30
+
31
+ private
32
+
33
+ def last?(kind, at) = [kind, *@words[at + 1, 2]] == @last
34
+
35
+ def read(kind, at)
36
+ case kind
37
+ when ROW_CALL then called(at)
38
+ when ROW_KWARGS then keywords(at)
39
+ when ROW_UNWIND then unwound(at)
40
+ end
41
+ end
42
+
43
+ def called(at)
44
+ @made[@words[at + 1, 2]] = made_from(row(at)) if @words.fetch(at + 9) == @object
45
+ end
46
+
47
+ def row(at) = @words[at, Rows.length(@words.fetch(at))]
48
+
49
+ def made_from(row)
50
+ made = Made.new(*row[1, 5], *row[7, 2], nils(Rows.arguments(row, 0).first), {})
51
+ made.file = names.fetch(made.file)
52
+ made
53
+ end
54
+
55
+ def unwound(at)
56
+ made = @made[@words[at + 1, 2]] and made.raised = true
57
+ end
58
+
59
+ def nils(classes)
60
+ classes.each_index.select { |index| classes.fetch(index) == @nil }
61
+ end
62
+
63
+ def keywords(at)
64
+ made = @made[@words[at + 1, 2]] or return
65
+ made.keywords = keyword_nils(*Rows.keywords(row(at)))
66
+ end
67
+
68
+ def keyword_nils(classes, _ids, keys)
69
+ names.fetch_values(*keys).zip(classes.map { |klass| klass == @nil }).to_h
70
+ end
71
+
72
+ def names
73
+ @names ||= @ids.invert
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "objects"
4
+ require_relative "weighed"
5
+ require_relative "told"
6
+ require_relative "beyond"
7
+
8
+ module RuntimeAnalysis
9
+ module Trace
10
+ class NilOrigin
11
+ class Provenance
12
+ Read = Struct.new(:object, :klass, :ivar, :kind, :tree, :step, :file)
13
+ Never = Struct.new(:built)
14
+
15
+ World = Struct.new(:tree, :objects, :paths, :index, :built, :given, :entry, :root)
16
+
17
+ def self.world(*fields) = World.new(*fields)
18
+ def self.never?(ending) = ending.instance_of?(Never)
19
+
20
+ def initialize(world, told)
21
+ @world = world
22
+ @told = told
23
+ end
24
+
25
+ def of(node, hops, raised)
26
+ start
27
+ call = hops.reverse.find { |kind, _| kind == :returned }&.last
28
+ read = call ? read_by(call) : read_at(node, raised)
29
+ read ? held(read).merge(from_request(read)) : from_elsewhere(node, hops, call, raised)
30
+ end
31
+
32
+ private
33
+
34
+ def start
35
+ @source = Source.new
36
+ @assigning = {}
37
+ @beyond = Beyond.new(@world, @source)
38
+ end
39
+
40
+ def held(read)
41
+ read.kind == ROW_UNWIND ? { held_in: read.ivar } : {}
42
+ end
43
+
44
+ def from_request(read)
45
+ sets, ending = follow(read)
46
+ return @told.ended(read, ending, @source) if sets.empty?
47
+
48
+ @told.set(sets, ending).merge(@beyond.behind(sets.last.last))
49
+ end
50
+
51
+ def from_elsewhere(node, hops, call, raised)
52
+ call || hops.empty? ? @beyond.of(node, call, raised) : @beyond.received(hops.last.last)
53
+ end
54
+
55
+ def read_at(node, raised)
56
+ kind, ivar = @source.receiver_at(*raised)
57
+ return unless kind == :ivar
58
+
59
+ klass, _name, file = frame(node)
60
+ Read.new(node.receiver, klass, ivar, ROW_UNWIND, node.tree, node.step, file)
61
+ end
62
+
63
+ def read_by(call)
64
+ klass, name, file, line = frame(call)
65
+ ivar = @source.read_by(file, line, name)
66
+ Read.new(call.receiver, klass, ivar, ROW_CALL, call.tree, call.step, file) if ivar
67
+ end
68
+
69
+ def follow(read, sets = [])
70
+ found = settle(read)
71
+ return [sets, found] unless Weighed.set?(found)
72
+
73
+ sets << [read, found]
74
+ handed = handed_on(found) if sets.size < NilOrigin.hops
75
+ handed ? follow(handed, sets) : [sets]
76
+ end
77
+
78
+ def settle(read)
79
+ calls = calls_before(read)
80
+ met = []
81
+ last = calls.find { |made| ends?(weigh(made, read.ivar), met) }
82
+ last ? close(met, weigh(last, read.ivar)) : open_ended(met, read, calls)
83
+ end
84
+
85
+ def calls_before(read)
86
+ @world.objects.calls_on(read.object, read.kind, read.tree, read.step).reverse
87
+ end
88
+
89
+ def open_ended(met, read, calls)
90
+ met.empty? ? unset(read, calls) : met
91
+ end
92
+
93
+ def ends?(weighed, met)
94
+ return true if weighed.last?
95
+
96
+ met.concat(weighed.candidates)
97
+ false
98
+ end
99
+
100
+ def weigh(made, ivar)
101
+ assigned = (@assigning[ivar] ||= {})[made.path] ||= assigning(made.path, ivar)
102
+ Weighed.new(made, @world.paths.frame_of(made.path), assigned)
103
+ end
104
+
105
+ def assigning(path, ivar)
106
+ _klass, name, file, line = @world.paths.frame_of(path)
107
+ @source.assigned_in(file, line, name).select { |found| found.ivar == ivar }
108
+ end
109
+
110
+ def close(met, weighed)
111
+ set = weighed.set
112
+ return set if set && met.empty?
113
+
114
+ met.concat(set ? [set] : weighed.unsure)
115
+ met unless met.empty?
116
+ end
117
+
118
+ def unset(read, calls)
119
+ built = calls.reverse.find { |made| @world.paths.frame_of(made.path).fetch(1) == :initialize }
120
+ built ? Never.new(built) : lived(@world.built.call(read.klass))
121
+ end
122
+
123
+ def lived(life)
124
+ life == :recorded ? (:not_built if @world.objects.whole?) : life
125
+ end
126
+
127
+ def handed_on(set)
128
+ returned = @beyond.returned_to(set) or return nil
129
+ read_by(returned)
130
+ end
131
+
132
+ def frame(call) = @world.paths.frame_of(call.path)
133
+ end
134
+ end
135
+ end
136
+ end