lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <742f7226-9c66-4cfb-ba31-222dfb54fc34@suse.com>
Date: Wed, 28 Aug 2024 14:46:03 +0200
From: Petr Pavlu <petr.pavlu@...e.com>
To: Sami Tolvanen <samitolvanen@...gle.com>
Cc: Masahiro Yamada <masahiroy@...nel.org>,
 Luis Chamberlain <mcgrof@...nel.org>, Miguel Ojeda <ojeda@...nel.org>,
 Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
 Matthew Maurer <mmaurer@...gle.com>, Alex Gaynor <alex.gaynor@...il.com>,
 Wedson Almeida Filho <wedsonaf@...il.com>, Gary Guo <gary@...yguo.net>,
 Petr Pavlu <petr.pavlu@...e.com>, Neal Gompa <neal@...pa.dev>,
 Hector Martin <marcan@...can.st>, Janne Grunau <j@...nau.net>,
 Asahi Linux <asahi@...ts.linux.dev>, linux-kbuild@...r.kernel.org,
 linux-kernel@...r.kernel.org, linux-modules@...r.kernel.org,
 rust-for-linux@...r.kernel.org
Subject: Re: [PATCH v2 05/19] gendwarfksyms: Expand base_type

On 8/15/24 19:39, Sami Tolvanen wrote:
> Start making gendwarfksyms more useful by adding support for
> expanding DW_TAG_base_type types and basic DWARF attributes.
> 
> Example:
> 
>   $ echo loops_per_jiffy | \
>       scripts/gendwarfksyms/gendwarfksyms --debug vmlinux.o
>   ...
>   gendwarfksyms: process_exported_symbols: loops_per_jiffy
>   variable base_type unsigned long byte_size(8) encoding(7);
>   ...
> 
> Signed-off-by: Sami Tolvanen <samitolvanen@...gle.com>
> ---
>  scripts/gendwarfksyms/dwarf.c | 121 +++++++++++++++++++++++++++++++++-
>  1 file changed, 120 insertions(+), 1 deletion(-)
> 
> diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c
> index 956b30224316..a37c9049d18e 100644
> --- a/scripts/gendwarfksyms/dwarf.c
> +++ b/scripts/gendwarfksyms/dwarf.c
> @@ -5,6 +5,17 @@
>  
>  #include "gendwarfksyms.h"
>  
> +#define DEFINE_GET_ATTR(attr, type)                                    \
> +	static bool get_##attr##_attr(Dwarf_Die *die, unsigned int id, \
> +				      type *value)                     \
> +	{                                                              \
> +		Dwarf_Attribute da;                                    \
> +		return dwarf_attr(die, id, &da) &&                     \
> +		       !dwarf_form##attr(&da, value);                  \
> +	}
> +
> +DEFINE_GET_ATTR(udata, Dwarf_Word)
> +
>  static bool get_ref_die_attr(Dwarf_Die *die, unsigned int id, Dwarf_Die *value)
>  {
>  	Dwarf_Attribute da;
> @@ -60,6 +71,74 @@ static int process(struct state *state, const char *s)
>  	return 0;
>  }
>  
> +#define MAX_FMT_BUFFER_SIZE 128
> +
> +static int process_fmt(struct state *state, const char *fmt, ...)

Nit: The state parameter is unused by a number of these process_*()
functions, including the leaf process(). I suggest removing it so it
doesn't need to be passed around unnecessarily.

> +{
> +	char buf[MAX_FMT_BUFFER_SIZE];
> +	va_list args;
> +	int res;
> +
> +	va_start(args, fmt);
> +
> +	res = checkp(vsnprintf(buf, sizeof(buf), fmt, args));
> +	if (res >= MAX_FMT_BUFFER_SIZE - 1) {

This check looks off by one, though on the safe side:
res >= sizeof(buf)

> +		error("vsnprintf overflow: increase MAX_FMT_BUFFER_SIZE");
> +		res = -1;
> +	} else {
> +		res = check(process(state, buf));
> +	}
> +
> +	va_end(args);
> +	return res;
> +}
> +
> +/* Process a fully qualified name from DWARF scopes */
> +static int process_fqn(struct state *state, Dwarf_Die *die)
> +{
> +	Dwarf_Die *scopes = NULL;
> +	const char *name;
> +	int res;
> +	int i;
> +
> +	res = checkp(dwarf_getscopes_die(die, &scopes));
> +	if (!res) {
> +		name = get_name(die);
> +		name = name ?: "<unnamed>";
> +		return check(process(state, name));
> +	}
> +
> +	for (i = res - 1; i >= 0; i--) {
> +		if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
> +			continue;
> +
> +		name = get_name(&scopes[i]);
> +		name = name ?: "<unnamed>";
> +		check(process(state, name));
> +		if (i > 0)
> +			check(process(state, "::"));

Failed check(process()) calls here return immediately and so would leak
scopes. However, I see this is fixed in the following patch
"gendwarfksyms: Add a cache for processed DIEs" so it's ok.

> +	}
> +
> +	free(scopes);
> +	return 0;
> +}
> +
> +#define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute)                         \
> +	static int process_##attribute##_attr(struct state *state,        \
> +					      Dwarf_Die *die)             \
> +	{                                                                 \
> +		Dwarf_Word value;                                         \
> +		if (get_udata_attr(die, DW_AT_##attribute, &value))       \
> +			check(process_fmt(state,                          \
> +					  " " #attribute "(%" PRIu64 ")", \
> +					  value));                        \
> +		return 0;                                                 \
> +	}
> +
> +DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment)
> +DEFINE_PROCESS_UDATA_ATTRIBUTE(byte_size)
> +DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding)
> +
>  bool match_all(Dwarf_Die *die)
>  {
>  	return true;
> @@ -81,6 +160,44 @@ int process_die_container(struct state *state, Dwarf_Die *die,
>  	return 0;
>  }
>  
> +static int process_type(struct state *state, Dwarf_Die *die);
> +
> +static int process_type_attr(struct state *state, Dwarf_Die *die)
> +{
> +	Dwarf_Die type;
> +
> +	if (get_ref_die_attr(die, DW_AT_type, &type))
> +		return check(process_type(state, &type));
> +
> +	/* Compilers can omit DW_AT_type -- print out 'void' to clarify */
> +	return check(process(state, "base_type void"));
> +}
> +
> +static int process_base_type(struct state *state, Dwarf_Die *die)
> +{
> +	check(process(state, "base_type "));
> +	check(process_fqn(state, die));
> +	check(process_byte_size_attr(state, die));
> +	check(process_encoding_attr(state, die));
> +	return check(process_alignment_attr(state, die));
> +}
> +
> +static int process_type(struct state *state, Dwarf_Die *die)
> +{
> +	int tag = dwarf_tag(die);
> +
> +	switch (tag) {
> +	case DW_TAG_base_type:
> +		check(process_base_type(state, die));
> +		break;
> +	default:
> +		debug("unimplemented type: %x", tag);
> +		break;
> +	}
> +
> +	return 0;
> +}
> +
>  /*
>   * Exported symbol processing
>   */
> @@ -91,7 +208,9 @@ static int process_subprogram(struct state *state, Dwarf_Die *die)
>  
>  static int process_variable(struct state *state, Dwarf_Die *die)
>  {
> -	return check(process(state, "variable;\n"));
> +	check(process(state, "variable "));
> +	check(process_type_attr(state, die));
> +	return check(process(state, ";\n"));
>  }
>  
>  static int process_symbol_ptr(struct state *state, Dwarf_Die *die)

-- 
Thanks,
Petr

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