1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2019, Joyent, Inc.
  14  */
  15 
  16 /*
  17  * Check qualifier encoding. Note that the needed_qualifier() workaround applies
  18  * to most of these.
  19  */
  20 
  21 #include "check-common.h"
  22 
  23 static check_descent_t check_descent_const_union_array_gcc4[] = {
  24         { "const union const_union [5]", CTF_K_CONST },
  25         { "union const_union [5]", CTF_K_ARRAY, "union const_union", 5 },
  26         { "union const_union", CTF_K_UNION },
  27         { NULL }
  28 };
  29 
  30 static check_descent_t check_descent_const_union_array_gcc7[] = {
  31         { "const union const_union [5]", CTF_K_ARRAY,
  32             "const union const_union", 5 },
  33         { "const union const_union", CTF_K_CONST },
  34         { "union const_union", CTF_K_UNION },
  35         { NULL }
  36 };
  37 
  38 static check_descent_test_t alt_descents_const_union_array[] = {
  39         { "const_union_array", check_descent_const_union_array_gcc4 },
  40         { "const_union_array", check_descent_const_union_array_gcc7 },
  41         { NULL }
  42 };
  43 
  44 static check_descent_t check_descent_const_struct_array_gcc4[] = {
  45         { "const struct const_struct [7]", CTF_K_CONST },
  46         { "struct const_struct [7]", CTF_K_ARRAY, "struct const_struct", 7 },
  47         { "struct const_struct", CTF_K_STRUCT },
  48         { NULL }
  49 };
  50 
  51 static check_descent_t check_descent_const_struct_array_gcc7[] = {
  52         { "const struct const_struct [7]", CTF_K_ARRAY,
  53             "const struct const_struct", 7 },
  54         { "const struct const_struct", CTF_K_CONST },
  55         { "struct const_struct", CTF_K_STRUCT },
  56         { NULL }
  57 };
  58 
  59 static check_descent_test_t alt_descents_const_struct_array[] = {
  60         { "const_struct_array", check_descent_const_struct_array_gcc4 },
  61         { "const_struct_array", check_descent_const_struct_array_gcc7 },
  62         { NULL }
  63 };
  64 
  65 static check_descent_t check_descent_volatile_struct_array_gcc4[] = {
  66         { "volatile struct volatile_struct [9]", CTF_K_VOLATILE },
  67         { "struct volatile_struct [9]", CTF_K_ARRAY,
  68             "struct volatile_struct", 9 },
  69         { "struct volatile_struct", CTF_K_STRUCT },
  70         { NULL }
  71 };
  72 
  73 static check_descent_t check_descent_volatile_struct_array_gcc7[] = {
  74         { "volatile struct volatile_struct [9]", CTF_K_ARRAY,
  75             "volatile struct volatile_struct", 9 },
  76         { "volatile struct volatile_struct", CTF_K_VOLATILE },
  77         { "struct volatile_struct", CTF_K_STRUCT },
  78         { NULL }
  79 };
  80 
  81 static check_descent_test_t alt_descents_volatile_struct_array[] = {
  82         { "volatile_struct_array", check_descent_volatile_struct_array_gcc4 },
  83         { "volatile_struct_array", check_descent_volatile_struct_array_gcc7 },
  84         { NULL }
  85 };
  86 
  87 static check_descent_t check_descent_c_int_array_gcc4[] = {
  88         { "const int [11]", CTF_K_CONST },
  89         { "int [11]", CTF_K_ARRAY, "int", 11 },
  90         { "int", CTF_K_INTEGER },
  91         { NULL }
  92 };
  93 
  94 static check_descent_t check_descent_c_int_array_gcc7[] = {
  95         { "const int [11]", CTF_K_ARRAY, "const int", 11 },
  96         { "const int", CTF_K_CONST },
  97         { "int", CTF_K_INTEGER },
  98         { NULL }
  99 };
 100 
 101 static check_descent_test_t alt_descents_c_int_array[] = {
 102         { "c_int_array", check_descent_c_int_array_gcc4 },
 103         { "c_int_array", check_descent_c_int_array_gcc7 },
 104         { NULL }
 105 };
 106 
 107 static check_descent_t check_descent_cv_int_array_gcc4[] = {
 108         { "const volatile int [13]", CTF_K_CONST },
 109         { "volatile int [13]", CTF_K_VOLATILE },
 110         { "int [13]", CTF_K_ARRAY, "int", 13 },
 111         { "int", CTF_K_INTEGER },
 112         { NULL }
 113 };
 114 
 115 static check_descent_t check_descent_cv_int_array_gcc7[] = {
 116         { "volatile const int [13]", CTF_K_ARRAY, "volatile const int", 13 },
 117         { "volatile const int", CTF_K_VOLATILE },
 118         { "const int", CTF_K_CONST },
 119         { "int", CTF_K_INTEGER },
 120         { NULL }
 121 };
 122 
 123 static check_descent_test_t alt_descents_cv_int_array[] = {
 124         { "cv_int_array", check_descent_cv_int_array_gcc4 },
 125         { "cv_int_array", check_descent_cv_int_array_gcc7 },
 126         { NULL }
 127 };
 128 
 129 static check_descent_t check_descent_vc_int_array_gcc4[] = {
 130         { "const volatile int [15]", CTF_K_CONST },
 131         { "volatile int [15]", CTF_K_VOLATILE },
 132         { "int [15]", CTF_K_ARRAY, "int", 15 },
 133         { "int", CTF_K_INTEGER },
 134         { NULL }
 135 };
 136 
 137 static check_descent_t check_descent_vc_int_array_gcc7[] = {
 138         { "volatile const int [15]", CTF_K_ARRAY, "volatile const int", 15 },
 139         { "volatile const int", CTF_K_VOLATILE },
 140         { "const int", CTF_K_CONST },
 141         { "int", CTF_K_INTEGER },
 142         { NULL }
 143 };
 144 
 145 static check_descent_test_t alt_descents_vc_int_array[] = {
 146         { "vc_int_array", check_descent_vc_int_array_gcc4 },
 147         { "vc_int_array", check_descent_vc_int_array_gcc7 },
 148         { NULL }
 149 };
 150 
 151 static check_descent_t check_descent_vc_int_array2_gcc4[] = {
 152         { "const volatile int [17]", CTF_K_CONST },
 153         { "volatile int [17]", CTF_K_VOLATILE },
 154         { "int [17]", CTF_K_ARRAY, "int", 17 },
 155         { "int", CTF_K_INTEGER },
 156         { NULL }
 157 };
 158 
 159 static check_descent_t check_descent_vc_int_array2_gcc7[] = {
 160         { "volatile const int [17]", CTF_K_ARRAY, "volatile const int", 17 },
 161         { "volatile const int", CTF_K_VOLATILE },
 162         { "const int", CTF_K_CONST },
 163         { "int", CTF_K_INTEGER },
 164         { NULL }
 165 };
 166 
 167 static check_descent_test_t alt_descents_vc_int_array2[] = {
 168         { "vc_int_array2", check_descent_vc_int_array2_gcc4 },
 169         { "vc_int_array2", check_descent_vc_int_array2_gcc7 },
 170         { NULL }
 171 };
 172 
 173 static check_descent_t check_descent_c_2d_array_gcc4[] = {
 174         { "const int [4][2]", CTF_K_CONST },
 175         { "int [4][2]", CTF_K_ARRAY, "int [2]", 4 },
 176         { "int [2]", CTF_K_ARRAY, "int", 2 },
 177         { "int", CTF_K_INTEGER },
 178         { NULL }
 179 };
 180 
 181 static check_descent_t check_descent_c_2d_array_gcc7[] = {
 182         { "const int [4][2]", CTF_K_ARRAY, "const int [2]", 4 },
 183         { "const int [2]", CTF_K_ARRAY, "const int", 2 },
 184         { "const int", CTF_K_CONST },
 185         { "int", CTF_K_INTEGER },
 186         { NULL }
 187 };
 188 
 189 static check_descent_test_t alt_descents_c_2d_array[] = {
 190         { "c_2d_array", check_descent_c_2d_array_gcc4 },
 191         { "c_2d_array", check_descent_c_2d_array_gcc7 },
 192         { NULL }
 193 };
 194 
 195 static check_descent_t check_descent_cv_3d_array_gcc4[] = {
 196         { "const volatile int [3][2][1]", CTF_K_CONST },
 197         { "volatile int [3][2][1]", CTF_K_VOLATILE },
 198         { "int [3][2][1]", CTF_K_ARRAY, "int [2][1]", 3 },
 199         { "int [2][1]", CTF_K_ARRAY, "int [1]", 2 },
 200         { "int [1]", CTF_K_ARRAY, "int", 1 },
 201         { "int", CTF_K_INTEGER },
 202         { NULL }
 203 };
 204 
 205 static check_descent_t check_descent_cv_3d_array_gcc7[] = {
 206         { "volatile const int [3][2][1]", CTF_K_ARRAY,
 207             "volatile const int [2][1]", 3 },
 208         { "volatile const int [2][1]", CTF_K_ARRAY,
 209             "volatile const int [1]", 2 },
 210         { "volatile const int [1]", CTF_K_ARRAY, "volatile const int", 1 },
 211         { "volatile const int", CTF_K_VOLATILE },
 212         { "const int", CTF_K_CONST },
 213         { "int", CTF_K_INTEGER },
 214         { NULL }
 215 };
 216 
 217 static check_descent_test_t alt_descents_cv_3d_array[] = {
 218         { "cv_3d_array", check_descent_cv_3d_array_gcc4 },
 219         { "cv_3d_array", check_descent_cv_3d_array_gcc7 },
 220         { NULL }
 221 };
 222 
 223 static check_descent_t check_descent_ptr_to_const_int[] = {
 224         { "const int *", CTF_K_POINTER },
 225         { "const int", CTF_K_CONST },
 226         { "int", CTF_K_INTEGER },
 227         { NULL }
 228 };
 229 
 230 static check_descent_test_t alt_descents_ptr_to_const_int[] = {
 231         { "ptr_to_const_int", check_descent_ptr_to_const_int },
 232         { NULL }
 233 };
 234 
 235 static check_descent_t check_descent_const_ptr_to_int[] = {
 236         { "int *const", CTF_K_CONST },
 237         { "int *", CTF_K_POINTER },
 238         { "int", CTF_K_INTEGER },
 239         { NULL }
 240 };
 241 
 242 static check_descent_test_t alt_descents_const_ptr_to_int[] = {
 243         { "const_ptr_to_int", check_descent_const_ptr_to_int },
 244         { NULL }
 245 };
 246 
 247 static check_descent_t check_descent_const_ptr_to_const_int[] = {
 248         { "const int *const", CTF_K_CONST },
 249         { "const int *", CTF_K_POINTER },
 250         { "const int", CTF_K_CONST },
 251         { "int", CTF_K_INTEGER },
 252         { NULL }
 253 };
 254 
 255 static check_descent_test_t alt_descents_const_ptr_to_const_int[] = {
 256         { "const_ptr_to_const_int", check_descent_const_ptr_to_const_int },
 257         { NULL }
 258 };
 259 
 260 static check_descent_test_t *alt_descents[] = {
 261         alt_descents_const_union_array,
 262         alt_descents_const_struct_array,
 263         alt_descents_volatile_struct_array,
 264         alt_descents_c_int_array,
 265         alt_descents_cv_int_array,
 266         alt_descents_vc_int_array,
 267         alt_descents_vc_int_array2,
 268         alt_descents_c_2d_array,
 269         alt_descents_cv_3d_array,
 270         alt_descents_ptr_to_const_int,
 271         alt_descents_const_ptr_to_int,
 272         alt_descents_const_ptr_to_const_int,
 273         NULL
 274 };
 275 
 276 int
 277 main(int argc, char *argv[])
 278 {
 279         int i, ret = 0;
 280 
 281         if (argc < 2) {
 282                 errx(EXIT_FAILURE, "missing test files");
 283         }
 284 
 285         for (i = 1; i < argc; i++) {
 286                 ctf_file_t *fp;
 287 
 288                 if ((fp = ctf_open(argv[i], &ret)) == NULL) {
 289                         warnx("failed to open %s: %s", argv[i],
 290                             ctf_errmsg(ret));
 291                         ret = EXIT_FAILURE;
 292                         continue;
 293                 }
 294 
 295                 for (uint_t j = 0; alt_descents[j] != NULL; j++) {
 296                         check_descent_test_t *descents = alt_descents[j];
 297                         int alt_ok = 0;
 298 
 299                         for (uint_t k = 0; descents[k].cdt_sym != NULL; k++) {
 300                                 if (ctftest_check_descent(descents[k].cdt_sym,
 301                                     fp, descents[k].cdt_tests, B_TRUE)) {
 302                                         alt_ok = 1;
 303                                         break;
 304                                 }
 305                         }
 306 
 307                         if (!alt_ok) {
 308                                 warnx("all descents failed for %s",
 309                                     descents[0].cdt_sym);
 310                                 ret = EXIT_FAILURE;
 311                         }
 312                 }
 313 
 314                 ctf_close(fp);
 315         }
 316 
 317         return (ret);
 318 }