With top-up attempts from batch 26 commit 7f99538 added, lwIP UDP route and validation passed 1 of 3 valid attempts and NuttX floating format layout 0 of 3; Qwen ran out of context in most failures. Each comes from the measured commit, its digest matches, and it is still present at the batch branch head. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
248 lines
10 KiB
Python
248 lines
10 KiB
Python
"""Behavioral observations through the public C formatting entry points."""
|
|
from decimal import Decimal, ROUND_HALF_EVEN, localcontext
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import random
|
|
import subprocess
|
|
import sys
|
|
import tarfile
|
|
import tempfile
|
|
|
|
HARNESS = r'''
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
|
|
static int via_va(char *out, size_t cap, const char *fmt, ...)
|
|
{
|
|
int rc;
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
rc = vsnprintf(out, cap, fmt, ap);
|
|
va_end(ap);
|
|
return rc;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
char line[512];
|
|
while (fgets(line, sizeof(line), stdin))
|
|
{
|
|
char *api = strtok(line, "\t");
|
|
int kind = atoi(strtok(NULL, "\t"));
|
|
int cap = atoi(strtok(NULL, "\t"));
|
|
int width = atoi(strtok(NULL, "\t"));
|
|
int precision = atoi(strtok(NULL, "\t"));
|
|
double value = strtod(strtok(NULL, "\t"), NULL);
|
|
char *fmt = strtok(NULL, "\n");
|
|
unsigned char out[192];
|
|
int rc;
|
|
memset(out, 0xa5, sizeof(out));
|
|
char *dest = cap == 0 ? NULL : (char *)out;
|
|
if (api[0] == 'S')
|
|
{
|
|
if (kind == 1)
|
|
rc = snprintf(dest, cap, fmt, width, precision, value);
|
|
else if (kind == 2)
|
|
rc = snprintf(dest, cap, fmt, value, value);
|
|
else
|
|
rc = snprintf(dest, cap, fmt, value);
|
|
}
|
|
else
|
|
{
|
|
if (kind == 1)
|
|
rc = via_va(dest, cap, fmt, width, precision, value);
|
|
else if (kind == 2)
|
|
rc = via_va(dest, cap, fmt, value, value);
|
|
else
|
|
rc = via_va(dest, cap, fmt, value);
|
|
}
|
|
printf("%d\t", rc);
|
|
for (unsigned i = 0; i < sizeof(out); i++)
|
|
printf("%02x", (unsigned)out[i]);
|
|
putchar('\n');
|
|
}
|
|
return 0;
|
|
}
|
|
'''
|
|
|
|
SOURCES = [
|
|
'libs/libc/stdio/lib_libvsprintf.c', 'libs/libc/stdio/lib_dtoa_engine.c',
|
|
'libs/libc/stdio/lib_dtoa_data.c', 'libs/libc/stdio/lib_ultoa_invert.c',
|
|
'libs/libc/stdio/lib_snprintf.c', 'libs/libc/stdio/lib_vsnprintf.c',
|
|
'libs/libc/stream/lib_memoutstream.c', 'libs/libc/stream/lib_nulloutstream.c',
|
|
'libs/libc/stream/lib_libnoflush.c',
|
|
]
|
|
|
|
|
|
def conversion(value, letter, precision, flags, width):
|
|
"""Apply the documented decimal representation and field rules."""
|
|
with localcontext() as ctx:
|
|
ctx.prec = 100
|
|
d = Decimal.from_float(value)
|
|
sign = '-' if d.is_signed() else ('+' if '+' in flags else (' ' if ' ' in flags else ''))
|
|
d = abs(d)
|
|
p = precision
|
|
if letter in 'fF':
|
|
number = format(d.quantize(Decimal(1).scaleb(-p), rounding=ROUND_HALF_EVEN), 'f')
|
|
else:
|
|
exponent = d.adjusted() if d else 0
|
|
mantissa = d.scaleb(-exponent).quantize(Decimal(1).scaleb(-p), rounding=ROUND_HALF_EVEN)
|
|
if mantissa >= 10:
|
|
mantissa /= 10
|
|
mantissa = mantissa.quantize(Decimal(1).scaleb(-p))
|
|
exponent += 1
|
|
number = format(mantissa, 'f')
|
|
if p == 0 and '#' in flags:
|
|
number += '.'
|
|
number += letter + ('-' if exponent < 0 else '+') + str(abs(exponent)).zfill(2)
|
|
if letter in 'fF' and p == 0 and '#' in flags:
|
|
number += '.'
|
|
text = sign + number
|
|
padding = max(abs(width) - len(text), 0)
|
|
if '-' in flags or width < 0:
|
|
return text + ' ' * padding
|
|
if '0' in flags:
|
|
return sign + '0' * padding + number
|
|
return ' ' * padding + text
|
|
|
|
|
|
def cases():
|
|
rng = random.Random(745210)
|
|
result = []
|
|
|
|
def add(value, letter, precision, flags='', width=0, kind=0):
|
|
token = '%'+flags
|
|
if kind == 1:
|
|
token += '*.*'+letter
|
|
elif precision is None:
|
|
token += (str(width) if width else '') + letter
|
|
else:
|
|
token += (str(width) if width else '') + '.'+str(precision)+letter
|
|
tag = 'row'+str(rng.randrange(100000, 1000000))+'|'
|
|
fmt = tag + token + '|done'
|
|
p = 6 if precision is None else precision
|
|
expected = tag + conversion(value, letter, p, flags, width) + '|done'
|
|
result.append((kind, width, p, value, fmt, expected))
|
|
|
|
# Exact integers exercise alternate form, signs, digit boundaries, and widths.
|
|
integers = [0.0, -0.0, 1., -1., 3., 8., 9., 10., 16., 99., 128., 999., 1000.,
|
|
-128., 65536., -65536., 10000000000., 1000000000000.]
|
|
for value in integers:
|
|
for letter in ('f', 'F'):
|
|
for flags, width in [('', 0), ('#', 0), ('+#', 14), (' #', 14), ('0#', 18),
|
|
('-#', 18), ('-0+#', 18), ('+ #', 3)]:
|
|
add(value, letter, 0, flags, width)
|
|
for value in [.25, -.25, .75, -.75, 1.25, -1.25, 12.25, -12.25]:
|
|
for letter in ('f', 'F'):
|
|
for flags, width in [('#', 0), ('+#0', 18), ('-#', 18)]:
|
|
add(value, letter, 0, flags, width)
|
|
# Finite exact binary fractions, enough decimal places to avoid tie rounding.
|
|
for value in [0., -0., .125, -.125, .015625, -.015625, .001953125, 12.25, -128.5, 1024.]:
|
|
for letter in ('f', 'F'):
|
|
for p in (6, 9, 12):
|
|
for flags, width in [('', 0), ('#', 24), ('+0', 24), ('-', 24)]:
|
|
add(value, letter, p, flags, width)
|
|
for value in integers + [.125, -.125, .015625, -.015625, .001953125, 2.**-29, 2.**-32]:
|
|
for letter in ('e', 'E'):
|
|
for p in (6, 9, 12):
|
|
for flags, width in [('', 0), ('#', 28), ('+0', 28), ('- ', 28), ('+ ', 2)]:
|
|
add(value, letter, p, flags, width)
|
|
# Zero precision scientific forms do not require rounding here.
|
|
for value in [0., -0., 1., -1., 10., 100., 1000., 1e9, 1e10, 1e12]:
|
|
for letter in ('e', 'E'):
|
|
for flags, width in [('', 0), ('#', 16), ('+#0', 16), ('-#', 16)]:
|
|
add(value, letter, 0, flags, width)
|
|
for value in [0., -0., 16., -128., .125, .015625]:
|
|
for letter in ('f', 'F', 'e', 'E'):
|
|
for width in (-28, 0, 28):
|
|
for flags in ('#', '+0', '-#'):
|
|
add(value, letter, 6, flags, width, 1)
|
|
for value in [0., -0., 16., -128., 1024.]:
|
|
for letter in ('f', 'F', 'e', 'E'):
|
|
for p in [None, 1, 2, 3, 4, 5, 7, 8, 10, 11]:
|
|
add(value, letter, p, '+#0', 28)
|
|
for letter in ('f', 'F', 'e', 'E'):
|
|
for flags in ('#', '+#0', '-0#'):
|
|
for p in (0, 1, 6, 12):
|
|
add(-128., letter, p, flags, 48, 1)
|
|
for value in [0., -0., 3., 128., -65536., 1e10]:
|
|
prefix = 'pair'+str(rng.randrange(100000, 1000000))+'|'
|
|
fmt = prefix + '%+#12.0f/%-#24.6E|tail'
|
|
expected = prefix + conversion(value, 'f', 0, '+#', 12) + '/' + conversion(value, 'E', 6, '-#', 24) + '|tail'
|
|
result.append((2, 0, 0, value, fmt, expected))
|
|
observations = []
|
|
for index, (kind, width, precision, value, fmt, expected) in enumerate(result):
|
|
n = len(expected)
|
|
caps = sorted({0, 1, 2, 7, 10, 11, 12, 13, n//2, n-7, n-6, n-5,
|
|
n-1, n, n+1, n+2, 192})
|
|
# Both public entry points see full output; short buffers rotate between them.
|
|
for cap in caps:
|
|
api = 'S' if (index+cap) % 2 else 'V'
|
|
raw = bytearray([0xa5]*192)
|
|
if cap:
|
|
prefix = expected.encode()[:cap-1]
|
|
raw[:len(prefix)] = prefix
|
|
raw[len(prefix)] = 0
|
|
request = '\t'.join([api, str(kind), str(cap), str(width), str(precision), value.hex(), fmt])
|
|
observations.append((request, str(n)+'\t'+raw.hex(), index, api, cap))
|
|
api = 'V' if (index+192) % 2 else 'S'
|
|
raw = expected.encode() + b'\0' + bytes([0xa5])*(191-n)
|
|
observations.append(('\t'.join([api, str(kind), '192', str(width), str(precision), value.hex(), fmt]),
|
|
str(n)+'\t'+raw.hex(), index, api, 192))
|
|
return observations
|
|
|
|
|
|
def main():
|
|
candidate = Path(sys.argv[1])
|
|
support = Path(__file__).with_name('support.tar').read_bytes()
|
|
rows = cases()
|
|
payload = '\n'.join(row[0] for row in rows) + '\n'
|
|
expected = [row[1] for row in rows]
|
|
scratch = Path(tempfile.mkdtemp(prefix='nuttx-format-'))
|
|
with tarfile.open(fileobj=io.BytesIO(support), mode='r:') as archive:
|
|
for member in archive.getmembers():
|
|
if not member.isfile() or Path(member.name).is_absolute() or '..' in Path(member.name).parts:
|
|
raise ValueError('invalid private support archive')
|
|
path = scratch / member.name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(archive.extractfile(member).read())
|
|
source = scratch / SOURCES[0]
|
|
source.parent.mkdir(parents=True, exist_ok=True)
|
|
source.write_bytes((candidate / SOURCES[0]).read_bytes())
|
|
(scratch / 'observe.c').write_text(HARNESS)
|
|
compiler = ['gcc', '-std=c11', '-D_GNU_SOURCE', '-O0', '-fno-builtin', '-Iport', '-Iinclude',
|
|
'-include', 'port/compat.h', 'observe.c', *SOURCES, '-lm', '-o', 'observe']
|
|
runner = [str(scratch / 'observe')]
|
|
working_directory = str(scratch)
|
|
# All scratch materials, inputs, expected values, and argv are ready before the compiler.
|
|
# The supervisor performs no scratch filesystem operation after this point.
|
|
built = subprocess.run(compiler, cwd=working_directory, capture_output=True, text=True, timeout=30)
|
|
if built.returncode:
|
|
print('FAIL: candidate build')
|
|
return 1
|
|
ran = subprocess.run(runner, cwd=working_directory, input=payload, capture_output=True, text=True, timeout=20)
|
|
if ran.returncode:
|
|
print('FAIL: candidate execution')
|
|
return 1
|
|
lines = ran.stdout.splitlines()
|
|
if len(lines) != len(expected):
|
|
print('FAIL: observation count')
|
|
return 1
|
|
mismatches = [i for i, (actual, wanted) in enumerate(zip(lines, expected)) if actual != wanted]
|
|
if mismatches:
|
|
i = mismatches[0]
|
|
print('first mismatch ' + json.dumps({'case': rows[i][2], 'api': rows[i][3], 'capacity': rows[i][4],
|
|
'request': rows[i][0], 'expected': expected[i], 'actual': lines[i]}), file=sys.stderr)
|
|
print('FAIL: formatting contract ('+str(len(mismatches))+' observations)')
|
|
return 1
|
|
print('observations='+str(len(rows)), file=sys.stderr)
|
|
print('PASS')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|