Commit graph

455 commits

Author SHA1 Message Date
Flavio Ceolin
417c18c974 tests: posis/fs: close only valid descriptor
Only call close() if open() has not returned a negative
value.

Fixes CID 211585

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-02-01 02:54:19 -05:00
Gaetan Perrot
c6c99245f6 posix: sched: Implement tests for set APIs for scheduling parameters
Implement tests for `sched_setparam()` and `sched_setscheduler()` .
Both functions are actually placeholders and just return `ENOSYS`
since Zephyr does not yet support processes or process scheduling.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-01-31 11:16:26 +01:00
Abhinav Srivastava
52485876f0 posix: Add tests for stropts
Add tests, Fix config issues, Re-add Timer

Signed-off-by: Abhinav Srivastava <atg271@gmail.com>
2024-01-31 09:05:50 +01:00
Adam Wojasinski
31af5f8fe1 tests: posix: Add test case for sysconf()
Adds test cases for sysconf() basic implementation.
Test case does not cover scenario where invalid value is passed
to the name argument of the function.

Signed-off-by: Adam Wojasinski <awojasinski@baylibre.com>
2024-01-30 18:01:18 +01:00
Christopher Friedt
e4b3df53dd tests: posix: pthread: identify thread ids of detached threads
Previously, the number 2 seemed to be somewhat magical. This
removes some of the mystery by labeling it.

So threads >= DETACH_THR_ID will be detached (we should not
attempt to join detached threads).

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-26 06:50:11 -05:00
Christopher Friedt
dbf7003162 tests: posix + libc: qemu_x86*: use dynamic stack size of 4096
For qemu_x86 and qemu_x86_64, it would seem that
heap-allocated thread stacks, as well as thread stacks
defined via K_THREAD_STACK_ARRAY_DEFINE(), must be at least
4096 bytes in size.

Using those sizes will ensure that these tests do not
fail with stack overflows or MMU faults.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-26 06:50:11 -05:00
Christopher Friedt
77135fcbc6 posix: pthread: embed posix_thread_attr inside of posix_thread
Rather than shadowing most of the fields from pthread_attr_t
(struct posix_thread_attr) inside of struct posix_thread, just
embed the structure itself.

Additionally, use pthread_attr_init() to allocate a thread
stack and pthread_attr_destroy() to destroy a thread stack,
dramatically simplifying pthread_create().

A fair bit of rework was needed already to mitigate bitrot
issues in the testsuite. More should probably be done later.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-26 06:50:11 -05:00
Christopher Friedt
5ca0f28bb2 tests: posix: common: dedicated config for static thread stacks
The testsuite has been shifted to use dynamic thread
stacks (either statically allocated via a pool, or
dynamically allocated via the heap).

However, we definitely still need coverage for manually
specified thread stacks - but lets avoid duplicating
tests unnecessarily.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-26 06:50:11 -05:00
Christopher Friedt
5eb0bbeb54 tests: posix: pthread_attr: skip when large stack allocation fails
Rather than fail when attempting to set an extraordinarily large
pthread stack size (much larger than most Zephyr platforms have)
consider the test non-fatal and skip it.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-26 06:50:11 -05:00
Adam Wojasinski
dfb6e5aa96 tests: posix: Add test cases for mq_notify()
Adds test cases testing following features:
- function error handling
- basic notification type
- thread notification type
- adding notification to non-empty queue

Signed-off-by: Adam Wojasinski <awojasinski@baylibre.com>
2024-01-25 19:47:37 +01:00
Christopher Friedt
7f57d5d6eb tests: posix: timer: run tests for realtime as well
Ensure that the realtime clock may also be used with
timer_create().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-22 06:48:55 -05:00
Christopher Friedt
0bed5dd459 tests: posix: semaphore: speed up named semaphore test
Reduce N_LOOPS from 999 to 32 by default and use a Kconfig
to encode it as CONFIG_TEST_SEM_N_LOOPS.

