jingfei195887 opened a new pull request, #4164:
URL: https://github.com/apache/nuttx-apps/pull/4164

   ## Summary
   
   Add one cmocka suite for file and inode reference counts. Its nine cases 
cover
   open/close/dup, dup2 replacement and self-dup, unlinking open files, 
epoll-held
   references, dup3/CLOEXEC, task_spawn inheritance, RAMMAP, invalid 
descriptors,
   and concurrent dup/close operations.
   
   Each test owns its temporary directory, descriptors, child, threads and 
mapping.
   Teardown runs after assertion failures. Semaphores synchronize the child with
   the parent; descriptor replacement retains the returned FDCHECK-tagged 
handle.
   The RAMMAP case uses a temporary character driver without mmap/XIP support, 
so
   it cannot silently exercise a filesystem's direct mapping implementation.
   The command returns the cmocka failure count.
   
   ## Impact
   
   - Functionality: YES, optional `cmocka_fs_refstest` test command. No 
production
     filesystem implementation changes.
   - Configuration: YES, `TESTING_FS_REFSTEST` and its directory, priority and 
stack
     options. Requires flat mode, cmocka, pthreads, full descriptor inheritance,
     waitpid and retained child status. Disabled by default.
   - Build: YES, normal Make/CMake application registration. No new external
     dependency beyond the existing optional cmocka framework.
   - Public API, hardware/drivers, security policy and compatibility: NO 
changes.
     The optional RAMMAP case temporarily registers its own character device.
   - Documentation: usage, prerequisites, cases and limitations in the added 
README;
     a companion NuttX Documentation PR follows for the same suite.
   - User adaptation: none unless enabling the suite. Provide a writable 
directory,
     a filesystem supporting unlink-open, and sufficient free descriptors.
   
   ## Testing
   
   I confirm that changes are verified on the local setup and work as intended.
   
   Host: Linux 6.8.0-138-generic x86_64; gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 
13.1.0. Builds ran serially using two CPUs.
   Target: `sim:nsh`, flat mode. Apps base 
`13ec1c27bd130d91c18f6ed8da41765a5a87fc1c`, apps head 
`4f331da1729a724a57e530beb5bdcf18a49ddaa0`;
   NuttX `695d40366dabf7f239c297fdbe57b2473c90cab3`.
   
   Testing logs before change: `git ls-tree 
13ec1c27bd130d91c18f6ed8da41765a5a87fc1c testing/fs/refstest`
   has no output: master has no such suite. This PR adds tests, so no production
   behavior change or before/after performance improvement is claimed.
   
   Testing logs after change:
   
   - Make, FDCHECK enabled with three descriptors per block, RAMMAP enabled:
     **9/9 tests passed twice** on tmpfs, command status 0 both times.
   - Make, RAMMAP disabled: **8/8 tests passed twice**, status 0. The RAMMAP 
case
     is not registered in this configuration.
   - CMake executable, FDCHECK and RAMMAP enabled: **9/9 tests passed twice**.
   - Each successful concurrent case requires all **400 dup/close pairs** to
     complete. Both repetitions preserve a pre-existing sentinel file and remove
     the temporary test directories and character devices.
   - Local fault injection adds `fail_msg` after acquiring a duplicate 
descriptor,
     synchronizing a live child, mapping the device and starting the worker 
threads.
     Both runs report exactly **four intentional failures and return 4**, 
continue
     through all nine cases, and leave only the sentinel. The injection is not 
in
     this PR; the restored source was checked byte-for-byte before the final 
runs.
   - Full-file checkpatch, commit checkpatch, Make/CMake, OSTest and tested-HEAD
     verification passed.
   
   The simulator logs an existing duplicate mount attempt for `/tmp`: both board
   bringup and init.rc mount tmpfs there. The separate `/refsdata` mount used by
   this suite succeeds. The injected build also reports a gcov checksum mismatch
   against a previous local profile; the final builds do not contain the 
