fix: 1, ELF core dump caused by GROUP peephole for '../examples/recursive_pointer.c'
authoryu.dongliang <18588496441@163.com>
Wed, 29 Nov 2023 08:34:35 +0000 (16:34 +0800)
committeryu.dongliang <18588496441@163.com>
Wed, 29 Nov 2023 08:34:59 +0000 (16:34 +0800)
     2, auto gc analysis failed for '../examples/recursive_pointer.c'

core/scf_basic_block.c
core/scf_optimizer_auto_gc_find.c
core/scf_optimizer_group.c
examples/recursive_pointer.c [new file with mode: 0644]
util/scf_vector.h

index dca0be5370c7ebb8a80ea942cecc40abc6aac63e..2a5fdb58adcc6552fb365bea7990aa1568f203d8 100644 (file)
@@ -284,6 +284,7 @@ void scf_basic_block_print_list(scf_list_t* h)
                        if (bb->ds_malloced) {
                                scf_dn_status_t* ds;
 
+                               printf("auto gc: \n");
                                for (i = 0; i < bb->ds_malloced->size; i++) {
                                        ds =        bb->ds_malloced->data[i];
 
@@ -291,6 +292,7 @@ void scf_basic_block_print_list(scf_list_t* h)
                                                continue;
                                        scf_dn_status_print(ds);
                                }
+                               printf("\n");
                        }
 #endif
                        if (bb->entry_dn_actives) {
index 60e7d063f9748cab44713aae7c233c55a192e601..23fb0dadb822a29bba465d6b1e2c29e3e38968fc 100644 (file)
@@ -76,6 +76,9 @@ static int _bb_add_ds_for_call(scf_basic_block_t* bb, scf_dn_status_t* ds_obj, s
        scf_dn_index_t*    di;
        scf_dn_index_t*    di2;
 
+       scf_variable_t*    v;
+       scf_variable_t*    v2;
+
        int i;
        for (i  = 0; i < bb2->ds_malloced->size; i++) {
                ds2 =        bb2->ds_malloced->data[i];
@@ -101,7 +104,6 @@ static int _bb_add_ds_for_call(scf_basic_block_t* bb, scf_dn_status_t* ds_obj, s
                        }
                }
 
-               int size = ds->dn_indexes->size;
                int j;
                for (j  = 0; j < ds2->dn_indexes->size; j++) {
                        di2 =        ds2->dn_indexes->data[j];
@@ -119,25 +121,42 @@ static int _bb_add_ds_for_call(scf_basic_block_t* bb, scf_dn_status_t* ds_obj, s
                                if (!di->dn) {
                                        scf_dn_status_free(ds);
                                        scf_dn_index_free (di);
+                                       return -ENOMEM;
                                }
                        }
 
-                       if (scf_vector_add(ds->dn_indexes, di) < 0) {
+                       if (scf_vector_add_front(ds->dn_indexes, di) < 0) {
                                scf_dn_status_free(ds);
                                scf_dn_index_free (di);
                                return -ENOMEM;
                        }
                }
 
-               int k;
-               for (k = 0; k < ds2->dn_indexes->size; k++) {
+               v  = ds ->dag_node->var;
+               v2 = ds2->dag_node->var;
 
-                       di = ds->dn_indexes->data[size + k];
+               int m = v ->nb_pointers + v ->nb_dimentions + (v ->type >= SCF_STRUCT);
+               int n = v2->nb_pointers + v2->nb_dimentions + (v2->type >= SCF_STRUCT);
 
-                       for (j = size + k - 1; j > k; j--)
-                               ds->dn_indexes->data[j + 1] = ds->dn_indexes->data[j];
+               for (j = 0; j + m < n; j++) {
 
-                       ds->dn_indexes->data[k] = di;
+                       di = ds->dn_indexes->data[ds->dn_indexes->size - 1];
+
+                       if (di->member || 0 == di->index) {
+                               assert(!di->dn);
+
+                               --ds->dn_indexes->size;
+                               scf_dn_index_free(di);
+                               di = NULL;
+                       } else{
+                               scf_dn_status_free(ds);
+                               return -ENOMEM;
+                       }
+               }
+
+               if (ds->dn_indexes->size <= 0) {
+                       scf_vector_free(ds->dn_indexes);
+                       ds->dn_indexes = NULL;
                }
 
                _bb_add_ds(bb, ds);
@@ -454,7 +473,7 @@ static int _auto_gc_find_argv_out(scf_basic_block_t* cur_bb, scf_3ac_code_t* c)
                if (!v1->auto_gc_flag)
                        continue;
 
-               scf_logd("f2: %s, v0: %s, v1: %s\n", f2->node.w->text->data, v0->w->text->data, v1->w->text->data);
+               scf_logd("i: %d, f2: %s, v0: %s, v1: %s\n", i, f2->node.w->text->data, v0->w->text->data, v1->w->text->data);
 
                ds_obj = NULL;
                if (SCF_OP_ADDRESS_OF == dn->type)
index 332fa7abcdc80400be4be995e999bc8ac0550faa..8d6aece021ad28556f52cbb283f6b9ba93f66230 100644 (file)
@@ -66,6 +66,9 @@ int _optimize_peep_hole(scf_bb_group_t* bbg, scf_basic_block_t* bb)
                if (bb_prev->nexts->size != 1)
                        return 0;
 
+               if (bb_prev->call_flag)
+                       return 0;
+
                if (!scf_vector_find(bbg->body, bb_prev))
                        return 0;
 
@@ -82,14 +85,17 @@ int _optimize_peep_hole(scf_bb_group_t* bbg, scf_basic_block_t* bb)
                                return 0;
                }
 
+               assert(0 == scf_vector_del(bb->dn_reloads, dn));
+
+               if (bb->call_flag)
+                       continue;
+
                for (j = 0; j < bb->prevs->size; j++) {
                        bb_prev   = bb->prevs->data[j];
 
                        assert(0 == scf_vector_del(bb_prev->dn_resaves, dn));
                }
 
-               assert(0 == scf_vector_del(bb->dn_reloads, dn));
-
                int ret = scf_vector_add_unique(bb->dn_resaves, dn);
                if (ret < 0)
                        return ret;
diff --git a/examples/recursive_pointer.c b/examples/recursive_pointer.c
new file mode 100644 (file)
index 0000000..2434b41
--- /dev/null
@@ -0,0 +1,37 @@
+include "../lib/scf_capi.c";
+
+int f(int** t0, int **t1);
+
+int g(int** s0, int** s1)
+{
+       if (!*s1)
+               *s1 = scf__auto_malloc(sizeof(int));
+
+       if (!*s0)
+               f(s0, s1);
+       return 0;
+}
+
+int f(int** t0, int **t1)
+{
+       if (!*t0)
+               *t0 = scf__auto_malloc(sizeof(int));
+
+       if (!*t1)
+               g(t0, t1);
+       return 0;
+}
+
+int main()
+{
+       int* p0 = NULL;
+       int* p1 = NULL;
+
+       f(&p0, &p1);
+
+       *p0 = 1;
+       *p1 = 2;
+
+       printf("%d, %d\n", *p0, *p1);
+       return 0;
+}
index d4249ca9c6d4cc40e3fbe1639734edc5bb13aad1..d8abe21c65e9828c2bb8383c191d5cc837d8adb7 100644 (file)
@@ -89,6 +89,20 @@ static inline int scf_vector_add(scf_vector_t* v, void* node)
        return 0;
 }
 
+static inline int scf_vector_add_front(scf_vector_t* v, void* node)
+{
+       int ret = scf_vector_add(v, node);
+       if (ret < 0)
+               return ret;
+
+       int i;
+       for (i = v->size - 2; i >= 0; i--)
+               v->data[i + 1] = v->data[i];
+
+       v->data[0] = node;
+       return 0;
+}
+
 static inline int scf_vector_del(scf_vector_t* v, void* node)
 {
        if (!v || !v->data)