Running TESTSUITE semaphore
===============================================================
START - test_named_semaphore
 PASS - test_named_semaphore in 5.685 seconds
===============================================================
START - test_semaphore
 PASS - test_semaphore in 5.010 seconds
===============================================================
TESTSUITE semaphore succeeded

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-22 09:49:39 +00:00
Jai Arora
ba7b7ffff2 posix: Adds test to check getpid() is not NULL
Adds test to check getpid() function is not NULL

Signed-off-by: Jai Arora <infolinesoni@gmail.com>
2024-01-20 12:41:28 +01:00
Jai Arora
b103dece67 posix: Adds test for clock_getcpuclockid()
Adds test for clock_getcpuclockid() to
make sure clock_id is equal to CLOCK_PROCESS_CPUTIME_ID
after a successful call

Fixes #59954

Signed-off-by: Jai Arora <infolinesoni@gmail.com>
2024-01-20 12:41:28 +01:00
Christopher Friedt
18c23dec5f tests: posix: common: free resource in after() for timer suite
Create a separate ZTEST_SUITE() to properly manage resources
that are shared between test cases. Add some assertions where
they were missing.

Additionally, we don't need verbose printing in this testsuite,
so change `printk()` to `LOG_DBG()`

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-19 13:14:56 -05:00
Yong Cong Sin
c3cc2e4e6d posix: pthread: test the priority conversion functions
Made the conversion functions non-static and added ztests for
them to make sure that they work across the full range of
Zephyr priorities.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:47 +01:00
Yong Cong Sin
98a631b5bc posix: pthread: allow oldstate & oldtype to be NULL
The POSIX standard doesn't specify if the argument to store
previous state/type in
`pthread_setcancelstate`/`pthread_setcancelstate` can be
`NULL`, but threading implementations in Linux & Apache NuttX
permit the arguments to be `NULL`.

This commit changes Zephyr's implementation to mimic that of
Linux & NuttX, so that user do not get caught off-guard by
NULL pointer dereferencing when porting code over from those
OSes.

Updated test accordingly.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-17 14:42:50 +01:00
Yong Cong Sin
d4c2d4140b tests: posix: common: turn the fdtable init test into a testsuite
As there isn't any ZTEST under the `posix_apis` testsuite, its
the testsuite is skipped and its setup function never ran,
let's convert the fdtable initialization setup function into a
test of `posix_apis`.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-17 05:41:37 -05:00
Yong Cong Sin
498529ea76 tests: posix: common: split posix_apis into multiple testsuites
Currently all components live under the `posix_apis` testsuite,
this commit split them into a testsuite of their own.

This commit also prefixed a few local functions/variables with
the `static` keyword.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-17 05:41:37 -05:00
Christopher Friedt
e223d01b67 tests: posix: shorten the filter in testcase.yaml
CONFIG_NATIVE_LIBC can be used in place of
CONFIG_NATIVE_BUILD and CONFIG_EXTERNAL_LIBC

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-17 04:59:16 -05:00
Christopher Friedt
9074feabf6 tests: posix: separate suite for pthread_t and pthread_attr_t
In order to make state more manageable among the other
posix api tests, create a separate ZTEST_SUITE() called pthread
and another ZTEST_SUITE() called pthread_attr.

The tests in the pthread suite mainly operate on the pthread_t
type, while tests in the pthread_attr suite mainly operate on
the pthread_attr_t type.

The pthread_attr testsuite is moved to a separate file
(pthread_attr.c).

Having separate test suites allows us to narrow the scope of
the tests and have a better degree of control over the state.

Some effort went in to highlighting undefined behaviour at the
API level. This was quite intentional, as we will need to be
be able to test / verify that we do in fact have deterministic
outcomes when users attempt operations that result in undefined
behaviour (e.g. assert).

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-16 00:35:45 -05:00
Christopher Friedt
a2a3140a74 tests: posix: common: move barrier test to barrier.c
Move the one barrier test to its own file and create
a ZTEST_SUITE() for barrier.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-16 00:35:45 -05:00
Christopher Friedt
414868aace tests: posix: common: move condattr test to cond.c
Move posix_apis.test_pthread_condattr to cond.c .

