broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(prog):
    labels_seen = set()
    for ins in prog:
        if ins[0] == 'label':
            if ins[1] in labels_seen:
                return ['duplicate-label', ins[1]]
            labels_seen.add(ins[1])
    for ins in prog:
        if ins[0] in ('jmp', 'jz') and ins[1] not in labels_seen:
            return ['undefined-label', ins[1]]
    long_form = set()
    def size(i, ins):
        if i in long_form:
            return 4 if ins[0] == 'jz' else 3
        return 2
    rounds = 0
    while True:
        rounds += 1
        addr = 0
        addrs = []
        where = {}
        for i, ins in enumerate(prog):
            addrs.append(addr)
            if ins[0] == 'label':
                where[ins[1]] = addr
            elif ins[0] == 'data':
                addr += ins[1]
            else:
                addr += size(i, ins)
        changed = False
        for i, ins in enumerate(prog):
            if ins[0] in ('jmp', 'jz') and i not in long_form:
                off = where[ins[1]] - (addrs[i] + 2)
                if abs(off) > 127:
                    long_form.add(i)
                    changed = True
        if not changed or rounds > len(prog):
            break
    out = []
    for i, ins in enumerate(prog):
        if ins[0] in ('jmp', 'jz'):
            suffix = '.w' if i in long_form else ''
            out.append([addrs[i], ins[0] + suffix, where[ins[1]] - (addrs[i] + size(i, ins))])
    return {'branches': out, 'size': addr}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: three-step widening cascade',
   ([['data', 1],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[1, 'jmp.w', 129], [14, 'jz.w', 128], [133, 'jmp.w', 128]], 'size': 265}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 1], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [129, 'jmp.w', -132]], 'size': 132}),
  ('forward branch just around the short limit',
   ([['data', 1], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[1, 'jmp', 127], [130, 'jz.w', 128]], 'size': 262}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 201], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 201], [205, 'jmp', -2]], 'size': 207}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 1], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [3, 'jmp', -3]], 'size': 5}),
  ('duplicate label', ([['label', 'a'], ['data', 1], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 1]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 2],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[2, 'jmp.w', 129], [15, 'jz.w', 128], [134, 'jmp.w', 128]], 'size': 266}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 2], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [130, 'jmp.w', -133]], 'size': 133}),
  ('forward branch just around the short limit',
   ([['data', 2], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[2, 'jmp', 127], [131, 'jz.w', 128]], 'size': 263}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 202], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 202], [206, 'jmp', -2]], 'size': 208}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 2], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [4, 'jmp', -4]], 'size': 6}),
  ('duplicate label', ([['label', 'a'], ['data', 2], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 2]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 3],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[3, 'jmp.w', 129], [16, 'jz.w', 128], [135, 'jmp.w', 128]], 'size': 267}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 3], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [131, 'jmp.w', -134]], 'size': 134}),
  ('forward branch just around the short limit',
   ([['data', 3], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[3, 'jmp', 127], [132, 'jz.w', 128]], 'size': 264}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 203], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 203], [207, 'jmp', -2]], 'size': 209}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 3], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [5, 'jmp', -5]], 'size': 7}),
  ('duplicate label', ([['label', 'a'], ['data', 3], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 3]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 4],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[4, 'jmp.w', 129], [17, 'jz.w', 128], [136, 'jmp.w', 128]], 'size': 268}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 4], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [132, 'jmp.w', -135]], 'size': 135}),
  ('forward branch just around the short limit',
   ([['data', 4], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[4, 'jmp', 127], [133, 'jz.w', 128]], 'size': 265}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 204], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 204], [208, 'jmp', -2]], 'size': 210}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 4], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [6, 'jmp', -6]], 'size': 8}),
  ('duplicate label', ([['label', 'a'], ['data', 4], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 4]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 5],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[5, 'jmp.w', 129], [18, 'jz.w', 128], [137, 'jmp.w', 128]], 'size': 269}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 5], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [133, 'jmp.w', -136]], 'size': 136}),
  ('forward branch just around the short limit',
   ([['data', 5], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[5, 'jmp', 127], [134, 'jz.w', 128]], 'size': 266}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 205], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 205], [209, 'jmp', -2]], 'size': 211}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 5], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [7, 'jmp', -7]], 'size': 9}),
  ('duplicate label', ([['label', 'a'], ['data', 5], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 5]],), ['undefined-label', 'nowhere'])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(prog):
    labels_seen = set()
    for ins in prog:
        if ins[0] == 'label':
            if ins[1] in labels_seen:
                return ['duplicate-label', ins[1]]
            labels_seen.add(ins[1])
    for ins in prog:
        if ins[0] in ('jmp', 'jz') and ins[1] not in labels_seen:
            return ['undefined-label', ins[1]]
    long_form = set()
    def size(i, ins):
        if i in long_form:
            return 4 if ins[0] == 'jz' else 3
        return 2
    rounds = 0
    while True:
        rounds += 1
        addr = 0
        addrs = []
        where = {}
        for i, ins in enumerate(prog):
            addrs.append(addr)
            if ins[0] == 'label':
                where[ins[1]] = addr
            elif ins[0] == 'data':
                addr += ins[1]
            else:
                addr += size(i, ins)
        changed = False
        for i, ins in enumerate(prog):
            if ins[0] in ('jmp', 'jz') and i not in long_form:
                off = where[ins[1]] - (addrs[i] + 2)
                if off < -128 or off > 128:
                    long_form.add(i)
                    changed = True
        if not changed or rounds > len(prog):
            break
    out = []
    for i, ins in enumerate(prog):
        if ins[0] in ('jmp', 'jz'):
            suffix = '.w' if i in long_form else ''
            out.append([addrs[i], ins[0] + suffix, where[ins[1]] - (addrs[i] + size(i, ins))])
    return {'branches': out, 'size': addr}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: three-step widening cascade',
   ([['data', 1],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[1, 'jmp.w', 129], [14, 'jz.w', 128], [133, 'jmp.w', 128]], 'size': 265}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 1], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [129, 'jmp.w', -132]], 'size': 132}),
  ('forward branch just around the short limit',
   ([['data', 1], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[1, 'jmp', 127], [130, 'jz.w', 128]], 'size': 262}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 201], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 201], [205, 'jmp', -2]], 'size': 207}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 1], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [3, 'jmp', -3]], 'size': 5}),
  ('duplicate label', ([['label', 'a'], ['data', 1], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 1]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 2],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[2, 'jmp.w', 129], [15, 'jz.w', 128], [134, 'jmp.w', 128]], 'size': 266}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 2], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [130, 'jmp.w', -133]], 'size': 133}),
  ('forward branch just around the short limit',
   ([['data', 2], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[2, 'jmp', 127], [131, 'jz.w', 128]], 'size': 263}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 202], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 202], [206, 'jmp', -2]], 'size': 208}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 2], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [4, 'jmp', -4]], 'size': 6}),
  ('duplicate label', ([['label', 'a'], ['data', 2], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 2]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 3],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[3, 'jmp.w', 129], [16, 'jz.w', 128], [135, 'jmp.w', 128]], 'size': 267}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 3], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [131, 'jmp.w', -134]], 'size': 134}),
  ('forward branch just around the short limit',
   ([['data', 3], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[3, 'jmp', 127], [132, 'jz.w', 128]], 'size': 264}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 203], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 203], [207, 'jmp', -2]], 'size': 209}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 3], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [5, 'jmp', -5]], 'size': 7}),
  ('duplicate label', ([['label', 'a'], ['data', 3], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 3]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 4],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[4, 'jmp.w', 129], [17, 'jz.w', 128], [136, 'jmp.w', 128]], 'size': 268}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 4], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [132, 'jmp.w', -135]], 'size': 135}),
  ('forward branch just around the short limit',
   ([['data', 4], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[4, 'jmp', 127], [133, 'jz.w', 128]], 'size': 265}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 204], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 204], [208, 'jmp', -2]], 'size': 210}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 4], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [6, 'jmp', -6]], 'size': 8}),
  ('duplicate label', ([['label', 'a'], ['data', 4], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 4]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 5],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[5, 'jmp.w', 129], [18, 'jz.w', 128], [137, 'jmp.w', 128]], 'size': 269}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 5], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [133, 'jmp.w', -136]], 'size': 136}),
  ('forward branch just around the short limit',
   ([['data', 5], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[5, 'jmp', 127], [134, 'jz.w', 128]], 'size': 266}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 205], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 205], [209, 'jmp', -2]], 'size': 211}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 5], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [7, 'jmp', -7]], 'size': 9}),
  ('duplicate label', ([['label', 'a'], ['data', 5], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 5]],), ['undefined-label', 'nowhere'])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Backward branches of exactly -128 bytes are widened, growing code for no reason.

