Skip to content

Commit

Permalink
Fix race condition in emar (emscripten-core#10161)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Jan 9, 2020
1 parent 1b0d5bc commit 4111bd1
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions emar.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os
import shutil
import sys
import tempfile

from tools.toolchain_profiler import ToolchainProfiler
from tools import shared
Expand All @@ -46,11 +47,13 @@ def run():
shared.exit_with_error(e)
newargs = [shared.LLVM_AR] + args[1:]

to_delete = []
tmpdir = None
response_filename = None

# The 3 argmuent form of ar doesn't involve other files. For example
# 'ar x libfoo.a'.
if len(newargs) > 3:
tmpdir = tempfile.mkdtemp(prefix='emar-')
cmd = newargs[1]
if 'r' in cmd or 'q' in cmd:
# We are adding files to the archive.
Expand All @@ -65,35 +68,28 @@ def run():
for j in range(out_arg_index + 1, len(newargs)):
orig_name = newargs[j]
full_name = os.path.abspath(orig_name)
dirname = os.path.dirname(full_name)
basename = os.path.basename(full_name)

h = hashlib.md5(full_name.encode('utf-8')).hexdigest()[:8]
parts = basename.split('.')
parts[0] += '_' + h
newname = '.'.join(parts)
full_newname = os.path.join(dirname, newname)
try:
shutil.copyfile(orig_name, full_newname)
newargs[j] = full_newname
to_delete.append(full_newname)
except Exception:
# it is ok to fail here, we just don't get hashing
pass
full_newname = os.path.join(tmpdir, newname)
shutil.copyfile(orig_name, full_newname)
newargs[j] = full_newname

if shared.DEBUG:
print('emar:', sys.argv, ' ==> ', newargs, file=sys.stderr)

response_filename = create_response_file(newargs[3:], shared.get_emscripten_temp_dir())
to_delete += [response_filename]
newargs = newargs[:3] + ['@' + response_filename]

if shared.DEBUG:
print('emar:', sys.argv, ' ==> ', newargs, file=sys.stderr)

rtn = shared.run_process(newargs, stdin=sys.stdin, check=False).returncode
for d in to_delete:
shared.try_delete(d)
if tmpdir:
shutil.rmtree(tmpdir)
shared.try_delete(response_filename)
return rtn


Expand Down

0 comments on commit 4111bd1

Please sign in to comment.