-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[lldb/linux] Make truncated reads work #106532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
CXX_SOURCES := main.cpp | ||
|
||
include Makefile.rules |
61 changes: 61 additions & 0 deletions
61
lldb/test/API/functionalities/memory/holes/TestMemoryHoles.py
This file contains hidden or 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,61 @@ | ||
""" | ||
Test the memory commands operating on memory regions with holes (inaccessible | ||
pages) | ||
""" | ||
|
||
import lldb | ||
from lldbsuite.test.lldbtest import * | ||
import lldbsuite.test.lldbutil as lldbutil | ||
from lldbsuite.test.decorators import * | ||
|
||
|
||
class MemoryHolesTestCase(TestBase): | ||
NO_DEBUG_INFO_TESTCASE = True | ||
|
||
def setUp(self): | ||
super().setUp() | ||
# Find the line number to break inside main(). | ||
self.line = line_number("main.cpp", "// break here") | ||
|
||
def _prepare_inferior(self): | ||
self.build() | ||
exe = self.getBuildArtifact("a.out") | ||
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) | ||
|
||
# Break in main() after the variables are assigned values. | ||
lldbutil.run_break_set_by_file_and_line( | ||
self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True | ||
) | ||
|
||
self.runCmd("run", RUN_SUCCEEDED) | ||
|
||
# The stop reason of the thread should be breakpoint. | ||
self.expect( | ||
"thread list", | ||
STOPPED_DUE_TO_BREAKPOINT, | ||
substrs=["stopped", "stop reason = breakpoint"], | ||
) | ||
|
||
# The breakpoint should have a hit count of 1. | ||
lldbutil.check_breakpoint(self, bpno=1, expected_hit_count=1) | ||
|
||
# Avoid the expression evaluator, as it can allocate allocate memory | ||
# inside the holes we've deliberately left empty. | ||
self.memory = self.frame().FindVariable("mem_with_holes").GetValueAsUnsigned() | ||
self.pagesize = self.frame().FindVariable("pagesize").GetValueAsUnsigned() | ||
positions = self.frame().FindVariable("positions") | ||
self.positions = [ | ||
positions.GetChildAtIndex(i).GetValueAsUnsigned() | ||
for i in range(positions.GetNumChildren()) | ||
] | ||
self.assertEqual(len(self.positions), 5) | ||
|
||
@expectedFailureWindows | ||
def test_memory_read(self): | ||
labath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._prepare_inferior() | ||
|
||
error = lldb.SBError() | ||
content = self.process().ReadMemory(self.memory, 2 * self.pagesize, error) | ||
self.assertEqual(len(content), self.pagesize) | ||
self.assertEqual(content[0:7], b"needle\0") | ||
self.assertTrue(error.Fail()) |
This file contains hidden or 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,88 @@ | ||
#include <algorithm> | ||
#include <cstdint> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <cstring> | ||
#include <iostream> | ||
|
||
constexpr size_t num_pages = 7; | ||
constexpr size_t accessible_pages[] = {0, 2, 4, 6}; | ||
|
||
bool is_accessible(size_t page) { | ||
return std::find(std::begin(accessible_pages), std::end(accessible_pages), | ||
page) != std::end(accessible_pages); | ||
} | ||
|
||
// allocate_memory_with_holes returns a pointer to `num_pages` pages of memory, | ||
// where some of the pages are inaccessible (even to debugging APIs). We use | ||
// this to test lldb's ability to skip over inaccessible blocks. | ||
#ifdef _WIN32 | ||
#include "Windows.h" | ||
|
||
int getpagesize() { | ||
SYSTEM_INFO system_info; | ||
GetSystemInfo(&system_info); | ||
return system_info.dwPageSize; | ||
} | ||
|
||
char *allocate_memory_with_holes() { | ||
int pagesize = getpagesize(); | ||
void *mem = | ||
VirtualAlloc(nullptr, num_pages * pagesize, MEM_RESERVE, PAGE_NOACCESS); | ||
if (!mem) { | ||
std::cerr << std::system_category().message(GetLastError()) << std::endl; | ||
exit(1); | ||
} | ||
char *bytes = static_cast<char *>(mem); | ||
for (size_t page = 0; page < num_pages; ++page) { | ||
if (!is_accessible(page)) | ||
continue; | ||
if (!VirtualAlloc(bytes + page * pagesize, pagesize, MEM_COMMIT, | ||
PAGE_READWRITE)) { | ||
std::cerr << std::system_category().message(GetLastError()) << std::endl; | ||
exit(1); | ||
} | ||
} | ||
return bytes; | ||
} | ||
#else | ||
#include "sys/mman.h" | ||
#include "unistd.h" | ||
|
||
char *allocate_memory_with_holes() { | ||
int pagesize = getpagesize(); | ||
void *mem = mmap(nullptr, num_pages * pagesize, PROT_READ | PROT_WRITE, | ||
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
if (mem == MAP_FAILED) { | ||
perror("mmap"); | ||
exit(1); | ||
} | ||
char *bytes = static_cast<char *>(mem); | ||
for (size_t page = 0; page < num_pages; ++page) { | ||
if (is_accessible(page)) | ||
continue; | ||
if (munmap(bytes + page * pagesize, pagesize) != 0) { | ||
perror("munmap"); | ||
exit(1); | ||
} | ||
} | ||
return bytes; | ||
} | ||
#endif | ||
|
||
int main(int argc, char const *argv[]) { | ||
char *mem_with_holes = allocate_memory_with_holes(); | ||
int pagesize = getpagesize(); | ||
char *positions[] = { | ||
mem_with_holes, // Beginning of memory | ||
mem_with_holes + 2 * pagesize, // After a hole | ||
mem_with_holes + 2 * pagesize + | ||
pagesize / 2, // Middle of a block, after an existing match. | ||
mem_with_holes + 5 * pagesize - 7, // End of a block | ||
mem_with_holes + 7 * pagesize - 7, // End of memory | ||
}; | ||
for (char *p : positions) | ||
strcpy(p, "needle"); | ||
|
||
return 0; // break here | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.