Skip to content

Commit

Permalink
Handle shift by 32 explicitly.
Browse files Browse the repository at this point in the history
Looks like it worked before due to undefined behavior always returning
0. But not anymore.

Test: 836-32768classes
Bug: 220821265
Change-Id: Iaaefc0d3237cf3b6bf50798097b98a8004089d7c
  • Loading branch information
Nicolas Geoffray committed Feb 23, 2022
1 parent 36de6a2 commit b875631
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libdexfile/dex/type_lookup_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ uint32_t TypeLookupTable::Lookup(const char* str, uint32_t hash) const {
return dex::kDexNoIndex;
}
// Look for the partial hash match first, even if traversing the wrong bucket's chain.
uint32_t compared_hash_bits = (hash << mask_bits_) >> (2 * mask_bits_);
uint32_t compared_hash_bits = static_cast<uint64_t>(hash << mask_bits_) >> (2 * mask_bits_);
while (compared_hash_bits != entry->GetHashBits(mask_bits_)) {
if (entry->IsLast(mask_bits_)) {
return dex::kDexNoIndex;
Expand Down
2 changes: 1 addition & 1 deletion libdexfile/dex/type_lookup_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class TypeLookupTable {

uint32_t GetHashBits(uint32_t mask_bits) const {
DCHECK_LE(mask_bits, 16u);
return data_ >> (2u * mask_bits);
return static_cast<uint64_t>(data_) >> (2u * mask_bits);
}

static uint32_t GetMask(uint32_t mask_bits) {
Expand Down
Binary file added test/836-32768classes/classes.dex
Binary file not shown.
Empty file.
1 change: 1 addition & 0 deletions test/836-32768classes/expected-stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
2 changes: 2 additions & 0 deletions test/836-32768classes/info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Regression test for a dex file with 32768 classes. We used to rely on undefined
shift behavior in the type lookup table code.

0 comments on commit b875631

Please sign in to comment.