Assemble ["label", name], ["data", nbytes], ["jmp", label], ["jz", label]. Branches start in the short form (2 bytes, signed 8-bit displacement from the end of the instruction, valid -128..127). A short branch whose displacement does not fit is widened (jmp.w 3 bytes, jz.w 4 bytes) and addresses are recomputed until no branch changes (branches never shrink). Report duplicate or undefined labels; otherwise return each branch as [address, mnemonic, displacement from its own end] and the total size.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(prog):
    labels_seen = set()
    for ins in prog:
        if ins[0] == 'label':
            if ins[1] in labels_seen:
                return ['duplicate-label', ins[1]]
            labels_seen.add(ins[1])
    for ins in prog:
        if ins[0] in ('jmp', 'jz') and ins[1] not in labels_seen:
            return ['undefined-label', ins[1]]
    long_form = set()
    def size(i, ins):
        if i in long_form:
            return 4 if ins[0] == 'jz' else 3
        return 2
    rounds = 0
    while True:
        rounds += 1
        addr = 0
        addrs = []
        where = {}
        for i, ins in enumerate(prog):
            addrs.append(addr)
            if ins[0] == 'label':
                where[ins[1]] = addr
            elif ins[0] == 'data':
                addr += ins[1]
            else:
                addr += size(i, ins)
        changed = False
        for i, ins in enumerate(prog):
            if ins[0] in ('jmp', 'jz') and i not in long_form:
                off = where[ins[1]] - (addrs[i] + 2)
                if off < -128 or off > 127:
                    long_form.add(i)
                    changed = True
        if not changed or rounds >= 1:
            break
    out = []
    for i, ins in enumerate(prog):
        if ins[0] in ('jmp', 'jz'):
            suffix = '.w' if i in long_form else ''
            out.append([addrs[i], ins[0] + suffix, where[ins[1]] - (addrs[i] + size(i, ins))])
    return {'branches': out, 'size': addr}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: three-step widening cascade',
   ([['data', 1],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[1, 'jmp.w', 129], [14, 'jz.w', 128], [133, 'jmp.w', 128]], 'size': 265}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 1], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [129, 'jmp.w', -132]], 'size': 132}),
  ('forward branch just around the short limit',
   ([['data', 1], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[1, 'jmp', 127], [130, 'jz.w', 128]], 'size': 262}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 201], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 201], [205, 'jmp', -2]], 'size': 207}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 1], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [3, 'jmp', -3]], 'size': 5}),
  ('duplicate label', ([['label', 'a'], ['data', 1], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 1]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 2],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[2, 'jmp.w', 129], [15, 'jz.w', 128], [134, 'jmp.w', 128]], 'size': 266}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 2], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [130, 'jmp.w', -133]], 'size': 133}),
  ('forward branch just around the short limit',
   ([['data', 2], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[2, 'jmp', 127], [131, 'jz.w', 128]], 'size': 263}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 202], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 202], [206, 'jmp', -2]], 'size': 208}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 2], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [4, 'jmp', -4]], 'size': 6}),
  ('duplicate label', ([['label', 'a'], ['data', 2], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 2]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 3],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[3, 'jmp.w', 129], [16, 'jz.w', 128], [135, 'jmp.w', 128]], 'size': 267}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 3], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [131, 'jmp.w', -134]], 'size': 134}),
  ('forward branch just around the short limit',
   ([['data', 3], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[3, 'jmp', 127], [132, 'jz.w', 128]], 'size': 264}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 203], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 203], [207, 'jmp', -2]], 'size': 209}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 3], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [5, 'jmp', -5]], 'size': 7}),
  ('duplicate label', ([['label', 'a'], ['data', 3], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 3]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 4],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[4, 'jmp.w', 129], [17, 'jz.w', 128], [136, 'jmp.w', 128]], 'size': 268}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 4], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [132, 'jmp.w', -135]], 'size': 135}),
  ('forward branch just around the short limit',
   ([['data', 4], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[4, 'jmp', 127], [133, 'jz.w', 128]], 'size': 265}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 204], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 204], [208, 'jmp', -2]], 'size': 210}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 4], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [6, 'jmp', -6]], 'size': 8}),
  ('duplicate label', ([['label', 'a'], ['data', 4], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 4]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 5],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[5, 'jmp.w', 129], [18, 'jz.w', 128], [137, 'jmp.w', 128]], 'size': 269}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 5], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [133, 'jmp.w', -136]], 'size': 136}),
  ('forward branch just around the short limit',
   ([['data', 5], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[5, 'jmp', 127], [134, 'jz.w', 128]], 'size': 266}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 205], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 205], [209, 'jmp', -2]], 'size': 211}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 5], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [7, 'jmp', -7]], 'size': 9}),
  ('duplicate label', ([['label', 'a'], ['data', 5], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 5]],), ['undefined-label', 'nowhere'])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(prog):
    labels_seen = set()
    for ins in prog:
        if ins[0] == 'label':
            if ins[1] in labels_seen:
                return ['duplicate-label', ins[1]]
            labels_seen.add(ins[1])
    for ins in prog:
        if ins[0] in ('jmp', 'jz') and ins[1] not in labels_seen:
            return ['undefined-label', ins[1]]
    long_form = set()
    def size(i, ins):
        if i in long_form:
            return 4 if ins[0] == 'jz' else 3
        return 2
    rounds = 0
    while True:
        rounds += 1
        addr = 0
        addrs = []
        where = {}
        for i, ins in enumerate(prog):
            addrs.append(addr)
            if ins[0] == 'label':
                where[ins[1]] = addr
            elif ins[0] == 'data':
                addr += ins[1]
            else:
                addr += size(i, ins)
        changed = False
        for i, ins in enumerate(prog):
            if ins[0] in ('jmp', 'jz') and i not in long_form:
                off = where[ins[1]] - (addrs[i] + 2)
                if off < -128 or off > 127:
                    long_form.add(i)
                    changed = True
        if not changed or rounds >= 2:
            break
    out = []
    for i, ins in enumerate(prog):
        if ins[0] in ('jmp', 'jz'):
            suffix = '.w' if i in long_form else ''
            out.append([addrs[i], ins[0] + suffix, where[ins[1]] - (addrs[i] + size(i, ins))])
    return {'branches': out, 'size': addr}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: three-step widening cascade',
   ([['data', 1],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[1, 'jmp.w', 129], [14, 'jz.w', 128], [133, 'jmp.w', 128]], 'size': 265}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 1], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [129, 'jmp.w', -132]], 'size': 132}),
  ('forward branch just around the short limit',
   ([['data', 1], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[1, 'jmp', 127], [130, 'jz.w', 128]], 'size': 262}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 201], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 201], [205, 'jmp', -2]], 'size': 207}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 1], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [3, 'jmp', -3]], 'size': 5}),
  ('duplicate label', ([['label', 'a'], ['data', 1], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 1]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 2],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[2, 'jmp.w', 129], [15, 'jz.w', 128], [134, 'jmp.w', 128]], 'size': 266}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 2], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [130, 'jmp.w', -133]], 'size': 133}),
  ('forward branch just around the short limit',
   ([['data', 2], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[2, 'jmp', 127], [131, 'jz.w', 128]], 'size': 263}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 202], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 202], [206, 'jmp', -2]], 'size': 208}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 2], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [4, 'jmp', -4]], 'size': 6}),
  ('duplicate label', ([['label', 'a'], ['data', 2], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 2]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 3],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[3, 'jmp.w', 129], [16, 'jz.w', 128], [135, 'jmp.w', 128]], 'size': 267}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 3], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [131, 'jmp.w', -134]], 'size': 134}),
  ('forward branch just around the short limit',
   ([['data', 3], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[3, 'jmp', 127], [132, 'jz.w', 128]], 'size': 264}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 203], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 203], [207, 'jmp', -2]], 'size': 209}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 3], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [5, 'jmp', -5]], 'size': 7}),
  ('duplicate label', ([['label', 'a'], ['data', 3], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 3]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 4],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[4, 'jmp.w', 129], [17, 'jz.w', 128], [136, 'jmp.w', 128]], 'size': 268}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 4], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [132, 'jmp.w', -135]], 'size': 135}),
  ('forward branch just around the short limit',
   ([['data', 4], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[4, 'jmp', 127], [133, 'jz.w', 128]], 'size': 265}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 204], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 204], [208, 'jmp', -2]], 'size': 210}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 4], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [6, 'jmp', -6]], 'size': 8}),
  ('duplicate label', ([['label', 'a'], ['data', 4], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 4]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 5],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[5, 'jmp.w', 129], [18, 'jz.w', 128], [137, 'jmp.w', 128]], 'size': 269}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 5], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [133, 'jmp.w', -136]], 'size': 136}),
  ('forward branch just around the short limit',
   ([['data', 5], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[5, 'jmp', 127], [134, 'jz.w', 128]], 'size': 266}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 205], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 205], [209, 'jmp', -2]], 'size': 211}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 5], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [7, 'jmp', -7]], 'size': 9}),
  ('duplicate label', ([['label', 'a'], ['data', 5], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 5]],), ['undefined-label', 'nowhere'])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A branch pushed out of range by another branch widening stays short and wraps.

