dart-sdk/runtime/vm/hash.h
Vyacheslav Egorov a2ab7ccea4 [vm] Avoid UB in FinalizeHash(hash)
FinalizeHash(hash) was trying to avoid UB in expression 1 << 32
by casting 1 to uintptr_t. This type however is not wide enough
on 32-bit platforms.

Instead just use explicit comparison hashbits < kBitsPerInt32 to
avoid overflow in left shift.

This bug went unnoticed for a while because it the only place
where we call FinalizeHash(hash) is in the snapshot profile
writer code and it only triggers when gen_snapshot is a
32-bit binary - which is only true on Windows, as Mac and Linux
seem to use simarm_x64 configuration instead.

This UB was explicitly affecting the code behavior because C++
compiler would either optimize out or change behavior of any
code that consumed value produced by FinalizeHash(hash).

Fixes https://github.com/flutter/flutter/issues/97764

TEST=vm/cc/DirectChainedHashMap

Change-Id: I39f2b09e7516c875b765e5a065d1c1331f89fa33
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250741
Commit-Queue: Slava Egorov <vegorov@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
2022-07-06 12:48:16 +00:00

42 lines
1 KiB
C++

// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_HASH_H_
#define RUNTIME_VM_HASH_H_
#include "platform/globals.h"
namespace dart {
inline uint32_t CombineHashes(uint32_t hash, uint32_t other_hash) {
hash += other_hash;
hash += hash << 10;
hash ^= hash >> 6; // Logical shift, unsigned hash.
return hash;
}
inline uint32_t FinalizeHash(uint32_t hash, intptr_t hashbits = kBitsPerInt32) {
hash += hash << 3;
hash ^= hash >> 11; // Logical shift, unsigned hash.
hash += hash << 15;
if (hashbits < kBitsPerInt32) {
hash &= (static_cast<uint32_t>(1) << hashbits) - 1;
}
return (hash == 0) ? 1 : hash;
}
inline uint32_t HashBytes(const uint8_t* bytes, intptr_t size) {
uint32_t hash = size;
while (size > 0) {
hash = CombineHashes(hash, *bytes);
bytes++;
size--;
}
return hash;
}
} // namespace dart
#endif // RUNTIME_VM_HASH_H_