From 42513d61fdb3e37e54cf933493968fbe605657f0 Mon Sep 17 00:00:00 2001 From: dearblue Date: Sun, 1 Sep 2024 20:28:34 +0900 Subject: [PATCH] Need to place static proc objects into 8-byte alignments Static proc objects defined as methods may be placed in 4-byte alignments in 32-bit environments. This may be misinterpreted as an immediate value depending on the address. Since C11 and C++11 have additional language features for byte alignment, corresponding compilers use them to define the `mrb_alignas()` macro. For earlier compilers, they use their own extensions to define the `mrb_alignas()` macro. GCC supports `__attribute__((aligned(alignment)))` since at least version 2.95.3 (1999). https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#IDX305 According to GPT-4, support was added in version 2.7 (1995). It is not known which version of Visual C++ added support for `__declspec(align(n))`. According to GPT-4, at least Visual C++ 6.0 (1998) seems to support it. Also, the documentation of past Intel C/C++ compilers that support `__declspec(align(n))` makes reference to support with Visual C++ 4.2 (1996). https://www.intel.com/content/dam/www/public/ijkk/jp/ja/documents/developer/ccomp40j.pdf --- include/mruby/common.h | 20 ++++++++++++++++++++ mrbgems/mruby-catch/src/catch.c | 1 + src/class.c | 1 + src/proc.c | 1 + 4 files changed, 23 insertions(+) diff --git a/include/mruby/common.h b/include/mruby/common.h index fd5c11022..e70fd4010 100644 --- a/include/mruby/common.h +++ b/include/mruby/common.h @@ -59,6 +59,26 @@ MRB_BEGIN_DECL # define mrb_deprecated #endif +/** Declare a type or object as an alignment requirement. */ +#ifndef mrb_alignas +# if defined(__cplusplus) && __cplusplus >= 201103L +# // https://en.cppreference.com/w/cpp/language/alignas +# define mrb_alignas(n) alignas(n) +# elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L +# // https://en.cppreference.com/w/c/language/_Alignas +# define mrb_alignas(n) _Alignas(n) +# elif defined(_MSC_VER) || defined(__INTEL_COMPILER) +# // https://learn.microsoft.com/en-us/cpp/cpp/align-cpp?view=msvc-170 +# define mrb_alignas(n) __declspec(align(n)) +# elif defined(__GNUC__) || defined(__clang__) +# // https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-aligned-type-attribute +# define mrb_alignas(n) __attribute__((aligned(n))) +# else +# // `mrb_alignas` defined as dummy. If necessary, send issues to https://github.com/mruby/mruby . +# define mrb_alignas(n) +# endif +#endif + /** Declare a function as always inlined. */ #if defined _MSC_VER && _MSC_VER < 1900 # ifndef __cplusplus diff --git a/mrbgems/mruby-catch/src/catch.c b/mrbgems/mruby-catch/src/catch.c index 67db7f309..c1e59ae8e 100644 --- a/mrbgems/mruby-catch/src/catch.c +++ b/mrbgems/mruby-catch/src/catch.c @@ -37,6 +37,7 @@ static const mrb_irep catch_irep = { NULL, sizeof(catch_iseq),0,3,0,0 }; +mrb_alignas(8) static const struct RProc catch_proc = { NULL, NULL, MRB_TT_PROC, MRB_GC_RED, MRB_FL_OBJ_IS_FROZEN | MRB_PROC_SCOPE | MRB_PROC_STRICT, { &catch_irep }, NULL, { NULL } diff --git a/src/class.c b/src/class.c index df74982f8..f0008a164 100644 --- a/src/class.c +++ b/src/class.c @@ -2915,6 +2915,7 @@ static const mrb_irep new_irep = { sizeof(new_iseq), 0, 2, 0, 0, }; +mrb_alignas(8) static const struct RProc new_proc = { NULL, NULL, MRB_TT_PROC, MRB_GC_RED, MRB_FL_OBJ_IS_FROZEN | MRB_PROC_SCOPE | MRB_PROC_STRICT, { &new_irep }, NULL, { NULL } diff --git a/src/proc.c b/src/proc.c index ad177635f..d020abe0a 100644 --- a/src/proc.c +++ b/src/proc.c @@ -36,6 +36,7 @@ static const mrb_irep call_irep = { 0, /* refcnt */ }; +mrb_alignas(8) static const struct RProc call_proc = { NULL, NULL, MRB_TT_PROC, MRB_GC_RED, MRB_FL_OBJ_IS_FROZEN | MRB_PROC_SCOPE | MRB_PROC_STRICT, { &call_irep }, NULL, { NULL }