Create a separate cond ZTEST_SUITE().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-16 00:35:45 -05:00
Dmitrii Golovanov
68d1a52417 posix: sched: Implement get APIs for scheduling parameters
Initial implementation of `sched_getparam()` and `sched_getscheduler()`
POSIX APIs as a part of PSE53 `_POSIX_PRIORITY_SCHEDULING` option group.
Both functions are actually placeholders and just return `ENOSYS`
since Zephyr does not yet support processes or process scheduling.

Signed-off-by: Dmitrii Golovanov <dmitrii.golovanov@intel.com>
2024-01-15 09:57:44 +01:00
Dmitrii Golovanov
b184fc3a62 posix: sched: Add CONFIG_POSIX_PRIORITY_SCHEDULING
Add `CONFIG_POSIX_PRIORITY_SCHEDULING` Kconfig option to select
APIs from PSE53 `_POSIX_PRIORITY_SCHEDULING` option group.

Signed-off-by: Dmitrii Golovanov <dmitrii.golovanov@intel.com>
2024-01-15 09:57:44 +01:00
Yong Cong Sin
b42e362fa8 tests: posix: pthread: add test for sigprocmask()
Refactor the existing `pthread_sigmask` test function to
get its function-under-test via arg and test `sigprocmask` with
that.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-11 07:49:26 -05:00
Yong Cong Sin
73da1e80f4 posix: signal: implement sigprocmask()
Implement `sigprocmask()` by simply redirecting call to the
`pthread_sigmask()` as they are identical.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-11 07:49:26 -05:00
Nicolas Pitre
e5b3231354 tests: posix: semaphore: assorted adjustments
- Adjust refcount checks with regards to previous commit.

- Remove redundant zassert_not_null() on local pointers after a
  sem_close(). There is no implicit reference passing in C.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2024-01-10 07:18:19 -05:00
Nicolas Pitre
cf74f22524 posix: semaphore: optimize named semaphore implementation a bit
- Regroup refcount decrement and semaphore destruction by making the
  linked state into a counted reference for it. This allows for
  simplifying the code and cleanly adding a few assertions in a common
  location.

- Remove redundant initialization to NULL on memory about to be freed
  and local pointer in nsem_cleanup().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2024-01-10 07:18:19 -05:00
Yong Cong Sin
b8662b97f2 tests: posix: semaphore: run normal semaphore test with named semaphore
Run the normal semaphore test with a named semaphore.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-10 07:18:19 -05:00
Yong Cong Sin
3bfcf123a7 tests: posix: semaphore: refactor test_semaphore
Localize a few public variables and refactor the test and
functions so that they are reusable.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-10 07:18:19 -05:00
Yong Cong Sin
abb21d48d9 posix: semaphore: implement sem_open(), sem_unlink() & sem_close()
Implements `sem_open()`, `sem_unlink()` & `sem_close()`
functions and added tests for them.

Updated existing tests and POSIX docs.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-10 07:18:19 -05:00
Jai Arora
a7c1a5f9d6 posix: Adds test case for clock_getcpuclockid function
Adds ZTEST for clock_getcpuclockid function

Signed-off-by: Jai Arora <infolinesoni@gmail.com>
2024-01-08 12:44:45 +01:00
Christopher Friedt
80e3f4aee6 tests: posix: common: rwlock: remove overspecified pthread_attr_t
Much of tests/posix/common still overspecifies pthread_attr_t
options.

Default thread attributes are perfectly fine for the vast
majority of spawned threads in this testsuite, so simply use
a NULL pthread_attr_t* argument.

This fixes piles of bugs because we have not properly used
pthread_attr_destroy() appropriately.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-04 15:13:47 -05:00
Christopher Friedt
e14f362547 tests: posix: common: sem: remove overspecified pthread_attr_t
Much of tests/posix/common still overspecifies pthread_attr_t
options.

