Commit Graph

770 Commits

Author SHA1 Message Date
Andreas Schneider 4e8a7cd581 cmake: Add more compiler warnings
See https://fedoraproject.org/wiki/Changes/PortingToModernC
2023-02-01 08:38:44 +01:00
Andreas Schneider 2df2d3a3ce cmake: Use C99 and define GNU and POSIX flags directly at source files
Fixes #50
2023-02-01 08:38:44 +01:00
Andreas Schneider 91b66f83c8 example: Fix pos value as snprintf returns an int 2023-02-01 08:38:44 +01:00
Andreas Schneider a6b7cbca1c include: Improve expect_check() documentation 2023-02-01 08:38:44 +01:00
Andreas Schneider 5e3411c7b9 tests: Mark state in some tests as unused 2023-02-01 08:38:44 +01:00
Andreas Schneider 99b7572239 tests: Make test_expect_check more robust 2023-02-01 08:38:44 +01:00
Andreas Schneider 57d2b8d207 Improve INSTALL.md
Fixes #63
2023-01-29 14:42:25 +01:00
Andreas Schneider 10569355fd doc: Link to the examples for mocking
Fixes #65
2023-01-29 14:32:42 +01:00
Andreas Schneider 6c021b6142 include: Improve documentation
Fixes #79
2023-01-29 14:32:42 +01:00
Andreas Schneider 23f1a46053 tests: Add tests for will_return/mock with ptr and type saftey checking 2023-01-29 14:32:42 +01:00
Andreas Schneider 5ad69f2067 cmocka: Add some kind of type safety for will_return and mock
Fixes #75
2023-01-29 14:28:20 +01:00
Andreas Schneider 5f1159a9ad tests: Add tests for new will_return and mock macros 2023-01-29 13:49:16 +01:00
Andreas Schneider 7a7653ca12 include: Add specific will_return and mock macros for different types 2023-01-29 13:49:14 +01:00
Andreas Schneider c21515239b include: Add assign_(int|uint|double|ptr)_to_cmocka_value() macros
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 11:16:59 +01:00
Andreas Schneider 8bea210101 include: Move values in CMockaValueData union around 2023-01-29 10:38:21 +01:00
Alois Klink 018bd67ad4 test: Test assert_ptr_equal and _not_equal macros
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:42 +01:00
Alois Klink a9cf98826b include: Remove cast_to_uintptr_type macro
No longer required, as CMocka now never converts pointers to intergers.

BREAKING CHANGE: The cast_to_uintptr_type macro has been removed.

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:42 +01:00
Alois Klink 4e2e7c16d0 include: Remove cast_ptr_to_uintmax_type macro
No longer required, as CMocka can now store pointers directly in
a CMockaValueData without casting pointers to `uintmax_t`
(not safe on some platforms).

BREAKING CHANGE: The cast_ptr_to_uintmax_type() macro has been removed.

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:42 +01:00
Alois Klink 09621179af cmocka: Improve pointer assert functions
Use pointer types for assert_* pointer functions, instead of casting
to uintmax_t.

On some platforms, casting pointers to uintmax_t loses information.

As an additional benefit, we can now use the `%p` specifier to print
pointers. On GCC, this will print (nil) for NULL pointers.

BREAKING CHANGE: The `assert_{not_,}null` and `assert_ptr_{not_,}equal`
                 functions may now throw `Wint-conversion` warnings
                 when used with non-pointer types.
on-behalf-of: @nqminds <info@nqminds.com>

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:42 +01:00
Alois Klink 079391e485 test: Test expect_string and expect_memory
`expect_string` already had tests in the `example/` folder,
but `expect_memory`, `expect_not_memory`, and `expect_not_string`
were previously untested.

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:41 +01:00
Alois Klink 611d089f5e cmocka: Change `_mock` to return CMockaValueData
Change the `_mock()` function to return CMockaValueData.

Additionally, the `will_return_ptr*` functions now must be used
for pointer types (we can't use C11 _Generic since CMocka still
uses C99).

BREAKING CHANGE: `will_return*` may throw compiler errors/warnings
                 when used with pointers. Please use the new
                 `will_return_ptr*` macros instead.
on-behalf-of: @nqminds <info@nqminds.com>

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:41 +01:00
Alois Klink d5ccd8844d cmocka: Make expect_check to use CMockaValueData
Change all `expect_check*()` macros to use CMockaValueData
internally, instead of `uintmax_t`.

This breaks ABI compatibility on some platforms.

The API for most macros is the same, but the `expect_check()` and
`expect_check_count()` macros now need to be called with the new
`CMockaValueData` type.

BREAKING CHANGE: `expect_check()` and `expect_check_count()` now
                 use the type CMockaValueData in the check function and
                 check data.
on-behalf-of: @nqminds <info@nqminds.com>

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:41 +01:00
Alois Klink ed92933525 include: Add cast_ptr_to_cmocka_value() macro
This macro is used to cast a `void *` pointer to a CMockaValueData.

Unlike `cast_ptr_to_uintmax_type()`, this macro uses implicit type
casts, so the compiler may warn/error if passing an incompatible type.

on-behalf-of: @nqminds <info@nqminds.com>

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:41 +01:00
Alois Klink e99e764e59 include: Add cast_int_to_cmocka_value() macro
This macro is used to cast data as an integer to a `CMockaValueData`.

For backwards compatibility reasons, this explicitly casts to
`uintmax_t`, just like the `cast_to_uintmax_type()` macro.
For most compilers, this will supress warnings about passing
float/intmax_t to this macro.

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:41 +01:00
Alois Klink 0acfe428ed include: Add CMockaValueData typedef
Currently, CMocka stores pointers by converting them to uintmax_t
via uintptr_t. However, on some platforms this doesn't work
(casting pointers to uintptr_t is fine, but not going to uintmax_t).

The `CMockaValueData` typedef will be used to allow storing multiple
types of value in CMocka functions without using undefined behavior.

It is currently defined as:

```c
typedef union {
  /** Holds integral types */
  uintmax_t uint_val;
  /** Holds pointer data */
  const void *ptr;
  // The following aren't used by CMocka currently, but are added to
  // avoid breaking ABI compatibility in the future
  /** Holds function pointer data */
  void *(*func)(void);
  /** Holds signed integral types */
  intmax_t int_val;
  /** Holds real/floating-pointing types*/
  double real_val; // TODO: Should we use `long double` instead
} CMockaValueData;
```

I wanted to add `long double` to this union as well, as even if it's
not used now, in would be nice to add now to avoid breaking the ABI
in the future (`long double` can be 64-,80-, or 128-bits wide), but
GCC prints an ugly warning since GCC 4.4 (from 2009) changed their libc
ABI on `long double`s in `union`s.

`intmax_t` is added just in case any platform has a smaller `intmax_t`
than `uintmax_t` (e.g. maybe one's complement platforms like UNISYS).

on-behalf-of: @nqminds <info@nqminds.com>

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:41 +01:00
Alois Klink 9179fdcb74 src: disable casting integers to (void *)
Disable casting integers to and from (void *) in free_symbol_map_value.

Instead, we can just use a pointer to an integer.

Casting an integer to (void *) is implementation-defined,
unless that integer was previously casted from `*intptr_t` from a
valid pointer.

on-behalf-of: @nqminds <info@nqminds.com>

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:41 +01:00
Alois Klink bfdf78c4e6 cmake: disable `-Wdeclaration-after-statement`
Disable `-Wdeclaration-after-statement`.

Before ISO C99, declaring variables after statements was not allowed
by the C standard, but was allowed by GCC as an extension.

CMocka now requires a minimum version of C99,
since commit 5a4b15870e, so this
behaviour is valid on all compilers.

on-behalf-of: @nqminds <info@nqminds.com>

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:41 +01:00
Alois Klink baf224881e src: set MALLOC_ALIGNMENT to `16`
Hard-code the default `MALLOC_ALIGNMENT` to `16`.

On most non-Windows x86_64 platforms, the default alignment should be
16-bytes, as `long double` is an 80-bit large floating point number.

C11 has a built-in that's perfect for this, `alignof(max_align_t)`, but
unfortunately, cmocka only supports C99.

Unfortunately, we can't use `sizeof(long double)`, because on some
platforms, it returns 12, which is not a power of 2. A hard-coded value
of 16 may be over-aligned on some platforms, but there's no downside
to this, other than using up more RAM than normal.

on-behalf-of: @nqminds <info@nqminds.com>

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-29 10:35:41 +01:00
Chris Schick 43b42c598e Wrapped test and example build in project test
- Checks to see if CMocka is the top project
- Does not build tests or examples if consumed as a dependency through
  FetchContent

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-25 11:46:56 +01:00
Andreas Schneider 77c8555580 cmake: Do not use CMAKE_(BINARY|SOURCE)_DIR for compile_commands.json
This should fix being imported by other projects.

Fixes #85
2023-01-24 18:07:27 +01:00
Xiang Xiao 092e5fb85f cmocka.c: Reduce the call stack consumption of printf
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: Idf179ad5eb81bbc63eea4f71980857831668e7a8

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2023-01-09 14:28:49 +01:00
Alois Klink 0559ef6e8e src: use __builtin_align_down, if available
In _test_malloc, we manually align a pointer to MALLOC_ALIGNMENT
using `& ~(MALLOC_ALIGNMENT - 1)`. However, this has two problems:

1. We're casting the pointer to `size_t`, which isn't guaranteed
   to hold a pointer. Instead, we should cast to `uintptr_t`.
2. Modifying a pointer as a integer is undefined behavior, and on
   some platforms (e.g. CHERI), this does not work.

C++11 has std::align that does this for us, but unfortunately, there
isn't a way to do this in ISO C that is guaranteed to work, except for
in Clang v10+, which has a builtin extension called
__builtin_align_down that can align pointers safely for us.

See:
https://clang.llvm.org/docs/LanguageExtensions.html#alignment-builtins
on-behalf-of: @nqminds <info@nqminds.com>

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2022-12-23 15:21:16 +01:00
Alois Klink 426784f405 build: improve AddCMockaTest `LINK_OPTIONS` docs
The `LINK_OPTIONS` argument for `add_cmocka_test()` doesn't
set the `LINK_OPTIONS` CMake property, as it was only added
in CMake 3.13.

These options are mostly the same, but some options that are valid
`LINK_OPTIONS` are not valid `LINK_FLAGS` (e.g using `LINKER:` prefixes)

This commit documents that the `LINK_OPTIONS` argument instead
is used to set the `LINK_FLAGS` CMake property.

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2022-12-23 11:53:27 +01:00
Alois Klink 57234cdef3 cmocka: fix setjmp in expect_assert_failure macro
Assigning the result of `setjmp` to a variable is technically
undefined behavior in the C standard.

Most platforms allow this (NetBSD even uses this syntax in their tests)
and GCC/Clang doesn't even give us a warning. Nonetheless, we should
follow the ISO C specification.

See [§ 7.13.1.1.4 of the C99 spec.][1]

[1]: https://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf
on-behalf-of: @nqminds <info@nqminds.com>

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2022-12-23 11:32:08 +01:00
Alois Klink b78067e0c1 src: use value=`1` when calling longjmp()
Currently, `longjmp()` is always called with value=`0`.

According to the ISO C standard, calling longjmp with value 0
just acts like it is called with value 1.

However, some BSD platforms are buggy, and instead make setjmp
return 0 and cause an infinite loop. See [NetBSD PR/56066][1].

Calling `longjmp` with value 1 makes the meaning more clear as well.

[1]: https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=56066
on-behalf-of: @nqminds <info@nqminds.com>

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2022-12-23 11:32:08 +01:00
Alois Klink ac10ea33fd cmocka: fix `float` in assert_double_not_equal
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2022-12-21 12:41:06 +01:00
maxime 0bb27c3d67 Fixed warning when compiling on Windows
warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead,
use the ISO C++ conformant name: _strdup. See online help for details.
2022-12-01 19:50:16 +01:00
Biswapriyo Nath 08853ca354 cmake: Fix path relocation in pkgconfig file for mingw
This fixes path relocation in mingw environment by using predefined
Libs and Cflags variable in pkgconfig file. Otherwise, libdir and
includedir are not shown in pkgconf output. e.g.
* Without predefined variables:
  - pkgconf -cflags cmocka: No output
  - pkgconf -libs cmocka: -lcmocka
* With predefined variables:
  - pkgconf -cflags cmocka: -IC:/msys64/mingw64/include
  - pkgconf -libs cmocka: -LC:/msys64/mingw64/lib -lcmocka

Also official documentation suggests to use predefiend keywords here
https://people.freedesktop.org/~dbn/pkg-config-guide.html

Signed-off-by: Biswapriyo Nath <nathbappai@gmail.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2022-12-01 19:16:08 +01:00
Andreas Schneider 7057bba5b2 cmocka: Only handle exceptions on platforms supporting it
This also issues a warning during compile time.
2022-12-01 18:49:47 +01:00
Alois Klink cf1aef204d cmake: build cmocka with C_EXTENSIONS set to ON
When compiling code with CMake [`C_EXTENSIONS` `OFF`][1],
(e.g. using `-std=c11` instead of `-std=gnu11`), only standard
ISO C is available in C headers like `<setjmp.h>`.

Because cmocka uses POSIX extension functions, we need to set
`_POSIX_C_SOURCE` to the appropriate level before including these
headers. On gcc/clang, this is enabled by default when C_EXTENSIONS ON.

Fixes https://gitlab.com/cmocka/cmocka/-/issues/50, e.g. using cmocka
from a project that has C_EXTENSIONS OFF.

[1]: https://cmake.org/cmake/help/latest/prop_tgt/C_EXTENSIONS.html

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
2022-11-30 09:57:01 +01:00
Andreas Schneider defe7b4fdc coverity: Update for new functions 2022-11-28 13:31:18 +01:00
Andreas Schneider cf95fdd268 cmocka: Deprecate assert_in_set 2022-11-28 13:31:16 +01:00
Andreas Schneider e8c111dd73 tests: Add tests for assert_uint_not_in_set() 2022-11-28 13:25:03 +01:00
Andreas Schneider 15dcc205a7 cmocka: Add assert_uint_not_in_set() 2022-11-28 13:22:05 +01:00
Andreas Schneider f79c13de4d tests: Add tests for assert_uint_in_set() 2022-11-28 13:22:05 +01:00
Andreas Schneider a86cb89a84 cmocka: Add assert_uint_in_set() 2022-11-28 13:22:05 +01:00
Andreas Schneider 81fe84678b tests: Add tests for assert_not_int_in_set() 2022-11-28 13:22:05 +01:00
Andreas Schneider 74b9bf799e cmocka: Add assert_int_not_in_set() 2022-11-28 13:22:05 +01:00
Andreas Schneider 479d149adc tests: Add tests for assert_int_in_set() 2022-11-28 13:22:05 +01:00
Andreas Schneider 18aa5af612 cmocka: Add assert_int_in_set() 2022-11-28 13:22:05 +01:00