Runtime effects: Detect calls to undefined functions

Previously, the CPU backend would catch this later (ByteCodeGenerator),
and the GPU backend would assert in expandFormatArgs (out-of-range
function index).

Change-Id: Ib84bf7c477ddcc9fd3091c5b106ebdd654e9a30b
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/276000
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp
index a2456ce..478e8dd 100644
--- a/src/sksl/SkSLIRGenerator.cpp
+++ b/src/sksl/SkSLIRGenerator.cpp
@@ -1784,6 +1784,11 @@
         fErrors.error(offset, msg);
         return nullptr;
     }
+    if (fKind == Program::kPipelineStage_Kind && !function.fDefined && !function.fBuiltin) {
+        String msg = "call to undefined function '" + function.fName + "'";
+        fErrors.error(offset, msg);
+        return nullptr;
+    }
     std::vector<const Type*> types;
     const Type* returnType;
     if (!function.determineFinalTypes(arguments, &types, &returnType)) {
diff --git a/tests/SkRuntimeEffectTest.cpp b/tests/SkRuntimeEffectTest.cpp
index 9bfaf8b..ef4f7ad 100644
--- a/tests/SkRuntimeEffectTest.cpp
+++ b/tests/SkRuntimeEffectTest.cpp
@@ -14,9 +14,10 @@
 
 #include <algorithm>
 
-DEF_TEST(SkRuntimeEffectInvalidInputs, r) {
-    auto test = [r](const char* hdr, const char* expected) {
-        SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) {}", hdr);
+DEF_TEST(SkRuntimeEffectInvalid, r) {
+    auto test = [r](const char* hdr, const char* body, const char* expected) {
+        SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
+                                      hdr, body);
         auto[effect, errorText] = SkRuntimeEffect::Make(src);
         REPORTER_ASSERT(r, !effect);
         REPORTER_ASSERT(r, errorText.contains(expected),
@@ -26,27 +27,29 @@
 
     // Features that are only allowed in .fp files (key, in uniform, ctype, when, tracked).
     // Ensure that these fail, and the error messages contain the relevant keyword.
-    test("layout(key) in bool Input;", "key");
-    test("in uniform float Input;", "in uniform");
-    test("layout(ctype=SkRect) float4 Input;", "ctype");
-    test("in bool Flag; layout(when=Flag) uniform float Input;", "when");
-    test("layout(tracked) uniform float Input;", "tracked");
+    test("layout(key) in bool Input;", "", "key");
+    test("in uniform float Input;", "", "in uniform");
+    test("layout(ctype=SkRect) float4 Input;", "", "ctype");
+    test("in bool Flag; layout(when=Flag) uniform float Input;", "", "when");
+    test("layout(tracked) uniform float Input;", "", "tracked");
 
     // Runtime SkSL supports a limited set of uniform types. No samplers, for example:
-    test("uniform sampler2D s;", "sampler2D");
+    test("uniform sampler2D s;", "", "sampler2D");
 
     // 'in' variables can't be arrays
-    test("in int Input[2];", "array");
+    test("in int Input[2];", "", "array");
 
     // Type specific restrictions:
 
     // 'bool', 'int' can't be 'uniform'
-    test("uniform bool Input;", "'uniform'");
-    test("uniform int Input;", "'uniform'");
+    test("uniform bool Input;", "", "'uniform'");
+    test("uniform int Input;", "", "'uniform'");
 
     // vector and matrix types can't be 'in'
-    test("in float2 Input;", "'in'");
-    test("in half3x3 Input;", "'in'");
+    test("in float2 Input;", "", "'in'");
+    test("in half3x3 Input;", "", "'in'");
+
+    test("half missing();", "color.r = missing();", "undefined function");
 }
 
 // Our packing rules and unit test code here relies on this: