Skip to content

posix: headers: fix remaining newlib picolibc zephyr pthread inconsistencies #52316

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

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented Nov 17, 2022

17 commits that close the last posix-related gaps from Zephyr to Newlib.

Note: Compliance failures are false positives.

  • tests: posix: common: remove duplicate call to pthread_attr_init
  • libc: minimal: include: move sys/stat.h to posix
  • libc: minimal: include: move fcntl.h to posix
  • posix: fcntl: declare fcntl(2) in fcntl.h
  • posix: newlib compatible PTHREAD_CREATE_DETACHED and JOINABLE
  • posix: pthread_attr_t: match size of newlib variant
  • posix: sched: use newlib-compatible SCHED_RR FIFO definitions
  • include: posix: types: fix name on header guard
  • tests: posix: common: avoid direct pthread_attr_t field access
  • include: posix: signal: fix header guard
  • posix: headers: harmonize remaining types with newlib
  • posix: fcntl.h: conditionally define constants to agree with picolibc / newlib
  • posix: sys/stat.h: move O_ACCMODE, RD WRONLY, RDWR to fcntl.h
  • libc: minimal: stdio.h: define SEEK_SET, SEEK_CUR, SEEK_END
  • posix: sys/stat.h: adopt the sys/stat.h header from picolibc
  • posix: sys/stat.h: format for coding style compliance
  • posix: sys/stat.h: declare missing types in sys/stat.h

This was referenced Nov 17, 2022
@cfriedt cfriedt changed the title posix: headers: remaining newlib zephyr pthread inconsistencies posix: headers: fix remaining newlib zephyr pthread inconsistencies Nov 17, 2022
@cfriedt cfriedt force-pushed the fix-remaining-newlib-zephyr-pthread-inconsistencies branch 15 times, most recently from aad0ed9 to e19e209 Compare November 22, 2022 21:54
@cfriedt cfriedt marked this pull request as ready for review November 23, 2022 00:18
@cfriedt
Copy link
Member Author

cfriedt commented Dec 17, 2022

@cfriedt cfriedt force-pushed the fix-remaining-newlib-zephyr-pthread-inconsistencies branch from 0d507d8 to 58ea904 Compare December 17, 2022 15:39
@cfriedt
Copy link
Member Author

cfriedt commented Jan 3, 2023

@keith-packard - do you have an opportunity to re-review?

cfriedt added 17 commits January 5, 2023 16:03
There was a second call to `pthread_attr_init()` that reallly had
no sense being there. Also, it seems that there was a call to
`pthread_attr_destroy()` out of perhaps paranoia.

The duplicate call and `pthread_attr_destroy()` can be removed.

Signed-off-by: Chris Friedt <[email protected]>
The `sys/stat.h` header has never been a part of ISO C so move it
to `zephyr/include/posix/sys/`.

To ensure a smooth migration, leave a stub header in
`lib/libc/minimal/include/sys/` that prints a deprecation warning
suggesting developers either include `<zephyr/posix/sys/stat.h>`
or use `CONFIG_POSIX_API=y`.

Signed-off-by: Chris Friedt <[email protected]>
The `fcntl.h` header has never been a part of ISO C so move it to
`include/zephyr/posix`.

To ensure a smooth migration, a header was left in
`lib/libc/minimal/include` that prints a deprecation warning.

Users should either include `<zephyr/posix/fcntl.h>` or switch to
`CONFIG_POSIX_API=y`.

Signed-off-by: Chris Friedt <[email protected]>
The `fcntl.h` header should declare the `fcntl()` function,
according to the spec.

Signed-off-by: Chris Friedt <[email protected]>
Define `PTHREAD_CREATE_DETACHED` and
`PTHREAD_CREATE_JOINABLE` to be compatible with the Newlib
definitions.

This is a temporary workaround for zephyrproject-rtos#51211 until Newlib
headers are pulled in.

Signed-off-by: Chris Friedt <[email protected]>
Part of the POSIX Roadmap for LTSv3 is to adopt POSIX-related
headers from Newlib. The strategy to get there is to first make
the existing POSIX subsystem work in the presence of Newlib POSIX
types.

Part of the strategy involved adopting Newlib's `uint32_t`
abstraction for some Zephyr POSIX types.

However, some types are declared as structures. Luckily, the API
only passes those structures around in the form of pointers, and
the API only mutates those structures via global functions. With
that, we are able to alias Newlib POSIX types as Zephyr POSIX
structures.

One of the caveats to doing that without introducing stack
corruption is to ensure that the Zephyr POSIX types are <= their
respective Newlib counterparts.

There was only one Zephyr structure for which that requirement
did not hold: `pthread_attr_t`. We left `pthread_attr_t` as the
Newlib definition, and named the Zephyr variant
`struct pthread_attr`.

On 32-bit machines, both structures were 32-bytes.

```
sizeof(pthread_attr_t): 32 sizeof(struct pthread_attr): 32
```

However, on 64-bit machines, `pthread_attr_t` was 40 bytes, while
`struct pthread_attr` was 48 bytes.

```
sizeof(pthread_attr_t): 40 sizeof(struct pthread_attr): 48
```

That triggered the following assertion.

```
BUILD_ASSERT(sizeof(pthread_attr_t)
  >= sizeof(struct pthread_attr));
```

The `stacksize` field was subsequently changed from `size_t` to
`uint32_t`, and that reduced the latter to 40 bytes as well,
solving the last real problem.

```
sizeof(pthread_attr_t): 40 sizeof(struct pthread_attr): 40
```

Signed-off-by: Chris Friedt <[email protected]>
Zephyr's `SCHED_RR` and `SCHED_FIFO` definitions were slightly
different than Newlib's. Additionally, the test had hard-coded
magic numbers instead of using symbolic values.

Signed-off-by: Chris Friedt <[email protected]>
Just to match the filename remove the `SYS_` from
`POSIX_SYS_TYPES_H_`.

Signed-off-by: Chris Friedt <[email protected]>
Avoid directly accessing fields in `pthread_attr_t` and only
access / mutate them with POSIX functions.

Signed-off-by: Chris Friedt <[email protected]>
`POSIX__SIGNAL_H` is not representative of the filename.

Signed-off-by: Chris Friedt <[email protected]>
The remaining types that needed to be harmonized between
Newlib and Zephyr's POSIX definitions are:

* `struct sched_param`
  - don't re-define if using minimal libc
* `pthread_attr_t`
  - convert to `struct pthread_attr`
  - define type if using minimal libc
  - assert acceptible object size
* `pthread_mutexattr_t`
  - convert to `struct pthread_mutexattr`
  - define type if using minimal libc
  - assert acceptible object size
* `pthred_condattr_t`
  - convert to `struct pthread_condattr`
  - define type if using minimal libc
  - assert acceptible object size
* `pthread_once_t`
  - adopt newlib definition
  - define type if using minimal libc
Signed-off-by: Chris Friedt <[email protected]>
PicoLibC defines `O_CREAT` (really `_FCREAT`) as 0x0040.

Otherwise, these constants are identical.

Signed-off-by: Chris Friedt <[email protected]>
Previously, `<sys/stat.h>` was declaring the following constants
which should be declared in `<fcntl.h>` according to POSIX.

Signed-off-by: Chris Friedt <[email protected]>
The `SEEK_SET`, `SEEK_CUR`, and `SEEK_END` constants are defined
in `<stdio.h>`, not in `<sys/stat.h>`.

Signed-off-by: Chris Friedt <[email protected]>
This may be getting ahead of the curve a bit, but here we adopt
the `<sys/stat.h>` header from picolibc. It is mostly the same
as that of newlib but the picolibc variant seemed to be slightly
more "inclusive".

Removed the `<_ansi.h>` include.