Default thread attributes are perfectly fine for the vast
majority of spawned threads in this testsuite, so simply use
a NULL pthread_attr_t* argument.

This fixes piles of bugs because we have not properly used
pthread_attr_destroy() appropriately.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-04 15:13:47 -05:00
Christopher Friedt
90021b81d5 tests: posix: common: pthread: do not overspecify pthread_attr_t
Much of tests/posix/common still overspecifies pthread_attr_t
options.

Default thread attributes are perfectly fine for the vast
majority of spawned threads in this testsuite, so simply use
a NULL pthread_attr_t* argument.

This fixes piles of bugs because we have not properly used
pthread_attr_destroy() appropriately.

This is only a partial cleanup for pthread.c

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-04 15:13:47 -05:00
Christopher Friedt
d9cffcfc36 tests: posix: common: mutex: remove overspecified pthread_attr_t
Much of tests/posix/common still overspecifies pthread_attr_t
options.

Default thread attributes are perfectly fine for the vast
majority of spawned threads in this testsuite, so simply use
a NULL pthread_attr_t* argument.

This fixes piles of bugs because we have not properly used
pthread_attr_destroy() appropriately.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-04 15:13:47 -05:00
Christopher Friedt
bfb4a5141c tests: posix: common: mqueue: remove overspecified pthread_attr_t
Much of tests/posix/common still overspecifies pthread_attr_t
options.

Default thread attributes are perfectly fine for the vast
majority of spawned threads in this testsuite, so simply use
a NULL pthread_attr_t* argument.

This fixes piles of bugs because we have not properly used
pthread_attr_destroy() appropriately.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-04 15:13:47 -05:00
Christopher Friedt
aefbbe2788 tests: posix: common: key: remove overspecified pthread_attr_t
Much of tests/posix/common still overspecifies pthread_attr_t
options.

Default thread attributes are perfectly fine for the vast
majority of spawned threads in this testsuite, so simply use
a NULL pthread_attr_t* argument.

This fixes piles of bugs because we have not properly used
pthread_attr_destroy() appropriately.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-04 15:13:47 -05:00
Christopher Friedt
67a74e4350 tests: posix: test_realtime: improve test reliability
Previously posix_apis.test_realtime was failing (very)
frequently in CI, and in particular, when running on Qemu,
POSIX, or SMP targets.

We are using CLOCK_REALTIME for this test, which incurs an
additional syscall overhead above CLOCK_MONOTONIC. The act
of sleeping itself also incurs a syscall overhead.

The latency from one iteration to the next of the internal
loop is a bit of a random process due to scheduler or clock
noise (although the noise itself is still bounded).

In order to make this test robust against such noise, assert
only on the average time from one iteration to the next,
rather than the instantaneous time.

Rather than calculating a sample mean, use a running average
(aka Cumulative Moving Average) to save some bytes.

Report results, including low and high watermarks before
asserting that the average iteration time within expected
range.

==============================================================
START - test_realtime
I: n: 20, sleep: 100, margin: 10, lo: 110, avg: 110, hi: 110
 PASS - test_realtime in 2.198 seconds
==============================================================

Expect to see the low and high watermarks change more on
Qemu and POSIX platforms when running several jobs in parallel
with twister (such as in CI).

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-04 08:27:29 +09:00
Christopher Friedt
10156f5f1d posix: mqueue: pop mode as int with va_arg()
There was some discussion about whether it was suitable to have
an architecture-specific workaround in mqueue.c after that
workaround was copied to a different source file in a PR.

The original issue was that newlib and picolibc declare mode_t
to be unsigned short instead of unsigned long when __svr4__
is not defined along with __sparc__. This is specifically
impactful, because va_arg() deals (mainly) with 32-bit and
64-bit values that are all naturally aligned to 4 bytes.

#if defined(__sparc__) && !defined(__sparc_v9__)
#ifdef __svr4__
typedef unsigned long __mode_t;
#else
typedef unsigned short __mode_t;
#endif

A uint16_t is naturally aligned to 2 bytes, so not only would
a 16-bit mode_t be corrupted, it would also generate a warning
with recent gcc versions which is promoted to error (rightfully
so) when run through CI.

mqueue.c:61:35: error: 'mode_t' {aka 'short unsigned int'} is
  promoted to 'int' when passed through '...' [-Werror]
   61 |                 mode = va_arg(va, mode_t);

Instead of using an architecture-specific workaround, simply
add a build assert that the size of mode_t is less than or
equal to the size of an int, and use an int to retrieve it
via va_arg().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Fri Dec 29 10:06:44 2023 -0500
#
# On branch posix-mqueue-always-use-int-for-mode-t-va-arg
# Changes to be committed:
#	modified:   lib/posix/mqueue.c
#	modified:   tests/posix/common/testcase.yaml
#
2024-01-01 16:36:31 -05:00
Christopher Friedt
1f67dcbd89 posix: remove deprecated non-standard PTHREAD_MUTEX_DEFINE, etc
Remove the previously deprecated and non-standard macros
* PTHREAD_MUTEX_DEFINE()
* PTHREAD_COND_DEFINE()

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-28 03:40:57 -05:00
Christopher Friedt
b702c8e8f0 tests: posix: pthread: test that big stacks can be allocated
In some cases, users want to allocate (comparatively) massive
thread stacks.

Add a test to ensure we can allocate such a stack by default.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-23 22:29:41 -05:00
Christopher Friedt
5c78c66fc2 tests: posix: headers: check for pthread get / set guardsize
Add checks to ensure that pthread_getguardsize() and
pthread_setguardsize() are callable.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-20 14:20:58 +00:00
Christopher Friedt
5e245bd42e tests: posix: tests for pthread_getguardsize() pthread_setguardsize()
Add tests for pthread_getguardsize() and pthread_setguardsize().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-20 14:20:58 +00:00
Christopher Friedt
746a3b943d tests: posix: use consistent min stack sizes
* Use PTHREAD_STACK_MIN as provided by the specification
  instead of a hard-coded value of 1024
* add extra stack size to semaphore tests

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-20 14:20:58 +00:00
Christopher Friedt
04c9903055 posix: pthread_once: simplify and reduce size of pthread_once_t
Since pthread_once() is both the initializer and executor of
pthread_once_t, it can have maximally two states. Since the
implementation in Zephyr previously aimed to maximize libc
compatibility, we opted to use the definition of pthread_once_t
from newlib, which is a structure with two ints.

It does not make sense to use 64 bits to manage 2 possible
states. The control for that should effectively be a bool.

We maintain compatibility with newlib by asserting (at build
time), that newlib's pthread_once_t is larger than Zephyr's
new struct pthread_once (which just contains a bool).

This allows us to delete the non-standard pthread_key.h
header file (finally).

Reuse the pthread_pool_lock in order to synchronize the related
init function (so that it is only called maximally once from any
thread). The spinlock is only used to test the state and the
init function is not called with the spinlock held.

The alternative was to use an atomic inside of
struct pthread_once. But again, that could be up to 64-bits with
Zephyr's atomics implementation.

Ideally we would use C11 generics or something to support atomics
on 8, 16, 32, and 64-bit primitives.

Lastly, also update declarations for C11 threads as they mostly
mirror our pthread implementation.

This needed to be done as a single commit in order to ensure
continuity of build.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-14 09:32:58 +01:00
Christopher Friedt
4153a396f8 tests: posix: headers: enable tests for signal.h entities
Enable existsence checks for newl(-ish)ly added signal.h
definitions.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-04 20:49:35 -05:00
Christopher Friedt
c2ab4020b0 tests: posix: pthread: add test for pthread_sigmask()
Add a test for pthread_sigmask().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-04 20:49:35 -05:00
Christopher Friedt
e867c6f36d tests: posix: pthread: use MIN instead of min
MIN() has been defined in sys/util.h for a long time. It doesn't
make sense to separately define a lowercase version.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-04 20:49:35 -05:00