cd5466
From 104b009e26c050584e4d186c8cc4e1496a14061b Mon Sep 17 00:00:00 2001
cd5466
From: Nobuyoshi Nakada <nobu@ruby-lang.org>
cd5466
Date: Thu, 5 Aug 2021 20:09:25 +0900
cd5466
Subject: [PATCH] Get rid of type-punning pointer casts [Bug #18062]
cd5466
cd5466
---
cd5466
 vm_eval.c       |  4 +++-
cd5466
 vm_insnhelper.c |  7 +++++--
cd5466
 vm_method.c     | 41 ++++++++++++++++++++++++++---------------
cd5466
 3 files changed, 34 insertions(+), 18 deletions(-)
cd5466
cd5466
diff --git a/vm_eval.c b/vm_eval.c
cd5466
index 6d4b5c3c0b28..7ce9f157e671 100644
cd5466
--- a/vm_eval.c
cd5466
+++ b/vm_eval.c
cd5466
@@ -350,9 +350,11 @@ cc_new(VALUE klass, ID mid, int argc, const rb_callable_method_entry_t *cme)
cd5466
     {
cd5466
         struct rb_class_cc_entries *ccs;
cd5466
         struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
cd5466
+        VALUE ccs_data;
cd5466
 
cd5466
-        if (rb_id_table_lookup(cc_tbl, mid, (VALUE*)&ccs)) {
cd5466
+        if (rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
cd5466
             // ok
cd5466
+            ccs = (struct rb_class_cc_entries *)ccs_data;
cd5466
         }
cd5466
         else {
cd5466
             ccs = vm_ccs_create(klass, cme);
cd5466
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
cd5466
index 14928b2afe8e..e186376b24d7 100644
cd5466
--- a/vm_insnhelper.c
cd5466
+++ b/vm_insnhelper.c
cd5466
@@ -1637,9 +1637,11 @@ vm_search_cc(const VALUE klass, const struct rb_callinfo * const ci)
cd5466
     const ID mid = vm_ci_mid(ci);
cd5466
     struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
cd5466
     struct rb_class_cc_entries *ccs = NULL;
cd5466
+    VALUE ccs_data;
cd5466
 
cd5466
     if (cc_tbl) {
cd5466
-        if (rb_id_table_lookup(cc_tbl, mid, (VALUE *)&ccs)) {
cd5466
+        if (rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
cd5466
+            ccs = (struct rb_class_cc_entries *)ccs_data;
cd5466
             const int ccs_len = ccs->len;
cd5466
             VM_ASSERT(vm_ccs_verify(ccs, mid, klass));
cd5466
 
cd5466
@@ -1706,8 +1708,9 @@ vm_search_cc(const VALUE klass, const struct rb_callinfo * const ci)
cd5466
     if (ccs == NULL) {
cd5466
         VM_ASSERT(cc_tbl != NULL);
cd5466
 
cd5466
-        if (LIKELY(rb_id_table_lookup(cc_tbl, mid, (VALUE*)&ccs))) {
cd5466
+        if (LIKELY(rb_id_table_lookup(cc_tbl, mid, &ccs_data))) {
cd5466
             // rb_callable_method_entry() prepares ccs.
cd5466
+            ccs = (struct rb_class_cc_entries *)ccs_data;
cd5466
         }
cd5466
         else {
cd5466
             // TODO: required?
cd5466
diff --git a/vm_method.c b/vm_method.c
cd5466
index 016dba1dbb18..1fd0bd57f7ca 100644
cd5466
--- a/vm_method.c
cd5466
+++ b/vm_method.c
cd5466
@@ -42,11 +42,11 @@ vm_ccs_dump(VALUE klass, ID target_mid)
cd5466
 {
cd5466
     struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
cd5466
     if (cc_tbl) {
cd5466
-        const struct rb_class_cc_entries *ccs;
cd5466
+        VALUE ccs;
cd5466
         if (target_mid) {
cd5466
-            if (rb_id_table_lookup(cc_tbl, target_mid, (VALUE *)&ccs)) {
cd5466
+            if (rb_id_table_lookup(cc_tbl, target_mid, &ccs)) {
cd5466
                 fprintf(stderr, "  [CCTB] %p\n", (void *)cc_tbl);
cd5466
-                vm_ccs_dump_i(target_mid, (VALUE)ccs, NULL);
cd5466
+                vm_ccs_dump_i(target_mid, ccs, NULL);
cd5466
             }
cd5466
         }
cd5466
         else {
cd5466
@@ -72,11 +72,11 @@ vm_mtbl_dump(VALUE klass, ID target_mid)
cd5466
     fprintf(stderr, "# vm_mtbl\n");
cd5466
     while (klass) {
cd5466
         rp_m("  -> ", klass);
cd5466
-        rb_method_entry_t *me;
cd5466
+        VALUE me;
cd5466
 
cd5466
         if (RCLASS_M_TBL(klass)) {
cd5466
             if (target_mid != 0) {
cd5466
-                if (rb_id_table_lookup(RCLASS_M_TBL(klass), target_mid, (VALUE *)&me)) {
cd5466
+                if (rb_id_table_lookup(RCLASS_M_TBL(klass), target_mid, &me)) {
cd5466
                     rp_m("  [MTBL] ", me);
cd5466
                 }
cd5466
             }
cd5466
@@ -90,7 +90,7 @@ vm_mtbl_dump(VALUE klass, ID target_mid)
cd5466
         }
cd5466
         if (RCLASS_CALLABLE_M_TBL(klass)) {
cd5466
             if (target_mid != 0) {
cd5466
-                if (rb_id_table_lookup(RCLASS_CALLABLE_M_TBL(klass), target_mid, (VALUE *)&me)) {
cd5466
+                if (rb_id_table_lookup(RCLASS_CALLABLE_M_TBL(klass), target_mid, &me)) {
cd5466
                     rp_m("  [CM**] ", me);
cd5466
                 }
cd5466
             }
cd5466
@@ -144,10 +144,11 @@ clear_method_cache_by_id_in_class(VALUE klass, ID mid)
cd5466
         // check only current class
cd5466
 
cd5466
         struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
cd5466
-        struct rb_class_cc_entries *ccs;
cd5466
+        VALUE ccs_data;
cd5466
 
cd5466
         // invalidate CCs
cd5466
-        if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, (VALUE *)&ccs)) {
cd5466
+        if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
cd5466
+            struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
cd5466
             rb_vm_ccs_free(ccs);
cd5466
             rb_id_table_delete(cc_tbl, mid);
cd5466
             RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_ccs);
cd5466
@@ -205,9 +206,10 @@ clear_method_cache_by_id_in_class(VALUE klass, ID mid)
cd5466
         }
cd5466
         else {
cd5466
             rb_vm_t *vm = GET_VM();
cd5466
-            if (rb_id_table_lookup(vm->negative_cme_table, mid, (VALUE *)&cme)) {
cd5466
+            VALUE cme_data = (VALUE) cme;
cd5466
+            if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme_data)) {
cd5466
                 rb_id_table_delete(vm->negative_cme_table, mid);
cd5466
-                vm_me_invalidate_cache((rb_callable_method_entry_t *)cme);
cd5466
+                vm_me_invalidate_cache((rb_callable_method_entry_t *)cme_data);
cd5466
 
cd5466
                 RB_DEBUG_COUNTER_INC(cc_invalidate_negative);
cd5466
             }
cd5466
@@ -1023,6 +1025,7 @@ prepare_callable_method_entry(VALUE defined_class, ID id, const rb_method_entry_
cd5466
 {
cd5466
     struct rb_id_table *mtbl;
cd5466
     const rb_callable_method_entry_t *cme;
cd5466
+    VALUE cme_data;
cd5466
 
cd5466
     if (me) {
cd5466
         if (me->defined_class == 0) {
cd5466
@@ -1032,7 +1035,8 @@ prepare_callable_method_entry(VALUE defined_class, ID id, const rb_method_entry_
cd5466
 
cd5466
             mtbl = RCLASS_CALLABLE_M_TBL(defined_class);
cd5466
 
cd5466
-            if (mtbl && rb_id_table_lookup(mtbl, id, (VALUE *)&cme)) {
cd5466
+            if (mtbl && rb_id_table_lookup(mtbl, id, &cme_data)) {
cd5466
+                cme = (rb_callable_method_entry_t *)cme_data;
cd5466
                 RB_DEBUG_COUNTER_INC(mc_cme_complement_hit);
cd5466
                 VM_ASSERT(callable_method_entry_p(cme));
cd5466
                 VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
cd5466
@@ -1076,9 +1080,10 @@ cached_callable_method_entry(VALUE klass, ID mid)
cd5466
     ASSERT_vm_locking();
cd5466
 
cd5466
     struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
cd5466
-    struct rb_class_cc_entries *ccs;
cd5466
+    VALUE ccs_data;
cd5466
 
cd5466
-    if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, (VALUE *)&ccs)) {
cd5466
+    if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
cd5466
+        struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
cd5466
         VM_ASSERT(vm_ccs_p(ccs));
cd5466
 
cd5466
         if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
cd5466
@@ -1104,12 +1109,14 @@ cache_callable_method_entry(VALUE klass, ID mid, const rb_callable_method_entry_
cd5466
 
cd5466
     struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
cd5466
     struct rb_class_cc_entries *ccs;
cd5466
+    VALUE ccs_data;
cd5466
 
cd5466
     if (!cc_tbl) {
cd5466
         cc_tbl = RCLASS_CC_TBL(klass) = rb_id_table_create(2);
cd5466
     }
cd5466
 
cd5466
-    if (rb_id_table_lookup(cc_tbl, mid, (VALUE *)&ccs)) {
cd5466
+    if (rb_id_table_lookup(cc_tbl, mid, &ccs_data)) {
cd5466
+        ccs = (struct rb_class_cc_entries *)ccs_data;
cd5466
         VM_ASSERT(ccs->cme == cme);
cd5466
     }
cd5466
     else {
cd5466
@@ -1123,8 +1130,12 @@ negative_cme(ID mid)
cd5466
 {
cd5466
     rb_vm_t *vm = GET_VM();
cd5466
     const rb_callable_method_entry_t *cme;
cd5466
+    VALUE cme_data;
cd5466
 
cd5466
-    if (!rb_id_table_lookup(vm->negative_cme_table, mid, (VALUE *)&cme)) {
cd5466
+    if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme_data)) {
cd5466
+        cme = (rb_callable_method_entry_t *)cme_data;
cd5466
+    }
cd5466
+    else {
cd5466
         cme = (rb_callable_method_entry_t *)rb_method_entry_alloc(mid, Qnil, Qnil, NULL);
cd5466
         rb_id_table_insert(vm->negative_cme_table, mid, (VALUE)cme);
cd5466
     }