Skip to content
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

Add sanitizer build option when building a file #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions scripts/mbedtls_framework/c_build_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import os
import platform
import re
import subprocess
import sys
import tempfile
Expand Down Expand Up @@ -78,6 +79,14 @@ def generate_c_file(c_file,
}
''')

CMAKE_CACHE_FILE = 'CMakeCache.txt'
CMAKE_SANITIZER_REGEXP = r'CMAKE_BUILD_TYPE:STRING=(MemSan|Asan|TSan)'
SANITIZER_OPTIONS = {
'Asan' : '-fsanitize=address',
'MemSan' : '-fsanitize=memory',
'TSan' : '-fsanitize=thread',
}

def compile_c_file(c_filename, exe_filename, include_dirs):
"""Compile a C source file with the host compiler.

Expand Down Expand Up @@ -105,6 +114,17 @@ def compile_c_file(c_filename, exe_filename, include_dirs):
obj_filename = exe_filename[:-4] + '.obj'
cmd += ['-Fe' + exe_filename, '-Fo' + obj_filename]
else:
# There are checks for sanitizer presence in the library and can cause build error
# if try to build without sanitizer option.
# When using make the option is passed via CFLAGS so it needs to be added.
# When using cmake the sanitizer usage can be read from the cache file.
if os.path.isfile(CMAKE_CACHE_FILE):
with open(CMAKE_CACHE_FILE, 'r', encoding='utf-8') as file:
m = re.findall(CMAKE_SANITIZER_REGEXP, file.read(), re.DOTALL)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why re.findall and not re.search since we only use the first hit?

if m:
cmd += [SANITIZER_OPTIONS[m[0]]]
elif 'CFLAGS' in os.environ:
cmd += [os.environ['CFLAGS']]
cmd += ['-o' + exe_filename]

subprocess.check_call(cmd + [c_filename])
Expand Down