From d308b0127b45f1ec9498793a4f2b580057e297cf Mon Sep 17 00:00:00 2001 From: Jason Macnak Date: Fri, 16 Jul 2021 13:57:41 -0700 Subject: Make display unique id stable across x86 and x86_64 builds ... by using a minimal version of CityHash64 for the hash function. std::hash differs between x86 and x86_64 and also: "Hash functions are only required to produce the same result for the same input within a single execution of a program;" This is problematic for input device configuration files which reference the display unique id. Bug: b/186150820 Test: libsurfaceflinger_unittest Change-Id: If15b66775fa48f14cc56bbd23536b61844b7ae37 Merged-In: If15b66775fa48f14cc56bbd23536b61844b7ae37 --- services/surfaceflinger/Android.bp | 1 + .../DisplayHardware/DisplayIdentification.cpp | 6 +- services/surfaceflinger/DisplayHardware/Hash.cpp | 93 ++++++++++++++++++++++ services/surfaceflinger/DisplayHardware/Hash.h | 27 +++++++ .../tests/unittests/DisplayIdentificationTest.cpp | 9 ++- 5 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 services/surfaceflinger/DisplayHardware/Hash.cpp create mode 100644 services/surfaceflinger/DisplayHardware/Hash.h diff --git a/services/surfaceflinger/Android.bp b/services/surfaceflinger/Android.bp index e669e4532b..eb31e92645 100644 --- a/services/surfaceflinger/Android.bp +++ b/services/surfaceflinger/Android.bp @@ -146,6 +146,7 @@ filegroup { "DisplayHardware/ComposerHal.cpp", "DisplayHardware/DisplayIdentification.cpp", "DisplayHardware/FramebufferSurface.cpp", + "DisplayHardware/Hash.cpp", "DisplayHardware/HWC2.cpp", "DisplayHardware/HWComposer.cpp", "DisplayHardware/PowerAdvisor.cpp", diff --git a/services/surfaceflinger/DisplayHardware/DisplayIdentification.cpp b/services/surfaceflinger/DisplayHardware/DisplayIdentification.cpp index 98209bb9e4..83c2b2e789 100644 --- a/services/surfaceflinger/DisplayHardware/DisplayIdentification.cpp +++ b/services/surfaceflinger/DisplayHardware/DisplayIdentification.cpp @@ -25,6 +25,7 @@ #include #include "DisplayIdentification.h" +#include "Hash.h" namespace android { namespace { @@ -262,8 +263,9 @@ std::optional parseEdid(const DisplayIdentificationData& edid) { } // Hash model string instead of using product code or (integer) serial number, since the latter - // have been observed to change on some displays with multiple inputs. - const auto modelHash = static_cast(std::hash()(modelString)); + // have been observed to change on some displays with multiple inputs. Use a stable hash instead + // of std::hash which is only required to be same within a single execution of a program. + const uint32_t modelHash = static_cast(cityHash64Len0To16(modelString)); // Parse extension blocks. std::optional cea861Block; diff --git a/services/surfaceflinger/DisplayHardware/Hash.cpp b/services/surfaceflinger/DisplayHardware/Hash.cpp new file mode 100644 index 0000000000..6056c8d6f8 --- /dev/null +++ b/services/surfaceflinger/DisplayHardware/Hash.cpp @@ -0,0 +1,93 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#undef LOG_TAG +#define LOG_TAG "DisplayIdentification" + +#include +#include + +#include + +#include "Hash.h" + +namespace android { +namespace { + +template +inline T load(const void* p) { + static_assert(std::is_integral::value, "T must be integral"); + + T r; + std::memcpy(&r, p, sizeof(r)); + return r; +} + +uint64_t rotateByAtLeast1(uint64_t val, uint8_t shift) { + return (val >> shift) | (val << (64 - shift)); +} + +uint64_t shiftMix(uint64_t val) { + return val ^ (val >> 47); +} + +uint64_t hash64Len16(uint64_t u, uint64_t v) { + constexpr uint64_t kMul = 0x9ddfea08eb382d69; + uint64_t a = (u ^ v) * kMul; + a ^= (a >> 47); + uint64_t b = (v ^ a) * kMul; + b ^= (b >> 47); + b *= kMul; + return b; +} + +uint64_t hash64Len0To16(const char* s, uint64_t len) { + constexpr uint64_t k2 = 0x9ae16a3b2f90404f; + constexpr uint64_t k3 = 0xc949d7c7509e6557; + + if (len > 8) { + const uint64_t a = load(s); + const uint64_t b = load(s + len - 8); + return hash64Len16(a, rotateByAtLeast1(b + len, static_cast(len))) ^ b; + } + if (len >= 4) { + const uint32_t a = load(s); + const uint32_t b = load(s + len - 4); + return hash64Len16(len + (a << 3), b); + } + if (len > 0) { + const unsigned char a = static_cast(s[0]); + const unsigned char b = static_cast(s[len >> 1]); + const unsigned char c = static_cast(s[len - 1]); + const uint32_t y = static_cast(a) + (static_cast(b) << 8); + const uint32_t z = static_cast(len) + (static_cast(c) << 2); + return shiftMix(y * k2 ^ z * k3) * k2; + } + return k2; +} + +} // namespace + +uint64_t cityHash64Len0To16(std::string_view sv) { + auto len = sv.length(); + if (len > 16) { + ALOGE("%s called with length %zu. Only hashing the first 16 chars", __FUNCTION__, len); + len = 16; + } + return hash64Len0To16(sv.data(), len); +} + +} // namespace android \ No newline at end of file diff --git a/services/surfaceflinger/DisplayHardware/Hash.h b/services/surfaceflinger/DisplayHardware/Hash.h new file mode 100644 index 0000000000..a7b6c717f6 --- /dev/null +++ b/services/surfaceflinger/DisplayHardware/Hash.h @@ -0,0 +1,27 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +namespace android { + +// CityHash64 implementation that only hashes at most the first 16 characters of the given string. +uint64_t cityHash64Len0To16(std::string_view sv); + +} // namespace android \ No newline at end of file diff --git a/services/surfaceflinger/tests/unittests/DisplayIdentificationTest.cpp b/services/surfaceflinger/tests/unittests/DisplayIdentificationTest.cpp index dc04b6d91d..cd4a5c961d 100644 --- a/services/surfaceflinger/tests/unittests/DisplayIdentificationTest.cpp +++ b/services/surfaceflinger/tests/unittests/DisplayIdentificationTest.cpp @@ -25,6 +25,7 @@ #include #include "DisplayHardware/DisplayIdentification.h" +#include "DisplayHardware/Hash.h" using ::testing::ElementsAre; @@ -134,7 +135,7 @@ DisplayIdentificationData asDisplayIdentificationData(const unsigned char (&byte } uint32_t hash(const char* str) { - return static_cast(std::hash()(str)); + return static_cast(cityHash64Len0To16(str)); } } // namespace @@ -309,9 +310,9 @@ TEST(DisplayIdentificationTest, parseDisplayIdentificationData) { ASSERT_TRUE(tertiaryInfo); // Display IDs should be unique. - EXPECT_NE(primaryInfo->id, secondaryInfo->id); - EXPECT_NE(primaryInfo->id, tertiaryInfo->id); - EXPECT_NE(secondaryInfo->id, tertiaryInfo->id); + EXPECT_EQ(4633257497453176576, primaryInfo->id.value); + EXPECT_EQ(4621520285560261121, secondaryInfo->id.value); + EXPECT_EQ(4633127902230889474, tertiaryInfo->id.value); } TEST(DisplayIdentificationTest, deviceProductInfo) { -- cgit v1.2.3-59-g8ed1b