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>
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>
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>
`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>
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>
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>
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>
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>
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>
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>
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>
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>
- 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>
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>
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>
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>
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>
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>
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>