summaryrefslogtreecommitdiff
path: root/ext/-test-/stack/stack.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/-test-/stack/stack.c')
-rw-r--r--ext/-test-/stack/stack.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/ext/-test-/stack/stack.c b/ext/-test-/stack/stack.c
new file mode 100644
index 0000000000..f0e65e74b2
--- /dev/null
+++ b/ext/-test-/stack/stack.c
@@ -0,0 +1,35 @@
+#include "ruby.h"
+#include "internal/string.h"
+
+static VALUE
+stack_overflow(VALUE self)
+{
+ size_t i = 0;
+
+ while (1) {
+ // Allocate and touch memory to force actual stack usage:
+ volatile char *stack = alloca(1024);
+ stack[0] = (char)i;
+ stack[1023] = (char)i;
+ i++;
+ }
+
+ return Qnil;
+}
+
+static VALUE
+asan_p(VALUE klass)
+{
+#if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
+ return Qtrue;
+#else
+ return Qfalse;
+#endif
+}
+
+void
+Init_stack(VALUE klass)
+{
+ rb_define_singleton_method(rb_cThread, "stack_overflow", stack_overflow, 0);
+ rb_define_singleton_method(rb_cThread, "asan?", asan_p, 0);
+}