From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=209.132.183.28; helo=mx1.redhat.com; envelope-from=lersek@redhat.com; receiver=edk2-devel@lists.01.org Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B2F0F211BA45E for ; Fri, 25 Jan 2019 01:07:20 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1B9F21B7A; Fri, 25 Jan 2019 09:07:20 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-120-91.rdu2.redhat.com [10.10.120.91]) by smtp.corp.redhat.com (Postfix) with ESMTP id C70E017D93; Fri, 25 Jan 2019 09:07:18 +0000 (UTC) To: Neo Hsueh , edk2-devel@lists.01.org Cc: Michael D Kinney , Dandan Bi , Liming Gao References: <20190124231819.2352-1-hong-chihx.hsueh@intel.com> From: Laszlo Ersek Message-ID: <074d40da-f8e4-cd0b-1993-4b8d2c01e507@redhat.com> Date: Fri, 25 Jan 2019 10:07:17 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20190124231819.2352-1-hong-chihx.hsueh@intel.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 25 Jan 2019 09:07:20 +0000 (UTC) Subject: Re: [PATCH] MdePkg/BasePeCoffLib: skip runtime relocation if relocation info is invalid. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Jan 2019 09:07:20 -0000 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 01/25/19 00:18, Neo Hsueh wrote: > 1.Skip runtime relocation for PE images that provide invalid relocation > infomation (ex: RelocDir->Size = 0) to fix a hang observed while booting > Windows. > 2.Add a magic number check for PE32+ image. > > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Neo Hsueh > Cc: Michael D Kinney > Cc: Liming Gao > Cc: Dandan Bi > --- > MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 19 ++++++++++++++++--- > 1 file changed, 16 insertions(+), 3 deletions(-) I can't comment on the technical details of the patch, but I have some comments on the organization of the patch. First, the two changes that it implements should be separate patches. > diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c > index 1bd079ad6a..6477ef0759 100644 > --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c > +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c > @@ -1725,11 +1725,18 @@ PeCoffLoaderRelocateImageForRuntime ( > NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes; > DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[0]); > } else { > + if (Hdr.Pe32Plus->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) { > + // > + // Use PE32+ offset > + // > + NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes; > + DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[0]); > + } else { > // > - // Use PE32+ offset > + // Not a valid PE image so Exit > // > - NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes; > - DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[0]); > + return; > + } > } > > // Second, my understanding is that in edk2, we don't do if (Condition1) { } else { if (Condition2) { } else { } } Instead, we prefer if (Condition1) { } else if (Condition2) { } else { } As far as I know, this is the only construct where we don't require braces after an "else". See: https://edk2-docs.gitbooks.io/edk-ii-c-coding-standards-specification/content/5_source_files/57_c_programming.html#57342-when-an-else-is-used-it-may-start-on-the-same-line-as-the-close-brace-of-the-if-or-be-on-the-following-line-and-aligned-with-the-closing-brace Third, the return statement and the comment are not properly indented in the last branch. Thanks Laszlo > @@ -1746,6 +1753,12 @@ PeCoffLoaderRelocateImageForRuntime ( > RelocDir->VirtualAddress + RelocDir->Size - 1, > 0 > ); > + if (RelocBase == NULL || RelocBaseEnd == NULL || RelocBaseEnd < RelocBase) { > + // > + // relocation block is not valid, just return > + // > + return; > + } > } else { > // > // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image. >