Skip to content

Conversation

@asuhan
Copy link
Contributor

@asuhan asuhan commented Aug 4, 2017

Going over 2 GB is quite possible when using Thrift for serialization. We can detect the condition and throw an exception.

while (len > avail) {
new_size = new_size > 0 ? new_size * 2 : 1;
if (new_size > std::numeric_limits<uint32_t>::max()) {
throw TTransportException("Internal buffer size exceeded 2GB");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to see a TTransportException with an error type BAD_ARGS here, however it looks like error types are not being used elsewhere in this module.

@jeking3
Copy link
Contributor

jeking3 commented Aug 10, 2017

Please rebase (and squash to a single commit, if you can) and push here, so the build system can build the change and we can verify it before merging it. Thanks.

@asuhan
Copy link
Contributor Author

asuhan commented Aug 10, 2017

Thanks for the review, I'll address both. It's my preference to squash the commits as well, sounds like I've misinterpreted https://thrift.apache.org/docs/HowToContribute (it says "When bugfixing: add test that will isolate bug before applying change that fixes it").

@asuhan
Copy link
Contributor Author

asuhan commented Aug 11, 2017

I've disabled the test for Windows since allocating 1GB on 32-bit is likely to fail with bad_alloc.

Test failure looks unrelated, also got a clean run before: https://travis-ci.org/apache/thrift/jobs/263298365.

Copy link
Contributor

@jeking3 jeking3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring build job 19 failure, it's the ssl.d intermittent issue.

Copy link
Contributor

@jeking3 jeking3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed one thing in my previous review.

uint64_t new_size = bufferSize_;
while (len > avail) {
new_size = new_size > 0 ? new_size * 2 : 1;
if (new_size > std::numeric_limits<uint32_t>::max()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be int32_t instead of uint32_t, if we want to test against 2GB? Sorry I didn't catch this before.

Copy link
Contributor Author

@asuhan asuhan Aug 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We check it post-resize and it fails if we go past 4GB, therefore pre-resize it must be at most 2GB. Checking against int32_t would be overly conservative; we mainly care about avoiding the arithmetic overflow. Maybe the error message could be improved? "Internal buffer size was already past 2GB when we attempted to resize" would be a more precise description, but I didn't want to make it overly verbose / obscure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, if we only go through powers of 2 when resizing (which looks to be the case), the buffer can only grow to 2GB since the maximum value of uint32_t is one short of 4GB.

}

#ifndef _WIN32
// We can't allocate 1 GB of memory in 32-bit environments.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me wonder if the TBufferTransport should have a size limit that is configurable, with a default of INT32_MAX, and then the test can make a smaller one like 4KB, and write 4KB and then one byte more, instead of using up 2GB of memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd have to write in a loop to trigger buffer resizing if I understand correctly. It'd save the vector allocation in the test, but TMemoryBuffer would still grow. I agree a 50% reduction would be better than nothing, for sure.

@asuhan
Copy link
Contributor Author

asuhan commented Aug 13, 2017

Are there any additional changes I should make to this pull request? As I've said in the inline comment, I believe comparing to std::numeric_limits<uint32_t>::max() is actually consistent with what the error message says.

@jeking3
Copy link
Contributor

jeking3 commented Aug 13, 2017

I'd prefer if we refactored it to make the limit configurable with a default matching what it is now; that way tests won't need multiple gigabytes of memory to function properly. I'll approve and merge this for now as it is an improvement.

@asfgit asfgit closed this in 20116c6 Aug 13, 2017
jeking3 pushed a commit to jeking3/thrift that referenced this pull request Nov 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants