Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASoC: SOF/Intel/AMD/Mediatek/IMX: enable GCC '-fanalyzer' checks #4340

Merged
merged 10 commits into from
Jul 13, 2023

Conversation

plbossart
Copy link
Member

Cleanups to work-around issues, prior to enable this static analysis by default.

It's pretty interesting that none of the other tools reported these issues....

@dbaluta @yaochunhung @kuanhsuncheng @crojewsk-intel @ujfalusi @ranj063 @marc-hb feedback welcome.

@marc-hb
Copy link
Collaborator

marc-hb commented May 6, 2023

https://github.com/thesofproject/linux/actions/runs/4897771652/jobs/8746195783?pr=4340 failed like this:

gcc: error: unrecognized command line option ‘-fanalyzer’

Try an Ubuntu upgrade like this one:

Even if not enough it won't hurt anyway!

@marc-hb
Copy link
Collaborator

marc-hb commented May 6, 2023

It's not a problem today, since we do
check in the first use of the function, but static analysis tools are
not aware of the different ALSA stages. Rather than argue forever,
let's just add the error checks consistently and make sure this tool
can be added to the CI checks.

I like this pragmatic approach. I would add that it's NOT just to please static analyzers and silence false positives:

  • This also saves time for human code reviewers / "human analyzers"
  • Defensive programming makes the code more future proof. The conditions that are true today may be different tomorrow.

This is a false-positive, the GCC analyzer generated that report by
considering if (bytes->block) as true in some cases and false in
others.

This one looks more like a relatively simple bug that should be reported to GCC? Does it still show with cutting edge GCC? What is the most recent GCC version you tried?

@plbossart
Copy link
Member Author

plbossart commented May 8, 2023

This '-fanalyzer' option was introduced with GCC 10

https://gcc.gnu.org/onlinedocs/gcc-10.1.0/gcc/Static-Analyzer-Options.html

I have GCC11 with Ubuntu 22.10

@plbossart plbossart force-pushed the fix/gcc-fanalyzer branch from 3c9b6ae to 7ef818d Compare May 8, 2023 12:38
@plbossart plbossart force-pushed the fix/gcc-fanalyzer branch 2 times, most recently from 98e6f21 to 5a3c647 Compare May 8, 2023 15:53
@plbossart plbossart marked this pull request as ready for review May 8, 2023 15:54
@marc-hb
Copy link
Collaborator

marc-hb commented May 8, 2023

Debian/Ubuntu support multiple gcc versions in a single distribution release BUT only one default version. For Ubuntu 20.04, the default gcc version was gcc 9.3: https://packages.ubuntu.com/focal/devel/

I see you've updated to ubuntu-latest which currently points at ubuntu 22.04 which fixes the failure. I would not point at ubuntu-latest because it may change at a very inconvenient moment, I would go for ubuntu-22.04 instead. Github has historically provided only LTS runners (used by 95% of Ubuntu users according to Ubuntu stats) so manually upgrading every 2 years seems like a much smaller and less risky inconvenience. No big deal, up to you!

https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners
https://ubuntu.com/about/release-cycle

@plbossart
Copy link
Member Author

I would not point at ubuntu-latest because it may change at a very inconvenient moment, I would go for ubuntu-22.04 instead.

Yeah, that's what I had initially and then I changed my position. How would we know that something is broken with ubuntu-latest if we don't test it? In other words, let's use GitHub Actions to test GitHub actions. If things break we can always go back to the last version known to work.

@marc-hb
Copy link
Collaborator

marc-hb commented May 8, 2023

How would we know that something is broken with ubuntu-latest if we don't test it?

I've never seen this being a real issue in any project. Linux distributions are very good with backwards compatibility, especially LTS releases. Now Ubuntu bugs and gcc bugs do exist of course (examples https://github.com/thesofproject/sof-test/discussions, thesofproject/sof#7114,...) but I've never seen any affect something as common as compiling the kernel.

The converse is much more common: starting to use some brand new features without realizing it. That's why it's good for CI to always lag some (reasonable) distance behind developers. CI effectively defines the minimum requirements.

Moreever CI never stays stuck on old releases for a very long time because some new fancy feature like -fanalyzer is eventually found missing which forces an upgrade: exactly what is happening here right now.

Just sharing my experience, again no big deal.

@marc-hb marc-hb requested a review from andyross May 8, 2023 21:07
ipc->private_data = data;
}

