diff --git a/node.gyp b/node.gyp index 009c3f7b36fb..5d45f53f920f 100644 --- a/node.gyp +++ b/node.gyp @@ -526,6 +526,7 @@ 'src/ffi/types.h', ], 'node_mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_mksnapshot<(EXECUTABLE_SUFFIX)', + 'node_mksnapshot_wxneeded_prefix%': [], 'node_js2c_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_js2c<(EXECUTABLE_SUFFIX)', 'conditions': [ [ 'node_shared=="true"', { @@ -541,6 +542,15 @@ }, { 'use_openssl_def%': 0, }], + [ 'OS=="freebsd"', { + 'node_mksnapshot_wxneeded_prefix': [ + '<(python)', + '<(DEPTH)/tools/freebsd-wxneeded.py', + '--exec', + 'node_mksnapshot', + '--', + ], + }], ], }, @@ -1107,6 +1117,7 @@ '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', ], 'action': [ + '<@(node_mksnapshot_wxneeded_prefix)', '<@(emulator)', '<(node_mksnapshot_exec)', '--build-snapshot', @@ -1127,6 +1138,7 @@ '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', ], 'action': [ + '<@(node_mksnapshot_wxneeded_prefix)', '<@(emulator)', '<@(_inputs)', '<@(_outputs)', @@ -1771,6 +1783,38 @@ }, ], }], + ['OS=="freebsd"', { + 'targets': [ + { + # FreeBSD enforces W^X when kern.elf{32,64}.allow_wx is 0. Node + # needs RWX pages for V8's code range, so mark the linked executable + # as requiring W+X mappings. + 'target_name': 'node_wxneeded', + 'type': 'none', + 'dependencies': [ + '<(node_core_target_name)', + ], + 'actions': [ + { + 'action_name': 'mark_node_wxneeded', + 'inputs': [ + '<(PRODUCT_DIR)/<(node_core_target_name)', + ], + 'outputs': [ + '<(SHARED_INTERMEDIATE_DIR)/node.wxneeded', + ], + 'action': [ + '<(python)', + '<(DEPTH)/tools/freebsd-wxneeded.py', + '--mark', + '<(PRODUCT_DIR)/<(node_core_target_name)', + '<(SHARED_INTERMEDIATE_DIR)/node.wxneeded', + ], + }, + ], + }, + ], + }], ['OS=="win" and node_shared=="true"', { 'targets': [ { diff --git a/tools/freebsd-wxneeded.py b/tools/freebsd-wxneeded.py new file mode 100755 index 000000000000..79919ce360b4 --- /dev/null +++ b/tools/freebsd-wxneeded.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +"""Mark FreeBSD binaries as requiring W+X mappings, or run them after marking. + +FreeBSD enforces W^X when kern.elf{32,64}.allow_wx is 0. V8 requires RWX pages +for its code range, so build-time V8 tools need the NT_FREEBSD_FCTL_WXNEEDED +feature bit set before they are executed. +""" + +import os +import subprocess +import sys + + +def mark_wxneeded(binary): + subprocess.check_call(['/usr/bin/elfctl', '-e', '+wxneeded', binary]) + + +def main(): + if len(sys.argv) == 4 and sys.argv[1] == '--mark': + mark_wxneeded(sys.argv[2]) + with open(sys.argv[3], 'w'): + pass + return 0 + + if len(sys.argv) >= 5 and sys.argv[1] == '--exec' and sys.argv[3] == '--': + binary_name = sys.argv[2] + command = sys.argv[4:] + for argument in command: + if os.path.basename(argument) == binary_name: + mark_wxneeded(argument) + break + else: + print(f'freebsd-wxneeded.py: {binary_name} not found in command', + file=sys.stderr) + return 2 + os.execvp(command[0], command) + + print('usage: freebsd-wxneeded.py --exec -- [args...]', + file=sys.stderr) + print(' freebsd-wxneeded.py --mark ', + file=sys.stderr) + return 2 + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/tools/v8_gypfiles/v8.gyp b/tools/v8_gypfiles/v8.gyp index 2d5d07f9cb00..13596c389a2e 100644 --- a/tools/v8_gypfiles/v8.gyp +++ b/tools/v8_gypfiles/v8.gyp @@ -9,6 +9,7 @@ 'v8_vector_stores%': 0, 'v8_embed_script%': "", 'mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)mksnapshot<(EXECUTABLE_SUFFIX)', + 'mksnapshot_wxneeded_prefix%': [], 'v8_os_page_size%': 0, 'generate_bytecode_output_root': '<(SHARED_INTERMEDIATE_DIR)/generate-bytecode-output-root', 'generate_bytecode_builtins_list_output': '<(generate_bytecode_output_root)/builtins-generated/bytecodes-builtins-list.h', @@ -482,6 +483,7 @@ }], ], 'action': [ + '<@(mksnapshot_wxneeded_prefix)', '<@(emulator)', '>@(_inputs)', '>@(mksnapshot_flags)', @@ -494,6 +496,17 @@ '<(perfetto_gyp_file):perfetto_sdk', ], }], + ['OS=="freebsd"', { + 'variables': { + 'mksnapshot_wxneeded_prefix': [ + '<(python)', + '<(DEPTH)/tools/freebsd-wxneeded.py', + '--exec', + 'mksnapshot', + '--', + ], + }, + }], ['want_separate_host_toolset', { 'dependencies': [ 'generate_bytecode_builtins_list#target',