Assemble ["label", name], ["data", nbytes], ["jmp", label], ["jz", label]. Branches start in the short form (2 bytes, signed 8-bit displacement from the end of the instruction, valid -128..127). A short branch whose displacement does not fit is widened (jmp.w 3 bytes, jz.w 4 bytes) and addresses are recomputed until no branch changes (branches never shrink). Report duplicate or undefined labels; otherwise return each branch as [address, mnemonic, displacement from its own end] and the total size.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(prog):
    labels_seen = set()
    for ins in prog:
        if ins[0] == 'label':
            if ins[1] in labels_seen:
                return ['duplicate-label', ins[1]]
            labels_seen.add(ins[1])
    for ins in prog:
        if ins[0] in ('jmp', 'jz') and ins[1] not in labels_seen:
            return ['undefined-label', ins[1]]
    long_form = set()
    def size(i, ins):
        if i in long_form:
            return 3
        return 2
    rounds = 0
    while True:
        rounds += 1
        addr = 0
        addrs = []
        where = {}
        for i, ins in enumerate(prog):
            addrs.append(addr)
            if ins[0] == 'label':
                where[ins[1]] = addr
            elif ins[0] == 'data':
                addr += ins[1]
            else:
                addr += size(i, ins)
        changed = False
        for i, ins in enumerate(prog):
            if ins[0] in ('jmp', 'jz') and i not in long_form:
                off = where[ins[1]] - (addrs[i] + 2)
                if off < -128 or off > 127:
                    long_form.add(i)
                    changed = True
        if not changed or rounds > len(prog):
            break
    out = []
    for i, ins in enumerate(prog):
        if ins[0] in ('jmp', 'jz'):
            suffix = '.w' if i in long_form else ''
            out.append([addrs[i], ins[0] + suffix, where[ins[1]] - (addrs[i] + size(i, ins))])
    return {'branches': out, 'size': addr}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: three-step widening cascade',
   ([['data', 1],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[1, 'jmp.w', 129], [14, 'jz.w', 128], [133, 'jmp.w', 128]], 'size': 265}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 1], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [129, 'jmp.w', -132]], 'size': 132}),
  ('forward branch just around the short limit',
   ([['data', 1], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[1, 'jmp', 127], [130, 'jz.w', 128]], 'size': 262}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 201], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 201], [205, 'jmp', -2]], 'size': 207}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 1], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [3, 'jmp', -3]], 'size': 5}),
  ('duplicate label', ([['label', 'a'], ['data', 1], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 1]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 2],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[2, 'jmp.w', 129], [15, 'jz.w', 128], [134, 'jmp.w', 128]], 'size': 266}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 2], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [130, 'jmp.w', -133]], 'size': 133}),
  ('forward branch just around the short limit',
   ([['data', 2], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[2, 'jmp', 127], [131, 'jz.w', 128]], 'size': 263}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 202], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 202], [206, 'jmp', -2]], 'size': 208}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 2], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [4, 'jmp', -4]], 'size': 6}),
  ('duplicate label', ([['label', 'a'], ['data', 2], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 2]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 3],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[3, 'jmp.w', 129], [16, 'jz.w', 128], [135, 'jmp.w', 128]], 'size': 267}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 3], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [131, 'jmp.w', -134]], 'size': 134}),
  ('forward branch just around the short limit',
   ([['data', 3], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[3, 'jmp', 127], [132, 'jz.w', 128]], 'size': 264}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 203], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 203], [207, 'jmp', -2]], 'size': 209}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 3], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [5, 'jmp', -5]], 'size': 7}),
  ('duplicate label', ([['label', 'a'], ['data', 3], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 3]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 4],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[4, 'jmp.w', 129], [17, 'jz.w', 128], [136, 'jmp.w', 128]], 'size': 268}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 4], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [132, 'jmp.w', -135]], 'size': 135}),
  ('forward branch just around the short limit',
   ([['data', 4], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[4, 'jmp', 127], [133, 'jz.w', 128]], 'size': 265}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 204], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 204], [208, 'jmp', -2]], 'size': 210}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 4], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [6, 'jmp', -6]], 'size': 8}),
  ('duplicate label', ([['label', 'a'], ['data', 4], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 4]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 5],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[5, 'jmp.w', 129], [18, 'jz.w', 128], [137, 'jmp.w', 128]], 'size': 269}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 5], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [133, 'jmp.w', -136]], 'size': 136}),
  ('forward branch just around the short limit',
   ([['data', 5], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[5, 'jmp', 127], [134, 'jz.w', 128]], 'size': 266}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 205], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 205], [209, 'jmp', -2]], 'size': 211}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 5], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [7, 'jmp', -7]], 'size': 9}),
  ('duplicate label', ([['label', 'a'], ['data', 5], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 5]],), ['undefined-label', 'nowhere'])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(prog):
    labels_seen = set()
    for ins in prog:
        if ins[0] == 'label':
            if ins[1] in labels_seen:
                return ['duplicate-label', ins[1]]
            labels_seen.add(ins[1])
    for ins in prog:
        if ins[0] in ('jmp', 'jz') and ins[1] not in labels_seen:
            return ['undefined-label', ins[1]]
    long_form = set()
    def size(i, ins):
        if i in long_form:
            return 4
        return 2
    rounds = 0
    while True:
        rounds += 1
        addr = 0
        addrs = []
        where = {}
        for i, ins in enumerate(prog):
            addrs.append(addr)
            if ins[0] == 'label':
                where[ins[1]] = addr
            elif ins[0] == 'data':
                addr += ins[1]
            else:
                addr += size(i, ins)
        changed = False
        for i, ins in enumerate(prog):
            if ins[0] in ('jmp', 'jz') and i not in long_form:
                off = where[ins[1]] - (addrs[i] + 2)
                if off < -128 or off > 127:
                    long_form.add(i)
                    changed = True
        if not changed or rounds > len(prog):
            break
    out = []
    for i, ins in enumerate(prog):
        if ins[0] in ('jmp', 'jz'):
            suffix = '.w' if i in long_form else ''
            out.append([addrs[i], ins[0] + suffix, where[ins[1]] - (addrs[i] + size(i, ins))])
    return {'branches': out, 'size': addr}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: three-step widening cascade',
   ([['data', 1],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[1, 'jmp.w', 129], [14, 'jz.w', 128], [133, 'jmp.w', 128]], 'size': 265}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 1], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [129, 'jmp.w', -132]], 'size': 132}),
  ('forward branch just around the short limit',
   ([['data', 1], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[1, 'jmp', 127], [130, 'jz.w', 128]], 'size': 262}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 201], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 201], [205, 'jmp', -2]], 'size': 207}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 1], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [3, 'jmp', -3]], 'size': 5}),
  ('duplicate label', ([['label', 'a'], ['data', 1], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 1]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 2],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[2, 'jmp.w', 129], [15, 'jz.w', 128], [134, 'jmp.w', 128]], 'size': 266}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 2], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [130, 'jmp.w', -133]], 'size': 133}),
  ('forward branch just around the short limit',
   ([['data', 2], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[2, 'jmp', 127], [131, 'jz.w', 128]], 'size': 263}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 202], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 202], [206, 'jmp', -2]], 'size': 208}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 2], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [4, 'jmp', -4]], 'size': 6}),
  ('duplicate label', ([['label', 'a'], ['data', 2], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 2]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 3],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[3, 'jmp.w', 129], [16, 'jz.w', 128], [135, 'jmp.w', 128]], 'size': 267}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 3], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [131, 'jmp.w', -134]], 'size': 134}),
  ('forward branch just around the short limit',
   ([['data', 3], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[3, 'jmp', 127], [132, 'jz.w', 128]], 'size': 264}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 203], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 203], [207, 'jmp', -2]], 'size': 209}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 3], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [5, 'jmp', -5]], 'size': 7}),
  ('duplicate label', ([['label', 'a'], ['data', 3], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 3]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 4],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[4, 'jmp.w', 129], [17, 'jz.w', 128], [136, 'jmp.w', 128]], 'size': 268}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 4], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [132, 'jmp.w', -135]], 'size': 135}),
  ('forward branch just around the short limit',
   ([['data', 4], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[4, 'jmp', 127], [133, 'jz.w', 128]], 'size': 265}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 204], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 204], [208, 'jmp', -2]], 'size': 210}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 4], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [6, 'jmp', -6]], 'size': 8}),
  ('duplicate label', ([['label', 'a'], ['data', 4], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 4]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 5],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[5, 'jmp.w', 129], [18, 'jz.w', 128], [137, 'jmp.w', 128]], 'size': 269}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 5], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [133, 'jmp.w', -136]], 'size': 136}),
  ('forward branch just around the short limit',
   ([['data', 5], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[5, 'jmp', 127], [134, 'jz.w', 128]], 'size': 266}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 205], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 205], [209, 'jmp', -2]], 'size': 211}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 5], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [7, 'jmp', -7]], 'size': 9}),
  ('duplicate label', ([['label', 'a'], ['data', 5], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 5]],), ['undefined-label', 'nowhere'])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Code after a widened jz is laid out one byte too early.