static inline void *imx_dsp_get_data(struct imx_dsp_ipc *ipc)
{
if (!ipc)
return NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is not aligned with most of changes in this PR. Can we check ipc before it is used ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this quite different, the other cases are more complicated and will have multiple cases and tests before returning NULL. Here it's just a straightforward test of something being NULL.

But that would be for @dbaluta @paulstelian97 to comment, I don't have any specific opinion on this code.

@plbossart plbossart force-pushed the fix/gcc-fanalyzer branch from 5a3c647 to bb5385e Compare May 16, 2023 16:13
@plbossart
Copy link
Member Author

No reply from @crojewsk-intel, dropped AVS patches and dropped AVS from the checks.

@plbossart plbossart requested a review from RanderWang May 16, 2023 16:14
@crojewsk-intel
Copy link

Sorry for the late reply. My two cents (example based on bdw_rt286):

struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, "rt286-aif1");

if (!codec_dai)
	return 0;

in my opinion is an invalid scenario. Given the DAI LINKs declaration that is part of the machine board driver, such situation may never occur - if e.g.: rt286-aif1 is missing, sound card won't even be enumerated.

Another thing to consider is correctness against PM expectations. That is, on suspend we should not prevent the system-wide suspend so 0 shall be returned regardless but on resume that's not the case and errors should be propagated. Of course, there's another level of complication as results snd_soc_card_suspend_pre() and snd_soc_card_resume_post() are completely ignored by the framework. Oh well..

RanderWang
RanderWang previously approved these changes May 18, 2023
sound/soc/sof/intel/hda-dai.c Outdated Show resolved Hide resolved
sound/soc/sof/topology.c Show resolved Hide resolved
sound/soc/intel/atom/sst/sst_stream.c Show resolved Hide resolved
.github/workflows/buildtest.yml Outdated Show resolved Hide resolved
sound/soc/intel/atom/sst/sst_stream.c Show resolved Hide resolved
@plbossart
Copy link
Member Author

struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, "rt286-aif1");

if (!codec_dai)
	return 0;

in my opinion is an invalid scenario. Given the DAI LINKs declaration that is part of the machine board driver, such situation may never occur - if e.g.: rt286-aif1 is missing, sound card won't even be enumerated.

Static analysis can't know that. It just looks at the result of snd_soc_card_get_codec_dai() and tells you there is one possible case of NULL dereference.

@plbossart plbossart requested a review from ranj063 May 22, 2023 21:53
@plbossart plbossart force-pushed the fix/gcc-fanalyzer branch from e97bbb8 to 45bb03b Compare May 22, 2023 22:01
@plbossart plbossart mentioned this pull request May 30, 2023
plbossart added 10 commits June 29, 2023 11:33
…sis warnings

make KCFLAGS='-fanalyzer' sound/soc/sof/ reports several NULL pointer
dereference paths.

sof_ipc4_probe_get_module_info() can return a NULL value, but it's
only tested in the init state. Static analyzers cannot know the probe
state machine and hence flags a potential issue for all calls of that
function.

Squelch these errors by adding the same check consistently.

Signed-off-by: Pierre-Louis Bossart <[email protected]>
make KCFLAGS='-fanalyzer' sound/soc/sof/ reports an issue with memcpy:

sound/soc/sof/ipc3.c: In function ‘ipc3_wait_tx_done’:
sound/soc/sof/ipc3.c:309:33: error: use of NULL ‘reply_data’ where
non-null expected [CWE-476] [-Werror=analyzer-null-argument]

  309 |                        memcpy(reply_data, msg->reply_data,
      |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  310 |                               msg->reply_size);

The finding is legit with this call:
    return sof_ipc3_tx_msg(sdev, &pm_ctx, sizeof(pm_ctx), NULL, 0, false);

Static analysis has no way of knowing that the reply will be zero-sized.

Add a check to only do the memcpy if the reply size is not zero and
the destination pointer is not NULL.

Signed-off-by: Pierre-Louis Bossart <[email protected]>
make KCFLAGS='-fanalyzer' sound/soc/sof/intel/ reports a possible NULL
pointer dereference.

sound/soc/sof/topology.c:1136:21: error: dereference of NULL ‘w’
[CWE-476] [-Werror=analyzer-null-dereference]

 1136 |     strcmp(w->sname, rtd->dai_link->stream_name))

