Просмотр исходного кода

Fix unaligned load/store UB in bus and zstd compression
commit_hash:55e574599005f5286f646ebba93d5550325708bc

nadya73 1 месяц назад
Родитель
Сommit
8647d4a864

+ 0 - 31
library/cpp/yt/misc/unaligned-inl.h

@@ -1,31 +0,0 @@
-#ifndef UNALIGNED_INL_H_
-#error "Direct inclusion of this file is not allowed, include unaligned.h"
-// For the sake of sane code completion.
-#include "unaligned.h"
-#endif
-
-#include <cstring>
-
-namespace NYT {
-
-////////////////////////////////////////////////////////////////////////////////
-
-template <class T>
-    requires std::is_trivial_v<T>
-T UnalignedLoad(const T* ptr)
-{
-    T value;
-    std::memcpy(&value, ptr, sizeof(T));
-    return value;
-}
-
-template <class T>
-    requires std::is_trivial_v<T>
-void UnalignedStore(T* ptr, const T& value)
-{
-    std::memcpy(ptr, &value, sizeof(T));
-}
-
-////////////////////////////////////////////////////////////////////////////////
-
-} // namespace NYT

+ 0 - 23
library/cpp/yt/misc/unaligned.h

@@ -1,23 +0,0 @@
-#pragma once
-
-#include <type_traits>
-
-namespace NYT {
-
-////////////////////////////////////////////////////////////////////////////////
-
-template <class T>
-    requires std::is_trivial_v<T>
-T UnalignedLoad(const T* ptr);
-
-template <class T>
-    requires std::is_trivial_v<T>
-void UnalignedStore(T* ptr, const T& value);
-
-////////////////////////////////////////////////////////////////////////////////
-
-} // namespace NYT
-
-#define UNALIGNED_INL_H_
-#include "unaligned-inl.h"
-#undef UNALIGNED_INL_H_

+ 4 - 6
yt/yt/core/bus/tcp/packet.cpp

@@ -6,8 +6,6 @@
 
 #include <library/cpp/yt/string/guid.h>
 
-#include <library/cpp/yt/misc/unaligned.h>
-
 namespace NYT::NBus {
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -152,23 +150,23 @@ protected:
 
     ui32 GetPartSize(int index) const
     {
-        return UnalignedLoad(PartSizes_ + index);
+        return ReadUnaligned<ui32>(PartSizes_ + index);
     }
 
     void SetPartSize(int index, ui32 size)
     {
-        UnalignedStore(PartSizes_ + index, size);
+        WriteUnaligned<ui32>(PartSizes_ + index, size);
     }
 
 
     ui64 GetPartChecksum(int index) const
     {
-        return UnalignedLoad(PartChecksums_ + index);
+        return ReadUnaligned<ui64>(PartChecksums_ + index);
     }
 
     void SetPartChecksum(int index, ui64 checksum)
     {
-        UnalignedStore(PartChecksums_ + index, checksum);
+        WriteUnaligned<ui64>(PartChecksums_ + index, checksum);
     }
 
 private:

+ 1 - 3
yt/yt/core/logging/zstd_compression.cpp

@@ -4,8 +4,6 @@
 
 #include <yt/yt/core/misc/finally.h>
 
-#include <library/cpp/yt/misc/unaligned.h>
-
 #include <contrib/libs/zstd/include/zstd.h>
 
 namespace NYT::NLogging {
@@ -49,7 +47,7 @@ static std::optional<i64> FindSyncTag(const char* buf, size_t size, i64 offset)
             continue;
         }
 
-        ui64 tagOffset = UnalignedLoad(reinterpret_cast<const ui64*>(tag + sizeof(ZstdSyncTag)));
+        ui64 tagOffset = ReadUnaligned<ui64>(tag + sizeof(ZstdSyncTag));
         ui64 tagOffsetExpected = offset + (tag - buf);
 
         if (tagOffset == tagOffsetExpected) {