Make js_get_stack_pointer more portable (#793)

By using code from LLVM.

3f79057338/clang/lib/Basic/Stack.cpp (L23-L38)
This commit is contained in:
Ivan Komissarov 2025-01-08 21:56:43 +03:00 committed by GitHub
parent 291eb9c5db
commit 0fb7f4e208
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 5 deletions

View file

@ -62,10 +62,6 @@ extern "C" {
# define __maybe_unused
# define __attribute__(x)
# define __attribute(x)
# include <intrin.h>
static void *__builtin_frame_address(unsigned int level) {
return (void *)((char*)_AddressOfReturnAddress() - sizeof(int *) - level * sizeof(int *));
}
#else
# define likely(x) __builtin_expect(!!(x), 1)
# define unlikely(x) __builtin_expect(!!(x), 0)

View file

@ -33,6 +33,7 @@
#if !defined(_MSC_VER)
#include <sys/time.h>
#if defined(_WIN32)
#include <intrin.h>
#include <timezoneapi.h>
#endif
#endif
@ -1774,10 +1775,23 @@ static int init_class_range(JSRuntime *rt, JSClassShortDef const *tab,
return 0;
}
/* Note: OS and CPU dependent */
/* Uses code from LLVM project. */
static inline uintptr_t js_get_stack_pointer(void)
{
#if defined(__clang__) || defined(__GNUC__)
return (uintptr_t)__builtin_frame_address(0);
#elif defined(_MSC_VER)
return (uintptr_t)_AddressOfReturnAddress();
#else
char CharOnStack = 0;
// The volatile store here is intended to escape the local variable, to
// prevent the compiler from optimizing CharOnStack into anything other
// than a char on the stack.
//
// Tested on: MSVC 2015 - 2019, GCC 4.9 - 9, Clang 3.2 - 9, ICC 13 - 19.
char *volatile Ptr = &CharOnStack;
return (uintptr_t) Ptr;
#endif
}
static inline BOOL js_check_stack_overflow(JSRuntime *rt, size_t alloca_size)