Skip to content

Commit

Permalink
Add custom "emscripten_metadata" section to standalone WASM
Browse files Browse the repository at this point in the history
Currently it's not possible execute Emscripten-generated WASM files
without parsing certain data from the accompanying JS. This change adds
that data to the wasm file itself so that standalone WASM files
are executable by third-party WASM runtimes.
  • Loading branch information
rianhunter committed Jan 5, 2019
1 parent 9cdaf9e commit 7aa3b58
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
2 changes: 2 additions & 0 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2605,6 +2605,8 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,
if not shared.Settings.WASM_BACKEND and not DEBUG:
os.unlink(asm_target) # we don't need the asm.js, it can just confuse
sys.exit(0) # and we are done.
else:
shared.WebAssembly.add_emscripten_metadata(final, wasm_binary_target)
if options.opt_level >= 2:
# minify the JS
optimizer.do_minify() # calculate how to minify
Expand Down
63 changes: 58 additions & 5 deletions tools/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,14 @@ def get_emscripten_version(path):
EMSCRIPTEN_VERSION = get_emscripten_version(path_from_root('emscripten-version.txt'))
parts = [int(x) for x in EMSCRIPTEN_VERSION.split('.')]
EMSCRIPTEN_VERSION_MAJOR, EMSCRIPTEN_VERSION_MINOR, EMSCRIPTEN_VERSION_TINY = parts
# For the Emscripten-specific WASM metadata section, follows semver, changes
# whenever metadata section changes structure
# NB: major version 0 implies no compatibility
(EMSCRIPTEN_METADATA_MAJOR, EMSCRIPTEN_METADATA_MINOR) = (0, 0)
# For the JS/WASM ABI, follows semver, changes whenever musl C types
# change size/signedness or syscalls change signature
# NB: major version 0 implies no backward- compatibility
(EMSCRIPTEN_ABI_MAJOR, EMSCRIPTEN_ABI_MINOR) = (0, 0)


def generate_sanity():
Expand Down Expand Up @@ -2974,16 +2982,61 @@ def lebify(x):
return bytearray(ret)

@staticmethod
def make_shared_library(js_file, wasm_file, needed_dynlibs):
# a wasm shared library has a special "dylink" section, see tools-conventions repo
def get_js_data(js_file, shared=False):
js = open(js_file).read()
m = re.search("var STATIC_BUMP = (\d+);", js)
mem_size = int(m.group(1))
m = re.search("Module\['wasmTableSize'\] = (\d+);", js)
table_size = int(m.group(1))
m = re.search('gb = alignMemory\(getMemory\(\d+ \+ (\d+)\), (\d+) \|\| 1\);', js)
assert m.group(1) == m.group(2), 'js must contain a clear alignment for the wasm shared library'
mem_align = int(m.group(1))
if shared:
m = re.search('gb = alignMemory\(getMemory\(\d+ \+ (\d+)\), (\d+) \|\| 1\);', js)
assert m.group(1) == m.group(2), 'js must contain a clear alignment for the wasm shared library'
mem_align = int(m.group(1))
else:
mem_align = None
return (mem_size, table_size, mem_align)

@staticmethod
def add_emscripten_metadata(js_file, wasm_file):
(mem_size, table_size, _) = WebAssembly.get_js_data(js_file)
wasm = open(wasm_file).read()

raw_name = b'emscripten_metadata'
name = bytes(bytearray([len(raw_name)])) + raw_name
contents = b''.join(map(bytes, map(WebAssembly.lebify, [
# metadata section version
EMSCRIPTEN_METADATA_MAJOR,
EMSCRIPTEN_METADATA_MINOR,
# NB: The structure of the following should only be changed
# if EMSCRIPTEN_METADATA_MAJOR is incremented
# ABI version
EMSCRIPTEN_ABI_MAJOR,
EMSCRIPTEN_ABI_MINOR,
# static bump
mem_size,
# table size
table_size,
# NB: more data can be appended here as long as you increase
# the EMSCRIPTEN_METADATA_MINOR
])))

new_section = b''.join([
b'\0', # custom section byte ID (== 0)
bytes(WebAssembly.lebify(len(name) + len(contents))),
name,
contents,
])

with open(wasm_file, 'wb') as f:
f.write(wasm[0:8]) # copy magic number and version
# Emscripten metadata should be before any standard wasm section
f.write(new_section)
f.write(wasm[8:])

@staticmethod
def make_shared_library(js_file, wasm_file, needed_dynlibs):
# a wasm shared library has a special "dylink" section, see tools-conventions repo
(mem_size, table_size, mem_align) = WebAssembly.get_js_data(js_file, True)
mem_align = int(math.log(mem_align, 2))
logger.debug('creating wasm dynamic library with mem size %d, table size %d, align %d' % (mem_size, table_size, mem_align))
wso = js_file + '.wso'
Expand Down

0 comments on commit 7aa3b58

Please sign in to comment.