All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
To: Jaroslav Kysela <perex@perex.cz>
Cc: tiwai@suse.de, alsa-devel@alsa-project.org, tanjeff@cccmz.de
Subject: Re: [alsa-lib][PATCH 0/6] add API of equality and comparison for a pair of control element IDs
Date: Mon, 22 Mar 2021 13:41:03 +0900	[thread overview]
Message-ID: <20210322044103.GA3554@laptop> (raw)
In-Reply-To: <fa976b85-2d27-6e5f-9811-21e5031e39c2@perex.cz>

Hi Jaroslav,

Sorry to be my late reply but I have some private troubles (to search my
domestic cat back to the street several days ago.).

On Thu, Mar 18, 2021 at 06:04:36PM +0100, Jaroslav Kysela wrote:
> Dne 18. 03. 21 v 17:37 Takashi Sakamoto napsal(a):
> > On Thu, Mar 18, 2021 at 12:42:30PM +0100, Jaroslav Kysela wrote:
> >> Dne 18. 03. 21 v 11:30 Takashi Sakamoto napsal(a):
> >>> Hi,
> >>>
> >>> This patchset is a fix for bug issued in the message thread[1].
> >>>
> >>> In this development period, alsa-lib got new API as implementation for
> >>> one of comparison algorithms to a pair of control element IDs. However,
> >>> it has several issues.
> >>>
> >>> At first, the name, 'snd_ctl_elem_id_compare()', is inappropriate since it
> >>> implements one of comparison algorithms. The name itself implies the
> >>> algorithm is single and unique for control element ID. However, the
> >>> target structure, 'struct snd_ctl_elem_id', is hybrid and compound one.
> >>> We can not find such single and unique comparison algorithm for it.
> >>>
> >>> Secondary, it subtracts a pair of values in fields of 'unsigned int' type
> >>> in storage size of the type. It brings integer overflow.
> >>
> >> I don't think that this extra handling is really required. The unsigned /
> >> signed conversions are well known and the overflow results with a negative
> >> signed value. Why add more branches to the instruction chain?
> >  
> > For this kind of question, it's preferable to write actual test:
> > 
> > ```
> > int main()
> > {
> >   snd_ctl_elem_id_t *l, *r;
> > 
> >   snd_ctl_elem_id_alloca(&l);
> >   snd_ctl_elem_id_alloca(&r);
> > 
> >   snd_ctl_elem_id_set_device(l, 0);
> >   snd_ctl_elem_id_set_device(r, UINT_MAX);
> > 
> >   assert(snd_ctl_elem_id_compare(l, r) < 0);
> > 
> >   return 0;
> > }
> > ```
> > 
> > The assertion hits. For conversion detail:
> > 
> > ```
> > $ cat test1.c
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <limits.h>
> > 
> > int main()
> > {
> > 	unsigned int a, b;
> > 	int diff;
> > 
> > 	a = 0;
> > 	b = 10;
> > 	diff = a - b;
> > 	printf("%08x\n", diff);
> > 
> > 	a = 0;
> > 	b = UINT_MAX;
> > 	diff = a - b;
> > 	printf("%08x\n", diff);
> > 
> > 	return EXIT_SUCCESS;
> > }
> > ```
> > 
> > The above test results in 0x00000001 for -UINT_MAX case under x386/x86_64,
> > like:
> > 
> > ```
> > $ gcc -m32 -o ./test1 ./test1.c ; ./test1
> > fffffff6
> > 00000001
> > $ gcc -m64 -o ./test1 ./test1.c ; ./test1
> > fffffff6
> > 00000001
> > ```
> > 
> > We can see integer overflow in both machine architectures due to
> > calculation under 32 bit storage.
> > 
> > Well, let us prepare 64 bit storage for both of minuend and subtrahend
> > to get negative value in 64 bit storage. In the case, narrower conversion
> > to 32 bit integer is unavoidable since it's assigned to integer value
> > returned from the function in your implementation. In the case, converted
> > value is _not_ negative according to assignment rule in C language
> > specification.
> > 
> > ```
> > $ cat test2.c
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <limits.h>
> > 
> > int main()
> > {
> > 	unsigned int a, b;
> > 	long long diff;
> > 	int ret;
> > 
> > 	a = 0;
> > 	b = UINT_MAX;
> > 
> > 	// Calculate under 64 bit storage, then assign to 64 bit storage.
> > 	diff = (long long)a - (long long)b;
> > 	printf("%016llx\n", diff);
> > 
> > 	// Narrower conversion occurs now.
> > 	ret = (int)diff;
> > 	printf("%08x\n", ret);
> > 
> > 	return EXIT_SUCCESS;
> > }
> > $ gcc -m32 -o ./test2 ./test2.c ; ./test2
> > ffffffff00000001
> > 00000001
> > $ gcc -m64 -o ./test2 ./test2.c ; ./test2
> > ffffffff00000001
> > 00000001
> > ```
> > 
> > We can see easy example in the clause of 'Assignment operators' in the
> > specification. This is the reason to use condition branches in the patchset.
> 
> The point is that none of the compared unsigned fields is really above the
> 31-bit range, so you're trying to resolve an academical problem instead to add
> the debug checks (asserts) if the input values are in the acceptable range.
> Only the numid functions require this.

