summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorKJ Tsanaktsidis <kj@kjtsanaktsidis.id.au>2023-11-12 13:34:43 +1100
committerKJ Tsanaktsidis <kj@kjtsanaktsidis.id.au>2024-01-19 09:55:12 +1100
commitcabdaebc701217049d8a6457c5100f23910f4423 (patch)
treec359115e5faa4f7461d32b252d79455ad2be349c /internal
parent807714447ef02c77bb0e17fe27d96ee2692264f8 (diff)
Make stack bounds detection work with ASAN
Where a local variable is used as part of the stack bounds detection, it has to actually be on the stack. ASAN can put local variable on "fake stacks", however, with addresses in different memory mappings. This completely destroys the stack bounds calculation, and can lead to e.g. things not getting GC marked on the machine stack or stackoverflow checks that always fail. The __asan_addr_is_in_fake_stack helper can be used to get the _real_ stack address of such variables, and thus perform the stack size calculation properly [Bug #20001]
Diffstat (limited to 'internal')
-rw-r--r--internal/sanitizers.h26
1 files changed, 26 insertions, 0 deletions
diff --git a/internal/sanitizers.h b/internal/sanitizers.h
index 7b7d166c74..6b2a131925 100644
--- a/internal/sanitizers.h
+++ b/internal/sanitizers.h
@@ -64,6 +64,8 @@
# define __asan_poison_memory_region(x, y)
# define __asan_unpoison_memory_region(x, y)
# define __asan_region_is_poisoned(x, y) 0
+# define __asan_get_current_fake_stack() NULL
+# define __asan_addr_is_in_fake_stack(fake_stack, slot, start, end) NULL
#endif
#if !__has_feature(memory_sanitizer)
@@ -183,4 +185,28 @@ asan_unpoison_object(VALUE obj, bool newobj_p)
asan_unpoison_memory_region(ptr, SIZEOF_VALUE, newobj_p);
}
+
+/*!
+ * Checks if the given pointer is on an ASAN fake stack. If so, it returns the
+ * address this variable has on the real frame; if not, it returns the origin
+ * address unmodified.
+ *
+ * n.b. - _dereferencing_ the returned address is meaningless and should not
+ * be done; even though ASAN reserves space for the variable in both the real and
+ * fake stacks, the _value_ of that variable is only in the fake stack.
+ *
+ * n.b. - this only works for addresses passed in from local variables on the same
+ * thread, because the ASAN fake stacks are threadlocal.
+ *
+ * \param[in] slot the address of some local variable
+ * \retval a pointer to something from that frame on the _real_ machine stack
+ */
+static inline void *
+asan_get_real_stack_addr(void* slot)
+{
+ VALUE *addr;
+ addr = __asan_addr_is_in_fake_stack(__asan_get_current_fake_stack(), slot, NULL, NULL);
+ return addr ? addr : slot;
+}
+
#endif /* INTERNAL_SANITIZERS_H */