Skip to content

Commit 5bc52de

Browse files
os-dlgao4
authored andcommitted
MdeModulePkg: DebugImageInfoTable: Fix Array Maintenance
The DebugImageInfoTable contains an array of image info structures. The current implementation removes an entry by freeing the info structure and putting NULL in that entry of the array. It then decrements the table size tracked in the table. However, the array is invalid at this point, it contains a NULL entry, which the UEFI spec does not envision and it contains a valid entry past the end of the array as tracked in the spec defined config table. If the table is consumed at this point it can lead to an invalid assessment of the image state, which defeats the purpose of the table. When a new info structure is added, it then scans for the first NULL entry adds a pointer to the new info structure there and increments the table size to cover the entrythat was formerly past the end of the array. The current implementation requires that once an unload happens, more loads happen than unloads and that the last operation is not an unload (which won't be true in the shell, e.g.). This is needlessly complex, as the order of the table doesn't matter (and in fact this implementation doesn't preserve image loading order either). This patch updates the removal function to free the desired info structure, move the last entry of the array to this freed spot, mark the last entry as NULL, and decrement the table count. The entry addition function then just always puts a new entry at the end of the array, expanding it as necessary. This simplifies the logic and covers the gaps that were present. Signed-off-by: Oliver Smith-Denny <[email protected]>
1 parent 867fad8 commit 5bc52de

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,20 +177,7 @@ CoreNewDebugImageInfoEntry (
177177

178178
Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;
179179

180-
if (mDebugInfoTableHeader.TableSize < mMaxTableEntries) {
181-
//
182-
// We still have empty entires in the Table, find the first empty entry.
183-
//
184-
Index = 0;
185-
while (Table[Index].NormalImage != NULL) {
186-
Index++;
187-
}
188-
189-
//
190-
// There must be an empty entry in the in the table.
191-
//
192-
ASSERT (Index < mMaxTableEntries);
193-
} else {
180+
if (mDebugInfoTableHeader.TableSize >= mMaxTableEntries) {
194181
//
195182
// Table is full, so re-allocate another page for a larger table...
196183
//
@@ -218,10 +205,12 @@ CoreNewDebugImageInfoEntry (
218205
// Enlarge the max table entries and set the first empty entry index to
219206
// be the original max table entries.
220207
//
221-
Index = mMaxTableEntries;
222208
mMaxTableEntries += EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;
223209
}
224210

211+
// We always put the next entry at the end of the currently consumed table (i.e. first free entry)
212+
Index = mDebugInfoTableHeader.TableSize;
213+
225214
//
226215
// Allocate data for new entry
227216
//
@@ -264,11 +253,13 @@ CoreRemoveDebugImageInfoEntry (
264253
for (Index = 0; Index < mMaxTableEntries; Index++) {
265254
if ((Table[Index].NormalImage != NULL) && (Table[Index].NormalImage->ImageHandle == ImageHandle)) {
266255
//
267-
// Found a match. Free up the record, then NULL the pointer to indicate the slot
268-
// is free.
256+
// Found a match. Free up the record, then move the final entry down to this slot; we don't care about the
257+
// order of the array
269258
//
270259
CoreFreePool (Table[Index].NormalImage);
271-
Table[Index].NormalImage = NULL;
260+
Table[Index].NormalImage = Table[mDebugInfoTableHeader.TableSize - 1].NormalImage;
261+
Table[mDebugInfoTableHeader.TableSize - 1].NormalImage = NULL;
262+
272263
//
273264
// Decrease the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
274265
//

0 commit comments

Comments
 (0)