Skip to contentSkip to conversation

x86/pkeys: Add self-tests

bug report Dan Carpenter 1 participant 1 message Feb 7, 2022 Kselftest

lore
Share & tools
lore
#0 lore raw

Hello Dave Hansen,

The patch 5f23f6d082a9: "x86/pkeys: Add self-tests" from Jul 29, 2016, leads to the following Smatch static checker warning:

tools/testing/selftests/vm/protection_keys.c:647 record_pkey_malloc()
warn: address of 'pkey_malloc_records[i]' is probably non-NULL

tools/testing/selftests/vm/protection_keys.c

    638 long nr_pkey_malloc_records;
    639 void record_pkey_malloc(void *ptr, long size, int prot)
    640 {
    641         long i;
    642         struct pkey_malloc_record *rec = NULL;
    643 
    644         for (i = 0; i < nr_pkey_malloc_records; i++) {
    645                 rec = &pkey_malloc_records[i];
    646                 /* find a free record */
--> 647                 if (rec)
    648                         break;
    649         }
    650         if (!rec) {

This code is supposed re-allocate memory. If we run out, then allocate 2x the memory. But it only works for the first allocation where "pkey_malloc_records" is NULL.

For the following allocations it will just select &pkey_malloc_records[0] and re-use that again.

    651                 /* every record is full */
    652                 size_t old_nr_records = nr_pkey_malloc_records;
    653                 size_t new_nr_records = (nr_pkey_malloc_records * 2 + 1);
    654                 size_t new_size = new_nr_records * sizeof(struct pkey_malloc_record);
    655                 dprintf2("new_nr_records: %zd\n", new_nr_records);
    656                 dprintf2("new_size: %zd\n", new_size);
    657                 pkey_malloc_records = realloc(pkey_malloc_records, new_size);
    658                 pkey_assert(pkey_malloc_records != NULL);
    659                 rec = &pkey_malloc_records[nr_pkey_malloc_records];
    660                 /*
    661                  * realloc() does not initialize memory, so zero it from
    662                  * the first new record all the way to the end.
    663                  */
    664                 for (i = 0; i < new_nr_records - old_nr_records; i++)
    665                         memset(rec + i, 0, sizeof(*rec));
    666         }
    667         dprintf3("filling malloc record[%d/%p]: {%p, %ld}\n",
    668                 (int)(rec - pkey_malloc_records), rec, ptr, size);
    669         rec->ptr = ptr;
    670         rec->size = size;
    671         rec->prot = prot;
    672         pkey_last_malloc_record = rec;
    673         nr_pkey_malloc_records++;
    674 }

regards,
dan carpenter