From b69f8b201ba3767020d33a7ebe066466f00d4223 Mon Sep 17 00:00:00 2001 From: Kenneth Russel Date: Tue, 3 Jan 2006 02:36:17 +0000 Subject: Added checks on number of remaining elements and bytes for Buffers and arrays passed to certain APIs. Default is to not emit such range checks. The checks are currently most fully implemented for image- and texture-related APIs. Verified with debugging code and with demos that all textures used in demos are properly checked. git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/trunk@509 232f8b59-042b-4e1e-8c03-345bb8c30851 --- .../com/sun/gluegen/JavaMethodBindingEmitter.java | 68 ++++++++++++++++++++-- 1 file changed, 62 insertions(+), 6 deletions(-) (limited to 'src/classes/com/sun/gluegen/JavaMethodBindingEmitter.java') diff --git a/src/classes/com/sun/gluegen/JavaMethodBindingEmitter.java b/src/classes/com/sun/gluegen/JavaMethodBindingEmitter.java index 5a7e065f7..7e08977a6 100644 --- a/src/classes/com/sun/gluegen/JavaMethodBindingEmitter.java +++ b/src/classes/com/sun/gluegen/JavaMethodBindingEmitter.java @@ -87,6 +87,10 @@ public class JavaMethodBindingEmitter extends FunctionEmitter // number of elements of the returned array. private String returnedArrayLengthExpression; + // Range-check expressions for various Buffer arguments + private Map/**/ rangeCheckExpressions; + private Map/**/ byteRangeCheckExpressions; + public JavaMethodBindingEmitter(MethodBinding binding, PrintWriter output, String runtimeExceptionType, @@ -126,9 +130,11 @@ public class JavaMethodBindingEmitter extends FunctionEmitter forDirectBufferImplementation = arg.forDirectBufferImplementation; forIndirectBufferAndArrayImplementation = arg.forIndirectBufferAndArrayImplementation; isUnimplemented = arg.isUnimplemented; - returnedArrayLengthExpression = arg.returnedArrayLengthExpression; prologue = arg.prologue; epilogue = arg.epilogue; + returnedArrayLengthExpression = arg.returnedArrayLengthExpression; + rangeCheckExpressions = arg.rangeCheckExpressions; + byteRangeCheckExpressions = arg.byteRangeCheckExpressions; } public final MethodBinding getBinding() { return binding; } @@ -193,6 +199,14 @@ public class JavaMethodBindingEmitter extends FunctionEmitter this.forImplementingMethodCall = impl; } + public void setRangeCheckExpressions(Map/**/ rangeChecks) { + this.rangeCheckExpressions = rangeChecks; + } + + public void setByteRangeCheckExpressions(Map/**/ rangeChecks) { + this.byteRangeCheckExpressions = rangeChecks; + } + protected void emitReturnType(PrintWriter writer) { writer.print(getReturnTypeString(false)); @@ -424,6 +438,44 @@ public class JavaMethodBindingEmitter extends FunctionEmitter } } } + + emitManuallySpecifiedRangeChecks(rangeCheckExpressions, false, writer); + emitManuallySpecifiedRangeChecks(byteRangeCheckExpressions, true, writer); + } + + protected void emitManuallySpecifiedRangeChecks(Map/**/ rangeChecks, + boolean inBytes, + PrintWriter writer) { + if (rangeChecks == null) { + return; + } + + // Check lengths of arrays and buffers with user-specified checks + for (Iterator iter = rangeChecks.keySet().iterator(); iter.hasNext(); ) { + Integer argNumBox = (Integer) iter.next(); + int argNum = argNumBox.intValue(); + JavaType type = binding.getJavaArgumentType(argNum); + String argName = getArgumentName(argNum); + if (type.isPrimitiveArray()) { + String offsetArg = offsetArgName(argNum); + if (inBytes) { + throw new RuntimeException("Can not specify RangeCheckBytes for primitive array arguments (failed on function " + + binding.getName() + ", argument " + argName + ")"); + } + writer.println(" BufferFactory.rangeCheck(" + argName + ", " + offsetArg + ", " + + new MessageFormat((String) rangeChecks.get(argNumBox)).format(argumentNameArray()) + + ");"); + } else if (!type.isPrimitive()) { + // Assume it's a Buffer + writer.print(" BufferFactory.rangeCheck"); + if (inBytes) { + writer.print("Bytes"); + } + writer.println("(" + argName + ", " + + new MessageFormat((String) rangeChecks.get(argNumBox)).format(argumentNameArray()) + + ");"); + } + } } protected void emitCall(MethodBinding binding, PrintWriter writer, boolean direct) { @@ -601,11 +653,7 @@ public class JavaMethodBindingEmitter extends FunctionEmitter writer.print(" return " + returnType.getName() + ".create(_res.order(ByteOrder.nativeOrder()))"); } else { writer.println(" _res.order(ByteOrder.nativeOrder());"); - String[] argumentNames = new String[binding.getNumArguments()]; - for (int i = 0; i < binding.getNumArguments(); i++) { - argumentNames[i] = getArgumentName(i); - } - String expr = new MessageFormat(fmt).format(argumentNames); + String expr = new MessageFormat(fmt).format(argumentNameArray()); PointerType cReturnTypePointer = binding.getCReturnType().asPointer(); CompoundType cReturnType = null; if (cReturnTypePointer != null) { @@ -648,6 +696,14 @@ public class JavaMethodBindingEmitter extends FunctionEmitter } } + protected String[] argumentNameArray() { + String[] argumentNames = new String[binding.getNumArguments()]; + for (int i = 0; i < binding.getNumArguments(); i++) { + argumentNames[i] = getArgumentName(i); + } + return argumentNames; + } + public static String javaThisArgumentName() { return "jthis0"; } -- cgit v1.2.3