Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 1 | .. SPDX-License-Identifier: CC-BY-SA-2.0-UK |
| 2 | |
| 3 | ******************* |
| 4 | Reproducible Builds |
| 5 | ******************* |
| 6 | |
| 7 | ================ |
| 8 | How we define it |
| 9 | ================ |
| 10 | |
| 11 | The Yocto Project defines reproducibility as where a given input build |
| 12 | configuration will give the same binary output regardless of when it is built |
| 13 | (now or in 5 years time), regardless of the path on the filesystem the build is |
| 14 | run in, and regardless of the distro and tools on the underlying host system the |
| 15 | build is running on. |
| 16 | |
| 17 | ============== |
| 18 | Why it matters |
| 19 | ============== |
| 20 | |
| 21 | The project aligns with the `Reproducible Builds project |
| 22 | <https://reproducible-builds.org/>`_, which shares information about why |
| 23 | reproducibility matters. The primary focus of the project is the ability to |
| 24 | detect security issues being introduced. However, from a Yocto Project |
| 25 | perspective, it is also hugely important that our builds are deterministic. When |
| 26 | you build a given input set of metadata, we expect you to get consistent output. |
| 27 | This has always been a key focus but, :yocto_docs:`since release 3.1 ("dunfell") |
| 28 | </ref-manual/migration-3.1.html#reproducible-builds-now-enabled-by-default>`, |
| 29 | it is now true down to the binary level including timestamps. |
| 30 | |
| 31 | For example, at some point in the future life of a product, you find that you |
| 32 | need to rebuild to add a security fix. If this happens, only the components that |
| 33 | have been modified should change at the binary level. This would lead to much |
| 34 | easier and clearer bounds on where validation is needed. |
| 35 | |
| 36 | This also gives an additional benefit to the project builds themselves, our hash |
| 37 | equivalence for :ref:`Shared State <overview-manual/concepts:Shared State>` |
| 38 | object reuse works much more effectively when the binary output remains the |
| 39 | same. |
| 40 | |
| 41 | .. note:: |
| 42 | |
| 43 | We strongly advise you to make sure your project builds reproducibly |
| 44 | before finalizing your production images. It would be too late if you |
| 45 | only address this issue when the first updates are required. |
| 46 | |
| 47 | =================== |
| 48 | How we implement it |
| 49 | =================== |
| 50 | |
| 51 | There are many different aspects to build reproducibility, but some particular |
| 52 | things we do within the build system to ensure reproducibility include: |
| 53 | |
| 54 | - Adding mappings to the compiler options to ensure debug filepaths are mapped |
| 55 | to consistent target compatible paths. This is done through the |
| 56 | ``DEBUG_PREFIX_MAP`` variable which sets the ``-fmacro-prefix-map`` and |
| 57 | ``-fdebug-prefix-map`` compiler options correctly to map to target paths. |
| 58 | - Being explicit about recipe dependencies and their configuration (no floating |
| 59 | configure options or host dependencies creeping in). In particular this means |
| 60 | making sure :term:`PACKAGECONFIG` coverage covers configure options which may |
| 61 | otherwise try and auto-detect host dependencies. |
| 62 | - Using recipe specific sysroots to isolate recipes so they only see their |
| 63 | dependencies. These are visible as ``recipe-sysroot`` and |
| 64 | ``recipe-sysroot-native`` directories within the :term:`WORKDIR` of a given |
| 65 | recipe and are populated only with the dependencies a recipe has. |
| 66 | - Build images from a reduced package set: only packages from recipes the image |
| 67 | depends upon. |
| 68 | - Filtering the tools available from the host's ``PATH`` to only a specific set |
| 69 | of tools, set using the :term:`HOSTTOOLS` variable. |
| 70 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 71 | .. note:: |
| 72 | |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 73 | Because of an open bug in GCC, using ``DISTRO_FEATURES:append = " lto"`` or |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 74 | adding ``-flto`` (Link Time Optimization) to ``CFLAGS`` makes the resulting |
| 75 | binary non-reproducible, in that it depends on the full absolute build path |
| 76 | to ``recipe-sysroot-native``, so installing the Yocto Project in a different |
| 77 | directory results in a different binary. |
| 78 | |
| 79 | This issue is addressed by |
| 80 | :yocto_bugs:`bug 14481 - Programs built with -flto are not reproducible</show_bug.cgi?id=14481>`. |
| 81 | |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 82 | ========================================= |
| 83 | Can we prove the project is reproducible? |
| 84 | ========================================= |
| 85 | |
| 86 | Yes, we can prove it and we regularly test this on the Autobuilder. At the |
| 87 | time of writing (release 3.3, "hardknott"), :term:`OpenEmbedded-Core (OE-Core)` |
| 88 | is 100% reproducible for all its recipes (i.e. world builds) apart from the Go |
| 89 | language and Ruby documentation packages. Unfortunately, the current |
| 90 | implementation of the Go language has fundamental reproducibility problems as |
| 91 | it always depends upon the paths it is built in. |
| 92 | |
| 93 | .. note:: |
| 94 | |
| 95 | Only BitBake and :term:`OpenEmbedded-Core (OE-Core)`, which is the ``meta`` |
| 96 | layer in Poky, guarantee complete reproducibility. The moment you add |
| 97 | another layer, this warranty is voided, because of additional configuration |
| 98 | files, ``bbappend`` files, overridden classes, etc. |
| 99 | |
| 100 | To run our automated selftest, as we use in our CI on the Autobuilder, you can |
| 101 | run:: |
| 102 | |
| 103 | oe-selftest -r reproducible.ReproducibleTests.test_reproducible_builds |
| 104 | |
| 105 | This defaults to including a ``world`` build so, if other layers are added, it would |
| 106 | also run the tests for recipes in the additional layers. The first build will be |
| 107 | run using :ref:`Shared State <overview-manual/concepts:Shared State>` if |
| 108 | available, the second build explicitly disables |
| 109 | :ref:`Shared State <overview-manual/concepts:Shared State>` and builds on the |
| 110 | specific host the build is running on. This means we can test reproducibility |
| 111 | builds between different host distributions over time on the Autobuilder. |
| 112 | |
| 113 | If ``OEQA_DEBUGGING_SAVED_OUTPUT`` is set, any differing packages will be saved |
| 114 | here. The test is also able to run the ``diffoscope`` command on the output to |
| 115 | generate HTML files showing the differences between the packages, to aid |
| 116 | debugging. On the Autobuilder, these appear under |
| 117 | https://autobuilder.yocto.io/pub/repro-fail/ in the form ``oe-reproducible + |
| 118 | <date> + <random ID>``, e.g. ``oe-reproducible-20200202-1lm8o1th``. |
| 119 | |
| 120 | The project's current reproducibility status can be seen at |
| 121 | :yocto_home:`/reproducible-build-results/` |
| 122 | |
| 123 | You can also check the reproducibility status on supported host distributions: |
| 124 | |
| 125 | - CentOS: :yocto_ab:`/typhoon/#/builders/reproducible-centos` |
| 126 | - Debian: :yocto_ab:`/typhoon/#/builders/reproducible-debian` |
| 127 | - Fedora: :yocto_ab:`/typhoon/#/builders/reproducible-fedora` |
| 128 | - Ubuntu: :yocto_ab:`/typhoon/#/builders/reproducible-ubuntu` |
| 129 | |
| 130 | =============================== |
| 131 | Can I test my layer or recipes? |
| 132 | =============================== |
| 133 | |
| 134 | Once again, you can run a ``world`` test using the |
| 135 | :ref:`oe-selftest <ref-manual/release-process:Testing and Quality Assurance>` |
| 136 | command provided above. This functionality is implemented |
| 137 | in :oe_git:`meta/lib/oeqa/selftest/cases/reproducible.py |
| 138 | </openembedded-core/tree/meta/lib/oeqa/selftest/cases/reproducible.py>`. |
| 139 | |
| 140 | You could subclass the test and change ``targets`` to a different target. |
| 141 | |
| 142 | You may also change ``sstate_targets`` which would allow you to "pre-cache" some |
| 143 | set of recipes before the test, meaning they are excluded from reproducibility |
| 144 | testing. As a practical example, you could set ``sstate_targets`` to |
| 145 | ``core-image-sato``, then setting ``targets`` to ``core-image-sato-sdk`` would |
| 146 | run reproducibility tests only on the targets belonging only to ``core-image-sato-sdk``. |