|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// ---------------------------------------------------------------------------- |
| 3 | +// Copyright 2025-2026 Arm Limited |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 6 | +// use this file except in compliance with the License. You may obtain a copy |
| 7 | +// of the License at: |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +// License for the specific language governing permissions and limitations |
| 15 | +// under the License. |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | + |
| 18 | +/** |
| 19 | + * @brief Unit tests for the vectorized SIMD functionality. |
| 20 | + */ |
| 21 | + |
| 22 | +#include <limits> |
| 23 | + |
| 24 | +#include "gtest/gtest.h" |
| 25 | + |
| 26 | +#include "../astcenc.h" |
| 27 | + |
| 28 | +namespace astcenc |
| 29 | +{ |
| 30 | + |
| 31 | +/** @brief Input overflow tests for xy * z. */ |
| 32 | +TEST(compress, overflow_in_z) |
| 33 | +{ |
| 34 | + astcenc_error status; |
| 35 | + astcenc_config config; |
| 36 | + astcenc_context* context; |
| 37 | + |
| 38 | + static const astcenc_swizzle swizzle { |
| 39 | + ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A |
| 40 | + }; |
| 41 | + |
| 42 | + astcenc_config_init(ASTCENC_PRF_LDR, 4, 4, 1, ASTCENC_PRE_MEDIUM, 0, &config); |
| 43 | + status = astcenc_context_alloc(&config, 1, &context, nullptr); |
| 44 | + EXPECT_EQ(status, ASTCENC_SUCCESS); |
| 45 | + |
| 46 | + // Arrays are too short, but should never be touched |
| 47 | + uint8_t data[1]; |
| 48 | + uint8_t input[1]; |
| 49 | + |
| 50 | + astcenc_image image; |
| 51 | + // x * y always fits in 64-bit size_t, but xy * z will overflow |
| 52 | + image.dim_x = 0xFFFFFFFFu; |
| 53 | + image.dim_y = 0xFFFFFFFFu; |
| 54 | + image.dim_z = 0xFFFFFFFFu; |
| 55 | + image.data_type = ASTCENC_TYPE_U8; |
| 56 | + uint8_t* slices = input; |
| 57 | + image.data = reinterpret_cast<void**>(&slices); |
| 58 | + |
| 59 | + status = astcenc_compress_image(context, &image, &swizzle, data, -1, 0); |
| 60 | + EXPECT_EQ(status, ASTCENC_ERR_BAD_PARAM); |
| 61 | + |
| 62 | + astcenc_context_free(context); |
| 63 | +} |
| 64 | + |
| 65 | +/** @brief Input overflow tests for xyz * 16. */ |
| 66 | +TEST(compress, overflow_in_16) |
| 67 | +{ |
| 68 | + astcenc_error status; |
| 69 | + astcenc_config config; |
| 70 | + astcenc_context* context; |
| 71 | + |
| 72 | + static const astcenc_swizzle swizzle { |
| 73 | + ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A |
| 74 | + }; |
| 75 | + |
| 76 | + astcenc_config_init(ASTCENC_PRF_LDR, 4, 4, 1, ASTCENC_PRE_MEDIUM, 0, &config); |
| 77 | + status = astcenc_context_alloc(&config, 1, &context, nullptr); |
| 78 | + EXPECT_EQ(status, ASTCENC_SUCCESS); |
| 79 | + |
| 80 | + // Arrays are too short, but should never be touched |
| 81 | + uint8_t data[1]; |
| 82 | + uint8_t input[1]; |
| 83 | + |
| 84 | + astcenc_image image; |
| 85 | + // xyz (in blocks) always fits in 64-bit size_t, but xyz * 16 will overflow |
| 86 | + image.dim_x = 0x80000000u; |
| 87 | + image.dim_y = 0x80000000u; |
| 88 | + image.dim_z = 0x00000010u; |
| 89 | + image.data_type = ASTCENC_TYPE_U8; |
| 90 | + uint8_t* slices = input; |
| 91 | + image.data = reinterpret_cast<void**>(&slices); |
| 92 | + |
| 93 | + status = astcenc_compress_image(context, &image, &swizzle, data, -1, 0); |
| 94 | + EXPECT_EQ(status, ASTCENC_ERR_BAD_PARAM); |
| 95 | + |
| 96 | + astcenc_context_free(context); |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +/** @brief Input data buffer overrun tests. */ |
| 101 | +TEST(compress, data_buffer_exceeded) |
| 102 | +{ |
| 103 | + astcenc_error status; |
| 104 | + astcenc_config config; |
| 105 | + astcenc_context* context; |
| 106 | + |
| 107 | + static const astcenc_swizzle swizzle { |
| 108 | + ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A |
| 109 | + }; |
| 110 | + |
| 111 | + astcenc_config_init(ASTCENC_PRF_LDR, 4, 4, 1, ASTCENC_PRE_MEDIUM, 0, &config); |
| 112 | + status = astcenc_context_alloc(&config, 1, &context, nullptr); |
| 113 | + EXPECT_EQ(status, ASTCENC_SUCCESS); |
| 114 | + |
| 115 | + // Arrays are too short, but should never be touched |
| 116 | + uint8_t data[1]; |
| 117 | + uint8_t input[1]; |
| 118 | + |
| 119 | + astcenc_image image; |
| 120 | + image.dim_x = 0x4u; |
| 121 | + image.dim_y = 0x4u; |
| 122 | + image.dim_z = 0x1u; |
| 123 | + image.data_type = ASTCENC_TYPE_U8; |
| 124 | + uint8_t* slices = input; |
| 125 | + image.data = reinterpret_cast<void**>(&slices); |
| 126 | + |
| 127 | + // Data size is 1 byte too short so this should error |
| 128 | + status = astcenc_compress_image(context, &image, &swizzle, data, 15, 0); |
| 129 | + EXPECT_EQ(status, ASTCENC_ERR_OUT_OF_MEM); |
| 130 | + |
| 131 | + astcenc_context_free(context); |
| 132 | +} |
| 133 | + |
| 134 | +} |
0 commit comments