Assemble ["label", name], ["data", nbytes], ["jmp", label], ["jz", label]. Branches start in the short form (2 bytes, signed 8-bit displacement from the end of the instruction, valid -128..127). A short branch whose displacement does not fit is widened (jmp.w 3 bytes, jz.w 4 bytes) and addresses are recomputed until no branch changes (branches never shrink). Report duplicate or undefined labels; otherwise return each branch as [address, mnemonic, displacement from its own end] and the total size.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(data, kinds):
    pos = 0
    out = []
    for kind in kinds:
        bits = 32 if kind in ('u32', 's32') else 64
        maxlen = (bits + 6) // 7
        result = 0
        shift = 0
        n = 0
        while True:
            if pos >= len(data):
                return out + [['unexpected-end', pos]]
            byte = data[pos]
            pos += 1
            n += 1
            result |= (byte & 0x7F) << shift
            shift += 7
            if byte & 0x80:
                break
            if n == maxlen:
                return out + [['too-long', pos]]
        if kind[0] == 's' and byte & 0x40:
            result -= 1 << shift
        if kind == 's32' and not -(1 << 31) <= result < (1 << 31):
            return out + [['out-of-range', pos]]
        if kind == 'u32' and result >= (1 << 32):
            return out + [['out-of-range', pos]]
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: mixed immediates', ([237, 7, 179, 127, 127], ['u32', 's32', 's64']), [1005, -77, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 1], ['u32']), [268435456]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 1], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([65, 128, 64], ['u32', 'u32']), [65, 8192]),
  ('negative s64 spanning several bytes', ([255, 255, 255, 255, 255, 95], ['s64']), [-1099511627777]),
  ('truncated immediate', ([128, 129], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([213, 15, 230, 126, 127], ['u32', 's32', 's64']), [2005, -154, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 2], ['u32']), [536870912]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 2], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([66, 128, 64], ['u32', 'u32']), [66, 8192]),
  ('negative s64 spanning several bytes', ([254, 255, 255, 255, 255, 95], ['s64']), [-1099511627778]),
  ('truncated immediate', ([128, 130], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([189, 23, 153, 126, 127], ['u32', 's32', 's64']), [3005, -231, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 3], ['u32']), [805306368]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 3], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([67, 128, 64], ['u32', 'u32']), [67, 8192]),
  ('negative s64 spanning several bytes', ([253, 255, 255, 255, 255, 95], ['s64']), [-1099511627779]),
  ('truncated immediate', ([128, 131], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([165, 31, 204, 125, 127], ['u32', 's32', 's64']), [4005, -308, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 4], ['u32']), [1073741824]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 4], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([68, 128, 64], ['u32', 'u32']), [68, 8192]),
  ('negative s64 spanning several bytes', ([252, 255, 255, 255, 255, 95], ['s64']), [-1099511627780]),
  ('truncated immediate', ([128, 132], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([141, 39, 255, 124, 127], ['u32', 's32', 's64']), [5005, -385, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 5], ['u32']), [1342177280]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 5], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([69, 128, 64], ['u32', 'u32']), [69, 8192]),
  ('negative s64 spanning several bytes', ([251, 255, 255, 255, 255, 95], ['s64']), [-1099511627781]),
  ('truncated immediate', ([128, 133], ['u64']), [['unexpected-end', 2]])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(data, kinds):
    pos = 0
    out = []
    for kind in kinds:
        bits = 32 if kind in ('u32', 's32') else 64
        maxlen = (bits + 6) // 7
        result = 0
        shift = 0
        n = 0
        while True:
            if pos >= len(data):
                return out + [['unexpected-end', pos]]
            byte = data[pos]
            pos += 1
            n += 1
            result |= (byte & 0x7F) << shift
            shift += 7
            if byte < 0x7F:
                break
            if n == maxlen:
                return out + [['too-long', pos]]
        if kind[0] == 's' and byte & 0x40:
            result -= 1 << shift
        if kind == 's32' and not -(1 << 31) <= result < (1 << 31):
            return out + [['out-of-range', pos]]
        if kind == 'u32' and result >= (1 << 32):
            return out + [['out-of-range', pos]]
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: mixed immediates', ([237, 7, 179, 127, 127], ['u32', 's32', 's64']), [1005, -77, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 1], ['u32']), [268435456]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 1], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([65, 128, 64], ['u32', 'u32']), [65, 8192]),
  ('negative s64 spanning several bytes', ([255, 255, 255, 255, 255, 95], ['s64']), [-1099511627777]),
  ('truncated immediate', ([128, 129], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([213, 15, 230, 126, 127], ['u32', 's32', 's64']), [2005, -154, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 2], ['u32']), [536870912]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 2], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([66, 128, 64], ['u32', 'u32']), [66, 8192]),
  ('negative s64 spanning several bytes', ([254, 255, 255, 255, 255, 95], ['s64']), [-1099511627778]),
  ('truncated immediate', ([128, 130], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([189, 23, 153, 126, 127], ['u32', 's32', 's64']), [3005, -231, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 3], ['u32']), [805306368]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 3], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([67, 128, 64], ['u32', 'u32']), [67, 8192]),
  ('negative s64 spanning several bytes', ([253, 255, 255, 255, 255, 95], ['s64']), [-1099511627779]),
  ('truncated immediate', ([128, 131], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([165, 31, 204, 125, 127], ['u32', 's32', 's64']), [4005, -308, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 4], ['u32']), [1073741824]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 4], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([68, 128, 64], ['u32', 'u32']), [68, 8192]),
  ('negative s64 spanning several bytes', ([252, 255, 255, 255, 255, 95], ['s64']), [-1099511627780]),
  ('truncated immediate', ([128, 132], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([141, 39, 255, 124, 127], ['u32', 's32', 's64']), [5005, -385, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 5], ['u32']), [1342177280]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 5], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([69, 128, 64], ['u32', 'u32']), [69, 8192]),
  ('negative s64 spanning several bytes', ([251, 255, 255, 255, 255, 95], ['s64']), [-1099511627781]),
  ('truncated immediate', ([128, 133], ['u64']), [['unexpected-end', 2]])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Multi-byte immediates are cut after their first byte and single-byte ones swallow the next operand.

Decode consecutive LEB128 immediates of kinds u32, s32, u64, s64 from a byte list: 7 payload bits per byte, least significant group first, bit 0x80 means another byte follows; at most ceil(bits/7) bytes (5 for 32-bit, 10 for 64-bit). Signed kinds sign-extend from bit 0x40 of the final byte; u32/s32 values outside their range are errors. Return decoded values followed by the first error, if any: ["unexpected-end", pos], ["too-long", pos] or ["out-of-range", pos].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(data, kinds):
    pos = 0
    out = []
    for kind in kinds:
        bits = 32 if kind in ('u32', 's32') else 64
        maxlen = (bits + 6) // 7
        result = 0
        shift = 0
        n = 0
        while True:
            if pos >= len(data):
                return out + [['unexpected-end', pos]]
            byte = data[pos]
            pos += 1
            n += 1
            result |= (byte & 0x7F) << shift
            shift += 7
            if (byte & 0x80) == 0:
                break
            if n == maxlen:
                return out + [['too-long', pos]]
        if kind[0] == 's' and byte & 0x80:
            result -= 1 << shift
        if kind == 's32' and not -(1 << 31) <= result < (1 << 31):
            return out + [['out-of-range', pos]]
        if kind == 'u32' and result >= (1 << 32):
            return out + [['out-of-range', pos]]
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: mixed immediates', ([237, 7, 179, 127, 127], ['u32', 's32', 's64']), [1005, -77, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 1], ['u32']), [268435456]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 1], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([65, 128, 64], ['u32', 'u32']), [65, 8192]),
  ('negative s64 spanning several bytes', ([255, 255, 255, 255, 255, 95], ['s64']), [-1099511627777]),
  ('truncated immediate', ([128, 129], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([213, 15, 230, 126, 127], ['u32', 's32', 's64']), [2005, -154, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 2], ['u32']), [536870912]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 2], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([66, 128, 64], ['u32', 'u32']), [66, 8192]),
  ('negative s64 spanning several bytes', ([254, 255, 255, 255, 255, 95], ['s64']), [-1099511627778]),
  ('truncated immediate', ([128, 130], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([189, 23, 153, 126, 127], ['u32', 's32', 's64']), [3005, -231, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 3], ['u32']), [805306368]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 3], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([67, 128, 64], ['u32', 'u32']), [67, 8192]),
  ('negative s64 spanning several bytes', ([253, 255, 255, 255, 255, 95], ['s64']), [-1099511627779]),
  ('truncated immediate', ([128, 131], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([165, 31, 204, 125, 127], ['u32', 's32', 's64']), [4005, -308, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 4], ['u32']), [1073741824]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 4], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([68, 128, 64], ['u32', 'u32']), [68, 8192]),
  ('negative s64 spanning several bytes', ([252, 255, 255, 255, 255, 95], ['s64']), [-1099511627780]),
  ('truncated immediate', ([128, 132], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([141, 39, 255, 124, 127], ['u32', 's32', 's64']), [5005, -385, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 5], ['u32']), [1342177280]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 5], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([69, 128, 64], ['u32', 'u32']), [69, 8192]),
  ('negative s64 spanning several bytes', ([251, 255, 255, 255, 255, 95], ['s64']), [-1099511627781]),
  ('truncated immediate', ([128, 133], ['u64']), [['unexpected-end', 2]])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(data, kinds):
    pos = 0
    out = []
    for kind in kinds:
        bits = 32 if kind in ('u32', 's32') else 64
        maxlen = (bits + 6) // 7
        result = 0
        shift = 0
        n = 0
        while True:
            if pos >= len(data):
                return out + [['unexpected-end', pos]]
            byte = data[pos]
            pos += 1
            n += 1
            result |= (byte & 0x7F) << shift
            shift += 7
            if (byte & 0x80) == 0:
                break
            if n == maxlen:
                return out + [['too-long', pos]]
        if kind[0] == 's' and result & (1 << (bits - 1)):
            result -= 1 << shift
        if kind == 's32' and not -(1 << 31) <= result < (1 << 31):
            return out + [['out-of-range', pos]]
        if kind == 'u32' and result >= (1 << 32):
            return out + [['out-of-range', pos]]
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: mixed immediates', ([237, 7, 179, 127, 127], ['u32', 's32', 's64']), [1005, -77, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 1], ['u32']), [268435456]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 1], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([65, 128, 64], ['u32', 'u32']), [65, 8192]),
  ('negative s64 spanning several bytes', ([255, 255, 255, 255, 255, 95], ['s64']), [-1099511627777]),
  ('truncated immediate', ([128, 129], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([213, 15, 230, 126, 127], ['u32', 's32', 's64']), [2005, -154, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 2], ['u32']), [536870912]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 2], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([66, 128, 64], ['u32', 'u32']), [66, 8192]),
  ('negative s64 spanning several bytes', ([254, 255, 255, 255, 255, 95], ['s64']), [-1099511627778]),
  ('truncated immediate', ([128, 130], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([189, 23, 153, 126, 127], ['u32', 's32', 's64']), [3005, -231, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 3], ['u32']), [805306368]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 3], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([67, 128, 64], ['u32', 'u32']), [67, 8192]),
  ('negative s64 spanning several bytes', ([253, 255, 255, 255, 255, 95], ['s64']), [-1099511627779]),
  ('truncated immediate', ([128, 131], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([165, 31, 204, 125, 127], ['u32', 's32', 's64']), [4005, -308, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 4], ['u32']), [1073741824]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 4], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([68, 128, 64], ['u32', 'u32']), [68, 8192]),
  ('negative s64 spanning several bytes', ([252, 255, 255, 255, 255, 95], ['s64']), [-1099511627780]),
  ('truncated immediate', ([128, 132], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([141, 39, 255, 124, 127], ['u32', 's32', 's64']), [5005, -385, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 5], ['u32']), [1342177280]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 5], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([69, 128, 64], ['u32', 'u32']), [69, 8192]),
  ('negative s64 spanning several bytes', ([251, 255, 255, 255, 255, 95], ['s64']), [-1099511627781]),
  ('truncated immediate', ([128, 133], ['u64']), [['unexpected-end', 2]])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Negative signed immediates decode as large positive numbers.

