Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Go's varint 64 bit integer decoding in binary decoder #1589

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/stirling/utils/binary_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
namespace px {
namespace stirling {

constexpr int kMaxVarintLen64 = 10;

/**
* Provides functions to extract bytes from a bytes buffer.
*/
Expand Down Expand Up @@ -60,6 +62,35 @@ class BinaryDecoder {
return val;
}

// Extract UVarInt encoded value and return result as uint64_t. The details of this encoding's
// specification can be see in the following link:
// https://cs.opensource.google/go/go/+/refs/tags/go1.20.5:src/encoding/binary/varint.go;l=7-25
StatusOr<uint64_t> ExtractUVarInt() {
uint64_t x = 0;
uint bits = 0;
int i = 0;

while (buf_.size() >= 1 && i < kMaxVarintLen64) {
ddelnano marked this conversation as resolved.
Show resolved Hide resolved
uint8_t b = buf_[0];
buf_.remove_prefix(1);

if (b < 0x80) {
if (i == kMaxVarintLen64 - 1 && b > 1) {
return error::ResourceUnavailable("Insufficient number of bytes.");
}
return x | uint64_t(b) << bits;
}

x |= uint64_t(b & 0x7f) << bits;
bits += 7;
i++;
}
if (i == kMaxVarintLen64) {
return error::ResourceUnavailable("Insufficient number of bytes.");
}
return 0;
}

template <typename TCharType = char>
StatusOr<std::basic_string_view<TCharType>> ExtractString(size_t len) {
static_assert(sizeof(TCharType) == 1);
Expand Down
44 changes: 44 additions & 0 deletions src/stirling/utils/binary_decoder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "src/stirling/utils/binary_decoder.h"

#include <string>
#include <utility>
#include <vector>

#include <gmock/gmock.h>
#include <gtest/gtest.h>
Expand Down Expand Up @@ -51,6 +53,48 @@ TEST(BinaryDecoderTest, ExtractInt) {
EXPECT_EQ(0, bin_decoder.BufSize());
}

TEST(BinaryDecoderTest, ExtractUVarInt) {
std::vector<std::pair<std::vector<uint8_t>, uint64_t>> uVarInts = {
{{}, 0},
{{0x01}, 1},
{{0x02}, 2},
{{0x7f}, 127},
{{0x80, 0x01}, 128},
{{0xff, 0x01}, 255},
{{0x80, 0x02}, 256},
{{0x80, 0x80, 0x02}, 32768},
{{0x80, 0x80, 0x80, 0x02}, 4194304},
{{0x80, 0x80, 0x80, 0x80, 0x02}, 536870912},
{{0x80, 0x80, 0x80, 0x80, 0x80, 0x02}, 68719476736},
{{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02}, 8796093022208},
{{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02}, 1125899906842624},
{{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02}, 144115188075855872},
{{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00}, 0},
{{0xd7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}, 18446744073709551575UL},
};
for (auto& p : uVarInts) {
auto data = p.first;
std::string_view s(reinterpret_cast<char*>(data.data()), data.size());
BinaryDecoder bin_decoder(s);
ASSERT_OK_AND_EQ(bin_decoder.ExtractUVarInt(), p.second);
EXPECT_EQ(0, bin_decoder.BufSize());
}
}

TEST(BinaryDecoderTest, ExtractUVarIntOverflow) {
std::vector<std::vector<uint8_t>> uVarInts = {
{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02},
{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02},
{0xd7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01},
{0xd7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
};
for (auto& data : uVarInts) {
std::string_view s(reinterpret_cast<char*>(data.data()), data.size());
BinaryDecoder bin_decoder(s);
EXPECT_NOT_OK(bin_decoder.ExtractUVarInt());
}
}

TEST(BinaryDecoderTest, ExtractString) {
std::string_view data("abc123");
BinaryDecoder bin_decoder(data);
Expand Down