[haiku-commits] Change in haiku[master]: [WIP] bfs: fix misaligned access

  • From: Gerrit <review@xxxxxxxxxxxxxxxxxxx>
  • To: waddlesplash <waddlesplash@xxxxxxxxx>, haiku-commits@xxxxxxxxxxxxx
  • Date: Mon, 16 Mar 2020 08:55:34 +0000

From Adrien Destugues <pulkomandy@xxxxxxxxx>:

Adrien Destugues has uploaded this change for review. ( 
https://review.haiku-os.org/c/haiku/+/2363 ;)


Change subject: [WIP] bfs: fix misaligned access
......................................................................

[WIP] bfs: fix misaligned access

The BFS on-disk data is not aligned. Reading it to memory and trying to
access fields directly does not work on sparc. memcpy the data to an
aligned variable before handling it with its native size.

The Values() method should probably be removed completely, and ValueAt()
used everywhere instead.
---
M src/add-ons/kernel/file_systems/bfs/BPlusTree.cpp
M src/add-ons/kernel/file_systems/bfs/BPlusTree.h
2 files changed, 21 insertions(+), 5 deletions(-)



  git pull ssh://git.haiku-os.org:22/haiku refs/changes/63/2363/1

diff --git a/src/add-ons/kernel/file_systems/bfs/BPlusTree.cpp 
b/src/add-ons/kernel/file_systems/bfs/BPlusTree.cpp
index 47125d6..a5ef4dc 100644
--- a/src/add-ons/kernel/file_systems/bfs/BPlusTree.cpp
+++ b/src/add-ons/kernel/file_systems/bfs/BPlusTree.cpp
@@ -1055,7 +1055,6 @@
                return B_ENTRY_NOT_FOUND;
        }

-       off_t* values = node->Values();
        int16 saveIndex = -1;

        // binary search in the key array
@@ -1082,8 +1081,9 @@
                } else {
                        if (_index)
                                *_index = i;
-                       if (_next)
-                               *_next = BFS_ENDIAN_TO_HOST_INT64(values[i]);
+                       if (_next) {
+                               *_next = 
BFS_ENDIAN_TO_HOST_INT64(node->ValueAt(i));
+                       }
                        return B_OK;
                }
        }
@@ -1094,7 +1094,7 @@
                if (saveIndex == node->NumKeys())
                        *_next = node->OverflowLink();
                else
-                       *_next = BFS_ENDIAN_TO_HOST_INT64(values[saveIndex]);
+                       *_next = 
BFS_ENDIAN_TO_HOST_INT64(node->ValueAt(saveIndex));
        }
        return B_ENTRY_NOT_FOUND;
 }
@@ -2307,7 +2307,7 @@
 #endif
                if (node->OverflowLink() == BPLUSTREE_NULL) {
                        if (status == B_OK && _value != NULL)
-                               *_value = 
BFS_ENDIAN_TO_HOST_INT64(node->Values()[keyIndex]);
+                               *_value = 
BFS_ENDIAN_TO_HOST_INT64(node->ValueAt(keyIndex));

 #ifdef DEBUG
                        if (levels != (int32)fHeader.MaxNumberOfLevels())
diff --git a/src/add-ons/kernel/file_systems/bfs/BPlusTree.h 
b/src/add-ons/kernel/file_systems/bfs/BPlusTree.h
index 24bab65..f57d5fe 100644
--- a/src/add-ons/kernel/file_systems/bfs/BPlusTree.h
+++ b/src/add-ons/kernel/file_systems/bfs/BPlusTree.h
@@ -99,6 +99,7 @@

        inline  uint16*                         KeyLengths() const;
        inline  off_t*                          Values() const;
+       inline  off_t                           ValueAt(int index) const;
        inline  uint8*                          Keys() const;
        inline  int32                           Used() const;
                        uint8*                          KeyAt(int32 index, 
uint16* keyLength) const;
@@ -574,6 +575,21 @@
 }


+inline off_t
+bplustree_node::ValueAt(int index) const
+{
+       off_t* base = Values();
+#if __sparc64__
+       // Use byte access to fix alignment
+       off_t tmp;
+       memcpy(&tmp, base + index, sizeof(tmp));
+       return tmp;
+#else
+       return base[index];
+#endif
+}
+
+
 inline uint8*
 bplustree_node::Keys() const
 {

--
To view, visit https://review.haiku-os.org/c/haiku/+/2363
To unsubscribe, or for help writing mail filters, visit 
https://review.haiku-os.org/settings

Gerrit-Project: haiku
Gerrit-Branch: master
Gerrit-Change-Id: I3bf62590dee059ad32b1845bdc4eace165b73203
Gerrit-Change-Number: 2363
Gerrit-PatchSet: 1
Gerrit-Owner: Adrien Destugues <pulkomandy@xxxxxxxxx>
Gerrit-MessageType: newchange

Other related posts:

  • » [haiku-commits] Change in haiku[master]: [WIP] bfs: fix misaligned access - Gerrit