Re: [PATCH] perf: fix bug mismatch with -c option definition

From: Stephane Eranian
Date: Mon May 17 2010 - 11:39:26 EST


On Mon, May 17, 2010 at 4:19 PM, Arnaldo Carvalho de Melo
<acme@xxxxxxxxxxxxx> wrote:
> Em Mon, May 17, 2010 at 12:04:01PM +0200, Stephane Eranian escreveu:
>> Â The -c option defines the user requested sampling period. It was implemented
>> Â using an unsigned int variable but the type of the option was OPT_LONG. Thus,
>> Â the option parser was overwriting memory belonging to other variables, namely
>> Â the mmap_pages leading to a zero page sampling buffer. The bug was exposed
>> Â only when compiling at -O0, probably because the compiler was padding
>> Â variables at higher optimization levels.
>
> Well spotted!

As you can imagine, it was not so trivial to find ;->

>
>> Â This patch fixes this problem by declaring user_interval as u64. This also
>> Â avoids wrap-around issues for large period on 32-bit systems.
>>
>> Â Signed-off-by: Stephane Eranian <eranian@xxxxxxxxxx>
>> --
>> Âtools/perf/builtin-record.c | Â Â8 ++++----
>> Â1 file changed, 4 insertions(+), 4 deletions(-)
>>
>> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
>> index 0f467cf..78f64cc 100644
>> --- a/tools/perf/builtin-record.c
>> +++ b/tools/perf/builtin-record.c
>> @@ -33,8 +33,8 @@ enum write_mode_t {
>>
>> Âstatic int          *fd[MAX_NR_CPUS][MAX_COUNTERS];
>>
>> -static unsigned int     Âuser_interval          = UINT_MAX;
>> -static long         Âdefault_interval        Â=   Â0;
>> +static u64          user_interval          = ULLONG_MAX;
>> +static u64          default_interval        Â=   Â0;
>
> The parsing code uses this for OPT_LONG:
>
> Â Â Â Âcase OPTION_LONG:
> Â Â Â Â Â Â Â Âif (unset) {
> Â Â Â Â Â Â Â Â Â Â Â Â*(long *)opt->value = 0;
> Â Â Â Â Â Â Â Â Â Â Â Âreturn 0;
> Â Â Â Â Â Â Â Â}
> Â Â Â Â Â Â Â Âif (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
> Â Â Â Â Â Â Â Â Â Â Â Â*(long *)opt->value = opt->defval;
> Â Â Â Â Â Â Â Â Â Â Â Âreturn 0;
> Â Â Â Â Â Â Â Â}
> Â Â Â Â Â Â Â Âif (get_arg(p, opt, flags, &arg))
> Â Â Â Â Â Â Â Â Â Â Â Âreturn -1;
> Â Â Â Â Â Â Â Â*(long *)opt->value = strtol(arg, (char **)&s, 10);
>
> So I think we should augment the parsing code to have OPTION_ULONG, and,
> for handling u64, OPTION_ULLONG.
>
> I'll add that and then modify your patch to use it.
>
That's fine too.
Thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/