summaryrefslogtreecommitdiff
path: root/testsuite
diff options
context:
space:
mode:
authorVincent Penquerc'h <vincent.penquerch@collabora.co.uk>2014-05-01 15:47:41 +0100
committerVincent Penquerc'h <vincent.penquerch@collabora.co.uk>2014-06-06 12:36:13 +0100
commit988914384acdf30eab22226d79de3541e7e6f02d (patch)
tree15e4aa1a7d235fc269951e537d8c7cf2ccec9bc5 /testsuite
parent909f41fb49ddb7d8745a3fd6565bf2eb6531e3e6 (diff)
Add some checks on the number of variables per type
We want to ensure no more than, say, 8 constants are added to a program. Adding more will violate pervasive assumptions in the code, and may lead to various buffer overflows. By trapping these add creation time, we prevent these issues without cluttering the code with range checks. The user is assumed non malicious here. Add a test to check we can add up to and including the limit for a type, but no more.
Diffstat (limited to 'testsuite')
-rw-r--r--testsuite/Makefile.am3
-rw-r--r--testsuite/test-limits.c72
2 files changed, 74 insertions, 1 deletions
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index a2fc635..9fb62fa 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -12,7 +12,8 @@ TESTS = \
exec_parse \
perf_opcodes_sys perf_parse \
memcpy_speed \
- abi
+ abi \
+ test-limits
noinst_PROGRAMS = $(TESTS) generate_xml_table generate_xml_table2 \
generate_opcodes_sys compile_parse compile_parse_c memcpy_speed \
diff --git a/testsuite/test-limits.c b/testsuite/test-limits.c
new file mode 100644
index 0000000..ed19d8a
--- /dev/null
+++ b/testsuite/test-limits.c
@@ -0,0 +1,72 @@
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <orc/orc.h>
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <orc-test/orctest.h>
+
+
+static int error = FALSE;
+static const char *names = "0123456789abcdefX";
+
+static void
+test_simple (int max, int (*adder) (OrcProgram *, int, const char *))
+{
+ OrcProgram *p;
+ int v;
+ OrcCompileResult result;
+
+ p = orc_program_new ();
+
+ /* dummy program so compile doesn't barf */
+ orc_program_add_destination (p, 2, "d1");
+ orc_program_add_source (p, 2, "s1");
+ orc_program_append_str (p, "addw", "d1", "d1", "s1");
+
+ /* we've alreay added one of those */
+ if (adder == orc_program_add_destination || adder == orc_program_add_source)
+ max--;
+
+ /* Check we can add up to the claimed max */
+ for (v = 0; v < max; v++)
+ (*adder) (p, 2, names + v);
+ result = orc_program_compile (p);
+ if (!ORC_COMPILE_RESULT_IS_SUCCESSFUL (result))
+ error = TRUE;
+
+ /* Check we can not add one more */
+ (*adder) (p, 2, names + v);
+ result = orc_program_compile (p);
+ if (ORC_COMPILE_RESULT_IS_SUCCESSFUL (result))
+ error = TRUE;
+
+ orc_program_free (p);
+}
+
+static int
+add_constant (OrcProgram *program, int size, const char *name)
+{
+ return orc_program_add_constant (program, size, 0, name);
+}
+
+int
+main (int argc, char *argv[])
+{
+ orc_init();
+ orc_test_init();
+
+ test_simple (ORC_MAX_DEST_VARS, orc_program_add_destination);
+ test_simple (ORC_MAX_SRC_VARS, orc_program_add_source);
+ test_simple (ORC_MAX_TEMP_VARS, orc_program_add_temporary);
+ test_simple (ORC_MAX_CONST_VARS, add_constant);
+ test_simple (ORC_MAX_PARAM_VARS, orc_program_add_parameter);
+ test_simple (ORC_MAX_ACCUM_VARS, orc_program_add_accumulator);
+
+ if (error) return 1;
+ return 0;
+}
+