• Bug#977031: device-tree-compiler: Please backport patch to fix unaligne

    From John Paul Adrian Glaubitz@21:1/5 to All on Thu Dec 10 10:40:01 2020
    XPost: linux.debian.bugs.dist

    This is a multi-part MIME message sent by reportbug.


    Source: device-tree-compiler
    Severity: normal
    Tags: patch
    User: debian-sparc@lists.debian.org
    Usertags: sparc64
    X-Debbugs-Cc: debian-sparc@lists.debian.org

    Hi!

    device-tree-compiler currently FTBFS on sparc64 due to unaligned access crashes.

    Luckily, that problem has been fixed upstream already:

    commit b28464a550c536296439b5785ed8852d1e15b35b
    Author: David Gibson <david@gibson.dropbear.id.au>
    Date: Tue Apr 14 15:02:51 2020 +1000

    Fix some potential unaligned accesses in dtc

    Because of the convention of packed representations in property layouts,
    it's not uncommon to have integer values in properties which aren't
    naturally aligned. Thus, there are several places in the dtc code where we
    cast a potentially unaligned byte pointer into an integer pointer and load
    it directly. On a number of architectures (including sparc64 and arm) this
    won't work and will cause a fault. In some cases it may be trapped and
    emulated by the kernel, but not always.

    Therefore, replace such direct unaligned reads with a helper which will
    handle unaligned data reads (a variant on the fdtXX_ld() functions already
    used in libfdt).

    Signed-off-by: David Gibson <david@gibson.dropbear.id.au>

    Could you backport this patch which applies cleanly to 1.6.0? I'm attaching it.

    Thanks,
    Adrian

    --
    .''`. John Paul Adrian Glaubitz
    : :' : Debian Developer - glaubitz@debian.org
    `. `' Freie Universitaet Berlin - glaubitz@physik.fu-berlin.de
    `- GPG: 62FF 8A75 84E0 2956 9546 0006 7426 3B37 F5B5 F913

    From b28464a550c536296439b5785ed8852d1e15b35b Mon Sep 17 00:00:00 2001
    From: David Gibson <david@gibson.dropbear.id.au>
    Date: Tue, 14 Apr 2020 15:02:51 +1000
    Subject: [PATCH] Fix some potential unaligned accesses in dtc

    Because of the convention of packed representations in property layouts,
    it's not uncommon to have integer values in properties which aren't
    naturally aligned. Thus, there are several places in the dtc code where we cast a potentially unaligned byte pointer into an integer pointer and load
    it directly. On a number of architectures (including sparc64 and arm) this won't work and will cause a fault. In some cases it may be trapped and emulated by the kernel, but not always.

    Therefore, replace such direct unaligned reads with a helper which will
    handle unaligned data reads (a variant on the fdtXX_ld() functions already
    used in libfdt).

    Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
    ---
    dtc.h | 31 +++++++++++++++++++++++++++++++
    flattree.c | 2 +-
    treesource.c | 6 +++---
    yamltree.c | 6 +++---
    4 files changed, 38 insertions(+), 7 deletions(-)

    diff --git a/dtc.h b/dtc.h
    index 6e74ece..a08f415 100644
    --- a/dtc.h
    +++ b/dtc.h
    @@ -51,6 +51,37 @@ extern int annotate; /* annotate .dts with input source location */

    typedef uint32_t cell_t;

    +static inline uint16_t dtb_ld16(const void *p)
    +{
    + const uint8_t *bp = (const uint8_t *)p;
    +
    + return ((uint16_t)bp[0] << 8)
    + | bp[1];
    +}
    +
    +static inline uint32_t dtb_ld32(const void *p)
    +{
    + const uint8_t *bp = (const uint8_t *)p;
    +
    + return ((uint32_t)bp[0] << 24)
    + | ((uint32_t)bp[1] << 16)
    + | ((uint32_t)bp[2] << 8)
    + | bp[3];
    +}
    +
    +static inline uint64_t dtb_ld64(const void *p)
    +{
    + const uint8_t *bp = (const uint8_t *)p;
    +
    + return ((uint64_t)bp[0] << 56)
    + | ((uint64_t)bp[1] << 48)
    + | ((uint64_t)bp[2] << 40)
    + | ((uint64_t)bp[3] << 32)
    + | ((uint64_t)bp[4] << 24)
    + | ((uint64_