rlbox.mozbuild (3838B)
1 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- 2 # vim: set filetype=python: 3 # This Source Code Form is subject to the terms of the Mozilla Public 4 # License, v. 2.0. If a copy of the MPL was not distributed with this 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 7 8 @template 9 def RLBoxLibrary(name, use_segue=True): 10 WASM_SOURCES += [ 11 "/memory/mozalloc/mozalloc.cpp", 12 "/third_party/rlbox_wasm2c_sandbox/c_src/wasm2c_sandbox_wrapper.c", 13 ] 14 15 if CONFIG["DEVELOPER_OPTIONS"]: 16 wasm_sources = sorted(f"!{name}.wasm_{n}.c" for n in range(16)) 17 else: 18 wasm_sources = [f"!{name}.wasm.c"] 19 SOURCES += wasm_sources 20 SOURCES += ["/third_party/wasm2c/wasm2c/wasm-rt-impl.c"] 21 SOURCES += ["/third_party/wasm2c/wasm2c/wasm-rt-mem-impl.c"] 22 23 # Configuration for the wasm2c runtime used by RLBox 24 25 # Enable SIMD autovectorization 26 if CONFIG["WASM_CC_VERSION"] and int(CONFIG["WASM_CC_VERSION"].split(".")[0]) >= 11: 27 WASM_CXXFLAGS += ["-msimd128"] 28 29 # Use a mmap style allocation 30 DEFINES["WASM_RT_USE_MMAP"] = 1 31 32 # Don't use internal signal handler as Firefox already provides one 33 DEFINES["WASM_RT_SKIP_SIGNAL_RECOVERY"] = 1 34 35 # We provide a custom trap handler that calls MOZ_CRASH 36 DEFINES["WASM_RT_TRAP_HANDLER"] = "moz_wasm2c_trap_handler" 37 38 # Don't limit the nested call depth 39 DEFINES["WASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION"] = 1 40 41 # Configure the wasm runtime to invoke a callback when a Wasm memory growth 42 # fails inside the sandbox. This information is used to annotate crash reports. 43 DEFINES["WASM_RT_GROW_FAILED_HANDLER"] = "moz_wasm2c_memgrow_failed" 44 45 for source in wasm_sources: 46 SOURCES[source].flags += ["-Wno-unused"] 47 48 # Use Segue optimization. Since this requires passing the "-mfsgsbase", we 49 # restrict this to compilers and architectures that support this flag. 50 # Manually exclude android for now due to bug in upstream wasm2c support 51 if ( 52 use_segue 53 and CONFIG["CC_TYPE"] in ("clang", "gcc") 54 and CONFIG["TARGET_CPU"] == "x86_64" 55 and CONFIG["OS_TARGET"] != "Android" 56 # https://github.com/llvm/llvm-project/issues/124238 57 and not (CONFIG["MOZ_ASAN"] or CONFIG["MOZ_TSAN"]) 58 ): 59 # Setting WASM_RT_ALLOW_SEGUE means Segue is used on supported 60 # OS/compiler/Arch combinations. For example, it is used for 61 # Linux/clang/x86_64 but not on Windows 62 DEFINES["WASM_RT_ALLOW_SEGUE"] = 1 63 64 # Segue can assume there is no other use of the segment register by the 65 # application. This is fine as the System V ABI for x86_64 systems does 66 # not use the gs register for any other purpose. 67 DEFINES["WASM_RT_SEGUE_FREE_SEGMENT"] = 1 68 69 # Add a compiler flag for the wasm2c produced file to enable use of 70 # wrgsbase64 instruction via intrinsics. While this instruction is 71 # available only on CPUs starting Ivybridge, the wasm2c runtime checks for 72 # support before using this instruction. Thus, enabling this flag does not 73 # introduce a dependency on newer CPUs to Firefox. 74 CFLAGS += ["-mfsgsbase"] 75 76 WASM_DEFINES["MOZ_IN_WASM_SANDBOX"] = True 77 if CONFIG["ENABLE_CLANG_PLUGIN"]: 78 WASM_DEFINES["MOZ_CLANG_PLUGIN"] = True 79 80 SANDBOXED_WASM_LIBRARY_NAME = f"{name}.wasm" 81 82 # Ideally we'd also list {name}.wasm.h as an output, but that would put the 83 # generation in export rather than pre-compile, and we prefer the latter. 84 GeneratedFile( 85 f"{name}.wasm.c", 86 script="/config/wasm2c.py", 87 entry_point="wasm2c", 88 inputs=["!/dist/host/bin/wasm2c" + CONFIG["HOST_BIN_SUFFIX"], f"!{name}.wasm"], 89 flags=["--num-outputs", len(wasm_sources)] if len(wasm_sources) > 1 else [], 90 )