public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Laszlo Ersek" <lersek@redhat.com>
To: Gerd Hoffmann <kraxel@redhat.com>, devel@edk2.groups.io
Cc: Eric Dong <eric.dong@intel.com>,
	Oliver Steffen <osteffen@redhat.com>,
	Rahul Kumar <rahul1.kumar@intel.com>, Ray Ni <ray.ni@intel.com>,
	Pawel Polawski <ppolawsk@redhat.com>
Subject: Re: [PATCH 1/1] UefiCpuPkg/PiSmmCpuDxeSmm: fix format string
Date: Fri, 24 Mar 2023 15:56:34 +0100	[thread overview]
Message-ID: <ddbd935b-3b91-96c4-844a-598312dac65e@redhat.com> (raw)
In-Reply-To: <20230324134157.118321-1-kraxel@redhat.com>

Hi Gerd,

On 3/24/23 14:41, Gerd Hoffmann wrote:
> BufferPages is UINTN, so we need "%Lu" when printing it.
>
> Fixes: 4f441d024bee ("UefiCpuPkg/PiSmmCpuDxeSmm: fix error handling")
> Reported-by: Laszlo Ersek <lersek@redhat.com>
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
> index cf69161caa4b..08663b1b1ab4 100644
> --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
> +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
> @@ -880,7 +880,7 @@ PiCpuSmmEntry (
>      BufferPages = EFI_SIZE_TO_PAGES (SIZE_32KB + TileSize * (mMaxNumberOfCpus - 1));
>      Buffer      = AllocateAlignedCodePages (BufferPages, SIZE_4KB);
>      if (Buffer == NULL) {
> -      DEBUG ((DEBUG_ERROR, "Failed to allocate %d pages.\n", BufferPages));
> +      DEBUG ((DEBUG_ERROR, "Failed to allocate %Lu pages.\n", BufferPages));
>        CpuDeadLoop ();
>        return EFI_OUT_OF_RESOURCES;
>      }

you missed part of my off-list comment; it was:

> "BufferPages" has type UINTN, it should not be printed with "%d" (%d
> is for INT32). The proper way to print a UINTN value with DEBUG is to
> cast the value to UINT64, and print that with %Lu (or %Lx).

Refer to "cast the value to UINT64".

UINTN is either UINT32 or UINT64. There is no format specifier that's
dedicated specifically to UINTN. You can't use fixed %u with UINTN
because that will be wrong on 64-bit platforms (%u takes UINT32 but
UINTN will be UINT64), and you can't use fixed %Lu with UINTN because
that will be wrong on 32-bit platforms (%Lu takes UINT64 but UINTN will
be UINT32). The proper way is to (1) cast the UINTN to UINT64 first
(which will be a no-op on 64-bit platforms, and it will widen UINT32 to
UINT64 on 32-bit platforms, without changing the value), and then (2)
print *that* (the now certainly UINT64 value) with %Lu.

  DEBUG ((
    DEBUG_ERROR,
    "Failed to allocate %Lu pages.\n",
    (UINT64)BufferPages
    ));

(

Here's an idea:

> diff --git a/MdePkg/Include/Library/PrintLib.h b/MdePkg/Include/Library/PrintLib.h
> index 8d523cac528d..bdacec9777f0 100644
> --- a/MdePkg/Include/Library/PrintLib.h
> +++ b/MdePkg/Include/Library/PrintLib.h
> @@ -197,6 +197,17 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  #define PREFIX_ZERO   0x20
>  #define RADIX_HEX     0x80
>
> +#if defined (MDE_CPU_AARCH64) || defined (MDE_CPU_LOONGARCH64) || \
> +    defined (MDE_CPU_RISCV64) || defined (MDE_CPU_X64)
> +#define PRIuN "%Lu"
> +#define PRIxN "%Lx"
> +#elif defined (MDE_CPU_ARM) || defined (MDE_CPU_IA32)
> +#define PRIuN "%Lu"
> +#define PRIxN "%Lx"
> +#else
> +#error Unknown CPU architecture.
> +#endif
> +
>  /**
>    Produces a Null-terminated Unicode string in an output buffer based on
>    a Null-terminated Unicode format string and a VA_LIST argument list.

and then you could do

  DEBUG ((
    DEBUG_ERROR,
    "Failed to allocate " PRIuN " pages.\n",
    BufferPages
    ));

(Note the lack of a cast.)

Note: I do not cover MDE_CPU_EBC above, in "PrintLib.h". I don't know
how EBC (EFI Byte Code) deals with 32-bit vs. 64-bit. The UEFI 2.10 spec
says in "22.1.1 Processor Architecture Independence": "64-bit C source
code" and "The interpreter handles 32 vs. 64 bit issues". Unfortunately,
that doesn't seem to help: it does not help us decide the size of UINTN
at compile time.

More precisely: if you check "MdePkg/Include/Ebc/ProcessorBind.h", it
contains:

> ///
> /// Signed value of native width.  (4 bytes on supported 32-bit processor instructions,
> /// 8 bytes on supported 64-bit processor instructions)
> /// "long" type scales to the processor native size with EBC compiler
> ///
> typedef long INTN;
> ///
> /// The unsigned value of native width.  (4 bytes on supported 32-bit processor instructions;
> /// 8 bytes on supported 64-bit processor instructions)
> /// "long" type scales to the processor native size with the EBC compiler.
> ///
> typedef unsigned long UINTN;

In other words, the size of UINTN *is* chosen at compile time, but the
choice cannot be determined from MDE_CPU_EBC. And no other macro that
"MdePkg/Include/Ebc/ProcessorBind.h" defines seems to be suitable for
investigation with #if preprocessing directives.

Anyway: the explicit (UINT64) cast with the open-coded %Lu conversion
specifier will work fine on EBC as well, so I suggest sticking with
that.

)

Laszlo


  parent reply	other threads:[~2023-03-24 14:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-24 13:41 [PATCH 1/1] UefiCpuPkg/PiSmmCpuDxeSmm: fix format string Gerd Hoffmann
2023-03-24 13:59 ` Ni, Ray
2023-03-24 15:27   ` Michael D Kinney
2023-03-24 15:35     ` Michael D Kinney
2023-03-24 14:56 ` Laszlo Ersek [this message]
2023-03-24 14:58   ` Laszlo Ersek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ddbd935b-3b91-96c4-844a-598312dac65e@redhat.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox