Skip to content

Commit 645c1ce

Browse files
authored
Merge pull request #9021 from naveenkaje/fix_part_size_check
tools: check that part size is not exceeding region size
2 parents 7e18cc5 + 43da2f2 commit 645c1ce

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

tools/build_api.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def _fill_header(region_list, current_region):
411411
return header
412412

413413

414-
def merge_region_list(region_list, destination, notify, padding=b'\xFF'):
414+
def merge_region_list(region_list, destination, notify, config, padding=b'\xFF'):
415415
"""Merge the region_list into a single image
416416
417417
Positional Arguments:
@@ -435,6 +435,13 @@ def merge_region_list(region_list, destination, notify, padding=b'\xFF'):
435435
notify.info(" Filling region %s with %s" % (region.name, region.filename))
436436
part = intelhex_offset(region.filename, offset=region.start)
437437
part.start_addr = None
438+
# Normally, we assume that part.maxddr() can be beyond
439+
# end of rom. However, if the size is restricted with config, do check.
440+
if config.target.restrict_size is not None:
441+
part_size = (part.maxaddr() - part.minaddr()) + 1
442+
if part_size > region.size:
443+
raise ToolException("Contents of region %s does not fit"
444+
% region.name)
438445
merged.merge(part)
439446

440447
# Hex file can have gaps, so no padding needed. While other formats may
@@ -555,13 +562,13 @@ def build_project(src_paths, build_path, target, toolchain_name,
555562
for r in region_list]
556563
res = "%s.%s" % (join(build_path, name),
557564
getattr(toolchain.target, "OUTPUT_EXT", "bin"))
558-
merge_region_list(region_list, res, notify)
565+
merge_region_list(region_list, res, notify, toolchain.config)
559566
update_regions = [
560567
r for r in region_list if r.name in UPDATE_WHITELIST
561568
]
562569
if update_regions:
563570
update_res = join(build_path, generate_update_filename(name, toolchain.target))
564-
merge_region_list(update_regions, update_res, notify)
571+
merge_region_list(update_regions, update_res, notify, toolchain.config)
565572
res = (res, update_res)
566573
else:
567574
res = (res, None)

tools/test/build_api/build_api_test.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
import unittest
1919
from collections import namedtuple
2020
from mock import patch, MagicMock
21-
from tools.build_api import prepare_toolchain, build_project, build_library
21+
from tools.build_api import prepare_toolchain, build_project, build_library, merge_region_list
2222
from tools.resources import Resources
2323
from tools.toolchains import TOOLCHAINS
2424
from tools.notifier.mock import MockNotifier
25-
25+
from tools.config import Region
26+
from tools.utils import ToolException
27+
from intelhex import IntelHex
28+
import intelhex
2629
"""
2730
Tests for build_api.py
2831
"""
@@ -240,5 +243,36 @@ def test_build_library_no_app_config(self, mock_prepare_toolchain, mock_exists,
240243
self.assertEqual(args[1]['app_config'], None,
241244
"prepare_toolchain was called with an incorrect app_config")
242245

246+
@patch('tools.build_api.intelhex_offset')
247+
@patch('tools.config')
248+
def test_merge_region_no_fit(self, mock_config, mock_intelhex_offset):
249+
"""
250+
Test that merge region fails as expected when part size overflows region size.
251+
"""
252+
max_addr = 87444
253+
# create a dummy hex file with above max_addr
254+
mock_intelhex_offset.return_value = IntelHex({0:2, max_addr:0})
255+
256+
# create application and post-application regions and merge.
257+
region_application = Region("application", 10000, 86000, True, "random.hex")
258+
region_post_application = Region("postapplication", 100000, 90000, False, None)
259+
260+
notify = MockNotifier()
261+
region_list = [region_application, region_post_application]
262+
# path to store the result in, should not get used as we expect exception.
263+
res = "./"
264+
mock_config.target.restrict_size = 90000
265+
toolexception = False
266+
267+
try:
268+
merge_region_list(region_list, res, notify, mock_config)
269+
except ToolException:
270+
toolexception = True
271+
except Exception as e:
272+
print("%s %s" % (e.message, e.args))
273+
274+
self.assertTrue(toolexception, "Expected ToolException not raised")
275+
276+
243277
if __name__ == '__main__':
244278
unittest.main()

0 commit comments

Comments
 (0)