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

   ## Summary
   
   Restore the `clock_t` conversion before `MSEC2TICK` in `nx_poll`.
   On targets with 32-bit `long`, the millisecond-to-microsecond intermediate
   can overflow before the result reaches the semaphore wait. A long poll can
   therefore wait for an incorrect number of ticks or return immediately.
   
   The original fix was included in #14039. Commit
   `c47b1e2c5b30cfd00afcce99ed021c77661d7b7f` later removed this conversion.
   The current `clock.h` constants remain `long` literals, so the current tree
   still needs the promotion on 32-bit targets. This PR restores the original
   one-line fix and retains its author and signatures.
   
   ## Impact
   
   Fixes timeout arithmetic in the VFS poll path. There are no public API,
   configuration, driver, build-system or documentation-interface changes.
   Zero-timeout, indefinite-wait and event/error behavior remain unchanged.
   The conversion uses signed 64-bit `clock_t` before multiplication/rounding.
   
   `nxsem_tickwait` still accepts a 32-bit tick count. This PR does not extend
   that API's representable range. Every timeout tested at 1500 and 10000us per
   tick fits that range. No physical hardware or SMP result is claimed.
   
   ## Testing
   
   Build host: Linux x86-64, GCC13.1.0. Targets: 32/64-bit `sim:nsh`,
   protected `qemu-armv7r:pnsh` and kernel `qemu-armv8a:knsh` (build only).
   Baseline `7acaadba591a451ab64d155d4c84a4f0c6d12517` and branch 
