Parses the constant section and populates the constant table
| 725 | |
| 726 | /// Parses the constant section and populates the constant table |
| 727 | static LogicalResult |
| 728 | parseConstantSection(ArrayRef<uint8_t> payload, |
| 729 | std::vector<ArrayRef<uint8_t>> &constants, |
| 730 | MLIRContext &context) { |
| 731 | EncodingReader reader(payload, context); |
| 732 | uint64_t numConstants; |
| 733 | if (failed(reader.readVarInt(numConstants, |
| 734 | std::numeric_limits<uint32_t>::max() - 1))) |
| 735 | return failure(); |
| 736 | // Handle empty constant section case |
| 737 | if (numConstants == 0) |
| 738 | return success(); |
| 739 | // Ensure 8-byte alignment for the start indices array |
| 740 | if (failed(reader.skipPadding(alignof(uint64_t)))) |
| 741 | return failure(); |
| 742 | // Check if we have enough data to read the indices |
| 743 | if (reader.remaining() / sizeof(uint64_t) < numConstants) |
| 744 | return reader.emitError() << "insufficient data for constant indices"; |
| 745 | |
| 746 | // Read constant start indices as a contiguous array |
| 747 | const uint64_t *startIndicesPtr = |
| 748 | reinterpret_cast<const uint64_t *>(reader.getCurrentPtr()); |
| 749 | if (!startIndicesPtr) |
| 750 | return failure(); |
| 751 | |
| 752 | ArrayRef<uint64_t> constantStartIndices(startIndicesPtr, numConstants); |
| 753 | if (failed(reader.skip(constantStartIndices.size() * sizeof(uint64_t)))) |
| 754 | return failure(); |
| 755 | ArrayRef<uint8_t> constantData = payload.slice(reader.currentOffset()); |
| 756 | // Populate constants based on constantStartIndices |
| 757 | constants.reserve(numConstants); |
| 758 | for (uint64_t i = 0; i < numConstants; ++i) { |
| 759 | uint64_t start = constantStartIndices[i]; |
| 760 | uint64_t end = (i + 1 < numConstants) ? constantStartIndices[i + 1] |
| 761 | : constantData.size(); |
| 762 | if (end < start) |
| 763 | return reader.emitError() |
| 764 | << "invalid constant start indices: end (" << end |
| 765 | << ") is less than start (" << start << ") for constant " << i; |
| 766 | size_t constantSize = end - start; |
| 767 | if (constantSize + start > constantData.size()) |
| 768 | return reader.emitError() |
| 769 | << "constant " << i << " extends beyond available data: " |
| 770 | << "size=" << constantSize << ", start=" << start |
| 771 | << ", total data size=" << constantData.size(); |
| 772 | constants.push_back(constantData.slice(start, constantSize)); |
| 773 | } |
| 774 | return success(); |
| 775 | } |
| 776 | |
| 777 | namespace { |
| 778 |
no test coverage detected