Fix a mem corruption in ANGLE translator.

Basically outside TCompile::compile(), the global parse context is
invalid, and should never be queried.

BUG=angle:568
TEST=webgl conformance tests, no crash

Change-Id: I5573ce2bf3bf838ab24f59dda00948f60a0b023d
Reviewed-on: https://chromium-review.googlesource.com/197178
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Kenneth Russell <kbr@chromium.org>
Tested-by: Zhenyao Mo <zmo@chromium.org>
diff --git a/src/compiler/translator/Compiler.cpp b/src/compiler/translator/Compiler.cpp
index 36b313e..ca459b3 100644
--- a/src/compiler/translator/Compiler.cpp
+++ b/src/compiler/translator/Compiler.cpp
@@ -28,12 +28,11 @@
      return spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC;
 }
 
-size_t GetGlobalMaxTokenSize()
+size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
 {
-    TParseContext *parseContext = GetGlobalParseContext();
     // WebGL defines a max token legnth of 256, while ES2 leaves max token
     // size undefined. ES3 defines a max size of 1024 characters.
-    if (IsWebGLBasedSpec(parseContext->shaderSpec))
+    if (IsWebGLBasedSpec(spec))
     {
         return 256;
     }
@@ -261,7 +260,7 @@
 
     // Cleanup memory.
     intermediate.remove(parseContext.treeRoot);
-
+    SetGlobalParseContext(NULL);
     return success;
 }
 
diff --git a/src/compiler/translator/ShHandle.h b/src/compiler/translator/ShHandle.h
index 5991c5e..da522f1 100644
--- a/src/compiler/translator/ShHandle.h
+++ b/src/compiler/translator/ShHandle.h
@@ -75,10 +75,10 @@
     ShHashFunction64 getHashFunction() const { return hashFunction; }
     NameMap& getNameMap() { return nameMap; }
     TSymbolTable& getSymbolTable() { return symbolTable; }
+    ShShaderSpec getShaderSpec() const { return shaderSpec; }
 
 protected:
     ShShaderType getShaderType() const { return shaderType; }
-    ShShaderSpec getShaderSpec() const { return shaderSpec; }
     // Initialize symbol-table with built-in symbols.
     bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
     // Clears the results from the previous compilation.
diff --git a/src/compiler/translator/ShaderLang.cpp b/src/compiler/translator/ShaderLang.cpp
index db37d21..b98c371 100644
--- a/src/compiler/translator/ShaderLang.cpp
+++ b/src/compiler/translator/ShaderLang.cpp
@@ -187,27 +187,27 @@
         *params = compiler->getUniforms().size();
         break;
     case SH_ACTIVE_UNIFORM_MAX_LENGTH:
-        *params = 1 + GetGlobalMaxTokenSize();
+        *params = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec());
         break;
     case SH_ACTIVE_ATTRIBUTES:
         *params = compiler->getAttribs().size();
         break;
     case SH_ACTIVE_ATTRIBUTE_MAX_LENGTH:
-        *params = 1 + GetGlobalMaxTokenSize();
+        *params = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec());
         break;
     case SH_VARYINGS:
         *params = compiler->getVaryings().size();
         break;
     case SH_VARYING_MAX_LENGTH:
-        *params = 1 + GetGlobalMaxTokenSize();
+        *params = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec());
         break;
     case SH_MAPPED_NAME_MAX_LENGTH:
         // Use longer length than MAX_SHORTENED_IDENTIFIER_SIZE to
         // handle array and struct dereferences.
-        *params = 1 + GetGlobalMaxTokenSize();
+        *params = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec());
         break;
     case SH_NAME_MAX_LENGTH:
-        *params = 1 + GetGlobalMaxTokenSize();
+        *params = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec());
         break;
     case SH_HASHED_NAME_MAX_LENGTH:
         if (compiler->getHashFunction() == NULL) {
@@ -315,14 +315,14 @@
     // This size must match that queried by
     // SH_ACTIVE_UNIFORM_MAX_LENGTH, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, SH_VARYING_MAX_LENGTH
     // in ShGetInfo, below.
-    size_t variableLength = 1 + GetGlobalMaxTokenSize();
+    size_t variableLength = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec());
     ASSERT(checkVariableMaxLengths(handle, variableLength));
     strncpy(name, varInfo.name.c_str(), variableLength);
     name[variableLength - 1] = 0;
     if (mappedName) {
         // This size must match that queried by
         // SH_MAPPED_NAME_MAX_LENGTH in ShGetInfo, below.
-        size_t maxMappedNameLength = 1 + GetGlobalMaxTokenSize();
+        size_t maxMappedNameLength = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec());
         ASSERT(checkMappedNameMaxLength(handle, maxMappedNameLength));
         strncpy(mappedName, varInfo.mappedName.c_str(), maxMappedNameLength);
         mappedName[maxMappedNameLength - 1] = 0;
diff --git a/src/compiler/translator/glslang.l b/src/compiler/translator/glslang.l
index 8eb49a2..518b78d 100644
--- a/src/compiler/translator/glslang.l
+++ b/src/compiler/translator/glslang.l
@@ -551,7 +551,7 @@
     if (context->fragmentPrecisionHigh)
         context->preprocessor.predefineMacro("GL_FRAGMENT_PRECISION_HIGH", 1);
 
-    context->preprocessor.setMaxTokenSize(GetGlobalMaxTokenSize());
+    context->preprocessor.setMaxTokenSize(GetGlobalMaxTokenSize(context->shaderSpec));
 
     return 0;
 }
diff --git a/src/compiler/translator/glslang_lex.cpp b/src/compiler/translator/glslang_lex.cpp
index 5ec2e6b..5cfbba6 100644
--- a/src/compiler/translator/glslang_lex.cpp
+++ b/src/compiler/translator/glslang_lex.cpp
@@ -3353,7 +3353,7 @@
     if (context->fragmentPrecisionHigh)
         context->preprocessor.predefineMacro("GL_FRAGMENT_PRECISION_HIGH", 1);
 
-    context->preprocessor.setMaxTokenSize(GetGlobalMaxTokenSize());
+    context->preprocessor.setMaxTokenSize(GetGlobalMaxTokenSize(context->shaderSpec));
 
     return 0;
 }
diff --git a/src/compiler/translator/length_limits.h b/src/compiler/translator/length_limits.h
index fd90584..df70ee5 100644
--- a/src/compiler/translator/length_limits.h
+++ b/src/compiler/translator/length_limits.h
@@ -11,9 +11,11 @@
 #if !defined(__LENGTH_LIMITS_H)
 #define __LENGTH_LIMITS_H 1
 
+#include "GLSLANG/ShaderLang.h"
+
 // These constants are factored out from the rest of the headers to
 // make it easier to reference them from the compiler sources.
 
-size_t GetGlobalMaxTokenSize();
+size_t GetGlobalMaxTokenSize(ShShaderSpec spec);
 
 #endif // !(defined(__LENGTH_LIMITS_H)