diff mbox series

[1/6] symbols: add minimal self-test

Message ID a556439c-b652-4789-bbdd-6d6402b2a124@suse.com (mailing list archive)
State New
Headers show
Series symbols: assorted adjustments | expand

Commit Message

Jan Beulich March 13, 2025, 1:52 p.m. UTC
... before making changes to the involved logic.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
With this FAST_SYMBOL_LOOKUP may make sense to permit enabling even
when LIVEPATCH=n. Thoughts? (In this case "symbols: centralize and re-
arrange $(all_symbols) calculation" would want pulling ahead.)

Comments

Andrew Cooper March 13, 2025, 3:35 p.m. UTC | #1
On 13/03/2025 1:52 pm, Jan Beulich wrote:
> ... before making changes to the involved logic.
>
> Signed-off-by: Jan Beulich <jbeulich@suse.com>
> ---
> With this FAST_SYMBOL_LOOKUP may make sense to permit enabling even
> when LIVEPATCH=n. Thoughts? (In this case "symbols: centralize and re-
> arrange $(all_symbols) calculation" would want pulling ahead.)
>
> --- a/xen/common/symbols.c
> +++ b/xen/common/symbols.c
> @@ -260,6 +260,41 @@ unsigned long symbols_lookup_by_name(con
>      return 0;
>  }
>  
> +#ifdef CONFIG_SELF_TESTS
> +
> +static void __init test_lookup(unsigned long addr, const char *expected)
> +{
> +    char buf[KSYM_NAME_LEN + 1];
> +    const char *name, *symname;
> +    unsigned long size, offs;
> +
> +    name = symbols_lookup(addr, &size, &offs, buf);
> +    if ( !name )
> +        panic("%s: address not found\n", expected);
> +    if ( offs )
> +        panic("%s: non-zero offset (%#lx) unexpected\n", expected, offs);
> +
> +    /* Cope with static symbols, where varying file names/paths may be used. */
> +    symname = strchr(name, '#');
> +    symname = symname ? symname + 1 : name;
> +    if ( strcmp(symname, expected) )
> +        panic("%s: unexpected symbol name: '%s'\n", expected, symname);
> +
> +    offs = symbols_lookup_by_name(name);
> +    if ( offs != addr )
> +        panic("%s: address %#lx unexpected; wanted %#lx\n",
> +              expected, offs, addr);
> +}
> +
> +static void __init __constructor test_symbols(void)
> +{
> +    /* Be sure to only try this for cf_check functions. */

I'm very happy to see the take-up of SELF_TESTs.  Although I probably
ought to tie it into a Kconfig option to make the errors non-fatal,
which I've been meaning to do for a bit.

One question though.  cf_check is an x86-ism, even if it leaks out into
common code.

I think you mean "functions emitted into the final image"?  If so, I
don't think this is relevant then, because ...

> +    test_lookup((unsigned long)dump_execstate, "dump_execstate");
> +    test_lookup((unsigned long)test_symbols, __func__);

... taking the function address here forces it to be emitted even if it
would otherwise have been inlined.

~Andrew
Jan Beulich March 13, 2025, 3:39 p.m. UTC | #2
On 13.03.2025 16:35, Andrew Cooper wrote:
> On 13/03/2025 1:52 pm, Jan Beulich wrote:
>> ... before making changes to the involved logic.
>>
>> Signed-off-by: Jan Beulich <jbeulich@suse.com>
>> ---
>> With this FAST_SYMBOL_LOOKUP may make sense to permit enabling even
>> when LIVEPATCH=n. Thoughts? (In this case "symbols: centralize and re-
>> arrange $(all_symbols) calculation" would want pulling ahead.)
>>
>> --- a/xen/common/symbols.c
>> +++ b/xen/common/symbols.c
>> @@ -260,6 +260,41 @@ unsigned long symbols_lookup_by_name(con
>>      return 0;
>>  }
>>  
>> +#ifdef CONFIG_SELF_TESTS
>> +
>> +static void __init test_lookup(unsigned long addr, const char *expected)
>> +{
>> +    char buf[KSYM_NAME_LEN + 1];
>> +    const char *name, *symname;
>> +    unsigned long size, offs;
>> +
>> +    name = symbols_lookup(addr, &size, &offs, buf);
>> +    if ( !name )
>> +        panic("%s: address not found\n", expected);
>> +    if ( offs )
>> +        panic("%s: non-zero offset (%#lx) unexpected\n", expected, offs);
>> +
>> +    /* Cope with static symbols, where varying file names/paths may be used. */
>> +    symname = strchr(name, '#');
>> +    symname = symname ? symname + 1 : name;
>> +    if ( strcmp(symname, expected) )
>> +        panic("%s: unexpected symbol name: '%s'\n", expected, symname);
>> +
>> +    offs = symbols_lookup_by_name(name);
>> +    if ( offs != addr )
>> +        panic("%s: address %#lx unexpected; wanted %#lx\n",
>> +              expected, offs, addr);
>> +}
>> +
>> +static void __init __constructor test_symbols(void)
>> +{
>> +    /* Be sure to only try this for cf_check functions. */
> 
> I'm very happy to see the take-up of SELF_TESTs.  Although I probably
> ought to tie it into a Kconfig option to make the errors non-fatal,
> which I've been meaning to do for a bit.
> 
> One question though.  cf_check is an x86-ism, even if it leaks out into
> common code.
> 
> I think you mean "functions emitted into the final image"?  If so, I
> don't think this is relevant then, because ...
> 
>> +    test_lookup((unsigned long)dump_execstate, "dump_execstate");
>> +    test_lookup((unsigned long)test_symbols, __func__);
> 
> ... taking the function address here forces it to be emitted even if it
> would otherwise have been inlined.

No, I really mean cf_check. If we took the address of a non-cf_check
function, the special gcc13 build's checking would trigger, aiui.

Jan
Andrew Cooper March 13, 2025, 3:44 p.m. UTC | #3
On 13/03/2025 3:39 pm, Jan Beulich wrote:
> On 13.03.2025 16:35, Andrew Cooper wrote:
>> On 13/03/2025 1:52 pm, Jan Beulich wrote:
>>> ... before making changes to the involved logic.
>>>
>>> Signed-off-by: Jan Beulich <jbeulich@suse.com>
>>> ---
>>> With this FAST_SYMBOL_LOOKUP may make sense to permit enabling even
>>> when LIVEPATCH=n. Thoughts? (In this case "symbols: centralize and re-
>>> arrange $(all_symbols) calculation" would want pulling ahead.)
>>>
>>> --- a/xen/common/symbols.c
>>> +++ b/xen/common/symbols.c
>>> @@ -260,6 +260,41 @@ unsigned long symbols_lookup_by_name(con
>>>      return 0;
>>>  }
>>>  
>>> +#ifdef CONFIG_SELF_TESTS
>>> +
>>> +static void __init test_lookup(unsigned long addr, const char *expected)
>>> +{
>>> +    char buf[KSYM_NAME_LEN + 1];
>>> +    const char *name, *symname;
>>> +    unsigned long size, offs;
>>> +
>>> +    name = symbols_lookup(addr, &size, &offs, buf);
>>> +    if ( !name )
>>> +        panic("%s: address not found\n", expected);
>>> +    if ( offs )
>>> +        panic("%s: non-zero offset (%#lx) unexpected\n", expected, offs);
>>> +
>>> +    /* Cope with static symbols, where varying file names/paths may be used. */
>>> +    symname = strchr(name, '#');
>>> +    symname = symname ? symname + 1 : name;
>>> +    if ( strcmp(symname, expected) )
>>> +        panic("%s: unexpected symbol name: '%s'\n", expected, symname);
>>> +
>>> +    offs = symbols_lookup_by_name(name);
>>> +    if ( offs != addr )
>>> +        panic("%s: address %#lx unexpected; wanted %#lx\n",
>>> +              expected, offs, addr);
>>> +}
>>> +
>>> +static void __init __constructor test_symbols(void)
>>> +{
>>> +    /* Be sure to only try this for cf_check functions. */
>> I'm very happy to see the take-up of SELF_TESTs.  Although I probably
>> ought to tie it into a Kconfig option to make the errors non-fatal,
>> which I've been meaning to do for a bit.
>>
>> One question though.  cf_check is an x86-ism, even if it leaks out into
>> common code.
>>
>> I think you mean "functions emitted into the final image"?  If so, I
>> don't think this is relevant then, because ...
>>
>>> +    test_lookup((unsigned long)dump_execstate, "dump_execstate");
>>> +    test_lookup((unsigned long)test_symbols, __func__);
>> ... taking the function address here forces it to be emitted even if it
>> would otherwise have been inlined.
> No, I really mean cf_check. If we took the address of a non-cf_check
> function, the special gcc13 build's checking would trigger, aiui.

It's GCC-11 sadly.  cf_check is part of the function type, and triggers
when a function type check would be relevant.  Just casing to an integer
won't trigger it, I don't think.

~Andrew
Jan Beulich March 13, 2025, 4:04 p.m. UTC | #4
On 13.03.2025 16:44, Andrew Cooper wrote:
> On 13/03/2025 3:39 pm, Jan Beulich wrote:
>> On 13.03.2025 16:35, Andrew Cooper wrote:
>>> On 13/03/2025 1:52 pm, Jan Beulich wrote:
>>>> ... before making changes to the involved logic.
>>>>
>>>> Signed-off-by: Jan Beulich <jbeulich@suse.com>
>>>> ---
>>>> With this FAST_SYMBOL_LOOKUP may make sense to permit enabling even
>>>> when LIVEPATCH=n. Thoughts? (In this case "symbols: centralize and re-
>>>> arrange $(all_symbols) calculation" would want pulling ahead.)
>>>>
>>>> --- a/xen/common/symbols.c
>>>> +++ b/xen/common/symbols.c
>>>> @@ -260,6 +260,41 @@ unsigned long symbols_lookup_by_name(con
>>>>      return 0;
>>>>  }
>>>>  
>>>> +#ifdef CONFIG_SELF_TESTS
>>>> +
>>>> +static void __init test_lookup(unsigned long addr, const char *expected)
>>>> +{
>>>> +    char buf[KSYM_NAME_LEN + 1];
>>>> +    const char *name, *symname;
>>>> +    unsigned long size, offs;
>>>> +
>>>> +    name = symbols_lookup(addr, &size, &offs, buf);
>>>> +    if ( !name )
>>>> +        panic("%s: address not found\n", expected);
>>>> +    if ( offs )
>>>> +        panic("%s: non-zero offset (%#lx) unexpected\n", expected, offs);
>>>> +
>>>> +    /* Cope with static symbols, where varying file names/paths may be used. */
>>>> +    symname = strchr(name, '#');
>>>> +    symname = symname ? symname + 1 : name;
>>>> +    if ( strcmp(symname, expected) )
>>>> +        panic("%s: unexpected symbol name: '%s'\n", expected, symname);
>>>> +
>>>> +    offs = symbols_lookup_by_name(name);
>>>> +    if ( offs != addr )
>>>> +        panic("%s: address %#lx unexpected; wanted %#lx\n",
>>>> +              expected, offs, addr);
>>>> +}
>>>> +
>>>> +static void __init __constructor test_symbols(void)
>>>> +{
>>>> +    /* Be sure to only try this for cf_check functions. */
>>> I'm very happy to see the take-up of SELF_TESTs.  Although I probably
>>> ought to tie it into a Kconfig option to make the errors non-fatal,
>>> which I've been meaning to do for a bit.
>>>
>>> One question though.  cf_check is an x86-ism, even if it leaks out into
>>> common code.
>>>
>>> I think you mean "functions emitted into the final image"?  If so, I
>>> don't think this is relevant then, because ...
>>>
>>>> +    test_lookup((unsigned long)dump_execstate, "dump_execstate");
>>>> +    test_lookup((unsigned long)test_symbols, __func__);
>>> ... taking the function address here forces it to be emitted even if it
>>> would otherwise have been inlined.
>> No, I really mean cf_check. If we took the address of a non-cf_check
>> function, the special gcc13 build's checking would trigger, aiui.
> 
> It's GCC-11 sadly.  cf_check is part of the function type, and triggers
> when a function type check would be relevant.  Just casing to an integer
> won't trigger it, I don't think.

Is there a way to double check? I'd be happy to drop that comment (and
use some other, maybe less random function), but I don't have a compiler
available that includes that patch.

Jan
diff mbox series

Patch

--- a/xen/common/symbols.c
+++ b/xen/common/symbols.c
@@ -260,6 +260,41 @@  unsigned long symbols_lookup_by_name(con
     return 0;
 }
 
+#ifdef CONFIG_SELF_TESTS
+
+static void __init test_lookup(unsigned long addr, const char *expected)
+{
+    char buf[KSYM_NAME_LEN + 1];
+    const char *name, *symname;
+    unsigned long size, offs;
+
+    name = symbols_lookup(addr, &size, &offs, buf);
+    if ( !name )
+        panic("%s: address not found\n", expected);
+    if ( offs )
+        panic("%s: non-zero offset (%#lx) unexpected\n", expected, offs);
+
+    /* Cope with static symbols, where varying file names/paths may be used. */
+    symname = strchr(name, '#');
+    symname = symname ? symname + 1 : name;
+    if ( strcmp(symname, expected) )
+        panic("%s: unexpected symbol name: '%s'\n", expected, symname);
+
+    offs = symbols_lookup_by_name(name);
+    if ( offs != addr )
+        panic("%s: address %#lx unexpected; wanted %#lx\n",
+              expected, offs, addr);
+}
+
+static void __init __constructor test_symbols(void)
+{
+    /* Be sure to only try this for cf_check functions. */
+    test_lookup((unsigned long)dump_execstate, "dump_execstate");
+    test_lookup((unsigned long)test_symbols, __func__);
+}
+
+#endif /* CONFIG_SELF_TESTS */
+
 /*
  * Local variables:
  * mode: C