Skip to content

Commit

Permalink
Remove compiler/ast/endianness.h
Browse files Browse the repository at this point in the history
Summary:
Remove `compiler/ast/endianness.h` because it has nothing to do with AST and only used in two places. Replace its uses with conditionally compiled calls to `__builtin_bswap64`. Another option is to move `endianness.h` outside of AST but it's probably an overkill considering that there are only two uses, three lines of code each.

While at it fix UBs due to invalid type conversion.

Reviewed By: Mizuchi

Differential Revision: D30883166

fbshipit-source-id: 024b0c09454057b732a36baa024e1de77fc8535c
  • Loading branch information
vitaut authored and facebook-github-bot committed Sep 15, 2021
1 parent b2345d5 commit 226d9d8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 76 deletions.
53 changes: 0 additions & 53 deletions thrift/compiler/ast/endianness.h

This file was deleted.

21 changes: 8 additions & 13 deletions thrift/compiler/ast/t_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include <openssl/sha.h>

#include <thrift/compiler/ast/endianness.h>
#include <thrift/compiler/ast/t_program.h>
#include <thrift/compiler/ast/t_typedef.h>

Expand Down Expand Up @@ -60,19 +59,15 @@ const std::string& t_type::type_name(type t) {
}

uint64_t t_type::get_type_id() const {
// This union allows the conversion of the SHA char buffer to a 64bit uint
union {
uint64_t val;
unsigned char buf[SHA_DIGEST_LENGTH];
} u;

std::string name = get_full_name();
SHA1(reinterpret_cast<const unsigned char*>(name.data()), name.size(), u.buf);
const auto hash =
apache::thrift::compiler::bswap_host_to_little_endian(u.val);
type tv = get_type_value();

return (hash & ~t_type::kTypeMask) | int(tv);
unsigned char buf[SHA_DIGEST_LENGTH] = {};
SHA1(reinterpret_cast<const unsigned char*>(name.data()), name.size(), buf);
uint64_t hash = 0;
std::memcpy(&hash, &buf, sizeof(hash));
#if !defined(_WIN32) && __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
hash = __builtin_bswap64(hash);
#endif
return (hash & ~t_type::kTypeMask) | static_cast<int>(get_type_value());
}

std::string t_type::get_scoped_name() const {
Expand Down
18 changes: 8 additions & 10 deletions thrift/compiler/generate/t_concat_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

#include <openssl/sha.h>

#include <thrift/compiler/ast/endianness.h>

#include <thrift/compiler/generate/t_generator.h>

using namespace std;
Expand Down Expand Up @@ -152,17 +150,17 @@ std::string t_concat_generator::generate_structural_id(
std::string hashable_keys_list = ss.str();

// Hash the string and generate a portable hash number.
union {
uint64_t val;
unsigned char buf[SHA_DIGEST_LENGTH];
} u;
unsigned char buf[SHA_DIGEST_LENGTH] = {};
SHA1(
reinterpret_cast<const unsigned char*>(hashable_keys_list.data()),
hashable_keys_list.size(),
u.buf);
const uint64_t hash =
(apache::thrift::compiler::bswap_host_to_little_endian(u.val) &
0x7FFFFFFFFFFFFFFFull); // 63 bits
buf);
uint64_t hash = 0;
std::memcpy(&hash, &buf, sizeof(hash));
#if !defined(_WIN32) && __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
hash = __builtin_bswap64(hash);
#endif
hash &= 0x7FFFFFFFFFFFFFFFull; // 63 bits

// Generate a readable number.
char structural_id[21];
Expand Down

0 comments on commit 226d9d8

Please sign in to comment.