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 <grpc/byte_buffer.h>
20 #include <grpc/slice.h>
21 #include <grpcpp/impl/grpc_library.h>
22 #include <grpcpp/impl/proto_utils.h>
23 #include <gtest/gtest.h>
24
25 #include "test/core/test_util/test_config.h"
26
27 namespace grpc {
28
29 namespace internal {
30
31 // Provide access to ProtoBufferWriter internals.
32 class ProtoBufferWriterPeer {
33 public:
ProtoBufferWriterPeer(ProtoBufferWriter * writer)34 explicit ProtoBufferWriterPeer(ProtoBufferWriter* writer) : writer_(writer) {}
have_backup() const35 bool have_backup() const { return writer_->have_backup_; }
backup_slice() const36 const grpc_slice& backup_slice() const { return writer_->backup_slice_; }
slice() const37 const grpc_slice& slice() const { return writer_->slice_; }
38
39 private:
40 ProtoBufferWriter* writer_;
41 };
42
43 // Provide access to ByteBuffer internals.
44 class GrpcByteBufferPeer {
45 public:
GrpcByteBufferPeer(ByteBuffer * bb)46 explicit GrpcByteBufferPeer(ByteBuffer* bb) : bb_(bb) {}
c_buffer()47 grpc_byte_buffer* c_buffer() { return bb_->c_buffer(); }
48
49 private:
50 ByteBuffer* bb_;
51 };
52
53 class ProtoUtilsTest : public ::testing::Test {
54 protected:
SetUpTestSuite()55 static void SetUpTestSuite() {
56 // Ensure the ProtoBufferWriter internals are initialized.
57 grpc::internal::GrpcLibrary lib;
58 grpc_init();
59 }
60
TearDownTestSuite()61 static void TearDownTestSuite() { grpc_shutdown(); }
62 };
63
64 // Regression test for a memory corruption bug where a series of
65 // ProtoBufferWriter Next()/Backup() invocations could result in a dangling
66 // pointer returned by Next() due to the interaction between grpc_slice inlining
67 // and GRPC_SLICE_START_PTR.
TEST_F(ProtoUtilsTest,TinyBackupThenNext)68 TEST_F(ProtoUtilsTest, TinyBackupThenNext) {
69 ByteBuffer bp;
70 const int block_size = 1024;
71 ProtoBufferWriter writer(&bp, block_size, 8192);
72 ProtoBufferWriterPeer peer(&writer);
73
74 void* data;
75 int size;
76 // Allocate a slice.
77 ASSERT_TRUE(writer.Next(&data, &size));
78 EXPECT_EQ(block_size, size);
79 // Return a single byte.
80 writer.BackUp(1);
81 EXPECT_FALSE(peer.have_backup());
82 // On the next allocation, the returned slice is non-inlined.
83 ASSERT_TRUE(writer.Next(&data, &size));
84 EXPECT_TRUE(peer.slice().refcount != nullptr);
85 EXPECT_EQ(block_size, size);
86 }
87
88 namespace {
89
90 // Set backup_size to 0 to indicate no backup is needed.
BufferWriterTest(int block_size,int total_size,int backup_size)91 void BufferWriterTest(int block_size, int total_size, int backup_size) {
92 ByteBuffer bb;
93 ProtoBufferWriter writer(&bb, block_size, total_size);
94
95 int written_size = 0;
96 void* data;
97 int size = 0;
98 bool backed_up_entire_slice = false;
99
100 while (written_size < total_size) {
101 EXPECT_TRUE(writer.Next(&data, &size));
102 EXPECT_GT(size, 0);
103 EXPECT_TRUE(data);
104 int write_size = size;
105 bool should_backup = false;
106 if (backup_size > 0 && size > backup_size) {
107 write_size = size - backup_size;
108 should_backup = true;
109 } else if (size == backup_size && !backed_up_entire_slice) {
110 // only backup entire slice once.
111 backed_up_entire_slice = true;
112 should_backup = true;
113 write_size = 0;
114 }
115 // May need a last backup.
116 if (write_size + written_size > total_size) {
117 write_size = total_size - written_size;
118 should_backup = true;
119 backup_size = size - write_size;
120 ASSERT_GT(backup_size, 0);
121 }
122 for (int i = 0; i < write_size; i++) {
123 (static_cast<uint8_t*>(data))[i] = written_size % 128;
124 written_size++;
125 }
126 if (should_backup) {
127 writer.BackUp(backup_size);
128 }
129 }
130 EXPECT_EQ(bb.Length(), (size_t)total_size);
131
132 grpc_byte_buffer_reader reader;
133 GrpcByteBufferPeer peer(&bb);
134 grpc_byte_buffer_reader_init(&reader, peer.c_buffer());
135 int read_bytes = 0;
136 while (read_bytes < total_size) {
137 grpc_slice s;
138 EXPECT_TRUE(grpc_byte_buffer_reader_next(&reader, &s));
139 for (size_t i = 0; i < GRPC_SLICE_LENGTH(s); i++) {
140 EXPECT_EQ(GRPC_SLICE_START_PTR(s)[i], read_bytes % 128);
141 read_bytes++;
142 }
143 grpc_slice_unref(s);
144 }
145 EXPECT_EQ(read_bytes, total_size);
146 grpc_byte_buffer_reader_destroy(&reader);
147 }
148
149 class WriterTest : public ::testing::Test {
150 protected:
SetUpTestSuite()151 static void SetUpTestSuite() {
152 grpc::internal::GrpcLibrary lib;
153 // Ensure the ProtoBufferWriter internals are initialized.
154 grpc_init();
155 }
156
TearDownTestSuite()157 static void TearDownTestSuite() { grpc_shutdown(); }
158 };
159
TEST_F(WriterTest,TinyBlockTinyBackup)160 TEST_F(WriterTest, TinyBlockTinyBackup) {
161 for (int i = 2; i < static_cast<int> GRPC_SLICE_INLINED_SIZE; i++) {
162 BufferWriterTest(i, 256, 1);
163 }
164 }
165
TEST_F(WriterTest,SmallBlockTinyBackup)166 TEST_F(WriterTest, SmallBlockTinyBackup) { BufferWriterTest(64, 256, 1); }
167
TEST_F(WriterTest,SmallBlockNoBackup)168 TEST_F(WriterTest, SmallBlockNoBackup) { BufferWriterTest(64, 256, 0); }
169
TEST_F(WriterTest,SmallBlockFullBackup)170 TEST_F(WriterTest, SmallBlockFullBackup) { BufferWriterTest(64, 256, 64); }
171
TEST_F(WriterTest,LargeBlockTinyBackup)172 TEST_F(WriterTest, LargeBlockTinyBackup) { BufferWriterTest(4096, 8192, 1); }
173
TEST_F(WriterTest,LargeBlockNoBackup)174 TEST_F(WriterTest, LargeBlockNoBackup) { BufferWriterTest(4096, 8192, 0); }
175
TEST_F(WriterTest,LargeBlockFullBackup)176 TEST_F(WriterTest, LargeBlockFullBackup) { BufferWriterTest(4096, 8192, 4096); }
177
TEST_F(WriterTest,LargeBlockLargeBackup)178 TEST_F(WriterTest, LargeBlockLargeBackup) {
179 BufferWriterTest(4096, 8192, 4095);
180 }
181
182 } // namespace
183 } // namespace internal
184 } // namespace grpc
185
main(int argc,char ** argv)186 int main(int argc, char** argv) {
187 grpc::testing::TestEnvironment env(&argc, argv);
188 ::testing::InitGoogleTest(&argc, argv);
189 return RUN_ALL_TESTS();
190 }
191