From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 377D5AC18EC for ; Tue, 26 Mar 2024 20:01:43 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=soEf1M+BJuM9zXY5twgygarn1HkeVJkCOl/tj6Axgww=; c=relaxed/simple; d=groups.io; h=MIME-Version:From:Date:Message-ID:Subject:To:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Type; s=20240206; t=1711483301; v=1; b=AU1p2huMsE5pDLnG//7yuCJrPuQQvOk6ONZBhlFgWxPtDmDpT1ORap1Pn+AnzjJDWqO9Q+eR 20u0V0jjPUBwfxlV2XFQIzMt/wSHIFJx11G2ZkN1tppOBmDWeq2UlykrlNuXGn+zSfaQHprqTRP YWvBJHy7tQG2f/J0d/R38LZHA1MpqCHZPpKwBmswtKJ+zZUuLzKisqQmLPp/XmynLHC+4xOm3fj JSEbPoq8gWwI7oHVx2CsdLW9Nsv7tEze/i4UAz1G4R34hxCpXP4Aue8VMzN/RzE4KMQlvH8U3vs ET8NBj4bpaOAN/gP2EkHXR0+LH2Bymt3GyRLLxLjdz5BA== X-Received: by 127.0.0.2 with SMTP id AyrrYY7687511xUKM2BggBKr; Tue, 26 Mar 2024 13:01:41 -0700 X-Received: from mail-oo1-f47.google.com (mail-oo1-f47.google.com [209.85.161.47]) by mx.groups.io with SMTP id smtpd.web10.15660.1711472686581409254 for ; Tue, 26 Mar 2024 10:04:46 -0700 X-Received: by mail-oo1-f47.google.com with SMTP id 006d021491bc7-5a46abf093cso3645939eaf.2 for ; Tue, 26 Mar 2024 10:04:46 -0700 (PDT) X-Gm-Message-State: xxgMk14GhgowXL2rxOg95eWWx7686176AA= X-Google-Smtp-Source: AGHT+IFUwrLM72Eu0dl3zIAeN8JdmKsxTaD36bc2xoCO9ZkgKiclmYqfM444Xj30kUEaukVow3ZE0AV8+ye9XcMuEoI= X-Received: by 2002:a05:6808:f14:b0:3c3:c251:58ee with SMTP id m20-20020a0568080f1400b003c3c25158eemr3846779oiw.4.1711472685721; Tue, 26 Mar 2024 10:04:45 -0700 (PDT) MIME-Version: 1.0 From: "Glenn Griffin" Date: Tue, 26 Mar 2024 10:04:35 -0700 Message-ID: Subject: [edk2-devel] [PATCH 1/1] OvmfPkg: Align the SEC module within OvmfPkgX64 To: devel@edk2.groups.io Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Tue, 26 Mar 2024 13:01:41 -0700 Reply-To: devel@edk2.groups.io,ggriffiniii@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Type: text/plain; charset="UTF-8" X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=AU1p2huM; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io Prior to this change the alignment of the SEC module would be 4 bytes. This is inconsistent with the expectations of the compiler and can lead to unexpected behavior. For example a modern version of clang with size optimizations enabled (-Oz) can break the ALIGN_POINTER macro in the SEC module. Here is a minimal example: https://godbolt.org/z/fzW67ndTY ``` unsigned char mArray[112]; #define ALIGN_VALUE(Value, Alignment) ((Value) + (((Alignment) - (Value)) & ((Alignment) - 1))) #define ALIGN_POINTER(Pointer, Alignment) ((void *) (ALIGN_VALUE ((uint64_t)(Pointer), (Alignment)))) int main() { printf("0x%llx\n", mArray); printf("0x%llx\n", ALIGN_POINTER(mArray, 64)); return 0; } ``` The above code compiles down to: ``` main: # @main push r14 push rbx push rax lea rbx, [rip + .L.str] lea r14, [rip + mArray] mov rdi, rbx mov rsi, r14 xor eax, eax call printf@PLT push 64 pop rsi sub esi, r14d and esi, 48 add rsi, r14 mov rdi, rbx xor eax, eax call printf@PLT xor eax, eax add rsp, 8 pop rbx pop r14 ret mArray: .zero 112 .L.str: .asciz "0x%llx\n" ``` Note that the `value & 63` in the ALIGN_VALUE implementation gets transformed into `and esi, 48` in the assembly. The compiler knows the array address is already aligned so it believes there is no need to clear the last 4 bits of the address. However by mapping the data section that contains mArray onto a 4-byte-aligned base address we violate the compiler's expectations. The last 4 bits of the mArray address are no longer zeroes leading to an ALIGN_POINTER macro that doesn't work. Cc: Ard Biesheuvel Signed-off-by: Glenn Griffin --- OvmfPkg/OvmfPkgX64.fdf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OvmfPkg/OvmfPkgX64.fdf b/OvmfPkg/OvmfPkgX64.fdf index eb3fb90cb8b6..8b60355de40b 100644 --- a/OvmfPkg/OvmfPkgX64.fdf +++ b/OvmfPkg/OvmfPkgX64.fdf @@ -434,7 +434,7 @@ [FV.FVMAIN_COMPACT] [Rule.Common.SEC] FILE SEC = $(NAMED_GUID) { - PE32 PE32 $(INF_OUTPUT)/$(MODULE_NAME).efi + PE32 PE32 Align=Auto $(INF_OUTPUT)/$(MODULE_NAME).efi UI STRING ="$(MODULE_NAME)" Optional VERSION STRING ="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER) } -- 2.44.0.396.g6e790dbe36-goog -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#117139): https://edk2.groups.io/g/devel/message/117139 Mute This Topic: https://groups.io/mt/105165329/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-