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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
/*
* KDUMA - Red-Zone memory allocator.
*
* Copyright (C) 2006 Michael Eddington <meddington@gmail.com>
* Copyright (C) 2002-2005 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
* Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
*
* License: GNU GPL (GNU General Public License, see COPYING-GPL)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* FILE CONTENTS:
* --------------
* This is a small tool to generate the "duma_config.h" configuration file.
* Its purpose is to allow fixed size memory allocation on stack, which can
* get protected calling page protection functions.
* Also its nice for the user to be able to see the page size.
*/
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <stdarg.h>
#ifdef _MSC_VER
#include <direct.h>
#endif
#ifndef WIN32
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#else
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <winbase.h>
typedef LPVOID caddr_t;
typedef unsigned u_int;
#endif
/*
* retrieve page size
* size_t Page_Size(void)
*/
static size_t
Page_Size(void)
{
#if defined(WIN32)
SYSTEM_INFO SystemInfo;
GetSystemInfo( &SystemInfo );
return (size_t)SystemInfo.dwPageSize;
#elif defined(_SC_PAGESIZE)
return (size_t)sysconf(_SC_PAGESIZE);
#elif defined(_SC_PAGE_SIZE)
return (size_t)sysconf(_SC_PAGE_SIZE);
#else
/* extern int getpagesize(); */
return getpagesize();
#endif
}
int initattr_ok = 0;
void
#ifdef __GNUC__
__attribute ((constructor))
#endif
init_function(void)
{
initattr_ok = 1;
}
typedef struct
{
int id;
int size;
char *type;
}
DataType_T;
DataType_T sIntTypes[] =
{
{ 0, sizeof(char) , "char" }
, { 1, sizeof(short int), "short int" }
, { 2, sizeof(int) , "int" }
, { 3, sizeof(long int) , "long int" }
/* hier zusaetzliche compiler-spezifische datentypen hinzufuegen */
#ifdef _MSC_VER
, { 4, sizeof(__int64), "__int64" }
#endif
#ifdef __GNUC__
, { 5, sizeof(long long int), "long long int" }
#endif
};
/* test access to each data type */
void testAlignment(int addrIdx, char * buffer, int alignment, int max_sizeof)
{
int offset;
switch ( sIntTypes[addrIdx].id )
{
case 0:
#define TYPE unsigned char
for(offset=0; offset < max_sizeof; ++offset)
{
TYPE addr = (TYPE)(buffer) + offset;
if ( addr % alignment == 0 )
{
*( (unsigned char *) addr ) = 0;
*( (unsigned short int*) addr ) = 0;
*( (unsigned int *) addr ) = 0;
*( (unsigned long int *) addr ) = 0L;
*( (float *) addr ) = 0.0F;
*( (double *) addr ) = 0.0;
*( (long double *) addr ) = 0.0;
#ifdef _MSC_VER
*( (unsigned __int64 *) addr ) = 0L;
#endif
#ifdef __GNUC__
*( (unsigned long long int *) addr ) = 0L;
#endif
}
}
break;
case 1:
#undef TYPE
#define TYPE unsigned short int
for(offset=0; offset < max_sizeof; ++offset)
{
TYPE addr = (TYPE)(buffer) + offset;
if ( addr % alignment == 0 )
{
*( (unsigned char *) addr ) = 0;
*( (unsigned short int*) addr ) = 0;
*( (unsigned int *) addr ) = 0;
*( (unsigned long int *) addr ) = 0L;
*( (float *) addr ) = 0.0F;
*( (double *) addr ) = 0.0;
*( (long double *) addr ) = 0.0;
#ifdef _MSC_VER
*( (unsigned __int64 *) addr ) = 0L;
#endif
#ifdef __GNUC__
*( (unsigned long long int *) addr ) = 0L;
#endif
}
}
break;
case 2:
#undef TYPE
#define TYPE unsigned int
for(offset=0; offset < max_sizeof; ++offset)
{
TYPE addr = (TYPE)(buffer) + offset;
if ( addr % alignment == 0 )
{
*( (unsigned char *) addr ) = 0;
*( (unsigned short int*) addr ) = 0;
*( (unsigned int *) addr ) = 0;
*( (unsigned long int *) addr ) = 0L;
*( (float *) addr ) = 0.0F;
*( (double *) addr ) = 0.0;
*( (long double *) addr ) = 0.0;
#ifdef _MSC_VER
*( (unsigned __int64 *) addr ) = 0L;
#endif
#ifdef __GNUC__
*( (unsigned long long int *) addr ) = 0L;
#endif
}
}
break;
case 3:
#undef TYPE
#define TYPE unsigned long int
for(offset=0; offset < max_sizeof; ++offset)
{
TYPE addr = (TYPE)(buffer) + offset;
if ( addr % alignment == 0 )
{
*( (unsigned char *) addr ) = 0;
*( (unsigned short int*) addr ) = 0;
*( (unsigned int *) addr ) = 0;
*( (unsigned long int *) addr ) = 0L;
*( (float *) addr ) = 0.0F;
*( (double *) addr ) = 0.0;
*( (long double *) addr ) = 0.0;
#ifdef _MSC_VER
*( (unsigned __int64 *) addr ) = 0L;
#endif
#ifdef __GNUC__
*( (unsigned long long int *) addr ) = 0L;
#endif
}
}
break;
#ifdef _MSC_VER
case 4:
#undef TYPE
#define TYPE unsigned __int64
for(offset=0; offset < max_sizeof; ++offset)
{
TYPE addr = (TYPE)(buffer) + offset;
if ( addr % alignment == 0 )
{
*( (unsigned char *) addr ) = 0;
*( (unsigned short int*) addr ) = 0;
*( (unsigned int *) addr ) = 0;
*( (unsigned long int *) addr ) = 0L;
*( (float *) addr ) = 0.0F;
*( (double *) addr ) = 0.0;
*( (long double *) addr ) = 0.0;
#ifdef _MSC_VER
*( (unsigned __int64 *) addr ) = 0L;
#endif
#ifdef __GNUC__
*( (unsigned long long int *) addr ) = 0L;
#endif
}
}
break;
#endif
#ifdef __GNUC__
case 5:
#undef TYPE
#define TYPE unsigned long long int
for(offset=0; offset < max_sizeof; ++offset)
{
TYPE addr = (TYPE)(buffer) + offset;
if ( addr % alignment == 0 )
{
*( (unsigned char *) addr ) = 0;
*( (unsigned short int*) addr ) = 0;
*( (unsigned int *) addr ) = 0;
*( (unsigned long int *) addr ) = 0L;
*( (float *) addr ) = 0.0F;
*( (double *) addr ) = 0.0;
*( (long double *) addr ) = 0.0;
#ifdef _MSC_VER
*( (unsigned __int64 *) addr ) = 0L;
#endif
#ifdef __GNUC__
*( (unsigned long long int *) addr ) = 0L;
#endif
}
}
break;
#endif
;
}
}
void writeFile(const char * filename, unsigned long pagesize, int addrIdx, int sizeIdx, int alignment)
{
FILE * f = fopen(filename, "w");
if (!f)
return;
fprintf(f, "/*\n");
fprintf(f, " * WARNING: DO NOT CHANGE THIS FILE!\n");
fprintf(f, " * This file is automatically generated from createconf\n");
#if defined(WIN32)
#if defined(_MSC_VER)
fprintf(f, " * using Microsoft Visual C++ under MS Windows(TM) the %s\n", __DATE__ );
#else
printf(f, " * under MS Windows(TM) the %s\n", __DATE__ );
#endif
#elif ( defined(sgi) )
fprintf(f, " * under sgi the %s\n", __DATE__ );
#elif ( defined(_AIX) )
fprintf(f, " * under AIX the %s\n", __DATE__ );
#elif ( defined(__linux__) )
fprintf(f, " * under Linux the %s\n", __DATE__ );
#else
fprintf(f, " * the %s \n", __DATE__ );
#endif
fprintf(f, " */\n");
fprintf(f, "\n");
fprintf(f, "#ifndef _DUMA_CONFIG_H_\n");
fprintf(f, "#define _DUMA_CONFIG_H_\n");
fprintf(f, "\n");
fprintf(f, "\n");
fprintf(f, "/* Variable: DUMA_PAGE_SIZE\n");
fprintf(f, " *\n");
fprintf(f, " * Number of bytes per virtual-memory page, as returned by Page_Size().\n");
fprintf(f, " */\n");
fprintf(f, "#define DUMA_PAGE_SIZE %lu\n", pagesize);
fprintf(f, "\n");
fprintf(f, "/* Variable: DUMA_MIN_ALIGNMENT\n");
fprintf(f, " *\n");
fprintf(f, " * Minimum required alignment by CPU.\n");
fprintf(f, " */\n");
fprintf(f, "#define DUMA_MIN_ALIGNMENT %d\n", alignment);
fprintf(f, "\n");
fprintf(f, "/* Variable: DUMA_GNU_INIT_ATTR\n");
fprintf(f, " *\n");
fprintf(f, " * Build environment supports the '__attribute ((constructor))'?\n");
fprintf(f, " */\n");
if (initattr_ok)
fprintf(f, "#define DUMA_GNU_INIT_ATTR 1\n");
else
fprintf(f, "/* #define DUMA_GNU_INIT_ATTR 0 */\n");
fprintf(f, "\n");
fprintf(f, "/* Type: DUMA_ADDR\n");
fprintf(f, " *\n");
fprintf(f, " * An integer type with same size as 'void *'\n");
fprintf(f, " */\n");
if (addrIdx >= 0)
fprintf(f, "typedef unsigned %s DUMA_ADDR;\n", sIntTypes[addrIdx].type);
else
fprintf(f, "/* Error: No datatype for DUMA_ADDR found! */\n");
fprintf(f, "\n");
fprintf(f, "/* Type: DUMA_SIZE\n");
fprintf(f, " *\n");
fprintf(f, " * An integer type with same size as 'size_t'\n");
fprintf(f, " */\n");
if (sizeIdx >= 0)
fprintf(f, "typedef unsigned %s DUMA_SIZE;\n", sIntTypes[sizeIdx].type);
else
fprintf(f, "/* No datatype for DUMA_SIZE found! */\n");
fprintf(f, "\n");
fprintf(f, "#endif /* _DUMA_CONFIG_H_ */\n");
fclose(f);
}
int main()
{
int iNumIntTypes, iIt;
int addrIdx, sizeIdx;
int alignment, max_sizeof;
char buffer[1024];
char filename[1024];
unsigned long pagesize = Page_Size();
iNumIntTypes = sizeof( sIntTypes ) / sizeof( sIntTypes[0] );
// detect compatible type for ADDRESS
addrIdx = -1;
for (iIt = 0; iIt < iNumIntTypes; ++iIt)
{
if ( sizeof(void*) == sIntTypes[iIt].size )
{
addrIdx = iIt;
break;
}
}
// detect compatible type for SIZE_T
sizeIdx = -1;
for (iIt = 0; iIt < iNumIntTypes; ++iIt)
{
if ( sizeof(size_t) == sIntTypes[iIt].size )
{
sizeIdx = iIt;
break;
}
}
/* detect maximum data type, which should be maximum alignment */
max_sizeof = 0;
for (iIt = 0; iIt < iNumIntTypes; ++iIt)
{
if ( max_sizeof < sIntTypes[iIt].size )
max_sizeof = sIntTypes[iIt].size;
}
if ( max_sizeof < sizeof(float) )
max_sizeof = sizeof(float);
if ( max_sizeof < sizeof(double) )
max_sizeof = sizeof(double);
if ( max_sizeof < sizeof(long double) )
max_sizeof = sizeof(long double);
#ifdef _MSC_VER
{
/* fix output path when started form Visual C++ IDE */
char directory[1024];
char * start;
_getcwd( directory, 1024 ); /* get current directory */
if ( ( start = strstr( directory, "win32-msvc" ) ) )
{
*start = 0;
strcpy(filename, directory);
strcat(filename, "duma_config.h");
}
else
strcpy( filename, "duma_config.h" );
}
#else
strcpy( filename, "duma_config.h" );
#endif
/* detect minimum alignment */
alignment = max_sizeof;
do
{
/* do the alignment access tests */
testAlignment(addrIdx, buffer, alignment, max_sizeof);
/* write whole config file. next test may crash ! */
writeFile(filename, pagesize, addrIdx, sizeIdx, alignment);
/* try next lower alignment */
alignment >>= 1;
}
while ( alignment > 0 );
return 0;
}
|