Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Initialize the random number generator in sqlite4_initialize().
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | sqlite4_env
Files: files | file ages | folders
SHA1: a34def59f7c2a77465a85ae8ab2ccd425c1be775
User & Date: drh 2012-06-25 18:53:52.825
Context
2012-06-25
19:28
Fixes to the kvwrap test interface. At this point, most tests pass, though there are still massive memory leaks. Major unresolved issues: (1) the global function. (2) API for substituting new storage engines. Leaf check-in: dee2172b52 user: drh tags: sqlite4_env
18:53
Initialize the random number generator in sqlite4_initialize(). check-in: a34def59f7 user: drh tags: sqlite4_env
2012-06-23
17:26
Tests run to completion now - no more assertion faults. But still lots of errors and memory leaks. check-in: 272ebdd8c0 user: drh tags: sqlite4_env
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/main.c.
120
121
122
123
124
125
126


127
128
129
130
131
132
133
    pEnv->pPrngMutex = sqlite4MutexAlloc(pEnv, SQLITE_MUTEX_FAST);
    if( pEnv->pMemMutex==0 || pEnv->pPrngMutex==0 ) rc = SQLITE_NOMEM;
  }else{
    pEnv->pMemMutex = 0;
    pEnv->pPrngMutex = 0;
  }
  pEnv->isInit = 1;



  /* Register global functions */
  if( rc==SQLITE_OK ){
    sqlite4RegisterGlobalFunctions(pEnv);
  }

  /* The following is just a sanity check to make sure SQLite has







>
>







120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    pEnv->pPrngMutex = sqlite4MutexAlloc(pEnv, SQLITE_MUTEX_FAST);
    if( pEnv->pMemMutex==0 || pEnv->pPrngMutex==0 ) rc = SQLITE_NOMEM;
  }else{
    pEnv->pMemMutex = 0;
    pEnv->pPrngMutex = 0;
  }
  pEnv->isInit = 1;

  sqlite4OsInit(pEnv);

  /* Register global functions */
  if( rc==SQLITE_OK ){
    sqlite4RegisterGlobalFunctions(pEnv);
  }

  /* The following is just a sanity check to make sure SQLite has
Changes to src/os.c.
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
** This file contains OS interface code that is common to all
** architectures.
*/
#define _SQLITE_OS_C_ 1
#include "sqliteInt.h"
#undef _SQLITE_OS_C_

int sqlite4OsRandomness(sqlite4_env *pEnv, int nByte, unsigned char *zBufOut){
  memset(zBufOut, 0, nByte);
  return SQLITE_OK;
}



/*
** The following variable, if set to a non-zero value, is interpreted as
** the number of seconds since 1970 and is used to set the result of
** sqlite4OsCurrentTime() during testing.
*/
unsigned int sqlite4_current_time = 0; /* Fake system time */
int sqlite4OsCurrentTime(sqlite4_env *pEnv, sqlite4_uint64 *pTimeOut){













  UNUSED_PARAMETER(pEnv);


























  *pTimeOut = (sqlite4_uint64)sqlite4_current_time * 1000;


















  return SQLITE_OK;
}

/*
** This function is a wrapper around the OS specific implementation of
** sqlite4_os_init(). The purpose of the wrapper is to provide the
** ability to simulate a malloc failure, so that the handling of an
** error in sqlite4_os_init() by the upper layers can be tested.
*/
int sqlite4OsInit(sqlite4_env *pEnv){
  void *p = sqlite4_malloc(pEnv, 10);
  if( p==0 ) return SQLITE_NOMEM;
  sqlite4_free(pEnv, p);

  return SQLITE_OK; /*sqlite4_os_init();*/
}







<
<
|
<
>
>








>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













>


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
** This file contains OS interface code that is common to all
** architectures.
*/
#define _SQLITE_OS_C_ 1
#include "sqliteInt.h"
#undef _SQLITE_OS_C_



#if SQLITE_OS_UNIX

#include <sys/time.h>
#endif

/*
** The following variable, if set to a non-zero value, is interpreted as
** the number of seconds since 1970 and is used to set the result of
** sqlite4OsCurrentTime() during testing.
*/
unsigned int sqlite4_current_time = 0; /* Fake system time */
int sqlite4OsCurrentTime(sqlite4_env *pEnv, sqlite4_uint64 *pTimeOut){
  int rc = SQLITE_OK;
  if( sqlite4_current_time ){
    *pTimeOut = (sqlite4_uint64)sqlite4_current_time * 1000;
    return SQLITE_OK;
  }
#if SQLITE_OS_UNIX
  static const sqlite4_int64 unixEpoch = 24405875*(sqlite4_int64)8640000;
  struct timeval sNow;
  if( gettimeofday(&sNow, 0)==0 ){
    *pTimeOut = unixEpoch + 1000*(sqlite4_int64)sNow.tv_sec + sNow.tv_usec/1000;
  }else{
    rc = SQLITE_ERROR;
  }
  UNUSED_PARAMETER(pEnv);
#endif
#if SQLITE_OS_WIN
  FILETIME ft;
  static const sqlite4_int64 winFiletimeEpoch =
                                 23058135*(sqlite4_int64)8640000;
  /* 2^32 - to avoid use of LL and warnings in gcc */
  static const sqlite4_int64 max32BitValue = 
      (sqlite4_int64)2000000000 + (sqlite4_int64)2000000000
         + (sqlite4_int64)294967296;
  GetSystemTimeAsFileTime( &ft );
  *pTimeOut = winFiletimeEpoch +
                ((((sqlite4_int64)ft.dwHighDateTime)*max32BitValue) + 
                   (sqlite4_int64)ft.dwLowDateTime)/(sqlite4_int64)10000;
  UNUSED_PARAMETER(pEnv);
#endif
  return rc;
}

