From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR02-AM5-obe.outbound.protection.outlook.com (mail-oln040092067062.outbound.protection.outlook.com [40.92.67.62]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id A8E4A21E1DAFA for ; Thu, 3 Aug 2017 12:50:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=D5+5i3Omu4pGSnpXcXdjaIuY9vyWACbeO5Yc/sBX8ac=; b=c99X2PS1OiYp3lsXGsCsSQ02kq2rXZQEvv9DVIt730EqyZm7NuJK+ZpZqUc17YYSzwheCibPiskK2XHxYzGnlUIOZk1OAopDJ2rlWCiQqHWopjW+vwEv5riWttQYIB9j2p8gJFeY2jvjtI0wDWlF0HtkX9zpw9BvrNvEGasagJtevZwmhvRm5NFsZk9ct+2gvD+rxgyIw6U2lIzm8bDR3AWhiEd61Wq+LHN9sbWm3GcBo/5Rgf1/8dk/6Eoe/qloCc9PapT6bQhzzma1xw/1gvi8/5qn3ZoYUlzK/NPVbAg+uAH6B05MlkgORsitEeZ/KTI7vkOioCFssdhOzmnskQ== Received: from AM5EUR02FT039.eop-EUR02.prod.protection.outlook.com (10.152.8.56) by AM5EUR02HT164.eop-EUR02.prod.protection.outlook.com (10.152.8.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.1304.16; Thu, 3 Aug 2017 19:52:15 +0000 Received: from AM4PR06MB1491.eurprd06.prod.outlook.com (10.152.8.59) by AM5EUR02FT039.mail.protection.outlook.com (10.152.9.27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1304.16 via Frontend Transport; Thu, 3 Aug 2017 19:52:15 +0000 Received: from AM4PR06MB1491.eurprd06.prod.outlook.com ([fe80::dcd0:60e6:90e0:871b]) by AM4PR06MB1491.eurprd06.prod.outlook.com ([fe80::dcd0:60e6:90e0:871b%13]) with mapi id 15.01.1304.025; Thu, 3 Aug 2017 19:52:15 +0000 From: =?iso-8859-1?Q?Marvin_H=E4user?= To: "edk2-devel@lists.01.org" CC: "michael.d.kinney@intel.com" , "liming.gao@intel.com" Thread-Topic: [PATCH v3 2/2] MdePkg/BaseLib: Update internal LinkedList verifications. Thread-Index: AQHTDJIB5CmdExrhpkiLrV2XlUwfmA== Date: Thu, 3 Aug 2017 19:52:15 +0000 Message-ID: References: <20170803195151.16648-1-Marvin.Haeuser@outlook.com> In-Reply-To: <20170803195151.16648-1-Marvin.Haeuser@outlook.com> Accept-Language: de-DE, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: lists.01.org; dkim=none (message not signed) header.d=none;lists.01.org; dmarc=none action=none header.from=outlook.com; x-incomingtopheadermarker: OriginalChecksum:BBB254038BE33557336A3591D135EEA7C58AA0816DAE2139EA0C1F23E7D36E73; UpperCasedChecksum:AE2A34D8E38063497984FBA45D8A4EC0B11DDCD072E626E0FEB8C834AC8032AB; SizeAsReceived:7406; Count:46 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [DAGEqp6ax4vrcllcF78/FkJHn4ps5/j9] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; AM5EUR02HT164; 7:ANpUZcTKoZQ9UQsWBSDnAGOBioRDt9VQq+aRl5XugRN2kmI9gsOZ6rG0WXmp4+HGvDvH1AFZ1kveuhvZ4qMLHr9Bmtr+y8oO1eW2ViU6iHxw/i7VY5YOo6fGnYlepBTdPDvOJMS4vMz/mLiIgJMjUj/HHjWEVSkEwjMDBLBjC8Epb/36FxuWOLzctVf0nM6ApUkbEJWCfNz/wzDp03gmqMTf89ce1xPZrUjY/7k6g9EXLvyujkBpu2gVhf7O7YnJG1u+gTS8WcVQqm4stWHGVBI1FTzverew6lUTD7jxcCIG117Fz0mzq/F/nmPv1j12BSpWojskqecx6oFDJQ8Eau0gSAnDMgjAUryVYp2f0Mk+arEipB53LG2NrnDwnBkMRS1nJ1ZKMwCaKlwxYTwD4nM5OOUIdZn1VkTVhfQV/qgN+MiOkTdtp0+/L4bSGM716qcNWmgeB46wEF0CY/l4U4ZAMOM8N78/5jUEGGBqtnHdVveCbLQY18yXRLwMG/Di8+IH4ZH4j2uFSv5ymwKq0y//Diyokr1Gn5D0xJ+29b3IG25xfw91BFxbdMhnaRdxD6BqA6q2mogYf9mC1YImR5EgGGZpXhs5k/HjIaiorRf64DkDyNplvt07s7W8O6bPjKIzcybLptWkOEiCOiwO3yHEfrbKDDfyv4mH7UWalmpme1Xfu2Y0qi4bufsIUL1Qgp9MoOAYPFegN+nd/bmubtU4oI464LViODSZmNfXxMpLsmMlbYxiaQDJHxdjeF1lFlf/45S22M2F86kiUnluXQ== x-incomingheadercount: 46 x-eopattributedmessage: 0 x-forefront-antispam-report: EFV:NLI; SFV:NSPM; SFS:(7070007)(98901004); DIR:OUT; SFP:1901; SCL:1; SRVR:AM5EUR02HT164; H:AM4PR06MB1491.eurprd06.prod.outlook.com; FPR:; SPF:None; LANG:en; x-ms-office365-filtering-correlation-id: 6d7a86b5-9b84-46dc-cd13-08d4daa923ad x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(300000503095)(300135400095)(201702061074)(5061506573)(5061507331)(1603103135)(2017031320274)(2017031324274)(2017031323274)(2017031322377)(1601125374)(1603101448)(1701031045)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:AM5EUR02HT164; x-ms-traffictypediagnostic: AM5EUR02HT164: x-exchange-antispam-report-test: UriScan:(189930954265078)(788757137089); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(444000031); SRVR:AM5EUR02HT164; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:AM5EUR02HT164; x-forefront-prvs: 03883BD916 spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Aug 2017 19:52:15.4784 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5EUR02HT164 Subject: [PATCH v3 2/2] MdePkg/BaseLib: Update internal LinkedList verifications. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Aug 2017 19:50:06 -0000 Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable 1) Replace InternalBaseLibIsNodeInList() with InternalBaseLibIsListValid(). - The verification whether Node is within the doubly-linked List is now done by IsNodeInList(). - Whether the list is valid is returned. 2) The comments within InsertHeadList() and InsertTailList() stated that it is checked whether Entry is not part of the doubly-linked list. This was not done as argument 3 of InternalBaseLibIsNodeInList() indicated whether the check is done, not whether to check if the node is or is not in the list. This has been fixed by using IsNodeInList() for the ASSERTs. V2: - Fix IsListEmpty() to ASSERT when the passed list is invalid. - Introduce the VERIFY_IS_NODE_IN_LIST() macro to only verify whether the passed node is part of the list when PcdVerifyNodeInList is TRUE. V3: - Introduce the ASSERT_VERIFY_NODE_IN_VALID_LIST() macro which, depending on the value of PcdVerifyNodeInList, verifies whether SecondEntry is or is not part of the same doubly-linked list as FirstEntry and unconditionally verifies whether the doubly-linked list FirstEntry is part of is valid. This prevents InternalBaseLibIsListValid() from being called twice when a function ASSERTs via the result of IsNodeInList(), as it calls InternalBaseLibIsListValid() already. - Remove the VERIFY_IS_NODE_IN_LIST() macro in favor of ASSERT_VERIFY_NODE_IN_VALID_LIST(). Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Marvin Haeuser --- MdePkg/Library/BaseLib/LinkedList.c | 107 +++++++++----------- 1 file changed, 49 insertions(+), 58 deletions(-) diff --git a/MdePkg/Library/BaseLib/LinkedList.c b/MdePkg/Library/BaseLib/L= inkedList.c index af27b0dd9a5c..30fd7009e007 100644 --- a/MdePkg/Library/BaseLib/LinkedList.c +++ b/MdePkg/Library/BaseLib/LinkedList.c @@ -15,25 +15,47 @@ #include "BaseLibInternals.h" =20 /** - Worker function that locates the Node in the List. + If PcdVerifyNodeInList is TRUE, ASSERTs when SecondEntry is or is not pa= rt of + the same doubly-linked list as FirstEntry depending on the value of InLi= st. + Independent of PcdVerifyNodeInList, ASSERTs when FirstEntry is not part = of a + valid list. =20 - By searching the List, finds the location of the Node in List. At the sa= me time, - verifies the validity of this list. + If FirstEntry is NULL, then ASSERT(). + If FirstEntry->ForwardLink is NULL, then ASSERT(). + If FirstEntry->BackLink is NULL, then ASSERT(). + If PcdMaximumLinkedListLength is not zero, and List contains more than + PcdMaximumLinkedListLength nodes, then ASSERT(). + If PcdVerifyNodeInList is TRUE and SecondEntry is NULL, then ASSERT(). + + @param FirstEntry A pointer to a node in a linked list. + @param SecondEntry A pointer to the node to locate. + @param InList Defines whether to check if SecondEntry is or is no= t part + of the same doubly-linked list as FirstEntry. + +**/ +#if !defined (MDEPKG_NDEBUG) + #define ASSERT_VERIFY_NODE_IN_VALID_LIST(FirstEntry, SecondEntry, InList= ) \ + do { = \ + if (FeaturePcdGet (PcdVerifyNodeInList)) { = \ + ASSERT (InList =3D=3D IsNodeInList ((FirstEntry), (SecondEntry)));= \ + } else { = \ + ASSERT (InternalBaseLibIsListValid (FirstEntry)); = \ + } = \ + } while (FALSE) +#else + #define ASSERT_VERIFY_NODE_IN_VALID_LIST(FirstEntry, SecondEntry, InList= ) +#endif + +/** + Worker function that verifies the validity of this list. =20 If List is NULL, then ASSERT(). If List->ForwardLink is NULL, then ASSERT(). - If List->backLink is NULL, then ASSERT(). - If Node is NULL, then ASSERT(). - If PcdVerifyNodeInList is TRUE and DoMembershipCheck is TRUE and Node=20 - is in not a member of List, then return FALSE + If List->BackLink is NULL, then ASSERT(). If PcdMaximumLinkedListLength is not zero, and List contains more than PcdMaximumLinkedListLength nodes, then ASSERT(). =20 @param List A pointer to a node in a linked list. - @param Node A pointer to a node in a linked list. - @param VerifyNodeInList TRUE if a check should be made to see if Node = is a=20 - member of List. FALSE if no membership test s= hould=20 - be performed. =20 @retval TRUE if PcdVerifyNodeInList is FALSE @retval TRUE if DoMembershipCheck is FALSE @@ -45,10 +67,8 @@ **/ BOOLEAN EFIAPI -InternalBaseLibIsNodeInList ( - IN CONST LIST_ENTRY *List, - IN CONST LIST_ENTRY *Node, - IN BOOLEAN VerifyNodeInList +InternalBaseLibIsListValid ( + IN CONST LIST_ENTRY *List ) { UINTN Count; @@ -60,40 +80,11 @@ InternalBaseLibIsNodeInList ( ASSERT (List !=3D NULL); ASSERT (List->ForwardLink !=3D NULL); ASSERT (List->BackLink !=3D NULL); - ASSERT (Node !=3D NULL); - - Count =3D 0; - Ptr =3D List; - - if (FeaturePcdGet (PcdVerifyNodeInList) && VerifyNodeInList) { - // - // Check to see if Node is a member of List. =20 - // Exit early if the number of nodes in List >=3D PcdMaximumLinkedList= Length - // - do { - Ptr =3D Ptr->ForwardLink; - if (PcdGet32 (PcdMaximumLinkedListLength) > 0) { - Count++; - // - // ASSERT() if the linked list is too long - // - ASSERT (Count < PcdGet32 (PcdMaximumLinkedListLength)); - - // - // Return if the linked list is too long - // - if (Count >=3D PcdGet32 (PcdMaximumLinkedListLength)) { - return (BOOLEAN)(Ptr =3D=3D Node); - } - } - } while ((Ptr !=3D List) && (Ptr !=3D Node));=20 - - if (Ptr !=3D Node) { - return FALSE; - } - } =20 if (PcdGet32 (PcdMaximumLinkedListLength) > 0) { + Count =3D 0; + Ptr =3D List; + // // Count the total number of nodes in List. // Exit early if the number of nodes in List >=3D PcdMaximumLinkedList= Length @@ -104,9 +95,9 @@ InternalBaseLibIsNodeInList ( } while ((Ptr !=3D List) && (Count < PcdGet32 (PcdMaximumLinkedListLen= gth))); =20 // - // ASSERT() if the linked list is too long + // return whether linked list is too long // - ASSERT (Count < PcdGet32 (PcdMaximumLinkedListLength)); + return (BOOLEAN)(Count < PcdGet32 (PcdMaximumLinkedListLength)); } =20 return TRUE; @@ -238,7 +229,7 @@ InsertHeadList ( // // ASSERT List not too long and Entry is not one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (ListHead, Entry, FALSE)); + ASSERT_VERIFY_NODE_IN_VALID_LIST (ListHead, Entry, FALSE); =20 Entry->ForwardLink =3D ListHead->ForwardLink; Entry->BackLink =3D ListHead; @@ -279,7 +270,7 @@ InsertTailList ( // // ASSERT List not too long and Entry is not one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (ListHead, Entry, FALSE)); + ASSERT_VERIFY_NODE_IN_VALID_LIST (ListHead, Entry, FALSE); =20 Entry->ForwardLink =3D ListHead; Entry->BackLink =3D ListHead->BackLink; @@ -317,7 +308,7 @@ GetFirstNode ( // // ASSERT List not too long // - ASSERT (InternalBaseLibIsNodeInList (List, List, FALSE)); + ASSERT (InternalBaseLibIsListValid (List)); =20 return List->ForwardLink; } @@ -353,7 +344,7 @@ GetNextNode ( // // ASSERT List not too long and Node is one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE)); + ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE); =20 return Node->ForwardLink; } @@ -389,7 +380,7 @@ GetPreviousNode ( // // ASSERT List not too long and Node is one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE)); + ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE); =20 return Node->BackLink; } @@ -422,7 +413,7 @@ IsListEmpty ( // // ASSERT List not too long // - ASSERT (InternalBaseLibIsNodeInList (ListHead, ListHead, FALSE)); + ASSERT (InternalBaseLibIsListValid (ListHead)); =20 return (BOOLEAN)(ListHead->ForwardLink =3D=3D ListHead); } @@ -463,7 +454,7 @@ IsNull ( // // ASSERT List not too long and Node is one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE)); + ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE); =20 return (BOOLEAN)(Node =3D=3D List); } @@ -501,7 +492,7 @@ IsNodeAtEnd ( // // ASSERT List not too long and Node is one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE)); + ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE); =20 return (BOOLEAN)(!IsNull (List, Node) && List->BackLink =3D=3D Node); } @@ -548,7 +539,7 @@ SwapListEntries ( // // ASSERT Entry1 and Entry2 are in the same linked list // - ASSERT (InternalBaseLibIsNodeInList (FirstEntry, SecondEntry, TRUE)); + ASSERT_VERIFY_NODE_IN_VALID_LIST (FirstEntry, SecondEntry, TRUE); =20 // // Ptr is the node pointed to by FirstEntry->ForwardLink --=20 2.12.2.windows.2