Skip to content

Commit 7f02cb7

Browse files
committed
[spirv-ll] Fix segfault on invalid Function storage classes
This is invalid SPIR-V (unfortunately generated by DPC++) but that doesn't mean we should segfault. Instead, report an error to the user.
1 parent aac1273 commit 7f02cb7

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

modules/compiler/spirv-ll/source/builder_core.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -2583,6 +2583,11 @@ cargo::optional<Error> Builder::create<OpVariable>(const OpVariable *op) {
25832583
case spv::StorageClassFunction: {
25842584
// Visible only within the declaring function of the current
25852585
// invocation. Regular function memory.
2586+
if (!IRBuilder.GetInsertBlock()) {
2587+
return Error{
2588+
"invalid SPIR-V: variables can not have a function[7] "
2589+
"storage class outside of a function"};
2590+
}
25862591
value = IRBuilder.CreateAlloca(varTy);
25872592
value->setName(name);
25882593
if (initializer) {

modules/compiler/spirv-ll/test/spvasm/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,7 @@ set(SPVASM_FILES
23622362
op_opencl_arg_md.spvasm
23632363
unsupported_capability.spvasm
23642364
op_get_kernel_preferred_work_group_size_multiple.spvasm
2365+
invalid_storage_class.spvasm
23652366
)
23662367

23672368
if(SpirvAsVersionYear GREATER 2019)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; Copyright (C) Codeplay Software Limited
2+
;
3+
; Licensed under the Apache License, Version 2.0 (the "License") with LLVM
4+
; Exceptions; you may not use this file except in compliance with the License.
5+
; You may obtain a copy of the License at
6+
;
7+
; https://github.com/codeplaysoftware/oneapi-construction-kit/blob/main/LICENSE.txt
8+
;
9+
; Unless required by applicable law or agreed to in writing, software
10+
; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
; License for the specific language governing permissions and limitations
13+
; under the License.
14+
;
15+
; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
16+
17+
; It is invalid to use the 'Function' storage class outside of a function.
18+
; Ensure we gracefully fail when we encounter this.
19+
20+
; RUN: %if online-spirv-as %{ spirv-as --target-env %spv_tgt_env -o %spv_file_s %s %}
21+
; RUN: not spirv-ll-tool -a OpenCL -b 64 %spv_file_s 2>&1 | FileCheck %s
22+
23+
; CHECK: variables can not have a function[7] storage class outside of a function
24+
25+
OpCapability Addresses
26+
OpCapability Linkage
27+
OpCapability Kernel
28+
OpCapability Int64
29+
OpCapability Int8
30+
%1 = OpExtInstImport "OpenCL.std"
31+
OpMemoryModel Physical64 OpenCL
32+
OpEntryPoint Kernel %54 "_ZTSZN24kernel_bundle_spec_constL22CATCH2_INTERNAL_TEST_4EvE13simple_kernel"
33+
OpSource OpenCL_CPP 100000
34+
%uint = OpTypeInt 32 0
35+
%_ptr_Function_uint = OpTypePointer Function %uint
36+
%my_ptr = OpVariable %_ptr_Function_uint Function

0 commit comments

Comments
 (0)