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