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

   ## Summary
   
   `tsearch()`, `tfind()`, `tdelete()`, `twalk()` and `tdestroy()` call the 
function that the caller gives: `compar`, `action` or `free_node`. An FDPIC 
module gives the address of a function descriptor, not a code address. The 
firmware branched to the descriptor in the module's data segment, and the 
module stopped with a usage fault.
   
   Commit 1: with `CONFIG_FDPIC`, each of the five functions resolves its 
callback one time at entry with `fdpic_callback()`, as `lfind()`, `bsearch()` 
and `qsort()` do.
   
   - `tsearch()`, `tfind()` and `tdelete()` resolve `compar`.
   - `twalk()` resolves `action` and gives it to the recursive `twalk_node()`.
   - `tdestroy()` resolves `free_node`. `tdestroy()` called itself recursively, 
so the recursion moves into the new static `tdestroy_node()`: a second resolve 
would read a code address as a descriptor.
   
   Commit 2 adds these functions, and `lfind()`/`lsearch()`, to the list of 
callback entry points in `Documentation/os/binfmt/fdpic.rst`.
   
   ## Impact
   
   Only `CONFIG_FDPIC` builds change behaviour (an FDPIC module can now call 
these functions). A caller that is not a module reads 0 from the PIC base 
register, and `fdpic_callback()` gives its pointer back unchanged.
   
   Without `CONFIG_FDPIC`, `nucleo-f072rb:nsh` (Cortex-M0, it does not call 
these functions): text, data and bss are the same as on master. In `libc.a`, 
`lib_tdestroy.o` grows from 78 to 100 bytes (the call to the new helper); the 
other four objects have the same size.
   
   No in-tree code calls these functions. No Kconfig option, no API change.
   
   ## Testing
   
   FDPIC: `mps2-an500:xipfs` with `CONFIG_FDPIC` (QEMU), built with 
`EXTRAFLAGS=-Werror`, 0 warnings. I changed the callback module of 
`examples/fdpicxip` and `xipfs_test` by hand (not in this PR): `callback tree 
[fn]` builds a tree with `tsearch()`, then calls `tfind()`, `twalk()`, 
`tdelete()` and `tdestroy()` with module functions. With one function selected, 
the tree has one node, so only that function calls back.
   
   On master, each of the five functions stops the module with a usage fault 
(invalid state). PC is in the module's data (the descriptor) and LR is in the 
function:
   
   | Function | LR | PC |
   | --- | --- | --- |
   | `tsearch()` | `0x41313` (`tsearch` at `0x412c6`) | `0x60050938` |
   | `tfind()` | `0x41417` (`tfind` at `0x413ec`) | `0x60050938` |
   | `twalk()` | `0x41687` (`twalk_node` at `0x41660`) | `0x60050940` |
   | `tdelete()` | `0x414d5` (`tdelete` at `0x41448`) | `0x60050938` |
   | `tdestroy()` | `0x41741` (`tdestroy` at `0x41704`) | `0x60050948` |
   
   With this PR: `xipfs_test fdpic` 39 passed, 0 failed (the 34 existing checks 
and the 5 new ones), for all five functions together and for each one alone.
   
   Without FDPIC, the tree functions still work, and ostest exits with status 0:
   
   - `qemu-armv8a:knsh` (kernel build, arm64, QEMU with HVF): a test of the 
five functions (2000 keys in random order, in-order walk, depth bound, nodes do 
not move on delete, `tdestroy()` frees each key one time): PASS. Then ostest.
   - `qemu-armv8a:nsh` (flat build, arm64, QEMU with HVF): ostest.
   
   No new warnings in these builds.
   
   `tools/checkpatch.sh -c -u -m -g` passes.
   


-- 
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