Re: [PATCH v12 2/3] selftests: tdx: Test TDX attestation GetReport support

From: Sathyanarayanan Kuppuswamy
Date: Fri Sep 09 2022 - 14:40:25 EST




On 9/9/22 6:36 AM, Wander Lairson Costa wrote:
> On Thu, Sep 8, 2022 at 8:45 PM Sathyanarayanan Kuppuswamy
> <sathyanarayanan.kuppuswamy@xxxxxxxxxxxxxxx> wrote:
>>
>>
>>
>> On 9/8/22 7:16 AM, Wander Lairson Costa wrote:
>>>> +#ifdef DEBUG
>>>> +static void print_array_hex(const char *title, const char *prefix_str,
>>>> + const void *buf, int len)
>>>> +{
>>>> + const __u8 *ptr = buf;
>>>> + int i, rowsize = HEX_DUMP_SIZE;
>>>> +
>>>> + if (!len || !buf)
>>>> + return;
>>>> +
>>>> + printf("\t\t%s", title);
>>>> +
>>>> + for (i = 0; i < len; i++) {
>>>> + if (!(i % rowsize))
>>>> + printf("\n%s%.8x:", prefix_str, i);
>>>> + printf(" %.2x", ptr[i]);
>>>> + }
>>>> +
>>>> + printf("\n");
>>>> +}
>>>> +#endif
>>>> +
>>>> +TEST(verify_report)
>>>> +{
>>>> + __u8 reportdata[TDX_REPORTDATA_LEN];
>>>> + struct tdreport tdreport;
>>>> + struct tdx_report_req req;
>>>> + int devfd, i;
>>>> +
>>>> + devfd = open(TDX_GUEST_DEVNAME, O_RDWR | O_SYNC);
>>>> +
>>>> + ASSERT_LT(0, devfd);
>>>> +
>>>> + /* Generate sample report data */
>>>> + for (i = 0; i < TDX_REPORTDATA_LEN; i++)
>>>> + reportdata[i] = i;
>>>> +
>>>> + /* Initialize IOCTL request */
>>>> + req.subtype = 0;
>>>> + req.reportdata = (__u64)reportdata;
>>>> + req.rpd_len = TDX_REPORTDATA_LEN;
>>>> + req.tdreport = (__u64)&tdreport;
>>>> + req.tdr_len = sizeof(tdreport);
>>>> +
>>>> + /* Get TDREPORT */
>>>> + ASSERT_EQ(0, ioctl(devfd, TDX_CMD_GET_REPORT, &req));
>>>> +
>>>> +#ifdef DEBUG
>>>> + print_array_hex("\n\t\tTDX report data\n", "",
>>>> + reportdata, sizeof(reportdata));
>>>> +
>>>> + print_array_hex("\n\t\tTDX tdreport data\n", "",
>>>> + &tdreport, sizeof(tdreport));
>>>> +#endif
>>> You can unconditionally define print_array_hex, and
>>> use `if (DEBUG)` instead of #ifdef `DEBUG here`. The compiler
>>> will get rid of the unused code when DEBUG is not defined
>>> as expected, but you get the parser to validate it
>>> independent of the definition of DEBUG.
>>
>> Currently, DEBUG is a macro, so we cannot use if (DEBUG) directly.
>> You are suggesting to change DEBUG to a variable? Any reason to
>> make this change? I think both changes are functionally similar.
>> So I am wondering why to make this change?
>>
>
> My thought is always to define DEBUG. If in debug mode it is defined
> to 1; otherwise to 0.
> Then, you can use `if (DEBUG)` instead of `#ifdef DEBUG`. But the
> former will always check the syntax of the debug code,
> independent of the value of DEBUG, and the compiler will generate the
> same code. The GNU coding standard [1] explains that
> better than I do.
>
> [1] https://www.gnu.org/prep/standards/standards.html#Conditional-Compilation

Got it. I will use if (DEBUG).

>
>>>
>>
>> --
>> Sathyanarayanan Kuppuswamy
>> Linux Kernel Developer
>>
>

--
Sathyanarayanan Kuppuswamy
Linux Kernel Developer