/*
** Write nByte bytes of randomness into zBufOut[].  This is used to initialize
** the PRNGs.  nByte will always be 8.
*/
int sqlite4OsRandomness(sqlite4_env *pEnv, int nByte, unsigned char *zBufOut){
  static sqlite4_uint64 cnt = 0;
  unsigned char *p;
  int i;
  sqlite4_uint64 now;
  sqlite4_uint64 x = 0;

#if 0 && SQLITE_OS_UNIX
  int fd = open("/dev/urandom", O_RDONLY, 0);
  if( fd>=0 ){
    read(fd, zBufOut, nByte);
    close(fd);
  }
  x = getpid();
#endif
  sqlite4OsCurrentTime(pEnv, &now);
  x ^= now;
  memset(zBufOut, 0, nByte);
  cnt++;
  x ^= cnt;
  p = (unsigned char*)&x;
  for(i=0; i<8; i++) zBufOut[i%nByte] ^= p[i];
    
  return SQLITE_OK;
}

/*
** This function is a wrapper around the OS specific implementation of
** sqlite4_os_init(). The purpose of the wrapper is to provide the
** ability to simulate a malloc failure, so that the handling of an
** error in sqlite4_os_init() by the upper layers can be tested.
*/
int sqlite4OsInit(sqlite4_env *pEnv){
  void *p = sqlite4_malloc(pEnv, 10);
  if( p==0 ) return SQLITE_NOMEM;
  sqlite4_free(pEnv, p);
  sqlite4OsRandomness(pEnv, 8, (unsigned char*)&pEnv->prngX);
  return SQLITE_OK; /*sqlite4_os_init();*/
}
Changes to src/random.c.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33


/*
** Get a single 8-bit random value from the PRNG.  The Mutex
** must be held while executing this routine.
*/
static u8 randomByte(sqlite4_env *pEnv){
  pEnv->prngX = (pEnv->prngX>>1) ^ ((1+~(pEnv->prngX&1)) & 0xd0000001);
  pEnv->prngY = pEnv->prngY*1103515245 + 12345;
  return (u8)((pEnv->prngX ^ pEnv->prngY)&0xff);
}

/*
** Return N random bytes.
*/







|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33


/*
** Get a single 8-bit random value from the PRNG.  The Mutex
** must be held while executing this routine.
*/
static u8 randomByte(sqlite4_env *pEnv){
  pEnv->prngX = (pEnv->prngX>>1) ^ ((-(pEnv->prngX&1)) & 0xd0000001);
  pEnv->prngY = pEnv->prngY*1103515245 + 12345;
  return (u8)((pEnv->prngX ^ pEnv->prngY)&0xff);
}

/*
** Return N random bytes.
*/
Changes to test/log1.test.
434
435
436
437
438
439
440

441
442
443
444
445
446
447
  copy_db_files test.db test.db2
  sqlite4 db2 test.db2
  execsql { SELECT count(*) FROM t1 ; PRAGMA integrity_check } db2
} {21 ok}
db2 close

do_test 11.21 { sqlite4_lsm_work db main -flush } {0}

do_execsql_test 11.22 {
  INSERT INTO t1 VALUES(randstr(10,10), randstr(100,100));
}
do_test 11.23 { 
  sqlite4_lsm_info db main log-structure 
} {1335 1482 0 1259 1483 1908}
do_test 11.24 { sqlite4_lsm_work db main -checkpoint } {0}







>







434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  copy_db_files test.db test.db2
  sqlite4 db2 test.db2
  execsql { SELECT count(*) FROM t1 ; PRAGMA integrity_check } db2
} {21 ok}
db2 close

do_test 11.21 { sqlite4_lsm_work db main -flush } {0}
db eval {SELECT randstr(5,5)}
do_execsql_test 11.22 {
  INSERT INTO t1 VALUES(randstr(10,10), randstr(100,100));
}
do_test 11.23 { 
  sqlite4_lsm_info db main log-structure 
} {1335 1482 0 1259 1483 1908}
do_test 11.24 { sqlite4_lsm_work db main -checkpoint } {0}