Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10253,6 +10253,14 @@ if(EXISTS "${INTAKE_PYTHON}")
set_tests_properties(bcpack_symbols_cli_check PROPERTIES TIMEOUT 60)
add_test(NAME bcpack_tables COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/bcpack_tables_check.py")
# The class of a failed BCPack request from the synthetic streams (src/startup_stubs/bcpack_inspect.h, #1198);
# the collapse control reports every failure as one class and must fail.
add_executable(bcpack_inspect_check tests/bcpack_inspect_check.cpp)
target_compile_features(bcpack_inspect_check PRIVATE cxx_std_17)
target_include_directories(bcpack_inspect_check PRIVATE src)
add_test(NAME bcpack_inspect COMMAND bcpack_inspect_check)
add_test(NAME bcpack_inspect_collapse_control COMMAND bcpack_inspect_check --mutate collapse)
set_tests_properties(bcpack_inspect_collapse_control PROPERTIES WILL_FAIL TRUE)
add_test(NAME bcpack_probe_cli COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/bcpack_probe_cli_check.py")
add_test(NAME bc7_block_reference COMMAND "${INTAKE_PYTHON}" -B
Expand Down
80 changes: 73 additions & 7 deletions src/startup_stubs/bcpack_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,44 @@ inline bool phase1(Lane& ln) {
return true;
}

// The original cycle loop. decode_lanes no longer calls it; tests compare every path with it.
// Why decode_lanes returned false, for inspect_bcpack (bcpack_inspect.h). Filled only when a report is passed.
enum class LaneFailure : uint8_t {
none,
bad_arguments, // no lanes, more than 16, a zero target or an alphabet wider than 8 bits
header_invalid, // a fragment header names an alphabet wider than 8 bits
fragment_not_home, // a flushed fragment did not end at the rANS home state
end_state_invalid, // all lanes finished in a state that is not a terminal state
trailing_input, // all lanes finished and a non-zero input byte is left
request_shorter, // every lane reached its target and the decode still did not finish
request_longer, // all input read, every lane stopped at a fragment boundary before its target
truncated, // all input read, a lane stopped inside a fragment before its target
no_progress, // no progress for 64 cycles in another state, or the cycle limit
};

struct LaneReport {
LaneFailure failure = LaneFailure::none;
size_t bytes_read = 0; // input bytes handed to lanes when the decode ended
uint32_t min_symbols = 0; // fewest and most symbols any lane had produced
uint32_t max_symbols = 0;
uint32_t target_symbols = 0; // symbols per lane that were requested
};

// The original cycle loop. Without a report decode_lanes uses a faster path; with one it runs this loop,
// which names the failure. Tests compare every path with it.
inline bool decode_lanes_reference(const uint8_t* input, size_t input_size,
const uint8_t* alphabet_bits, size_t num_lanes,
size_t target_symbols_per_lane,
std::vector<uint16_t>& symbols) {
if (num_lanes == 0 || num_lanes > 16 || target_symbols_per_lane == 0) return false;
std::vector<uint16_t>& symbols, LaneReport* report = nullptr) {
if (report) *report = LaneReport{};
if (num_lanes == 0 || num_lanes > 16 || target_symbols_per_lane == 0) {
if (report) report->failure = LaneFailure::bad_arguments;
return false;
}
for (size_t i = 0; i < num_lanes; ++i) {
if (alphabet_bits[i] > 8) return false;
if (alphabet_bits[i] > 8) {
if (report) report->failure = LaneFailure::bad_arguments;
return false;
}
}
symbols.assign(num_lanes * target_symbols_per_lane, 0);

Expand All @@ -274,6 +304,31 @@ inline bool decode_lanes_reference(const uint8_t* input, size_t input_size,
uint32_t last_state_sum = UINT32_MAX;
uint64_t last_change = 0;

// Records why and where the decode stopped; nothing happens without a report.
auto note = [&](LaneFailure failure) {
if (!report) return;
report->failure = failure;
report->bytes_read = pos;
report->target_symbols = static_cast<uint32_t>(target_symbols_per_lane);
report->min_symbols = lanes[0].nsyms;
report->max_symbols = lanes[0].nsyms;
for (const Lane& ln : lanes) {
if (ln.nsyms < report->min_symbols) report->min_symbols = ln.nsyms;
if (ln.nsyms > report->max_symbols) report->max_symbols = ln.nsyms;
}
};
// A stall: the request and the stream disagree about the symbol count, or the stream is damaged.
auto stall_failure = [&]() {
bool met = true, at_boundary = true;
for (const Lane& ln : lanes) {
met &= ln.nsyms == target_symbols_per_lane;
at_boundary &= ln.state == LANE_HDR0 && !ln.ib_full;
}
if (met) return LaneFailure::request_shorter;
if (!met && pos == input_size) return at_boundary ? LaneFailure::request_longer : LaneFailure::truncated;
return LaneFailure::no_progress;
};

while (cycle < max_cycles) {
size_t served = 0;
for (size_t i = 0; i < num_lanes; ++i) {
Expand Down Expand Up @@ -319,7 +374,10 @@ inline bool decode_lanes_reference(const uint8_t* input, size_t input_size,
for (size_t i = 0; i < num_lanes; ++i) {
Lane& ln = lanes[i];
if (!ln.stall) {
if (!phase1(ln)) return false;
if (!phase1(ln)) {
note(ln.eab > 8 ? LaneFailure::header_invalid : LaneFailure::fragment_not_home);
return false;
}
}
uint32_t bit = 1u << i;
if (!(ln.done && !ln.sym_full && (slots & bit) == 0)) {
Expand All @@ -337,12 +395,17 @@ inline bool decode_lanes_reference(const uint8_t* input, size_t input_size,
for (size_t i = 0; i < num_lanes; ++i) {
uint32_t f = lanes[i].final_state;
if (f != 0x1000000 && f != 0x10000 && f != 0x100) {
note(LaneFailure::end_state_invalid);
return false;
}
}
for (size_t p = pos; p < input_size; ++p) {
if (input[p] != 0) return false;
if (input[p] != 0) {
note(LaneFailure::trailing_input);
return false;
}
}
if (report) note(LaneFailure::none);
return true;
}

Expand All @@ -352,9 +415,11 @@ inline bool decode_lanes_reference(const uint8_t* input, size_t input_size,
last_state_sum = state_sum;
last_change = cycle;
} else if (cycle - last_change > 64) {
note(stall_failure());
return false;
}
}
note(LaneFailure::no_progress);
return false;
}

Expand Down Expand Up @@ -848,7 +913,8 @@ inline bool decode_lanes_on(int path, const uint8_t* input, size_t input_size,
inline bool decode_lanes(const uint8_t* input, size_t input_size,
const uint8_t* alphabet_bits, size_t num_lanes,
size_t target_symbols_per_lane,
std::vector<uint16_t>& symbols) {
std::vector<uint16_t>& symbols, LaneReport* report = nullptr) {
if (report) return decode_lanes_reference(input, input_size, alphabet_bits, num_lanes, target_symbols_per_lane, symbols, report);
#ifdef X2W_BCPACK_LANE_TESTS
int path = lane_path_supported(lane_path_override) ? lane_path_override : best_lane_path();
#else
Expand Down
149 changes: 149 additions & 0 deletions src/startup_stubs/bcpack_inspect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Names the reason a BCPack request fails (Refs #1198). decode_bcpack answers true or false; this header
// runs the same lane decode on the cold path with a LaneReport and returns the class of failure plus
// what the lanes had done when it stopped. It does not change decode_bcpack or any request path.
//
// The class is a reading of lane state, not a proof about the source data. A stream cut exactly at a
// fragment boundary in every lane reads as request_longer_than_stream, because it is a shorter stream.
// Every format decode_bcpack decodes (BC1 to BC7) has a lane layout here, the same alphabets and symbols per
// block; add a layout entry when decode_bcpack gains a format (bcpack_inspect_check compares the table with the
// decoder's fixtures and with decode_bcpack itself, so drift fails the test). The BC1 to BC5 block stages fail
// only on a destination that is too small, which this header never passes. BC7 (an unmapped control code) and
// BC6H (a mode outside 1 to 14) can reject lane symbols, and both are reported as control_code_unmapped.
#pragma once

#include <cstddef>
#include <cstdint>
#include <vector>

#include "bcpack_decoder.h"

namespace x2w_bcpack {

enum class InspectClass : uint8_t {
ok,
bad_arguments, // null or empty input, empty or oversized destination, or no lane layout fits
unsupported_format, // a format number without a lane layout in this header
destination_misaligned, // destination size is not a whole number of blocks
header_invalid, // a fragment header names an alphabet index above 8
fragment_not_home, // a flushed fragment did not end at the rANS home state
end_state_invalid, // all lanes finished in a state that is not a terminal state
trailing_input, // all lanes finished and a non-zero input byte is left
request_shorter_than_stream, // every lane reached the requested count and the decode still did not finish
request_longer_than_stream, // all input read, every lane stopped at a fragment boundary short of the count
truncated_in_fragment, // all input read, a lane stopped inside a fragment short of the count
cycle_limit, // no progress for 64 cycles in another state, or the cycle limit
control_code_unmapped, // lanes decoded; a BC7 block names an unmapped control code or a BC6H block a mode outside 1 to 14
};

struct InspectResult {
InspectClass cls = InspectClass::bad_arguments;
size_t bytes_read = 0; // input bytes the lanes had taken when the decode ended
uint32_t min_symbols_per_lane = 0;
uint32_t max_symbols_per_lane = 0;
uint32_t target_symbols_per_lane = 0; // what the destination size asked for
};

// Largest destination inspected: the 16 MiB decoded-chunk cap of the chunk-window experiment. Lane symbols cost
// (output / block_bytes) * symbols_per_block * lanes * 2 bytes, at most 4x the output for BC7 (64 MiB at the
// cap). A declared size above the cap returns bad_arguments before anything is allocated.
inline constexpr size_t inspect_max_output = size_t{16} << 20;

struct InspectLayout {
unsigned format;
size_t lanes;
uint8_t alphabet_bits[16];
size_t block_bytes;
size_t symbols_per_block; // per lane
};

inline constexpr InspectLayout INSPECT_LAYOUTS[] = {
{ 1, 12, { 7, 7, 4, 5, 4, 4, 5, 4, 7, 7, 7, 7 }, 8, 1 },
{ 2, 15, { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 5, 4, 7, 7 }, 16, 2 },
{ 6, 16, { 3, 3, 7, 7, 7, 4, 4, 4, 5, 5, 5, 7, 7, 7, 7, 4 }, 16, 2 },
{ 7, 16, { 3, 3, 7, 7, 7, 7, 7, 6, 6, 4, 5, 7, 7, 7, 7, 4 }, 16, 2 },
{ 3, 12, { 3, 3, 7, 5, 5, 5, 5, 4, 5, 4, 7, 7 }, 16, 2 },
{ 4, 12, { 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 5, 5 }, 8, 1 },
{ 5, 12, { 3, 3, 7, 7, 5, 5, 5, 5, 5, 5, 5, 5 }, 16, 2 },
};

inline const InspectLayout* inspect_layout(unsigned format) {
for (const InspectLayout& layout : INSPECT_LAYOUTS) {
if (layout.format == format) return &layout;
}
return nullptr;
}

inline InspectClass inspect_class(LaneFailure failure) {
switch (failure) {
case LaneFailure::none: return InspectClass::ok;
case LaneFailure::bad_arguments: return InspectClass::bad_arguments;
case LaneFailure::header_invalid: return InspectClass::header_invalid;
case LaneFailure::fragment_not_home: return InspectClass::fragment_not_home;
case LaneFailure::end_state_invalid: return InspectClass::end_state_invalid;
case LaneFailure::trailing_input: return InspectClass::trailing_input;
case LaneFailure::request_shorter: return InspectClass::request_shorter_than_stream;
case LaneFailure::request_longer: return InspectClass::request_longer_than_stream;
case LaneFailure::truncated: return InspectClass::truncated_in_fragment;
case LaneFailure::no_progress: return InspectClass::cycle_limit;
}
return InspectClass::cycle_limit;
}

inline const char* inspect_class_name(InspectClass cls) {
switch (cls) {
case InspectClass::ok: return "ok";
case InspectClass::bad_arguments: return "bad_arguments";
case InspectClass::unsupported_format: return "unsupported_format";
case InspectClass::destination_misaligned: return "destination_misaligned";
case InspectClass::header_invalid: return "header_invalid";
case InspectClass::fragment_not_home: return "fragment_not_home";
case InspectClass::end_state_invalid: return "end_state_invalid";
case InspectClass::trailing_input: return "trailing_input";
case InspectClass::request_shorter_than_stream: return "request_shorter_than_stream";
case InspectClass::request_longer_than_stream: return "request_longer_than_stream";
case InspectClass::truncated_in_fragment: return "truncated_in_fragment";
case InspectClass::cycle_limit: return "cycle_limit";
case InspectClass::control_code_unmapped: return "control_code_unmapped";
}
return "unknown";
}

// Decodes the request like decode_bcpack would and reports the class. For a stream that decodes it
// returns ok with the bytes the lanes took. Allocates a scratch destination: call it on failures only.
inline InspectResult inspect_bcpack(const unsigned char* input, size_t input_size, unsigned bc_format,
size_t output_size) {
InspectResult result;
if (!input || input_size == 0 || output_size == 0 || output_size > inspect_max_output) return result;
const InspectLayout* layout = inspect_layout(bc_format);
if (!layout) {
result.cls = InspectClass::unsupported_format;
return result;
}
if (output_size % layout->block_bytes != 0) {
result.cls = InspectClass::destination_misaligned;
return result;
}
const size_t num_blocks = output_size / layout->block_bytes;
LaneReport report;
std::vector<uint16_t> symbols;
const bool lanes_ok = decode_lanes(input, input_size, layout->alphabet_bits, layout->lanes,
num_blocks * layout->symbols_per_block, symbols, &report);
result.bytes_read = report.bytes_read;
result.min_symbols_per_lane = report.min_symbols;
result.max_symbols_per_lane = report.max_symbols;
result.target_symbols_per_lane = report.target_symbols;
if (!lanes_ok) {
result.cls = inspect_class(report.failure);
return result;
}
result.cls = InspectClass::ok;
if (bc_format == 7 || bc_format == 6) {
std::vector<unsigned char> scratch(output_size);
const bool blocks_ok = bc_format == 7 ? decode_bc7_blocks(symbols, num_blocks, scratch.data(), scratch.size())
: decode_bc6_blocks(symbols, num_blocks, scratch.data(), scratch.size());
if (!blocks_ok) result.cls = InspectClass::control_code_unmapped;
}
return result;
}

} // namespace x2w_bcpack
Loading
Loading