Tint/GLSL: fix null ptr deref in Texture1D -> 2D.

Bug: 1405676
Change-Id: If6edb0ba2b6c1ddd5d75421d234e168297e1b622
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/116700
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Stephen White <senorblanco@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/transform/texture_1d_to_2d.cc b/src/tint/transform/texture_1d_to_2d.cc
index 0e19d02..85cd512 100644
--- a/src/tint/transform/texture_1d_to_2d.cc
+++ b/src/tint/transform/texture_1d_to_2d.cc
@@ -126,7 +126,10 @@
                 return nullptr;
             }
             const auto& signature = builtin->Signature();
-            auto texture = signature.Parameter(sem::ParameterUsage::kTexture);
+            auto* texture = signature.Parameter(sem::ParameterUsage::kTexture);
+            if (!texture) {
+                return nullptr;
+            }
             auto* tex = texture->Type()->As<type::Texture>();
             if (tex->dim() != ast::TextureDimension::k1d) {
                 return nullptr;
diff --git a/src/tint/transform/texture_1d_to_2d_test.cc b/src/tint/transform/texture_1d_to_2d_test.cc
index 83a12bd..687988c 100644
--- a/src/tint/transform/texture_1d_to_2d_test.cc
+++ b/src/tint/transform/texture_1d_to_2d_test.cc
@@ -275,5 +275,30 @@
     EXPECT_EQ(expect, str(got));
 }
 
+TEST_F(Texture1DTo2DTest, TextureAndNonTextureBuiltin) {
+    auto* src = R"(
+@group(0) @binding(0) var tex : texture_1d<i32>;
+
+fn d() {
+  textureLoad(tex, 1, 0);
+  let l = sin(3.0);
+}
+)";
+
+    auto* expect = R"(
+@group(0) @binding(0) var tex : texture_2d<i32>;
+
+fn d() {
+  textureLoad(tex, vec2<i32>(1, 0), 0);
+  let l = sin(3.0);
+}
+)";
+
+    DataMap data;
+    auto got = Run<Texture1DTo2D>(src, data);
+
+    EXPECT_EQ(expect, str(got));
+}
+
 }  // namespace
 }  // namespace tint::transform
diff --git a/test/tint/bug/chromium/1405676.wgsl b/test/tint/bug/chromium/1405676.wgsl
new file mode 100644
index 0000000..38f66a4
--- /dev/null
+++ b/test/tint/bug/chromium/1405676.wgsl
@@ -0,0 +1,6 @@
+@group(0) @binding(0) var arg_0 : texture_1d<i32>;
+
+fn d() {
+  textureLoad(arg_0, 1, 0);
+  let l = sin(3.0);
+}
diff --git a/test/tint/bug/chromium/1405676.wgsl.expected.dxc.hlsl b/test/tint/bug/chromium/1405676.wgsl.expected.dxc.hlsl
new file mode 100644
index 0000000..4b4cf12
--- /dev/null
+++ b/test/tint/bug/chromium/1405676.wgsl.expected.dxc.hlsl
@@ -0,0 +1,11 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+Texture1D<int4> arg_0 : register(t0, space0);
+
+void d() {
+  arg_0.Load(int2(1, 0));
+  const float l = 0.141120002f;
+}
diff --git a/test/tint/bug/chromium/1405676.wgsl.expected.fxc.hlsl b/test/tint/bug/chromium/1405676.wgsl.expected.fxc.hlsl
new file mode 100644
index 0000000..4b4cf12
--- /dev/null
+++ b/test/tint/bug/chromium/1405676.wgsl.expected.fxc.hlsl
@@ -0,0 +1,11 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+Texture1D<int4> arg_0 : register(t0, space0);
+
+void d() {
+  arg_0.Load(int2(1, 0));
+  const float l = 0.141120002f;
+}
diff --git a/test/tint/bug/chromium/1405676.wgsl.expected.glsl b/test/tint/bug/chromium/1405676.wgsl.expected.glsl
new file mode 100644
index 0000000..6a46c61
--- /dev/null
+++ b/test/tint/bug/chromium/1405676.wgsl.expected.glsl
@@ -0,0 +1,12 @@
+#version 310 es
+
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void unused_entry_point() {
+  return;
+}
+uniform highp isampler2D arg_0_1;
+void d() {
+  texelFetch(arg_0_1, ivec2(1, 0), 0);
+  float l = 0.141120002f;
+}
+
diff --git a/test/tint/bug/chromium/1405676.wgsl.expected.msl b/test/tint/bug/chromium/1405676.wgsl.expected.msl
new file mode 100644
index 0000000..b32437b
--- /dev/null
+++ b/test/tint/bug/chromium/1405676.wgsl.expected.msl
@@ -0,0 +1,8 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void d(texture1d<int, access::sample> tint_symbol) {
+  tint_symbol.read(uint(1), 0);
+  float const l = 0.141120002f;
+}
+
diff --git a/test/tint/bug/chromium/1405676.wgsl.expected.spvasm b/test/tint/bug/chromium/1405676.wgsl.expected.spvasm
new file mode 100644
index 0000000..9394063
--- /dev/null
+++ b/test/tint/bug/chromium/1405676.wgsl.expected.spvasm
@@ -0,0 +1,36 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 18
+; Schema: 0
+               OpCapability Shader
+               OpCapability Sampled1D
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %arg_0 "arg_0"
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %d "d"
+               OpDecorate %arg_0 DescriptorSet 0
+               OpDecorate %arg_0 Binding 0
+        %int = OpTypeInt 32 1
+          %3 = OpTypeImage %int 1D 0 0 0 1 Unknown
+%_ptr_UniformConstant_3 = OpTypePointer UniformConstant %3
+      %arg_0 = OpVariable %_ptr_UniformConstant_3 UniformConstant
+       %void = OpTypeVoid
+          %5 = OpTypeFunction %void
+      %v4int = OpTypeVector %int 4
+      %int_1 = OpConstant %int 1
+         %15 = OpConstantNull %int
+      %float = OpTypeFloat 32
+%float_0_141120002 = OpConstant %float 0.141120002
+%unused_entry_point = OpFunction %void None %5
+          %8 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %d = OpFunction %void None %5
+         %10 = OpLabel
+         %13 = OpLoad %3 %arg_0
+         %11 = OpImageFetch %v4int %13 %int_1 Lod %15
+               OpReturn
+               OpFunctionEnd
diff --git a/test/tint/bug/chromium/1405676.wgsl.expected.wgsl b/test/tint/bug/chromium/1405676.wgsl.expected.wgsl
new file mode 100644
index 0000000..38f66a4
--- /dev/null
+++ b/test/tint/bug/chromium/1405676.wgsl.expected.wgsl
@@ -0,0 +1,6 @@
+@group(0) @binding(0) var arg_0 : texture_1d<i32>;
+
+fn d() {
+  textureLoad(arg_0, 1, 0);
+  let l = sin(3.0);
+}