Skip to content

util: Add MemBlockBuilder class to help manage blocks of memory for safe memcpy#9306

Merged
jmarantz merged 15 commits intoenvoyproxy:masterfrom
jmarantz:memblock
Dec 15, 2019
Merged

util: Add MemBlockBuilder class to help manage blocks of memory for safe memcpy#9306
jmarantz merged 15 commits intoenvoyproxy:masterfrom
jmarantz:memblock

Conversation

@jmarantz
Copy link
Copy Markdown
Contributor

Description: Split out from #8779 per suggestion from @htuch -- adds a class to help manage memory blocks, to make it easier to reason about the correctness of calls to memcpy.
Risk Level: low -- this PR just adds/tests a new utility class
Testing: just the new test
Docs Changes: n/a
Release Notes: n/a

…mcpy.

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Copy link
Copy Markdown
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

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

LGTM modulo comments, thanks, this is a great improvement IMHO.
/wait

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
htuch
htuch previously approved these changes Dec 12, 2019
Copy link
Copy Markdown
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

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

Awesome, this is a really nice contribution, @envoyproxy/maintainers please encourage the use of this pattern going forward where applicable.

@htuch
Copy link
Copy Markdown
Member

htuch commented Dec 12, 2019

@jmarantz looks like legit CI failures on coverage related to this PR, want to take a look?

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
@jmarantz
Copy link
Copy Markdown
Contributor Author

woo-hoo: this squirrels by the coverage issues matching the expected error message; ptal.

htuch
htuch previously approved these changes Dec 13, 2019
@jmarantz
Copy link
Copy Markdown
Contributor Author

@envoyproxy/maintainers can I have a non-googler review? This is should be relatively simple, as this adds a new class & tests but does not use it yet.

Copy link
Copy Markdown
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

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

Nice. A few comments for discussion.

/wait-any

* @param data The block of objects to insert.
* @param size The number of elements in the block.
*/
void appendData(const T* data, uint64_t size) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about using absl::span here? Seems more flexible and a bit more type checked for many uses?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm glad you mentioned this because I did not know about absl::Span<>. Pretty cool; I'll keep that one in mind. But I don't see how that data structure helps in this case, as the functionality it automates isn't really what this class does, which is provide a builder-like interface for filling in an owned piece of data.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You are passing const T* data, uint64_t size which can be expressed in a span, just like a string_view. It seemed a more flexible interface that does the same thing? nbd either way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see...ok that definitely improves the interface. Thanks!

I also did -- in a separate commit -- a change to use a span in the rep as well. That reduces the number of lines of code in this impl. Note than in absl::Span::remove_prefix() there is an assert() already, so I removed the SECURITY_ASSERT for now, and key off that in the death-tests.

I feel like the change in AppendOne from *write_ptr_++ = ch to write_span_[0] = ch; write_span_.remove_prefix(1); is a little less intuitive and might not compile to something quite as fast. WDYT? I can always roll back e0d6ee1

Signed-off-by: Joshua Marantz <jmarantz@google.com>
…at check the redundancy.

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
… correct assertion.

Signed-off-by: Joshua Marantz <jmarantz@google.com>
…e disabled for optimized builds.

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
Copy link
Copy Markdown
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

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

Nice, thanks.

*
* @param capacity The number of memory elements to allocate.
*/
void populate(uint64_t capacity) { populateHelper(capacity, std::make_unique<T[]>(capacity)); }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: I would probably call this resize(...) but up to you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's better; will adjust in a follow-up though.

Comment on lines +55 to +56
*write_span_.data() = object;
write_span_.remove_prefix(1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed this is not quite as intuitive but I'm fine either way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i'll see how this feels for now. Thanks.

@jmarantz jmarantz merged commit 7f4c687 into envoyproxy:master Dec 15, 2019
@jmarantz jmarantz deleted the memblock branch December 15, 2019 17:39
prakhag1 pushed a commit to prakhag1/envoy that referenced this pull request Jan 3, 2020
…afe memcpy (envoyproxy#9306)

* Add MemBlockBuilder class to help manage blocks of memory for safe memcpy.

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Prakhar <prakhar_au@yahoo.com>
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.

3 participants