This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
libtest.c
167 lines (141 loc) · 3.89 KB
/
libtest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "libtest.h"
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
unsigned long simpleAddNoPrintTest(unsigned long a, unsigned long b)
{
return a + b;
}
double simpleDivideTest(double a, double b)
{
return a / b;
}
int simpleAddTest(int a, int b)
{
printf("simpleAddTest\n");
fflush(stdout);
return a + b;
}
size_t simpleStrLenTest(const char* str)
{
printf("simpleStrLenTest\n");
fflush(stdout);
return strlen(str);
}
int simpleCallbackTest(unsigned a, const char* b, CallbackType callback)
{
int ret;
printf("simpleCallbackTest\n");
fflush(stdout);
ret = callback(a + 1, b, &a);
return ret;
}
int simpleWriteToFileTest(FILE* file, const char* str)
{
printf("simpleWriteToFileTest\n");
fflush(stdout);
return fputs(str, file);
}
char* simpleEchoTest(char * str)
{
printf("simpleEchoTest\n");
fflush(stdout);
return str;
}
float simpleFloatAddTest(const float a, const float b)
{
printf("simpleFloatAddTest\n");
return a + b;
}
double simpleDoubleAddTest(const double a, const double b)
{
printf("simpleDoubleAddTest\n");
return a + b;
}
unsigned long simpleLongAddTest(unsigned long a, unsigned long b)
{
printf("simpleLongAddTest\n");
fflush(stdout);
return a + b;
}
struct testStruct simpleTestStructVal()
{
struct testStruct ret;
ret.fieldLong = 7;
ret.fieldString = "Hello";
ret.fieldBool = 1;
strcpy(ret.fieldFixedArr, "Bye");
return ret;
}
struct testStruct* simpleTestStructPtr()
{
struct testStruct* ret = (struct testStruct*) malloc(sizeof(struct testStruct));
ret->fieldLong = 7;
ret->fieldString = "Hello";
ret->fieldBool = 1;
strcpy(ret->fieldFixedArr, "Bye");
return ret;
}
struct testStruct simpleTestStructValBadPtr()
{
struct testStruct ret = simpleTestStructVal();
//explicitly mess up the top bits of the pointer. The sandbox checks outside the sandbox should catch this
if (sizeof(void*) == 4) {
ret.fieldString = (char *)((((uintptr_t) ret.fieldString) & 0x3FFFFFFF) | 0xC0000000);
} else {
ret.fieldString = (char *)((((uintptr_t) ret.fieldString) & 0xFFFFFFFF) | 0x1234567800000000);
}
return ret;
}
struct testStruct* simpleTestStructPtrBadPtr()
{
struct testStruct* ret = simpleTestStructPtr();
//explicitly mess up the top bits of the pointer. The sandbox checks outside the sandbox should catch this
if (sizeof(void*) == 4) {
ret->fieldString = (char *)((((uintptr_t) ret->fieldString) & 0x3FFFFFFF) | 0xC0000000);
} else {
ret->fieldString = (char *)((((uintptr_t) ret->fieldString) & 0xFFFFFFFF) | 0x1234567800000000);
}
return ret;
}
int* echoPointer(int* pointer)
{
return pointer;
}
double simplePointerValAddTest(double* ptr, double val)
{
printf("simplePointerValAddTest\n");
return val + *ptr;
}
struct pointersStruct initializePointerStruct(char* initVal)
{
struct pointersStruct ret;
ret.firstPointer = initVal;
ret.pointerArray[0] = (char*) (((uintptr_t) initVal) + 1);
ret.pointerArray[1] = (char*) (((uintptr_t) initVal) + 2);
ret.pointerArray[2] = (char*) (((uintptr_t) initVal) + 3);
ret.pointerArray[3] = (char*) (((uintptr_t) initVal) + 4);
ret.lastPointer = (char*) (((uintptr_t) initVal) + 5);
return ret;
}
struct pointersStruct* initializePointerStructPtr(char* initVal)
{
struct pointersStruct* ret = (struct pointersStruct*) malloc(sizeof(struct pointersStruct));
ret->firstPointer = initVal;
ret->pointerArray[0] = (char*) (((uintptr_t) initVal) + 1);
ret->pointerArray[1] = (char*) (((uintptr_t) initVal) + 2);
ret->pointerArray[2] = (char*) (((uintptr_t) initVal) + 3);
ret->pointerArray[3] = (char*) (((uintptr_t) initVal) + 4);
ret->lastPointer = (char*) (((uintptr_t) initVal) + 5);
return ret;
}
int internalCallback(unsigned a, const char* b, unsigned c[1])
{
return a + strlen(b);
}
void simplePointerWrite(int* ptr, int val) {
*ptr = val;
}
int simpleCallbackTest2(unsigned long startVal, CallbackType2 cb) {
return cb(startVal, startVal+1, startVal+2, startVal+3, startVal+4, startVal+5);
}