-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fix: correct frame-pointer wraparound when processing batch
basic_log::output_worker calculates frame positions by taking the "front" address (current read pointer) from mpsc_ring_buffer and then advancing that until it reaches the end of the batch. However, if the batch size is large enough to go past the end of the ring buffer then no wrap around is applied to the frame pointer. This isn't strictly a buffer overrun because, due to the nature of the ring buffer implementation, the same physical memory is mapped in a second time at the end and we are in fact meant to go past the end in situations where objects overlap the end. However, we can run into trouble when an object was constructed in the first virtual memory-map and then accessed through the second virtual-memory map. For plain old data it works fine, but if the object has code that depends on the object's this-pointer staying consistent (a reasonable expectation) then formatting or destroying the object can lead to undefined behavior. This surfaced as github issue #36 in which the libstdc++ implementation of std::string had optimizations that would make use of the address of a class member to determine which deallocation strategy must be used. The fix is fairly simple: instead of using the frame pointer as loop invariant we use the ring buffer's 64-bit position to determine when the end is reached, and whenever we need to use the frame pointer we make sure that it is consistently wrapped to always start in the first virtual-memory block.
- Loading branch information
1 parent
5ac98bd
commit 7050d07
Showing
3 changed files
with
84 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* This file is part of reckless logging | ||
* Copyright 2015-2020 Mattias Flodin <[email protected]> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
// There was a bug in the pointer arithmetic in basic_log that would sometimes | ||
// cause a different virtual address (but the same physical address) than was | ||
// used during construction to be used for destroying an object submitted to | ||
// the log. This test case reproduces that bug. | ||
|
||
#include <reckless/policy_log.hpp> | ||
#include <reckless/file_writer.hpp> | ||
|
||
#include <cstdio> | ||
#include <cstdlib> | ||
|
||
struct holder { | ||
void* pmy_address; | ||
holder() | ||
{ | ||
pmy_address = this; | ||
} | ||
holder(const holder&) | ||
{ | ||
pmy_address = this; | ||
} | ||
~holder() | ||
{ | ||
if (this != pmy_address) { | ||
std::fprintf(stderr, "inconsistent object address; expected %p " | ||
"but got %p\n", pmy_address, this); | ||
std::fflush(stderr); | ||
std::abort(); | ||
} | ||
} | ||
}; | ||
|
||
char const* format(reckless::output_buffer* p, char const* fmt, holder const&) | ||
{ | ||
return fmt+1; | ||
} | ||
|
||
int main() | ||
{ | ||
reckless::file_writer writer("log.txt"); | ||
reckless::policy_log<> log(&writer); | ||
for(int i=0; i!=500000; i++) { | ||
log.write("%s", holder()); | ||
} | ||
} |