| 
 | 1 | +--[[  | 
 | 2 | +SPDX-License-Identifier: ISC  | 
 | 3 | +Copyright (c) 2023-2025, Sergey Bronnikov.  | 
 | 4 | +
  | 
 | 5 | +ARM64: Should not fuse sign-extension into logical operands,  | 
 | 6 | +can fuse rotations though,  | 
 | 7 | +https://github.com/LuaJIT/LuaJIT/issues/1076  | 
 | 8 | +
  | 
 | 9 | +Wrong code generation for constants in bitwise operations,  | 
 | 10 | +https://github.com/lua/lua/commit/c764ca71a639f5585b5f466bea25dc42b855a4b0  | 
 | 11 | +
  | 
 | 12 | +Synopsis: bit.bor(x1 [,x2...])  | 
 | 13 | +]]  | 
 | 14 | + | 
 | 15 | +local luzer = require("luzer")  | 
 | 16 | +local test_lib = require("lib")  | 
 | 17 | + | 
 | 18 | +local unpack = unpack or table.unpack  | 
 | 19 | + | 
 | 20 | +local bor  | 
 | 21 | +if test_lib.lua_version() == "LuaJIT" then  | 
 | 22 | +    bor = bit.bor  | 
 | 23 | +else  | 
 | 24 | +    bor = test_lib.bitwise_op("|")  | 
 | 25 | +end  | 
 | 26 | + | 
 | 27 | +local function TestOneInput(buf)  | 
 | 28 | +    local fdp = luzer.FuzzedDataProvider(buf)  | 
 | 29 | +    local MAX_INT = test_lib.MAX_INT  | 
 | 30 | +    local MIN_INT = test_lib.MIN_INT  | 
 | 31 | +    local x = fdp:consume_integer(MIN_INT, MAX_INT)  | 
 | 32 | +    local y = fdp:consume_integer(MIN_INT, MAX_INT)  | 
 | 33 | +    local z = fdp:consume_integer(MIN_INT, MAX_INT)  | 
 | 34 | +    local res = bor(x, y)  | 
 | 35 | +    assert(type(res) == "number")  | 
 | 36 | + | 
 | 37 | +    -- Commutative law.  | 
 | 38 | +    assert(bor(x, y) == bor(y, x))  | 
 | 39 | + | 
 | 40 | +    assert(bor(x, bor(y, z)) == bor(bor(x, y), z))  | 
 | 41 | +    assert(bor(x, 0) == x)  | 
 | 42 | +    assert(bor(x, x) == x)  | 
 | 43 | +    if test_lib.lua_version() == "LuaJIT" then  | 
 | 44 | +        local MAX_UINT = bor(test_lib.MAX_INT, test_lib.MIN_INT)  | 
 | 45 | +        assert(bor(x, MAX_UINT) == MAX_UINT)  | 
 | 46 | +    else  | 
 | 47 | +        local MAX_UINT64 = bor(test_lib.MAX_INT64, test_lib.MIN_INT64)  | 
 | 48 | +        assert(bor(x, MAX_UINT64) == MAX_UINT64)  | 
 | 49 | +    end  | 
 | 50 | + | 
 | 51 | +    -- Multiple arguments.  | 
 | 52 | +    -- `n` must be less than UINT_MAX and there are at least extra  | 
 | 53 | +    -- free stack slots in the stack, otherwise an error  | 
 | 54 | +    -- "too many results to unpack" is raised, see <ltablib.c>.  | 
 | 55 | +    local n = fdp:consume_integer(2, 1024)  | 
 | 56 | +    local args = fdp:consume_integers(0, MAX_INT, n)  | 
 | 57 | +    res = bor(unpack(args))  | 
 | 58 | +    assert(type(res) == "number")  | 
 | 59 | + | 
 | 60 | +    -- Commutative law.  | 
 | 61 | +    table.sort(args)  | 
 | 62 | +    assert(res == bor(unpack(args)))  | 
 | 63 | +end  | 
 | 64 | + | 
 | 65 | +local args = {  | 
 | 66 | +    artifact_prefix = "bitop_bor_",  | 
 | 67 | +}  | 
 | 68 | +luzer.Fuzz(TestOneInput, nil, args)  | 
0 commit comments