Print this page
rm code review

Split Close
Expand all
Collapse all
          --- old/usr/src/common/ctf/ctf_create.c
          +++ new/usr/src/common/ctf/ctf_create.c
↓ open down ↓ 27 lines elided ↑ open up ↑
  28   28   * Copyright 2020 Joyent, Inc.
  29   29   */
  30   30  
  31   31  #include <sys/sysmacros.h>
  32   32  #include <sys/param.h>
  33   33  #include <sys/mman.h>
  34   34  #include <ctf_impl.h>
  35   35  #include <sys/debug.h>
  36   36  
  37   37  /*
       38 + * SSIZE_MAX is not available in the kernel, so we define it here rather than
       39 + * accidentally inject into headers where it's not wanted.
       40 + */
       41 +#ifndef SSIZE_MAX
       42 +#define SSIZE_MAX (LONG_MAX)
       43 +#endif
       44 +
       45 +/*
  38   46   * This static string is used as the template for initially populating a
  39   47   * dynamic container's string table.  We always store \0 in the first byte,
  40   48   * and we use the generic string "PARENT" to mark this container's parent
  41   49   * if one is associated with the container using ctf_import().
  42   50   */
  43   51  static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT";
  44   52  
  45   53  /*
  46   54   * To create an empty CTF container, we just declare a zeroed header and call
  47   55   * ctf_bufopen() on it.  If ctf_bufopen succeeds, we mark the new container r/w
↓ open down ↓ 1203 lines elided ↑ open up ↑
1251 1259   * case, except for packed enums.
1252 1260   */
1253 1261  ctf_id_t
1254 1262  ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name, size_t size)
1255 1263  {
1256 1264          ctf_hash_t *hp = &fp->ctf_enums;
1257 1265          ctf_helem_t *hep = NULL;
1258 1266          ctf_dtdef_t *dtd = NULL;
1259 1267          ctf_id_t type = CTF_ERR;
1260 1268  
     1269 +        /* Check we could return something valid in ctf_type_size. */
     1270 +        if (size > SSIZE_MAX)
     1271 +                return (ctf_set_errno(fp, EINVAL));
     1272 +
1261 1273          if (name != NULL)
1262 1274                  hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1263 1275  
1264 1276          if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1265 1277                  type = hep->h_type;
1266 1278                  dtd = ctf_dtd_lookup(fp, type);
1267 1279                  if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1268 1280                          dtd = NULL;
1269 1281          }
1270 1282  
1271 1283          if (dtd == NULL) {
1272 1284                  type = ctf_add_generic(fp, flag, name, &dtd);
1273 1285                  if (type == CTF_ERR)
1274 1286                          return (CTF_ERR); /* errno is set for us */
1275 1287          }
1276 1288  
1277 1289          VERIFY(type != CTF_ERR);
1278 1290          dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0);
1279 1291  
1280      -        if (size == 0) {
1281      -                dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
1282      -        } else {
1283      -                if (size > CTF_MAX_SIZE) {
1284      -                        dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1285      -                        dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
1286      -                        dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
1287      -                } else
1288      -                        dtd->dtd_data.ctt_size = size;
1289      -        }
     1292 +        ctf_set_ctt_size(&dtd->dtd_data, size == 0 ?
     1293 +            fp->ctf_dmodel->ctd_int : size);
1290 1294  
1291 1295          /*
1292 1296           * Always dirty in case we modified a forward.
1293 1297           */
1294 1298          fp->ctf_flags |= LCTF_DIRTY;
1295 1299  
1296 1300          return (type);
1297 1301  }
1298 1302  
1299 1303  ctf_id_t
↓ open down ↓ 258 lines elided ↑ open up ↑
1558 1562                  } else {
1559 1563                          dmd->dmd_offset = offset;
1560 1564                          ssize = (offset + mbitsz) / NBBY;
1561 1565                  }
1562 1566          } else {
1563 1567                  dmd->dmd_offset = 0;
1564 1568                  ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
1565 1569                  ssize = MAX(ssize, msize);
1566 1570          }
1567 1571  
1568      -        if (ssize > CTF_MAX_SIZE) {
1569      -                dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1570      -                dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);
1571      -                dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);
1572      -        } else
1573      -                dtd->dtd_data.ctt_size = (ushort_t)ssize;
     1572 +        ctf_set_ctt_size(&dtd->dtd_data, ssize);
