Skip to content

Conversation

@andyross
Copy link
Contributor

[First submission to oss-fuzz. Not sure if I'm checking all the correct boxes to get this submitted successfully. Be gentle.]

SOF has a new fuzzing framework that leverages the underlying Zephyr OS's native_posix integration to get a more complete build than just the protocol layer. In theory all the non-hardware-specific code is reachable from the fuzzer inputs.

Also adds support for the newer "IPC4" protocol as a separate target.

Copy link
Contributor

Choose a reason for hiding this comment

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

any reason we are tied to a particular release?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just a general intuition that Zephyr SDK updates tend to be high churn and need some manual attention anyway. Also as I understand the setup, docker isn't going to be sensitive to things like remotely-pulled "latest" links and we'd need to manually rebuild the image anyway if the SDK changed underneath it.

Copy link

@marc-hb marc-hb May 16, 2023

Choose a reason for hiding this comment

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

Just a general intuition that Zephyr SDK updates tend to be high churn and need some manual attention anyway.

Confirmed. SDK "flag day" learned the hard way this week: thesofproject/sof#7614

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way would could "inject" the version into this script? Updating things in this repo at any level of regularity is not simple.

Copy link

Choose a reason for hiding this comment

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

https://docs.docker.com/engine/reference/builder/#arg ?

It could also be a more dynamic ENV that defaults to 0.16.1 (why 0.16.0 BTW?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uh... because that line of the script predates the .1 release and I missed it? :)

I'll dig up a latest link (I'm like 80% sure we have one) and use that. It won't hurt anything, and if nothing else it's a good opportunity to figure out which SDK subsets we really need so the image doesn't have to pull all the architecture toolchains we don't care about.

Copy link
Contributor

@DonggeLiu DonggeLiu left a comment

Choose a reason for hiding this comment

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

LGTM, if you could address the questions from others : )

@andyross
Copy link
Contributor Author

Updated the SDK URL (and also replaced it with the vastly smaller host-tools-only version). There's no clean way to download "latest" without traversing a directory, but at least this will fail and require manual update (which is statistically probably anyway) every time the contents of the /latest/ path change.

Copy link
Contributor

Choose a reason for hiding this comment

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

We don't update copyright years.

@andyross
Copy link
Contributor Author

de-update copyright date

Copy link

Choose a reason for hiding this comment

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

Dunno whether this matters here or not but we use a Docker image for that reason; to track ALSA: https://github.com/thesofproject/sof/blob/main/scripts/docker_build/sof_builder/Dockerfile

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that's mostly a drive-by comment. But it seems like there's space for SOF to specify "we are based on this version of alsa-lib/alsa-utils as of your current tree". West would be handy, but then we'd have to rebuild alsa along with the firmware, and that's probably a pain.

Copy link

@marc-hb marc-hb May 18, 2023

Choose a reason for hiding this comment

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

Yeah, that's mostly a drive-by comment. But it seems like there's space for SOF to specify "we are based on this version of alsa-lib/alsa-utils as of your current tree".

So maybe this comment is saying either too much or too little. Because the longer story is that only building topologies requires the latest and greatest ALSA, that's pretty much the only job the SOF "ALSA container" is used for. For building the kernel or the firmware a cutting edge ALSA has never been required in my experience.

So do you really need the latest ALSA here? Maybe not.

Copy link

Choose a reason for hiding this comment

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

resolved

Copy link

@marc-hb marc-hb May 17, 2023

Choose a reason for hiding this comment

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

You will get a 2x speed up with this recent and wonderful git option. I started carpet bombing SOF CI with it and it's been fantastic

Suggested change
RUN cd sof && west update
RUN cd sof && west update --fetch-opt=--filter=tree:0

It's better than --depth because it breaks neither git describe nor git merge-base thesofproject/linux#2556

More info in zephyrproject-rtos/west#638 and others

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm... getting fatal: --filter can only be used when extensions.partialClone is set when I try this? Maybe this is too bleeding edge for the git in the 20.04 image? Or maybe I just need to read some more docs. CI optimization is pretty far from my happy zone.

