23#include "ruby/internal/config.h"
41#if defined(_MSC_VER) && defined(_WIN64)
44# pragma intrinsic(_umul128)
47# pragma intrinsic(__umulh)
59#include "ruby/internal/cast.h"
64#include "ruby/internal/stdckdint.h"
66#include "ruby/backward/2/limits.h"
76#elif RBIMPL_HAS_BUILTIN(__builtin_alloca)
77# define alloca __builtin_alloca
80#elif defined(__cplusplus)
81extern "C" void *alloca(
size_t);
88#if defined(__DOXYGEN__)
96typedef uint128_t DSIZE_T;
97#elif defined(HAVE_INT128_T) && SIZEOF_SIZE_T <= 8
98# define DSIZE_T uint128_t
99#elif SIZEOF_SIZE_T * 2 <= SIZEOF_LONG_LONG
100# define DSIZE_T unsigned LONG_LONG
111# define RUBY_ALLOCV_LIMIT 0
113# define RUBY_ALLOCV_LIMIT 1024
167#define RB_GC_GUARD(v) \
169 volatile VALUE *rb_gc_guarded_ptr = &(v); \
170 __asm__("" : : "m"(rb_gc_guarded_ptr)); \
173#elif defined _MSC_VER
174#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr(&(v)))
176#define HAVE_RB_GC_GUARDED_PTR_VAL 1
177#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr_val(&(v),(v)))
199#define RB_ALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xmalloc2((n), sizeof(type)))
213#define RB_ALLOC(type) RBIMPL_CAST((type *)ruby_xmalloc(sizeof(type)))
234#define RB_ZALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xcalloc((n), sizeof(type)))
249#define RB_ZALLOC(type) (RB_ZALLOC_N(type, 1))
282#define RB_REALLOC_N(var,type,n) \
283 ((var) = RBIMPL_CAST((type *)ruby_xrealloc2((void *)(var), (n), sizeof(type))))
292#define ALLOCA_N(type,n) \
293 RBIMPL_CAST((type *)alloca(rbimpl_size_mul_or_raise(sizeof(type), (n))))
304#define RB_ALLOCV(v, n) \
305 ((n) < RUBY_ALLOCV_LIMIT ? \
306 ((v) = 0, alloca(n)) : \
307 rb_alloc_tmp_buffer(&(v), (n)))
336#define RB_ALLOCV_N(type, v, n) \
337 RBIMPL_CAST((type *) \
338 (((size_t)(n) < RUBY_ALLOCV_LIMIT / sizeof(type)) ? \
339 ((v) = 0, alloca((n) * sizeof(type))) : \
340 rb_alloc_tmp_buffer2(&(v), (n), sizeof(type))))
349#define RB_ALLOCV_END(v) rb_free_tmp_buffer(&(v))
360#define MEMZERO(p,type,n) memset((p), 0, rbimpl_size_mul_or_raise(sizeof(type), (n)))
372#define MEMCPY(p1,p2,type,n) ruby_nonempty_memcpy((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
384#define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
397#define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
399#define ALLOC_N RB_ALLOC_N
400#define ALLOC RB_ALLOC
401#define ZALLOC_N RB_ZALLOC_N
402#define ZALLOC RB_ZALLOC
403#define REALLOC_N RB_REALLOC_N
404#define ALLOCV RB_ALLOCV
405#define ALLOCV_N RB_ALLOCV_N
406#define ALLOCV_END RB_ALLOCV_END
419struct rbimpl_size_mul_overflow_tag {
440void *rb_alloc_tmp_buffer(volatile
VALUE *store,
long len);
463void *rb_alloc_tmp_buffer_with_count(volatile
VALUE *store,
size_t len,
size_t count);
476void rb_free_tmp_buffer(volatile
VALUE *store);
489void ruby_malloc_size_overflow(
size_t x,
size_t y);
501void ruby_malloc_add_size_overflow(
size_t x,
size_t y);
503#ifdef HAVE_RB_GC_GUARDED_PTR_VAL
509# pragma optimize("", off)
520static inline volatile VALUE *
521rb_gc_guarded_ptr(
volatile VALUE *ptr)
526# pragma optimize("", on)
550 if (c2 > max)
return 1;
551 *c = RBIMPL_CAST((
size_t)c2);
553 if (b != 0 && a > max / b)
return 1;
559#if defined(__DOXYGEN__)
561#elif RBIMPL_COMPILER_SINCE(GCC, 7, 0, 0)
563#elif RBIMPL_COMPILER_SINCE(Clang, 7, 0, 0)
583static
inline struct rbimpl_size_mul_overflow_tag
584rbimpl_size_mul_overflow(
size_t x,
size_t y)
586 struct rbimpl_size_mul_overflow_tag ret = {
false, 0, };
589 ret.left = ckd_mul(&ret.right, x, y);
591#elif RBIMPL_HAS_BUILTIN(__builtin_mul_overflow)
592 ret.left = __builtin_mul_overflow(x, y, &ret.right);
594#elif defined(DSIZE_T)
598 ret.left = dz > SIZE_MAX;
599 ret.right = RBIMPL_CAST((
size_t)dz);
601#elif defined(_MSC_VER) && defined(_M_AMD64)
602 unsigned __int64 dp = 0;
603 unsigned __int64 dz = _umul128(x, y, &dp);
604 ret.left = RBIMPL_CAST((
bool)dp);
605 ret.right = RBIMPL_CAST((
size_t)dz);
607#elif defined(_MSC_VER) && defined(_M_ARM64)
608 ret.left = __umulh(x, y) != 0;
613 ret.left = (y != 0) && (x > SIZE_MAX / y);
636rbimpl_size_mul_or_raise(
size_t x,
size_t y)
638 struct rbimpl_size_mul_overflow_tag size =
639 rbimpl_size_mul_overflow(x, y);
641 if (RB_LIKELY(! size.left)) {
645 ruby_malloc_size_overflow(x, y);
650#if defined(__DOXYGEN__)
652#elif RBIMPL_COMPILER_SINCE(GCC, 7, 0, 0)
654#elif RBIMPL_COMPILER_SINCE(Clang, 7, 0, 0)
671static
inline struct rbimpl_size_mul_overflow_tag
672rbimpl_size_add_overflow(
size_t x,
size_t y)
674 struct rbimpl_size_mul_overflow_tag ret = {
false, 0, };
677 ret.left = ckd_add(&ret.right, x, y);
679#elif RBIMPL_HAS_BUILTIN(__builtin_add_overflow)
680 ret.left = __builtin_add_overflow(x, y, &ret.right);
682#elif defined(DSIZE_T)
686 ret.left = dz > SIZE_MAX;
687 ret.right = (size_t)dz;
691 ret.left = ret.right < y;
711rbimpl_size_add_or_raise(
size_t x,
size_t y)
713 struct rbimpl_size_mul_overflow_tag size =
714 rbimpl_size_add_overflow(x, y);
716 if (RB_LIKELY(!size.left)) {
720 ruby_malloc_add_size_overflow(x, y);
742 const size_t total_size = rbimpl_size_mul_or_raise(RBIMPL_CAST((
size_t)count), elsize);
743 const size_t cnt = (total_size +
sizeof(
VALUE) - 1) /
sizeof(
VALUE);
744 return rb_alloc_tmp_buffer_with_count(store, total_size, cnt);
755ruby_nonempty_memcpy(
void *dest, const
void *src,
size_t n)
758 return memcpy(dest, src, n);
Defines RBIMPL_ATTR_ALLOC_SIZE.
#define RBIMPL_ATTR_ALLOC_SIZE(tuple)
Wraps (or simulates) __attribute__((alloc_size))
Defines ASSUME / RB_LIKELY / UNREACHABLE.
Defines RBIMPL_ATTR_CONST.
#define RBIMPL_ATTR_CONST()
Wraps (or simulates) __attribute__((const))
#define RBIMPL_ATTR_CONSTEXPR(_)
Wraps (or simulates) C++11 constexpr.
Tweaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
#define RB_GNUC_EXTENSION
This is expanded to nothing for non-GCC compilers.
Defines RBIMPL_HAS_BUILTIN.
int len
Length of the buffer.
#define RBIMPL_UNREACHABLE_RETURN(_)
Wraps (or simulates) __builtin_unreachable.
Defines RBIMPL_ALIGNAS / RBIMPL_ALIGNOF.
static void * rb_alloc_tmp_buffer2(volatile VALUE *store, long count, size_t elsize)
This is an implementation detail of RB_ALLOCV_N().
static int rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
Defines RBIMPL_ATTR_NOALIAS.
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Defines RBIMPL_ATTR_NORETURN.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Defines RBIMPL_ATTR_RESTRICT.
#define RBIMPL_ATTR_RESTRICT()
Wraps (or simulates) __declspec(restrict)
Defines RBIMPL_ATTR_RETURNS_NONNULL.
#define RBIMPL_ATTR_RETURNS_NONNULL()
Wraps (or simulates) __attribute__((returns_nonnull))
uintptr_t VALUE
Type that represents a Ruby object.