1574 1573  
1575 1574          dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
1576 1575          ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1577 1576  
1578 1577          if (s != NULL)
1579 1578                  fp->ctf_dtstrlen += strlen(s) + 1;
1580 1579  
1581 1580          ctf_ref_inc(fp, type);
1582 1581          fp->ctf_flags |= LCTF_DIRTY;
1583 1582          return (0);
↓ open down ↓ 116 lines elided ↑ open up ↑
1700 1699          const ctf_type_t *tp;
1701 1700          const char *name;
1702 1701          uint_t kind, flag, vlen;
1703 1702  
1704 1703          ctf_bundle_t src, dst;
1705 1704          ctf_encoding_t src_en, dst_en;
1706 1705          ctf_arinfo_t src_ar, dst_ar;
1707 1706  
1708 1707          ctf_dtdef_t *dtd;
1709 1708          ctf_funcinfo_t ctc;
1710      -        ssize_t size;
1711 1709  
1712 1710          ctf_hash_t *hp;
1713 1711          ctf_helem_t *hep;
1714 1712  
1715 1713          if (dst_fp == src_fp)
1716 1714                  return (src_type);
1717 1715  
1718 1716          if (!(dst_fp->ctf_flags & LCTF_RDWR))
1719 1717                  return (ctf_set_errno(dst_fp, ECTF_RDONLY));
1720 1718  
↓ open down ↓ 174 lines elided ↑ open up ↑
1895 1893                  dst_type = ctf_add_generic(dst_fp, flag, name, &dtd);
1896 1894                  if (dst_type == CTF_ERR)
1897 1895                          return (CTF_ERR); /* errno is set for us */
1898 1896  
1899 1897                  dst.ctb_type = dst_type;
1900 1898                  dst.ctb_dtd = dtd;
1901 1899  
1902 1900                  if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0)
1903 1901                          errs++; /* increment errs and fail at bottom of case */
1904 1902  
1905      -                if ((size = ctf_type_size(src_fp, src_type)) > CTF_MAX_SIZE) {
1906      -                        dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1907      -                        dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
1908      -                        dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
1909      -                } else
1910      -                        dtd->dtd_data.ctt_size = (ushort_t)size;
     1903 +                ctf_set_ctt_size(&dtd->dtd_data,
     1904 +                    ctf_type_size(src_fp, src_type));
1911 1905  
1912 1906                  dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen);
1913 1907  
1914 1908                  /*
1915 1909                   * Make a final pass through the members changing each dmd_type
1916 1910                   * (a src_fp type) to an equivalent type in dst_fp.  We pass
1917 1911                   * through all members, leaving any that fail set to CTF_ERR.
1918 1912                   */
1919 1913                  for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1920 1914                      dmd != NULL; dmd = ctf_list_next(dmd)) {
↓ open down ↓ 14 lines elided ↑ open up ↑
1935 1929                          ctf_ref_inc(dst_fp, dmd->dmd_type);
1936 1930                  break;
1937 1931          }
1938 1932  
1939 1933          case CTF_K_ENUM:
1940 1934                  if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1941 1935                          if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) ||
1942 1936                              ctf_enum_iter(dst_fp, dst_type, enumcmp, &src))
1943 1937                                  return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1944 1938                  } else {
1945      -                        size_t size = ctf_type_size(src_fp, src_type);
     1939 +                        ssize_t size = ctf_type_size(src_fp, src_type);
     1940 +
     1941 +                        if (size == CTF_ERR)
     1942 +                                return (CTF_ERR); /* errno is set for us */
     1943 +
1946 1944                          dst_type = ctf_add_enum(dst_fp, flag, name, size);
1947 1945                          if ((dst.ctb_type = dst_type) == CTF_ERR ||
1948 1946                              ctf_enum_iter(src_fp, src_type, enumadd, &dst))
1949 1947                                  return (CTF_ERR); /* errno is set for us */
1950 1948                  }
1951 1949                  break;
1952 1950  
1953 1951          case CTF_K_FORWARD:
1954 1952                  if (dst_type == CTF_ERR) {
1955 1953                          dst_type = ctf_add_forward(dst_fp,
↓ open down ↓ 217 lines elided ↑ open up ↑
2173 2171  
2174 2172          if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
2175 2173                  return (ctf_set_errno(fp, ECTF_NOTSOU));
2176 2174  
2177 2175          if ((oldsz = dtd->dtd_data.ctt_size) == CTF_LSIZE_SENT)
2178 2176                  oldsz = CTF_TYPE_LSIZE(&dtd->dtd_data);
2179 2177  
2180 2178          if (newsz < oldsz)
2181 2179                  return (ctf_set_errno(fp, EINVAL));
2182 2180  
2183      -        if (newsz > CTF_MAX_SIZE) {
2184      -                dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
2185      -                dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(newsz);
2186      -                dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(newsz);
2187      -        } else {
2188      -                dtd->dtd_data.ctt_size = (ushort_t)newsz;
2189      -        }
     2181 +        ctf_set_ctt_size(&dtd->dtd_data, newsz);
2190 2182  
2191 2183          fp->ctf_flags |= LCTF_DIRTY;
2192 2184          return (0);
2193 2185  }
2194 2186  
2195 2187  int
2196 2188  ctf_set_root(ctf_file_t *fp, ctf_id_t id, const boolean_t vis)
2197 2189  {
2198 2190          ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id);
2199 2191          uint_t kind, vlen;
↓ open down ↓ 13 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX