Re: Having problems when trying to instrument all code of a specific UEFI driver (including the library code)

Andrew Fish

On Apr 9, 2021, at 7:55 AM, wrote:

Hello everyone,

I hope this is the right place to ask this, I haven't been able to find answers online or in the edk2 wiki.

I'm trying to apply sanitizers to system management mode (SMM) drivers (VariableSmm.efi, for instance) in edk2, but I'm running into the problem that at the moment I am unable to instrument the library code that later gets linked with (the library code referenced in the .inf file). At the moment, when instrumenting a driver, for example VariableSmm.inf, I'm providing compilation flags via the [BuildOptions] section and then append the flags to the *_*_*_CC_FLAGS variable. This allows me to instrument the source files present under the [Sources] section. However, the library code under the [LibraryClasses] section that later gets linked to create the eventual .efi file is not instrumented, as it is already compiled separately before.

So the problem I have is that for certain UEFI drivers I want to instrument the library code, but for others not. So to clarify, for UEFI driver A, I want the library code compiled with "-fsanitize=memory", while for other UEFI drivers, I want to use the default compiled library code. I haven't found a clean way to do this in the building process. Is there anyone who has tips regarding this problem? I hope the problem is clear, if not, please indicate so.

If you add the --report-file=REPORTFILE to the build command when you compile it will generate a report about your build. I think the info in this report can help you out.
1) It ill show all the library instances that map to the [LibraryClassess] list in the driver INF file for every driver.
a) This shows you the mapping of the [LibraryClassess] to specific library instances.
b) This includes the list of [LibraryClasses] from the dependent library instances [LibraryClasses] section that got indirectly linked to resolve dependencies for the libraries, so the full set.
c) This list also shows you the library constructors and the order they are called.
2) The report file also will show you the per compiler CC_FLAGS and given what you are doing this may be helpful.

The makefiles are generated and live with the drivers build output under the Build dir so somethings looking at the generated makefiles is helpful. I generally find the log file is a lot easier than looking into the makefiles.

[1] $ .
Loading previous configuration from /Volumes/Case/edk2-github/Conf/
WORKSPACE: /Volumes/Case/edk2-github
EDK_TOOLS_PATH: /Volumes/Case/edk2-github/BaseTools
CONF_PATH: /Volumes/Case/edk2-github/Conf
$ build --help
Usage: build.exe [options] [all|fds|genc|genmake|clean|cleanall|cleanlib|modules|libraries|run]

Copyright (c) 2007 - 2018, Intel Corporation All rights reserved.

--version show program's version number and exit
-h, --help show this help message and exit
ARCHS is one of list: IA32, X64, ARM, AARCH64, RISCV64
or EBC, which overrides target.txt's TARGET_ARCH
definition. To specify more archs, please repeat this
Build the platform specified by the DSC file name
argument, overriding target.txt's ACTIVE_PLATFORM
Build the module specified by the INF file name
Using the TARGET to build the platform, overriding
target.txt's TARGET definition.
Using the Tool Chain Tagname to build the platform,
overriding target.txt's TOOL_CHAIN_TAG definition.
-x SKUID, --sku-id=SKUID
Using this name of SKU ID to build the platform,
overriding SKUID_IDENTIFIER in DSC file.
-n THREADNUMBER Build the platform using multi-threaded compiler. The
value overrides target.txt's
MAX_CONCURRENT_THREAD_NUMBER. When value is set to 0,
tool automatically detect number of processor threads,
set value to 1 means disable multi-thread build, and
set value to more than 1 means user specify the
threads number to build.
The name of the FDF file to use, which overrides the
setting in the DSC file.
-r ROMIMAGE, --rom-image=ROMIMAGE
The name of FD to be generated. The name must be from
[FD] section in FDF file.
-i FVIMAGE, --fv-image=FVIMAGE
The name of FV to be generated. The name must be from
[FV] section in FDF file.
-C CAPNAME, --capsule-image=CAPNAME
The name of Capsule to be generated. The name must be
from [Capsule] section in FDF file.
-u, --skip-autogen Skip AutoGen step.
-e, --re-parse Re-parse all meta-data files.
-c, --case-insensitive
Don't check case of file name.
-w, --warning-as-error
Treat warning in tools as error.
Put log in specified file as well as on console.
-s, --silent Make use of silent mode of (n)make.
-q, --quiet Disable all messages except FATAL ERRORS.
-v, --verbose Turn on verbose output with informational messages
printed, including library instances selected, final
dependency expression, and warning messages, etc.
-d DEBUG, --debug=DEBUG
Enable debug messages at specified level.
-D MACROS, --define=MACROS
Macro: "Name [= Value]".
Create/overwrite the report to the specified filename.
Flags that control the type of build report to
generate. Must be one of: [PCD, LIBRARY, FLASH,
EXECUTION_ORDER]. To specify more than one flag,
repeat this option on the command line and the default
-F FLAG, --flag=FLAG Specify the specific option to parse EDK UNI file.
Must be one of: [-c, -s]. -c is for EDK framework UNI
file, and -s is for EDK UEFI UNI file. This option can
also be specified by setting *_*_*_BUILD_FLAGS in
[BuildOptions] section of platform DSC. If they are
both specified, this value will override the setting
in [BuildOptions] section of platform DSC.
-N, --no-cache Disable build cache mechanism
--conf=CONFDIRECTORY Specify the customized Conf directory.
--check-usage Check usage content of entries listed in INF file.
--ignore-sources Focus to a binary build and ignore all source files
--pcd=OPTIONPCD Set PCD value by command line. Format: "PcdName=Value"
Specify the maximum line length of build command.
Default is 4096.
--hash Enable hash-based caching during build process.
Generate a cache of binary files in the specified
Consume a cache of binary files from the specified
Enable GenFds multi thread to generate ffs file.
Disable GenFds multi thread to generate ffs file.
Disable the include path check for outside of package.


Andrew Fish

Kind regards,

Join to automatically receive all group messages.