Copy link

Choose a reason for hiding this comment

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

Maybe this is too bleeding edge for the git in the 20.04 image?

Yes: I can reproduce with git 2.25.1 in Ubuntu 20.04 but not with git 2.34.1 in Ubuntu 22.04.

This happens only when trying to fetch with the --filter optimization on an existing, not already "filtered" clone (and only on 20.04).

This happens because west clones missing modules in two steps: 1. git initialization of a "non-filtered". 2. git fetch --filter=... (boom)

A tested workaround is git config remote.upstream.partialclonefilter tree:0 between 1. and 2. but of course you can't inject any command between west steps 1. and 2.

https://git-scm.com/docs/repository-version

Ubuntu 22.04 is not affected because git fetch with version 2.34.1 automagically adds the git config above.

Do you really need to use Ubuntu 20.04? We recently upgraded everything to 22.04 and we experienced a few bugs but absolutely nothing related to toolchains or building anything.

Copy link

@marc-hb marc-hb May 18, 2023

Choose a reason for hiding this comment

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

You will get a 2x speed up with this recent and wonderful git option.

Finally woke up and realized this is run at image creation time; to sort-of "warm up a git cache". So while this git optimization trick is very good to know it's totally off-topic here, sorry the distraction. Resolving this comment.

EDIT: I can't resolve comments here for some reason. My shame will stay on display.

Copy link
Contributor

Choose a reason for hiding this comment

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

@marc-hb its because we are not part of the repo

Copy link

Choose a reason for hiding this comment

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

Can this be overriden on some --arg command line? If yes maybe say it for the Docker noobs among us.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At the docker level, yes. docker build --build-arg SDK_VER=0.16.2 would have the expected effect. I have no idea if that's wired meaingfully to the oss-fuzz infrastructure or not.

Really this is just here to define a local variable for clarity. Docker has two variable tyes: ARG works like this and can be set externally, but only works within the dockerfile itself. ENV cannot be set externally, but also persists into the default environment variables of processes spawned in the container, which is undesirable here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Bleh if the versions weren't part of the release file name this would be much easier. Seems like a common problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we will have to leave this as it, not much hope for fixing it without some additional logic

Copy link

Choose a reason for hiding this comment

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

Nit: the -J hasn't been needed for ages: auto-detect. Except when using a pipe?

Copy link

Choose a reason for hiding this comment

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

Do you really want to use a pipe? I mean without set -o pipefail? Downloads are the commands the most likely to fail in this script, aren't they?

Consider how I landed here :-)

Copy link

Choose a reason for hiding this comment

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

resolved

Copy link

Choose a reason for hiding this comment

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

This seems unnecessary, bash supports multilines strings?

Suggested change
BASE_CFG=`cat <<EOF
BASE_CFG='
-DCONFIG_ASSERT=y
...

Copy link

@marc-hb marc-hb May 18, 2023

Choose a reason for hiding this comment

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

The recommended and shellcheck-compliant way to do this is using a bash array:

BASE_CFG=(
-DCONFIG_ASSERT=y
-DCONFIG_SYS_HEAP_BIG_ONLY=y
...
)
declare -p BASE_CFG
west build -p -b native_posix ./app -- "${BASE_CFG[@]}"

shellcheck saves lives; use it always. It's also a great... teacher.

You can't go wrong with arrays, they're always safe.

http://mywiki.wooledge.org/BashGuide/Arrays
http://mywiki.wooledge.org/Quotes

Copy link

Choose a reason for hiding this comment

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

resolved

@andyross
Copy link
Contributor Author

Hm... looks like the target list isn't as autodetecty as I thought it was. Need to figure out the yaml scheme to get it to conform to what the runner thinks it should be. Will update the shellisms.

@andyross
Copy link
Contributor Author

One more spin. Update shell fu per @marc-hb. Declare the right set of architectures/sanitizers in yaml.

marc-hb added a commit to marc-hb/sof that referenced this pull request May 18, 2023
-Wl,EL is a linker option, not a compiler option and clang does not
like it at compilation time; it fails like this:
```
cd smex
cmake -B build -DCMAKE_C_COMPILER=clang
make -C build

