Commit 2d8e56aa authored by Denis Solonkov's avatar Denis Solonkov Committed by Commit Bot

Zeroes the [l, r) range in virtual memory and update tests.

We can't expect that the test array would be put in the same place in
memory between test runs and since [l, r) is being rounded to the page
size. Due to that we can't make any assumptions about the elements
inside of the final range and can't calculate the resulting sum, since
all elements inside of the range are zeroed.

The tests were modified the following way:
1) All zeroes in the test array were replaced by one to be able to
  locate all zeroed elements in it.
2) GetSum method was replaced by GetZeroes method which is expected to
  return 4096.
3) The size of the array was set to 2*4096 - 1 to prevent unlikely case
  of array aligning perfectly on memory and having 2 pages cut out of
  it.

Bug: 998082
Change-Id: Idde87569decd3a52ecd1a0c034ab638833d8e0b3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1795791
Commit-Queue: Denis Solonkov <solonkovda@google.com>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Reviewed-by: default avatarAlex Ilin <alexilin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697190}
parent 1452070b
...@@ -227,12 +227,15 @@ def _MovePhdrToTheEnd(data): ...@@ -227,12 +227,15 @@ def _MovePhdrToTheEnd(data):
elf_hdr.PatchData(data) elf_hdr.PatchData(data)
def _SplitLoadSegment(data, l, r): def _SplitLoadSegmentAndNullifyRange(data, l, r):
"""Find LOAD segment covering [l, r) and splits it into three segments. """Find LOAD segment covering [l, r) and splits it into three segments.
Split is done so one of the LOAD segments contains only [l, r) and nothing Split is done so one of the LOAD segments contains only [l, r) and nothing
else. If the range is located at the start or at the end of the segment less else. If the range is located at the start or at the end of the segment less
than three segments may be created. than three segments may be created.
The resulting LOAD segment containing [l, r) is edited so it sets the
corresponding virtual address range to zeroes, ignoring file content.
""" """
elf_hdr = elf_headers.ElfHeader(data) elf_hdr = elf_headers.ElfHeader(data)
...@@ -284,7 +287,7 @@ def _SplitLoadSegment(data, l, r): ...@@ -284,7 +287,7 @@ def _SplitLoadSegment(data, l, r):
range_phdr.p_offset = l range_phdr.p_offset = l
range_phdr.p_vaddr = central_segment_address range_phdr.p_vaddr = central_segment_address
range_phdr.p_paddr = central_segment_address range_phdr.p_paddr = central_segment_address
range_phdr.p_filesz = r - l range_phdr.p_filesz = 0
range_phdr.p_memsz = r - l range_phdr.p_memsz = r - l
elf_hdr.PatchData(data) elf_hdr.PatchData(data)
...@@ -314,7 +317,7 @@ def main(): ...@@ -314,7 +317,7 @@ def main():
_CopyRangeIntoCompressedSection(data, left_range, right_range) _CopyRangeIntoCompressedSection(data, left_range, right_range)
_MovePhdrToTheEnd(data) _MovePhdrToTheEnd(data)
_SplitLoadSegment(data, left_range, right_range) _SplitLoadSegmentAndNullifyRange(data, left_range, right_range)
with open(args.output, 'wb') as f: with open(args.output, 'wb') as f:
f.write(data) f.write(data)
......
...@@ -117,7 +117,7 @@ class CompressionScriptTest(unittest.TestCase): ...@@ -117,7 +117,7 @@ class CompressionScriptTest(unittest.TestCase):
patched_library_path = self._RunScript(library_path) patched_library_path = self._RunScript(library_path)
opener_output = self._RunOpener(opener_path, patched_library_path) opener_output = self._RunOpener(opener_path, patched_library_path)
self.assertEqual(opener_output, '1046506\n') self.assertEqual(opener_output, '4096\n')
def testAlignUp(self): def testAlignUp(self):
"""Tests for AlignUp method of the script.""" """Tests for AlignUp method of the script."""
......
...@@ -24,13 +24,13 @@ int main(int argc, char** argv) { ...@@ -24,13 +24,13 @@ int main(int argc, char** argv) {
return 1; return 1;
} }
TestFunction get_sum = TestFunction get_zeroes =
reinterpret_cast<TestFunction>(dlsym(handle, "GetSum")); reinterpret_cast<TestFunction>(dlsym(handle, "GetZeroes"));
if (get_sum == nullptr) { if (get_zeroes == nullptr) {
std::cerr << "GetSum method not found" << std::endl; std::cerr << "GetSum method not found" << std::endl;
return 1; return 1;
} }
std::cout << get_sum() << std::endl; std::cout << get_zeroes() << std::endl;
return 0; return 0;
} }
...@@ -8,20 +8,20 @@ ...@@ -8,20 +8,20 @@
// for the script. We expect library to not crash and return the 55 as a // for the script. We expect library to not crash and return the 55 as a
// result. // result.
#include <numeric> #include <algorithm>
#include <vector> #include <vector>
#include "libtest_array.h" // NOLINT(build/include) #include "libtest_array.h" // NOLINT(build/include)
extern "C" { extern "C" {
int GetSum(); int GetZeroes();
} }
int GetSum() { int GetZeroes() {
// We are using some c++ features here to better simulate a c++ library and // We are using some c++ features here to better simulate a c++ library and
// cause more code reach to catch potential memory errors. // cause more code reach to catch potential memory errors.
std::vector<int> sum_array(std::begin(array), std::end(array)); std::vector<int> sum_array(std::begin(array), std::end(array));
int sum = std::accumulate(sum_array.begin(), sum_array.end(), 0); int count = std::count(sum_array.begin(), sum_array.end(), 0);
// sum should be equal to 1046506. // count should be equal to 4096.
return sum; return count;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment