summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2022-02-18 10:59:45 -0500
committerPeter Zhu <peter@peterzhu.ca>2022-02-22 09:55:21 -0500
commit3df16924b45adfd88c20ef5fe25b10a1acb82dd7 (patch)
treeb943497ee6802001a6cd06de1e51470c0f920dad /include
parent37d5890e4941cedf6918821b29bb4a7e3a092e62 (diff)
[Feature #18249] Implement ABI checking
Header file include/ruby/internal/abi.h contains RUBY_ABI_VERSION which is the ABI version. This value should be bumped whenever an ABI incompatible change is introduced. When loading dynamic libraries, Ruby will compare its own `ruby_abi_version` and the `ruby_abi_version` of the loaded library. If these two values don't match it will raise a `LoadError`. This feature can also be turned off by setting the environment variable `RUBY_RUBY_ABI_CHECK=0`. This feature will prevent cases where previously installed native gems fail in unexpected ways due to incompatibility of changes in header files. This will force the developer to recompile their gems to use the same header files as the built Ruby. In Ruby, the ABI version is exposed through `RbConfig::CONFIG["ruby_abi_version"]`.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/5474
Diffstat (limited to 'include')
-rw-r--r--include/ruby/internal/abi.h45
-rw-r--r--include/ruby/ruby.h1
2 files changed, 46 insertions, 0 deletions
diff --git a/include/ruby/internal/abi.h b/include/ruby/internal/abi.h
new file mode 100644
index 0000000000..78ed4c1875
--- /dev/null
+++ b/include/ruby/internal/abi.h
@@ -0,0 +1,45 @@
+#ifndef RUBY_ABI_H
+#define RUBY_ABI_H
+
+/* This number represents Ruby's ABI version.
+ *
+ * In development Ruby, it should be bumped every time an ABI incompatible
+ * change is introduced. This will force other developers to rebuild extension
+ * gems.
+ *
+ * The following cases are considered as ABI incompatible changes:
+ * - Changing any data structures.
+ * - Changing macros or inline functions causing a change in behavior.
+ * - Deprecating or removing function declarations.
+ *
+ * The following cases are NOT considered as ABI incompatible changes:
+ * - Any changes that does not involve the header files in the `include`
+ * directory.
+ * - Adding macros, inline functions, or function declarations.
+ * - Backwards compatible refactors.
+ * - Editing comments.
+ *
+ * In released versions of Ruby, this number should not be changed since teeny
+ * versions of Ruby should guarantee ABI compatibility.
+ */
+#define RUBY_ABI_VERSION 0
+
+/* Windows does not support weak symbols so ruby_abi_version will not exist
+ * in the shared library. */
+#if defined(HAVE_FUNC_WEAK) && !defined(_WIN32) && !defined(__MINGW32__)
+# define RUBY_DLN_CHECK_ABI 1
+#else
+# define RUBY_DLN_CHECK_ABI 0
+#endif
+
+#if RUBY_DLN_CHECK_ABI
+
+RUBY_FUNC_EXPORTED unsigned long long __attribute__((weak))
+ruby_abi_version(void)
+{
+ return RUBY_ABI_VERSION;
+}
+
+#endif
+
+#endif
diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h
index f35d13685c..108127a93c 100644
--- a/include/ruby/ruby.h
+++ b/include/ruby/ruby.h
@@ -23,6 +23,7 @@
#include <stdarg.h>
#include "defines.h"
+#include "ruby/internal/abi.h"
#include "ruby/internal/anyargs.h"
#include "ruby/internal/arithmetic.h"
#include "ruby/internal/core.h"