diff --git a/tools/tests/mali_compat_spirv_test.c b/tools/tests/mali_compat_spirv_test.c index 57dce75d75..690ed5e9cd 100644 --- a/tools/tests/mali_compat_spirv_test.c +++ b/tools/tests/mali_compat_spirv_test.c @@ -365,12 +365,18 @@ static void check_pipelines(void) driver_calls = 0; failed_allocations = 0; fail_allocations = true; - VkResult compute_result = CreateComputePipelines(device, VK_NULL_HANDLE, 1, &compute, NULL, &pipeline); - VkResult graphics_result = CreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &graphics, NULL, &pipeline); + VkPipeline compute_outputs[2] = {(VkPipeline)(uintptr_t)1, (VkPipeline)(uintptr_t)2}; + VkPipeline graphics_outputs[2] = {(VkPipeline)(uintptr_t)1, (VkPipeline)(uintptr_t)2}; + VkResult compute_result = CreateComputePipelines(device, VK_NULL_HANDLE, 2, &compute, NULL, compute_outputs); + VkResult graphics_result = CreateGraphicsPipelines(device, VK_NULL_HANDLE, 2, &graphics, NULL, graphics_outputs); fail_allocations = false; check(failed_allocations > 0, "the allocations were made to fail"); check(compute_result == VK_ERROR_OUT_OF_HOST_MEMORY, "a compute pipeline without memory reports it"); check(graphics_result == VK_ERROR_OUT_OF_HOST_MEMORY, "a graphics pipeline without memory reports it"); + check(compute_outputs[0] == VK_NULL_HANDLE && compute_outputs[1] == VK_NULL_HANDLE, + "failed compute pipeline outputs are cleared"); + check(graphics_outputs[0] == VK_NULL_HANDLE && graphics_outputs[1] == VK_NULL_HANDLE, + "failed graphics pipeline outputs are cleared"); check(driver_calls == 0, "the driver is not called without memory"); pthread_mutex_destroy(&test_device.lock); } @@ -404,6 +410,38 @@ int main(int argc, char **argv) "a module selecting a structure is left alone"); check(!spirv_cap_version(nonsemantic, nonsemantic_count, &kept), "a module importing a NonSemantic set without SPV_KHR_non_semantic_info is left alone"); + uint32_t private_readonly[sizeof(zinklike) / sizeof(uint32_t) + 3]; + size_t declaration = 5; + while (OPCODE(zinklike[declaration]) != SpvOpTypeVoid) + declaration += LENGTH(zinklike[declaration]); + memcpy(private_readonly, zinklike, declaration * sizeof(uint32_t)); + private_readonly[declaration] = (3u << 16) | SpvOpDecorate; + private_readonly[declaration + 1] = 3; + private_readonly[declaration + 2] = SpvDecorationNonWritable; + memcpy(private_readonly + declaration + 3, zinklike + declaration, + (zinklike_count - declaration) * sizeof(uint32_t)); + check(!spirv_cap_version(private_readonly, zinklike_count + 3, &kept), + "a private NonWritable variable is left alone"); + if (argc > 6) { + struct spirv_code source = {.words = private_readonly, .count = zinklike_count + 3}; + write_module(argv[6], &source); + } + uint32_t converted[sizeof(zinklike) / sizeof(uint32_t) + 9]; + size_t function = declaration; + while (OPCODE(zinklike[function]) != SpvOpFunction) + function += LENGTH(zinklike[function]); + memcpy(converted, zinklike, function * sizeof(uint32_t)); + const uint32_t conversion[] = {(4u << 16) | SpvOpTypeInt, 23, 32, 0, + (5u << 16) | SpvOpSpecConstantOp, 23, 24, SpvOpUConvert, 15}; + memcpy(converted + function, conversion, sizeof(conversion)); + memcpy(converted + function + 9, zinklike + function, (zinklike_count - function) * sizeof(uint32_t)); + converted[3] = 25; + check(!spirv_cap_version(converted, zinklike_count + 9, &kept), + "a specialization UConvert without its extension is left alone"); + if (argc > 7) { + struct spirv_code source = {.words = converted, .count = zinklike_count + 9}; + write_module(argv[7], &source); + } check(kept.words == NULL, "no rejected module is handed back"); struct spirv_code extension = {0}; diff --git a/tools/tests/test_mali_compat_spirv.py b/tools/tests/test_mali_compat_spirv.py index 2ac6ea1b00..d9a33b60a9 100644 --- a/tools/tests/test_mali_compat_spirv.py +++ b/tools/tests/test_mali_compat_spirv.py @@ -37,13 +37,16 @@ class MaliCompatSpirvTest(unittest.TestCase): extension = Path(tmp) / "extension.spv" scalar = Path(tmp) / "scalar.spv" debug = Path(tmp) / "debug.spv" + private_readonly = Path(tmp) / "private-readonly.spv" + converted = Path(tmp) / "converted.spv" built = subprocess.run( [compiler, "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", "-UNDEBUG", "-pthread", "-Dcalloc=test_calloc", "-I", str(LAYER), "-I", str(vulkan), "-I", str(spirv), str(HARNESS), str(LAYER / "spirv.c"), str(LAYER / "pipeline.c"), "-o", str(program)], capture_output=True, text=True) self.assertEqual(built.returncode, 0, built.stdout + built.stderr) - ran = subprocess.run([str(program), str(module), str(guest), str(extension), str(scalar), str(debug)], + ran = subprocess.run([str(program), str(module), str(guest), str(extension), str(scalar), str(debug), + str(private_readonly), str(converted)], capture_output=True, text=True, timeout=30) self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr) self.assertIn("mali compat spirv ok", ran.stdout) @@ -55,6 +58,27 @@ class MaliCompatSpirvTest(unittest.TestCase): validator = shutil.which("spirv-val") if not validator: return + for original in (private_readonly, converted): + checked = subprocess.run([validator, "--target-env", "vulkan1.3", str(original)], + capture_output=True, text=True) + self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr) + downgraded = bytearray(original.read_bytes()) + downgraded[4:8] = (0x00010300).to_bytes(4, "little") + offset = 20 + while True: + instruction = int.from_bytes(downgraded[offset:offset + 4], "little") + length, opcode = instruction >> 16, instruction & 0xffff + if opcode == 15: + downgraded[offset:offset + 4] = (((length - 1) << 16) | opcode).to_bytes(4, "little") + del downgraded[offset + (length - 1) * 4:offset + length * 4] + break + offset += length * 4 + original.write_bytes(downgraded) + checked = subprocess.run([validator, "--target-env", "vulkan1.1", str(original)], + capture_output=True, text=True) + self.assertNotEqual(checked.returncode, 0, "the 1.3 form must be rejected") + expected = "NonWritable" if original == private_readonly else "UConvert" + self.assertIn(expected, checked.stdout + checked.stderr) for lowered in (module, guest, extension, scalar, debug): checked = subprocess.run([validator, "--target-env", "vulkan1.1", str(lowered)], capture_output=True, text=True) diff --git a/tools/venus/layer/pipeline.c b/tools/venus/layer/pipeline.c index 44d974425a..95c0ccd1ee 100644 --- a/tools/venus/layer/pipeline.c +++ b/tools/venus/layer/pipeline.c @@ -430,6 +430,8 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateGraphicsPipelines(VkDevice device, VkPipeli VkGraphicsPipelineCreateInfo *copies = calloc(count, sizeof(*copies)); struct pipeline_rewrite *rewrites = calloc(count, sizeof(*rewrites)); if (count && (!copies || !rewrites)) { + for (uint32_t i = 0; i < count; i++) + pipelines[i] = VK_NULL_HANDLE; free(copies); free(rewrites); return VK_ERROR_OUT_OF_HOST_MEMORY; @@ -454,6 +456,8 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateComputePipelines(VkDevice device, VkPipelin VkComputePipelineCreateInfo *copies = calloc(count, sizeof(*copies)); struct pipeline_rewrite *rewrites = calloc(count, sizeof(*rewrites)); if (count && (!copies || !rewrites)) { + for (uint32_t i = 0; i < count; i++) + pipelines[i] = VK_NULL_HANDLE; free(copies); free(rewrites); return VK_ERROR_OUT_OF_HOST_MEMORY; diff --git a/tools/venus/layer/spirv.c b/tools/venus/layer/spirv.c index 0691e1005e..65b37a8139 100644 --- a/tools/venus/layer/spirv.c +++ b/tools/venus/layer/spirv.c @@ -984,6 +984,15 @@ static bool available_in_1_3(const struct module *m, const struct newer_feature #define AVAILABLE(m, table, value) available_in_1_3(m, table, sizeof(table) / sizeof(table[0]), value) +static bool declares_capability(const struct module *m, uint32_t capability) +{ + for (size_t i = 5; i < m->first_declaration; i += LENGTH(m->w[i])) { + if (OPCODE(m->w[i]) == SpvOpCapability && LENGTH(m->w[i]) >= 2 && m->w[i + 1] == capability) + return true; + } + return false; +} + static uint32_t image_operands_at(uint32_t op) { switch (op) { @@ -1065,7 +1074,16 @@ static bool valid_in_1_3(const struct module *m, size_t i) case SpvOpDecorate: case SpvOpDecorateId: case SpvOpDecorateString: - return length >= 3 && AVAILABLE(m, newer_decorations, m->w[i + 2]); + if (length < 3 || !AVAILABLE(m, newer_decorations, m->w[i + 2])) + return false; + if (m->w[i + 2] == SpvDecorationNonWritable) { + size_t d = definition(m, m->w[i + 1]); + if (!d || (OPCODE(m->w[d]) == SpvOpVariable && + (LENGTH(m->w[d]) < 4 || m->w[d + 3] == SpvStorageClassPrivate || + m->w[d + 3] == SpvStorageClassFunction))) + return false; + } + return true; case SpvOpMemberDecorate: case SpvOpMemberDecorateString: return length >= 4 && AVAILABLE(m, newer_decorations, m->w[i + 3]); @@ -1073,6 +1091,10 @@ static bool valid_in_1_3(const struct module *m, size_t i) return length >= 4 && !(m->w[i + 3] & NEWER_LOOP_CONTROLS); case SpvOpSelect: return length >= 6 && selects_scalar_vector_or_pointer(m, m->w[i + 1]); + case SpvOpSpecConstantOp: + return length >= 5 && (m->w[i + 3] != SpvOpUConvert || + declares_capability(m, SpvCapabilityKernel) || + declares_extension(m, "SPV_AMD_gpu_shader_int16")); case SpvOpExtInstImport: return length >= 3 && (!names_prefix(&m->w[i + 2], length - 2, "NonSemantic.") || declares_extension(m, "SPV_KHR_non_semantic_info"));