Skip to content

Commit

Permalink
BaseTools: Detect library class mismatch
Browse files Browse the repository at this point in the history
Performs a check that will verify that the library instance implements
the library specified in the dsc by ensuring a LIBRARY_CLASS definition
exists in the INF [Defines] section and the value matches the library it
says it is implementing.

That is to say, for the following example:
`TestLib|Path/To/BaseTestLib.inf`, that BaseTestLib.inf has
`LIBRARY_CLASS = TestLib` defined in the [Defines] section.

Signed-off-by: Joey Vagedes <[email protected]>
  • Loading branch information
Javagedes committed Jul 1, 2024
1 parent 1748a3c commit db77981
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions BaseTools/Source/Python/AutoGen/AutoGenWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def run(self):
GlobalData.gEnableGenfdsMultiThread = self.data_pipe.Get("EnableGenfdsMultiThread")
GlobalData.gPlatformFinalPcds = self.data_pipe.Get("gPlatformFinalPcds")
GlobalData.file_lock = self.file_lock
GlobalData.gLogLibraryMismatch = False # MU_CHANGE
CommandTarget = self.data_pipe.Get("CommandTarget")
pcd_from_build_option = []
for pcd_tuple in self.data_pipe.Get("BuildOptPcd"):
Expand Down
1 change: 1 addition & 0 deletions BaseTools/Source/Python/Common/GlobalData.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
# Common lock for the file access in multiple process AutoGens
file_lock = None
# MU_CHANGE [BEGIN]: Add build-time random stack cookie support
gLogLibraryMismatch = True
gStackCookieValues32 = []
gStackCookieValues64 = []
# MU_CHANGE [END]
42 changes: 42 additions & 0 deletions BaseTools/Source/Python/Workspace/DscBuildData.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import json
import shutil

LoggedLibraryWarnings = [] # MU_CHANGE

def _IsFieldValueAnArray (Value):
Value = Value.strip()
if Value.startswith(TAB_GUID) and Value.endswith(')'):
Expand Down Expand Up @@ -764,6 +766,14 @@ def Modules(self):
LibraryPath = PathClass(NormPath(Record[1], Macros), GlobalData.gWorkspace, Arch=self._Arch)
LineNo = Record[-1]

# MU_CHANGE begin
# Validate that the Library instance implements the Library Class
if not self._ValidateLibraryClass(LibraryClass, LibraryPath) and self._ShouldLogLibrary(LineNo):
EdkLogger.warn("build",
f"{str(LibraryPath)} does not support LIBRARY_CLASS {LibraryClass}",
File=self.MetaFile)
# MU_CHANGE end

# check the file validation
ErrorCode, ErrorInfo = LibraryPath.Validate('.inf')
if ErrorCode != 0:
Expand Down Expand Up @@ -917,6 +927,15 @@ def LibraryClasses(self):
EdkLogger.verbose("Found forced library for arch=%s\n\t%s [%s]" % (Arch, LibraryInstance, LibraryClass))
LibraryClassSet.add(LibraryClass)
LibraryInstance = PathClass(NormPath(LibraryInstance, Macros), GlobalData.gWorkspace, Arch=self._Arch)

# MU_CHANGE begin
# Validate that the Library instance implements the Library Class
if not self._ValidateLibraryClass(LibraryClass, LibraryInstance) and self._ShouldLogLibrary(LineNo):
EdkLogger.warn("build",
f"{str(LibraryInstance)} does not support LIBRARY_CLASS {LibraryClass}",
File=self.MetaFile)
# MU_CHANGE end

# check the file validation
ErrorCode, ErrorInfo = LibraryInstance.Validate('.inf')
if ErrorCode != 0:
Expand Down Expand Up @@ -1187,6 +1206,29 @@ def __ParsePcdFromCommandLine(self):
for item in delete_assign:
GlobalData.BuildOptionPcd.remove(item)

# MU_CHANGE begin
def _ValidateLibraryClass(self, LibraryClass: str, LibraryInstance: PathClass) -> bool:
if LibraryClass.upper().startswith('NULL'):
return True

ParsedLibraryInfo = self._Bdb[LibraryInstance, self._Arch, self._Target, self._Toolchain]

for LibraryClassObject in ParsedLibraryInfo.LibraryClass:
if LibraryClassObject.LibraryClass == LibraryClass:
return True
return False

def _ShouldLogLibrary(self, LineNo) -> bool:
if not GlobalData.gLogLibraryMismatch:
return False

if LineNo in LoggedLibraryWarnings:
return False

LoggedLibraryWarnings.append(LineNo)
return True
# MU_CHANGE end

@staticmethod
def HandleFlexiblePcd(TokenSpaceGuidCName, TokenCName, PcdValue, PcdDatumType, GuidDict, FieldName=''):
if FieldName:
Expand Down

0 comments on commit db77981

Please sign in to comment.