pmdk icon indicating copy to clipboard operation
pmdk copied to clipboard

Inconsistency bugs in array example for libpmemobj

Open dibang2008 opened this issue 5 years ago • 1 comments

ISSUE: Inconsistency bugs in array example for libpmemobj

Environment Information

  • PMDK package version(s): 1.9
  • OS(es) version(s): Ubuntu 16 LTS
  • ndctl version(s): 58+
  • kernel version(s): 4.4.0-173-generic
  • compiler, libraries, packaging and other related tools version(s):

Please provide a reproduction of the bug:

It exists inconsistency bugs if calling following command. ./array /mnt/pmem/testfile alloc test2 100 int

How often bug is revealed: Always:

Actual behavior:

When a crash happens in a specified place, it causes inconsistency.

Expected behavior:

It always keeps consistent.

Details

  • First, line info->array= alloc_array\[type\](size); in Snippet C calls alloc_int in Snippet B and finally reaches Snippet A's alloc_int .
  • Then, the crash happens in specified place of Snippet A (right before return).
  • As a result, data of the array are persistent but previous info in Snippet C doesn't guarantee persistency. So it causes inconsistency

Snippet A

alloc_int(size_t size) { TOID(int) array; /* * To allocate persistent array of simple type is enough to allocate * pointer with size equal to number of elements multiplied by size of * user-defined structure. */ POBJ_ALLOC(pop, &array, int, sizeof(int) * size, NULL, NULL); if (TOID_IS_NULL(array)) { fprintf(stderr, "POBJ_ALLOC\n"); return OID_NULL; } for (size_t i = 0; i < size; i++) D_RW(array)[i] = (int)i; pmemobj_persist(pop, D_RW(array), size * sizeof(*D_RW(array)));
CRASH HAPPEN return array.oid; }

===============================================

Snippet B

static void static fn_alloc alloc_array[] = {NULL, alloc_int, alloc_pmemoid, alloc_toid};

=================================================

Snippet C

do_alloc(int argc, char *argv[]) { if (argc != 3) { printf("usage: ./array alloc " " \n"); return; } enum array_types type = get_type(argv[2]); if (type == UNKNOWN_ARRAY_TYPE) return; size_t size = atoi(argv[1]); TOID(struct array_info) array_info = find_array(argv[0]); if (!TOID_IS_NULL(array_info)) POBJ_FREE(&array_info); POBJ_ZNEW(pop, &array_info, struct array_info); struct array_info *info = D_RW(array_info); strncpy(info->name, argv[0], MAX_BUFFLEN - 1); info->name[MAX_BUFFLEN - 1] = '\0'; info->size = size; info->type = type; info->array = alloc_array[type](size); if (OID_IS_NULL(info->array)) assert(0); pmemobj_persist(pop, info, sizeof(*info)); }

Additional information about Priority and Help Requested:

Are you willing to submit a pull request with a proposed change? (Yes, No)

Requested priority: High

dibang2008 avatar Aug 03 '20 20:08 dibang2008

You are right, thanks for reporting this. I looked over that whole example, and I think it should be either rewritten or removed entirely.

pbalcer avatar Aug 18 '20 09:08 pbalcer