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,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: its wording is asserted in trace/nil_origin/provenance_spec.rb.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class NilOrigin
8
+ class Told
9
+ MOST = 5
10
+
11
+ def initialize(root)
12
+ @root = root
13
+ end
14
+
15
+ def set(sets, ending)
16
+ lines = sets.map { |read, found| "#{read.klass} #{verb(found)} #{reason(found)} at #{site(found)}" }
17
+ through(lines).merge({ origin: origin(sets.last.last) }, behind(ending))
18
+ end
19
+
20
+ def through(lines)
21
+ { kind: "set_in_request", because: "the same #{lines.first}", set_through: (lines if lines.size > 1) }
22
+ end
23
+
24
+ def behind(ending)
25
+ ending.instance_of?(Array) ? { candidates: candidates(ending) } : {}
26
+ end
27
+
28
+ def ended(read, ending, source)
29
+ return listed(ending) if ending.instance_of?(Array)
30
+ return never(read, ending.built) if Provenance.never?(ending)
31
+
32
+ lived(read, ending, source)
33
+ end
34
+
35
+ private
36
+
37
+ def origin(found)
38
+ { file: relative(found.made.file), line: found.made.line, reason: reason(found).delete_suffix(",") }
39
+ end
40
+
41
+ def listed(met)
42
+ origin = assigned_at(met.first) if sure?(met)
43
+ { kind: "set_in_request", candidates: candidates(met), origin: origin }
44
+ end
45
+
46
+ def candidates(met) = met.first(MOST).map { |found| candidate(found) }
47
+
48
+ def sure?(met)
49
+ only = met.first.assigned
50
+ met.size == 1 && only.nil_literal && only.always
51
+ end
52
+
53
+ def assigned_at(found)
54
+ { file: relative(found.frame.fetch(2)), line: found.assigned.line,
55
+ reason: "#{identity(found.frame)} assigns nil to #{found.assigned.ivar}" }
56
+ end
57
+
58
+ def lived(read, life, source)
59
+ return unbuilt(read) if life == :not_built
60
+ return {} unless life
61
+
62
+ life == :request ? each_request(read, idle(read, source)) : once(read, idle(read, source))
63
+ end
64
+
65
+ def unbuilt(read)
66
+ { kind: "process_memory", because: "this request has no call that built this #{read.klass}" }
67
+ end
68
+
69
+ def each_request(read, idle)
70
+ { kind: "never_assigned", candidates: idle.lines, origin: idle.origin,
71
+ because: "this #{read.klass} is built for each request, " \
72
+ "and no recorded method that ran on it assigns #{read.ivar}" }
73
+ end
74
+
75
+ def once(read, idle)
76
+ { kind: "process_memory", candidates: idle.lines, origin: idle.origin,
77
+ because: "this #{read.klass} is built once for the process, " \
78
+ "and no call of this request assigned #{read.ivar}" }
79
+ end
80
+
81
+ def idle(read, source) = Idle.new(read, source, @root)
82
+
83
+ class Idle
84
+ def initialize(read, source, root)
85
+ @read = read
86
+ @source = source
87
+ @file = read.file.delete_prefix("#{root}/")
88
+ @found = source.assigners(read.file, read.klass, read.ivar)
89
+ end
90
+
91
+ def lines
92
+ said = @found.first(MOST).map do |name, line|
93
+ "#{@read.klass}##{name} assigns #{@read.ivar} (#{@file}:#{line}) and did not run in this request"
94
+ end
95
+ said unless said.empty?
96
+ end
97
+
98
+ def origin
99
+ return unless @found.size == 1
100
+
101
+ name, assigned = @found.first
102
+ line, said = @source.declared(@read.file, name)
103
+ reason = line ? "`#{said}` did not run #{name}" : "#{name} did not run"
104
+ { file: @file, line: line || assigned, reason: reason }
105
+ end
106
+ end
107
+
108
+ def never(read, built)
109
+ { kind: "never_assigned",
110
+ because: "the same #{read.klass} was built at #{place(built.file, built.line)}, " \
111
+ "and no recorded method that ran on it assigns #{read.ivar}" }
112
+ end
113
+
114
+ def candidate(found)
115
+ return "#{said(found)} at #{site(found)}" if found.given
116
+
117
+ "#{identity(found.frame)} assigns #{what(found)}, and ran on this object at #{site(found)}"
118
+ end
119
+
120
+ def what(found)
121
+ assigned = found.assigned
122
+ "#{"nil to " if assigned.nil_literal}#{assigned.ivar} (#{place(found.frame.fetch(2), assigned.line)})"
123
+ end
124
+
125
+ def said(found)
126
+ found.writer? ? "`#{found.name}` was called with nil" : "it #{verb(found)} #{reason(found)}"
127
+ end
128
+
129
+ def verb(found) = found.builder? ? "was" : "had"
130
+
131
+ def reason(found)
132
+ return "`#{found.name}` called with nil" if found.writer?
133
+
134
+ found.builder? ? "built #{handed(found)}" : "`#{found.name}` called #{handed(found)}"
135
+ end
136
+
137
+ def handed(found)
138
+ name = found.assigned.param.name
139
+ { nil: "with nil for `#{name}`", default: "without `#{name}`, which defaults to nil," }.fetch(found.given)
140
+ end
141
+
142
+ def identity(frame) = Named.called(frame)
143
+
144
+ def site(found) = place(found.made.file, found.made.line)
145
+
146
+ def place(file, line) = "#{relative(file)}:#{line}"
147
+
148
+ def relative(file) = file.delete_prefix("#{@root}/")
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace/nil_origin/stored_spec.rb, which resolves receivers by these.
4
+
5
+ require_relative "rows"
6
+
7
+ module RuntimeAnalysis
8
+ module Trace
9
+ class NilOrigin
10
+ class Values
11
+ def initialize(held)
12
+ @words = held.fetch(:rows)
13
+ @names = held.fetch(:ids).invert
14
+ end
15
+
16
+ def result(call)
17
+ klass, id = call.returned
18
+ [@names.fetch(klass), id]
19
+ end
20
+
21
+ def arguments(call)
22
+ classes, ids = Rows.arguments(@words, call.at)
23
+ named(classes).zip(ids)
24
+ end
25
+
26
+ def keywords(call)
27
+ row = keywords_row_after(call) or return {}
28
+ classes, ids, names = Rows.keywords(row)
29
+ @names.fetch_values(*names).zip(named(classes).zip(ids)).to_h
30
+ end
31
+
32
+ private
33
+
34
+ def keywords_row_after(call)
35
+ row = row_at(call.at + row_at(call.at).size)
36
+ row if Rows.kind(row.first) == ROW_KWARGS
37
+ end
38
+
39
+ def row_at(at)
40
+ @words[at, Rows.length(@words.fetch(at))]
41
+ end
42
+
43
+ def named(classes) = @names.fetch_values(*classes)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace/nil_origin/provenance_spec.rb, case by case.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class NilOrigin
8
+ class Weighed
9
+ Set = Struct.new(:made, :frame, :assigned, :given)
10
+
11
+ class Set
12
+ def name = frame.fetch(1)
13
+ def writer? = name.end_with?("=")
14
+ def builder? = name == :initialize
15
+ end
16
+
17
+ def self.set?(found) = found.instance_of?(Set)
18
+
19
+ def initialize(made, frame, assigned)
20
+ @made = made
21
+ @frame = frame
22
+ @assigned = assigned
23
+ @given = shown
24
+ end
25
+
26
+ def set
27
+ Set.new(@made, @frame, sole, @given) if @given
28
+ end
29
+
30
+ def candidates
31
+ @assigned.map { |found| Set.new(@made, @frame, found) }
32
+ end
33
+
34
+ def unsure
35
+ @given.nil? ? candidates : []
36
+ end
37
+
38
+ def last?
39
+ !@made.raised && @assigned.any?(&:always)
40
+ end
41
+
42
+ private
43
+
44
+ def sole
45
+ @assigned.first if @assigned.size == 1
46
+ end
47
+
48
+ def shown
49
+ param = sole&.param or return nil
50
+ param.keyword ? keyword(param) : positional(param)
51
+ end
52
+
53
+ def positional(param)
54
+ return value(@made.nil_args.include?(param.index)) if param.index < @made.stored
55
+
56
+ left_out(param) if param.index >= @made.given
57
+ end
58
+
59
+ def keyword(param)
60
+ name = param.name.to_s
61
+ @made.keywords.key?(name) ? value(@made.keywords.fetch(name)) : left_out(param)
62
+ end
63
+
64
+ def value(is_nil) = is_nil && :nil
65
+
66
+ def left_out(param) = param.default_nil && :default
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: its order and wording are asserted in trace/nil_origin/stored_spec.rb.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class NilOrigin
8
+ class Writers
9
+ MOST = 5
10
+ TOUCHING = %w[update form assignment].freeze
11
+ UNCHECKED = %w[update_all update_columns raw_sql].freeze
12
+ ORDER = %i[added_after ran never_ran untouched].freeze
13
+ WHY = {
14
+ "migration" => "added the column with no default and no backfill",
15
+ "callback" => "a callback of the model assigns it",
16
+ "normalizes" => "normalises the value, which can give nil",
17
+ "create_without" => "creates the row without it",
18
+ "import" => "an import creates the row",
19
+ "seed" => "the seeds create the row",
20
+ "form" => "a form field that can be sent blank",
21
+ "insert_all" => "inserts rows with no callback"
22
+ }.freeze
23
+
24
+ def initialize(sites, row)
25
+ @sites = sites
26
+ @row = row
27
+ end
28
+
29
+ def told
30
+ ranked = ORDER.flat_map { |standing| @sites.select { |site| standing(site) == standing } }
31
+ ranked.first(MOST).map { |site| said(site) }
32
+ end
33
+
34
+ private
35
+
36
+ def said(site)
37
+ { file: site.fetch("file"), line: site.fetch("line"), how: site.fetch("how"), why: why(site) }
38
+ end
39
+
40
+ def standing(site)
41
+ return :added_after if added_after?(site)
42
+ return :untouched if untouched?(site)
43
+
44
+ never_ran?(site) ? :never_ran : :ran
45
+ end
46
+
47
+ def added_after?(site) = site.fetch("how") == "migration" && @row.include?("created_before_column_added")
48
+ def untouched?(site) = TOUCHING.include?(site.fetch("how")) && @row.include?("never_updated")
49
+ def never_ran?(site) = site["ran"]&.zero?
50
+
51
+ def why(site)
52
+ [reason(site), ("never ran in a recording" if never_ran?(site)),
53
+ ("the row was never updated" if untouched?(site))].compact.join("; ")
54
+ end
55
+
56
+ def reason(site)
57
+ how = site.fetch("how")
58
+ return "#{WHY.fetch(how)}, after this row was created" if added_after?(site)
59
+ return unchecked(site) if UNCHECKED.include?(how)
60
+ return "may set it through #{splat(site)}" if how == "create_open"
61
+
62
+ WHY.key?(how) ? left_out(site, WHY.fetch(how)) : assigned(site)
63
+ end
64
+
65
+ def splat(site) = site.key?("through") ? "`#{site.fetch("through")}`" : "a splat"
66
+
67
+ def unchecked(site)
68
+ "#{site["nil"] ? "writes nil" : "writes it"}, with no validation or callback"
69
+ end
70
+
71
+ def left_out(site, reason)
72
+ site.fetch("how") == "insert_all" && site["nil"] ? "#{reason} and without this column" : reason
73
+ end
74
+
75
+ def assigned(site) = site["nil"] ? "assigns nil" : "assigns a value that can be nil"
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,278 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "nil_origin/rows"
4
+ require_relative "nil_origin/named"
5
+ require_relative "nil_origin/lifetime"
6
+ require_relative "nil_origin/objects"
7
+ require_relative "nil_origin/values"
8
+ require_relative "nil_origin/provenance"
9
+ require_relative "nil_origin/answers"
10
+
11
+ module RuntimeAnalysis
12
+ module Trace
13
+ class NilOrigin
14
+ WORD_BYTES = 8
15
+ HELD_BYTES = 2 * 1024 * 1024
16
+ MOST = HELD_BYTES / WORD_BYTES
17
+ HOPS = 32
18
+ CAUSES = 8
19
+ NIL = "NilClass"
20
+ CAUSE = ::Exception.instance_method(:cause)
21
+ ROW_CALL, ROW_RETURN, ROW_KWARGS, ROW_UNWIND, ROW_ENTRY, ROW_SHIFT =
22
+ Native.rows.values_at(:call, :return, :kwargs, :unwind, :entry, :shift)
23
+
24
+ autoload :Source, File.join(__dir__, "nil_origin/source")
25
+ UNTRACED = "the nil did not come from a recorded call"
26
+
27
+ extend Answers
28
+
29
+ class << self
30
+ attr_writer :index
31
+
32
+ def index
33
+ @index ||= Index.refused("no index was loaded")
34
+ end
35
+
36
+ def constant(name)
37
+ Object.const_get(name)
38
+ rescue NameError
39
+ nil
40
+ end
41
+ end
42
+
43
+ Call = Struct.new(:tree, :step, :path, :line, :receiver, :at, :nil_arg, :children, :parent, :returned,
44
+ :returned_nil, :exploded)
45
+
46
+ def self.nil_error(error)
47
+ CAUSES.times do
48
+ return error if error.nil? || Recorder.missing(error)&.last == NIL
49
+
50
+ error = CAUSE.bind_call(error)
51
+ end
52
+ nil
53
+ end
54
+
55
+ def initialize(error, held, paths, root:)
56
+ @called = Recorder.missing(error).first
57
+ @file, @line = Recorder.raise_site(error)
58
+ @held = held
59
+ @paths = paths
60
+ @root = root
61
+ end
62
+
63
+ def facts(built)
64
+ @built = built
65
+ node = tree.exploded(@called, @file, @line)
66
+ told = { called: @called, raised_at: place(@file, @line), request: entry }
67
+ told.merge!(traced(node)) if node
68
+ told.merge(source).compact
69
+ end
70
+
71
+ private
72
+
73
+ def tree
74
+ @tree ||= Tree.new(@held, @paths)
75
+ end
76
+
77
+ def entry
78
+ @entry ||= tree.entry
79
+ end
80
+
81
+ def source
82
+ left_out = @held.fetch(:left_out)
83
+ { segment: @held.fetch(:segment), rows_left_out: (left_out if left_out.positive?) }
84
+ end
85
+
86
+ def traced(node)
87
+ hops = Trail.new(node, @line).hops
88
+ found = provenance.of(node, hops, [@file, @line, @called])
89
+ hops = [] if found.key?(:held_in)
90
+ told = { raised_in: identity(node) }.merge(origin(hops), passed(hops), found)
91
+ told.merge(sentence: sentence(told))
92
+ end
93
+
94
+ def provenance
95
+ Provenance.new(world, Told.new(@root))
96
+ end
97
+
98
+ def world
99
+ Provenance.world(tree, Objects.new(@held), @paths, NilOrigin.index, @built, Values.new(@held), entry,
100
+ @root)
101
+ end
102
+
103
+ def origin(hops)
104
+ call = hops.reverse.find { |kind, _| kind == :returned }&.last or return {}
105
+ { returned_by: identity(call), returned_at: place(*frame(call)[2, 2]) }
106
+ end
107
+
108
+ def passed(hops)
109
+ hops.empty? ? {} : { passed_through: hops.map { |kind, call| __send__(kind, call) } }
110
+ end
111
+
112
+ def sentence(told)
113
+ "`#{@called}` was called on nil#{from(told)} in #{told.fetch(:raised_in)}#{during}#{untraced(told)}"
114
+ end
115
+
116
+ def from(told)
117
+ return " returned by #{told.fetch(:returned_by)} (#{told.fetch(:returned_at)})," if told.key?(:returned_by)
118
+
119
+ " held in #{told.fetch(:held_in)}," if told.key?(:held_in)
120
+ end
121
+
122
+ def during
123
+ ", during #{entry}" if entry
124
+ end
125
+
126
+ def untraced(told)
127
+ "; #{UNTRACED}" unless told.key?(:returned_by) || told.key?(:held_in)
128
+ end
129
+
130
+ def returned(call) = "#{identity(call)} returned nil"
131
+
132
+ def received(call) = "#{identity(call)} received nil as argument #{call.nil_arg + 1}"
133
+
134
+ def frame(call) = @paths.frame_of(call.path)
135
+
136
+ def identity(call) = Named.identity(frame(call), @root)
137
+
138
+ def place(file, line)
139
+ "#{file.delete_prefix("#{@root}/")}:#{line}" if file
140
+ end
141
+
142
+ class Tree
143
+ TREE_AT = 1
144
+ STEP_AT = 2
145
+ PATH_AT = 3
146
+ LINE_AT = 5
147
+ RECEIVER_AT = 9
148
+ CALL_FIELDS = [TREE_AT, STEP_AT, PATH_AT, LINE_AT, RECEIVER_AT].freeze
149
+
150
+ def initialize(held, paths)
151
+ @words = held.fetch(:rows)
152
+ @ids = held.fetch(:ids)
153
+ @paths = paths
154
+ @nil = @ids[NIL]
155
+ end
156
+
157
+ def exploded(called, file, line)
158
+ @raise = [@ids[file], line, @ids[called], @nil]
159
+ calls_of(last_unwound || rewrapped).each_value.select(&:exploded).last
160
+ end
161
+
162
+ def call(tree, step) = calls_of(tree).fetch(step)
163
+
164
+ def entry
165
+ found = nil
166
+ Rows.each(@words) { |kind, at| found = @words.fetch(at + 2) if kind == ROW_ENTRY }
167
+ @ids.key(found)
168
+ end
169
+
170
+ private
171
+
172
+ def last_unwound
173
+ tree = nil
174
+ Rows.each(@words) { |kind, at| tree = @words.fetch(at + 1) if kind == ROW_UNWIND && raised_here?(at) }
175
+ tree
176
+ end
177
+
178
+ def rewrapped
179
+ @raise = @raise.first(2)
180
+ last_unwound
181
+ end
182
+
183
+ def raised_here?(at) = @words[at + 4, @raise.size] == @raise
184
+
185
+ def calls_of(tree)
186
+ (@built ||= {})[tree] ||= build(tree)
187
+ end
188
+
189
+ def build(tree)
190
+ @calls = {}
191
+ @open = []
192
+ Rows.each(@words) { |kind, at| read(kind, at) if @words.fetch(at + 1) == tree }
193
+ @calls
194
+ end
195
+
196
+ def read(kind, at)
197
+ case kind
198
+ when ROW_CALL then called(at)
199
+ when ROW_RETURN then returned(at)
200
+ when ROW_UNWIND then unwound(at)
201
+ end
202
+ end
203
+
204
+ def called(at)
205
+ fields = CALL_FIELDS.map { |field| @words.fetch(at + field) }
206
+ call = Call.new(*fields, at, nil_argument(at), [])
207
+ @calls[call.step] = attach(call)
208
+ end
209
+
210
+ def attach(call)
211
+ call.parent = caller_of(call)
212
+ call.parent.children << call if call.parent
213
+ (@open << call).last
214
+ end
215
+
216
+ def caller_of(call)
217
+ above = @paths.caller_of(call.path)&.id
218
+ kept = @open.index { |open| open.path == above }
219
+ @open = kept ? @open.first(kept + 1) : []
220
+ @open.last
221
+ end
222
+
223
+ def nil_argument(at) = Rows.arguments(@words, at).first.index(@nil)
224
+
225
+ def returned(at)
226
+ call = @calls[@words.fetch(at + 2)] or return
227
+ call.returned = [@words.fetch(at + 3), @words.fetch(at + 4)]
228
+ call.returned_nil = call.returned.first == @nil
229
+ end
230
+
231
+ def unwound(at)
232
+ call = @calls[@words.fetch(at + 2)] or return
233
+ call.exploded = raised_here?(at)
234
+ end
235
+ end
236
+
237
+ class Trail
238
+ def self.descent(call)
239
+ under = Enumerator.produce(call) { |made| last_nil(made.children) or raise StopIteration }
240
+ under.first(NilOrigin.hops).map { |made| [:returned, made] }
241
+ end
242
+
243
+ def self.last_nil(calls) = calls.select(&:returned_nil).last
244
+
245
+ def initialize(node, line)
246
+ @node = node
247
+ @line = line
248
+ end
249
+
250
+ def hops
251
+ source = returned_into
252
+ source ? Trail.descent(source) : received(@node)
253
+ end
254
+
255
+ private
256
+
257
+ def returned_into
258
+ in_time.reverse_each.find { |call| call.line == @line } || in_time.last
259
+ end
260
+
261
+ def in_time
262
+ @node.children.select { |call| call.returned_nil && call.line <= @line }
263
+ end
264
+
265
+ def received(call, room = NilOrigin.hops)
266
+ return [] if call&.nil_arg.nil? || room.zero?
267
+
268
+ source = Trail.last_nil(earlier(call))
269
+ [[:received, call], *(source ? Trail.descent(source) : received(call.parent, room - 1))]
270
+ end
271
+
272
+ def earlier(call)
273
+ call.parent&.children.to_a.select { |made| made.step < call.step }
274
+ end
275
+ end
276
+ end
277
+ end
278
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/native"
4
+ require_relative "pure"
5
+
6
+ module RuntimeAnalysis
7
+ module Trace
8
+ class Novelty < (Native.loaded? ? Object : Pure.novelty)
9
+ DEFAULT_RESAMPLE = 64
10
+
11
+ def initialize(resample: DEFAULT_RESAMPLE)
12
+ super()
13
+ self.resample = resample
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through novelty_spec.rb, which runs the novelty gate examples against it.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ module Pure
8
+ class Novelty
9
+ DEFAULT_RESAMPLE = 64
10
+
11
+ attr_reader :recorded, :skipped
12
+
13
+ def initialize(resample: DEFAULT_RESAMPLE)
14
+ @resample = resample
15
+ @seen = Set.new
16
+ @recorded = 0
17
+ @skipped = 0
18
+ end
19
+
20
+ attr_writer :resample
21
+
22
+ def record?(path_hash)
23
+ @seen.add?(path_hash) || due? ? kept : dropped
24
+ end
25
+
26
+ private
27
+
28
+ def due? = (@since += 1) >= @resample
29
+
30
+ def kept
31
+ @since = 0
32
+ @recorded += 1
33
+ true
34
+ end
35
+
36
+ def dropped
37
+ @skipped += 1
38
+ false
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end