The commit immediately following this declares a few missing types
only if they are not already declared.

Signed-off-by: Chris Friedt <[email protected]>
The original format of this file as imported from PicoLibC was not
compatible with Zephyr's coding stytle. This change fixes
compliance issues listed below:

- whitespace formatting
- block comment formatting
- named parameters

Introduction of new typedefs should be considered false-negatives
as these are existing standard POSIX types.

Signed-off-by: Chris Friedt <[email protected]>
After adopting the `<sys/stat.h>` header from picolibc, there is
a possibility that the following types are not defined.

```cpp
typedef int dev_t;
typedef int ino_t;
typedef unsigned short nlink_t;
typedef unsigned short uid_t;
typedef unsigned short gid_t;
typedef unsigned long blksize_t;
typedef unsigned long blkcnt_t;
```

Of the above missing types, the oonly ones that are used today
in Zephyr are `blksize_t` and `blkcnt_t`.

Signed-off-by: Chris Friedt <[email protected]>
@cfriedt cfriedt force-pushed the fix-remaining-newlib-zephyr-pthread-inconsistencies branch from 58ea904 to 6d921c7 Compare January 5, 2023 21:03
@cfriedt
Copy link
Member Author

cfriedt commented Jan 6, 2023

Just a reminder, this needs to be merged sooner rather than later as it's blocking the remaining PRs (in draft) for #51620 (Apache Thrift module) which will be upstreamed as part of the 3.3 release. Would be great to get it merged this week so the last couple of tasks can be completed.

@enjiamai
@stephanosio
@KangJianX
@jeremybettis
@rlubos
@tbursztyka
@pfalcon
@nashif
@Kludentwo
@mengxianglinx
@keith-packard

@keith-packard
Copy link
Collaborator

Afraid I'm unlikely to have time to review this patch in any detail; please don't wait on me.

@stephanosio
Copy link
Member

Compliance check failures are false positives:

NEW_TYPEDEFS: do not add new typedefs
File:include/zephyr/posix/posix_types.h
Line:51
 NEW_TYPEDEFS: do not add new typedefs
File:include/zephyr/posix/posix_types.h
Line:67
 NEW_TYPEDEFS: do not add new typedefs
File:include/zephyr/posix/posix_types.h
Line:78
 NEW_TYPEDEFS: do not add new typedefs
File:include/zephyr/posix/pthread_key.h
Line:19
 NEW_TYPEDEFS: do not add new typedefs
File:include/zephyr/posix/sys/stat.h
Line:43
 NEW_TYPEDEFS: do not add new typedefs
File:include/zephyr/posix/sys/stat.h
Line:48
 NEW_TYPEDEFS: do not add new typedefs
File:include/zephyr/posix/sys/stat.h
Line:53
 NEW_TYPEDEFS: do not add new typedefs
File:include/zephyr/posix/sys/stat.h
Line:58
 NEW_TYPEDEFS: do not add new typedefs
File:include/zephyr/posix/sys/stat.h
Line:63
 NEW_TYPEDEFS: do not add new typedefs
File:include/zephyr/posix/sys/stat.h
Line:68
 NEW_TYPEDEFS: do not add new typedefs
File:include/zephyr/posix/sys/stat.h
Line:73
 SYMBOLIC_PERMS: Symbolic permissions 'S_IRWXU' are not preferred. Consider using octal permissions '0700'.
File:include/zephyr/posix/sys/stat.h
Line:199
 SYMBOLIC_PERMS: Symbolic permissions 'S_IRUSR | S_IWUSR | S_IXUSR' are not preferred. Consider using octal permissions '0700'.
File:include/zephyr/posix/sys/stat.h
Line:199
 SYMBOLIC_PERMS: Symbolic permissions 'S_IRUSR' are not preferred. Consider using octal permissions '0[40](https://github.com/zephyrproject-rtos/zephyr/actions/runs/3850347152/jobs/6560386197#step:9:41)0'.