The code is rather confusing and can be simplified to make static
analysis happy. No functionality change.

Signed-off-by: Pierre-Louis Bossart <[email protected]>
imx_dsp_get_data() can return NULL, but the value is not checked
before being usd, leading to static analysis warnings.

sound/soc/sof/imx/imx8.c:85:32: error: dereference of NULL ‘0’
[CWE-476] [-Werror=analyzer-null-dereference]

   85 |         spin_lock_irqsave(&priv->sdev->ipc_lock, flags);
      |                            ~~~~^~~~~~

It appears this is not really a possible problem, so remove those checks.

Signed-off-by: Pierre-Louis Bossart <[email protected]>
mtk_adsp_ipc_get_data() can return NULL, but the value is not checked
before being used, leading to static analysis warnings.

sound/soc/sof/mediatek/mt8195/mt8195.c:90:32: error: dereference of
NULL ‘0’ [CWE-476] [-Werror=analyzer-null-dereference]

   90 |         spin_lock_irqsave(&priv->sdev->ipc_lock, flags);
      |                            ~~~~^~~~~~

It appears this is not really a possible problem, so remove those checks.

Signed-off-by: Pierre-Louis Bossart <[email protected]>
snd_soc_card_get_codec_dai() can return NULL, but that value is not
checked for, leading to false-positive static analysis warnings

sound/soc/intel/boards/bdw_rt286.c:190:16: error: dereference of NULL
‘codec_dai’ [CWE-476] [-Werror=analyzer-null-dereference]

  190 |         return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ‘card_suspend_pre’: event 1
    |
    |  190 |         return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
    |      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                |
    |      |                (1) dereference of NULL ‘codec_dai’

This NULL dereference cannot happen, the codec-dai "rt286-aif1" must exists
otherwise the card would not be created. Static analysis cannot know
that however so we might as well squelch this report.

Signed-off-by: Pierre-Louis Bossart <[email protected]>
make KCFLAGS='-fanalyzer' sound/soc/intel/atom/ reports a possible
NULL pointer dereference.

sound/soc/intel/atom/sst/sst_stream.c:221:40: error: dereference of
NULL ‘block’ [CWE-476] [-Werror=analyzer-null-dereference]
  221 |                         unsigned char *r = block->data;

This is a false-positive, the GCC analyzer generated that report by
considering if (bytes->block) as true in some cases and false in
others.

We can simplify the code and use a local variable so that static
analysis does not try to look for cases where bytes->block can be
modified concurrently.

Signed-off-by: Pierre-Louis Bossart <[email protected]>
snd_soc_card_get_codec_dai() can return NULL, but that value is not
checked for, leading to static analysis warnings.

Signed-off-by: Pierre-Louis Bossart <[email protected]>
Not sure why were stayed with 20.04 for so long...

Signed-off-by: Pierre-Louis Bossart <[email protected]>
make KCFLAGS='-fanalyzer' comes for free so we might as well use it,
eh?

Signed-off-by: Pierre-Louis Bossart <[email protected]>
@plbossart plbossart force-pushed the fix/gcc-fanalyzer branch from 45bb03b to 1347f6f Compare June 29, 2023 09:35
@plbossart
Copy link
Member Author

@dbaluta @paulstelian97 can I ask you to look at the IMX part?

@yaochunhung @kuanhsuncheng can I ask you to look at the Mediatek part (same concept as IMX)?

@plbossart
Copy link
Member Author

plbossart commented Jul 11, 2023

Unless I see a sustained objection, I am going to merge this series by the end of the week.

I don't see any reason why we should not use a tool because of lack of comments/approvals. This PR has been around for 2 months, time to move forward.

@ranj063 @ujfalusi @bardliao @dbaluta @paulstelian97 @yaochunhung @kuanhsuncheng @bhiregoudar FYI.

Copy link

@vijendarmukunda vijendarmukunda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dbaluta
Copy link
Collaborator

dbaluta commented Jul 11, 2023

Looks good to me.

Copy link

@yaochunhung yaochunhung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

@marc-hb
Copy link
Collaborator

marc-hb commented Jul 11, 2023

In thesofproject/sof#7562 we found that there is apparently no way to manage false positives.

@paulstelian97 paulstelian97 removed their request for review July 11, 2023 16:02
@plbossart plbossart merged commit 64c70e6 into thesofproject:topic/sof-dev Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants