Commit graph

210 commits

Author SHA1 Message Date
Alberto Escolar Piedras 320f23ef96 arch posix fuzzing: Move kconfig options to sample
Move the kconfig options used to configure the interrupt
and wait time to the sample which uses them instead of
having them in the architecture code.
This options are very particular for this sample and not
really an API.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-05-02 20:46:03 +03:00
Alberto Escolar Piedras df5440fc59 arch posix: Correct ARCH_POSIX_LIBFUZZER kconfig help message
This option can be used now also with native_sim
and seems to work fine with both 32 and 64 bit targets.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-05-02 20:46:03 +03:00
Alberto Escolar Piedras f5553004b0 sample fuzzer: Move fuzzer specific code to sample and fix for native_sim
Move the LLVM fuzzing specific code out of the board main
file and into the sample.
That way we avoid needing to duplicate it for native_sim and
avoid having a very adhoc interface between the fuzzer test
and runner code.

Also ensure it works for native_sim and not just native_posix

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-05-02 20:46:03 +03:00
Patryk Duda 4fe5ac9248 arch: posix: Undefine operating system specific macros for native_sim
Compilers predefine system-specific macros which carry information about
compiler, target architecture and operating system. It provides basic
compiler-dependent information like size of types, their maximal and
minimal values, etc. It allows to write common libc headers for multiple
architectures and operating systems.

These macros allow code to always determine what is the target operating
system. This is a problem when compiling code of modules that supports
multiple operating systems (e.g. cryptography libraries).

To avoid confusion we shouldn't leak host operating system macros (e.g.
__linux__, __linux, linux, etc.) when compiling for native_sim board.

Unfortunately, there is no single universal switch that disables all
operating system macros:
- '-undef' removes also architecture-related macros
- '--target' is only available for Clang compiler

This patch uses '-include' option to include file that undefines all
well-known operating system macros.

Run 'gcc -dM -E - < /dev/null | sort' to get full list of predefined
macros.

Signed-off-by: Patryk Duda <patrykd@google.com>
2024-04-30 14:30:30 -04:00
Pieter De Gendt ff6985766b arch: posix: Select at least C11 standard
Replace the global CSTD property with the CSTD kconfig option to select
at least C11 standard.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2024-04-25 09:54:39 +00:00
Alberto Escolar Piedras 4f7b144ef6 arch posix: When building for the native_simulator only link ASAN once
Only request the linker to link ASAN in the final stage, not
during the partial linking stage.
This fixes a link issue when building with llvm.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-04-12 15:03:35 +02:00
Alberto Escolar Piedras b59b21f8bb arch posix: pass -fsanitize-recover=all also to native_simulator build
If the CONFIG_ASAN_RECOVER option is set, also pass
-fsanitize-recover=all to the build of the native simulator
built files.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-04-12 15:03:35 +02:00
Alberto Escolar Piedras 7b8664c3f9 arch posix: Clarify some references to native_posix
Some old references to native_posix are better
refering to the native targets in general,
or being clarified as only applying to the old
native_posix like targets.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-03-20 15:43:05 +01:00
Daniel Leung dadbe105f2 posix: tracing for custom thread abort func
POSIX has a custom implementation for thread abort
(z_impl_k_thread_abort)	which lacks the	tracing	function
calls as in the	generic	version. So add	them.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2024-03-19 13:37:41 -05:00
Alberto Escolar Piedras bb18a30f89 arch/posix cmake: Replace native_posix w native_sim and use hwmv2 names
Let's fix the error messages:
* native_posix is in general replaced with native_sim
* let's use hwmv2 names

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-03-15 16:13:12 +01:00
Alberto Escolar Piedras ae358704d2 docs/Kconfig help: Replace native_[posix,sim]_64
Replace uses of native_[posix,sim]_64 in the documentation
and kconfig help messages with native_[posix,sim]/native/64
to match the hwmv2 naming.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-03-15 16:13:12 +01:00
Alberto Escolar Piedras 346997f8be cmake: Apply COMPILER_WARNINGS_AS_ERRORS also to nativesim runner
When COMPILER_WARNINGS_AS_ERRORS is set, let's also have the
native simulator runner (and anything Zephyr requests built with it)
be built with the same option to treat warnings as errors.
Otherwise we will miss those by default in CI.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-01-29 14:27:27 +01:00
Alberto Escolar Piedras ca68ef7e5a arch posix: Do not define standard SOURCE macros globally
Do not define these macros globally, but instead
define them only for this library and when needed.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-01-26 07:48:55 -05:00
Keith Packard 3cc4c5eb7a posix: Use _POSIX_C_SOURCE=200809L instead of 200809
This constant is supposed to be defined as a long instead of an int,
presumably to support systems where int isn't large enough.

Signed-off-by: Keith Packard <keithp@keithp.com>
2024-01-26 07:48:55 -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
Alberto Escolar Piedras 210bd28a24 native_simulator: Allow to pass extra options for localizing symbols
Some libraries (like Openthread's spinel code) define their
API as externally linkable. This will make those symbols
remain as externally linkable by default after the
Zephyr build has produced the native simulator library
(MCU code).
When building an AMP native_simulator executable with
several MCUs each including these, the linker will see
those symbols as still linkable and duplicated, and
throw an error.

So let's give the option for users/developers of those
libraries to define extra symbols they want
to localize before assembling the final executable.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-12-06 09:25:41 +00:00
Alberto Escolar Piedras ab896ad6ef arch posix: annotate posix_exit and nsi_exit as noreturn
Annotate posix_exit() and nsi_exit() as noreturn
mainly to ease the life of static analysis tools.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-11-22 09:52:52 +01:00
Grant Ramsay a3ff19a39e cmake: compiler: Add compiler property for no-builtin
Abstracts these flags for multiple toolchain support

Signed-off-by: Grant Ramsay <gramsay@enphaseenergy.com>
2023-11-13 10:21:41 +01:00
Gerard Marull-Paretas 691facc20f include: always use <> for Zephyr includes
Double quotes "" should only be used for local headers.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2023-09-14 13:49:58 +02:00
Anas Nashif 1fd3171f7b arch: remove wait_q.h include
This include is not used directly in those files, so remove it.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-09-12 12:55:36 -04:00
Alberto Escolar Piedras b021c7611d native simulator: Add property to collect libraries to link w runner
Add a property to the native_simulator target, to collect
the libraries we want to link with the runner, instead of
abusing the link options to pass them.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-09-04 13:16:58 +02:00
Alberto Escolar Piedras 232b4b0a7a native_sim: Define path to native_simulator on arch cmake files
To allow reusing the path between boards,
instead of defining it in the board, let's define it in
the arch cmake file.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-08-24 15:40:00 +02:00
Alberto Escolar Piedras d4e48d5feb POSIX arch: Fix literal floating comparison in 32bit targets
When building the 32bit native board targets variants
for x86(-64) hosts, gcc will promote float literals to double
(See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92875 )

This can result in unexpected comparison differences.

This is due to the compiler using the 8087 float mode by
default.
Instead let's tell the compiler to use the SSE float path,
which is the default for 64 bit x86-64 builds.

The assumption that any x86 host used for development
will have SSE support should be safe enough.

For more background see
https://github.com/zephyrproject-rtos/zephyr/issues/61345

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-08-14 13:37:27 +00:00
Jonathan Rico 05cc2f37a3 arch: posix: break debugger on fatal error
Add option to raise a SIGTRAP on fatal error, making the debugger break
instead of exiting.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
2023-08-04 14:15:19 +02:00
Alberto Escolar Piedras c044b2e916 posix arch: Fix very rare segfault on program termination
In some very rare cases (< 1/1000 runs), in very loaded machines,
a race in the glibc pthread_cancel() seems to be triggered.

In this the cancelled thread cleanup overtakes the pthread_cancel()
code, and frees the pthread structure before pthread_cancel()
has finished, resulting in a dereference into already
free'd memory, and therefore a segfault.
Calling pthread_cancel() during cleanup is not required beyond
preventing a valgrind memory leak report (all threads will be
stopped immediately on exit).
Therefore we stop doing this, to avoid this very rare crashes.

This issue was reproduced in Ubuntu 22.04, with its default
gcc 11.3.0 and glibc 2.35.
The issue may also have been seen very rarely in Zephyr's CI.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-07-27 17:02:36 +02:00
Michael Zimmermann 6168f47ac3 arch/posix: put fuzzing kconfigs into submenu
Without fuzzing enabled they don't do anything and should not be
selectable.

Signed-off-by: Michael Zimmermann <sigmaepsilon92@gmail.com>
2023-07-18 19:19:53 +00:00
Alberto Escolar Piedras ee268a6d4b posix arch cmake: Add clarification on use of target_link_options
Clarify why we use target_link_options() instead of
target_link_libraries()

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-07-10 09:51:25 +02:00
Alberto Escolar Piedras 265010a4b6 native boards: Make native simulator host trampolines avaliable to all
To ease writing common drivers, let's make the host trampolines
from the native simulator avaliable to all posix based boards.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-07-07 14:32:41 +02:00
Alberto Escolar Piedras dadacfaac2 soc (native): Use nce directly from native_simulator
Instead of keeping a replica of the nce code,
now that the native_simulator is in tree, let's use
it directly.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-07-07 12:54:19 +02:00
Alberto Escolar Piedras 2096006cd8 POSIX arch: Give a nicer error message if built in unsuported platform
Instead of failing badly later, let's give a clear error
message if the user tries to build in an unsupported platform.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-07-06 14:46:48 +02:00
Alberto Escolar Piedras ece74b7b7f native_sim: Add new native_sim board
Add the new native_sim board, based on the native simulator.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-07-05 07:01:19 -04:00
Alberto Escolar Piedras 945429e8c3 arch POSIX: provide a bottom which uses natsim's thread emul
This allows building with embedded libCs in the Zephyr side,
as the POSIX arch bottom is not anymore built in Zephyr context.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-07-05 07:01:19 -04:00
Alberto Escolar Piedras 41a185a1b5 arch POSIX: Use posix cheats only for native apps
native libraries do not need it

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-07-05 07:01:19 -04:00
Alberto Escolar Piedras 7ee41b8776 soc inf (native): Refactor into a top and bottom
Where the bottom is the only one which interacts with
the host operating system.
And the top the only one that interacts or is aware
of the hosted operating system (Zephyr).

The bottom uses the native simulator CPU
start/stop emulation.
By now we replicate its code as a provisional measure,
until the native simulator becomes standard.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-06-23 12:16:26 +02:00
Alberto Escolar Piedras bd66c1d953 arch POSIX: Refactor into a top and bottom
Where the bottom is the only one which interacts with
the host operating system, and does not necessarily
need to know about the Zephyr OS.
This is in preparation for the native simulator,
which which the bottom is also fully Zephy agnostic.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-06-23 12:16:26 +02:00
Alberto Escolar Piedras 78e1d6fa5e arch posix: Set C standard version in arch instead of boards
The issue due to which we set the default compiler C std version
to C11 is not specific to any particular POSIX arch board,
but to all. Instead of setting this property for each board,
let's set it at the architecture level.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-06-23 08:59:21 +02:00
Keith Packard 6ccdf47f60 lib/cpp: Remove CONFIG_CPP_MAIN
With Zephyr now always using `int main(void)`, there's no longer any need
for this definition. The last remaining use which gated the declaration of
_posix_zephyr_main isn't necessary as adding that declaration
unconditionally is harmless.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-31 06:16:35 -04:00
Alberto Escolar Piedras 926def6510 arch posix: posix_cheats.h main() type changed in comment
Align the comment with the new int main(void) convention
used in Zephyr.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-04-14 09:55:48 +02:00
Peter Mitsis 66af4f443d arch: posix: Remove unused offset symbols
Removes unused offset symbols under the POSIX architecture.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2023-02-23 16:44:07 +01:00
Stephanos Ioannidis ae0437d2df arch: posix: Declare _posix_zephyr_main with int return type
This commit updates the `_posix_zephyr_main` declaration to use the
return type of `int` instead of `void` when `CONFIG_CPP_MAIN=y` (i.e.
C++-compliant main() support is enabled) so that Zephyr applications
defining their main() in a C++ source file can make use of the proper
main() definition of `int main(void)` as required by the C++ standard.

Note that the forward declaration of `_posix_zephyr_main` is required
if and only if the main() is defined in a C++ source file (i.e. when
`CONFIG_CPP_MAIN=y`).

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2022-11-05 16:41:45 +09:00
Pieter De Gendt 1694c0d60d arch: posix: Add defaults for recommended stack sizes
The CONFIG_STACK_SENTINEL adds 4 bytes to the stack. Take these
into account for CONFIG_ARCH_POSIX_RECOMMENDED_STACK_SIZE.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2022-10-11 15:22:57 +02:00
Andy Ross 99dd845067 arch/posix: Fix main() renaming trickery
It turns out that SOF is already using a symbol named
"zephyr_app_main()", so this produces a collision.  Pick something
that looks more relevant to "posix", and put an underscore on it (it's
a "system" symbol, after all).

Signed-off-by: Andy Ross <andyross@google.com>
2022-09-15 16:23:11 +00:00
Andy Ross ec44bc435c arch/posix: Fix 32 bit x86 fuzzing
It seems like libfuzzer wants to relocate 32 bit instrumented code
sections at runtime at addresses different than the ones in the ELF
file.  This is problematic, because Zephyr files are compiled
statically and so will crash the first time they try to jump to an
absolute .text address (basically at the first function call after a
fuzzer entry point).

It seems that building with -fPIC is enough to defeat this (we use the
host linker script, which will manage the GOT/PLT entries for us),
which will work as long as the fuzzer isn't playing games with data
other than text.  None of this seems to be documented, so... I guess
it's as good as we can get.  It works, at least.

(x86_64 binaries don't show the same behavior, they run where they
were linked)

Signed-off-by: Andy Ross <andyross@google.com>
2022-09-15 16:23:11 +00:00
Andy Ross 65d657685e arch/posix: Add libfuzzer support
Add support for LLVM's libfuzzer utility.  This works by building an
executable with a "LLVMFuzzerTestOneInput()" entry point (which is
external to Zephyr, running in the host process environment!), which
it drives out of its own main() routine.  The toolchain API is exposed
as just another sanitizer variant, which is clean.

Signed-off-by: Andy Ross <andyross@google.com>
2022-08-26 11:57:46 +02:00
Andy Ross 02b23f3733 arch/posix: Add MemorySanitizer support
Wire this up the same way ASAN works.  Right now it's support only by
recent clang versions (not gcc), and only in 64 bit mode.  But it's
capable of detecting uninitialized data reads, which ASAN is not.

This support is wired into the sys_heap (and thus k_heap/k_malloc)
layers, allowing detection of heap misuse like use-after-free.  Note
that there is one false negative lurking: due to complexity, in the
case where a sys_heap_realloc() call is able to shrink memory in
place, the now-unused suffix is not marked uninitialized immediately,
making it impossible to detect use-after-free of those particular
bytes.  But the system will recover cleanly the next time the memory
gets allocated.

Also no attempt was made to integrate this handling into the newlib or
picolibc allocators, though that should hopefully be possible via
similar means.

Signed-off-by: Andy Ross <andyross@google.com>
2022-08-19 08:30:01 +02:00
Andy Ross 74cc534758 cmake: Update CONFIG_ASAN support
This had bitrotten a bit, and didn't build as shipped.  Current
libasan implementations want -fsanitize=address passed as a linker
argument too.  We have grown a "lld" linker variant that needs the
same cmake treatment as the "ld" binutils one, but never got it.  But
the various flags had been cut/pasted around to different places, with
slightly different forms.  That's really sort of a mess, as sanitizer
support was only ever support with host toolchains for native_posix
(and AFAICT no one anywhere has made this work on cross compilers in
an embedded environment).  And the separate "gcc" vs. "llvm" layers
were silly, as there has only ever been one API for this feature (from
LLVM, then picked up compatibly by gcc).

Pull this stuff out and just do it in one place in the posix arch for
simplicity.

Also recent sanitizers are trying to add instrumentation padding
around data that we use linker trickery to pack tightly
(c.f. SYS_INIT, STRUCT_SECTION_ITERABLE) and we need a way
("__noasan") to turn that off.  Actually for gcc, it was enough to
just make the records const (already true for most of them, except a
native_posix init struct), but clang apparently isn't smart enough.

Finally, add an ASAN_RECOVER kconfig that enables the use of
"halt_on_error=0" in $ASAN_OPTIONS, which continues execution past the
first error.

Signed-off-by: Andy Ross <andyross@google.com>
2022-08-19 08:30:01 +02:00
Anas Nashif 98ab67d7dc scripts: move user_wordsize.py to scripts/build/user_wordsize.py
Move scripts needed by the build system and not designed to be run
individually or standalone into the build subfolder.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-07-12 10:03:45 +02:00
Fabio Baltieri e24314f10f include: add more missing zephyr/ prefixes
Adds few missing zephyr/ prefixes to leftover #include statements that
either got added recently or were using double quote format.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2022-05-27 15:20:27 -07:00
Gerard Marull-Paretas 16811660ee arch: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all arch code to the new
prefix <zephyr/...>. Note that the conversion has been scripted, refer
to zephyrproject-rtos#45388 for more details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-06 19:57:22 +02:00
Anas Nashif 399a0b4b31 debug: generate call graph profile data using gprof
This will generate profile data that can be analyzed using gprof. When
you build the application (currently for native_posix only), after
running the application you will get a file "gmon.out" with the call
graph which can be processed with gprof:

  gprof build/zephyr/zephyr.exe gmon.out > analysis.txt

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-04-22 16:04:08 -04:00