From 724a1d478f64c6ec8d7f71ed095de13d0a639a4d Mon Sep 17 00:00:00 2001 From: philip Date: Sat, 22 Apr 2017 19:28:08 -0400 Subject: [PATCH] Prevented multiple opens of the same file --- src/test/test_bugreports.c | 80 ++++++++++++++++++++++++++++++++++---- src/test/test_spiffs.c | 6 ++- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/test/test_bugreports.c b/src/test/test_bugreports.c index 101f379..d7b8b89 100644 --- a/src/test/test_bugreports.c +++ b/src/test/test_bugreports.c @@ -612,6 +612,8 @@ static int run_fuzz_test(FILE *f, int maxfds, int debuglog) { spiffs_file fd[4]; memset(fd, -1, sizeof(fd)); + int openindex[4]; + memset(openindex, -1, sizeof(openindex)); char *filename[8]; int i; @@ -645,14 +647,33 @@ static int run_fuzz_test(FILE *f, int maxfds, int debuglog) { break; } int fdn = ((arg >> 6) & 3) % maxfds; + int rc; switch(c) { case 'O': if (fd[fdn] >= 0) { LOGOP(" close(%d)\n", fd[fdn]); SPIFFS_close(FS, fd[fdn]); + openindex[fdn] = -1; + fd[fdn] = -1; } +#ifndef HAVE_MULTIPLE_OPEN + { + int index = (arg >> 3) & 7; + for (i = 0; i < sizeof(openindex) / sizeof(openindex[0]); i++) { + if (openindex[i] == index) { + break; + } + } + if (i < sizeof(openindex) / sizeof(openindex[0])) { + break; + } + } +#endif LOGOP(" open(\"%s\", 0x%x)", filename[(arg>>3) & 7], modes[arg & 7]); fd[fdn] = SPIFFS_open(FS, filename[(arg>>3) & 7], modes[arg & 7], 0); + if (fd[fdn] >= 0) { + openindex[fdn] = (arg >> 3) & 7; + } LOGOP(" -> %d\n", fd[fdn]); break; @@ -679,7 +700,7 @@ static int run_fuzz_test(FILE *f, int maxfds, int debuglog) { case 'W': if (fd[fdn] >= 0) { LOGOP(" write(%d, , %d)", fd[fdn], (15 << (arg & 7)) + (arg & 127)); - int rc = SPIFFS_write(FS, fd[fdn], buff, (15 << (arg & 7)) + (arg & 127)); + rc = SPIFFS_write(FS, fd[fdn], buff, (15 << (arg & 7)) + (arg & 127)); LOGOP(" -> %d\n", rc); } break; @@ -690,6 +711,7 @@ static int run_fuzz_test(FILE *f, int maxfds, int debuglog) { SPIFFS_close(FS, fd[fdn]); } fd[fdn] = -1; + openindex[fdn] = -1; break; case 'b': @@ -706,21 +728,51 @@ static int run_fuzz_test(FILE *f, int maxfds, int debuglog) { } break; +#ifdef HAVE_REMOVE_OPEN case 'D': if (fd[fdn] >= 0) { LOGOP(" fremove(%d)\n", fd[fdn]); SPIFFS_fremove(FS, fd[fdn]); } break; +#endif case 'd': - LOGOP(" remove(\"%s\")\n", filename[arg & 7]); - SPIFFS_remove(FS, filename[arg & 7]); +#ifndef HAVE_REMOVE_OPEN + { + int index = arg & 7; + for (i = 0; i < sizeof(openindex) / sizeof(openindex[0]); i++) { + if (openindex[i] == index) { + break; + } + } + if (i < sizeof(openindex) / sizeof(openindex[0])) { + break; + } + } +#endif + LOGOP(" remove(\"%s\")", filename[arg & 7]); + rc = SPIFFS_remove(FS, filename[arg & 7]); + LOGOP(" -> %d\n", rc); break; case 'r': - LOGOP(" rename(\"%s\", \"%s\")\n", filename[arg & 7], filename[(arg >> 3) & 7]); - SPIFFS_rename(FS, filename[arg & 7], filename[(arg >> 3) & 7]); +#ifndef HAVE_REMOVE_OPEN + { + int index = arg & 7; + for (i = 0; i < sizeof(openindex) / sizeof(openindex[0]); i++) { + if (openindex[i] == index) { + break; + } + } + if (i < sizeof(openindex) / sizeof(openindex[0])) { + break; + } + } +#endif + LOGOP(" rename(\"%s\", \"%s\")", filename[arg & 7], filename[(arg >> 3) & 7]); + rc = SPIFFS_rename(FS, filename[arg & 7], filename[(arg >> 3) & 7]); + LOGOP(" -> %d\n", rc); break; case 'U': @@ -729,21 +781,33 @@ static int run_fuzz_test(FILE *f, int maxfds, int debuglog) { fd[i] = -1; } { +#ifdef DO_UNMOUNT + LOGOP(" unmount\n"); + SPIFFS_unmount(FS); +#endif char *tmpfile = strdup("/tmp/fsdump.XXXXXX"); - LOGOP(" unmount and remount\n"); + LOGOP(" cycle and remount\n"); close(mkstemp(tmpfile)); fs_store_dump(tmpfile); fs_mount_dump(tmpfile, 0, 0, blocks * block_size, erase_size, block_size, page_size); unlink(tmpfile); free(tmpfile); +#ifndef NO_FORCE_CHECK + LOGOP(" forcecheck()"); + rc = SPIFFS_check(FS); + LOGOP(" -> %d\n", rc); +#endif } break; case 'c': - LOGOP(" check()\n"); - SPIFFS_check(FS); + { + LOGOP(" check()"); + rc = SPIFFS_check(FS); + LOGOP(" -> %d\n", rc); ungetc(arg, f); break; + } default: ungetc(arg, f); diff --git a/src/test/test_spiffs.c b/src/test/test_spiffs.c index 56be261..865c6ce 100644 --- a/src/test/test_spiffs.c +++ b/src/test/test_spiffs.c @@ -218,8 +218,8 @@ static s32_t _write( for (i = 0; i < size; i++) { if (((addr + i) & (SPIFFS_CFG_LOG_PAGE_SZ(&__fs)-1)) != offsetof(spiffs_page_header, flags)) { if (check_valid_flash && ((AREA(addr + i) ^ src[i]) & src[i])) { - printf("trying to write %02x to %02x at addr %08x\n", src[i], AREA(addr + i), addr+i); - spiffs_page_ix pix = (addr + i) / LOG_PAGE; + printf("trying to write %02x to %02x at addr %08x (as part of writing %d bytes to addr %08x)\n", src[i], AREA(addr + i), addr+i, size, addr); + spiffs_page_ix pix = (addr + i) / SPIFFS_CFG_LOG_PAGE_SZ(&__fs); dump_page(&__fs, pix); ERREXIT(); return -1; @@ -492,10 +492,12 @@ static void fs_create(u32_t spiflash_size, ASSERT(_fds != NULL, "testbench fd buffer could not be malloced"); memset(_fds, 0, _fds_sz); +#if SPIFFS_CACHE _cache_sz = sizeof(spiffs_cache) + cache_pages * (sizeof(spiffs_cache_page) + log_page_size); _cache = malloc(_cache_sz); ASSERT(_cache != NULL, "testbench cache could not be malloced"); memset(_cache, 0, _cache_sz); +#endif const u32_t work_sz = log_page_size * 2; _work = malloc(work_sz);