Compare commits
1 Commits
main
...
agent/bitt
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
893521a5ee |
@@ -3,7 +3,7 @@
|
||||
#include <tmc_jpeg_internal.h>
|
||||
|
||||
static s32 TMCJPEGDEC_exif_parse(const u8* data, u32 size, TMCCJPEGDecExifData* pInfo);
|
||||
static void TMCJPEGDEC_IFD0_tag_parse(TMCCJPEGDecExifData* pInfo, u16 byteOrder, const u8* entry);
|
||||
static void TMCJPEGDEC_IFD0_tag_parse(TMCCJPEGDecExifData* pInfo, u16 byteOrder, u8* entry);
|
||||
static void TMCJPEGDEC_IFD1_tag_parse(TMCCJPEGDecExifData* pInfo, u16 byteOrder, const u8* entry);
|
||||
static s32 TMCJPEGDEC_ThumbnailCheck(TMCCJPEGDecInitParam* param, TMCCJPEGDecExifInfo* info, u32 totalSize);
|
||||
|
||||
@@ -280,7 +280,7 @@ static s32 TMCJPEGDEC_exif_parse(const u8* data, u32 size, TMCCJPEGDecExifData*
|
||||
return -161;
|
||||
}
|
||||
for (index = 0; index < count; index++) {
|
||||
TMCJPEGDEC_IFD0_tag_parse(pInfo, byteOrder, entries);
|
||||
TMCJPEGDEC_IFD0_tag_parse(pInfo, byteOrder, (u8*)entries);
|
||||
entries += 12;
|
||||
}
|
||||
remaining -= entriesSize;
|
||||
@@ -331,19 +331,19 @@ static s32 TMCJPEGDEC_exif_parse(const u8* data, u32 size, TMCCJPEGDecExifData*
|
||||
return -161;
|
||||
}
|
||||
for (index = 0; index < count; index++) {
|
||||
TMCJPEGDEC_IFD0_tag_parse(pInfo, byteOrder, entries);
|
||||
TMCJPEGDEC_IFD0_tag_parse(pInfo, byteOrder, (u8*)entries);
|
||||
entries += 12;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void TMCJPEGDEC_IFD0_tag_parse(TMCCJPEGDecExifData* pInfo, u16 byteOrder, const u8* entry) {
|
||||
static void TMCJPEGDEC_IFD0_tag_parse(TMCCJPEGDecExifData* pInfo, u16 byteOrder, u8* entry) {
|
||||
s32 tag;
|
||||
u16 type;
|
||||
u32 type;
|
||||
|
||||
tag = readU16(entry, byteOrder);
|
||||
type = readU16(entry + 2, byteOrder);
|
||||
tag = readExifU16(entry, byteOrder);
|
||||
type = readExifU16(entry + 2, byteOrder);
|
||||
|
||||
switch (tag) {
|
||||
case 0x0103:
|
||||
@@ -501,6 +501,10 @@ static void TMCJPEGDEC_IFD1_tag_parse(TMCCJPEGDecExifData* pInfo, u16 byteOrder,
|
||||
case 0x8769:
|
||||
case 0x9000:
|
||||
case 0x9101:
|
||||
case 0x011C:
|
||||
case 0xA000:
|
||||
case 0xA001:
|
||||
case 0xA002:
|
||||
case 0xA003: {
|
||||
break;
|
||||
}
|
||||
@@ -516,7 +520,7 @@ static void TMCJPEGDEC_IFD1_tag_parse(TMCCJPEGDecExifData* pInfo, u16 byteOrder,
|
||||
return;
|
||||
}
|
||||
pInfo->xResNumIfd1 = readU32(p, byteOrder);
|
||||
p = (const u8*)(pInfo->thumbnailData + offset + 4);
|
||||
p = pInfo->thumbnailData + (offset + 4);
|
||||
if (pInfo->thumbnailData > p) {
|
||||
return;
|
||||
}
|
||||
@@ -538,7 +542,7 @@ static void TMCJPEGDEC_IFD1_tag_parse(TMCCJPEGDecExifData* pInfo, u16 byteOrder,
|
||||
return;
|
||||
}
|
||||
pInfo->planarConfigIfd1 = readU32(p, byteOrder);
|
||||
p = (const u8*)(pInfo->thumbnailData + offset + 4);
|
||||
p = pInfo->thumbnailData + (offset + 4);
|
||||
if (pInfo->thumbnailData > p) {
|
||||
return;
|
||||
}
|
||||
|
||||
244
tools/decomp-assist/jpeg-exif-allhands-attempts.md
Normal file
244
tools/decomp-assist/jpeg-exif-allhands-attempts.md
Normal file
@@ -0,0 +1,244 @@
|
||||
# EXIF parser reconstruction and aliasing audit
|
||||
|
||||
Base: `2a33684c` on `agent/bittle/allhands-jpeg-decompile`; EXIF source itself is
|
||||
unchanged from baseline `449529d9`. This is a separate candidate from the prior
|
||||
JPEG converter/IDCT commit. Those files and their logs were left unchanged.
|
||||
|
||||
## Retained reconstruction
|
||||
|
||||
1. The IFD0 entry parameter is `u8*`, with corresponding call-site casts from the
|
||||
parser's read-only directory cursor. With the reconstructed `const u8*`
|
||||
parameter, MWCC hoists all four version-byte loads before their stores.
|
||||
Removing this unsupported qualifier reproduces the original interleaved
|
||||
load/store sequence for tags 0x9000, 0x9101 and 0xA000.
|
||||
2. IFD0's decoded field type is widened to `u32` and both entry fields use the
|
||||
existing `readExifU16` reader. This reproduces the target's eager zero
|
||||
extension and initial instruction order; it does not change TIFF field size.
|
||||
3. IFD1 explicitly includes ignored 0x011C and 0xA000..0xA002 cases alongside
|
||||
existing ignored tags. Target dispatch compares against 0x011C and 0xA004;
|
||||
these case labels improve the recovered decision tree. The precise original
|
||||
no-op case list cannot be proved uniquely, since unknown tags also return
|
||||
unchanged. No new tag interpretation or payload write is introduced.
|
||||
4. IFD1 rational denominators use `thumbnailData + (offset + 4)` without a
|
||||
redundant same-type cast.
|
||||
|
||||
No new exact functions. Exact stays 3/6, matched code 1348/5088 bytes, allocated
|
||||
data 0 bytes / 100%, and linking is unchanged. Unit fuzzy 96.50157 -> 98.94104.
|
||||
|
||||
| Function | Before | After | Source/target instructions |
|
||||
| --- | ---: | ---: | ---: |
|
||||
| TMCCJPEGDecGetOffsetEXIF | 100 | 100 | 155/155, ctxdiff 0 |
|
||||
| TMCCJPEGDecGetInfoEXIF | 100 | 100 | 132/132, ctxdiff 0 |
|
||||
| TMCJPEGDEC_exif_parse | 99.43396 | 99.43396 | 212/212 |
|
||||
| TMCJPEGDEC_IFD0_tag_parse | 94.46986 | 99.386696 | 480/481 |
|
||||
| TMCJPEGDEC_IFD1_tag_parse | 93.099174 | 96.14876 | 239/242 |
|
||||
| TMCJPEGDEC_ThumbnailCheck | 100 | 100 | 50/50, ctxdiff 0 |
|
||||
|
||||
IFD0 still lacks the original low-tag `bltlr` leaf. Aligning that instruction and
|
||||
its branch displacements leaves 32 GPR-operand differences in the two rational
|
||||
field cases. IFD1 still has decision-tree and register differences. The parser
|
||||
retains 21 GPR-operand differences. Nothing here claims exact completion.
|
||||
|
||||
## Concrete behavioral distinction
|
||||
|
||||
An internal IFD0 entry can be arranged with its four version bytes overlapping
|
||||
the destination by one byte. Initial source bytes 01 02 03 04 produce:
|
||||
|
||||
- Baseline compiled code: destination 01 02 03 04
|
||||
- Original object: destination 01 01 01 01
|
||||
- Corrected compiled code: destination 01 01 01 01
|
||||
|
||||
This is evidence about the internal parser's byte-copy/aliasing behavior. It is
|
||||
not a claim that ordinary nonoverlapping EXIF input was decoded incorrectly, or
|
||||
that a public API vulnerability has been established. Both byte orders and all
|
||||
three version fields were checked at offsets -3 through +3, giving 42 overlap
|
||||
cases. The unchanged baseline object fails this regression test; the final
|
||||
object passes all 42. Local destination casts, explicit loops and removal of
|
||||
other const qualifiers did not fix the discrepancy and were restored.
|
||||
|
||||
## Validation
|
||||
|
||||
- Pool checked before tuning and after the final build: identical, zero strings
|
||||
- No allocated non-code sections in either EXIF object
|
||||
- Fresh official report: all six function scores compared with baseline,
|
||||
zero regressions; all three exact functions retain ctxdiff zero differences
|
||||
- Full 4.3U build: PASS
|
||||
- DOL SHA1: `26116613f624061ba99c8d1a299aaa6efa85670d`
|
||||
- `git diff --check 2a33684c`: PASS, including the committed candidate diff
|
||||
- No header/configuration/linking edits, no changes to the separately owned
|
||||
entropy decoder, no remote writes or original binary data committed
|
||||
|
||||
Build/report commands:
|
||||
|
||||
```sh
|
||||
python3 configure.py --version 43U --wrapper ../toolchain/wibo-build/wibo
|
||||
WIBO_SJIS_MISSING_IMPORTS=1 ../.venv/bin/ninja
|
||||
build/tools/objdiff-cli report generate -p . -o build/43U/report.json -f json
|
||||
PYTHONPATH=../local-tools ../.venv/bin/python tools/decomp-assist/pool_diff.py build/43U/src/libs/RVLMiddleware/TMC_JPEG/src/exif/exif_parse.o build/43U/obj/libs/RVLMiddleware/TMC_JPEG/src/exif/exif_parse.o
|
||||
PYTHONPATH=../local-tools ../.venv/bin/python tools/decomp-assist/ctxdiff.py libs/RVLMiddleware/TMC_JPEG/src/exif/exif_parse <symbol>
|
||||
../.venv/bin/python tools/decomp-assist/jpeg_exif_differential_audit.py
|
||||
../.venv/bin/python tools/decomp-assist/jpeg_exif_differential_audit.py --overlap-only
|
||||
```
|
||||
|
||||
## Differential test scope
|
||||
|
||||
`jpeg_exif_differential_audit.py` is a read-only bounded integer PPC interpreter
|
||||
and deterministic test generator. It reads local ELF objects and never writes
|
||||
original executable bytes to the repository. It is a separate test tool; the
|
||||
previous converter/IDCT audit remains unchanged.
|
||||
|
||||
- 267,282 paired calls, seed 449529
|
||||
- Every 16-bit tag selector in both byte orders for both IFD handlers
|
||||
- Field types 0, 3, 4 and 0xFFFF; valid, boundary, out-of-range and wrapping
|
||||
offsets for supported scalar/rational/byte-array tags
|
||||
- Header sizes and offsets, valid/invalid byte order and TIFF magic, truncated
|
||||
entry arrays, IFD0/IFD1/Exif-directory chains and directory-boundary lengths
|
||||
- 42 overlapping version-byte cases that distinguish baseline from original
|
||||
- Every non-stack byte compared, including guard regions; parser return values,
|
||||
stack pointer, LR and GPR14..GPR31 preservation checked
|
||||
- Complete instruction-address coverage of IFD0 and the central parser in both
|
||||
objects; IFD1 covers every source instruction and 241/242 target instructions
|
||||
(one unreachable return in the target dispatch is not visited)
|
||||
|
||||
Limitations: this is not formal equivalence or a Wii runtime test. Instruction
|
||||
coverage is not exhaustive path/input coverage. The custom interpreter models
|
||||
only the observed integer subset, condition comparisons and internal EXIF calls;
|
||||
unsupported instructions/relocations/callees fail closed. FPU, XER overflow,
|
||||
exceptions, timing, cache/MMIO and a full system ABI are not modeled. Relocations
|
||||
use collision-checked symbol tokens; actual linked addresses are not tested.
|
||||
Public JPEG I/O, thumbnail decoding, arbitrary pointer values and all possible
|
||||
payload alias layouts are outside this test. The three exact wrappers are
|
||||
checked by objdiff/ctxdiff rather than executing their external dependencies.
|
||||
|
||||
Run from the repository root with pyelftools and Capstone available, and without
|
||||
Python's `-O` option. `--source-object` and `--target-object` can select separately
|
||||
compiled objects, allowing the overlap regression to be reproduced on baseline
|
||||
without changing the working tree. Final output is in
|
||||
`jpeg-exif-allhands-differential-results.txt`.
|
||||
|
||||
## Existing external branch overlap
|
||||
|
||||
Read-only comparison used the already fetched ref
|
||||
`origin/agent/w1009/tmc` at `cd90be696cb86258a1bc9fb9dc2b304f2bed318d`.
|
||||
No fetch, author communication, branch checkout or merge was performed.
|
||||
|
||||
Its `1c7cf40c` independently removes const from both tag-entry parameters, so the
|
||||
central IFD0 qualifier correction overlaps. Its `81440c32` then uses a u16 tag,
|
||||
u16 type and explicit tag remask. Its no-op case ranges differ from this
|
||||
candidate's explicit ignored tags. It also changes the shared 16-bit reader and
|
||||
parser declaration order. This candidate does not contain those changes.
|
||||
|
||||
An isolated `/tmp` compile of that branch's EXIF source, using the current
|
||||
checkout's original MWCC flags, produced:
|
||||
|
||||
| Measure/function | This candidate | cd90be69 EXIF object |
|
||||
| --- | ---: | ---: |
|
||||
| Unit fuzzy | 98.94104 | 99.01337 |
|
||||
| Central parser | 99.43396 | 98.679245 |
|
||||
| IFD0 | 99.386696 | 98.37838 |
|
||||
| IFD1 | 96.14876 | 99.194214 |
|
||||
| Exact functions | 3/6 | 3/6 |
|
||||
|
||||
Neither candidate wholly supersedes the other. This candidate has the better
|
||||
IFD0 and preserves the current parser score; the external candidate has the
|
||||
better IFD1. Integration must preserve the parser score and independently
|
||||
revalidate any combined source. The isolated comparison is not a full gate of
|
||||
the external branch or its other files.
|
||||
|
||||
Relative to baseline 449529d9, the external RGB565 edits concern its setter and
|
||||
do not overlap the prior candidate's RGB444 body. Its RGBA420 edge edits overlap
|
||||
that prior candidate's changed expression context. It does not change IDCT.
|
||||
The previously committed JPEG candidate remains separate and unchanged.
|
||||
|
||||
## Attempts
|
||||
|
||||
All rejected/equal variants were restored. The lower-tag 0x0100 experiment had a
|
||||
fuzzy improvement but was explicitly discarded after review: the original
|
||||
branch shape does not establish that particular no-op label, and it changed
|
||||
other lower dispatch comparisons. It is absent from the final source.
|
||||
|
||||
### trials
|
||||
|
||||
```text
|
||||
TMCJPEGDEC_IFD0_tag_parse word-width-field-type: 94.469860 -> 95.122660; RETAIN
|
||||
TMCJPEGDEC_IFD0_tag_parse explicit-ignored-compression-return: 95.122660 -> 94.914764; restore
|
||||
TMCJPEGDEC_IFD0_tag_parse explicit-ignored-compression-break: 95.122660 -> 95.122660; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse recognized-ignored-planar-and-pixel-tags: 93.099174 -> 95.942150; RETAIN
|
||||
TMCJPEGDEC_IFD1_tag_parse ignored-low-strip-offset-own-return: 95.942150 -> 95.342970; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ignored-orientation-own-return: 95.942150 -> 95.533060; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ignored-datetime-own-return: 95.942150 -> 95.528920; restore
|
||||
```
|
||||
|
||||
### control-trials
|
||||
|
||||
```text
|
||||
TMCJPEGDEC_IFD0_tag_parse explicit-unknown-tag-return: 95.122660 -> 94.498960; restore
|
||||
TMCJPEGDEC_IFD0_tag_parse explicit-unknown-tag-break: 95.122660 -> 95.122660; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse explicit-unknown-tag-return: 95.942150 -> 95.876030; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse explicit-unknown-tag-break: 95.942150 -> 95.942150; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ifd1-orientation-uses-unknown-exit: 95.942150 -> 95.756195; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ifd1-planar-uses-unknown-exit: 95.942150 -> 91.694214; restore
|
||||
```
|
||||
|
||||
### ignored-tag-trials
|
||||
|
||||
```text
|
||||
TMCJPEGDEC_IFD0_tag_parse ignored-image-fields-before-compression: 95.122660 -> 95.110180; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ignored-image-fields: 95.942150 -> 95.735535; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ifd1-planar-logical-case-order: 95.942150 -> 95.942150; restore
|
||||
```
|
||||
|
||||
### reader-trials
|
||||
|
||||
```text
|
||||
TMCJPEGDEC_IFD0_tag_parse shared-exif-tag-reader: 95.122660 -> 95.195430; RETAIN
|
||||
TMCJPEGDEC_IFD0_tag_parse shared-exif-type-reader: 95.195430 -> 95.517670; RETAIN
|
||||
TMCJPEGDEC_IFD0_tag_parse shared-exif-short-reader: 95.517670 -> 95.517670; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse shared-exif-tag-reader: 95.942150 -> 95.942150; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse shared-exif-short-reader: 95.942150 -> 95.942150; restore
|
||||
```
|
||||
|
||||
### lower-tag-trials
|
||||
|
||||
```text
|
||||
TMCJPEGDEC_IFD0_tag_parse ignored-lower-tags-(256,): 95.517670 -> 93.004160; restore
|
||||
TMCJPEGDEC_IFD0_tag_parse ignored-lower-tags-(257,): 95.517670 -> 93.004160; restore
|
||||
TMCJPEGDEC_IFD0_tag_parse ignored-lower-tags-(258,): 95.517670 -> 95.505196; restore
|
||||
TMCJPEGDEC_IFD0_tag_parse ignored-lower-tags-(256, 257): 95.517670 -> 93.004160; restore
|
||||
TMCJPEGDEC_IFD0_tag_parse ignored-lower-tags-(256, 258): 95.517670 -> 93.004160; restore
|
||||
TMCJPEGDEC_IFD0_tag_parse ignored-lower-tags-(257, 258): 95.517670 -> 95.505196; restore
|
||||
TMCJPEGDEC_IFD0_tag_parse ignored-lower-tags-(256, 257, 258): 95.517670 -> 95.505196; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ignored-lower-tags-(256,): 95.942150 -> 96.665290; RETAIN
|
||||
TMCJPEGDEC_IFD1_tag_parse ignored-lower-tags-(257,): 96.665290 -> 96.665290; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ignored-lower-tags-(258,): 96.665290 -> 95.735535; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ignored-lower-tags-(256, 257): 96.665290 -> 96.644630; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ignored-lower-tags-(256, 258): 96.665290 -> 96.256195; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ignored-lower-tags-(257, 258): 96.665290 -> 95.735535; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse ignored-lower-tags-(256, 257, 258): 96.665290 -> 95.735535; restore
|
||||
```
|
||||
|
||||
### byte-copy-trials
|
||||
|
||||
```text
|
||||
TMCJPEGDEC_IFD0_tag_parse alias-preserving-version-byte-copy: 95.517670 -> 95.517670; restore
|
||||
```
|
||||
|
||||
### version-loop-trials
|
||||
|
||||
```text
|
||||
TMCJPEGDEC_IFD0_tag_parse forward-version-byte-loops: 95.517670 -> 95.517670; restore
|
||||
```
|
||||
|
||||
### qualifier-trials
|
||||
|
||||
```text
|
||||
TMCJPEGDEC_IFD0_tag_parse mutable-entry-view: 95.517670 -> 99.386696; RETAIN
|
||||
TMCJPEGDEC_IFD1_tag_parse mutable-entry-view: 95.942150 -> 95.942150; restore
|
||||
```
|
||||
|
||||
### pointer-trials
|
||||
|
||||
```text
|
||||
TMCJPEGDEC_IFD1_tag_parse mutable-rational-byte-view: 95.942150 -> 95.942150; restore
|
||||
TMCJPEGDEC_IFD1_tag_parse direct-denominator-byte-address: 95.942150 -> 96.148760; RETAIN
|
||||
```
|
||||
@@ -0,0 +1,6 @@
|
||||
TMCJPEGDEC_IFD0_tag_parse 131072 selector cases PASS
|
||||
TMCJPEGDEC_IFD1_tag_parse 131072 selector cases PASS
|
||||
TMCJPEGDEC_IFD0_tag_parse 132746 cases PASS; own-body instruction coverage 480 / 480 source; 481 / 481 original
|
||||
TMCJPEGDEC_IFD1_tag_parse 132704 cases PASS; own-body instruction coverage 239 / 239 source; 241 / 242 original
|
||||
TMCJPEGDEC_exif_parse 1832 cases PASS; own-body instruction coverage 212 / 212 source; 212 / 212 original
|
||||
TOTAL 267282 paired EXIF calls PASS
|
||||
368
tools/decomp-assist/jpeg_exif_differential_audit.py
Normal file
368
tools/decomp-assist/jpeg_exif_differential_audit.py
Normal file
@@ -0,0 +1,368 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Read-only, bounded PPC integer interpreter for differential EXIF checks.
|
||||
Loads source and original ELF .text; never copies original code into sources.
|
||||
Only internal EXIF calls are supported. No FPU/system instructions are supported.
|
||||
"""
|
||||
import random
|
||||
import re
|
||||
import struct
|
||||
import zlib
|
||||
|
||||
import capstone
|
||||
from elftools.elf.elffile import ELFFile
|
||||
U32 = 0xFFFFFFFF
|
||||
|
||||
def signed(x):
|
||||
x &= U32
|
||||
return x - 0x100000000 if x & 0x80000000 else x
|
||||
|
||||
def rotate(x, n):
|
||||
return (x << n | x >> (32 - n if n else 32)) & U32
|
||||
|
||||
def mask(b, e):
|
||||
return sum((1 << 31 - i for i in range(32) if b <= i <= e or (b > e and (i >= b or i <= e))))
|
||||
|
||||
def load_object(path):
|
||||
with open(path, 'rb') as f:
|
||||
e = ELFFile(f)
|
||||
section = e.get_section_by_name('.text')
|
||||
data = bytearray(section.data())
|
||||
syms = e.get_section_by_name('.symtab')
|
||||
calls = {}
|
||||
symbol_addresses = {}
|
||||
rel = e.get_section_by_name('.rela.text')
|
||||
if rel:
|
||||
for r in rel.iter_relocations():
|
||||
name = syms.get_symbol(r['r_info_sym']).name
|
||||
off = r['r_offset']
|
||||
typ = r['r_info_type']
|
||||
addr = 0x800000 + (zlib.crc32(name.encode()) & 0x7FFF) * 4 + r['r_addend']
|
||||
assert addr not in symbol_addresses or symbol_addresses[addr] == name
|
||||
symbol_addresses[addr] = name
|
||||
if typ == 10:
|
||||
calls[off] = name
|
||||
elif typ == 4:
|
||||
data[off:off + 2] = struct.pack('>H', addr & 65535)
|
||||
elif typ == 6:
|
||||
data[off:off + 2] = struct.pack('>H', addr + 0x8000 >> 16 & 65535)
|
||||
else:
|
||||
raise ValueError(('relocation', typ))
|
||||
dis = capstone.Cs(capstone.CS_ARCH_PPC, capstone.CS_MODE_32 | capstone.CS_MODE_BIG_ENDIAN)
|
||||
funcs = {}
|
||||
for sym in syms.iter_symbols():
|
||||
if sym['st_info']['type'] != 'STT_FUNC' or not sym['st_size']:
|
||||
continue
|
||||
start = sym['st_value']
|
||||
size = sym['st_size']
|
||||
inst = {}
|
||||
for i in dis.disasm(bytes(data[start:start + size]), start):
|
||||
ops = []
|
||||
for o in i.op_str.split(', '):
|
||||
if not o:
|
||||
continue
|
||||
if o.startswith('cr'):
|
||||
ops.append(('cr', int(o[2:])))
|
||||
elif o.startswith('r'):
|
||||
ops.append(int(o[1:]))
|
||||
elif '(' in o:
|
||||
off, reg = o.split('(')
|
||||
ops.append((int(off, 0), int(reg[1:-1])))
|
||||
else:
|
||||
ops.append(int(o, 0))
|
||||
inst[i.address] = (i.mnemonic, ops)
|
||||
funcs[sym.name] = (start, inst, calls)
|
||||
return funcs
|
||||
|
||||
def run(functions, name, initial, args):
|
||||
pc = functions[name][0]
|
||||
ins = {address: instruction for _, body, _ in functions.values() for address, instruction in body.items()}
|
||||
calls = functions[name][2]
|
||||
mem = bytearray(initial)
|
||||
r = [i * 1122867 & U32 for i in range(32)]
|
||||
r[1] = 0x3F000
|
||||
r[3:3 + len(args)] = args
|
||||
saved_registers = r[14:].copy()
|
||||
cr = [0] * 8
|
||||
ctr = 0
|
||||
lr = 0
|
||||
steps = 0
|
||||
coverage = set()
|
||||
|
||||
def ld(a, n):
|
||||
assert 0 <= a <= len(mem) - n, hex(a)
|
||||
return int.from_bytes(mem[a:a + n], 'big')
|
||||
|
||||
def st(a, v, n):
|
||||
assert 0 <= a <= len(mem) - n, hex(a)
|
||||
mem[a:a + n] = (v & (1 << 8 * n) - 1).to_bytes(n, 'big')
|
||||
while True:
|
||||
steps += 1
|
||||
if steps > 100000:
|
||||
raise RuntimeError('step bound')
|
||||
coverage.add(pc)
|
||||
op, a = ins[pc]
|
||||
pc += 4
|
||||
dot = op.endswith('.')
|
||||
op = op.rstrip('.')
|
||||
val = None
|
||||
if op == 'blr':
|
||||
if lr == 0:
|
||||
break
|
||||
pc = lr
|
||||
elif op == 'bl':
|
||||
callee = calls.get(pc - 4)
|
||||
assert callee in functions, ('Unsupported external call', callee)
|
||||
lr = pc
|
||||
pc = functions[callee][0]
|
||||
elif op == 'b':
|
||||
pc = a[0]
|
||||
elif op == 'bdnz':
|
||||
ctr = ctr - 1 & U32
|
||||
if ctr:
|
||||
pc = a[0]
|
||||
elif op in ['beqlr', 'bnelr', 'blelr', 'bgelr', 'bltlr', 'bgtlr']:
|
||||
c = cr[a[0][1]] if a else cr[0]
|
||||
condition = {'beqlr': c == 0, 'bnelr': c != 0, 'blelr': c <= 0, 'bgelr': c >= 0, 'bltlr': c < 0, 'bgtlr': c > 0}[op]
|
||||
if condition:
|
||||
if lr == 0:
|
||||
break
|
||||
pc = lr
|
||||
elif op in ['beq', 'bne', 'ble', 'bge', 'blt', 'bgt']:
|
||||
c = cr[a[0][1]] if len(a) > 1 else cr[0]
|
||||
if {'beq': c == 0, 'bne': c != 0, 'ble': c <= 0, 'bge': c >= 0, 'blt': c < 0, 'bgt': c > 0}[op]:
|
||||
pc = a[-1]
|
||||
elif op in ['cmpwi', 'cmpw', 'cmplw', 'cmplwi']:
|
||||
c = a[0][1] if isinstance(a[0], tuple) else 0
|
||||
a = ins[pc - 4][1]
|
||||
if isinstance(a[0], tuple):
|
||||
a = a[1:]
|
||||
x = r[a[0]]
|
||||
y = a[1] if op in ['cmpwi', 'cmplwi'] else r[a[1]]
|
||||
if op not in ['cmplw', 'cmplwi']:
|
||||
x = signed(x)
|
||||
y = signed(y)
|
||||
cr[c] = (x > y) - (x < y)
|
||||
elif op in ['lbz', 'lhz', 'lwz', 'lmw']:
|
||||
off, reg = a[1]
|
||||
addr = (r[reg] if reg else 0) + off & U32
|
||||
if op == 'lmw':
|
||||
for j in range(a[0], 32):
|
||||
r[j] = ld(addr + (j - a[0]) * 4, 4)
|
||||
else:
|
||||
r[a[0]] = ld(addr, {'lbz': 1, 'lhz': 2, 'lwz': 4}[op])
|
||||
elif op in ['stb', 'sth', 'stw', 'stwu', 'stmw']:
|
||||
off, reg = a[1]
|
||||
addr = (r[reg] if reg else 0) + off & U32
|
||||
if op == 'stmw':
|
||||
for j in range(a[0], 32):
|
||||
st(addr + (j - a[0]) * 4, r[j], 4)
|
||||
else:
|
||||
st(addr, r[a[0]], {'stb': 1, 'sth': 2, 'stw': 4, 'stwu': 4}[op])
|
||||
if op == 'stwu':
|
||||
r[reg] = addr
|
||||
elif op in ['stbx', 'sthx']:
|
||||
st((r[a[1]] if a[1] else 0) + r[a[2]] & U32, r[a[0]], 1 if op == 'stbx' else 2)
|
||||
elif op == 'mflr':
|
||||
r[a[0]] = lr
|
||||
elif op == 'mtlr':
|
||||
lr = r[a[0]]
|
||||
elif op == 'mtctr':
|
||||
ctr = r[a[0]]
|
||||
else:
|
||||
d = a[0]
|
||||
if op == 'li':
|
||||
val = a[1]
|
||||
elif op == 'lis':
|
||||
val = a[1] << 16
|
||||
elif op == 'mr':
|
||||
val = r[a[1]]
|
||||
elif op == 'add':
|
||||
val = r[a[1]] + r[a[2]]
|
||||
elif op == 'addi':
|
||||
val = (r[a[1]] if a[1] else 0) + a[2]
|
||||
elif op == 'addis':
|
||||
val = (r[a[1]] if a[1] else 0) + (a[2] << 16)
|
||||
elif op == 'subf':
|
||||
val = r[a[2]] - r[a[1]]
|
||||
elif op == 'subfic':
|
||||
val = a[2] - r[a[1]]
|
||||
elif op == 'mulli':
|
||||
val = r[a[1]] * a[2]
|
||||
elif op == 'mullw':
|
||||
val = r[a[1]] * r[a[2]]
|
||||
elif op == 'divw':
|
||||
x = signed(r[a[1]])
|
||||
y = signed(r[a[2]])
|
||||
val = abs(x) // abs(y) * (-1 if (x < 0) != (y < 0) else 1)
|
||||
elif op == 'or':
|
||||
val = r[a[1]] | r[a[2]]
|
||||
elif op == 'andc':
|
||||
val = r[a[1]] & ~r[a[2]]
|
||||
elif op == 'andi':
|
||||
val = r[a[1]] & a[2]
|
||||
elif op == 'neg':
|
||||
val = -r[a[1]]
|
||||
elif op == 'extsb':
|
||||
val = (r[a[1]] & 255) - (256 if r[a[1]] & 128 else 0)
|
||||
elif op == 'srawi':
|
||||
val = signed(r[a[1]]) >> a[2]
|
||||
elif op == 'srwi':
|
||||
val = r[a[1]] >> a[2]
|
||||
elif op == 'slwi':
|
||||
val = r[a[1]] << a[2]
|
||||
elif op == 'clrlwi':
|
||||
val = r[a[1]] & (1 << 32 - a[2]) - 1
|
||||
elif op == 'rotlwi':
|
||||
val = rotate(r[a[1]], a[2])
|
||||
elif op == 'rlwimi':
|
||||
bits = mask(a[3], a[4])
|
||||
val = (r[d] & ~bits) | (rotate(r[a[1]], a[2]) & bits)
|
||||
elif op == 'rlwinm':
|
||||
val = rotate(r[a[1]], a[2]) & mask(a[3], a[4])
|
||||
else:
|
||||
raise ValueError((hex(pc - 4), op, a))
|
||||
r[d] = val & U32
|
||||
if dot:
|
||||
cr[0] = (signed(val) > 0) - (signed(val) < 0)
|
||||
assert r[1] == 0x3F000, 'Stack pointer not restored'
|
||||
assert lr == 0, 'Link register not restored'
|
||||
assert r[14:] == saved_registers, 'Nonvolatile registers not restored'
|
||||
return (mem, r[3], coverage)
|
||||
|
||||
def put(m, a, v, n=4):
|
||||
m[a:a + n] = (v & (1 << 8 * n) - 1).to_bytes(n, 'big')
|
||||
|
||||
def compare(src, orig, name, m, args, case, returns=False):
|
||||
a, ra, ca = run(src, name, m, args)
|
||||
b, rb, cb = run(orig, name, m, args)
|
||||
# The stack can differ because each implementation owns its frame.
|
||||
if a[:0x3E000] != b[:0x3E000]:
|
||||
diff = [hex(i) for i in range(0x3E000) if a[i] != b[i]][:12]
|
||||
raise AssertionError((name, case, 'memory mismatch', diff))
|
||||
if returns and ra != rb:
|
||||
raise AssertionError((name, case, 'return', ra, rb))
|
||||
return (ca, cb)
|
||||
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
from collections import defaultdict
|
||||
path = 'build/43U/{}/libs/RVLMiddleware/TMC_JPEG/src/exif/exif_parse.o'
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--source-object', default=path.format('src'))
|
||||
parser.add_argument('--target-object', default=path.format('obj'))
|
||||
parser.add_argument('--overlap-only', action='store_true')
|
||||
args = parser.parse_args()
|
||||
src = load_object(args.source_object)
|
||||
orig = load_object(args.target_object)
|
||||
assert src.keys() == orig.keys()
|
||||
assert len(src) == 6
|
||||
names = ['TMCJPEGDEC_IFD0_tag_parse', 'TMCJPEGDEC_IFD1_tag_parse', 'TMCJPEGDEC_exif_parse']
|
||||
coverage = {name: [set(), set()] for name in names}
|
||||
counts = defaultdict(int)
|
||||
rng = random.Random(449529)
|
||||
info, entry, data = 0x1000, 0x2000, 0x3000
|
||||
memory = bytearray([0xA5]) * 0x40000
|
||||
memory[data:data + 0x2000] = bytes(rng.randrange(256) for _ in range(0x2000))
|
||||
put(memory, info + 0x674, data)
|
||||
put(memory, info + 0x678, data + 0x1000)
|
||||
|
||||
def exif_put(m, pos, value, size, order):
|
||||
m[pos:pos + size] = (value & ((1 << (8 * size)) - 1)).to_bytes(size, 'little' if order == 0x4949 else 'big')
|
||||
|
||||
def check(name, m, args, case, returns=False):
|
||||
ca, cb = compare(src, orig, name, m, args, case, returns)
|
||||
coverage[name][0].update(ca)
|
||||
coverage[name][1].update(cb)
|
||||
counts[name] += 1
|
||||
|
||||
for order in [0x4949, 0x4D4D]:
|
||||
for tag, offset in [(0x9000, 0x63C), (0x9101, 0x640), (0xA000, 0x644)]:
|
||||
for delta in range(-3, 4):
|
||||
m = bytearray(memory)
|
||||
destination = info + offset
|
||||
aliased_entry = destination + delta - 8
|
||||
exif_put(m, aliased_entry, tag, 2, order)
|
||||
m[aliased_entry + 8:aliased_entry + 12] = bytes([1, 2, 3, 4])
|
||||
check(names[0], m, [info, order, aliased_entry], ('overlapping-version-bytes', order, tag, delta))
|
||||
|
||||
if args.overlap_only:
|
||||
print('TOTAL', sum(counts.values()), 'overlapping version-byte calls PASS')
|
||||
return
|
||||
|
||||
for name in names[:2]:
|
||||
selector_start = counts[name]
|
||||
for order in [0x4949, 0x4D4D]:
|
||||
exif_put(memory, entry + 2, 4, 2, order)
|
||||
exif_put(memory, entry + 4, 1, 4, order)
|
||||
exif_put(memory, entry + 8, 0x100, 4, order)
|
||||
for tag in range(0x10000):
|
||||
exif_put(memory, entry, tag, 2, order)
|
||||
check(name, memory, [info, order, entry], ('all-tags', order, tag))
|
||||
print(name, counts[name] - selector_start, 'selector cases PASS', flush=True)
|
||||
for order in [0x4949, 0x4D4D]:
|
||||
for tag in [0x103, 0x112, 0x11A, 0x11B, 0x128, 0x12D, 0x132, 0x201, 0x202, 0x213, 0x8769, 0x9000, 0x9101, 0xA000, 0xA001, 0xA002, 0xA003]:
|
||||
exif_put(memory, entry, tag, 2, order)
|
||||
for field_type in [0, 3, 4, 0xFFFF]:
|
||||
exif_put(memory, entry + 2, field_type, 2, order)
|
||||
for offset in [0, 1, 0xFF0, 0xFEC, 0xFF8, 0xFFC, 0xFFE, 0xFFF, 0x1000, 0x1001, 0xFFFFFFFF, 0xFFFFD000]:
|
||||
exif_put(memory, entry + 8, offset, 4, order)
|
||||
check(name, memory, [info, order, entry], ('bounds', order, tag, field_type, offset))
|
||||
|
||||
name = names[2]
|
||||
for order in [0x4949, 0x4D4D, 0, 0x1234]:
|
||||
for size in [0, 1, 7, 8, 9, 10, 13, 14, 25, 26, 40, 64, 128, 0x1000]:
|
||||
for ifd_offset in [0, 2, 8, 10, 0x1000, 0xFFFFFFFF]:
|
||||
for count in [0, 1, 2, 0xFFFF]:
|
||||
m = bytearray(memory)
|
||||
m[data:data + 0x1000] = bytes(0x1000)
|
||||
exif_put(m, data, order, 2, order)
|
||||
exif_put(m, data + 2, 42, 2, order)
|
||||
exif_put(m, data + 4, ifd_offset, 4, order)
|
||||
if 8 <= ifd_offset < 0x1000:
|
||||
exif_put(m, data + ifd_offset, count, 2, order)
|
||||
check(name, m, [data, size, info], ('header-bounds', order, size, ifd_offset, count), True)
|
||||
|
||||
for order in [0x4949, 0x4D4D]:
|
||||
for magic in [0, 41, 43, 0xFFFF]:
|
||||
m = bytearray(memory)
|
||||
exif_put(m, data, order, 2, order)
|
||||
exif_put(m, data + 2, magic, 2, order)
|
||||
check(name, m, [data, 128, info], ('invalid-TIFF-magic', order, magic), True)
|
||||
|
||||
for order in [0x4949, 0x4D4D]:
|
||||
for count0 in [0, 1, 2, 3]:
|
||||
for count1 in [0, 1, 2, 3]:
|
||||
for size in [40, 64, 65, 66, 70, 79, 80, 100, 128, 129, 130, 140, 144, 160, 256]:
|
||||
m = bytearray(memory)
|
||||
m[data:data + 0x1000] = bytes(0x1000)
|
||||
exif_put(m, data, order, 2, order)
|
||||
exif_put(m, data + 2, 42, 2, order)
|
||||
exif_put(m, data + 4, 8, 4, order)
|
||||
exif_put(m, data + 8, count0, 2, order)
|
||||
for index, tag in enumerate([0x112, 0xA002, 0x8769][:count0]):
|
||||
pos = data + 10 + index * 12
|
||||
exif_put(m, pos, tag, 2, order)
|
||||
exif_put(m, pos + 2, 4, 2, order)
|
||||
exif_put(m, pos + 8, 128 if tag == 0x8769 else 3, 4, order)
|
||||
exif_put(m, data + 10 + count0 * 12, 64, 4, order)
|
||||
exif_put(m, data + 64, count1, 2, order)
|
||||
for index, tag in enumerate([0x103, 0x201, 0x202][:count1]):
|
||||
pos = data + 66 + index * 12
|
||||
exif_put(m, pos, tag, 2, order)
|
||||
exif_put(m, pos + 2, 4, 2, order)
|
||||
exif_put(m, pos + 8, 6, 4, order)
|
||||
exif_put(m, data + 128, 1, 2, order)
|
||||
exif_put(m, data + 130, 0xA003, 2, order)
|
||||
exif_put(m, data + 132, 4, 2, order)
|
||||
exif_put(m, data + 138, 640, 4, order)
|
||||
check(name, m, [data, size, info], ('directory-chain', order, count0, count1, size), True)
|
||||
|
||||
for name in names:
|
||||
source_body = set(src[name][1])
|
||||
original_body = set(orig[name][1])
|
||||
print(name, counts[name], 'cases PASS; own-body instruction coverage', len(coverage[name][0] & source_body), '/', len(source_body), 'source;', len(coverage[name][1] & original_body), '/', len(original_body), 'original', flush=True)
|
||||
print('TOTAL', sum(counts.values()), 'paired EXIF calls PASS')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user