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

   ## Summary
   
   Two `freopen()` fixes for `CONFIG_FDSAN=y`, both in 
`libs/libc/stdio/lib_freopen.c`:
   
   * **libc/stdio: bypass fdsan for standard streams in freopen**
     stdin/stdout/stderr are set up by the boot-time console open + dup2 and
     never get an fdsan owner tag, so their descriptors stay unowned.
     `freopen()` closed the old descriptor with `android_fdsan_close_with_tag()`
     using the FILE tag, which hits the fdsan "fd N is unowned" check and
     PANICs, e.g. `freopen("/dev/null", "w", stderr)`.
     For the three standard streams, close the old fd with plain `close()` and
     do not tag the new one, matching `fopen()`/`fdopen()`, which only tag
     regular streams and leave `sl_std[]` unowned.  Regular streams keep the
     tagged close, so real ownership errors are still caught.
   
   * **libc/stdio: fix freopen errno clobbered by fdsan on failed open**
     When `open()` fails, `android_fdsan_exchange_owner_tag()` was called with
     fd = -1 before the `fd < 0` check.  Its `ioctl(-1)` sets errno to EBADF and
     overwrites the real cause (ENOENT, EACCES, EISDIR, ENAMETOOLONG ...).
     Move the tag exchange after the `fd < 0` check.
   
   The second commit depends on the first (same hunk), so they are submitted
   together.
   
   Related: #17910 added fdsan support to freopen.
   
   ## Impact
   
   * Is new feature added? Is existing feature changed? NO.
   * Impact on user (will user need to adapt to change)? NO.  With
     `CONFIG_FDSAN=y`, `freopen()` on a standard stream no longer panics and a
     failed `freopen()` reports the `open()` errno.  Builds without
     `CONFIG_FDSAN` are unchanged.
   * Impact on build (will build process change)? NO.
   * Impact on hardware (will arch(s) / board(s) / driver(s) change)? NO.
   * Impact on documentation (is update required / provided)? NO.
   * Impact on security (any sort of implications)? NO.
   * Impact on compatibility (backward/forward/interoperability)? NO.
   * Anything else to consider or add? NO.
   
   ## Testing
   
   I confirm that changes are verified on local setup and works as intended:
   * Build Host(s): Ubuntu 22.04.5 LTS, x86_64, gcc 13.4.0
   * Target(s): sim:nsh and sim:smp with `CONFIG_FDSAN=y`.  Only verified on
     the simulator, not on real hardware.
   
   Regression:
   * `./tools/checkpatch.sh -m -g` on both commits: pass.
   * sim:nsh and sim:smp (make, `CONFIG_FDSAN=y`): build OK, no new warnings,
     `ostest` exits with status 0.
   * sim:nsh CMake build: OK.
   * Each commit builds and runs on its own.
   
   Functional test: a small app run from NSH on sim:nsh with `CONFIG_FDSAN=y`:
   
   ```c
   fp = fopen("/dev/null", "r");
   errno = 0;
   ret = freopen("/nonexistent/file", "r", fp);   /* test1: expect NULL, ENOENT 
*/
   ret = freopen("/dev/null", "w", stderr);       /* test2: expect stderr */
   ```
   
   Testing logs before change (master):
   
   ```
   nsh> freopentest
   test1 failed-open: ret=NULL errno=9 (Unknown error 9) -> FAIL
   test2 freopen(stderr): start
   dump_assert_info: Assertion failed panic: at file: :0 task: freopentest 
process: freopentest 0x40087774
   ```
   
   Testing logs after change:
   
   ```
   nsh> freopentest
   test1 failed-open: ret=NULL errno=2 (Unknown error 2) -> PASS
   test2 freopen(stderr): start
   test2 freopen(stderr): ret=stderr -> PASS
   nsh> poweroff
   ```
   
   With only the first commit applied, test2 passes and test1 still reports
   errno=9.
   
   ## 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