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,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through state_spec.rb, which runs the call-tree examples against it.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ module Pure
8
+ class State
9
+ HASH_MASK = (1 << 62) - 1
10
+ FOLD_MASK = (1 << 40) - 1
11
+ MULTIPLIER = 1_000_003
12
+
13
+ SEQUENCE = Object.new.tap do |seq|
14
+ seq.instance_variable_set(:@mutex, Mutex.new)
15
+ seq.instance_variable_set(:@value, 0)
16
+ def seq.next = @mutex.synchronize { @value += 1 }
17
+ end
18
+ private_constant :SEQUENCE
19
+
20
+ def self.next_uuid
21
+ SEQUENCE.next
22
+ end
23
+
24
+ attr_reader :path_hash, :gem_run
25
+
26
+ attr_accessor :entry_point, :inner_entry
27
+
28
+ def tree = @uuid
29
+
30
+ def initialize
31
+ @stamped = @capturing = false
32
+ @step = @path_hash = @gem_run = 0
33
+ @hashes = []
34
+ @gem_runs = []
35
+ end
36
+
37
+ def enter(frame, gem: false)
38
+ @uuid ||= self.class.next_uuid
39
+ @step += 1
40
+ push(frame.instance_of?(Integer) ? frame : frame.hash, gem)
41
+ [@uuid, @step]
42
+ end
43
+
44
+ def within?(depth)
45
+ !@hashes.empty? && @gem_run < depth
46
+ end
47
+
48
+ def capturing? = @capturing
49
+
50
+ def capture
51
+ @capturing = true
52
+ yield
53
+ ensure
54
+ @capturing = false
55
+ end
56
+
57
+ def parent_hash
58
+ @hashes.last
59
+ end
60
+
61
+ def idle? = @hashes.empty?
62
+
63
+ def depth = @hashes.size
64
+
65
+ def first_record?
66
+ return false if @stamped
67
+
68
+ @stamped = true
69
+ end
70
+
71
+ def leave
72
+ @path_hash = @hashes.pop
73
+ @gem_run = @gem_runs.pop
74
+ roll if @hashes.empty?
75
+ end
76
+
77
+ private
78
+
79
+ def push(key, gem)
80
+ @hashes.push(@path_hash)
81
+ @path_hash = (((@path_hash & FOLD_MASK) * MULTIPLIER) ^ key) & HASH_MASK
82
+ @gem_runs.push(@gem_run)
83
+ @gem_run = gem ? @gem_run + 1 : 0
84
+ end
85
+
86
+ def roll
87
+ @uuid = nil
88
+ @stamped = false
89
+ @step = @path_hash = @gem_run = 0
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through state_spec.rb and novelty_spec.rb, which run the Ruby stand-ins it answers.
4
+
5
+ require_relative "pure/state"
6
+ require_relative "pure/novelty"
7
+
8
+ module RuntimeAnalysis
9
+ module Trace
10
+ module Pure
11
+ def self.state = State
12
+ def self.novelty = Novelty
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Trace
5
+ class Recorder
6
+ module ClassName
7
+ KLASS = ::Kernel.instance_method(:class)
8
+ OBJECT_ID = ::Kernel.instance_method(:object_id)
9
+ NAME = ::Module.instance_method(:name)
10
+ ANCESTORS = ::Module.instance_method(:ancestors)
11
+ NO_ID_KEPT = 0
12
+ OWN = "RuntimeAnalysis::"
13
+
14
+ module_function
15
+
16
+ def of(receiver)
17
+ klass = class_of(receiver)
18
+ NAME.bind_call(klass) || anonymous(klass)
19
+ end
20
+
21
+ def of_value(value)
22
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
23
+ Module === value ? "#<Class:#{of(value)}>" : of(value)
24
+ # rubocop:enable Style/CaseEquality
25
+ end
26
+
27
+ def id_of(object)
28
+ return NO_ID_KEPT if id_made_from_value?(object)
29
+
30
+ OBJECT_ID.bind_call(object)
31
+ end
32
+
33
+ def id_made_from_value?(object)
34
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
35
+ Integer === object || Float === object || Symbol === object
36
+ # rubocop:enable Style/CaseEquality
37
+ end
38
+
39
+ def class_of(receiver)
40
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
41
+ Module === receiver ? receiver : KLASS.bind_call(receiver)
42
+ # rubocop:enable Style/CaseEquality
43
+ end
44
+
45
+ def anonymous(klass)
46
+ names = superclasses_first(klass).filter_map { |mod| NAME.bind_call(mod) }
47
+ "#{names.find { |name| !name.start_with?(OWN) }}(anonymous)"
48
+ end
49
+
50
+ def superclasses_first(klass)
51
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
52
+ added_below_object(klass).partition { |mod| Class === mod }.flatten
53
+ # rubocop:enable Style/CaseEquality
54
+ end
55
+
56
+ def added_below_object(klass)
57
+ below_object = ANCESTORS.bind_call(klass).take_while { |mod| !Object.equal?(mod) }
58
+ below_object.reject { |mod| BasicObject.equal?(mod) }
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "class_name"
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Recorder
8
+ module Fingerprint
9
+ HASH = ::Kernel.instance_method(:hash)
10
+ KLASS = ::Kernel.instance_method(:class)
11
+ BYTESIZE = ::String.instance_method(:bytesize)
12
+ IDENTITY = 0
13
+ PRINT = 1
14
+ LONGEST_STRING = 128
15
+ PRINTS = (1 << 62) - 1
16
+
17
+ module_function
18
+
19
+ def of(value)
20
+ small?(value) ? [PRINT, print(value)] : [IDENTITY, ClassName.id_of(value)]
21
+ end
22
+
23
+ def identity(value) = [IDENTITY, ClassName.id_of(value)]
24
+
25
+ def print(value)
26
+ ([HASH.bind_call(KLASS.bind_call(value)), HASH.bind_call(value)].hash % PRINTS) + 1
27
+ end
28
+
29
+ def small?(value)
30
+ case value
31
+ when Integer, Float, Symbol, nil, true, false then true
32
+ when String then BYTESIZE.bind_call(value) <= LONGEST_STRING
33
+ else false
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/native"
4
+ require_relative "class_name"
5
+
6
+ module RuntimeAnalysis
7
+ module Trace
8
+ class Recorder
9
+ module FrameKey
10
+ MASK = (1 << 62) - 1
11
+
12
+ if Native.loaded?
13
+ WRAPPER = Native.wrapper
14
+
15
+ def self.of(id, receiver, _entry) = WRAPPER.frame_key(id, receiver)
16
+ else
17
+ BY_METHOD = 2_654_435_761
18
+ BY_CLASS = 40_503
19
+
20
+ def self.of(id, receiver, entry)
21
+ ((id * BY_METHOD) ^ entry.fetch(6).to_i ^ (ClassName.class_of(receiver).object_id * BY_CLASS)) & MASK
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Trace
5
+ class Recorder
6
+ class Paths
7
+ class Path
8
+ attr_reader :id
9
+
10
+ def initialize(table, id)
11
+ @table = table
12
+ @id = id
13
+ end
14
+
15
+ def parent = @table.caller_of(id)
16
+ def frame = @table.frame_of(id)
17
+ def call_file = @table.file_of(id)
18
+ def call_line = @table.line_of(id)
19
+
20
+ def ==(other)
21
+ other.instance_of?(Path) && other.id == id
22
+ end
23
+
24
+ def inspect = "#<#{self.class} #{id}>"
25
+ end
26
+
27
+ def initialize
28
+ @published = {}
29
+ @frame_list = Numbered.new
30
+ @file_list = Numbered.new
31
+ @mutex = Mutex.new
32
+ number_from_one
33
+ end
34
+
35
+ def [](path_hash)
36
+ number = @published[path_hash]
37
+ Path.new(self, number) if number
38
+ end
39
+
40
+ def define(path_hash, parent_hash, frame, loc)
41
+ number = @published[path_hash] ||
42
+ @mutex.synchronize { @published[path_hash] ||= add(parent_hash, frame, loc) }
43
+ Path.new(self, number)
44
+ end
45
+
46
+ def size = @callers.size - 1
47
+
48
+ def caller_of(number)
49
+ caller = @callers.fetch(number)
50
+ Path.new(self, caller) unless caller.zero?
51
+ end
52
+
53
+ def frame_of(number) = @frame_list[@frames.fetch(number)]
54
+ def file_of(number) = @file_list[@files.fetch(number)]
55
+ def line_of(number) = @lines.fetch(number)
56
+
57
+ private
58
+
59
+ def number_from_one
60
+ @callers = [nil]
61
+ @frames = [nil]
62
+ @files = [nil]
63
+ @lines = [nil]
64
+ end
65
+
66
+ def add(parent_hash, frame, loc)
67
+ @callers << (@published[parent_hash] || 0)
68
+ @frames << @frame_list.number(frame)
69
+ @files << @file_list.number(loc&.path)
70
+ @lines << loc&.lineno
71
+ @callers.size - 1
72
+ end
73
+
74
+ class Numbered
75
+ def initialize
76
+ @values = []
77
+ @numbers = {}
78
+ end
79
+
80
+ def [](number) = @values.fetch(number)
81
+
82
+ def number(value)
83
+ @numbers[value] ||= (@values << value).size - 1
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "class_name"
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Recorder
8
+ module RaiseSite
9
+ LOCATIONS = ::Exception.instance_method(:backtrace_locations)
10
+ NAME = ::NameError.instance_method(:name)
11
+ RECEIVER = ::NameError.instance_method(:receiver)
12
+
13
+ module_function
14
+
15
+ def of(error)
16
+ location = LOCATIONS.bind_call(error)&.first
17
+ location && [location.path, location.lineno]
18
+ end
19
+
20
+ def missing(error)
21
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
22
+ return unless NameError === error
23
+ # rubocop:enable Style/CaseEquality
24
+
25
+ [NAME.bind_call(error).to_s, receiver_class(error)]
26
+ end
27
+
28
+ def receiver_class(error)
29
+ ClassName.of(RECEIVER.bind_call(error))
30
+ rescue ArgumentError
31
+ nil
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through recorder_spec.rb and trace_spec.rb, which record calls into the app and gems.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Recorder
8
+ module Reachability
9
+ FILE = 2
10
+ DEPTH = 4
11
+ HEAD_INTO_GEM = 3
12
+
13
+ private
14
+
15
+ def reachable?(entry, state)
16
+ depth = entry.fetch(DEPTH)
17
+ depth ? state.within?(depth) : traceable?(entry.fetch(FILE))
18
+ end
19
+
20
+ def gem?(entry)
21
+ !entry.fetch(DEPTH).nil?
22
+ end
23
+
24
+ def direct?(head, site)
25
+ return true unless head.fetch(HEAD_INTO_GEM)
26
+
27
+ file = site.path
28
+ State.current.gem_run == 1 ? traceable?(file) : !gem_depth(file).nil?
29
+ end
30
+
31
+ def gem_depth(file)
32
+ @gem_depths ||= {}
33
+ @gem_depths.fetch(file) { |key| @gem_depths[key] = @inclusion.gem_depth(key) }
34
+ end
35
+
36
+ def traceable?(file)
37
+ include_cache.fetch(file) { |key| include_cache[key] = @inclusion.include?(key) }
38
+ end
39
+
40
+ def include_cache
41
+ @include_cache ||= {}
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through recorder_spec.rb, which records calls through the recorder.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Recorder
8
+ module RowEmission
9
+ private
10
+
11
+ def emit((frame, uuid, step), loc, receiver, args, kwargs)
12
+ @logger.log_call(
13
+ stack_uuid: uuid, step: step, path: path_for(frame, loc), call_file: loc&.path, call_line: loc&.lineno,
14
+ arg_classes: arg_classes(args), receiver_id: receiver_id(receiver), arg_ids: arg_ids(args)
15
+ )
16
+ emit_kwargs(uuid, step, kwargs) if capture_kwargs?(kwargs)
17
+ end
18
+
19
+ def emit_answered((frame, uuid, step), args)
20
+ name = args&.first if frame.fetch(1) == :method_missing
21
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
22
+ @logger.log_answered(stack_uuid: uuid, step: step, name: name) if Symbol === name
23
+ # rubocop:enable Style/CaseEquality
24
+ end
25
+
26
+ def received(head, args, kwargs)
27
+ return [args, kwargs] unless head.fetch(HEAD_KEYWORDS_AS_HASH) && kwargs && !kwargs.empty?
28
+
29
+ [[*args, kwargs], nil]
30
+ end
31
+
32
+ def stamp_entry(uuid)
33
+ entry_point = State.current.entry_point
34
+ @seen.tree(entry_point)
35
+ @logger.log_entry(stack_uuid: uuid, entry_point: entry_point) if entry_point
36
+ log_inner_entry(uuid)
37
+ end
38
+
39
+ def log_inner_entry(uuid)
40
+ identity = State.current.inner_entry
41
+ @logger.log_inner_entry(stack_uuid: uuid, identity: identity) if identity
42
+ end
43
+
44
+ def emit_kwargs(uuid, step, kwargs)
45
+ @logger.log_kwargs(
46
+ stack_uuid: uuid, step: step,
47
+ kwarg_classes: kwargs.transform_values { |value| ClassName.of_value(value) },
48
+ kwarg_ids: (kwargs.transform_values { |value| ClassName.id_of(value) } if @tier >= 3)
49
+ )
50
+ end
51
+
52
+ def capture_kwargs?(kwargs)
53
+ @tier >= 2 && kwargs && !kwargs.empty?
54
+ end
55
+
56
+ def log_return((uuid, step), result)
57
+ @logger.log_return(
58
+ stack_uuid: uuid, step: step, return_class: ClassName.of_value(result), return_id: ClassName.id_of(result),
59
+ elements: element_capture(result)
60
+ )
61
+ end
62
+
63
+ MAX_ELEMENTS = 16
64
+ HEAD_KEYWORDS_AS_HASH = 4
65
+ HEAD_PROBED = 5
66
+ RECEIVER = 0
67
+ POSITIONAL = 1
68
+ KEYWORD = 2
69
+ RETURNED = 3
70
+
71
+ def emit_values((_, uuid, step), receiver, args, kwargs)
72
+ log_value(uuid, step, RECEIVER, 0, Fingerprint.identity(receiver))
73
+ log_positionals(uuid, step, args) if args
74
+ kwargs&.each { |name, value| log_keyword(uuid, step, name, value) }
75
+ [uuid, step, true]
76
+ end
77
+
78
+ def log_positionals(uuid, step, args)
79
+ args.first(MAX_ELEMENTS).each_with_index do |arg, index|
80
+ log_value(uuid, step, POSITIONAL, index, Fingerprint.of(arg))
81
+ end
82
+ end
83
+
84
+ def log_keyword(uuid, step, name, value)
85
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
86
+ return unless Symbol === name
87
+ # rubocop:enable Style/CaseEquality
88
+
89
+ how, print = Fingerprint.of(value)
90
+ @logger.log_value(stack_uuid: uuid, step: step, role: KEYWORD, key: name, how: how, print: print)
91
+ end
92
+
93
+ def log_returned(token, result)
94
+ return log_return(token, result) unless token.fetch(2, false)
95
+
96
+ log_return(token, result) if @tier >= 3
97
+ log_value(*token.first(2), RETURNED, 0, Fingerprint.of(result))
98
+ end
99
+
100
+ def log_value(uuid, step, role, key, (how, print))
101
+ @logger.log_value(stack_uuid: uuid, step: step, role: role, key: key, how: how, print: print)
102
+ end
103
+
104
+ def element_capture(result)
105
+ # rubocop:disable Style/CaseEquality -- host object: is_a? would be a message sent to it
106
+ return unless Array === result
107
+ # rubocop:enable Style/CaseEquality
108
+
109
+ [result.size, result.first(MAX_ELEMENTS).map do |element|
110
+ [ClassName.of_value(element), ClassName.id_of(element)]
111
+ end]
112
+ end
113
+
114
+ def log_unwind((uuid, step), error)
115
+ @logger.log_unwind(stack_uuid: uuid, step: step, exception_class: ClassName.of(error),
116
+ raised_at: RaiseSite.of(error), missing: RaiseSite.missing(error))
117
+ end
118
+
119
+ def arg_classes(args)
120
+ return unless @tier >= 2 && args
121
+
122
+ args.map { |arg| ClassName.of_value(arg) }
123
+ end
124
+
125
+ def receiver_id(receiver)
126
+ ClassName.id_of(receiver) if @tier >= 3
127
+ end
128
+
129
+ def arg_ids(args)
130
+ return unless @tier >= 3 && args
131
+
132
+ args.map { |arg| ClassName.id_of(arg) }
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through recorder_spec.rb, which records calls through the recorder.
4
+
5
+ require "runtime_analysis/failsafe"
6
+
7
+ module RuntimeAnalysis
8
+ module Trace
9
+ class Recorder
10
+ module SafeSteps
11
+ FRAMES = Failsafe.frames
12
+ FAILED = Failsafe.failed
13
+
14
+ # rubocop:disable Metrics/MethodLength -- hot path: split, it is a frame and a call more for every recorded call
15
+ def before(id, receiver, args, kwargs, depth)
16
+ state = floor = nil
17
+ token = Failsafe.masked do
18
+ floor = Failsafe.rescued { (state = State.current).depth }
19
+ settled(state, floor, begun(id, receiver, args, kwargs, depth)) if state
20
+ end
21
+ entered = true
22
+ token
23
+ ensure
24
+ put_back(state, floor) unless entered
25
+ end
26
+ # rubocop:enable Metrics/MethodLength
27
+
28
+ def finish(token, error, result)
29
+ safely { |state| finished(state, token, error, result) }
30
+ end
31
+
32
+ def outcome(token, error, result)
33
+ Failsafe.attempt { complete(token, error, result) }
34
+ end
35
+
36
+ def exit_tree
37
+ safely { |state| leave(state) }
38
+ end
39
+
40
+ private
41
+
42
+ def begun(id, receiver, args, kwargs, depth)
43
+ head = Failsafe.rescued { capture_head(id, receiver) }
44
+ return head unless head.instance_of?(Array)
45
+
46
+ caller_locations(depth + FRAMES + 2, 1) in [site]
47
+ Failsafe.rescued { emit_tail(head, receiver, args, kwargs, site) }
48
+ end
49
+
50
+ def settled(state, floor, answer)
51
+ FAILED.equal?(answer) ? put_back(state, floor) : answer
52
+ end
53
+
54
+ def finished(state, token, error, result)
55
+ complete(token, error, result)
56
+ ensure
57
+ leave(state, error)
58
+ end
59
+
60
+ def safely(&)
61
+ Failsafe.guard { leaving(State.current, &) }
62
+ end
63
+
64
+ def leaving(state)
65
+ floor = state.depth.pred.clamp(0..)
66
+ yield state
67
+ ensure
68
+ settle(state, floor)
69
+ end
70
+
71
+ def put_back(state, floor)
72
+ Failsafe.guard { settle(state, floor) }
73
+ nil
74
+ end
75
+
76
+ def settle(state, floor)
77
+ (state.depth - floor).times { leave(state) }
78
+ ensure
79
+ (state.depth - floor).times { state.leave }
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through recorder_spec.rb, which records calls through the recorder.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Recorder
8
+ module TreeSteps
9
+ NOT_RETURNED = Object.new.freeze
10
+
11
+ private
12
+
13
+ def leave(state, error = nil)
14
+ state.leave
15
+ @hold.tree_ended(state, error) if @hold && state.idle?
16
+ end
17
+
18
+ def enter(state, id, receiver, entry)
19
+ entered = state.enter(FrameKey.of(id, receiver, entry), gem: gem?(entry))
20
+ @hold&.tree_began(state) if entered.fetch(1) == 1
21
+ entered
22
+ end
23
+
24
+ def traced(token)
25
+ # rubocop:disable Style/SpecialGlobalVars -- host program: $ERROR_INFO needs English, which is not loaded into it
26
+ handled = $!
27
+ # rubocop:enable Style/SpecialGlobalVars
28
+ result = NOT_RETURNED
29
+ result = yield
30
+ ensure
31
+ closed(token, Unwinding.since(handled), result)
32
+ end
33
+
34
+ def closed(token, error, result)
35
+ safely do |state|
36
+ error || !NOT_RETURNED.equal?(result) ? finished(state, token, error, result) : leave(state)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through exception_untouched_spec.rb, trace_spec.rb and hold_spec.rb, which
4
+ # raise and throw through recorded calls.
5
+
6
+ module RuntimeAnalysis
7
+ module Trace
8
+ class Recorder
9
+ module Unwinding
10
+ SAME = ::BasicObject.instance_method(:equal?)
11
+
12
+ module_function
13
+
14
+ def since(handled)
15
+ # rubocop:disable Style/SpecialGlobalVars -- host program: $ERROR_INFO needs English, which is not loaded into it
16
+ error = $!
17
+ # rubocop:enable Style/SpecialGlobalVars
18
+ error unless SAME.bind_call(error, handled)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end