Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minimal libc + posix: add strerror and perror functions #46101

Merged
merged 4 commits into from
Jul 4, 2022

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented May 29, 2022

Add basic strerror() and perror() implementations to the minimal C library.

Also add myself as collaborator in that area.

With a simple change to samples/hello_world, we observe the following size differences on cortex-m3:

Before change:

Memory region         Used Size  Region Size  %age Used
           FLASH:        8576 B       256 KB      3.27%

After change (CONFIG_MINIMAL_LIBC_STRING_ERROR_TABLE=n):

Memory region         Used Size  Region Size  %age Used
           FLASH:        8576 B       256 KB      3.27%

After change (CONFIG_MINIMAL_LIBC_STRING_ERROR_TABLE=y):

Memory region         Used Size  Region Size  %age Used
           FLASH:       10852 B       256 KB      4.14%

Fixes #46099
Fixes #46100

@cfriedt cfriedt requested a review from nashif as a code owner May 29, 2022 14:08
@github-actions github-actions bot added area: C Library C Standard Library area: POSIX POSIX API Library labels May 29, 2022
@cfriedt cfriedt force-pushed the strerror-perror branch 2 times, most recently from d656113 to 92d6660 Compare May 29, 2022 14:23
@cfriedt
Copy link
Member Author

cfriedt commented May 29, 2022

Coding guideline issues are a false positive

Copy link
Member

@stephanosio stephanosio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to update https://github.com/zephyrproject-rtos/zephyr/blob/main/doc/services/portability/posix.rst as well.

Also as per #45776 and the previous discussion on the scope of the minimal libc, I am not convinced this should be a part of the minimal libc because:

  1. The original intent of the minimal libc was to provide only the minimal amount of functions required to make Zephyr run for testing and validation as per @nashif's comments in the linked discussion.
  2. These functions are not required by any of the Zephyr internal components.
  3. We will be moving on to using the picolibc as the default C library instead of the minimal libc in the near future, in which case the latter will be reserved only for internal testing and validation purposes and will not be used by end users/products.

Nevertheless, I am not necessarily opposed to merging this because we have added other "non-essential" features to the minimal libc before, and this feature can be configured such that it does cause any meaningful footprint increase.

lib/libc/Kconfig Outdated Show resolved Hide resolved
lib/libc/minimal/include/string.h Outdated Show resolved Hide resolved
lib/posix/perror.c Outdated Show resolved Hide resolved
scripts/gen_strerror.py Outdated Show resolved Hide resolved
@stephanosio stephanosio added the area: Minimal libc Minimal C Standard Library label May 30, 2022
@cfriedt
Copy link
Member Author

cfriedt commented May 30, 2022

Need to update https://github.com/zephyrproject-rtos/zephyr/blob/main/doc/services/portability/posix.rst as well.

Good catch - will do.

Also as per #45776 and the previous discussion on the scope of the minimal libc, I am not convinced this should be a part of the minimal libc because:

  1. The original intent of the minimal libc was to provide only the minimal amount of functions required to make Zephyr run for testing and validation as per @nashif's comments in the linked discussion.

Yes, definitely.

Another aspect about the minimal libc, was to minimize the amount of maintenance necessary as well. This implementation of strerror() requires only a negligible amount of maintenance. Quite literally just maintaining errno.h, which is something we do already.

I can definitely see the frustration about manually maintaining a separate table of error strings that would rely on a human to remember to update. At least this way it's minimal in that sense.

  1. These functions are not required by any of the Zephyr internal components.

Note: perror() is a PR to lib/posix and not to the libc. It's a common function used on posix systems and references strerror() in most implementations. If perror() exists, then most likely strerror() would exist as well. I would suggest using a Kconfig regardless of the libc though to explicitly include the error string table.

  1. We will be moving on to using the picolibc as the default C library instead of the minimal libc in the near future, in which case the latter will be reserved only for internal testing and validation purposes and will not be used by end users/products.

That works for me too - I would make this PR to picolibc as well, if it does not already have strerror().

I wouldn't necessarily say with absolute certainty that users/products will not use the minimal libc though. If it's in-tree (rather than a module) that might be enough for some organizations to stick with it, as the cost of adding / updating another repo is large in comparison.

Nevertheless, I am not necessarily opposed to merging this because we have added other "non-essential" features to the minimal libc before, and this feature can be configured such that it does cause any meaningful footprint increase.

Thanks. Yeah, I thought that would be a concern for sure, and so wanted to make the default "no code size increase".

@cfriedt cfriedt force-pushed the strerror-perror branch 3 times, most recently from 970d7f9 to e28c4dc Compare May 30, 2022 14:15
@cfriedt cfriedt requested a review from aescolar as a code owner May 30, 2022 14:15
@cfriedt
Copy link
Member Author

cfriedt commented Jun 29, 2022

Looks like some CI issue atm - will see if I can re-run
Screen Shot 2022-06-29 at 11 10 55 AM
Screen Shot 2022-06-29 at 11 10 45 AM
.

tests/lib/c_lib/Kconfig-e Outdated Show resolved Hide resolved
@stephanosio
Copy link
Member

Looks like some CI issue atm - will see if I can re-run

Yea, GitHub is having (yet another) bad day.

https://discord.com/channels/720317445772017664/733037738068148335/991718319310766090

stephanosio
stephanosio previously approved these changes Jun 29, 2022
@cfriedt
Copy link
Member Author

cfriedt commented Jul 1, 2022

@keith-packard - could use your review here as well

Copy link
Collaborator

@keith-packard keith-packard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have the strings present by default so that naive apps 'just work'.

@cfriedt
Copy link
Member Author

cfriedt commented Jul 1, 2022

Let's have the strings present by default so that naive apps 'just work'.

@keith-packard - I've flipped and I've flopped already. Am going to go with the maintainer's request here.

#46101 (comment)

keith-packard
keith-packard previously approved these changes Jul 1, 2022
@cfriedt
Copy link
Member Author

cfriedt commented Jul 2, 2022

@stephanosio, @keith-packard - please re-ack :-)

Add simple strerror() and strerror_r() implementations.

Fixes zephyrproject-rtos#46099

Signed-off-by: Christopher Friedt <[email protected]>
Add tests for strerror() and strerror_r()

Signed-off-by: Christopher Friedt <[email protected]>
Add a trivial implementation of `perror()`.

Fixes zephyrproject-rtos#46100

Signed-off-by: Christopher Friedt <[email protected]>
Add myself as collaborator on the C library.

Signed-off-by: Christopher Friedt <[email protected]>
@carlescufi
Copy link
Member

@keith-packard can you please re-approve?

Copy link
Collaborator

@keith-packard keith-packard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm.

@carlescufi carlescufi merged commit b4d7b1c into zephyrproject-rtos:main Jul 4, 2022
@cfriedt cfriedt deleted the strerror-perror branch July 4, 2022 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lib: posix: support for perror() libc: minimal: add strerror() function
6 participants