injection.
   No real hardware or SMP validation was performed. These finite concurrency
   tests do not establish race freedom on all targets.
   
   Reproduce with cmocka and the suite enabled, set the mount directory to
   `/refsdata`, build, and run the following NSH commands:
   
   <details>
   <summary>NSH script</summary>
   
   ```text
   mount -t tmpfs /refsdata
   echo preserve-existing > /refsdata/keep
   cmocka_fs_refstest
   echo refs-first=$?
   cmocka_fs_refstest
   echo refs-second=$?
   ls -l /refsdata
   cat /refsdata/keep
   ls /dev
   poweroff
   ```
   
   </details>
   
   <details>
   <summary>fdcheck runtime</summary>
   
   ```text
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> mount -t tmpfs /refsdata
   nsh> echo preserve-existing > /refsdata/keep
   nsh> cmocka_fs_refstest
   [==========] tests: Running 9 test(s).
   [ RUN      ] test_nuttx_fs_refs01
   [       OK ] test_nuttx_fs_refs01
   [ RUN      ] test_nuttx_fs_refs02
   [       OK ] test_nuttx_fs_refs02
   [ RUN      ] test_nuttx_fs_refs03
   [       OK ] test_nuttx_fs_refs03
   [ RUN      ] test_nuttx_fs_refs04
   [       OK ] test_nuttx_fs_refs04
   [ RUN      ] test_nuttx_fs_refs05
   [       OK ] test_nuttx_fs_refs05
   [ RUN      ] test_nuttx_fs_refs06
   [       OK ] test_nuttx_fs_refs06
   [ RUN      ] test_nuttx_fs_refs07
   [       OK ] test_nuttx_fs_refs07
   [ RUN      ] test_nuttx_fs_refs08
   [       OK ] test_nuttx_fs_refs08
   [ RUN      ] test_nuttx_fs_refs09
   [       OK ] test_nuttx_fs_refs09
   [==========] tests: 9 test(s) run.
   [  PASSED  ] 9 test(s).
   nsh> echo refs-first=$?
   refs-first=0
   nsh> cmocka_fs_refstest
   [==========] tests: Running 9 test(s).
   [ RUN      ] test_nuttx_fs_refs01
   [       OK ] test_nuttx_fs_refs01
   [ RUN      ] test_nuttx_fs_refs02
   [       OK ] test_nuttx_fs_refs02
   [ RUN      ] test_nuttx_fs_refs03
   [       OK ] test_nuttx_fs_refs03
   [ RUN      ] test_nuttx_fs_refs04
   [       OK ] test_nuttx_fs_refs04
   [ RUN      ] test_nuttx_fs_refs05
   [       OK ] test_nuttx_fs_refs05
   [ RUN      ] test_nuttx_fs_refs06
   [       OK ] test_nuttx_fs_refs06
   [ RUN      ] test_nuttx_fs_refs07
   [       OK ] test_nuttx_fs_refs07
   [ RUN      ] test_nuttx_fs_refs08
   [       OK ] test_nuttx_fs_refs08
   [ RUN      ] test_nuttx_fs_refs09
   [       OK ] test_nuttx_fs_refs09
   [==========] tests: 9 test(s) run.
   [  PASSED  ] 9 test(s).
   nsh> echo refs-second=$?
   refs-second=0
   nsh> ls -l /refsdata
   /refsdata:
    -rwxrwxrwx          18 2026-11-26 16:51 keep
   nsh> cat /refsdata/keep
   preserve-existing
   nsh> ls /dev
   /dev:
    console
    gpio0
    gpio1
    gpio2
    gpio3
    loop
    null
    oneshot
    ram0
    ram1
    rtc0
    zero
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>no-rammap runtime</summary>
   
   ```text
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> mount -t tmpfs /refsdata
   nsh> echo preserve-existing > /refsdata/keep
   nsh> cmocka_fs_refstest
   [==========] tests: Running 8 test(s).
   [ RUN      ] test_nuttx_fs_refs01
   [       OK ] test_nuttx_fs_refs01
   [ RUN      ] test_nuttx_fs_refs02
   [       OK ] test_nuttx_fs_refs02
   [ RUN      ] test_nuttx_fs_refs03
   [       OK ] test_nuttx_fs_refs03
   [ RUN      ] test_nuttx_fs_refs04
   [       OK ] test_nuttx_fs_refs04
   [ RUN      ] test_nuttx_fs_refs05
   [       OK ] test_nuttx_fs_refs05
   [ RUN      ] test_nuttx_fs_refs06
   [       OK ] test_nuttx_fs_refs06
   [ RUN      ] test_nuttx_fs_refs08
   [       OK ] test_nuttx_fs_refs08
   [ RUN      ] test_nuttx_fs_refs09
   [       OK ] test_nuttx_fs_refs09
   [==========] tests: 8 test(s) run.
   [  PASSED  ] 8 test(s).
   nsh> echo refs-first=$?
   refs-first=0
   nsh> cmocka_fs_refstest
   [==========] tests: Running 8 test(s).
   [ RUN      ] test_nuttx_fs_refs01
   [       OK ] test_nuttx_fs_refs01
   [ RUN      ] test_nuttx_fs_refs02
   [       OK ] test_nuttx_fs_refs02
   [ RUN      ] test_nuttx_fs_refs03
   [       OK ] test_nuttx_fs_refs03
   [ RUN      ] test_nuttx_fs_refs04
   [       OK ] test_nuttx_fs_refs04
   [ RUN      ] test_nuttx_fs_refs05
   [       OK ] test_nuttx_fs_refs05
   [ RUN      ] test_nuttx_fs_refs06
   [       OK ] test_nuttx_fs_refs06
   [ RUN      ] test_nuttx_fs_refs08
   [       OK ] test_nuttx_fs_refs08
   [ RUN      ] test_nuttx_fs_refs09
   [       OK ] test_nuttx_fs_refs09
   [==========] tests: 8 test(s) run.
   [  PASSED  ] 8 test(s).
   nsh> echo refs-second=$?
   refs-second=0
   nsh> ls -l /refsdata
   /refsdata:
    -rwxrwxrwx          18 2026-11-26 16:52 keep
   nsh> cat /refsdata/keep
   preserve-existing
   nsh> ls /dev
   /dev:
    console
    gpio0
    gpio1
    gpio2
    gpio3
    loop
    null
    oneshot
    ram0
    ram1
    rtc0
    zero
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>cmake-runtime runtime</summary>
   
   ```text
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> mount -t tmpfs /refsdata
   nsh> echo preserve-existing > /refsdata/keep
   nsh> cmocka_fs_refstest
   [==========] tests: Running 9 test(s).
   [ RUN      ] test_nuttx_fs_refs01
   [       OK ] test_nuttx_fs_refs01
   [ RUN      ] test_nuttx_fs_refs02
   [       OK ] test_nuttx_fs_refs02
   [ RUN      ] test_nuttx_fs_refs03
   [       OK ] test_nuttx_fs_refs03
   [ RUN      ] test_nuttx_fs_refs04
   [       OK ] test_nuttx_fs_refs04
   [ RUN      ] test_nuttx_fs_refs05
   [       OK ] test_nuttx_fs_refs05
   [ RUN      ] test_nuttx_fs_refs06
   [       OK ] test_nuttx_fs_refs06
   [ RUN      ] test_nuttx_fs_refs07
   [       OK ] test_nuttx_fs_refs07
   [ RUN      ] test_nuttx_fs_refs08
   [       OK ] test_nuttx_fs_refs08
   [ RUN      ] test_nuttx_fs_refs09
   [       OK ] test_nuttx_fs_refs09
   [==========] tests: 9 test(s) run.
   [  PASSED  ] 9 test(s).
   nsh> echo refs-first=$?
   refs-first=0
   nsh> cmocka_fs_refstest
   [==========] tests: Running 9 test(s).
   [ RUN      ] test_nuttx_fs_refs01
   [       OK ] test_nuttx_fs_refs01
   [ RUN      ] test_nuttx_fs_refs02
   [       OK ] test_nuttx_fs_refs02
   [ RUN      ] test_nuttx_fs_refs03
   [       OK ] test_nuttx_fs_refs03
   [ RUN      ] test_nuttx_fs_refs04
   [       OK ] test_nuttx_fs_refs04
   [ RUN      ] test_nuttx_fs_refs05
   [       OK ] test_nuttx_fs_refs05
   [ RUN      ] test_nuttx_fs_refs06
   [       OK ] test_nuttx_fs_refs06
   [ RUN      ] test_nuttx_fs_refs07
   [       OK ] test_nuttx_fs_refs07
   [ RUN      ] test_nuttx_fs_refs08
   [       OK ] test_nuttx_fs_refs08
   [ RUN      ] test_nuttx_fs_refs09
   [       OK ] test_nuttx_fs_refs09
   [==========] tests: 9 test(s) run.
   [  PASSED  ] 9 test(s).
   nsh> echo refs-second=$?
   refs-second=0
   nsh> ls -l /refsdata
   /refsdata:
    -rwxrwxrwx          18 2026-11-26 16:57 keep
   nsh> cat /refsdata/keep
   preserve-existing
   nsh> ls /dev
   /dev:
    console
    gpio0
    gpio1
    gpio2
    gpio3
    loop
    null
    oneshot
    ram0
    ram1
    rtc0
    zero
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>Intentional assertion failures and cleanup</summary>
   
   ```text
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> mount -t tmpfs /refsdata
   nsh> echo preserve-existing > /refsdata/keep
   nsh> cmocka_fs_refstest
   [==========] tests: Running 9 test(s).
   [ RUN      ] test_nuttx_fs_refs01
   [  ERROR   ] --- ERROR: injected descriptor cleanup failure
   [   LINE   ] --- fs_refs_test.c:469: error: Failure!
   [  FAILED  ] test_nuttx_fs_refs01
   [ RUN      ] test_nuttx_fs_refs02
   [       OK ] test_nuttx_fs_refs02
   [ RUN      ] test_nuttx_fs_refs03
   [       OK ] test_nuttx_fs_refs03
   [ RUN      ] test_nuttx_fs_refs04
   [       OK ] test_nuttx_fs_refs04
   [ RUN      ] test_nuttx_fs_refs05
   [       OK ] test_nuttx_fs_refs05
   [ RUN      ] test_nuttx_fs_refs06
   [  ERROR   ] --- ERROR: injected child cleanup failure
   [   LINE   ] --- fs_refs_test.c:626: error: Failure!
   [  FAILED  ] test_nuttx_fs_refs06
   [ RUN      ] test_nuttx_fs_refs07
   [  ERROR   ] --- ERROR: injected mapping cleanup failure
   [   LINE   ] --- fs_refs_test.c:662: error: Failure!
   [  FAILED  ] test_nuttx_fs_refs07
   [ RUN      ] test_nuttx_fs_refs08
   [       OK ] test_nuttx_fs_refs08
   [ RUN      ] test_nuttx_fs_refs09
   [  ERROR   ] --- ERROR: injected thread cleanup failure
   [   LINE   ] --- fs_refs_test.c:716: error: Failure!
   [  FAILED  ] test_nuttx_fs_refs09
   [==========] tests: 9 test(s) run.
   [  PASSED  ] 5 test(s).
   [  FAILED  ] tests: 4 test(s), listed below:
   [  FAILED  ] test_nuttx_fs_refs01
   [  FAILED  ] test_nuttx_fs_refs06
   [  FAILED  ] test_nuttx_fs_refs07
   [  FAILED  ] test_nuttx_fs_refs09
   
    4 FAILED TEST(S)
   nsh> echo refs-first=$?
   refs-first=4
   nsh> cmocka_fs_refstest
   [==========] tests: Running 9 test(s).
   [ RUN      ] test_nuttx_fs_refs01
   [  ERROR   ] --- ERROR: injected descriptor cleanup failure
   [   LINE   ] --- fs_refs_test.c:469: error: Failure!
   [  FAILED  ] test_nuttx_fs_refs01
   [ RUN      ] test_nuttx_fs_refs02
   [       OK ] test_nuttx_fs_refs02
   [ RUN      ] test_nuttx_fs_refs03
   [       OK ] test_nuttx_fs_refs03
   [ RUN      ] test_nuttx_fs_refs04
   [       OK ] test_nuttx_fs_refs04
   [ RUN      ] test_nuttx_fs_refs05
   [       OK ] test_nuttx_fs_refs05
   [ RUN      ] test_nuttx_fs_refs06
   [  ERROR   ] --- ERROR: injected child cleanup failure
   [   LINE   ] --- fs_refs_test.c:626: error: Failure!
   [  FAILED  ] test_nuttx_fs_refs06
   [ RUN      ] test_nuttx_fs_refs07
   [  ERROR   ] --- ERROR: injected mapping cleanup failure
   [   LINE   ] --- fs_refs_test.c:662: error: Failure!
   [  FAILED  ] test_nuttx_fs_refs07
   [ RUN      ] test_nuttx_fs_refs08
   [       OK ] test_nuttx_fs_refs08
   [ RUN      ] test_nuttx_fs_refs09
   [  ERROR   ] --- ERROR: injected thread cleanup failure
   [   LINE   ] --- fs_refs_test.c:716: error: Failure!
   [  FAILED  ] test_nuttx_fs_refs09
   [==========] tests: 9 test(s) run.
   [  PASSED  ] 5 test(s).
   [  FAILED  ] tests: 4 test(s), listed below:
   [  FAILED  ] test_nuttx_fs_refs01
   [  FAILED  ] test_nuttx_fs_refs06
   [  FAILED  ] test_nuttx_fs_refs07
   [  FAILED  ] test_nuttx_fs_refs09
   
    4 FAILED TEST(S)
   nsh> echo refs-second=$?
   refs-second=4
   nsh> ls -l /refsdata
   /refsdata:
    -rwxrwxrwx          18 2026-11-26 16:49 keep
   nsh> cat /refsdata/keep
   preserve-existing
   nsh> ls /dev
   /dev:
    console
    gpio0
    gpio1
    gpio2
    gpio3
    loop
    null
    oneshot
    ram0
    ram1
    rtc0
    zero
   nsh> poweroff
   libgcov profiling 
error:/home/mi/test/nuttx_apache/fs-upstream/apps/testing/fs/refstest/fs_refs_test.c.home.mi.test.nuttx_apache.fs-upstream.apps.testing.fs.refstest.gcda:overwriting
 an existing profile data with a different checksum
   ```
   
   </details>
   
   <details>
   <summary>Local injection patch (not submitted)</summary>
   
   ```diff
   --- pr-reference-tests/injected-failures/original.c  2026-11-27 
00:49:09.696159183 +0800
   +++ apps/testing/fs/refstest/fs_refs_test.c  2026-11-27 00:49:09.768160138 
+0800
   @@ -466,6 +466,7 @@
    
      s->fd[1] = dup(s->fd[0]);
      assert_true(s->fd[1] >= 0);
   +  fail_msg("injected descriptor cleanup failure");
      ASSERT_REFS(s->fd[0], 2, s->inode_refs);
    
      s->fd[2] = open(s->path, O_RDWR);
   @@ -622,6 +623,7 @@
                            NULL, NULL, argv, NULL);
      assert_true(s->child > 0);
      assert_int_equal(wait_sem(&s->ready), 0);
   +  fail_msg("injected child cleanup failure");
      ASSERT_REFS(s->fd[0], 2, s->inode_refs + 1);
      ASSERT_REFS(s->fd[1], 1, s->inode_refs + 1);
      assert_int_equal(sem_post(&s->release), 0);
   @@ -657,6 +659,7 @@
    
      s->mapped = mmap(NULL, MAPSIZE, PROT_READ, MAP_PRIVATE, s->fd[1], 0);
      assert_ptr_not_equal(s->mapped, MAP_FAILED);
   +  fail_msg("injected mapping cleanup failure");
      for (i = 0; i < MAPSIZE; i++)
        {
          assert_int_equal(((FAR char *)s->mapped)[i], 'A');
   @@ -710,6 +713,8 @@
          s->workers[i].started = true;
        }
    
   +  fail_msg("injected thread cleanup failure");
   +
      for (i = 0; i < NTHREADS; i++)
        {
          assert_int_equal(pthread_join(s->workers[i].thread, NULL), 0);
   ```
   
   </details>
   
   <details>
   <summary>Final Make/CMake/OSTest gate</summary>
   
   ```text
   target   : apps
   branch   : fs/reference-count-tests
   head     : 4f331da1729a724a57e530beb5bdcf18a49ddaa0
   subject  : fs/test: add testcases for reference of inode and file
   commits  : 1
   base     : 13ec1c27bd (merge-base with origin/master)
   behind   : 0 commits of origin/master
   nuttx    : 695d40366d
   apps     : 4f331da172
   options  : CONFIG_ALLOW_MIT_COMPONENTS CONFIG_LIBC_REGEX 
CONFIG_TESTING_CMOCKA CONFIG_TESTING_FS_REFSTEST CONFIG_FS_TMPFS 
CONFIG_FS_RAMMAP CONFIG_FDCHECK CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=3 
CONFIG_TESTING_FS_REFSTEST_MOUNTDIR="/refsdata"
   host     : Linux x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
   checkpatch: rc=0
   signer   : jingfei <[email protected]>
   commits missing your Signed-off-by: 0
   make build: rc=0
   warnings in changed files: 0
   ostest: Exiting with status 0
   cmake reconfigure: rc=0
   cmake build: rc=0
   worktree tracked changes after: 0
   RESULT: PASS
   ```
   
   </details>
   
   ## PR verification Self-Check
   
   - [x] This PR introduces only one functional change.
   - [x] I have updated all required description fields above.
   - [x] My PR follows the contributing guidelines for scope, coding style and 
testing.
   - [ ] My PR is still work in progress (not ready for review).
   - [x] My PR is ready for review and can be safely merged into the codebase.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to