clang-15: error: -Wl,-EL: 'linker' input unused
          [-Werror,-Wunused-command-line-argument]
```

Reported by @andyross in google/oss-fuzz#10342

oss-fuzz does not need smex at all but this one-line fix is just
faster and simpler than a bigger CMake re-architecture just for
oss-fuzz.

Also simplify this for clang compatibility:
```
error: unknown warning option '-Wimplicit-fallthrough=3'; did you mean
     '-Wimplicit-fallthrough'? [-Werror,-Wunknown-warning-option]
```

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
SOF has a new fuzzing framework that leverages the underlying Zephyr
OS's native_posix integration to get a more complete build than just
the protocol layer.  In theory all the non-hardware-specific code is
reachable from the fuzzer inputs.

Also adds support for the newer "IPC4" protocol as a separate target.

Signed-off-by: Andy Ross <andyross@google.com>
@andyross
Copy link
Contributor Author

Yet one more, because @marc-hb pointed out that ALSA isn't actually required for a SOF firmware build anymore (no topology files for fuzzing). And also because CI seemed to be indefinitely stuck...

kv2019i pushed a commit to thesofproject/sof that referenced this pull request May 22, 2023
-Wl,EL is a linker option, not a compiler option and clang does not
like it at compilation time; it fails like this:
```
cd smex
cmake -B build -DCMAKE_C_COMPILER=clang
make -C build

clang-15: error: -Wl,-EL: 'linker' input unused
          [-Werror,-Wunused-command-line-argument]
```

Reported by @andyross in google/oss-fuzz#10342

oss-fuzz does not need smex at all but this one-line fix is just
faster and simpler than a bigger CMake re-architecture just for
oss-fuzz.

Also simplify this for clang compatibility:
```
error: unknown warning option '-Wimplicit-fallthrough=3'; did you mean
     '-Wimplicit-fallthrough'? [-Werror,-Wunknown-warning-option]
```

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
@jonathanmetzman
Copy link
Contributor

Is this ready to merge?

@andyross
Copy link
Contributor Author

Should be, yes.

@cujomalainey
Copy link
Contributor

Yes please go ahead :)

@DonggeLiu DonggeLiu merged commit b4d067d into google:master May 23, 2023
marc-hb added a commit to marc-hb/sof that referenced this pull request May 23, 2023
Just a stable-v2.2 test.

Sample failure on the main branch:
https://github.com/thesofproject/sof/actions/runs/5053499443/

Probably since google/oss-fuzz#10342 ?

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
Copy link
Contributor

@jonathanmetzman jonathanmetzman left a comment

Choose a reason for hiding this comment

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

The build is broken because our tooling (srcmap) breaks on this: https://oss-fuzz-build-logs.storage.googleapis.com/index.html#sound-open-firmware

Comment on lines +22 to +31
# Environment: Zephyr has its own toolchain selection mechanism, and
# the oss-fuzz environment generates warnings and confuses things when
# building some of the (un-fuzzed) build-time tools that aren't quite
# clang-compatible.
export ZEPHYR_TOOLCHAIN_VARIANT=llvm
unset CC
unset CCC
unset CXX
unset CFLAGS
unset CXXFLAGS
Copy link
Contributor

Choose a reason for hiding this comment

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

Not really a fan of this. LibFuzzer is bundled with clang and using your own clang can cause many other problems. I guess if you do this, we can't help you with build issues.

Copy link
Contributor

Choose a reason for hiding this comment

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

I may even revert this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isn't being used to build the fuzzed binary, which is grabbing clang off of PATH. It's there because there's a build-time tool in SOF (that actually isn't used except on real firmware) that doesn't build with clang due to routine warning collisions. AFAICT @marc-hb fixed this upstream, not sure if that patch merged or not.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, false alarm, this seems fine.

Copy link

Choose a reason for hiding this comment

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

AFAICT @marc-hb fixed this upstream, not sure if that patch merged or not.

Yes it's merged:

WORKDIR $SRC

# SOF itself (also pulls in Zephyr + modules via west)
RUN west init -m https://github.com/thesofproject/sof sof
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you use git instead? This is breaking some of our tooling that finds revisions (it assumes git or svn usage like all other users).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

West is a git wrapper, analogous to repo. It's possible, but error prone, to manually clone all the relevant repositories. The structure isn't complicated, $SRC/sof is the top level directory which contains $SRC/sof/sof, which is the git repo corresponding to the project itself, and $SRC/sof/zephyr, and a few other bits under $SRC/sof/modules/*.

What are the requirements here? Can I symlink you something to the top level that matches what you want to see?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I see it. srcmap assumes that the git archive was pulled from a remote with the name "origin". This is true by default with an unadorned git clone. It's not required, and in fact west allows multiple remotes with names like "thesofproject" or "upstream".

FWIW: a straightforward fix to srcmap, which replaces the assumption that "there is always a remote named origin" with "we only care about the first remote listed" is something like (untested):

--- a/infra/base-images/base-builder/srcmap
+++ b/infra/base-images/base-builder/srcmap
@@ -35,7 +35,7 @@ fi
 for DOT_GIT_DIR in $(find $PATHS_TO_SCAN -name ".git" -type d); do
   GIT_DIR=$(dirname $DOT_GIT_DIR)
   cd $GIT_DIR
-  GIT_URL=$(git config --get remote.origin.url)
+  GIT_URL=$(git config --get remote.$(git remote | head -1).url)
   GIT_REV=$(git rev-parse HEAD)
   jq_inplace $SRCMAP ".\"$GIT_DIR\" = { type: \"git\", url: \"$GIT_URL\", rev: \"$GIT_REV\" }"
 done

It's worth pointing out that these are still just assumptions. In the general case it's not possible to look at a .git directory and ask the question "from where was this pulled?". Maybe it would be better to allow projects to specify the srcmap themselves for edge cases like this?

Is there a way for me to run srcmap locally in a way that corresponds to your build failures to test this? I'm digging around and getting a little lost.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively, I could update the project code to go in and deliberately add a "origin" remote that copies the one already there. That's more code and won't help anyone else, but might be more palatable?

Copy link
Contributor

@jonathanmetzman jonathanmetzman May 24, 2023

Choose a reason for hiding this comment

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

I think the origin trick would work. If you're using the infra/helper.py shell command to build/debug, you can run the srcmap command (inside our docker container) to figure out if this will work or not.

Copy link

Choose a reason for hiding this comment

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

FWIW west list has format options, for instance:

west list -f '{url}'
https://github.com/thesofproject/rimage
https://github.com/thesofproject/tomlc99
https://github.com/zephyrproject-rtos/zephyr
https://github.com/zephyrproject-rtos/hal_xtensa
https://github.com/zephyrproject-rtos/lz4
https://github.com/zephyrproject-rtos/mbedtls
https://github.com/zephyrproject-rtos/mipi-sys-t
...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Global fix up at #10383

Will look at the project-side workaround now. Honestly didn't even know about west list, but that's cleaner than find $SRC -name .git for sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Project-local fix in #10385

Copy link

Choose a reason for hiding this comment

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

Copy link
Contributor

@jonathanmetzman jonathanmetzman left a comment

Choose a reason for hiding this comment

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

The build is broken because our tooling (srcmap) breaks on this: https://oss-fuzz-build-logs.storage.googleapis.com/index.html#sound-open-firmware

@cujomalainey
Copy link
Contributor

@andyross you should have access to the dashboard now, please look into this

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.

5 participants