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

Unconditionally define POSIX sysconf macros PAGE_SIZE and PAGESIZE #74624

Merged
merged 1 commit into from
Jul 10, 2024

Conversation

besmarsh
Copy link
Contributor

@besmarsh besmarsh commented Jun 20, 2024

The POSIX sysconf() option _SC_PAGESIZE was defined as PAGESIZE, which was defined as PAGE_SIZE (coming from CONFIG_POSIX_PAGE_SIZE) IF not already defined. The STM32 HAL defines PAGESIZE for flash pages as FLASH_PAGE_SIZE, which is only defined in the HAL for some STM32 families. This caused build failures for many STM32 targets. See issue #74623 for full description.

In addition, the POSIX sysconf() options _SC_PAGE_SIZE and _SC_PAGESIZE should be synonymous, but the existing definition could result in them differing when PAGESIZE is already defined elsewhere.

This issue was present in v3.6 but is more of an issue for v3.7 as it causes build failures for applications not even using sysconf at all, if they target an affected STM32 family and enable CONFIG_POSIX_API and CONFIG_CPP.

This PR removes the conditional definition of PAGESIZE and uses PAGE_SIZE (set from CONFIG_POSIX_PAGE_SIZE) for both _SC_PAGE_SIZE and _SC_PAGESIZE.

The POSIX macros PAGE_SIZE and PAGESIZE (queriable through sysconf()) were conditionally defined only if an existing definition did not already exist. These should be defined unconditionally in their header to ensure they get the correct values.

If these macros are defined elsewhere with a different meaning, that's a problem. There was an issue where PAGESIZE was already defined with a different meaning. See #74623 and #74428.

The POSIX macro ATEXIT_MAX is also conditionally defined and should be unconditionally defined, but there is currently a definition in picolibc (picolibc/newlib/libc/include/stdlib.h) so this change will be done separately.

This PR defines PAGE_SIZE and PAGESIZE unconditionally.

@zephyrbot zephyrbot added the area: POSIX POSIX API Library label Jun 20, 2024
@zephyrbot zephyrbot requested review from cfriedt and ycsin June 20, 2024 14:46
@besmarsh
Copy link
Contributor Author

I've just seen #70136, #73363, and #74428 regarding this same issue. The approach there was to make changes to the STM32 HAL - I guess that solves the issue too, but I think the fix here is good for making the _SC_PAGE_SIZE and _SC_PAGESIZE options consistent and equal.

@cfriedt
Copy link
Member

cfriedt commented Jun 20, 2024

#74428 should fix this

@cfriedt cfriedt added the platform: STM32 ST Micro STM32 label Jun 20, 2024
@besmarsh
Copy link
Contributor Author

This is fixed by #74428, but this change ensures that no other rogue PAGESIZE definitions can cause the same issue, and that _SC_PAGE_SIZE and _SC_PAGESIZE always return the same.

Copy link
Member

@cfriedt cfriedt left a comment

Choose a reason for hiding this comment

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

Removing the conditions around defining PAGESIZE should be all that is needed here, but PAGESIZE also needs to be defined according to the spec.

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html

Comment on lines 321 to 324
#ifndef PAGESIZE
#define PAGESIZE PAGE_SIZE
#endif

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#ifndef PAGESIZE
#define PAGESIZE PAGE_SIZE
#endif
#define PAGESIZE PAGE_SIZE

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should the conditions around the definitions of PAGE_SIZE and ATEXIT_MAX just above also be removed to ensure they are defined correctly?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please

#define __z_posix_sysconf_SC_PAGESIZE PAGESIZE
#define __z_posix_sysconf_SC_PAGESIZE PAGE_SIZE
Copy link
Member

Choose a reason for hiding this comment

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

It would be preferable to keep the original definition here. Otherwise, it would be an unnecessary special case for scripts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keeping the original definition is fine if PAGESIZE and PAGE_SIZE are unconditionally defined equal.

Copy link
Member

Choose a reason for hiding this comment

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

Yes

@besmarsh besmarsh changed the title Fix POSIX sysconf implementation for _SC_PAGESIZE/_SC_PAGE_SIZE options Unconditionally define POSIX sysconf macros (ATEXIT, PAGE_SIZE, PAGESIZE) Jun 25, 2024
@besmarsh
Copy link
Contributor Author

PR updated, including change of title and description after scope change.

@besmarsh
Copy link
Contributor Author

ATEXIT_MAX is also defined in picolibc/newlib/libc/include/stdlib.h - I don't know if it should be there or not

@cfriedt
Copy link
Member

cfriedt commented Jun 26, 2024

@besmarsh - if you prefer, you can go back to the way it was before.

There will be a "Great Reckoning" not far down the road where we make internal Zephyr headers as consistent as possible with both Picolibc and Newlib.

@erwango erwango removed the platform: STM32 ST Micro STM32 label Jul 3, 2024
@erwango
Copy link
Member

erwango commented Jul 3, 2024

Removing the STM32 label as this part of the issues has been solved

The POSIX macros PAGE_SIZE and PAGESIZE (queriable through
sysconf()) were conditionally defined only if an existing definition
did not already exist. These should be defined unconditionally in their
header to ensure they get the correct values.

If these macros are defined elsewhere with a different meaning, that's a
problem. There was an issue where PAGESIZE was already defined with a
different meaning. See zephyrproject-rtos#74623 and zephyrproject-rtos#74428.

The POSIX macro ATEXIT_MAX is also conditionally defined and should be
unconditionally defined, but there is currently a definition in picolibc
(picolibc/newlib/libc/include/stdlib.h) so this change will be done
separately.

This commit defines PAGE_SIZE and PAGESIZE unconditionally.

Signed-off-by: Ben Marsh <[email protected]>
@besmarsh besmarsh changed the title Unconditionally define POSIX sysconf macros (ATEXIT, PAGE_SIZE, PAGESIZE) Unconditionally define POSIX sysconf macros PAGE_SIZE and PAGESIZE Jul 8, 2024
@besmarsh
Copy link
Contributor Author

besmarsh commented Jul 9, 2024

@besmarsh - if you prefer, you can go back to the way it was before.

There will be a "Great Reckoning" not far down the road where we make internal Zephyr headers as consistent as possible with both Picolibc and Newlib.

I've removed the ATEXIT_MAX change and just changed PAGE_SIZE and PAGESIZE. The ATEXIT_MAX change can wait for the alignment of Zephyr and Picolibc/Newlib headers.

@ycsin ycsin added this to the v3.7.0 milestone Jul 10, 2024
@aescolar aescolar merged commit 8c0d3de into zephyrproject-rtos:main Jul 10, 2024
22 checks passed
@besmarsh besmarsh deleted the sysconf-fix branch July 22, 2024 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: POSIX POSIX API Library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants