Current FunctionMockLib is relying on the subhoob module to support the
backend operation by bitbanging the binary post-disassembly.
However subhook module is a x64 centric module and does not support
AArch64 usage.
This change removes the mock function support for AArch64. The
functionality will need other solutions to be properly supported.
Signed-off-by: Kun Qin <kun.qin@microsoft.com>
This change adds the support of float operations for AArch64 host based
unit tests by overriding the compiler flags.
Signed-off-by: Kun Qin <kun.qin@microsoft.com>
AArch64 does not generate divide-by-zero exceptions like x86. Yet the
current generate exception test is using this operation to attempt
triggering exceptions.
This change abstracted the exception generation logic into per-arch
files and use undefined instruction to trigger AArch64 exceptions.
Signed-off-by: Kun Qin <kun.qin@microsoft.com>
The SampleGoogleTestGenerateException GoogleTest sample previously
generated a CPU exception by performing an integer divide by zero. That
mechanism is X86-specific: the AArch64 architecture defines SDIV/UDIV by
zero to return zero rather than raising a synchronous abort, so the
sample produced no exception when built and run on AArch64 hosts and the
test failed due to the test assert instead of triggering an exception
through the framework.
This change replaces the divide-by-zero with a NULL pointer write
performed through a small volatile helper, which is expected to work on
all host systems.
The MSVC '/wd4723' build option is no longer needed because it was added
to silence the divide-by-zero compile-time warning.
Signed-off-by: Kun Qin <kun.qin@microsoft.com>
AArch64 does not have divide-by-zero exception.
This change modifies the corresponding test to work with AArch64.
Signed-off-by: Kun Qin <kun.qin@microsoft.com>
In preparation for the MarkdownLintCheck plugin being added to the
repo, this change defaults the plugin to `AuditOnly` mode in each
package. This allows package maintainers to enable the plugin as they
see fit.
Continuous-integration-options: PatchCheck.ignore-multi-package
Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
UNIT_TESTING_CODE_COVERAGE_ENABLE and UNIT_TESTING_ADDRESS_SANITIZER_ENABLE
are unconditionally set to TRUE in UnitTestFrameworkPkgHost.dsc.inc,
preventing platform DSC files from disabling them. Wrap both assignments
with !ifndef so platforms can override the values by pre-defining these
macros before the !include directive.
Signed-off-by: Abdul Lateef Attar <AbdulLateef.Attar@amd.com>
GCC enables -Wsign-compare by default but CLANG does
not enable -Wsign-compare by default. This causes
GCC unit test builds to fail with a signed/unsigned
mismatch when using EXPECT_EQ(), ASSERT_EQ(),
EXPECT_NE() or ASSERT_NE() with one of the items
being compared being literal 0. However, CLANG does
not fail for these builds.
Add -Wsign-compare to CXX_FLAGS for CLANGDWARF and
CLANGPDB to align CLANG error checking with GCC.
Add /w44388 and /w44389 to MSFT family compiler
CXX_FLAGS to align VS20xx error checking with GCC.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Add tests that check a local variable against 0 for values
of type UINT8, UINT16, UINT32, UINT64, UINTN, UINT32 bitfields,
and UINT64 bitfields.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Fixes: #11113
Currently, the global handle key and key inside handle structure is
incremented only when a new handle is allocated for protocol interface
to be installed.
However, when caller already supplies a handle gHandleDatabaseKey never
get incremented.
Move handle key incremental outside if block, just below the else
statement which allows gHandleDatabaseKey to always incremented whether
handle is supplied or not.
Signed-off-by: Khalid Ali <khaliidcaliy@gmail.com>
Replace traditional `#ifndef`/`#define`/`#endif` include guards
with `#pragma` once.
`#pragma once` is a widely supported preprocessor directive that
prevents header files from being included multiple times. It is
supported by all toolchains used to build edk2: GCC, Clang/LLVM, and
MSVC.
Compared to macro-based include guards, `#pragma once`:
- Eliminates the risk of macro name collisions or copy/paste errors
where two headers inadvertently use the same guard macro.
- Eliminate inconsistency in the way include guard macros are named
(e.g., some files use `__FILE_H__`, others use `FILE_H_`, etc.).
- Reduces boilerplate (three lines replaced by one).
- Avoids polluting the macro namespace with guard symbols.
- Can improve build times as the preprocessor can skip re-opening the
file entirely, rather than re-reading it to find the matching
`#endif` ("multiple-include optimization").
- Note that some compilers may already optimize traditional include
guards, by recognzining the idiomatic pattern.
This change is made acknowledging that overall portability of the
code will technically be reduced, as `#pragma once` is not part of the
C/C++ standards.
However, this is considered acceptable given:
1. edk2 already defines a subset of supported compilers in
BaseTools/Conf/tools_def.template, all of which have supported
`#pragma once` for over two decades.
2. There have been concerns raised to the project about inconsistent
include guard naming and potential macro collisions.
Approximate compiler support dates:
- MSVC: Supported since Visual C++ 4.2 (1996)
- GCC: Supported since 3.4 (2004)
(http://gnu.ist.utl.pt/software/gcc/gcc-3.4/changes.html)
- Clang (LLVM based): Since initial release in 2007
Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
Add .exe file extension to host-based unit test applications
built in a Windows Mingw build environment to match host-based
unit test applications generated in VS20xx environments.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
* Add checks for supported tool chains with error messages.
* Add support for MINGW CLANGDWARF builds
* Fix IA32 C++ alignment errors using CLANGDWARF
* Add CLANG native code coverage compiler/linker options
* Reduce Visual Studio support to VS2019 and VS2022 and simplify
linker options for VS20xx and CLANGPDB using VS libraries.
* Add correct ASAN libraries for IA32/X64 CLANGPDB builds
* Add -z noexectstack linker option for all Linux builds.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
There are cases where DSC files must have logic to detect
if host-based unit tests are being built in a Windows
environment. Add HOST_BASED_UNIT_TESTING_ENABLED define
set to TRUE if that condition is detected.
One example is the optimized assembly files from Openssl
project that generate NASM source files that assume
Windows calling convention. Those NASM files must be used
for both firmware builds where the default compiler
calling convention is the Windows calling convention and
host-based unit tests built using any of the compilers
supported in Windows environment.
Also update UnitTestFrameworkPkgHost.dsc.inc to set
BUILD_TARGETS to NOOPT because host-based unit tests
only support NOOPT. If the DSC file that includes
UnitTestFrameworkPkgHost.dsc.inc uses a BUILD_TARGETS
other than NOOPT, this will override the DSC setting.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Update UnitTestFrameworkPkgHost.dsc.inc DSC file to use the
correct StackCheckLib class instead of NULL. This guarantees
that the StackCheckLib is only linked with modules that have
a StackCheckLib class dependency.
Must also add -fno-stack-protector to GCC and CLANGPDB to
disable use of stack checks by the compiler. ASAN provides
better coverage for access outside stack frames.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Remove workaround to define _MSC_VER when it is not defined
when building with clang on Windows. This workaround was
required when CLANGPDB undefined _MSC_VER in tools_def.txt.
With -U _MSC_VER removed from tools_def.txt, this workaround
for host-based unit test builds is no longer required.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Fix error reported by clang in UnitTestDebugAssertLibHost
that the function snprintf() is undefined. Add include of
<stdio.h> to resolve error.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Update UnitTestLib to not pass UnitTest->Log as the format
string to printf(). If UnitTest->Log contains any printf()
format specifiers, then printf() will look for arguments that
are not present and will read beyond the stack frame.
Change use of printf() to an fwrite() to stdout followed by
an fflush() to stdout.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Linux/GCC host-based unit test application builds use a
default stack size of 8MB. Windows/VS20xx and Windows/CLANGPDB
host-based unit test application builds use a default stack size
of 1MB. This can allow Linux unit tests to pass and Windows unit
tests to fail with stack overflow if large local variables are
used. ASAN increases stack usage, so this condition can occur
more frequently when ASAN enabled.
Update MSFT and CLANGPDB for host-based unit tests to use a
default stack size of 8MB so all tool chains use the same
default stack size for host-based unit tests.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Remove /EHsc from CC_FLAGS. /EHs is added to CC_FLAGS in
UnitTestFrameworkPkgHost.dsc.inc for all components.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Update MSFT CC_FLAGS options to remove /EHs and /MTd that
are added by UnitTestFrameworkPkgHost.dsc.inc for all
components.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Add -DSUBHOOK_STATIC to all GCC family builds to match
defines in MSFT family builds.
Also remove /EHsc from MSFT CC_FLAGS. /EHs is added to
CC_FLAGS in UnitTestFrameworkPkgHost.dsc.inc for all
components.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Add CLANGPDB family defines to match MSFT family defines.
Also add -Wno-deprecated-declarations to CLANGPDB family
to ignore warnings detected when building cmocka sources
with clang compiler.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Update GoogleTestLib.h to support Windows/CLANGPDB build.
When using Windows/CLANGPDB, _MSC_VER is not defined. If
it is not defined, then set to 1900 for Visual Studio 2015
or newer compatibility when using windows include files for
host based unit test application builds. If _MSC_VER is not
set, then those application fail to build.
Also address a Windows/CLANG issue when ASAN is enabled
by providing an alternate implementation of the
EXPECT_THROW_MESSAGE() and ASSERT_THROW_MESSAGE() macros.
Without this alternate implementation, an exception is
generated due to the description of the ASSERT() condition
being NULL.
Update all implementations of EXPECT_THROW_MESSAGE() and
ASSERT_THROW_MESSAGE() to support parameters to the
statement executed to return values.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Update host-based unit test [BuildOptions] to support
CLANGDWARF and CLANGPDB.
* Use CLANG specific code coverage options
* Map CLANG trap function to abort() to resolve link issues
when building Google Test based unit tests. The default
mapping from tools_def.txt is to an unresolved function
to generate a build failure. Mapping to abort() overrides
the default mapping. If a unit test introduces undefined
behavior in a code path that is executed, then abort() is
called and the unit test application exits with an error
and fails the unit test.
* Update GCC to pass --coverage in link command so the
linker automatically includes -lgcov. The explicit
linking of -lgcov is removed.
* Add /EHs to all MSFT components instead of only components
of type HOST_APPLICATION.
* Add CXX_FLAGS for all tool chain families. Use c++ 20 for
MSFT and GCC and c++ 17 for clang. Clang generates errors
compiling Google Test with c++ 20.
* Add -z noexecstack to all GCC builds to reduce warnings.
* Add UNIT_TESTING_CODE_COVERAGE_ENABLE that is enabled by
default. Can be used to temporarily disable code coverage
when debugging interactions between unit testing features.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
edk2 is dropping support for the ARM32 architecture. This
commit removes ARM32 code from UnitTestFrameworkPkg. It also
drops irrelevant VALID_ARCHITECTURES comments from infs that
are not arch specific.
Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
Disable write strings warning for host-based unit test
builds when building modules of type HOST_APPLICATION.
Modules of type HOST_APPLICATION may compile and link code
under test into the host-based unit test. When building
GoogleTest based unit tests, the G++ compiler is used. The
G++ compiler is stricter than GCC for passing a const string
into a function parameter that is not const. This is not a
warning for GCC build of firmware code. But GoogleTest builds
that use g++ detect as a warning and that breaks the build.
The firmware code under test can not be changed, so the warning
-Wno-write-strings is added to CC_FLAGS to ignore this warning.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
For Linux/GCC host-based unit tests, -fno-pei is added
to CC_FLAGS and this causes builds of .aslc/.act files for
ACPI tables used in host-based unit tests to fail. Add
-fpie to ASLCC_FLAGS in UnitTestFrameworkPkgHost.dsc.inc
to override this CC_FLAGS setting and resolve the build issue.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
gtest.h uses standard includes that define NULL to __null. This
is different that the C++ standard that uses nullptr. Redefine
NULL in GoogleTestLib.h to use C++ form of nullptr. This allows
NULL to be used in checks and mocks without explicit typecasts.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
There are duplicate library function names between 2 Unit Test libraries
(UnitTestPeiServicesTablePointerLib and UnitTestUefiBootServicesTableLib).
There are 3 functions (UnitTestAllocatePages, UnitTestAllocatePool and
UnitTestFreePage). This update avoids the duplication and prevents a build
error when building a Unit Test application that links the libraries based
on UnitTestFrameworkPkg.
Signed-off-by: Phil Noh <Phil.Noh@amd.com>
PR #6408 introduced a regression by removing /WHOLEARCHIVE
from VS20xx DLINK_FLAGS when building host based unit tests.
PR #5098 added /WHOLEARCHIVE to resolve issues when building
host based unit tests with GoogleTest.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
PR #6408 introduced a host specific NORETURN function to resolve
false positives from the address sanitizer when LongJump() is
called and the stack is reset to a previous stack frame.
This approach was discussed here:
https://github.com/tianocore/edk2/pull/6408/files#r1918810499
False positives are still being observed with this initial
solution.
The address sanitizer provides __asan_handle_no_return()
to clean up shadow memory before a NORETURN function is
called and provides a simpler implementation for this
issue without having to introduce a host specific
NORETURN function.
https://github.com/llvm/llvm-project/blob/main/compiler-rt/include/sanitizer/asan_interface.h
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
SafeIntLib has been added to UnitTestFrameworkPkg Common Includes DSC
for usage in host and target based tests.
Signed-off-by: Sachin Ganesh <sachinganesh@ami.com>
Add HostMemoryAllocationBelowAddressLib class and implementation that
uses OS specific services to perform pool and page allocations below
a specified address in a host based unit test application execution
environment. This library class is only required for mocking buffers
that are assumed to be below a specific address by code under test.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Use snprintf() in host based unit tests to format log messages
and add host specific wrapper for LongJump() that is decorated
with NORETURN to provide hint to address sanitizer that LongJump()
never returns.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Add GoogleTest and Framework based unit tests that are expected
to fail and be caught by Address Sanitizer. These unit tests
verify that an address sanitizer is enabled and detecting the
following conditions. It also provide examples of the expected
output when an Address Sanitizer detected these types of issues.
* double free
* buffer overflow
* buffer underflow
* null ptr
* invalid address
* divide by zero
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
* Update host based unit test VS20xx builds to use /MTd instead of
/MT to enable use of debug libraries for host based unit tests.
* Enable /fsanitize=address for host based unit test VS2019 builds
* Enable /fsanitize=address for host based unit test VS2022 builds
* Enable -fsanitize=address for host based unit test GCC builds
* Add UNIT_TESTING_ADDRESS_SANITIZER_ENABLE define that is set to
TRUE by default so it is always enabled, but can be set to FALSE
to temporarily disable during development/debug of unit tests.
* Add address sanitizer information to ReadMe.md
Enabling the Address Sanitizer can detect double frees, buffer
overflow, buffer underflow, access to invalid addresses, and
various exceptions. These can be detected in both the unit test
case sources as well as the code under test.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Update DSC files to always enable DEBUG_CLEAR_MEMORY() macros
so memory is cleared on every memory allocation/free operation.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Implement FreeUnitTestEntry(), FreeUnitTestSuiteEntry(), and
FreeUnitTestFramework() so the UnitTestLib does not introduce
any memory leaks.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Add use of DEBUG_CLEAR_MEMORY() macros on all allocation and free
operations in MemoryAllocationLibPosix to match behavior of all other
MemoryAllocationLib instances.
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>