Hm. I think you have the assumption to 'device' and 'subdevice' fields.

If the value of these fields directly derived from any fields
systematically which Linux kernel or middleware of ALSA kernel stuffs
maintains with 'int' type, it would be valid. However, the decision to
assign specific value to these fields is left to driver developer, by
declaring 'struct snd_kcontrol_new'[1] in driver code. We wouldn't see such
code that the developer construct 'pseudo' device and subdevice to deliver
specific information to userspace, it could be.

(once I've investigated to use this design to ALSA firewire stack.)

Additionally, alsa-lib has plug-in framework to have several backend which
works beyond the most of API calls[2]. The 'hw' plugin is one of them,
which directly communicate to ALSA control core via system calls.
Developers and users have some opportunities to implement and use the
other backend, then they are free from your assumption. In this point,
any assumption to 'index' field is not better as well.

Of course, you can insist the above topics are not practical, something
belongs to domains of academical or logical. However, I put safety in the
first place, to avoid bugs which expectedly appears in future. I'd like
you to take enough care of downstream user's demands.

Well, if code revising is not acceptable to you, it's better to add
any assertion to check range of value as you mentioned as well as good
documentation. In this case, your function is not generic one and should
be renamed that it works conditionally. 'snd_ctl_elem_id_compare()' is not
acceptable.


[1] include/sound/control.h
https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git/tree/include/sound/control.h?h=for-next#n41
[2] External Control Plugin SDK
https://www.alsa-project.org/alsa-doc/alsa-lib/ctl_external_plugins.html


Regards

Takashi Sakamoto

      reply	other threads:[~2021-03-22  4:42 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-18 10:30 [alsa-lib][PATCH 0/6] add API of equality and comparison for a pair of control element IDs Takashi Sakamoto
2021-03-18 10:30 ` [alsa-lib][PATCH 1/6] test: ctl-elem-id: add test program for future APIs relevant to control element ID Takashi Sakamoto
2021-03-18 10:30 ` [alsa-lib][PATCH 2/6] ctl: add API to check equality between a pair of control element IDs by numid Takashi Sakamoto
2021-03-18 10:30 ` [alsa-lib][PATCH 3/6] ctl: add API to check equality between a pair of control element IDs by tuple Takashi Sakamoto
2021-03-18 10:30 ` [alsa-lib][PATCH 4/6] ctl: add API to compare a pair of control element IDs by numid Takashi Sakamoto
2021-03-18 10:30 ` [alsa-lib][PATCH 5/6] ctl: add API to compare a pair of control element IDs by one of algorithms according to tuple Takashi Sakamoto
2021-03-18 10:30 ` [alsa-lib][PATCH 6/6] ctl: drop deprecated API to compare a pair of control element IDs Takashi Sakamoto
2021-03-18 11:42 ` [alsa-lib][PATCH 0/6] add API of equality and comparison for " Jaroslav Kysela
2021-03-18 16:37   ` Takashi Sakamoto
2021-03-18 16:56     ` Takashi Sakamoto
2021-03-18 17:17       ` Jaroslav Kysela
2021-03-22  4:49         ` Takashi Sakamoto
2021-03-18 17:04     ` Jaroslav Kysela
2021-03-22  4:41       ` Takashi Sakamoto [this message]

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-all 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=20210322044103.GA3554@laptop \
    --to=o-takashi@sakamocchi.jp \
    --cc=alsa-devel@alsa-project.org \
    --cc=perex@perex.cz \
    --cc=tanjeff@cccmz.de \
    --cc=tiwai@suse.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.