Skip to content

tools: check that part size is not exceeding region size #9021

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 2 commits into from
Feb 7, 2019

Conversation

naveenkaje
Copy link
Contributor

@naveenkaje naveenkaje commented Dec 7, 2018

Description

Check that the Intel hex file segment fits within the region. Add a test to the tools test suite to verify the same.

Pull request type

[X] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Docs update
[ ] Test update
[ ] Breaking change

Reviewers

@theotherjimmy

@naveenkaje
Copy link
Contributor Author

Addressing #9012 (comment)

@ciarmcom
Copy link
Member

ciarmcom commented Dec 8, 2018

@naveenkaje, thank you for your changes.
@ARMmbed/mbed-os-tools @ARMmbed/mbed-os-maintainers please review.

Copy link
Contributor

@theotherjimmy theotherjimmy left a comment

Choose a reason for hiding this comment

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

part.maxaddr may be past the end of ROM, as introduced by #8097

@theotherjimmy
Copy link
Contributor

OOps, got to give you a path forward. Support that use case please.

@cmonr
Copy link
Contributor

cmonr commented Dec 21, 2018

@naveenkaje Has any progress been made on this PR?

@naveenkaje
Copy link
Contributor Author

Will make changes and upload.

@naveenkaje naveenkaje force-pushed the fix_part_size_check branch from 2ac5a4a to 2e74b8b Compare January 7, 2019 21:39
@0xc0170
Copy link
Contributor

0xc0170 commented Jan 16, 2019

Ready for another review (it was updated)

@theotherjimmy
Copy link
Contributor

@naveenkaje Would it be possible to add some tests for merge_region_list?

@naveenkaje
Copy link
Contributor Author

naveenkaje commented Jan 24, 2019

@theotherjimmy Here are logs from a test in which target.restrict_size is set to overflow and with this change, such a condition is caught.

Test Log:

Configuration in mbed_app.json
"DISCO_L476VG": {
    "target.restrict_size": "0x15690"
}
Build log

Using ROM regions application, post_application in this build.
  Region application: size 0x15000, offset 0x8000000
  Region post_application: size 0xeb000, offset 0x8015000
Compile [  0.1%]: except.S
Compile [  0.2%]: mbed_tz_context.c
Compile [  0.4%]: MCR20Drv.c
Compile [  0.5%]: rf_configuration.c
Compile [  0.6%]: fslittle_test.c
...
Compile [ 99.8%]: stm_spi_api.c
Compile [ 99.9%]: us_ticker.c
Compile [100.0%]: trng_api.c
Link: mbed-os-example-blinky_application
Elf2Bin: mbed-os-example-blinky_application
Merging Regions
  Filling region application with ./BUILD/DISCO_L476VG/GCC_ARM-DEBUG/mbed-os-example-blinky_application.bin
[ERROR] Contents of region application does not fit
[mbed] ERROR: "/usr/bin/python" returned error.
       Code: 1
       Path: "/home/navkaj01/code/mbed-os-example-blinky"
       Command: "/usr/bin/python -u /home/navkaj01/code/mbed-os-example-blinky/mbed-os/tools/make.py -t GCC_ARM -m DISCO_L476VG --profile debug --source . --build ./BUILD/DISCO_L476VG/GCC_ARM-DEBUG -c"
       Tip: You could retry the last command with "-v" flag for verbose output
---

@theotherjimmy
Copy link
Contributor

@naveenkaje I meant some unit tests. That way we can have a quick sanity check and faster feedback in case anyone wants to refactor this code.

@naveenkaje
Copy link
Contributor Author

naveenkaje commented Jan 29, 2019

Test results

$ python -m pytest -v tools/test/build_api/build_api_test.py
========================================================= test session starts ==========================================================
platform linux2 -- Python 2.7.12, pytest-3.3.0, py-1.7.0, pluggy-0.6.0 -- /usr/bin/python                                               
cachedir: .cache                                                                                                                        
<snip>                                                                                    
plugins: hypothesis-3.88.3                                                                                                              
collected 8 items                                                                                                                       
                                                                                                                                        
tools/test/build_api/build_api_test.py::BuildApiTests::test_always_complete_build PASSED                                         [ 12%] 
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_library_app_config PASSED                                      [ 25%] 
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_library_no_app_config PASSED                                   [ 37%] 
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_project_app_config PASSED                                      [ 50%] 
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_project_no_app_config PASSED                                   [ 62%] 
tools/test/build_api/build_api_test.py::BuildApiTests::test_merge_region_no_fit PASSED                                           [ 75%] 
tools/test/build_api/build_api_test.py::BuildApiTests::test_prepare_toolchain_app_config PASSED                                  [ 87%] 
tools/test/build_api/build_api_test.py::BuildApiTests::test_prepare_toolchain_no_app_config PASSED                               [100%] 
                                                                                                                                        
======================================================= 8 passed in 1.25 seconds =======================================================

Copy link
Contributor

@theotherjimmy theotherjimmy left a comment

Choose a reason for hiding this comment

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

The new test cannot fail: it catches the only exception of the function under test and does not assert.

Please add an assert.

@patch('tools.build_api.Resources')
@patch('tools.build_api.mkdir')
@patch('os.path.exists')
@patch('tools.build_api.prepare_toolchain')
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need this patch

"""
Test that merge region fails as expected when part size overflows region size.
"""
with patch("tools.build_api.intelhex_offset") as _intelhex_offset:
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you move this patch into the @patch directorator section above the funnction?

region_list = [region_application, region_post_application]
region_list = list(region_list)
toolchain = prepare_toolchain(self.src_paths, self.build_path, self.target,
self.toolchain_name, notify=notify)
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need a toolchain object at all, so you can delete this line and the line above

mock_prepare_toolchain.config = MagicMock(
has_regions=True, name=None, lib_config_data=None)
region_list = [region_application, region_post_application]
region_list = list(region_list)
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is not needed, as region_list is already a list.

@@ -238,5 +240,41 @@ def test_build_library_no_app_config(self, mock_prepare_toolchain, mock_exists,
self.assertEqual(args[1]['app_config'], None,
"prepare_toolchain was called with an incorrect app_config")

@patch('tools.build_api.Resources')
@patch('tools.build_api.mkdir')
@patch('os.path.exists')
Copy link
Contributor

Choose a reason for hiding this comment

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

os.path.exsits is not used, so you don't need this patch either.

@theotherjimmy
Copy link
Contributor

Come to think of it, you probably don't need to patch tools.build_api.Resources or tools.build_api.mkdir, either.


notify = MockNotifier()
region_list = [region_application, region_post_application]
toolchain = prepare_toolchain(self.src_paths, self.build_path, self.target,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

toolchain needed to pass to merge_region_list()

Copy link
Contributor

Choose a reason for hiding this comment

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

merge_region_list(region_list, res, notify, toolchain.config)

Nope. It needs a config object, or stand in. That's not a toolchain object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @theotherjimmy got it. Updated the patch.

Copy link
Contributor

@theotherjimmy theotherjimmy left a comment

Choose a reason for hiding this comment

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

Improving, but we still don't assert any behavior of merge_region_list. Please add an assert to the return value or in either the try or the except path, enforcing that only one of them is possible.

mock_intelhex_offset.return_value = IntelHex({0:2, max_addr:0})

self.assertEqual(0, mock_intelhex_offset.return_value.minaddr())
self.assertEqual(max_addr, mock_intelhex_offset.return_value.maxaddr())
Copy link
Contributor

Choose a reason for hiding this comment

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

These asserts don't test any tools code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

since we use the IntelHex() library to generate this object, we can forego these asserts?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes.

@naveenkaje
Copy link
Contributor Author

naveenkaje commented Jan 29, 2019

Without fix 060d3d3

$ python -m pytest -v tools/test/build_api/build_api_test.py                                          │
======================================================== test session starts ========================================================│
platform linux2 -- Python 2.7.12, pytest-3.3.0, py-1.7.0, pluggy-0.6.0 -- /usr/bin/python                                            │
cachedir: .cache                                                                                                                     │
<snip>
plugins: hypothesis-3.88.3                                                                                                           │
collected 8 items                                                                                                                    │
                                                                                                                                     │
tools/test/build_api/build_api_test.py::BuildApiTests::test_always_complete_build PASSED                                      [ 12%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_library_app_config PASSED                                   [ 25%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_library_no_app_config PASSED                                [ 37%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_project_app_config PASSED                                   [ 50%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_project_no_app_config PASSED                                [ 62%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_merge_region_no_fit FAILED                                        [ 75%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_prepare_toolchain_app_config PASSED                               [ 87%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_prepare_toolchain_no_app_config PASSED                            [100%] │
                                                                                                                                     │
============================================================= FAILURES ==============================================================│
______________________________________________ BuildApiTests.test_merge_region_no_fit ______________________________________________

With fix 060d3d3

======================================================== test session starts ========================================================│
platform linux2 -- Python 2.7.12, pytest-3.3.0, py-1.7.0, pluggy-0.6.0 -- /usr/bin/python                                            │
cachedir: .cache                                                                                                                     │
<snip>
plugins: hypothesis-3.88.3                                                                                                           │
collected 8 items                                                                                                                    │
                                                                                                                                     │
tools/test/build_api/build_api_test.py::BuildApiTests::test_always_complete_build PASSED                                      [ 12%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_library_app_config PASSED                                   [ 25%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_library_no_app_config PASSED                                [ 37%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_project_app_config PASSED                                   [ 50%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_build_project_no_app_config PASSED                                [ 62%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_merge_region_no_fit PASSED                                        [ 75%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_prepare_toolchain_app_config PASSED                               [ 87%] │
tools/test/build_api/build_api_test.py::BuildApiTests::test_prepare_toolchain_no_app_config PASSED                            [100%] │
                                                                                                                                     │
===================================================== 8 passed in 1.30 seconds ======================================================│

@naveenkaje naveenkaje force-pushed the fix_part_size_check branch 2 times, most recently from 1306bb1 to 3f39f99 Compare January 31, 2019 15:40
If config is specified, check that part size is not
exceeding the region. Normally we now assume that
part.maxaddr() can be beyond end of rom.
@0xc0170
Copy link
Contributor

0xc0170 commented Feb 4, 2019

CI started

@mbed-ci
Copy link

mbed-ci commented Feb 4, 2019

Test run: SUCCESS

Summary: 12 of 12 test jobs passed
Build number : 1
Build artifacts

@NirSonnenschein
Copy link
Contributor

@0xc0170 CI passed, does this need another review?

@NirSonnenschein
Copy link
Contributor

@naveenkaje , please restore the template to the PR description and fill in.

@adbridge adbridge merged commit 645c1ce into ARMmbed:master Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants