From 8adc166579a5bbd579f1f2c95270dfc90f515b3b Mon Sep 17 00:00:00 2001 From: jfrijters Date: Thu, 21 Jun 2007 12:04:27 +0000 Subject: [PATCH] Made LVT handling more robust against bogus entries. --- runtime/compiler.cs | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/runtime/compiler.cs b/runtime/compiler.cs index 0631bc36..31c429ec 100644 --- a/runtime/compiler.cs +++ b/runtime/compiler.cs @@ -1234,28 +1234,40 @@ class Compiler for(int i = 0; i < lvt.Length; i++) { // TODO validate the contents of the LVT entry - int index = FindPcIndex(lvt[i].start_pc); - if(index > 0) + int startIndex = SafeFindPcIndex(lvt[i].start_pc); + if(startIndex > 0) { // NOTE javac (correctly) sets start_pc of the LVT entry to the instruction // following the store that first initializes the local, so we have to // detect that case and adjust our local scope (because we'll be creating // the local when we encounter the first store). - LocalVar v = ma.GetLocalVar(index - 1); + LocalVar v = ma.GetLocalVar(startIndex - 1); if(v != null && v.local == lvt[i].index) { - index--; + startIndex--; } } - scope[index]++; int end = lvt[i].start_pc + lvt[i].length; + int endIndex; if(end == m.Instructions[m.Instructions.Length - 1].PC) { - scope[m.Instructions.Length - 1]--; + endIndex = m.Instructions.Length - 1; } else { - scope[FindPcIndex(end)]--; + endIndex = SafeFindPcIndex(end); + } + if(startIndex != -1 && endIndex != -1) + { + scope[startIndex]++; + scope[endIndex]--; + } + else + { + // the LVT range is invalid, but we need to have a scope for the variable, + // so we create an artificial scope that spans the method + scope[0]++; + scope[m.Instructions.Length - 1]--; } } } @@ -3618,6 +3630,15 @@ class Compiler return m.PcIndexMap[target]; } + private int SafeFindPcIndex(int target) + { + if(target < 0 || target >= m.PcIndexMap.Length) + { + return -1; + } + return m.PcIndexMap[target]; + } + private LocalVar LoadLocal(ClassFile.Method.Instruction instr) { LocalVar v = ma.GetLocalVar(FindPcIndex(instr.PC));