Skip to content

Commit 9ae01e4

Browse files
committed
ci: Add a test app for not placing embedded file paths into binaries
Doubles as a test app that building with assertions off doesn't produce warnings. Closes #6306
1 parent 74fa526 commit 9ae01e4

10 files changed

+176
-0
lines changed

tools/ci/executable-list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ tools/mass_mfg/mfg_gen.py
9999
tools/mkdfu.py
100100
tools/mkuf2.py
101101
tools/set-submodules-to-github.sh
102+
tools/test_apps/system/no_embedded_paths/check_for_file_paths.py
102103
tools/test_idf_monitor/run_test_idf_monitor.py
103104
tools/test_idf_py/test_idf_py.py
104105
tools/test_idf_size/test.sh
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# The following lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(no_embedded_paths)
7+
8+
idf_build_get_property(idf_path IDF_PATH)
9+
idf_build_get_property(python PYTHON)
10+
idf_build_get_property(elf EXECUTABLE)
11+
12+
# If the configuration is one that doesn't expect any paths to be found then run this build step
13+
# after building the ELF, will fail if it finds any file paths in binary files
14+
if(CONFIG_OPTIMIZATION_ASSERTIONS_SILENT OR CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED)
15+
add_custom_command(
16+
TARGET ${elf}
17+
POST_BUILD
18+
COMMAND ${python} "${CMAKE_CURRENT_LIST_DIR}/check_for_file_paths.py" "${idf_path}" "${CMAKE_BINARY_DIR}"
19+
)
20+
endif()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# No Embedded Paths
2+
3+
This test app exists to verify that paths (like __FILE__) are not compiled into
4+
any object files in configurations where this should be avoided.
5+
6+
It doubles up as a build-time check that disabling assertions doesn't lead to
7+
any warnings.
8+
9+
(These configurations include: assertions disabled, 'silent' asserts, any reproducible
10+
builds configuration.)
11+
12+
Not embedding paths reduces the binary size, avoids leaking information about
13+
the compilation environment, and is a necessary step to supporet reproducible
14+
builds across projects built in different directories.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python
2+
#
3+
# 'check_for_file_paths.py' is a CI tool that checks all the unlinked object files
4+
# in a CMake build directory for embedded copies of IDF_PATH.
5+
#
6+
# Designed to be run in CI as a check that __FILE__ macros haven't snuck into any source code.
7+
#
8+
# Checking the unlinked object files means we don't rely on anything being actually linked into the binary,
9+
# just anything which could potentially be linked.
10+
#
11+
# Usage:
12+
# ./check_for_file_paths.py <IDF_PATH> <BUILD_DIR>
13+
#
14+
#
15+
#
16+
# Copyright 2019 Espressif Systems (Shanghai) PTE LTD
17+
#
18+
# Licensed under the Apache License, Version 2.0 (the "License");
19+
# you may not use this file except in compliance with the License.
20+
# You may obtain a copy of the License at
21+
#
22+
# http://www.apache.org/licenses/LICENSE-2.0
23+
#
24+
# Unless required by applicable law or agreed to in writing, software
25+
# distributed under the License is distributed on an "AS IS" BASIS,
26+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27+
# See the License for the specific language governing permissions and
28+
# limitations under the License.
29+
#
30+
import os
31+
import re
32+
import sys
33+
34+
from elftools.elf.elffile import ELFFile
35+
36+
# If an ESP-IDF source file has no option but to include __FILE__ macros, name it here (as re expression).
37+
#
38+
# IMPORTANT: This should only be used for upstream code where there is no other
39+
# option. ESP-IDF code should avoid embedding file names as much as possible to
40+
# limit the binary size and support reproducible builds
41+
#
42+
# Note: once ESP-IDF moves to Python >=3.6 then this can be simplified to use 'glob' and '**'
43+
EXCEPTIONS = [
44+
r'openssl/.+/ssl_pm.c.obj$', # openssl API requires __FILE__ in error reporting functions, as per upstream API
45+
r'openssl/.+/ssl_bio.c.obj$',
46+
r'unity/.+/unity_runner.c.obj$', # unity is not for production use, has __FILE__ for test information
47+
]
48+
49+
50+
def main(): # type: () -> None
51+
idf_path = sys.argv[1]
52+
build_dir = sys.argv[2]
53+
54+
assert os.path.exists(idf_path)
55+
assert os.path.exists(build_dir)
56+
57+
print('Checking object files in {} for mentions of {}...'.format(build_dir, idf_path))
58+
59+
# note: once ESP-IDF moves to Python >=3.6 then this can be simplified to use 'glob' and f'{build_dir}**/*.obj'
60+
files = []
61+
for (dirpath, _, filepaths) in os.walk(build_dir):
62+
files += [os.path.join(dirpath, filepath) for filepath in filepaths if filepath.endswith('.obj')]
63+
64+
print('Found {} object files...'.format(len(files)))
65+
66+
idf_path_binary = idf_path.encode() # we're going to be checking binary streams (note: probably non-ascii IDF_PATH will not match OK)
67+
68+
failures = 0
69+
for obj_file in files:
70+
if not any(re.search(exception, obj_file) for exception in EXCEPTIONS):
71+
failures += check_file(obj_file, idf_path_binary)
72+
if failures > 0:
73+
raise SystemExit('{} source files are embedding file paths, see list above.'.format(failures))
74+
print('No embedded file paths found')
75+
76+
77+
def check_file(obj_file, idf_path): # type: (str, bytes) -> int
78+
failures = 0
79+
with open(obj_file, 'rb') as f:
80+
elf = ELFFile(f)
81+
for sec in elf.iter_sections():
82+
# can't find a better way to filter out only sections likely to contain strings,
83+
# and exclude debug sections. .dram matches DRAM_STR, which links to .dram1
84+
if '.rodata' in sec.name or '.dram' in sec.name:
85+
contents = sec.data()
86+
if idf_path in contents:
87+
print('error: {} contains an unwanted __FILE__ macro'.format(obj_file))
88+
failures += 1
89+
break
90+
return failures
91+
92+
93+
if __name__ == '__main__':
94+
main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "test_no_embedded_paths_main.c"
2+
INCLUDE_DIRS ".")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* This test app only exists for the build stage, so doesn't need to do anything at runtime */
2+
void app_main(void)
3+
{
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y
2+
CONFIG_FREERTOS_ASSERT_DISABLE=y
3+
4+
# compiling as many files as possible here (we don't have 100% coverage of course, due to config options, but
5+
# try to maximize what we can check
6+
CONFIG_BT_ENABLED=y
7+
CONFIG_BT_BLUEDROID_ENABLED=y
8+
CONFIG_BLE_MESH=y
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y
2+
CONFIG_FREERTOS_ASSERT_DISABLE=y
3+
4+
# the other sdkconfig builds Bluedroid, build NimBLE here
5+
#
6+
# (Note: ESP32-S2 will build both these configs as well, but they're identical. This is simpler than
7+
# needing to specify per-target configs for both Bluedroid and Nimble on ESP32, ESP32-C3.)
8+
CONFIG_BT_ENABLED=y
9+
CONFIG_BT_NIMBLE_ENABLED=y
10+
CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=n
11+
CONFIG_BT_NIMBLE_MESH=y
12+
CONFIG_BLE_MESH=y
13+
CONFIG_BT_NIMBLE_MAX_CONNECTIONS=1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
2+
3+
# compiling as many files as possible here (we don't have 100% coverage of course, due to config options, but
4+
# try to maximize what we can check
5+
CONFIG_BT_ENABLED=y
6+
CONFIG_BT_BLUEDROID_ENABLED=y
7+
CONFIG_BLE_MESH=y
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
2+
CONFIG_FREERTOS_ASSERT_DISABLE=y
3+
4+
# the other sdkconfig builds Bluedroid, build NimBLE here
5+
#
6+
# (Note: ESP32-S2 will build both these configs as well, but they're identical. This is simpler than
7+
# needing to specify per-target configs for both Bluedroid and Nimble on ESP32, ESP32-C3.)
8+
CONFIG_BT_ENABLED=y
9+
CONFIG_BT_NIMBLE_ENABLED=y
10+
CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=n
11+
CONFIG_BT_NIMBLE_MESH=y
12+
CONFIG_BLE_MESH=y
13+
CONFIG_BT_NIMBLE_MAX_CONNECTIONS=1

0 commit comments

Comments
 (0)