From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id E7ECCAFE35D; Thu, 11 Apr 2024 18:26:28 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E7ECCAFE35D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1712849189; bh=aK57q7neWqad15fZ8PzfnjYJQwHrAlKDyOg3XutT8pk=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Yh1s2UBz6Zxv4tdmIf6utjY4a9R7DNCCPNKVvkeLaBBJdgSFAGWqkxpfp8rHOlBY6 skFE+uUsKDETBtw3nZ0Le5XHlKYB1//NrwTqhLR5LDewgRyc63W6rf6L39l6BZHg4T XZbEtimnao+TYQPeOwtGQbQKGNMnO31/nOvqWaL8= Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id E38A5AFE358 for ; Thu, 11 Apr 2024 18:25:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E38A5AFE358 Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-516d727074eso7885947e87.0 for ; Thu, 11 Apr 2024 08:25:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712849159; x=1713453959; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6Dv3rR0KLIOshqtbHroriSz5kfMSFsw37LxeWEEbwr4=; b=QItbiR/CDOhjFI21TGJ41lmblLK5Uh4AFx6cje8FNdFdf945ocDjFLYDkRt/5aKLup nOqQYOMBhAW5qDBBkVFZfgDlDWZTnbaucIWo6eBrVEIBNKckzVCQHElb0x03KboLEAtA x/CX+qtXZ+DGghb9nsw7v3qyecbM0Oo7z78blKlXRQeU91bPSWtv5bFviA9BvUoGMWjc eJkDnAC01aASwiy3bTqKwPEINjHM6PKMN8EPfldbBZmAw0KeAiLYS64pnPNndDAQpwbc OEM0dw/Xy13oVgVVoBeVdsLJb1uS6Yc0xRU3bRVjLwks/2AUiZafC2t4XlB9/jVHjOLE TWbg== X-Gm-Message-State: AOJu0YxygR6gesTALHFGd8xCLdH2cKEl3V/BmkTiMsHQ+DHu5tB80Ciz VNVtlw3PY8R+SCsj4mfTi3KDuGxI80awXSu+Qar/L/yFrFRLMw3sx4OewQ== X-Google-Smtp-Source: AGHT+IFAfdwlpPZY3KNdGwLqbSWljuaQ8OFydLSpnxfSrT0O1nz8NfkX4WHNDKYyeJSWVl0nJK9KFw== X-Received: by 2002:a19:f710:0:b0:516:c5b0:c5ce with SMTP id z16-20020a19f710000000b00516c5b0c5cemr23826lfe.45.1712849158654; Thu, 11 Apr 2024 08:25:58 -0700 (PDT) Received: from pony.. ([5.181.62.126]) by smtp.gmail.com with ESMTPSA id b5-20020ac25e85000000b00516d6419a70sm235623lfq.85.2024.04.11.08.25.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 08:25:58 -0700 (PDT) To: tarantool-patches@dev.tarantool.org, Sergey Kaplun , Maxim Kokryashkin Date: Thu, 11 Apr 2024 16:22:06 +0300 Message-Id: <7bdffd2650a785877e03584e6d532e855d09de8a.1712841312.git.sergeyb@tarantool.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit 3/4][v2] OSX/iOS/ARM64: Fix generation of Mach-O object files. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall Thanks to Carlo Cabrera. (cherry picked from commit 3065c910ad6027031aabe2dfd3c26a3d0f014b4f) The Mach-O FAT object constructed by LuaJIT had an incorrect format. The problem is reproduced when the target hardware platform has AVX512F and LuaJIT is compiled with enabled AVX512F instructions. The problem arises because LuaJIT FFI code for Mach-O file generation in `bcsave.lua` relies on undefined behavior for conversions to `uint32_t`. AVX512F has the `vcvttsd2usi` instruction, which converts `double`/`float` to `uint32_t/uint64_t`. Earlier architectures (SSE2, AVX2) are sorely lacking such an instruction, as they only support signed conversions. Unsigned conversions are done with a signed convert and range shifting - the exact algorithm depends on the compiler. A side-effect of these workarounds is that negative `double`/`float` often inadvertently convert 'as expected', even though this is invoking undefined behavior. Whereas `vcvttsd2usi` always returns 0x80000000 or 0x8000000000000000 for out-of-range inputs. The patch fixes the problem, however, the real issue remains unfixed. Sergey Bronnikov: * added the description, the test for the problem and flavor with AVX512 to `exotic-builds-testing` workflow Part of tarantool/tarantool#9595 --- src/jit/bcsave.lua | 6 +- test/LuaJIT-tests/CMakeLists.txt | 9 + ...-865-cross-generation-mach-o-file.test.lua | 300 ++++++++++++++++++ 3 files changed, 312 insertions(+), 3 deletions(-) create mode 100644 test/tarantool-tests/lj-865-cross-generation-mach-o-file.test.lua diff --git a/src/jit/bcsave.lua b/src/jit/bcsave.lua index a287d675..7aec1555 100644 --- a/src/jit/bcsave.lua +++ b/src/jit/bcsave.lua @@ -446,18 +446,18 @@ typedef struct { uint32_t value; } mach_nlist; typedef struct { - uint32_t strx; + int32_t strx; uint8_t type, sect; uint16_t desc; uint64_t value; } mach_nlist_64; typedef struct { - uint32_t magic, nfat_arch; + int32_t magic, nfat_arch; } mach_fat_header; typedef struct { - uint32_t cputype, cpusubtype, offset, size, align; + int32_t cputype, cpusubtype, offset, size, align; } mach_fat_arch; typedef struct { struct { diff --git a/test/LuaJIT-tests/CMakeLists.txt b/test/LuaJIT-tests/CMakeLists.txt index b8e4dfc4..6d073700 100644 --- a/test/LuaJIT-tests/CMakeLists.txt +++ b/test/LuaJIT-tests/CMakeLists.txt @@ -52,6 +52,15 @@ if(LUAJIT_NO_UNWIND) set(LUAJIT_TEST_TAGS_EXTRA +internal_unwinder) endif() +if(CMAKE_C_FLAGS MATCHES "-march=skylake-avx512") + # FIXME: Test verifies bitwise operations on numbers. + # There is a known issue - bitop doesn't work in LuaJIT built + # with the enabled AVX512 instruction set, see + # https://github.com/tarantool/tarantool/issues/6787. + # Hence, skip this when "skylake-avx512" is passed. + set(LUAJIT_TEST_TAGS_EXTRA +avx512) +endif() + set(TEST_SUITE_NAME "LuaJIT-tests") # XXX: The call produces both test and target diff --git a/test/tarantool-tests/lj-865-cross-generation-mach-o-file.test.lua b/test/tarantool-tests/lj-865-cross-generation-mach-o-file.test.lua new file mode 100644 index 00000000..04fb5495 --- /dev/null +++ b/test/tarantool-tests/lj-865-cross-generation-mach-o-file.test.lua @@ -0,0 +1,300 @@ +local tap = require('tap') +local test = tap.test('lj-865-cross-generation-mach-o-file') +local utils = require('utils') + +test:plan(1) + +-- The test creates an object file in Mach-O format with LuaJIT +-- bytecode and checks the validity of the object file fields. +-- +-- The original problem is reproduced with LuaJIT that built with +-- enabled AVX512F instructions. The support for AVX512F could be +-- checked in `/proc/cpuinfo` on Linux and +-- `sysctl hw.optional.avx512f` on Mac. AVX512F must be +-- implicitly enabled in a C compiler by passing a CPU codename. +-- Please take a look at the GCC Online Documentation [1] for +-- available CPU codenames. Also, see the Wikipedia for CPUs with +-- AVX-512 support [2]. +-- To detect the CPU codename execute: +-- `gcc -march=native -Q --help=target | grep march`. +-- +-- 1. https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html +-- 2. https://en.wikipedia.org/wiki/AVX-512#CPUs_with_AVX-512 +-- +-- Manual steps for reproducing are the following: +-- +-- $ CC=gcc TARGET_CFLAGS='skylake-avx512' cmake -S . -B build +-- $ cmake --build build --parallel +-- $ echo > test.lua +-- $ LUA_PATH="src/?.lua;;" luajit -b -o osx -a arm test.lua test.o +-- $ file test.o +-- empty.o: DOS executable (block device driver) + +local ffi = require('ffi') + +-- LuaJIT can generate so called Universal Binary with Lua +-- bytecode. The Universal Binary format is a format for +-- executable files that run natively on hardware platforms with +-- different hardware architectures. This concept is more +-- generally known as a fat binary. +-- +-- The format of the Mach-O is described in the document +-- "OS X ABI Mach-O File Format Reference", published by Apple +-- company. The copy of the (now removed) official documentation +-- can be found here [1]. Yet another source of truth is +-- XNU headers, see the definition of C-structures in: +-- [2] (`nlist_64`), [3] (`fat_arch` and `fat_header`). +-- +-- There are a good visual representation of Universal Binary +-- in "Mac OS X Internals" book (pages 67-68) [5] and in the [6]. +-- Below is the schematic structure of Universal Binary, which +-- includes two executables for PowerPC and Intel i386 (omitted): +-- +-- 0x0000000 --------------------------------------- +-- | +-- struct | 0xcafebabe FAT_MAGIC magic +-- fat_header | ------------------------------------- +-- | 0x00000003 nfat_arch +-- --------------------------------------- +-- | 0x00000012 CPU_TYPE_POWERPC cputype +-- | ------------------------------------- +-- | 0x00000000 CPU_SUBTYPE_POWERPC_ALL cpusubtype +-- struct | ------------------------------------- +-- fat_arch | 0x00001000 4096 bytes offset +-- | ------------------------------------- +-- | 0x00004224 16932 bytes size +-- | ------------------------------------- +-- | 0x0000000c 2^12 = 4096 bytes align +-- --------------------------------------- +-- --------------------------------------- +-- | 0x00000007 CPU_TYPE_I386 cputype +-- | ------------------------------------- +-- | 0x00000003 CPU_SUBTYPE_I386_ALL cpusubtype +-- struct | ------------------------------------- +-- fat_arch | 0x00006000 24576 bytes offset +-- | ------------------------------------- +-- | 0x0000292c 10540 bytes size +-- | ------------------------------------- +-- | 0x0000000c 2^12 = 4096 bytes align +-- --------------------------------------- +-- Unused +-- 0x00001000 --------------------------------------- +-- | 0xfeedface MH_MAGIC magic +-- | ------------------------------------ +-- | 0x00000012 CPU_TYPE_POWERPC cputype +-- | ------------------------------------ +-- struct | 0x00000000 CPU_SUBTYPE_POWERPC_ALL cpusubtype +-- mach_header | ------------------------------------ +-- | 0x00000002 MH_EXECUTE filetype +-- | ------------------------------------ +-- | 0x0000000b 10 load commands ncmds +-- | ------------------------------------ +-- | 0x00000574 1396 bytes sizeofcmds +-- | ------------------------------------ +-- | 0x00000085 DYLDLINK TWOLEVEL flags +-- -------------------------------------- +-- Load commands +-- --------------------------------------- +-- Data +-- --------------------------------------- +-- +-- < x86 executable > +-- +-- 1. https://github.com/aidansteele/osx-abi-macho-file-format-reference +-- 2. https://github.com/apple-oss-distributions/xnu/blob/xnu-10002.1.13/EXTERNAL_HEADERS/mach-o/nlist.h +-- 3. https://github.com/apple-oss-distributions/xnu/blob/xnu-10002.1.13/EXTERNAL_HEADERS/mach-o/fat.h +-- 4. https://developer.apple.com/documentation/apple-silicon/addressing-architectural-differences-in-your-macos-code +-- 5. https://reverseengineering.stackexchange.com/a/6357/46029 +-- 6. http://formats.kaitai.io/mach_o/index.html +-- +-- Using the same declarations as defined in . +ffi.cdef[[ +typedef struct +{ + uint32_t magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags; +} mach_header; + +typedef struct { + uint32_t cmd, cmdsize; + char segname[16]; + uint32_t vmaddr, vmsize, fileoff, filesize; + uint32_t maxprot, initprot, nsects, flags; +} mach_segment_command; + +typedef struct { + char sectname[16], segname[16]; + uint32_t addr, size; + uint32_t offset, align, reloff, nreloc, flags; + uint32_t reserved1, reserved2; +} mach_section; + +typedef struct { + uint32_t cmd, cmdsize, symoff, nsyms, stroff, strsize; +} mach_symtab_command; + +typedef struct { + int32_t strx; + uint8_t type, sect; + int16_t desc; + uint32_t value; +} mach_nlist; + +typedef struct +{ + int32_t magic, nfat_arch; +} mach_fat_header; + +typedef struct +{ + int32_t cputype, cpusubtype, offset, size, align; +} mach_fat_arch; + +typedef struct { + mach_fat_header fat; + mach_fat_arch fat_arch[2]; + struct { + mach_header hdr; + mach_segment_command seg; + mach_section sec; + mach_symtab_command sym; + } arch[2]; + mach_nlist sym_entry; + uint8_t space[4096]; +} mach_fat_obj; +]] + +local function create_obj_file(name, arch) + local mach_o_path = os.tmpname() .. '.o' + local lua_path = os.getenv('LUA_PATH') + local lua_bin = utils.exec.luacmd(arg):match('%S+') + local cmd_fmt = 'LUA_PATH="%s" %s -b -n "%s" -o osx -a %s -e "print()" %s' + local cmd = (cmd_fmt):format(lua_path, lua_bin, name, arch, mach_o_path) + local ret = os.execute(cmd) + assert(ret == 0, 'cannot create an object file') + return mach_o_path +end + +-- Parses a buffer in the Mach-O format and returns +-- the FAT magic number and `nfat_arch`. +local function read_mach_o(buf) + local res = { + header = { + magic = 0, + nfat_arch = 0, + }, + fat_arch = {}, + } + + -- Mach-O FAT object. + local mach_fat_obj_type = ffi.typeof('mach_fat_obj *') + local obj = ffi.cast(mach_fat_obj_type, buf) + + -- Mach-O FAT object header. + local mach_fat_header = obj.fat + -- Mach-O FAT is BE, target arch is LE. + local be32 = bit.bswap + res.header.magic = be32(mach_fat_header.magic) + res.header.nfat_arch = be32(mach_fat_header.nfat_arch) + + -- Mach-O FAT object arches. + for i = 0, res.header.nfat_arch - 1 do + local fat_arch = obj.fat_arch[i] + local arch = { + cputype = be32(fat_arch.cputype), + cpusubtype = be32(fat_arch.cpusubtype), + } + table.insert(res.fat_arch, arch) + end + + return res +end + +-- Universal Binary can contain executables for more than one +-- CPU architecture. For simplicity, the test compares the sum of +-- CPU types and CPU subtypes. +-- +-- has the definitions of the +-- numbers below. The original XNU source code may be found in +-- [1]. +-- +-- 1. https://opensource.apple.com/source/xnu/xnu-4570.41.2/osfmk/mach/machine.h.auto.html +-- +local SUM_CPUTYPE = { + arm = 7 + 12, +} +local SUM_CPUSUBTYPE = { + arm = 3 + 9, +} + +-- The function builds Mach-O FAT object file and retrieves +-- its header fields (magic and nfat_arch) and fields of each arch +-- (cputype, cpusubtype). +-- +-- The Mach-O FAT object header can be retrieved with `otool` on +-- macOS: +-- +-- $ otool -f empty.o +-- Fat headers +-- fat_magic 0xcafebabe +-- nfat_arch 2 +-- +-- +-- CPU type and subtype can be retrieved with `lipo` on macOS: +-- +-- $ luajit -b -o osx -a arm empty.lua empty.o +-- $ lipo -archs empty.o +-- i386 armv7 +-- $ luajit -b -o osx -a arm64 empty.lua empty.o +-- $ lipo -archs empty.o +-- x86_64 arm64 +local function build_and_check_mach_o(subtest, hw_arch) + assert(hw_arch == 'arm') + + subtest:plan(4) + -- FAT_MAGIC is an integer containing the value 0xCAFEBABE in + -- big-endian byte order format. On a big-endian host CPU, + -- this can be validated using the constant FAT_MAGIC; + -- on a little-endian host CPU, it can be validated using + -- the constant FAT_CIGAM. + -- + -- FAT_NARCH is an integer specifying the number of fat_arch + -- data structures that follow. This is the number of + -- architectures contained in this binary. + -- + -- See the aforementioned "OS X ABI Mach-O File Format + -- Reference". + local FAT_MAGIC = '0xffffffffcafebabe' + local FAT_NARCH = 2 + + local MODULE_NAME = 'lango_team' + + local mach_o_obj_path = create_obj_file(MODULE_NAME, hw_arch) + local mach_o_buf = utils.tools.read_file(mach_o_obj_path) + assert(mach_o_buf ~= nil and #mach_o_buf ~= 0, 'cannot read an object file') + + local mach_o = read_mach_o(mach_o_buf) + + -- Teardown. + assert(os.remove(mach_o_obj_path), 'remove an object file') + + local magic_str = string.format('%#x', mach_o.header.magic) + subtest:is(magic_str, FAT_MAGIC, + 'fat_magic is correct in Mach-O') + subtest:is(mach_o.header.nfat_arch, FAT_NARCH, + 'nfat_arch is correct in Mach-O') + + local total_cputype = 0 + local total_cpusubtype = 0 + for i = 1, FAT_NARCH do + total_cputype = total_cputype + mach_o.fat_arch[i].cputype + total_cpusubtype = total_cpusubtype + mach_o.fat_arch[i].cpusubtype + end + subtest:is(total_cputype, SUM_CPUTYPE[hw_arch], + 'cputype is correct in Mach-O') + subtest:is(total_cpusubtype, SUM_CPUSUBTYPE[hw_arch], + 'cpusubtype is correct in Mach-O') +end + +test:test('arm', build_and_check_mach_o, 'arm') + +test:done(true) -- 2.34.1