`abb05e9b38c1aa2f30fc781b0b2c311ce9bd3cf5`
   use identical configurations and byte-identical test fixtures in each pair.
   
   The test compiles the actual `fs_poll.c` under private symbol names and
   intercepts its wait functions to inspect the exact tick count without waiting
   for days. The same application also calls the normal production `poll` for
   short timeouts, delayed pipe readiness, large timeouts, an indefinite wait,
   and signal interruption. Captured tick checks cover zero/one/six ignored
   file descriptors, multiplication boundaries and `INT_MAX`.
   
   | Target | Tick interval | Baseline | Branch |
   |---|---:|---:|---:|
   |32-bit sim |1500 us |322 checks, 19 failures |322 checks, 0 failures |
   |32-bit sim |10000 us |322 checks, 0 failures |322 checks, 0 failures |
   |64-bit sim |1500 us |322 checks, 0 failures |322 checks, 0 failures |
   |64-bit sim |10000 us |322 checks, 0 failures |322 checks, 0 failures |
   
   The affected baseline has 18 incorrect captured tick values and one failed
   real delayed-readiness check. All four branch images pass 1288 checks.
   The default 10000us optimized baseline happened to pass these checks; the
   observed regression claim is for the 32-bit 1500 us configuration.
   
   Small optimized 1500 us simulator images exclude the fixture and coverage.
   Text/data/bss deltas are 100/
   0/0 bytes
   on 32-bit, and 0/
   0/0 bytes
   on 64-bit. Both protected/kernel complete images build with 1500us ticks.
   Full-file and commit checkpatch, Make/CMake and OSTest pass.
   
   Two early local fixture attempts were rejected: the first omitted its
   Make.defs registration, and the second used the wrong wait-probe parameter
   type. The final probe matches the actual `uint32_t` wait signature. Those
   attempts are retained locally and excluded from the results above.
   There is no elapsed-day, physical-hardware, SMP or protected/kernel runtime
   test. The change does not address waits that exceed the underlying wait API's
   tick range.
   
   <details>
   <summary>Local validation</summary>
   
   ```text
   target   : nuttx
   branch   : fs/poll-timeout-width
   head     : abb05e9b38c1aa2f30fc781b0b2c311ce9bd3cf5
   subject  : fs/vfs: result_independent_of_operands
   commits  : 1
   base     : 7acaadba59 (merge-base with upstream/master)
   behind   : 0 commits of upstream/master
   nuttx    : abb05e9b38
   apps     : 2c4483b732
   options  : CONFIG_EXAMPLES_POLLTIMECHECK CONFIG_USEC_PER_TICK=1500 
CONFIG_SIM_M32 CONFIG_DEBUG_ASSERTIONS
   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>
   
   <details>
   <summary>Baseline32-bit1500us</summary>
   
   ```text
   nsh> polltimeoutcheck
   POLLTIME sizeof(long)=4 tick_us=1500
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=4294967 nfds=0 got=0 expected=2863312
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=4294967 nfds=1 got=0 expected=2863312
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=4294967 nfds=6 got=0 expected=2863312
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=5000000 nfds=0 got=470022 expected=3333334
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=5000000 nfds=1 got=470022 expected=3333334
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=5000000 nfds=6 got=470022 expected=3333334
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=19768268 nfds=0 got=1725600 expected=13178846
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=19768268 nfds=1 got=1725600 expected=13178846
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=19768268 nfds=6 got=1725600 expected=13178846
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=2147483637 nfds=0 got=2863305 expected=1431655758
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=2147483637 nfds=1 got=2863305 expected=1431655758
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=2147483637 nfds=6 got=2863305 expected=1431655758
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=2147483646 nfds=0 got=2863311 expected=1431655764
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=2147483646 nfds=1 got=2863311 expected=1431655764
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=2147483646 nfds=6 got=2863311 expected=1431655764
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=2147483647 nfds=0 got=0 expected=1431655765
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=2147483647 nfds=1 got=0 expected=1431655765
   POLLTIME FAIL exact converted ticks
   POLLTIME MISMATCH ms=2147483647 nfds=6 got=0 expected=1431655765
   POLLTIME FAIL real delayed readiness
   POLLTIME RESULT checks=322 failures=19
   nsh> echo POLLTIME STATUS $?
   POLLTIME STATUS 1
   nsh> poweroff
   
   ```
   </details>
   
   <details>
   <summary>Branch32-bit1500us</summary>
   
   ```text
   nsh> polltimeoutcheck
   POLLTIME sizeof(long)=4 tick_us=1500
   POLLTIME RESULT checks=322 failures=0
   nsh> echo POLLTIME STATUS $?
   POLLTIME STATUS 0
   nsh> poweroff
   
   ```
   </details>
   
   <details>
   <summary>Baseline32-bit10000us</summary>
   
   ```text
   nsh> polltimeoutcheck
   POLLTIME sizeof(long)=4 tick_us=10000
   POLLTIME RESULT checks=322 failures=0
   nsh> echo POLLTIME STATUS $?
   POLLTIME STATUS 0
   nsh> poweroff
   
   ```
   </details>
   
   <details>
   <summary>Branch32-bit10000us</summary>
   
   ```text
   nsh> polltimeoutcheck
   POLLTIME sizeof(long)=4 tick_us=10000
   POLLTIME RESULT checks=322 failures=0
   nsh> echo POLLTIME STATUS $?
   POLLTIME STATUS 0
   nsh> poweroff
   
   ```
   </details>
   
   <details>
   <summary>Baseline64-bit1500us</summary>
   
   ```text
   nsh> polltimeoutcheck
   POLLTIME sizeof(long)=8 tick_us=1500
   POLLTIME RESULT checks=322 failures=0
   nsh> echo POLLTIME STATUS $?
   POLLTIME STATUS 0
   nsh> poweroff
   
   ```
   </details>
   
   <details>
   <summary>Branch64-bit1500us</summary>
   
   ```text
   nsh> polltimeoutcheck
   POLLTIME sizeof(long)=8 tick_us=1500
   POLLTIME RESULT checks=322 failures=0
   nsh> echo POLLTIME STATUS $?
   POLLTIME STATUS 0
   nsh> poweroff
   
   ```
   </details>
   
   <details>
   <summary>Baseline64-bit10000us</summary>
   
   ```text
   nsh> polltimeoutcheck
   POLLTIME sizeof(long)=8 tick_us=10000
   POLLTIME RESULT checks=322 failures=0
   nsh> echo POLLTIME STATUS $?
   POLLTIME STATUS 0
   nsh> poweroff
   
   ```
   </details>
   
   <details>
   <summary>Branch64-bit10000us</summary>
   
   ```text
   nsh> polltimeoutcheck
   POLLTIME sizeof(long)=8 tick_us=10000
   POLLTIME RESULT checks=322 failures=0
   nsh> echo POLLTIME STATUS $?
   POLLTIME STATUS 0
   nsh> poweroff
   
   ```
   </details>
   
   The following local fixture is not part of the production commit.
   
   <details>
   <summary>apps/examples/polltimeoutcheck/Kconfig</summary>
   
   ```text
   config EXAMPLES_POLLTIMECHECK
        tristate "Poll timeout conversion regression"
        default n
   
   ```
   </details>
   
   <details>
   <summary>apps/examples/polltimeoutcheck/Make.defs</summary>
   
   ```text
   ifneq ($(CONFIG_EXAMPLES_POLLTIMECHECK),)
   CONFIGURED_APPS += $(APPDIR)/examples/polltimeoutcheck
   endif
   
   ```
   </details>
   
   <details>
   <summary>apps/examples/polltimeoutcheck/Makefile</summary>
   
   ```text
   include $(APPDIR)/Make.defs
   PROGNAME = polltimeoutcheck
   PRIORITY = SCHED_PRIORITY_DEFAULT
   STACKSIZE = 8192
   MODULE = $(CONFIG_EXAMPLES_POLLTIMECHECK)
   CSRCS = poll_probe.c
   MAINSRC = polltimeoutcheck_main.c
   CFLAGS += -I$(APPDIR)/../nuttx/fs
   include $(APPDIR)/Application.mk
   
   ```
   </details>
   
   <details>
   <summary>apps/examples/polltimeoutcheck/CMakeLists.txt</summary>
   
   ```text
   if(CONFIG_EXAMPLES_POLLTIMECHECK)
     nuttx_add_application(
       NAME polltimeoutcheck
       STACKSIZE 8192
       SRCS polltimeoutcheck_main.c poll_probe.c
       INCLUDE_DIRECTORIES ${NUTTX_DIR}/fs)
   endif()
   
   ```
   </details>
   
   <details>
   <summary>apps/examples/polltimeoutcheck/poll_probe.c</summary>
   
   ```c
   /****************************************************************************
    * apps/examples/polltimeoutcheck/poll_probe.c
    *
    * SPDX-License-Identifier: Apache-2.0
    *
    
****************************************************************************/
   
   /****************************************************************************
    * Pre-processor Definitions
    
****************************************************************************/
   
   /* Compile the actual VFS source while observing waits without delaying
    * for days. Normal poll calls in the other translation unit stay untouched.
    */
   
   #define poll            polltest_poll
   #define nx_poll         polltest_nx_poll
   #define file_poll       polltest_file_poll
   #define poll_notify     polltest_notify
   #define poll_default_cb polltest_default_cb
   #define nxsem_tickwait  polltest_tickwait
   #define nxsem_wait      polltest_wait
   
   /****************************************************************************
    * Included Files
    
****************************************************************************/
   
   #include "vfs/fs_poll.c"
   
   /****************************************************************************
    * Public Data
    
****************************************************************************/
   
   clock_t g_polltest_ticks;
   int g_polltest_timed;
   int g_polltest_infinite;
   int g_polltest_error = -ETIMEDOUT;
   
   /****************************************************************************
    * Public Functions
    
****************************************************************************/
   
   int polltest_tickwait(FAR sem_t *sem, uint32_t delay)
   {
     g_polltest_ticks = delay;
     g_polltest_timed++;
     return g_polltest_error;
   }
   
   int polltest_wait(FAR sem_t *sem)
   {
     g_polltest_infinite++;
     return -EINTR;
   }
   
   ```
   </details>
   
   <details>
   <summary>apps/examples/polltimeoutcheck/polltimeoutcheck_main.c</summary>
   
   ```c
   /****************************************************************************
    * apps/examples/polltimeoutcheck/polltimeoutcheck_main.c
    *
    * SPDX-License-Identifier: Apache-2.0
    *
    
****************************************************************************/
   
   /****************************************************************************
    * Included Files
    
****************************************************************************/
   
   #include <nuttx/config.h>
   
   #include <errno.h>
   #include <inttypes.h>
   #include <limits.h>
   #include <poll.h>
   #include <pthread.h>
   #include <signal.h>
   #include <stdbool.h>
   #include <stdio.h>
   #include <string.h>
   #include <time.h>
   #include <unistd.h>
   
   #include <nuttx/clock.h>
   
   /****************************************************************************
    * Private Types
    
****************************************************************************/
   
   struct wake_s
   {
     int fd;
     bool signal;
     pthread_t target;
   };
   
   /****************************************************************************
    * Private Data
    
****************************************************************************/
   
   static int g_checks;
   static int g_failures;
   static volatile sig_atomic_t g_signals;
   
   /****************************************************************************
    * Public Data
    
****************************************************************************/
   
   extern clock_t g_polltest_ticks;
   extern int g_polltest_timed;
   extern int g_polltest_infinite;
   extern int g_polltest_error;
   
   /****************************************************************************
    * Public Function Prototypes
    
****************************************************************************/
   
   int polltest_poll(FAR struct pollfd *fds, nfds_t nfds, int timeout);
   
   /****************************************************************************
    * Private Functions
    
****************************************************************************/
   
   static void check(bool ok, FAR const char *name)
   {
     g_checks++;
     if (!ok)
       {
         g_failures++;
         printf("POLLTIME FAIL %s\n", name);
       }
   }
   
   static void reset_probe(void)
   {
     g_polltest_ticks = 0;
     g_polltest_timed = 0;
     g_polltest_infinite = 0;
     g_polltest_error = -ETIMEDOUT;
   }
   
   static void check_conversion(int timeout, int count)
   {
     struct pollfd fds[6];
     clock_t expected;
     int i;
     int ret;
   
     memset(fds, 0, sizeof(fds));
     for (i = 0; i < count; i++)
       {
         fds[i].fd = -1;
         fds[i].events = POLLIN;
         fds[i].revents = POLLOUT;
       }
   
     reset_probe();
     ret = polltest_poll(count ? fds : NULL, count, timeout);
     expected = ((int64_t)timeout * 1000 + CONFIG_USEC_PER_TICK - 1) /
                CONFIG_USEC_PER_TICK;
     check(ret == 0, "timeout result");
     check(g_polltest_timed == 1 && g_polltest_infinite == 0,
           "positive timeout dispatch");
     check(g_polltest_ticks == expected, "exact converted ticks");
     if (g_polltest_ticks != expected)
       {
         printf("POLLTIME MISMATCH ms=%d nfds=%d got=%" PRId64
                " expected=%" PRId64 "\n", timeout, count,
                (int64_t)g_polltest_ticks, (int64_t)expected);
       }
   
     for (i = 0; i < count; i++)
       {
         check(fds[i].revents == 0, "ignored descriptor cleared");
       }
   }
   
   static void signal_handler(int signo)
   {
     g_signals++;
   }
   
   static FAR void *wake_thread(FAR void *arg)
   {
     FAR struct wake_s *wake = arg;
   
     usleep(50000);
     if (wake->signal)
       {
         return (FAR void *)(intptr_t)pthread_kill(wake->target, SIGUSR1);
       }
   
     return (FAR void *)(intptr_t)(write(wake->fd, "x", 1) != 1);
   }
   
   static void check_real_poll(int timeout, bool send_signal)
   {
     struct wake_s wake;
     struct pollfd pfd;
     pthread_t thread;
     FAR void *status;
     int pipefd[2];
     int saved_errno;
     int ret;
     char byte;
   
     ret = pipe(pipefd);
     check(ret == 0, "pipe creation");
     if (ret < 0)
       {
         return;
       }
   
     wake.fd = pipefd[1];
     wake.signal = send_signal;
     wake.target = pthread_self();
     pfd.fd = pipefd[0];
     pfd.events = POLLIN;
     pfd.revents = 0;
     ret = pthread_create(&thread, NULL, wake_thread, &wake);
     check(ret == 0, "writer creation");
     if (ret == 0)
       {
         ret = poll(&pfd, 1, timeout);
         saved_errno = errno;
         check(pthread_join(thread, &status) == 0, "writer joined");
         check((intptr_t)status == 0, "writer status");
         if (send_signal)
           {
             check(ret == -1 && saved_errno == EINTR, "real signal interrupt");
             check(g_signals > 0, "signal delivered");
           }
         else
           {
             check(ret == 1 && (pfd.revents & POLLIN),
                   "real delayed readiness");
             check(read(pipefd[0], &byte, 1) == 1 && byte == 'x',
                   "read payload");
           }
       }
   
     check(close(pipefd[0]) == 0, "reader close");
     check(close(pipefd[1]) == 0, "writer close");
   }
   
   /****************************************************************************
    * Public Functions
    
****************************************************************************/
   
   int main(int argc, FAR char *argv[])
   {
     static const int timeouts[] =
       {
         1, 2, 9, 10, 11, 999, 1000, 1001, 2147482, 2147483,
         2147484, 4294967, 5000000, 19768268, INT_MAX - 10,
         INT_MAX - 1, INT_MAX
       };
   
     struct sigaction action;
     unsigned int i;
     int ret;
   
     printf("POLLTIME sizeof(long)=%zu tick_us=%d\n",
            sizeof(long), CONFIG_USEC_PER_TICK);
     for (i = 0; i < sizeof(timeouts) / sizeof(timeouts[0]); i++)
       {
         check_conversion(timeouts[i], 0);
         check_conversion(timeouts[i], 1);
         check_conversion(timeouts[i], 6);
       }
   
     reset_probe();
     check(polltest_poll(NULL, 0, 0) == 0, "zero timeout");
     check(g_polltest_timed == 0 && g_polltest_infinite == 0, "zero no wait");
     check(polltest_poll(NULL, 0, -1) == -1 && errno == EINTR,
           "negative indefinite");
     check(g_polltest_timed == 0 && g_polltest_infinite == 1,
           "indefinite dispatch");
     reset_probe();
     g_polltest_error = -EINTR;
     check(polltest_poll(NULL, 0, INT_MAX) == -1 && errno == EINTR,
           "positive interrupted errno");
     check(g_polltest_timed == 1, "interrupted dispatch");
     check(polltest_poll(NULL, 1, 0) == -1 && errno == EFAULT,
           "invalid array");
     check(poll(NULL, 0, 0) == 0, "real zero timeout");
     check(poll(NULL, 0, 20) == 0, "real short timeout");
     memset(&action, 0, sizeof(action));
     action.sa_handler = signal_handler;
     sigemptyset(&action.sa_mask);
     ret = sigaction(SIGUSR1, &action, NULL);
     check(ret == 0, "signal handler");
     check_real_poll(1000, false);
     check_real_poll(5000000, false);
     check_real_poll(INT_MAX, false);
     check_real_poll(-1, false);
     if (ret == 0)
       {
         check_real_poll(5000000, true);
       }
   
     printf("POLLTIME RESULT checks=%d failures=%d\n", g_checks, g_failures);
     return g_failures ? 1 : 0;
   }
   
   ```
   </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 adheres to Contributing Guidelines and Documentation (git commit 
title and message, coding standard, etc).
   - [ ] My PR is still work in progress (not ready for review).
   - [x] My PR is ready for review and can be safely merged into a 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