File: ModVerify.cpp

package info (click to toggle)
safeint 3.0.28a%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,668 kB
  • sloc: cpp: 24,880; ansic: 3,183; makefile: 53
file content (177 lines) | stat: -rw-r--r-- 3,716 bytes parent folder | download | duplicates (2)
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
168
169
170
171
172
173
174
175
176
177
// Copyright (c) OWASP Project (https://www.owasp.org), 2011. All rights reserved.
// Licensed under the MIT License.

#include "TestMain.h"
#include "TestCase.h"

#if defined(__GNUC__)
# include <stdint.h>
#endif

#if !defined(CHAR_BIT)
# include <limits.h>
#endif

namespace mod_verify
{
	template <typename T>
	std::string type_name()
	{
		std::ostringstream ostm;
		ostm << (std::numeric_limits<T>::is_signed ? "int" : "uint");
		ostm << (sizeof(T) == 1 ? 8 : sizeof(T) * 8);
		return ostm.str();
	}

// ModVerifyTest2 tests (x) % (0).
template<typename T>
struct ModVerifyTest1
{
	ModVerifyTest1<T>()
	{
		const size_t width = sizeof(T);
		const size_t shift = width * CHAR_BIT - 1;
		const bool expected = true;

		bool divzero;
		SafeInt<T> x = (T)((std::uint64_t)1 << shift);
		SafeInt<T> m(0);

		///////////////////////////////////////////////

		try
		{
			T r;
			r = x % m;
			divzero = false;
		}
		catch(SafeIntException&)
		{
			divzero = true;
		}

		if(divzero != expected)
		{
			std::cerr << "Error in case " << type_name<T>() << ": ";
			std::cerr << to_hex((T)x) << ", " << to_hex((T)m) << ", ";
			std::cerr << "expected = " << (expected ? "divzero" : "no divzero") << std::endl;
		}

		///////////////////////////////////////////////

		try
		{
			T r = x;
			r %= m;
			divzero = false;
		}
		catch(SafeIntException&)
		{
			divzero = true;
		}

		if(divzero != expected)
		{
			std::cerr << "Error in case " << type_name<T>() << ": ";
			std::cerr << to_hex((T)x) << ", " << to_hex((T)m) << ", ";
			std::cerr << "expected = " << (expected ? "divzero" : "no divzero") << std::endl;
		}
	}
};

// ModVerifyTest2 tests (INT_MIN) % (-1).
template<typename T>
struct ModVerifyTest2
{
	ModVerifyTest2<T>()
	{
		const size_t width = sizeof(T);
		const size_t shift = width * CHAR_BIT - 1;

		// GCC using a native built-in int will raise SIGFPE or
		// #DE. However, according to LeBlanc, SafeInt's contract
		// is to return the correct mathematical result or throw.
		// const bool expected = (s == Signed ? true : false);
		const bool expected = false;

		bool overflow;
		SafeInt<T> x = (T)((std::uint64_t)1 << shift);
		SafeInt<T> m((T)-1);

		///////////////////////////////////////////////

		try
		{
			T r;
			r = x % m;
			overflow = false;
		}
		catch(SafeIntException&)
		{
			overflow = true;
		}

		if(overflow != expected)
		{
			std::cerr << "Error in case " << type_name<T>() << ": ";
			std::cerr << to_hex((T)x) << ", " << to_hex((T)m) << ", ";
			std::cerr << "expected = " << (expected ? "overflow" : "no overflow") << std::endl;
		}

		///////////////////////////////////////////////

		try
		{
			T r = x;
			r %= m;
			overflow = false;
		}
		catch(SafeIntException&)
		{
			overflow = true;
		}

		if(overflow != expected)
		{
			std::cerr << "Error in case " << type_name<T>() << ": ";
			std::cerr << to_hex((T)x) << ", " << to_hex((T)m) << ", ";
			std::cerr << "expected = " << (expected ? "overflow" : "no overflow") << std::endl;
		}
	}
};

void ModVerify()
{
	std::cout << "Verifying Reduction:" << std::endl;

	ModVerifyTest1<std::uint64_t> t11;
	ModVerifyTest1<std::int64_t> t12;

	ModVerifyTest1<std::uint32_t> t13;
	ModVerifyTest1<std::int32_t> t14;

	ModVerifyTest1<std::uint16_t> t15;
	ModVerifyTest1<std::int16_t> t16;

	ModVerifyTest1<std::uint8_t> t17;
	ModVerifyTest1<std::int8_t> t18;


	ModVerifyTest2<std::uint64_t> t21;
	ModVerifyTest2<std::int64_t> t22;

	ModVerifyTest2<std::uint32_t> t23;
	ModVerifyTest2<std::int32_t> t24;

	ModVerifyTest2<std::uint16_t> t25;
	ModVerifyTest2<std::int16_t> t26;

	ModVerifyTest2<std::uint8_t> t27;
	ModVerifyTest2<std::int8_t> t28;

	// Lets see.....
	ModVerifyTest1<size_t> t50;
	ModVerifyTest2<size_t> t51;
}

}