Skip to content
Snippets Groups Projects
Commit 433dbb0f authored by an7s's avatar an7s
Browse files

Multiply examples

parent 31bea254
Branches
Tags
No related merge requests found
...@@ -145,6 +145,8 @@ examples/Makefile -text ...@@ -145,6 +145,8 @@ examples/Makefile -text
examples/dumbledore.c -text examples/dumbledore.c -text
examples/dumbledore_cmd.c -text examples/dumbledore_cmd.c -text
examples/integerbug.c -text examples/integerbug.c -text
examples/mul/generate_cprogs.sh -text
examples/mul/mul.ctmpl -text
examples/overflow1.c -text examples/overflow1.c -text
examples/test1.c -text examples/test1.c -text
libIRDB/Makefile -text libIRDB/Makefile -text
......
#define MAX_INT 2147483647 #include <stdio.h>
#define MAX_UINT 4294967295
#define INT_MAX 2147483647
#define UINT_MAX 4294967295
int int_fussy_overflow(int x, int y) int int_fussy_overflow(int x, int y)
{ {
...@@ -58,6 +60,23 @@ char* integer_underflow(unsigned len, char *src) ...@@ -58,6 +60,23 @@ char* integer_underflow(unsigned len, char *src)
return comm; return comm;
} }
#define BUFF_SIZE 10
// if len is < 0, this will bypass the check
// which will result in an overflow of buf[]
char* sign_error_buffer_overflow(int len, char *src)
{
printf("sign_error_buffer_overflow(): %d\n", len);
char buf[BUFF_SIZE];
if (len < BUFF_SIZE) {
printf("Copying %u bytes into buffer of size %u\n", len, BUFF_SIZE);
return memcpy(buf, src, len);
}
else {
return NULL;
}
}
char* signed_error(int size) char* signed_error(int size)
{ {
printf("signed_error(): %d\n", size); printf("signed_error(): %d\n", size);
...@@ -87,6 +106,24 @@ char* trunc_error(unsigned size, int numElements) ...@@ -87,6 +106,24 @@ char* trunc_error(unsigned size, int numElements)
return malloc(len * numElements); return malloc(len * numElements);
} }
short sign_extend_char_short(char c)
{
short s;
return s = c;
}
short sign_extend_char_long(char c)
{
long l;
return l = c;
}
short sign_extend_short_long(short s)
{
long l;
return l = s;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int selector = 0; int selector = 0;
...@@ -109,9 +146,10 @@ int main(int argc, char **argv) ...@@ -109,9 +146,10 @@ int main(int argc, char **argv)
bufptr = integer_underflow(10, buf); bufptr = integer_underflow(10, buf);
result = signed_error_bypass_check(10); result = signed_error_bypass_check(10);
bufptr = trunc_error(10, 10); bufptr = trunc_error(10, 10);
signed_overflow(2, 3);
break; break;
// bad inputs here // "bad" inputs here
case 1: case 1:
bufptr = integer_overflow_into_malloc_2(2000000000, 4); bufptr = integer_overflow_into_malloc_2(2000000000, 4);
break; break;
...@@ -128,10 +166,16 @@ int main(int argc, char **argv) ...@@ -128,10 +166,16 @@ int main(int argc, char **argv)
bufptr = trunc_error(65000, 10); bufptr = trunc_error(65000, 10);
break; break;
case 6: case 6:
int_fussy_overflow(MAX_INT,MAX_INT); int_fussy_overflow(INT_MAX,INT_MAX);
break; break;
case 7: case 7:
signed_overflow(MAX_INT, MAX_INT); signed_overflow(INT_MAX, INT_MAX);
break;
case 8:
signed_overflow(INT_MAX, INT_MAX);
break;
case 9:
sign_error_buffer_overflow(-1, buf);
break; break;
} }
} }
#!/bin/bash
#types="char short int long unsigned+char unsigned+short unsigned+int unsigned+long";
types="int char"
create_prog()
{
progname=$1
my_type1=$2
my_type2=$3
# substitute space for +
real_type1=`echo $my_type1 | sed "s/+/ /g"`
real_type2=`echo $my_type2 | sed "s/+/ /g"`
# substitute _ for + to get a valid C function name
name_type1=`echo $my_type1 | sed "s/+/_/g"`
name_type2=`echo $my_type2 | sed "s/+/_/g"`
function_name="test_${name_type1}_${name_type2}"
case $my_type1 in
"char") format_specifier="%c" ;;
"short") format_specifier="%hd" ;;
"int") format_specifier="%d" ;;
"long") format_specifier="%ld" ;;
"unsigned+char") format_specifier="%uc" ;;
"unsigned+short") format_specifier="%hu" ;;
"unsigned+int") format_specifier="%u" ;;
"unsigned+long") format_specifier="%ul" ;;
esac
# create the program.
cat mul.ctmpl | \
sed "s/#FUNCTION_NAME#/$function_name/g" | \
sed "s/#FORMAT_SPECIFIER#/$format_specifier/g" | \
sed "s/#TYPE1#/$real_type1/g" | \
sed "s/#TYPE2#/$real_type2/g" \
> $progname.c
gcc -w $progname.c -o $progname.orig.exe
$PEASOUP_HOME/tools/ps_analyze.sh --step ilr=off --step p1transform=off $progname.orig.exe $progname.protected.exe
}
for type1 in $types
do
for type2 in $types
do
progname_c=mul.$type1.$type2
# actually create the .c program
create_prog $progname_c "$type1" "$type2" static_lib
done
done
#TYPE1# #FUNCTION_NAME#(#TYPE1# x, #TYPE2# y)
{
#TYPE1# result = x * y;
printf("result = #FORMAT_SPECIFIER#\n", result);
return result;
}
main()
{
#TYPE1# x = 127;
#TYPE2# y = 0xFFFFFFFF;
#FUNCTION_NAME#(x, y);
x = 0x0FFFFFFF;
y = 0x0FFFFFFF;
#FUNCTION_NAME#(x, y);
x = 0x0000007F;
y = 0x0000007F;
#FUNCTION_NAME#(x, y);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment