Ticket #2996: 0009-FIX-SIGBUS-Don-t-read-unaligned-on-ARM.patch

File 0009-FIX-SIGBUS-Don-t-read-unaligned-on-ARM.patch, 8.0 KB (added by BogDan, 9 years ago)
  • source/lib/alignment.h

    From 70d0d2a966eefb682a676e38f5dc6e606017f380 Mon Sep 17 00:00:00 2001
    From: BogDan Vatra <bogdan@kde.org>
    Date: Sun, 28 Dec 2014 11:34:54 +0200
    Subject: [PATCH 09/11] FIX SIGBUS: Don't read unaligned on ARM.
    
    Add readUnAligned function, update XeroXMB
    ---
     source/lib/alignment.h    | 12 +++++++++
     source/ps/XML/XeroXMB.cpp | 62 +++++++++++++++++++++++------------------------
     2 files changed, 43 insertions(+), 31 deletions(-)
    
    diff --git a/source/lib/alignment.h b/source/lib/alignment.h
    index a294a0f..799e800 100644
    a b inline size_t Align(size_t n)  
    1818}
    1919
    2020
     21template<typename T>
     22inline T readUnaligned(const void *ptr)
     23{
     24#if ARCH_ARM
     25    T ret;
     26    memcpy(&ret, ptr, sizeof(T));
     27    return ret;
     28#else
     29    return *(T*)(ptr);
     30#endif
     31}
     32
    2133// bridge the differences between MSC and GCC alignment definitions.
    2234// example: ALIGNED(int, 8) myAlignedVariable = 0;
    2335#if MSC_VERSION
  • source/ps/XML/XeroXMB.cpp

    diff --git a/source/ps/XML/XeroXMB.cpp b/source/ps/XML/XeroXMB.cpp
    index 35b4712..0460cf4 100644
    a b bool XMBFile::Initialise(const char* FileData)  
    4141        return false;
    4242    ENSURE(!strcmp(Header, HeaderMagicStr) && "Invalid XMB header!");
    4343
    44     u32 Version = *(u32*)m_Pointer;
     44    u32 Version = readUnaligned<u32>(m_Pointer);
    4545    m_Pointer += 4;
    4646    if (Version != XMBVersion)
    4747        return false;
    bool XMBFile::Initialise(const char* FileData)  
    5555
    5656#ifdef XERO_USEMAP
    5757    // Build a std::map of all the names->ids
    58     u32 ElementNameCount = *(u32*)m_Pointer; m_Pointer += 4;
     58    u32 ElementNameCount = readUnAligned<u32>(m_Pointer); m_Pointer += 4;
    5959    for (i = 0; i < ElementNameCount; ++i)
    6060        m_ElementNames[ReadZStr8()] = i;
    6161
    62     u32 AttributeNameCount = *(u32*)m_Pointer; m_Pointer += 4;
     62    u32 AttributeNameCount = readUnAligned<u32>(m_Pointer); m_Pointer += 4;
    6363    for (i = 0; i < AttributeNameCount; ++i)
    6464        m_AttributeNames[ReadZStr8()] = i;
    6565#else
    6666    // Ignore all the names for now, and skip over them
    6767    // (remembering the position of the first)
    68     m_ElementNameCount = *(int*)m_Pointer; m_Pointer += 4;
     68    m_ElementNameCount = readUnaligned<int>(m_Pointer); m_Pointer += 4;
    6969    m_ElementPointer = m_Pointer;
    7070    for (i = 0; i < m_ElementNameCount; ++i)
    71         m_Pointer += 4 + *(int*)m_Pointer; // skip over the string
     71        m_Pointer += 4 + readUnaligned<int>(m_Pointer); // skip over the string
    7272
    73     m_AttributeNameCount = *(int*)m_Pointer; m_Pointer += 4;
     73    m_AttributeNameCount = readUnaligned<int>(m_Pointer); m_Pointer += 4;
    7474    m_AttributePointer = m_Pointer;
    7575    for (i = 0; i < m_AttributeNameCount; ++i)
    76         m_Pointer += 4 + *(int*)m_Pointer; // skip over the string
     76        m_Pointer += 4 + readUnaligned<int>(m_Pointer); // skip over the string
    7777#endif
    7878
    7979    return true;    // success
    bool XMBFile::Initialise(const char* FileData)  
    8181
    8282std::string XMBFile::ReadZStr8()
    8383{
    84     int Length = *(int*)m_Pointer;
     84    int Length = readUnaligned<int>(m_Pointer);
    8585    m_Pointer += 4;
    8686    std::string String (m_Pointer); // reads up until the first NULL
    8787    m_Pointer += Length;
    int XMBFile::GetElementID(const char* Name) const  
    119119    {
    120120        // See if this could be the right string, checking its
    121121        // length and then its contents
    122         if (*(int*)Pos == len && strncasecmp(Pos+4, Name, len) == 0)
     122        if (readUnaligned<int>(Pos) == len && strncasecmp(Pos+4, Name, len) == 0)
    123123            return i;
    124124        // If not, jump to the next string
    125         Pos += 4 + *(int*)Pos;
     125        Pos += 4 + readUnaligned<int>(Pos);
    126126    }
    127127    // Failed
    128128    return -1;
    int XMBFile::GetAttributeID(const char* Name) const  
    139139    {
    140140        // See if this could be the right string, checking its
    141141        // length and then its contents
    142         if (*(int*)Pos == len && strncasecmp(Pos+4, Name, len) == 0)
     142        if (readUnaligned<int>(Pos) == len && strncasecmp(Pos+4, Name, len) == 0)
    143143            return i;
    144144        // If not, jump to the next string
    145         Pos += 4 + *(int*)Pos;
     145        Pos += 4 + readUnaligned<int>(Pos);
    146146    }
    147147    // Failed
    148148    return -1;
    std::string XMBFile::GetElementString(const int ID) const  
    156156{
    157157    const char* Pos = m_ElementPointer;
    158158    for (int i = 0; i < ID; ++i)
    159         Pos += 4 + *(int*)Pos;
     159        Pos += 4 + readUnaligned<int>(Pos);
    160160    return std::string(Pos+4);
    161161}
    162162
    std::string XMBFile::GetAttributeString(const int ID) const  
    164164{
    165165    const char* Pos = m_AttributePointer;
    166166    for (int i = 0; i < ID; ++i)
    167         Pos += 4 + *(int*)Pos;
     167        Pos += 4 + readUnaligned<int>(Pos);
    168168    return std::string(Pos+4);
    169169}
    170170
    int XMBElement::GetNodeName() const  
    175175    if (m_Pointer == NULL)
    176176        return -1;
    177177
    178     return *(int*)(m_Pointer + 4); // == ElementName
     178    return readUnaligned<int>(m_Pointer + 4); // == ElementName
    179179}
    180180
    181181XMBElementList XMBElement::GetChildNodes() const
    XMBElementList XMBElement::GetChildNodes() const  
    184184        return XMBElementList(NULL, 0);
    185185
    186186    return XMBElementList(
    187         m_Pointer + 20 + *(int*)(m_Pointer + 16), // == Children[]
    188         *(int*)(m_Pointer + 12) // == ChildCount
     187        m_Pointer + 20 + readUnaligned<int>(m_Pointer + 16), // == Children[]
     188        readUnaligned<int>(m_Pointer + 12) // == ChildCount
    189189    );
    190190}
    191191
    XMBAttributeList XMBElement::GetAttributes() const  
    195195        return XMBAttributeList(NULL, 0);
    196196
    197197    return XMBAttributeList(
    198         m_Pointer + 24 + *(int*)(m_Pointer + 20), // == Attributes[]
    199         *(int*)(m_Pointer + 8) // == AttributeCount
     198        m_Pointer + 24 + readUnaligned<int>(m_Pointer + 20), // == Attributes[]
     199        readUnaligned<int>(m_Pointer + 8) // == AttributeCount
    200200    );
    201201}
    202202
    203203CStr8 XMBElement::GetText() const
    204204{
    205205    // Return empty string if there's no text
    206     if (m_Pointer == NULL || *(int*)(m_Pointer + 20) == 0)
     206    if (m_Pointer == NULL || readUnaligned<int>(m_Pointer + 20) == 0)
    207207        return CStr8();
    208208
    209209    return CStr8(m_Pointer + 28);
    CStr8 XMBElement::GetText() const  
    212212int XMBElement::GetLineNumber() const
    213213{
    214214    // Make sure there actually was some text to record the line of
    215     if (m_Pointer == NULL || *(int*)(m_Pointer + 20) == 0)
     215    if (m_Pointer == NULL || readUnaligned<int>(m_Pointer + 20) == 0)
    216216        return -1;
    217217    else
    218         return *(int*)(m_Pointer + 24);
     218        return readUnaligned<int>(m_Pointer + 24);
    219219}
    220220
    221221XMBElement XMBElementList::GetFirstNamedItem(const int ElementName) const
    XMBElement XMBElementList::GetFirstNamedItem(const int ElementName) const  
    226226    // fast enough with half a dozen attributes:
    227227    for (int i = 0; i < Count; ++i)
    228228    {
    229         int Length = *(int*)Pos;
    230         int Name = *(int*)(Pos+4);
     229        int Length = readUnaligned<int>(Pos);
     230        int Name = readUnaligned<int>(Pos+4);
    231231        if (Name == ElementName)
    232232            return XMBElement(Pos);
    233233        Pos += Length;
    XMBElement XMBElementList::Item(const int id)  
    247247    if (id == m_LastItemID+1)
    248248    {
    249249        Pos = m_LastPointer;
    250         Pos += *(int*)Pos; // skip over the last node
     250        Pos += readUnaligned<int>(Pos); // skip over the last node
    251251    }
    252252    else
    253253    {
    254254        Pos = m_Pointer;
    255255        // Skip over each preceding node
    256256        for (int i=0; i<id; ++i)
    257             Pos += *(int*)Pos;
     257            Pos += readUnaligned<int>(Pos);
    258258    }
    259259    // Cache information about this node
    260260    m_LastItemID = id;
    CStr8 XMBAttributeList::GetNamedItem(const int AttributeName) const  
    271271    // fast enough with half a dozen attributes:
    272272    for (int i = 0; i < Count; ++i)
    273273    {
    274         if (*(int*)Pos == AttributeName)
     274        if (readUnaligned<int>(Pos) == AttributeName)
    275275            return CStr8(Pos+8);
    276         Pos += 8 + *(int*)(Pos+4); // Skip over the string
     276        Pos += 8 + readUnaligned<int>(Pos+4); // Skip over the string
    277277    }
    278278
    279279    // Can't find attribute
    XMBAttribute XMBAttributeList::Item(const int id)  
    291291    {
    292292        Pos = m_LastPointer;
    293293        // Skip over the last attribute
    294         Pos += 8 + *(int*)(Pos+4);
     294        Pos += 8 + readUnaligned<int>(Pos+4);
    295295    }
    296296    else
    297297    {
    298298        Pos = m_Pointer;
    299299        // Skip over each preceding attribute
    300300        for (int i=0; i<id; ++i)
    301             Pos += 8 + *(int*)(Pos+4); // skip ID, length, and string data
     301            Pos += 8 + readUnaligned<int>(Pos+4); // skip ID, length, and string data
    302302    }
    303303    // Cache information about this attribute
    304304    m_LastItemID = id;
    305305    m_LastPointer = Pos;
    306306
    307     return XMBAttribute(*(int*)Pos, CStr8(Pos+8));
     307    return XMBAttribute(readUnaligned<int>(Pos), CStr8(Pos+8));
    308308}