contrast-agent 5.1.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. checksums.yaml +4 -4
  2. data/ext/cs__assess_array/cs__assess_array.c +7 -0
  3. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +19 -5
  4. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  5. data/ext/cs__assess_hash/cs__assess_hash.c +3 -4
  6. data/ext/cs__assess_kernel/cs__assess_kernel.c +7 -5
  7. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +26 -12
  8. data/ext/cs__assess_module/cs__assess_module.c +7 -7
  9. data/ext/cs__assess_string/cs__assess_string.c +13 -1
  10. data/ext/cs__common/cs__common.c +16 -11
  11. data/ext/cs__common/cs__common.h +1 -0
  12. data/ext/cs__contrast_patch/cs__contrast_patch.c +100 -64
  13. data/ext/cs__contrast_patch/cs__contrast_patch.h +2 -0
  14. data/ext/cs__os_information/cs__os_information.c +13 -10
  15. data/ext/cs__scope/cs__scope.c +796 -0
  16. data/ext/cs__scope/cs__scope.h +88 -0
  17. data/ext/cs__scope/extconf.rb +5 -0
  18. data/lib/contrast/agent/assess/contrast_event.rb +20 -13
  19. data/lib/contrast/agent/assess/contrast_object.rb +4 -1
  20. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -0
  21. data/lib/contrast/agent/assess/policy/policy_node.rb +50 -27
  22. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  23. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  24. data/lib/contrast/agent/assess/policy/propagation_method.rb +47 -13
  25. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -5
  26. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +118 -0
  27. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  28. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +2 -0
  29. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  30. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  31. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  32. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  33. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  34. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  35. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  36. data/lib/contrast/agent/assess/policy/trigger_method.rb +6 -1
  37. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  38. data/lib/contrast/agent/assess/rule/response/{autocomplete_rule.rb → auto_complete_rule.rb} +4 -3
  39. data/lib/contrast/agent/assess/rule/response/base_rule.rb +12 -79
  40. data/lib/contrast/agent/assess/rule/response/body_rule.rb +109 -0
  41. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +157 -0
  42. data/lib/contrast/agent/assess/rule/response/click_jacking_header_rule.rb +26 -0
  43. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +14 -15
  44. data/lib/contrast/agent/assess/rule/response/csp_header_missing_rule.rb +5 -25
  45. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +29 -0
  46. data/lib/contrast/agent/assess/rule/response/header_rule.rb +70 -0
  47. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +12 -36
  48. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +2 -1
  49. data/lib/contrast/agent/assess/rule/response/x_content_type_header_rule.rb +26 -0
  50. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +35 -0
  51. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  52. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  53. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  54. data/lib/contrast/agent/middleware.rb +4 -1
  55. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +1 -3
  56. data/lib/contrast/agent/patching/policy/patch.rb +2 -6
  57. data/lib/contrast/agent/patching/policy/patcher.rb +4 -4
  58. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -2
  59. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  60. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +147 -0
  61. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -1
  62. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -2
  63. data/lib/contrast/agent/protect/rule/base.rb +61 -2
  64. data/lib/contrast/agent/protect/rule/base_service.rb +12 -1
  65. data/lib/contrast/agent/protect/rule/cmd_injection.rb +15 -0
  66. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  67. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  68. data/lib/contrast/agent/protect/rule/deserialization.rb +6 -0
  69. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  70. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +13 -1
  71. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  72. data/lib/contrast/agent/protect/rule/no_sqli.rb +28 -0
  73. data/lib/contrast/agent/protect/rule/path_traversal.rb +1 -0
  74. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +88 -0
  75. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +118 -0
  76. data/lib/contrast/agent/protect/rule/sqli.rb +33 -0
  77. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  78. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  79. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  80. data/lib/contrast/agent/protect/rule/xxe.rb +4 -0
  81. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  82. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  83. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  84. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  85. data/lib/contrast/agent/reporting/input_analysis/input_analysis.rb +44 -0
  86. data/lib/contrast/agent/reporting/input_analysis/input_analysis_result.rb +115 -0
  87. data/lib/contrast/agent/reporting/input_analysis/input_type.rb +44 -0
  88. data/lib/contrast/agent/reporting/input_analysis/score_level.rb +21 -0
  89. data/lib/contrast/agent/reporting/masker/masker.rb +246 -0
  90. data/lib/contrast/agent/reporting/masker/masker_utils.rb +58 -0
  91. data/lib/contrast/agent/reporting/report.rb +3 -0
  92. data/lib/contrast/agent/reporting/reporter.rb +31 -12
  93. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +30 -0
  94. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +7 -3
  95. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +40 -0
  96. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  97. data/lib/contrast/agent/reporting/reporting_events/finding.rb +69 -36
  98. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +88 -59
  99. data/lib/contrast/agent/reporting/reporting_events/{finding_object.rb → finding_event_object.rb} +24 -20
  100. data/lib/contrast/agent/reporting/reporting_events/finding_event_parent_object.rb +39 -0
  101. data/lib/contrast/agent/reporting/reporting_events/finding_event_property.rb +40 -0
  102. data/lib/contrast/agent/reporting/reporting_events/{finding_signature.rb → finding_event_signature.rb} +29 -24
  103. data/lib/contrast/agent/reporting/reporting_events/finding_event_source.rb +12 -8
  104. data/lib/contrast/agent/reporting/reporting_events/{finding_stack.rb → finding_event_stack.rb} +23 -19
  105. data/lib/contrast/agent/reporting/reporting_events/{finding_taint_range.rb → finding_event_taint_range.rb} +17 -15
  106. data/lib/contrast/agent/reporting/reporting_events/finding_request.rb +26 -53
  107. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  108. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +9 -9
  109. data/lib/contrast/agent/reporting/reporting_events/poll.rb +29 -0
  110. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +2 -1
  111. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +6 -4
  112. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  113. data/lib/contrast/agent/reporting/reporting_events/route_discovery.rb +1 -0
  114. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +1 -1
  115. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +10 -3
  116. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +0 -1
  117. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +17 -5
  118. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +54 -45
  119. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  120. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +97 -0
  121. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +69 -7
  122. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  123. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +123 -85
  124. data/lib/contrast/agent/reporting/settings/application_settings.rb +9 -0
  125. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +5 -33
  126. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +1 -1
  127. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  128. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  129. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  130. data/lib/contrast/agent/request_context.rb +7 -2
  131. data/lib/contrast/agent/request_context_extend.rb +85 -21
  132. data/lib/contrast/agent/request_handler.rb +4 -0
  133. data/lib/contrast/agent/scope.rb +102 -107
  134. data/lib/contrast/agent/service_heartbeat.rb +45 -2
  135. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +51 -0
  136. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +36 -0
  137. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +97 -0
  138. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +65 -0
  139. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +47 -0
  140. data/lib/contrast/agent/{metric_telemetry_event.rb → telemetry/events/metric_telemetry_event.rb} +1 -1
  141. data/lib/contrast/agent/{startup_metrics_telemetry_event.rb → telemetry/events/startup_metrics_telemetry_event.rb} +3 -3
  142. data/lib/contrast/agent/{telemetry_event.rb → telemetry/events/telemetry_event.rb} +1 -1
  143. data/lib/contrast/agent/{telemetry.rb → telemetry/telemetry.rb} +32 -19
  144. data/lib/contrast/agent/thread_watcher.rb +1 -1
  145. data/lib/contrast/agent/version.rb +1 -1
  146. data/lib/contrast/agent.rb +3 -0
  147. data/lib/contrast/api/communication/speedracer.rb +1 -1
  148. data/lib/contrast/api/decorators/address.rb +1 -1
  149. data/lib/contrast/api/decorators/bot_blocker.rb +37 -0
  150. data/lib/contrast/api/decorators/ip_denylist.rb +37 -0
  151. data/lib/contrast/api/decorators/rasp_rule_sample.rb +29 -0
  152. data/lib/contrast/api/decorators/response_type.rb +30 -0
  153. data/lib/contrast/api/decorators/user_input.rb +11 -1
  154. data/lib/contrast/api/decorators/virtual_patch.rb +34 -0
  155. data/lib/contrast/api/decorators.rb +1 -0
  156. data/lib/contrast/components/app_context.rb +0 -4
  157. data/lib/contrast/components/assess.rb +14 -0
  158. data/lib/contrast/components/logger.rb +5 -0
  159. data/lib/contrast/components/protect.rb +6 -4
  160. data/lib/contrast/components/sampling.rb +7 -11
  161. data/lib/contrast/components/scope.rb +98 -91
  162. data/lib/contrast/components/settings.rb +106 -8
  163. data/lib/contrast/config/agent_configuration.rb +41 -12
  164. data/lib/contrast/config/api_configuration.rb +37 -12
  165. data/lib/contrast/config/api_proxy_configuration.rb +12 -3
  166. data/lib/contrast/config/application_configuration.rb +38 -14
  167. data/lib/contrast/config/assess_configuration.rb +47 -12
  168. data/lib/contrast/config/assess_rules_configuration.rb +15 -3
  169. data/lib/contrast/config/base_configuration.rb +18 -50
  170. data/lib/contrast/config/certification_configuration.rb +17 -3
  171. data/lib/contrast/config/exception_configuration.rb +14 -3
  172. data/lib/contrast/config/heap_dump_configuration.rb +43 -17
  173. data/lib/contrast/config/inventory_configuration.rb +17 -3
  174. data/lib/contrast/config/logger_configuration.rb +10 -3
  175. data/lib/contrast/config/protect_configuration.rb +17 -7
  176. data/lib/contrast/config/protect_rule_configuration.rb +17 -8
  177. data/lib/contrast/config/protect_rules_configuration.rb +115 -17
  178. data/lib/contrast/config/request_audit_configuration.rb +26 -3
  179. data/lib/contrast/config/root_configuration.rb +52 -12
  180. data/lib/contrast/config/ruby_configuration.rb +60 -22
  181. data/lib/contrast/config/sampling_configuration.rb +19 -9
  182. data/lib/contrast/config/server_configuration.rb +19 -10
  183. data/lib/contrast/config/service_configuration.rb +27 -11
  184. data/lib/contrast/configuration.rb +5 -3
  185. data/lib/contrast/extension/assess/string.rb +20 -1
  186. data/lib/contrast/extension/module.rb +0 -1
  187. data/lib/contrast/framework/manager.rb +2 -2
  188. data/lib/contrast/logger/application.rb +1 -1
  189. data/lib/contrast/logger/cef_log.rb +151 -0
  190. data/lib/contrast/tasks/config.rb +90 -3
  191. data/lib/contrast/utils/assess/object_store.rb +36 -0
  192. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  193. data/lib/contrast/utils/class_util.rb +3 -12
  194. data/lib/contrast/utils/hash_digest.rb +14 -6
  195. data/lib/contrast/utils/input_classification.rb +73 -0
  196. data/lib/contrast/utils/log_utils.rb +114 -0
  197. data/lib/contrast/utils/middleware_utils.rb +9 -9
  198. data/lib/contrast/utils/net_http_base.rb +13 -10
  199. data/lib/contrast/utils/object_share.rb +2 -1
  200. data/lib/contrast/utils/os.rb +0 -5
  201. data/lib/contrast/utils/patching/policy/patch_utils.rb +4 -9
  202. data/lib/contrast/utils/response_utils.rb +18 -33
  203. data/lib/contrast/utils/telemetry.rb +1 -1
  204. data/lib/contrast/utils/telemetry_client.rb +1 -1
  205. data/lib/contrast/utils/telemetry_identifier.rb +1 -1
  206. data/lib/contrast.rb +4 -3
  207. data/resources/assess/policy.json +98 -0
  208. data/resources/deadzone/policy.json +0 -86
  209. data/ruby-agent.gemspec +9 -8
  210. data/service_executables/VERSION +1 -1
  211. data/service_executables/linux/contrast-service +0 -0
  212. data/service_executables/mac/contrast-service +0 -0
  213. metadata +103 -38
  214. data/lib/contrast/agent/assess/rule/response/cachecontrol_rule.rb +0 -184
  215. data/lib/contrast/agent/assess/rule/response/clickjacking_rule.rb +0 -66
  216. data/lib/contrast/agent/assess/rule/response/x_content_type_rule.rb +0 -52
  217. data/lib/contrast/agent/assess/rule/response/x_xss_protection_rule.rb +0 -53
  218. data/lib/contrast/extension/kernel.rb +0 -54
@@ -3,6 +3,7 @@
3
3
 
4
4
  #include "cs__contrast_patch.h"
5
5
  #include "../cs__common/cs__common.h"
6
+ #include "../cs__scope/cs__scope.h"
6
7
  #include <ruby.h>
7
8
 
8
9
  VALUE build_preshift(const VALUE method_policy, const VALUE object,
@@ -34,25 +35,29 @@ VALUE contrast_patch_call_original(const VALUE *args) {
34
35
  method = args[3];
35
36
  method_id = SYM2ID(method);
36
37
 
37
- /* It looks like we can find the last Ruby block given so long as we don't
38
- * change Ruby method scope (always call this function from C, not Ruby),
39
- * which is the point of this C call.
40
- */
41
- /* Ruby >= 2.7 */
42
- #ifdef RB_PASS_CALLED_KEYWORDS
43
- if (rb_block_given_p()) {
44
- return rb_funcall_with_block_kw(object, method_id, argc, params, rb_block_proc(), RB_PASS_CALLED_KEYWORDS);
45
- } else {
46
- return rb_funcallv_kw(object, method_id, argc, params, RB_PASS_CALLED_KEYWORDS);
47
- }
48
- /* Ruby < 2.7 */
49
- #else
50
- if (rb_block_given_p()) {
51
- return rb_funcall_with_block(object, method_id, argc, params, rb_block_proc());
52
- } else {
53
- return rb_funcall2(object, method_id, argc, params);
54
- }
55
- #endif
38
+ /* It looks like we can find the last Ruby block given so long as we don't
39
+ * change Ruby method scope (always call this function from C, not Ruby),
40
+ * which is the point of this C call.
41
+ */
42
+ /* Ruby >= 2.7 */
43
+ #ifdef RB_PASS_CALLED_KEYWORDS
44
+ if (rb_block_given_p()) {
45
+ return rb_funcall_with_block_kw(object, method_id, argc, params,
46
+ rb_block_proc(),
47
+ RB_PASS_CALLED_KEYWORDS);
48
+ } else {
49
+ return rb_funcallv_kw(object, method_id, argc, params,
50
+ RB_PASS_CALLED_KEYWORDS);
51
+ }
52
+ /* Ruby < 2.7 */
53
+ #else
54
+ if (rb_block_given_p()) {
55
+ return rb_funcall_with_block(object, method_id, argc, params,
56
+ rb_block_proc());
57
+ } else {
58
+ return rb_funcall2(object, method_id, argc, params);
59
+ }
60
+ #endif
56
61
  }
57
62
 
58
63
  VALUE contrast_call_pre_patch(const VALUE method_policy, const VALUE method,
@@ -84,15 +89,22 @@ VALUE contrast_call_post_patch(const VALUE method_policy, const VALUE preshift,
84
89
  method_policy, preshift, object, ret, send, block);
85
90
  }
86
91
 
87
- VALUE contrast_patch_call_rescue(const VALUE *args) {
88
- int argc;
89
- VALUE exception, object, preshift, method_policy, method;
90
- VALUE *argv;
91
-
92
+ /* wrap rb_ensure so we can rescue an exception */
93
+ VALUE rescue_func(VALUE arg1) {
94
+ VALUE exception;
92
95
  /* rb_errinfo() gives the value of $!, the exception that
93
96
  * triggered a rescue block.
94
97
  */
95
98
  exception = rb_errinfo();
99
+ rb_exc_raise(exception);
100
+
101
+ return Qnil;
102
+ }
103
+
104
+ VALUE contrast_patch_call_ensure(const VALUE *args) {
105
+ int argc;
106
+ VALUE object, preshift, method_policy, method;
107
+ VALUE *argv;
96
108
 
97
109
  object = args[0];
98
110
  method = args[1];
@@ -103,12 +115,20 @@ VALUE contrast_patch_call_rescue(const VALUE *args) {
103
115
 
104
116
  contrast_call_post_patch(method_policy, preshift, object, Qnil, argc, argv);
105
117
 
106
- /* reraise the exception that got us here */
107
- rb_exc_raise(exception);
108
-
109
118
  return Qnil;
110
119
  }
111
120
 
121
+ VALUE ensure_wrapper(const VALUE *args) {
122
+ VALUE original_method, original_args, ensure_args;
123
+
124
+ original_method = args[0];
125
+ original_args = (VALUE)args[1];
126
+ ensure_args = (VALUE)args[2];
127
+
128
+ return rb_ensure(original_method, original_args, contrast_patch_call_ensure,
129
+ (VALUE)ensure_args);
130
+ }
131
+
112
132
  VALUE contrast_call_super(const VALUE *args) {
113
133
  int argc;
114
134
  VALUE *argv;
@@ -119,10 +139,12 @@ VALUE contrast_call_super(const VALUE *args) {
119
139
  }
120
140
 
121
141
  VALUE contrast_run_patches(const VALUE *wrapped_args) {
122
- VALUE impl, method, method_policy, object, original_args, original_ret, preshift, transformed_ret;
142
+ VALUE impl, method, method_policy, object, original_args, original_ret,
143
+ preshift, transformed_ret;
123
144
  int argc;
124
145
  VALUE *argv;
125
- VALUE rescue_args[6];
146
+ VALUE ensure_args[6];
147
+ VALUE rescue_wrapper_args[3];
126
148
 
127
149
  impl = wrapped_args[0];
128
150
  original_args = wrapped_args[1];
@@ -132,18 +154,22 @@ VALUE contrast_run_patches(const VALUE *wrapped_args) {
132
154
  argc = NUM2INT(wrapped_args[5]);
133
155
  argv = (VALUE *)wrapped_args[6];
134
156
 
135
- rescue_args[0] = object;
136
- rescue_args[1] = method;
137
- rescue_args[2] = INT2NUM(argc);
138
- rescue_args[3] = (VALUE)argv;
139
- rescue_args[4] = method_policy;
157
+ rescue_wrapper_args[0] = contrast_patch_call_original;
158
+ rescue_wrapper_args[1] = original_args;
159
+ rescue_wrapper_args[2] = ensure_args;
160
+
161
+ ensure_args[0] = object;
162
+ ensure_args[1] = method;
163
+ ensure_args[2] = INT2NUM(argc);
164
+ ensure_args[3] = (VALUE)argv;
165
+ ensure_args[4] = method_policy;
140
166
 
141
167
  /* Tracking, triggering, and propagation here. */
142
168
  contrast_call_pre_patch(method_policy, method, object, argc, argv, Qnil);
143
169
 
144
170
  /* Capture pre-call state */
145
171
  preshift = build_preshift(method_policy, object, argc, argv);
146
- rescue_args[5] = preshift;
172
+ ensure_args[5] = preshift;
147
173
 
148
174
  /* We wrap a call to the original method with a rescue block, and we use
149
175
  * rb_rescue2 to capture all Exception-inheriting exceptions (and if your
@@ -178,15 +204,14 @@ VALUE contrast_run_patches(const VALUE *wrapped_args) {
178
204
  switch (impl) {
179
205
  case IMPL_ALIAS_INSTANCE:
180
206
  case IMPL_ALIAS_SINGLETON:
181
- original_ret = rb_rescue2(contrast_patch_call_original, original_args,
182
- contrast_patch_call_rescue,
183
- (VALUE)rescue_args, rb_eException, 0);
207
+ original_ret =
208
+ rb_rescue(ensure_wrapper, rescue_wrapper_args, rescue_func, Qnil);
184
209
  break;
185
210
  case IMPL_PREPEND_INSTANCE:
186
211
  case IMPL_PREPEND_SINGLETON:
187
- original_ret = rb_rescue2(contrast_call_super, original_args,
188
- contrast_patch_call_rescue,
189
- (VALUE)rescue_args, rb_eException, 0);
212
+ rescue_wrapper_args[0] = contrast_call_super;
213
+ original_ret =
214
+ rb_rescue(ensure_wrapper, rescue_wrapper_args, rescue_func, Qnil);
190
215
  break;
191
216
  };
192
217
 
@@ -214,8 +239,10 @@ VALUE contrast_run_patches(const VALUE *wrapped_args) {
214
239
 
215
240
  VALUE contrast_ensure_function(const VALUE method_policy) {
216
241
  /* exit scope */
217
- rb_funcall(contrast_patcher(), rb_sym_exit_method_scope, 1, method_policy);
218
- rb_funcall(contrast_patcher(), rb_sym_exit_scope, 0);
242
+ VALUE scopes = rb_funcall(method_policy, rb_sym_scopes_to_exit, 0);
243
+
244
+ inst_methods_exit_method_scope(contrast_patcher(), scopes);
245
+ inst_methods_exit_cntr_scope(contrast_patcher(), 0);
219
246
 
220
247
  return Qnil;
221
248
  }
@@ -235,10 +262,10 @@ VALUE contrast_patch_dispatch(const int argc, const VALUE *argv,
235
262
  * which is unnecessary, or run Contrast analysis on Contrast code,
236
263
  * which will never terminate.
237
264
  */
238
- nested_scope = RTEST(rb_funcall(contrast_patcher(), rb_sym_in_scope, 0));
265
+ nested_scope = inst_methods_in_cntr_scope(contrast_patcher(), 0);
239
266
 
240
267
  /* enter scope */
241
- rb_funcall(contrast_patcher(), rb_sym_enter_scope, 0);
268
+ inst_methods_enter_cntr_scope(contrast_patcher(), 0);
242
269
 
243
270
  /* Get the name of the calling method */
244
271
  method = rb_funcall(object, rb_sym_method, 0);
@@ -253,9 +280,9 @@ VALUE contrast_patch_dispatch(const int argc, const VALUE *argv,
253
280
  rb_funcall(patch_status, rb_sym_info_for, 3, object, method, Qtrue);
254
281
  break;
255
282
  case IMPL_PREPEND_SINGLETON:
256
- known =
257
- rb_funcall(patch_status, rb_sym_info_for, 3, object, method, Qfalse);
258
- break;
283
+ known = rb_funcall(patch_status, rb_sym_info_for, 3, object, method,
284
+ Qfalse);
285
+ break;
259
286
  case IMPL_ALIAS_SINGLETON:
260
287
  known = rb_funcall(patch_status, rb_sym_info_for, 3, object, method,
261
288
  Qfalse);
@@ -270,7 +297,7 @@ VALUE contrast_patch_dispatch(const int argc, const VALUE *argv,
270
297
  }
271
298
 
272
299
  /* Check conditions for not doing Contrast analysis */
273
- if (nested_scope) {
300
+ if (nested_scope == Qtrue) {
274
301
  /* if we were in scope */
275
302
  do_contrast = 0;
276
303
  } else if (!RTEST(known)) {
@@ -299,7 +326,9 @@ VALUE contrast_patch_dispatch(const int argc, const VALUE *argv,
299
326
  }
300
327
 
301
328
  /* Enter any scopes specific to method policy */
302
- rb_funcall(contrast_patcher(), rb_sym_enter_method_scope, 1, method_policy);
329
+ VALUE scopes = rb_funcall(method_policy, rb_sym_scopes_to_enter, 0);
330
+
331
+ inst_methods_enter_method_scope(contrast_patcher(), scopes);
303
332
 
304
333
  /* If we're not doing Contrast analysis, exit scope and treat as normal. */
305
334
  if (!do_contrast) {
@@ -316,14 +345,14 @@ VALUE contrast_patch_dispatch(const int argc, const VALUE *argv,
316
345
  wrapped_args[5] = INT2NUM(argc);
317
346
  wrapped_args[6] = (VALUE)argv;
318
347
 
319
- return rb_ensure(contrast_run_patches, (VALUE)wrapped_args, contrast_ensure_function, method_policy);
348
+ return rb_ensure(contrast_run_patches, (VALUE)wrapped_args,
349
+ contrast_ensure_function, method_policy);
320
350
 
321
351
  call_original:
322
352
 
323
353
  /* exit scope */
324
354
  contrast_ensure_function(method_policy);
325
355
 
326
-
327
356
  switch (impl) {
328
357
  case IMPL_ALIAS_INSTANCE:
329
358
  case IMPL_ALIAS_SINGLETON:
@@ -345,19 +374,22 @@ VALUE contrast_alias_singleton_patch(const int argc, const VALUE *argv,
345
374
  }
346
375
 
347
376
  VALUE contrast_prepend_instance_patch(const int argc, const VALUE *argv,
348
- const VALUE object) {
377
+ const VALUE object) {
349
378
  return contrast_patch_dispatch(argc, argv, IMPL_PREPEND_INSTANCE, object);
350
379
  }
351
380
 
352
381
  VALUE contrast_prepend_singleton_patch(const int argc, const VALUE *argv,
353
- const VALUE object) {
382
+ const VALUE object) {
354
383
  return contrast_patch_dispatch(argc, argv, IMPL_PREPEND_SINGLETON, object);
355
384
  }
356
385
 
357
- VALUE contrast_patch_define_method(const VALUE self, const VALUE clazz, const VALUE method_policy,
386
+ VALUE contrast_patch_define_method(const VALUE self, const VALUE clazz,
387
+ const VALUE method_policy,
358
388
  const VALUE cs_method) {
359
- const VALUE original_method_name = rb_funcall(method_policy, rb_sym_method_name, 0);
360
- const VALUE is_instance_method = rb_funcall(method_policy, rb_sym_instance_method, 0);
389
+ const VALUE original_method_name =
390
+ rb_funcall(method_policy, rb_sym_method_name, 0);
391
+ const VALUE is_instance_method =
392
+ rb_funcall(method_policy, rb_sym_instance_method, 0);
361
393
  char *cStr;
362
394
  VALUE str;
363
395
  rb_funcall(patch_status, rb_sym_set_info_for, 5, clazz,
@@ -423,8 +455,8 @@ VALUE contrast_patch_prepend(const VALUE self, const VALUE originalModule,
423
455
  const VALUE is_instance_method =
424
456
  rb_funcall(method_policy, rb_sym_instance_method, 0);
425
457
 
426
- // Set the value for instance or singleton method
427
- if (RTEST(is_instance_method)){
458
+ // Set the value for instance or singleton method
459
+ if (RTEST(is_instance_method)) {
428
460
  rb_funcall(patch_status, rb_sym_set_info_for, 5, originalModule,
429
461
  original_method_name, method_policy, instance, Qnil);
430
462
 
@@ -441,17 +473,19 @@ VALUE contrast_patch_prepend(const VALUE self, const VALUE originalModule,
441
473
  rb_define_private_method(module, cMethodName,
442
474
  contrast_prepend_instance_patch, -1);
443
475
  } else {
444
- rb_define_method(module, cMethodName, contrast_prepend_instance_patch, -1);
476
+ rb_define_method(module, cMethodName,
477
+ contrast_prepend_instance_patch, -1);
445
478
  }
446
479
  } else {
447
- rb_define_singleton_method(module, cMethodName, contrast_prepend_singleton_patch,
448
- -1);
480
+ rb_define_singleton_method(module, cMethodName,
481
+ contrast_prepend_singleton_patch, -1);
449
482
  }
450
483
  rb_prepend_module(originalModule, module);
451
484
 
452
485
  if (rb_ver_below_three()) {
453
486
  VALUE module_at;
454
- VALUE rb_incl_in_mod_ary = rb_funcall(originalModule, rb_intern("included_in"), 0);
487
+ VALUE rb_incl_in_mod_ary =
488
+ rb_funcall(originalModule, rb_intern("included_in"), 0);
455
489
  if (RB_TYPE_P(rb_incl_in_mod_ary, T_ARRAY)) {
456
490
  int i = 0;
457
491
  int size = rb_funcall(rb_incl_in_mod_ary, rb_intern("length"), 0);
@@ -462,7 +496,7 @@ VALUE contrast_patch_prepend(const VALUE self, const VALUE originalModule,
462
496
  }
463
497
  }
464
498
  }
465
- }
499
+ }
466
500
  return Qtrue;
467
501
  }
468
502
 
@@ -488,6 +522,8 @@ void Init_cs__contrast_patch(void) {
488
522
 
489
523
  rb_sym_enter_method_scope = rb_intern("enter_method_scope!");
490
524
  rb_sym_exit_method_scope = rb_intern("exit_method_scope!");
525
+ rb_sym_scopes_to_enter = rb_intern("scopes_to_enter");
526
+ rb_sym_scopes_to_exit = rb_intern("scopes_to_exit");
491
527
 
492
528
  rb_define_module_function(contrast_patcher(), "contrast_define_method",
493
529
  contrast_patch_define_method, 3);
@@ -18,6 +18,8 @@ static VALUE rb_sym_cs_to_s;
18
18
 
19
19
  static VALUE rb_sym_enter_method_scope;
20
20
  static VALUE rb_sym_exit_method_scope;
21
+ static VALUE rb_sym_scopes_to_enter;
22
+ static VALUE rb_sym_scopes_to_exit;
21
23
 
22
24
  static VALUE rb_sym_build_method_name;
23
25
  static VALUE rb_sym_info_for;
@@ -8,24 +8,27 @@
8
8
 
9
9
  VALUE contrast, utils, os;
10
10
 
11
- VALUE contrast_get_system_information()
12
- {
11
+ VALUE contrast_get_system_information() {
13
12
  struct utsname uname_pointer;
14
13
 
15
- uname (&uname_pointer);
14
+ uname(&uname_pointer);
16
15
 
17
16
  VALUE rb_data_hash = rb_hash_new();
18
- rb_hash_aset(rb_data_hash, rb_str_new2("os_type"), rb_str_new2(uname_pointer.sysname));
19
- rb_hash_aset(rb_data_hash, rb_str_new2("os_version"), rb_str_new2(uname_pointer.release));
20
- rb_hash_aset(rb_data_hash, rb_str_new2("os_complete_version"), rb_str_new2(uname_pointer.version));
21
- rb_hash_aset(rb_data_hash, rb_str_new2("os_arch"), rb_str_new2(uname_pointer.machine));
17
+ rb_hash_aset(rb_data_hash, rb_str_new2("os_type"),
18
+ rb_str_new2(uname_pointer.sysname));
19
+ rb_hash_aset(rb_data_hash, rb_str_new2("os_version"),
20
+ rb_str_new2(uname_pointer.release));
21
+ rb_hash_aset(rb_data_hash, rb_str_new2("os_complete_version"),
22
+ rb_str_new2(uname_pointer.version));
23
+ rb_hash_aset(rb_data_hash, rb_str_new2("os_arch"),
24
+ rb_str_new2(uname_pointer.machine));
22
25
  return rb_data_hash;
23
26
  }
24
27
 
25
- void Init_cs__os_information(void)
26
- {
28
+ void Init_cs__os_information(void) {
27
29
  contrast = rb_define_module("Contrast");
28
30
  utils = rb_define_module_under(contrast, "Utils");
29
31
  os = rb_define_module_under(utils, "OS");
30
- rb_define_module_function(os, "get_system_information", contrast_get_system_information, 0);
32
+ rb_define_module_function(os, "get_system_information",
33
+ contrast_get_system_information, 0);
31
34
  }