Decode consecutive LEB128 immediates of kinds u32, s32, u64, s64 from a byte list: 7 payload bits per byte, least significant group first, bit 0x80 means another byte follows; at most ceil(bits/7) bytes (5 for 32-bit, 10 for 64-bit). Signed kinds sign-extend from bit 0x40 of the final byte; u32/s32 values outside their range are errors. Return decoded values followed by the first error, if any: ["unexpected-end", pos], ["too-long", pos] or ["out-of-range", pos].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(data, kinds):
    pos = 0
    out = []
    for kind in kinds:
        bits = 32 if kind in ('u32', 's32') else 64
        maxlen = bits // 7
        result = 0
        shift = 0
        n = 0
        while True:
            if pos >= len(data):
                return out + [['unexpected-end', pos]]
            byte = data[pos]
            pos += 1
            n += 1
            result |= (byte & 0x7F) << shift
            shift += 7
            if (byte & 0x80) == 0:
                break
            if n == maxlen:
                return out + [['too-long', pos]]
        if kind[0] == 's' and byte & 0x40:
            result -= 1 << shift
        if kind == 's32' and not -(1 << 31) <= result < (1 << 31):
            return out + [['out-of-range', pos]]
        if kind == 'u32' and result >= (1 << 32):
            return out + [['out-of-range', pos]]
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: mixed immediates', ([237, 7, 179, 127, 127], ['u32', 's32', 's64']), [1005, -77, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 1], ['u32']), [268435456]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 1], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([65, 128, 64], ['u32', 'u32']), [65, 8192]),
  ('negative s64 spanning several bytes', ([255, 255, 255, 255, 255, 95], ['s64']), [-1099511627777]),
  ('truncated immediate', ([128, 129], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([213, 15, 230, 126, 127], ['u32', 's32', 's64']), [2005, -154, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 2], ['u32']), [536870912]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 2], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([66, 128, 64], ['u32', 'u32']), [66, 8192]),
  ('negative s64 spanning several bytes', ([254, 255, 255, 255, 255, 95], ['s64']), [-1099511627778]),
  ('truncated immediate', ([128, 130], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([189, 23, 153, 126, 127], ['u32', 's32', 's64']), [3005, -231, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 3], ['u32']), [805306368]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 3], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([67, 128, 64], ['u32', 'u32']), [67, 8192]),
  ('negative s64 spanning several bytes', ([253, 255, 255, 255, 255, 95], ['s64']), [-1099511627779]),
  ('truncated immediate', ([128, 131], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([165, 31, 204, 125, 127], ['u32', 's32', 's64']), [4005, -308, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 4], ['u32']), [1073741824]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 4], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([68, 128, 64], ['u32', 'u32']), [68, 8192]),
  ('negative s64 spanning several bytes', ([252, 255, 255, 255, 255, 95], ['s64']), [-1099511627780]),
  ('truncated immediate', ([128, 132], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([141, 39, 255, 124, 127], ['u32', 's32', 's64']), [5005, -385, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 5], ['u32']), [1342177280]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 5], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([69, 128, 64], ['u32', 'u32']), [69, 8192]),
  ('negative s64 spanning several bytes', ([251, 255, 255, 255, 255, 95], ['s64']), [-1099511627781]),
  ('truncated immediate', ([128, 133], ['u64']), [['unexpected-end', 2]])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(data, kinds):
    pos = 0
    out = []
    for kind in kinds:
        bits = 32 if kind in ('u32', 's32') else 64
        maxlen = (bits + 6) // 7 + 1
        result = 0
        shift = 0
        n = 0
        while True:
            if pos >= len(data):
                return out + [['unexpected-end', pos]]
            byte = data[pos]
            pos += 1
            n += 1
            result |= (byte & 0x7F) << shift
            shift += 7
            if (byte & 0x80) == 0:
                break
            if n == maxlen:
                return out + [['too-long', pos]]
        if kind[0] == 's' and byte & 0x40:
            result -= 1 << shift
        if kind == 's32' and not -(1 << 31) <= result < (1 << 31):
            return out + [['out-of-range', pos]]
        if kind == 'u32' and result >= (1 << 32):
            return out + [['out-of-range', pos]]
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: mixed immediates', ([237, 7, 179, 127, 127], ['u32', 's32', 's64']), [1005, -77, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 1], ['u32']), [268435456]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 1], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([65, 128, 64], ['u32', 'u32']), [65, 8192]),
  ('negative s64 spanning several bytes', ([255, 255, 255, 255, 255, 95], ['s64']), [-1099511627777]),
  ('truncated immediate', ([128, 129], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([213, 15, 230, 126, 127], ['u32', 's32', 's64']), [2005, -154, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 2], ['u32']), [536870912]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 2], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([66, 128, 64], ['u32', 'u32']), [66, 8192]),
  ('negative s64 spanning several bytes', ([254, 255, 255, 255, 255, 95], ['s64']), [-1099511627778]),
  ('truncated immediate', ([128, 130], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([189, 23, 153, 126, 127], ['u32', 's32', 's64']), [3005, -231, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 3], ['u32']), [805306368]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 3], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([67, 128, 64], ['u32', 'u32']), [67, 8192]),
  ('negative s64 spanning several bytes', ([253, 255, 255, 255, 255, 95], ['s64']), [-1099511627779]),
  ('truncated immediate', ([128, 131], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([165, 31, 204, 125, 127], ['u32', 's32', 's64']), [4005, -308, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 4], ['u32']), [1073741824]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 4], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([68, 128, 64], ['u32', 'u32']), [68, 8192]),
  ('negative s64 spanning several bytes', ([252, 255, 255, 255, 255, 95], ['s64']), [-1099511627780]),
  ('truncated immediate', ([128, 132], ['u64']), [['unexpected-end', 2]])],
 [('regression: mixed immediates', ([141, 39, 255, 124, 127], ['u32', 's32', 's64']), [5005, -385, -1]),
  ('single byte 0x7F', ([127, 127], ['u32', 's32']), [127, -1]),
  ('five-byte u32 is the maximum length', ([128, 128, 128, 128, 5], ['u32']), [1342177280]),
  ('six-byte u32 is too long', ([128, 128, 128, 128, 128, 0, 5], ['u32', 'u32']), [['too-long', 5]]),
  ('s32 2^31 out of range, -2^31 in range',
   ([128, 128, 128, 128, 120, 128, 128, 128, 128, 8], ['s32', 's32']),
   [-2147483648, ['out-of-range', 10]]),
  ('u32 with bit 6 set in last byte stays positive', ([69, 128, 64], ['u32', 'u32']), [69, 8192]),
  ('negative s64 spanning several bytes', ([251, 255, 255, 255, 255, 95], ['s64']), [-1099511627781]),
  ('truncated immediate', ([128, 133], ['u64']), [['unexpected-end', 2]])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Legal five-byte u32 immediates are rejected as too long.

Decode consecutive LEB128 immediates of kinds u32, s32, u64, s64 from a byte list: 7 payload bits per byte, least significant group first, bit 0x80 means another byte follows; at most ceil(bits/7) bytes (5 for 32-bit, 10 for 64-bit). Signed kinds sign-extend from bit 0x40 of the final byte; u32/s32 values outside their range are errors. Return decoded values followed by the first error, if any: ["unexpected-end", pos], ["too-long", pos] or ["out-of-range", pos].

split: open-access