File:include/zephyr/posix/sys/stat.h
Line:200
 SYMBOLIC_PERMS: Symbolic permissions 'S_IWUSR' are not preferred. Consider using octal permissions '0200'.
File:include/zephyr/posix/sys/stat.h
Line:201
 SYMBOLIC_PERMS: Symbolic permissions 'S_IXUSR' are not preferred. Consider using octal permissions '0100'.
File:include/zephyr/posix/sys/stat.h
Line:202
 SYMBOLIC_PERMS: Symbolic permissions 'S_IRWXG' are not preferred. Consider using octal permissions '0070'.
File:include/zephyr/posix/sys/stat.h
Line:203
 SYMBOLIC_PERMS: Symbolic permissions 'S_IRGRP | S_IWGRP | S_IXGRP' are not preferred. Consider using octal permissions '0070'.
File:include/zephyr/posix/sys/stat.h
Line:203
 SYMBOLIC_PERMS: Symbolic permissions 'S_IRGRP' are not preferred. Consider using octal permissions '0040'.
File:include/zephyr/posix/sys/stat.h
Line:204
 SYMBOLIC_PERMS: Symbolic permissions 'S_IWGRP' are not preferred. Consider using octal permissions '0020'.
File:include/zephyr/posix/sys/stat.h
Line:205
 SYMBOLIC_PERMS: Symbolic permissions 'S_IXGRP' are not preferred. Consider using octal permissions '0010'.
File:include/zephyr/posix/sys/stat.h
Line:206
 SYMBOLIC_PERMS: Symbolic permissions 'S_IRWXO' are not preferred. Consider using octal permissions '0007'.
File:include/zephyr/posix/sys/stat.h
Line:207
 SYMBOLIC_PERMS: Symbolic permissions 'S_IROTH | S_IWOTH | S_IXOTH' are not preferred. Consider using octal permissions '0007'.
File:include/zephyr/posix/sys/stat.h
Line:207
 SYMBOLIC_PERMS: Symbolic permissions 'S_IROTH' are not preferred. Consider using octal permissions '0004'.
File:include/zephyr/posix/sys/stat.h
Line:208
 SYMBOLIC_PERMS: Symbolic permissions 'S_IWOTH' are not preferred. Consider using octal permissions '0002'.
File:include/zephyr/posix/sys/stat.h
Line:209
 SYMBOLIC_PERMS: Symbolic permissions 'S_IXOTH' are not preferred. Consider using octal permissions '0001'.
File:include/zephyr/posix/sys/stat.h
Line:210
 SYMBOLIC_PERMS: Symbolic permissions 'S_IRWXU | S_IRWXG | S_IRWXO' are not preferred. Consider using octal permissions '0777'.
File:include/zephyr/posix/sys/stat.h
Line:213
 SYMBOLIC_PERMS: Symbolic permissions 'S_IRWXU | S_IRWXG | S_IRWXO' are not preferred. Consider using octal permissions '0777'.
File:include/zephyr/posix/sys/stat.h
Line:214
 SYMBOLIC_PERMS: Symbolic permissions 'S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH' are not preferred. Consider using octal permissions '0[66](https://github.com/zephyrproject-rtos/zephyr/actions/runs/3850347152/jobs/6560386197#step:9:67)6'.
File:include/zephyr/posix/sys/stat.h
Line:215

Scancode failure has been sanctioned by the TSC:

Error: * scan/include/zephyr/posix/sys/stat.h has invalid license: bsd-new

@stephanosio stephanosio merged commit 90c558f into zephyrproject-rtos:main Jan 10, 2023
@cfriedt cfriedt deleted the fix-remaining-newlib-zephyr-pthread-inconsistencies branch January 20, 2023 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: C Library C Standard Library area: Networking area: POSIX POSIX API Library area: Sockets Networking sockets area: Wi-Fi Wi-Fi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants