Skip to content

Commit

Permalink
refactor out sanitizer plugin
Browse files Browse the repository at this point in the history
commit_hash:d0bd11c053e40d5117172fd59c19b6d6caf6fb8e
  • Loading branch information
pg committed Feb 20, 2025
1 parent c1a2810 commit c918a80
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 85 deletions.
3 changes: 0 additions & 3 deletions build/scripts/link_dyn_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,6 @@ def do_fix(p):

return list(f(list(parse_export_file(fname))))

if p.endswith('.supp'):
return []

if p.endswith('.pkg.fake'):
return []

Expand Down
84 changes: 2 additions & 82 deletions build/scripts/link_exe.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@
from process_whole_archive_option import ProcessWholeArchiveOption


def get_leaks_suppressions(cmd):
supp, newcmd = [], []
for arg in cmd:
if arg.endswith(".supp"):
supp.append(arg)
else:
newcmd.append(arg)
return supp, newcmd


CUDA_LIBRARIES = {
'-lcublas_static': '-lcublas',
'-lcublasLt_static': '-lcublasLt',
Expand Down Expand Up @@ -212,48 +202,6 @@ def remove_excessive_flags(cmd):
return flags


def fix_sanitize_flag(cmd, opts):
"""
Remove -fsanitize=address flag if sanitazers are linked explicitly for linux target.
"""
for flag in cmd:
if flag.startswith('--target') and 'linux' not in flag.lower():
# use toolchained sanitize libraries
return cmd
assert opts.clang_ver
CLANG_RT = 'contrib/libs/clang' + opts.clang_ver + '-rt/lib/'
sanitize_flags = {
'-fsanitize=address': CLANG_RT + 'asan',
'-fsanitize=memory': CLANG_RT + 'msan',
'-fsanitize=leak': CLANG_RT + 'lsan',
'-fsanitize=undefined': CLANG_RT + 'ubsan',
'-fsanitize=thread': CLANG_RT + 'tsan',
}

used_sanitize_libs = []
aux = []
for flag in cmd:
if flag.startswith('-fsanitize-coverage='):
# do not link sanitizer libraries from clang
aux.append('-fno-sanitize-link-runtime')
if flag in sanitize_flags and any(s.startswith(sanitize_flags[flag]) for s in cmd):
# exclude '-fsanitize=' if appropriate library is linked explicitly
continue
if any(flag.startswith(lib) for lib in sanitize_flags.values()):
used_sanitize_libs.append(flag)
continue
aux.append(flag)

# move sanitize libraries out of the repeatedly searched group of archives
flags = []
for flag in aux:
if flag == '-Wl,--start-group':
flags += ['-Wl,--whole-archive'] + used_sanitize_libs + ['-Wl,--no-whole-archive']
flags.append(flag)

return flags


def fix_cmd_for_dynamic_cuda(cmd):
flags = []
for flag in cmd:
Expand All @@ -278,27 +226,6 @@ def remove_libs(cmd, libs):
return flags


def gen_default_suppressions(inputs, output, source_root):
import collections
import os

supp_map = collections.defaultdict(set)
for filename in inputs:
sanitizer = os.path.basename(filename).split('.', 1)[0]
with open(os.path.join(source_root, filename)) as src:
for line in src:
line = line.strip()
if not line or line.startswith('#'):
continue
supp_map[sanitizer].add(line)

with open(output, "wb") as dst:
for supp_type, supps in supp_map.items():
dst.write('extern "C" const char *__%s_default_suppressions() {\n' % supp_type)
dst.write(' return "{}";\n'.format('\\n'.join(sorted(supps))))
dst.write('}\n')


def parse_args(args):
parser = optparse.OptionParser()
parser.disable_interspersed_args()
Expand Down Expand Up @@ -332,17 +259,16 @@ def parse_args(args):
args = args[:ib] + args[ie + 1:]

for p in plugins:
res = subprocess.check_output([sys.executable, p] + args).decode().strip()
res = subprocess.check_output([sys.executable, p, sys.argv[0]] + args).decode().strip()

if res:
args = json.loads(res)
args = json.loads(res)[1:]

opts, args = parse_args(args)
args = pcf.skip_markers(args)

cmd = args
cmd = remove_excessive_flags(cmd)
cmd = fix_sanitize_flag(cmd, opts)

if opts.dynamic_cuda:
cmd = fix_cmd_for_dynamic_cuda(cmd)
Expand All @@ -360,12 +286,6 @@ def parse_args(args):
assert opts.python
subprocess.check_call([opts.python] + [opts.custom_step] + args)

supp, cmd = get_leaks_suppressions(cmd)
if supp:
src_file = "default_suppressions.cpp"
gen_default_suppressions(supp, src_file, opts.source_root)
cmd += [src_file]

if opts.linker_output:
stdout = open(opts.linker_output, 'w')
else:
Expand Down
4 changes: 4 additions & 0 deletions build/ymake.core.conf
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,10 @@ module _LINK_UNIT: _BASE_UNIT {
LINK_SCRIPT_EXE_FLAGS += --dynamic-cuda
}

when ($SANITIZER_TYPE && $SANITIZER_TYPE != "no") {
PEERDIR += library/cpp/sanitizer/plugin
}

when ($USE_DYNAMIC_CUDA != "yes") {
when ($CUDA_ARCHITECTURES) {
LINK_DYN_LIB_FLAGS+=--cuda-architectures $CUDA_ARCHITECTURES
Expand Down

0 comments on commit c918a80

Please sign in to comment.