| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2017 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <gtest/gtest.h> |
| 20 |
|
| 21 |
#include <grpc/impl/codegen/byte_buffer.h> |
| 22 |
#include <grpc/slice.h> |
| 23 |
#include <grpcpp/impl/codegen/grpc_library.h> |
| 24 |
#include <grpcpp/impl/codegen/proto_utils.h> |
| 25 |
#include <grpcpp/impl/grpc_library.h> |
| 26 |
|
| 27 |
#include "test/core/util/test_config.h" |
| 28 |
|
| 29 |
namespace grpc { |
| 30 |
|
| 31 |
namespace internal { |
| 32 |
|
| 33 |
// Provide access to ProtoBufferWriter internals. |
| 34 |
class ProtoBufferWriterPeer { |
| 35 |
public: |
| 36 |
explicit ProtoBufferWriterPeer(ProtoBufferWriter* writer) : writer_(writer) {} |
| 37 |
bool have_backup() const { return writer_->have_backup_; } |
| 38 |
const grpc_slice& backup_slice() const { return writer_->backup_slice_; } |
| 39 |
const grpc_slice& slice() const { return writer_->slice_; } |
| 40 |
|
| 41 |
private: |
| 42 |
ProtoBufferWriter* writer_; |
| 43 |
}; |
| 44 |
|
| 45 |
// Provide access to ByteBuffer internals. |
| 46 |
class GrpcByteBufferPeer { |
| 47 |
public: |
| 48 |
explicit GrpcByteBufferPeer(ByteBuffer* bb) : bb_(bb) {} |
| 49 |
grpc_byte_buffer* c_buffer() { return bb_->c_buffer(); } |
| 50 |
|
| 51 |
private: |
| 52 |
ByteBuffer* bb_; |
| 53 |
}; |
| 54 |
|
| 55 |
class ProtoUtilsTest : public ::testing::Test { |
| 56 |
protected: |
| 57 |
static void SetUpTestCase() { |
| 58 |
// Ensure the ProtoBufferWriter internals are initialized. |
| 59 |
grpc::internal::GrpcLibraryInitializer init; |
| 60 |
init.summon(); |
| 61 |
grpc::GrpcLibraryCodegen lib; |
| 62 |
grpc_init(); |
| 63 |
} |
| 64 |
|
| 65 |
static void TearDownTestCase() { grpc_shutdown(); } |
| 66 |
}; |
| 67 |
|
| 68 |
// Regression test for a memory corruption bug where a series of |
| 69 |
// ProtoBufferWriter Next()/Backup() invocations could result in a dangling |
| 70 |
// pointer returned by Next() due to the interaction between grpc_slice inlining |
| 71 |
// and GRPC_SLICE_START_PTR. |
| 72 |
TEST_F(ProtoUtilsTest, TinyBackupThenNext) { |
| 73 |
ByteBuffer bp; |
| 74 |
const int block_size = 1024; |
| 75 |
ProtoBufferWriter writer(&bp, block_size, 8192); |
| 76 |
ProtoBufferWriterPeer peer(&writer); |
| 77 |
|
| 78 |
void* data; |
| 79 |
int size; |
| 80 |
// Allocate a slice. |
| 81 |
ASSERT_TRUE(writer.Next(&data, &size)); |
| 82 |
EXPECT_EQ(block_size, size); |
| 83 |
// Return a single byte. |
| 84 |
writer.BackUp(1); |
| 85 |
EXPECT_FALSE(peer.have_backup()); |
| 86 |
// On the next allocation, the returned slice is non-inlined. |
| 87 |
ASSERT_TRUE(writer.Next(&data, &size)); |
| 88 |
EXPECT_TRUE(peer.slice().refcount != nullptr); |
| 89 |
EXPECT_EQ(block_size, size); |
| 90 |
} |
| 91 |
|
| 92 |
namespace { |
| 93 |
|
| 94 |
// Set backup_size to 0 to indicate no backup is needed. |
| 95 |
void BufferWriterTest(int block_size, int total_size, int backup_size) { |
| 96 |
ByteBuffer bb; |
| 97 |
ProtoBufferWriter writer(&bb, block_size, total_size); |
| 98 |
|
| 99 |
int written_size = 0; |
| 100 |
void* data; |
| 101 |
int size = 0; |
| 102 |
bool backed_up_entire_slice = false; |
| 103 |
|
| 104 |
while (written_size < total_size) { |
| 105 |
EXPECT_TRUE(writer.Next(&data, &size)); |
| 106 |
EXPECT_GT(size, 0); |
| 107 |
EXPECT_TRUE(data); |
| 108 |
int write_size = size; |
| 109 |
bool should_backup = false; |
| 110 |
if (backup_size > 0 && size > backup_size) { |
| 111 |
write_size = size - backup_size; |
| 112 |
should_backup = true; |
| 113 |
} else if (size == backup_size && !backed_up_entire_slice) { |
| 114 |
// only backup entire slice once. |
| 115 |
backed_up_entire_slice = true; |
| 116 |
should_backup = true; |
| 117 |
write_size = 0; |
| 118 |
} |
| 119 |
// May need a last backup. |
| 120 |
if (write_size + written_size > total_size) { |
| 121 |
write_size = total_size - written_size; |
| 122 |
should_backup = true; |
| 123 |
backup_size = size - write_size; |
| 124 |
ASSERT_GT(backup_size, 0); |
| 125 |
} |
| 126 |
for (int i = 0; i < write_size; i++) { |
| 127 |
(static_cast<uint8_t*>(data))[i] = written_size % 128; |
| 128 |
written_size++; |
| 129 |
} |
| 130 |
if (should_backup) { |
| 131 |
writer.BackUp(backup_size); |
| 132 |
} |
| 133 |
} |
| 134 |
EXPECT_EQ(bb.Length(), (size_t)total_size); |
| 135 |
|
| 136 |
grpc_byte_buffer_reader reader; |
| 137 |
GrpcByteBufferPeer peer(&bb); |
| 138 |
grpc_byte_buffer_reader_init(&reader, peer.c_buffer()); |
| 139 |
int read_bytes = 0; |
| 140 |
while (read_bytes < total_size) { |
| 141 |
grpc_slice s; |
| 142 |
EXPECT_TRUE(grpc_byte_buffer_reader_next(&reader, &s)); |
| 143 |
for (size_t i = 0; i < GRPC_SLICE_LENGTH(s); i++) { |
| 144 |
EXPECT_EQ(GRPC_SLICE_START_PTR(s)[i], read_bytes % 128); |
| 145 |
read_bytes++; |
| 146 |
} |
| 147 |
grpc_slice_unref(s); |
| 148 |
} |
| 149 |
EXPECT_EQ(read_bytes, total_size); |
| 150 |
grpc_byte_buffer_reader_destroy(&reader); |
| 151 |
} |
| 152 |
|
| 153 |
class WriterTest : public ::testing::Test { |
| 154 |
protected: |
| 155 |
static void SetUpTestCase() { |
| 156 |
grpc::internal::GrpcLibraryInitializer init; |
| 157 |
init.summon(); |
| 158 |
grpc::GrpcLibraryCodegen lib; |
| 159 |
// Ensure the ProtoBufferWriter internals are initialized. |
| 160 |
grpc_init(); |
| 161 |
} |
| 162 |
|
| 163 |
static void TearDownTestCase() { grpc_shutdown(); } |
| 164 |
}; |
| 165 |
|
| 166 |
TEST_F(WriterTest, TinyBlockTinyBackup) { |
| 167 |
for (int i = 2; i < static_cast<int> GRPC_SLICE_INLINED_SIZE; i++) { |
| 168 |
BufferWriterTest(i, 256, 1); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
TEST_F(WriterTest, SmallBlockTinyBackup) { BufferWriterTest(64, 256, 1); } |
| 173 |
|
| 174 |
TEST_F(WriterTest, SmallBlockNoBackup) { BufferWriterTest(64, 256, 0); } |
| 175 |
|
| 176 |
TEST_F(WriterTest, SmallBlockFullBackup) { BufferWriterTest(64, 256, 64); } |
| 177 |
|
| 178 |
TEST_F(WriterTest, LargeBlockTinyBackup) { BufferWriterTest(4096, 8192, 1); } |
| 179 |
|
| 180 |
TEST_F(WriterTest, LargeBlockNoBackup) { BufferWriterTest(4096, 8192, 0); } |
| 181 |
|
| 182 |
TEST_F(WriterTest, LargeBlockFullBackup) { BufferWriterTest(4096, 8192, 4096); } |
| 183 |
|
| 184 |
TEST_F(WriterTest, LargeBlockLargeBackup) { |
| 185 |
BufferWriterTest(4096, 8192, 4095); |
| 186 |
} |
| 187 |
|
| 188 |
} // namespace |
| 189 |
} // namespace internal |
| 190 |
} // namespace grpc |
| 191 |
|
| 192 |
int main(int argc, char** argv) { |
| 193 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 194 |
::testing::InitGoogleTest(&argc, argv); |
| 195 |
return RUN_ALL_TESTS(); |
| 196 |
} |