Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: third_party/sqlite/sqlite-src-3080704/ext/misc/fuzzer.c

Issue 883353008: [sql] Import reference version of SQLite 3.8.7.4. (Closed) Base URL: http://chromium.googlesource.com/chromium/src.git@master
Patch Set: Hold back encoding change which is messing up patch. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 ** 2011 March 24 2 ** 2011 March 24
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
11 ************************************************************************* 11 *************************************************************************
12 ** 12 **
13 ** Code for demonstartion virtual table that generates variations 13 ** Code for a demonstration virtual table that generates variations
14 ** on an input word at increasing edit distances from the original. 14 ** on an input word at increasing edit distances from the original.
15 ** 15 **
16 ** A fuzzer virtual table is created like this: 16 ** A fuzzer virtual table is created like this:
17 ** 17 **
18 ** CREATE VIRTUAL TABLE temp.f USING fuzzer; 18 ** CREATE VIRTUAL TABLE f USING fuzzer(<fuzzer-data-table>);
19 ** 19 **
20 ** The name of the new virtual table in the example above is "f". 20 ** When it is created, the new fuzzer table must be supplied with the
21 ** Note that all fuzzer virtual tables must be TEMP tables. The 21 ** name of a "fuzzer data table", which must reside in the same database
22 ** "temp." prefix in front of the table name is required when the 22 ** file as the new fuzzer table. The fuzzer data table contains the various
23 ** table is being created. The "temp." prefix can be omitted when 23 ** transformations and their costs that the fuzzer logic uses to generate
24 ** using the table as long as the name is unambiguous. 24 ** variations.
25 ** 25 **
26 ** Before being used, the fuzzer needs to be programmed by giving it 26 ** The fuzzer data table must contain exactly four columns (more precisely,
27 ** character transformations and a cost associated with each transformation. 27 ** the statement "SELECT * FROM <fuzzer_data_table>" must return records
28 ** Examples: 28 ** that consist of four columns). It does not matter what the columns are
29 ** named.
29 ** 30 **
30 ** INSERT INTO f(cFrom,cTo,Cost) VALUES('','a',100); 31 ** Each row in the fuzzer data table represents a single character
32 ** transformation. The left most column of the row (column 0) contains an
33 ** integer value - the identifier of the ruleset to which the transformation
34 ** rule belongs (see "MULTIPLE RULE SETS" below). The second column of the
35 ** row (column 0) contains the input character or characters. The third
36 ** column contains the output character or characters. And the fourth column
37 ** contains the integer cost of making the transformation. For example:
31 ** 38 **
32 ** The above statement says that the cost of inserting a letter 'a' is 39 ** CREATE TABLE f_data(ruleset, cFrom, cTo, Cost);
33 ** 100. (All costs are integers. We recommend that costs be scaled so 40 ** INSERT INTO f_data(ruleset, cFrom, cTo, Cost) VALUES(0, '', 'a', 100);
34 ** that the average cost is around 100.) 41 ** INSERT INTO f_data(ruleset, cFrom, cTo, Cost) VALUES(0, 'b', '', 87);
42 ** INSERT INTO f_data(ruleset, cFrom, cTo, Cost) VALUES(0, 'o', 'oe', 38);
43 ** INSERT INTO f_data(ruleset, cFrom, cTo, Cost) VALUES(0, 'oe', 'o', 40);
35 ** 44 **
36 ** INSERT INTO f(cFrom,cTo,Cost) VALUES('b','',87); 45 ** The first row inserted into the fuzzer data table by the SQL script
37 ** 46 ** above indicates that the cost of inserting a letter 'a' is 100. (All
38 ** The above statement says that the cost of deleting a single letter 47 ** costs are integers. We recommend that costs be scaled so that the
39 ** 'b' is 87. 48 ** average cost is around 100.) The second INSERT statement creates a rule
40 ** 49 ** saying that the cost of deleting a single letter 'b' is 87. The third
41 ** INSERT INTO f(cFrom,cTo,Cost) VALUES('o','oe',38); 50 ** and fourth INSERT statements mean that the cost of transforming a
42 ** INSERT INTO f(cFrom,cTo,Cost) VALUES('oe','o',40); 51 ** single letter "o" into the two-letter sequence "oe" is 38 and that the
43 **
44 ** This third example says that the cost of transforming the single
45 ** letter "o" into the two-letter sequence "oe" is 38 and that the
46 ** cost of transforming "oe" back into "o" is 40. 52 ** cost of transforming "oe" back into "o" is 40.
47 ** 53 **
48 ** After all the transformation costs have been set, the fuzzer table 54 ** The contents of the fuzzer data table are loaded into main memory when
49 ** can be queried as follows: 55 ** a fuzzer table is first created, and may be internally reloaded by the
56 ** system at any subsequent time. Therefore, the fuzzer data table should be
57 ** populated before the fuzzer table is created and not modified thereafter.
58 ** If you do need to modify the contents of the fuzzer data table, it is
59 ** recommended that the associated fuzzer table be dropped, the fuzzer data
60 ** table edited, and the fuzzer table recreated within a single transaction.
61 ** Alternatively, the fuzzer data table can be edited then the database
62 ** connection can be closed and reopened.
63 **
64 ** Once it has been created, the fuzzer table can be queried as follows:
50 ** 65 **
51 ** SELECT word, distance FROM f 66 ** SELECT word, distance FROM f
52 ** WHERE word MATCH 'abcdefg' 67 ** WHERE word MATCH 'abcdefg'
53 ** AND distance<200; 68 ** AND distance<200;
54 ** 69 **
55 ** This first query outputs the string "abcdefg" and all strings that 70 ** This first query outputs the string "abcdefg" and all strings that
56 ** can be derived from that string by appling the specified transformations. 71 ** can be derived from that string by appling the specified transformations.
57 ** The strings are output together with their total transformation cost 72 ** The strings are output together with their total transformation cost
58 ** (called "distance") and appear in order of increasing cost. No string 73 ** (called "distance") and appear in order of increasing cost. No string
59 ** is output more than once. If there are multiple ways to transform the 74 ** is output more than once. If there are multiple ways to transform the
60 ** target string into the output string then the lowest cost transform is 75 ** target string into the output string then the lowest cost transform is
61 ** the one that is returned. In the example, the search is limited to 76 ** the one that is returned. In the example, the search is limited to
62 ** strings with a total distance of less than 200. 77 ** strings with a total distance of less than 200.
63 ** 78 **
79 ** The fuzzer is a read-only table. Any attempt to DELETE, INSERT, or
80 ** UPDATE on a fuzzer table will throw an error.
81 **
64 ** It is important to put some kind of a limit on the fuzzer output. This 82 ** It is important to put some kind of a limit on the fuzzer output. This
65 ** can be either in the form of a LIMIT clause at the end of the query, 83 ** can be either in the form of a LIMIT clause at the end of the query,
66 ** or better, a "distance<NNN" constraint where NNN is some number. The 84 ** or better, a "distance<NNN" constraint where NNN is some number. The
67 ** running time and memory requirement is exponential in the value of NNN 85 ** running time and memory requirement is exponential in the value of NNN
68 ** so you want to make sure that NNN is not too big. A value of NNN that 86 ** so you want to make sure that NNN is not too big. A value of NNN that
69 ** is about twice the average transformation cost seems to give good results. 87 ** is about twice the average transformation cost seems to give good results.
70 ** 88 **
71 ** The fuzzer table can be useful for tasks such as spelling correction. 89 ** The fuzzer table can be useful for tasks such as spelling correction.
72 ** Suppose there is a second table vocabulary(w) where the w column contains 90 ** Suppose there is a second table vocabulary(w) where the w column contains
73 ** all correctly spelled words. Let $word be a word you want to look up. 91 ** all correctly spelled words. Let $word be a word you want to look up.
(...skipping 12 matching lines...) Expand all
86 ** begin with some prefix $prefix: 104 ** begin with some prefix $prefix:
87 ** 105 **
88 ** SELECT vocabulary.w FROM f, vocabulary 106 ** SELECT vocabulary.w FROM f, vocabulary
89 ** WHERE f.word MATCH $prefix 107 ** WHERE f.word MATCH $prefix
90 ** AND f.distance<=200 108 ** AND f.distance<=200
91 ** AND vocabulary.w BETWEEN f.word AND (f.word || x'F7BFBFBF') 109 ** AND vocabulary.w BETWEEN f.word AND (f.word || x'F7BFBFBF')
92 ** LIMIT 50 110 ** LIMIT 50
93 ** 111 **
94 ** This last query will show up to 50 words out of the vocabulary that 112 ** This last query will show up to 50 words out of the vocabulary that
95 ** match or nearly match the $prefix. 113 ** match or nearly match the $prefix.
114 **
115 ** MULTIPLE RULE SETS
116 **
117 ** Normally, the "ruleset" value associated with all character transformations
118 ** in the fuzzer data table is zero. However, if required, the fuzzer table
119 ** allows multiple rulesets to be defined. Each query uses only a single
120 ** ruleset. This allows, for example, a single fuzzer table to support
121 ** multiple languages.
122 **
123 ** By default, only the rules from ruleset 0 are used. To specify an
124 ** alternative ruleset, a "ruleset = ?" expression must be added to the
125 ** WHERE clause of a SELECT, where ? is the identifier of the desired
126 ** ruleset. For example:
127 **
128 ** SELECT vocabulary.w FROM f, vocabulary
129 ** WHERE f.word MATCH $word
130 ** AND f.distance<=200
131 ** AND f.word=vocabulary.w
132 ** AND f.ruleset=1 -- Specify the ruleset to use here
133 ** LIMIT 20
134 **
135 ** If no "ruleset = ?" constraint is specified in the WHERE clause, ruleset
136 ** 0 is used.
137 **
138 ** LIMITS
139 **
140 ** The maximum ruleset number is 2147483647. The maximum length of either
141 ** of the strings in the second or third column of the fuzzer data table
142 ** is 50 bytes. The maximum cost on a rule is 1000.
96 */ 143 */
97 #include "sqlite3.h" 144 #include "sqlite3ext.h"
145 SQLITE_EXTENSION_INIT1
146
147 /* If SQLITE_DEBUG is not defined, disable assert statements. */
148 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
149 # define NDEBUG
150 #endif
151
98 #include <stdlib.h> 152 #include <stdlib.h>
99 #include <string.h> 153 #include <string.h>
100 #include <assert.h> 154 #include <assert.h>
101 #include <stdio.h> 155 #include <stdio.h>
102 156
103 #ifndef SQLITE_OMIT_VIRTUALTABLE 157 #ifndef SQLITE_OMIT_VIRTUALTABLE
104 158
105 /* 159 /*
106 ** Forward declaration of objects used by this implementation 160 ** Forward declaration of objects used by this implementation
107 */ 161 */
108 typedef struct fuzzer_vtab fuzzer_vtab; 162 typedef struct fuzzer_vtab fuzzer_vtab;
109 typedef struct fuzzer_cursor fuzzer_cursor; 163 typedef struct fuzzer_cursor fuzzer_cursor;
110 typedef struct fuzzer_rule fuzzer_rule; 164 typedef struct fuzzer_rule fuzzer_rule;
111 typedef struct fuzzer_seen fuzzer_seen; 165 typedef struct fuzzer_seen fuzzer_seen;
112 typedef struct fuzzer_stem fuzzer_stem; 166 typedef struct fuzzer_stem fuzzer_stem;
113 167
114 /* 168 /*
115 ** Type of the "cost" of an edit operation. Might be changed to 169 ** Various types.
116 ** "float" or "double" or "sqlite3_int64" in the future. 170 **
171 ** fuzzer_cost is the "cost" of an edit operation.
172 **
173 ** fuzzer_len is the length of a matching string.
174 **
175 ** fuzzer_ruleid is an ruleset identifier.
117 */ 176 */
118 typedef int fuzzer_cost; 177 typedef int fuzzer_cost;
178 typedef signed char fuzzer_len;
179 typedef int fuzzer_ruleid;
180
181 /*
182 ** Limits
183 */
184 #define FUZZER_MX_LENGTH 50 /* Maximum length of a rule string */
185 #define FUZZER_MX_RULEID 2147483647 /* Maximum rule ID */
186 #define FUZZER_MX_COST 1000 /* Maximum single-rule cost */
187 #define FUZZER_MX_OUTPUT_LENGTH 100 /* Maximum length of an output string */
119 188
120 189
121 /* 190 /*
122 ** Each transformation rule is stored as an instance of this object. 191 ** Each transformation rule is stored as an instance of this object.
123 ** All rules are kept on a linked list sorted by rCost. 192 ** All rules are kept on a linked list sorted by rCost.
124 */ 193 */
125 struct fuzzer_rule { 194 struct fuzzer_rule {
126 fuzzer_rule *pNext; /* Next rule in order of increasing rCost */ 195 fuzzer_rule *pNext; /* Next rule in order of increasing rCost */
127 fuzzer_cost rCost; /* Cost of this transformation */ 196 char *zFrom; /* Transform from */
128 int nFrom, nTo; /* Length of the zFrom and zTo strings */ 197 fuzzer_cost rCost; /* Cost of this transformation */
129 char *zFrom; /* Transform from */ 198 fuzzer_len nFrom, nTo; /* Length of the zFrom and zTo strings */
130 char zTo[4]; /* Transform to (extra space appended) */ 199 fuzzer_ruleid iRuleset; /* The rule set to which this rule belongs */
200 char zTo[4]; /* Transform to (extra space appended) */
131 }; 201 };
132 202
133 /* 203 /*
134 ** A stem object is used to generate variants. It is also used to record 204 ** A stem object is used to generate variants. It is also used to record
135 ** previously generated outputs. 205 ** previously generated outputs.
136 ** 206 **
137 ** Every stem is added to a hash table as it is output. Generation of 207 ** Every stem is added to a hash table as it is output. Generation of
138 ** duplicate stems is suppressed. 208 ** duplicate stems is suppressed.
139 ** 209 **
140 ** Active stems (those that might generate new outputs) are kepts on a linked 210 ** Active stems (those that might generate new outputs) are kepts on a linked
141 ** list sorted by increasing cost. The cost is the sum of rBaseCost and 211 ** list sorted by increasing cost. The cost is the sum of rBaseCost and
142 ** pRule->rCost. 212 ** pRule->rCost.
143 */ 213 */
144 struct fuzzer_stem { 214 struct fuzzer_stem {
145 char *zBasis; /* Word being fuzzed */ 215 char *zBasis; /* Word being fuzzed */
146 int nBasis; /* Length of the zBasis string */
147 const fuzzer_rule *pRule; /* Current rule to apply */ 216 const fuzzer_rule *pRule; /* Current rule to apply */
148 int n; /* Apply pRule at this character offset */ 217 fuzzer_stem *pNext; /* Next stem in rCost order */
218 fuzzer_stem *pHash; /* Next stem with same hash on zBasis */
149 fuzzer_cost rBaseCost; /* Base cost of getting to zBasis */ 219 fuzzer_cost rBaseCost; /* Base cost of getting to zBasis */
150 fuzzer_cost rCostX; /* Precomputed rBaseCost + pRule->rCost */ 220 fuzzer_cost rCostX; /* Precomputed rBaseCost + pRule->rCost */
151 fuzzer_stem *pNext; /* Next stem in rCost order */ 221 fuzzer_len nBasis; /* Length of the zBasis string */
152 fuzzer_stem *pHash; /* Next stem with same hash on zBasis */ 222 fuzzer_len n; /* Apply pRule at this character offset */
153 }; 223 };
154 224
155 /* 225 /*
156 ** A fuzzer virtual-table object 226 ** A fuzzer virtual-table object
157 */ 227 */
158 struct fuzzer_vtab { 228 struct fuzzer_vtab {
159 sqlite3_vtab base; /* Base class - must be first */ 229 sqlite3_vtab base; /* Base class - must be first */
160 char *zClassName; /* Name of this class. Default: "fuzzer" */ 230 char *zClassName; /* Name of this class. Default: "fuzzer" */
161 fuzzer_rule *pRule; /* All active rules in this fuzzer */ 231 fuzzer_rule *pRule; /* All active rules in this fuzzer */
162 fuzzer_rule *pNewRule; /* New rules to add when last cursor expires */
163 int nCursor; /* Number of active cursors */ 232 int nCursor; /* Number of active cursors */
164 }; 233 };
165 234
166 #define FUZZER_HASH 4001 /* Hash table size */ 235 #define FUZZER_HASH 4001 /* Hash table size */
167 #define FUZZER_NQUEUE 20 /* Number of slots on the stem queue */ 236 #define FUZZER_NQUEUE 20 /* Number of slots on the stem queue */
168 237
169 /* A fuzzer cursor object */ 238 /* A fuzzer cursor object */
170 struct fuzzer_cursor { 239 struct fuzzer_cursor {
171 sqlite3_vtab_cursor base; /* Base class - must be first */ 240 sqlite3_vtab_cursor base; /* Base class - must be first */
172 sqlite3_int64 iRowid; /* The rowid of the current word */ 241 sqlite3_int64 iRowid; /* The rowid of the current word */
173 fuzzer_vtab *pVtab; /* The virtual table this cursor belongs to */ 242 fuzzer_vtab *pVtab; /* The virtual table this cursor belongs to */
174 fuzzer_cost rLimit; /* Maximum cost of any term */ 243 fuzzer_cost rLimit; /* Maximum cost of any term */
175 fuzzer_stem *pStem; /* Stem with smallest rCostX */ 244 fuzzer_stem *pStem; /* Stem with smallest rCostX */
176 fuzzer_stem *pDone; /* Stems already processed to completion */ 245 fuzzer_stem *pDone; /* Stems already processed to completion */
177 fuzzer_stem *aQueue[FUZZER_NQUEUE]; /* Queue of stems with higher rCostX */ 246 fuzzer_stem *aQueue[FUZZER_NQUEUE]; /* Queue of stems with higher rCostX */
178 int mxQueue; /* Largest used index in aQueue[] */ 247 int mxQueue; /* Largest used index in aQueue[] */
179 char *zBuf; /* Temporary use buffer */ 248 char *zBuf; /* Temporary use buffer */
180 int nBuf; /* Bytes allocated for zBuf */ 249 int nBuf; /* Bytes allocated for zBuf */
181 int nStem; /* Number of stems allocated */ 250 int nStem; /* Number of stems allocated */
251 int iRuleset; /* Only process rules from this ruleset */
182 fuzzer_rule nullRule; /* Null rule used first */ 252 fuzzer_rule nullRule; /* Null rule used first */
183 fuzzer_stem *apHash[FUZZER_HASH]; /* Hash of previously generated terms */ 253 fuzzer_stem *apHash[FUZZER_HASH]; /* Hash of previously generated terms */
184 }; 254 };
185 255
186 /* Methods for the fuzzer module */
187 static int fuzzerConnect(
188 sqlite3 *db,
189 void *pAux,
190 int argc, const char *const*argv,
191 sqlite3_vtab **ppVtab,
192 char **pzErr
193 ){
194 fuzzer_vtab *pNew;
195 int n;
196 if( strcmp(argv[1],"temp")!=0 ){
197 *pzErr = sqlite3_mprintf("%s virtual tables must be TEMP", argv[0]);
198 return SQLITE_ERROR;
199 }
200 n = strlen(argv[0]) + 1;
201 pNew = sqlite3_malloc( sizeof(*pNew) + n );
202 if( pNew==0 ) return SQLITE_NOMEM;
203 pNew->zClassName = (char*)&pNew[1];
204 memcpy(pNew->zClassName, argv[0], n);
205 sqlite3_declare_vtab(db, "CREATE TABLE x(word,distance,cFrom,cTo,cost)");
206 memset(pNew, 0, sizeof(*pNew));
207 *ppVtab = &pNew->base;
208 return SQLITE_OK;
209 }
210 /* Note that for this virtual table, the xCreate and xConnect
211 ** methods are identical. */
212
213 static int fuzzerDisconnect(sqlite3_vtab *pVtab){
214 fuzzer_vtab *p = (fuzzer_vtab*)pVtab;
215 assert( p->nCursor==0 );
216 do{
217 while( p->pRule ){
218 fuzzer_rule *pRule = p->pRule;
219 p->pRule = pRule->pNext;
220 sqlite3_free(pRule);
221 }
222 p->pRule = p->pNewRule;
223 p->pNewRule = 0;
224 }while( p->pRule );
225 sqlite3_free(p);
226 return SQLITE_OK;
227 }
228 /* The xDisconnect and xDestroy methods are also the same */
229
230 /* 256 /*
231 ** The two input rule lists are both sorted in order of increasing 257 ** The two input rule lists are both sorted in order of increasing
232 ** cost. Merge them together into a single list, sorted by cost, and 258 ** cost. Merge them together into a single list, sorted by cost, and
233 ** return a pointer to the head of that list. 259 ** return a pointer to the head of that list.
234 */ 260 */
235 static fuzzer_rule *fuzzerMergeRules(fuzzer_rule *pA, fuzzer_rule *pB){ 261 static fuzzer_rule *fuzzerMergeRules(fuzzer_rule *pA, fuzzer_rule *pB){
236 fuzzer_rule head; 262 fuzzer_rule head;
237 fuzzer_rule *pTail; 263 fuzzer_rule *pTail;
238 264
239 pTail = &head; 265 pTail = &head;
240 while( pA && pB ){ 266 while( pA && pB ){
241 if( pA->rCost<=pB->rCost ){ 267 if( pA->rCost<=pB->rCost ){
242 pTail->pNext = pA; 268 pTail->pNext = pA;
243 pTail = pA; 269 pTail = pA;
244 pA = pA->pNext; 270 pA = pA->pNext;
245 }else{ 271 }else{
246 pTail->pNext = pB; 272 pTail->pNext = pB;
247 pTail = pB; 273 pTail = pB;
248 pB = pB->pNext; 274 pB = pB->pNext;
249 } 275 }
250 } 276 }
251 if( pA==0 ){ 277 if( pA==0 ){
252 pTail->pNext = pB; 278 pTail->pNext = pB;
253 }else{ 279 }else{
254 pTail->pNext = pA; 280 pTail->pNext = pA;
255 } 281 }
256 return head.pNext; 282 return head.pNext;
257 } 283 }
258 284
285 /*
286 ** Statement pStmt currently points to a row in the fuzzer data table. This
287 ** function allocates and populates a fuzzer_rule structure according to
288 ** the content of the row.
289 **
290 ** If successful, *ppRule is set to point to the new object and SQLITE_OK
291 ** is returned. Otherwise, *ppRule is zeroed, *pzErr may be set to point
292 ** to an error message and an SQLite error code returned.
293 */
294 static int fuzzerLoadOneRule(
295 fuzzer_vtab *p, /* Fuzzer virtual table handle */
296 sqlite3_stmt *pStmt, /* Base rule on statements current row */
297 fuzzer_rule **ppRule, /* OUT: New rule object */
298 char **pzErr /* OUT: Error message */
299 ){
300 sqlite3_int64 iRuleset = sqlite3_column_int64(pStmt, 0);
301 const char *zFrom = (const char *)sqlite3_column_text(pStmt, 1);
302 const char *zTo = (const char *)sqlite3_column_text(pStmt, 2);
303 int nCost = sqlite3_column_int(pStmt, 3);
304
305 int rc = SQLITE_OK; /* Return code */
306 int nFrom; /* Size of string zFrom, in bytes */
307 int nTo; /* Size of string zTo, in bytes */
308 fuzzer_rule *pRule = 0; /* New rule object to return */
309
310 if( zFrom==0 ) zFrom = "";
311 if( zTo==0 ) zTo = "";
312 nFrom = (int)strlen(zFrom);
313 nTo = (int)strlen(zTo);
314
315 /* Silently ignore null transformations */
316 if( strcmp(zFrom, zTo)==0 ){
317 *ppRule = 0;
318 return SQLITE_OK;
319 }
320
321 if( nCost<=0 || nCost>FUZZER_MX_COST ){
322 *pzErr = sqlite3_mprintf("%s: cost must be between 1 and %d",
323 p->zClassName, FUZZER_MX_COST
324 );
325 rc = SQLITE_ERROR;
326 }else
327 if( nFrom>FUZZER_MX_LENGTH || nTo>FUZZER_MX_LENGTH ){
328 *pzErr = sqlite3_mprintf("%s: maximum string length is %d",
329 p->zClassName, FUZZER_MX_LENGTH
330 );
331 rc = SQLITE_ERROR;
332 }else
333 if( iRuleset<0 || iRuleset>FUZZER_MX_RULEID ){
334 *pzErr = sqlite3_mprintf("%s: ruleset must be between 0 and %d",
335 p->zClassName, FUZZER_MX_RULEID
336 );
337 rc = SQLITE_ERROR;
338 }else{
339
340 pRule = sqlite3_malloc( sizeof(*pRule) + nFrom + nTo );
341 if( pRule==0 ){
342 rc = SQLITE_NOMEM;
343 }else{
344 memset(pRule, 0, sizeof(*pRule));
345 pRule->zFrom = &pRule->zTo[nTo+1];
346 pRule->nFrom = nFrom;
347 memcpy(pRule->zFrom, zFrom, nFrom+1);
348 memcpy(pRule->zTo, zTo, nTo+1);
349 pRule->nTo = nTo;
350 pRule->rCost = nCost;
351 pRule->iRuleset = (int)iRuleset;
352 }
353 }
354
355 *ppRule = pRule;
356 return rc;
357 }
259 358
260 /* 359 /*
261 ** Open a new fuzzer cursor. 360 ** Load the content of the fuzzer data table into memory.
262 */ 361 */
263 static int fuzzerOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ 362 static int fuzzerLoadRules(
264 fuzzer_vtab *p = (fuzzer_vtab*)pVTab; 363 sqlite3 *db, /* Database handle */
265 fuzzer_cursor *pCur; 364 fuzzer_vtab *p, /* Virtual fuzzer table to configure */
266 pCur = sqlite3_malloc( sizeof(*pCur) ); 365 const char *zDb, /* Database containing rules data */
267 if( pCur==0 ) return SQLITE_NOMEM; 366 const char *zData, /* Table containing rules data */
268 memset(pCur, 0, sizeof(*pCur)); 367 char **pzErr /* OUT: Error message */
269 pCur->pVtab = p; 368 ){
270 *ppCursor = &pCur->base; 369 int rc = SQLITE_OK; /* Return code */
271 if( p->nCursor==0 && p->pNewRule ){ 370 char *zSql; /* SELECT used to read from rules table */
371 fuzzer_rule *pHead = 0;
372
373 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zData);
374 if( zSql==0 ){
375 rc = SQLITE_NOMEM;
376 }else{
377 int rc2; /* finalize() return code */
378 sqlite3_stmt *pStmt = 0;
379 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
380 if( rc!=SQLITE_OK ){
381 *pzErr = sqlite3_mprintf("%s: %s", p->zClassName, sqlite3_errmsg(db));
382 }else if( sqlite3_column_count(pStmt)!=4 ){
383 *pzErr = sqlite3_mprintf("%s: %s has %d columns, expected 4",
384 p->zClassName, zData, sqlite3_column_count(pStmt)
385 );
386 rc = SQLITE_ERROR;
387 }else{
388 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
389 fuzzer_rule *pRule = 0;
390 rc = fuzzerLoadOneRule(p, pStmt, &pRule, pzErr);
391 if( pRule ){
392 pRule->pNext = pHead;
393 pHead = pRule;
394 }
395 }
396 }
397 rc2 = sqlite3_finalize(pStmt);
398 if( rc==SQLITE_OK ) rc = rc2;
399 }
400 sqlite3_free(zSql);
401
402 /* All rules are now in a singly linked list starting at pHead. This
403 ** block sorts them by cost and then sets fuzzer_vtab.pRule to point to
404 ** point to the head of the sorted list.
405 */
406 if( rc==SQLITE_OK ){
272 unsigned int i; 407 unsigned int i;
273 fuzzer_rule *pX; 408 fuzzer_rule *pX;
274 fuzzer_rule *a[15]; 409 fuzzer_rule *a[15];
275 for(i=0; i<sizeof(a)/sizeof(a[0]); i++) a[i] = 0; 410 for(i=0; i<sizeof(a)/sizeof(a[0]); i++) a[i] = 0;
276 while( (pX = p->pNewRule)!=0 ){ 411 while( (pX = pHead)!=0 ){
277 p->pNewRule = pX->pNext; 412 pHead = pX->pNext;
278 pX->pNext = 0; 413 pX->pNext = 0;
279 for(i=0; a[i] && i<sizeof(a)/sizeof(a[0])-1; i++){ 414 for(i=0; a[i] && i<sizeof(a)/sizeof(a[0])-1; i++){
280 pX = fuzzerMergeRules(a[i], pX); 415 pX = fuzzerMergeRules(a[i], pX);
281 a[i] = 0; 416 a[i] = 0;
282 } 417 }
283 a[i] = fuzzerMergeRules(a[i], pX); 418 a[i] = fuzzerMergeRules(a[i], pX);
284 } 419 }
285 for(pX=a[0], i=1; i<sizeof(a)/sizeof(a[0]); i++){ 420 for(pX=a[0], i=1; i<sizeof(a)/sizeof(a[0]); i++){
286 pX = fuzzerMergeRules(a[i], pX); 421 pX = fuzzerMergeRules(a[i], pX);
287 } 422 }
288 p->pRule = fuzzerMergeRules(p->pRule, pX); 423 p->pRule = fuzzerMergeRules(p->pRule, pX);
424 }else{
425 /* An error has occurred. Setting p->pRule to point to the head of the
426 ** allocated list ensures that the list will be cleaned up in this case.
427 */
428 assert( p->pRule==0 );
429 p->pRule = pHead;
289 } 430 }
431
432 return rc;
433 }
434
435 /*
436 ** This function converts an SQL quoted string into an unquoted string
437 ** and returns a pointer to a buffer allocated using sqlite3_malloc()
438 ** containing the result. The caller should eventually free this buffer
439 ** using sqlite3_free.
440 **
441 ** Examples:
442 **
443 ** "abc" becomes abc
444 ** 'xyz' becomes xyz
445 ** [pqr] becomes pqr
446 ** `mno` becomes mno
447 */
448 static char *fuzzerDequote(const char *zIn){
449 int nIn; /* Size of input string, in bytes */
450 char *zOut; /* Output (dequoted) string */
451
452 nIn = (int)strlen(zIn);
453 zOut = sqlite3_malloc(nIn+1);
454 if( zOut ){
455 char q = zIn[0]; /* Quote character (if any ) */
456
457 if( q!='[' && q!= '\'' && q!='"' && q!='`' ){
458 memcpy(zOut, zIn, nIn+1);
459 }else{
460 int iOut = 0; /* Index of next byte to write to output */
461 int iIn; /* Index of next byte to read from input */
462
463 if( q=='[' ) q = ']';
464 for(iIn=1; iIn<nIn; iIn++){
465 if( zIn[iIn]==q ) iIn++;
466 zOut[iOut++] = zIn[iIn];
467 }
468 }
469 assert( (int)strlen(zOut)<=nIn );
470 }
471 return zOut;
472 }
473
474 /*
475 ** xDisconnect/xDestroy method for the fuzzer module.
476 */
477 static int fuzzerDisconnect(sqlite3_vtab *pVtab){
478 fuzzer_vtab *p = (fuzzer_vtab*)pVtab;
479 assert( p->nCursor==0 );
480 while( p->pRule ){
481 fuzzer_rule *pRule = p->pRule;
482 p->pRule = pRule->pNext;
483 sqlite3_free(pRule);
484 }
485 sqlite3_free(p);
486 return SQLITE_OK;
487 }
488
489 /*
490 ** xConnect/xCreate method for the fuzzer module. Arguments are:
491 **
492 ** argv[0] -> module name ("fuzzer")
493 ** argv[1] -> database name
494 ** argv[2] -> table name
495 ** argv[3] -> fuzzer rule table name
496 */
497 static int fuzzerConnect(
498 sqlite3 *db,
499 void *pAux,
500 int argc, const char *const*argv,
501 sqlite3_vtab **ppVtab,
502 char **pzErr
503 ){
504 int rc = SQLITE_OK; /* Return code */
505 fuzzer_vtab *pNew = 0; /* New virtual table */
506 const char *zModule = argv[0];
507 const char *zDb = argv[1];
508
509 if( argc!=4 ){
510 *pzErr = sqlite3_mprintf(
511 "%s: wrong number of CREATE VIRTUAL TABLE arguments", zModule
512 );
513 rc = SQLITE_ERROR;
514 }else{
515 int nModule; /* Length of zModule, in bytes */
516
517 nModule = (int)strlen(zModule);
518 pNew = sqlite3_malloc( sizeof(*pNew) + nModule + 1);
519 if( pNew==0 ){
520 rc = SQLITE_NOMEM;
521 }else{
522 char *zTab; /* Dequoted name of fuzzer data table */
523
524 memset(pNew, 0, sizeof(*pNew));
525 pNew->zClassName = (char*)&pNew[1];
526 memcpy(pNew->zClassName, zModule, nModule+1);
527
528 zTab = fuzzerDequote(argv[3]);
529 if( zTab==0 ){
530 rc = SQLITE_NOMEM;
531 }else{
532 rc = fuzzerLoadRules(db, pNew, zDb, zTab, pzErr);
533 sqlite3_free(zTab);
534 }
535
536 if( rc==SQLITE_OK ){
537 rc = sqlite3_declare_vtab(db, "CREATE TABLE x(word,distance,ruleset)");
538 }
539 if( rc!=SQLITE_OK ){
540 fuzzerDisconnect((sqlite3_vtab *)pNew);
541 pNew = 0;
542 }
543 }
544 }
545
546 *ppVtab = (sqlite3_vtab *)pNew;
547 return rc;
548 }
549
550 /*
551 ** Open a new fuzzer cursor.
552 */
553 static int fuzzerOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
554 fuzzer_vtab *p = (fuzzer_vtab*)pVTab;
555 fuzzer_cursor *pCur;
556 pCur = sqlite3_malloc( sizeof(*pCur) );
557 if( pCur==0 ) return SQLITE_NOMEM;
558 memset(pCur, 0, sizeof(*pCur));
559 pCur->pVtab = p;
560 *ppCursor = &pCur->base;
290 p->nCursor++; 561 p->nCursor++;
291 return SQLITE_OK; 562 return SQLITE_OK;
292 } 563 }
293 564
294 /* 565 /*
295 ** Free all stems in a list. 566 ** Free all stems in a list.
296 */ 567 */
297 static void fuzzerClearStemList(fuzzer_stem *pStem){ 568 static void fuzzerClearStemList(fuzzer_stem *pStem){
298 while( pStem ){ 569 while( pStem ){
299 fuzzer_stem *pNext = pStem->pNext; 570 fuzzer_stem *pNext = pStem->pNext;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 607
337 /* 608 /*
338 ** Compute the current output term for a fuzzer_stem. 609 ** Compute the current output term for a fuzzer_stem.
339 */ 610 */
340 static int fuzzerRender( 611 static int fuzzerRender(
341 fuzzer_stem *pStem, /* The stem to be rendered */ 612 fuzzer_stem *pStem, /* The stem to be rendered */
342 char **pzBuf, /* Write results into this buffer. realloc if needed */ 613 char **pzBuf, /* Write results into this buffer. realloc if needed */
343 int *pnBuf /* Size of the buffer */ 614 int *pnBuf /* Size of the buffer */
344 ){ 615 ){
345 const fuzzer_rule *pRule = pStem->pRule; 616 const fuzzer_rule *pRule = pStem->pRule;
346 int n; 617 int n; /* Size of output term without nul-term */
347 char *z; 618 char *z; /* Buffer to assemble output term in */
348 619
349 n = pStem->nBasis + pRule->nTo - pRule->nFrom; 620 n = pStem->nBasis + pRule->nTo - pRule->nFrom;
350 if( (*pnBuf)<n+1 ){ 621 if( (*pnBuf)<n+1 ){
351 (*pzBuf) = sqlite3_realloc((*pzBuf), n+100); 622 (*pzBuf) = sqlite3_realloc((*pzBuf), n+100);
352 if( (*pzBuf)==0 ) return SQLITE_NOMEM; 623 if( (*pzBuf)==0 ) return SQLITE_NOMEM;
353 (*pnBuf) = n+100; 624 (*pnBuf) = n+100;
354 } 625 }
355 n = pStem->n; 626 n = pStem->n;
356 z = *pzBuf; 627 z = *pzBuf;
357 if( n<0 ){ 628 if( n<0 ){
358 memcpy(z, pStem->zBasis, pStem->nBasis+1); 629 memcpy(z, pStem->zBasis, pStem->nBasis+1);
359 }else{ 630 }else{
360 memcpy(z, pStem->zBasis, n); 631 memcpy(z, pStem->zBasis, n);
361 memcpy(&z[n], pRule->zTo, pRule->nTo); 632 memcpy(&z[n], pRule->zTo, pRule->nTo);
362 memcpy(&z[n+pRule->nTo], &pStem->zBasis[n+pRule->nFrom], 633 memcpy(&z[n+pRule->nTo], &pStem->zBasis[n+pRule->nFrom],
363 pStem->nBasis-n-pRule->nFrom+1); 634 pStem->nBasis-n-pRule->nFrom+1);
364 } 635 }
636
637 assert( z[pStem->nBasis + pRule->nTo - pRule->nFrom]==0 );
365 return SQLITE_OK; 638 return SQLITE_OK;
366 } 639 }
367 640
368 /* 641 /*
369 ** Compute a hash on zBasis. 642 ** Compute a hash on zBasis.
370 */ 643 */
371 static unsigned int fuzzerHash(const char *z){ 644 static unsigned int fuzzerHash(const char *z){
372 unsigned int h = 0; 645 unsigned int h = 0;
373 while( *z ){ h = (h<<3) ^ (h>>29) ^ *(z++); } 646 while( *z ){ h = (h<<3) ^ (h>>29) ^ *(z++); }
374 return h % FUZZER_HASH; 647 return h % FUZZER_HASH;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 */ 690 */
418 static int fuzzerSeen(fuzzer_cursor *pCur, fuzzer_stem *pStem){ 691 static int fuzzerSeen(fuzzer_cursor *pCur, fuzzer_stem *pStem){
419 unsigned int h; 692 unsigned int h;
420 fuzzer_stem *pLookup; 693 fuzzer_stem *pLookup;
421 694
422 if( fuzzerRender(pStem, &pCur->zBuf, &pCur->nBuf)==SQLITE_NOMEM ){ 695 if( fuzzerRender(pStem, &pCur->zBuf, &pCur->nBuf)==SQLITE_NOMEM ){
423 return -1; 696 return -1;
424 } 697 }
425 h = fuzzerHash(pCur->zBuf); 698 h = fuzzerHash(pCur->zBuf);
426 pLookup = pCur->apHash[h]; 699 pLookup = pCur->apHash[h];
427 while( pLookup && strcmp(pLookup->zBasis, pCur->zBuf)!=0 ){ 700 while( pLookup && strcmp(pLookup->zBasis, pCur->zBuf)!=0 ){
428 pLookup = pLookup->pHash; 701 pLookup = pLookup->pHash;
429 } 702 }
430 return pLookup!=0; 703 return pLookup!=0;
431 } 704 }
432 705
433 /* 706 /*
707 ** If argument pRule is NULL, this function returns false.
708 **
709 ** Otherwise, it returns true if rule pRule should be skipped. A rule
710 ** should be skipped if it does not belong to rule-set iRuleset, or if
711 ** applying it to stem pStem would create a string longer than
712 ** FUZZER_MX_OUTPUT_LENGTH bytes.
713 */
714 static int fuzzerSkipRule(
715 const fuzzer_rule *pRule, /* Determine whether or not to skip this */
716 fuzzer_stem *pStem, /* Stem rule may be applied to */
717 int iRuleset /* Rule-set used by the current query */
718 ){
719 return pRule && (
720 (pRule->iRuleset!=iRuleset)
721 || (pStem->nBasis + pRule->nTo - pRule->nFrom)>FUZZER_MX_OUTPUT_LENGTH
722 );
723 }
724
725 /*
434 ** Advance a fuzzer_stem to its next value. Return 0 if there are 726 ** Advance a fuzzer_stem to its next value. Return 0 if there are
435 ** no more values that can be generated by this fuzzer_stem. Return 727 ** no more values that can be generated by this fuzzer_stem. Return
436 ** -1 on a memory allocation failure. 728 ** -1 on a memory allocation failure.
437 */ 729 */
438 static int fuzzerAdvance(fuzzer_cursor *pCur, fuzzer_stem *pStem){ 730 static int fuzzerAdvance(fuzzer_cursor *pCur, fuzzer_stem *pStem){
439 const fuzzer_rule *pRule; 731 const fuzzer_rule *pRule;
440 while( (pRule = pStem->pRule)!=0 ){ 732 while( (pRule = pStem->pRule)!=0 ){
733 assert( pRule==&pCur->nullRule || pRule->iRuleset==pCur->iRuleset );
441 while( pStem->n < pStem->nBasis - pRule->nFrom ){ 734 while( pStem->n < pStem->nBasis - pRule->nFrom ){
442 pStem->n++; 735 pStem->n++;
443 if( pRule->nFrom==0 736 if( pRule->nFrom==0
444 || memcmp(&pStem->zBasis[pStem->n], pRule->zFrom, pRule->nFrom)==0 737 || memcmp(&pStem->zBasis[pStem->n], pRule->zFrom, pRule->nFrom)==0
445 ){ 738 ){
446 /* Found a rewrite case. Make sure it is not a duplicate */ 739 /* Found a rewrite case. Make sure it is not a duplicate */
447 int rc = fuzzerSeen(pCur, pStem); 740 int rc = fuzzerSeen(pCur, pStem);
448 if( rc<0 ) return -1; 741 if( rc<0 ) return -1;
449 if( rc==0 ){ 742 if( rc==0 ){
450 fuzzerCost(pStem); 743 fuzzerCost(pStem);
451 return 1; 744 return 1;
452 } 745 }
453 } 746 }
454 } 747 }
455 pStem->n = -1; 748 pStem->n = -1;
456 pStem->pRule = pRule->pNext; 749 do{
457 if( pStem->pRule && fuzzerCost(pStem)>pCur->rLimit ) pStem->pRule = 0; 750 pRule = pRule->pNext;
751 }while( fuzzerSkipRule(pRule, pStem, pCur->iRuleset) );
752 pStem->pRule = pRule;
753 if( pRule && fuzzerCost(pStem)>pCur->rLimit ) pStem->pRule = 0;
458 } 754 }
459 return 0; 755 return 0;
460 } 756 }
461 757
462 /* 758 /*
463 ** The two input stem lists are both sorted in order of increasing 759 ** The two input stem lists are both sorted in order of increasing
464 ** rCostX. Merge them together into a single list, sorted by rCostX, and 760 ** rCostX. Merge them together into a single list, sorted by rCostX, and
465 ** return a pointer to the head of that new list. 761 ** return a pointer to the head of that new list.
466 */ 762 */
467 static fuzzer_stem *fuzzerMergeStems(fuzzer_stem *pA, fuzzer_stem *pB){ 763 static fuzzer_stem *fuzzerMergeStems(fuzzer_stem *pA, fuzzer_stem *pB){
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 /* 861 /*
566 ** Allocate a new fuzzer_stem. Add it to the hash table but do not 862 ** Allocate a new fuzzer_stem. Add it to the hash table but do not
567 ** link it into either the pCur->pStem or pCur->pDone lists. 863 ** link it into either the pCur->pStem or pCur->pDone lists.
568 */ 864 */
569 static fuzzer_stem *fuzzerNewStem( 865 static fuzzer_stem *fuzzerNewStem(
570 fuzzer_cursor *pCur, 866 fuzzer_cursor *pCur,
571 const char *zWord, 867 const char *zWord,
572 fuzzer_cost rBaseCost 868 fuzzer_cost rBaseCost
573 ){ 869 ){
574 fuzzer_stem *pNew; 870 fuzzer_stem *pNew;
871 fuzzer_rule *pRule;
575 unsigned int h; 872 unsigned int h;
576 873
577 pNew = sqlite3_malloc( sizeof(*pNew) + strlen(zWord) + 1 ); 874 pNew = sqlite3_malloc( sizeof(*pNew) + (int)strlen(zWord) + 1 );
578 if( pNew==0 ) return 0; 875 if( pNew==0 ) return 0;
579 memset(pNew, 0, sizeof(*pNew)); 876 memset(pNew, 0, sizeof(*pNew));
580 pNew->zBasis = (char*)&pNew[1]; 877 pNew->zBasis = (char*)&pNew[1];
581 pNew->nBasis = strlen(zWord); 878 pNew->nBasis = (int)strlen(zWord);
582 memcpy(pNew->zBasis, zWord, pNew->nBasis+1); 879 memcpy(pNew->zBasis, zWord, pNew->nBasis+1);
583 pNew->pRule = pCur->pVtab->pRule; 880 pRule = pCur->pVtab->pRule;
881 while( fuzzerSkipRule(pRule, pNew, pCur->iRuleset) ){
882 pRule = pRule->pNext;
883 }
884 pNew->pRule = pRule;
584 pNew->n = -1; 885 pNew->n = -1;
585 pNew->rBaseCost = pNew->rCostX = rBaseCost; 886 pNew->rBaseCost = pNew->rCostX = rBaseCost;
586 h = fuzzerHash(pNew->zBasis); 887 h = fuzzerHash(pNew->zBasis);
587 pNew->pHash = pCur->apHash[h]; 888 pNew->pHash = pCur->apHash[h];
588 pCur->apHash[h] = pNew; 889 pCur->apHash[h] = pNew;
589 pCur->nStem++; 890 pCur->nStem++;
590 return pNew; 891 return pNew;
591 } 892 }
592 893
593 894
(...skipping 26 matching lines...) Expand all
620 } 921 }
621 }else{ 922 }else{
622 return SQLITE_NOMEM; 923 return SQLITE_NOMEM;
623 } 924 }
624 } 925 }
625 926
626 /* Adjust the priority queue so that the first element of the 927 /* Adjust the priority queue so that the first element of the
627 ** stem list is the next lowest cost word. 928 ** stem list is the next lowest cost word.
628 */ 929 */
629 while( (pStem = pCur->pStem)!=0 ){ 930 while( (pStem = pCur->pStem)!=0 ){
630 if( fuzzerAdvance(pCur, pStem) ){ 931 int res = fuzzerAdvance(pCur, pStem);
932 if( res<0 ){
933 return SQLITE_NOMEM;
934 }else if( res>0 ){
631 pCur->pStem = 0; 935 pCur->pStem = 0;
632 pStem = fuzzerInsert(pCur, pStem); 936 pStem = fuzzerInsert(pCur, pStem);
633 if( (rc = fuzzerSeen(pCur, pStem))!=0 ){ 937 if( (rc = fuzzerSeen(pCur, pStem))!=0 ){
634 if( rc<0 ) return SQLITE_NOMEM; 938 if( rc<0 ) return SQLITE_NOMEM;
635 continue; 939 continue;
636 } 940 }
637 return SQLITE_OK; /* New word found */ 941 return SQLITE_OK; /* New word found */
638 } 942 }
639 pCur->pStem = 0; 943 pCur->pStem = 0;
640 pStem->pNext = pCur->pDone; 944 pStem->pNext = pCur->pDone;
(...skipping 17 matching lines...) Expand all
658 ** Called to "rewind" a cursor back to the beginning so that 962 ** Called to "rewind" a cursor back to the beginning so that
659 ** it starts its output over again. Always called at least once 963 ** it starts its output over again. Always called at least once
660 ** prior to any fuzzerColumn, fuzzerRowid, or fuzzerEof call. 964 ** prior to any fuzzerColumn, fuzzerRowid, or fuzzerEof call.
661 */ 965 */
662 static int fuzzerFilter( 966 static int fuzzerFilter(
663 sqlite3_vtab_cursor *pVtabCursor, 967 sqlite3_vtab_cursor *pVtabCursor,
664 int idxNum, const char *idxStr, 968 int idxNum, const char *idxStr,
665 int argc, sqlite3_value **argv 969 int argc, sqlite3_value **argv
666 ){ 970 ){
667 fuzzer_cursor *pCur = (fuzzer_cursor *)pVtabCursor; 971 fuzzer_cursor *pCur = (fuzzer_cursor *)pVtabCursor;
668 const char *zWord = 0; 972 const char *zWord = "";
669 fuzzer_stem *pStem; 973 fuzzer_stem *pStem;
974 int idx;
670 975
671 fuzzerClearCursor(pCur, 1); 976 fuzzerClearCursor(pCur, 1);
672 pCur->rLimit = 2147483647; 977 pCur->rLimit = 2147483647;
673 if( idxNum==1 ){ 978 idx = 0;
979 if( idxNum & 1 ){
674 zWord = (const char*)sqlite3_value_text(argv[0]); 980 zWord = (const char*)sqlite3_value_text(argv[0]);
675 }else if( idxNum==2 ){ 981 idx++;
676 pCur->rLimit = (fuzzer_cost)sqlite3_value_int(argv[0]);
677 }else if( idxNum==3 ){
678 zWord = (const char*)sqlite3_value_text(argv[0]);
679 pCur->rLimit = (fuzzer_cost)sqlite3_value_int(argv[1]);
680 } 982 }
681 if( zWord==0 ) zWord = ""; 983 if( idxNum & 2 ){
682 pCur->pStem = pStem = fuzzerNewStem(pCur, zWord, (fuzzer_cost)0); 984 pCur->rLimit = (fuzzer_cost)sqlite3_value_int(argv[idx]);
683 if( pStem==0 ) return SQLITE_NOMEM; 985 idx++;
986 }
987 if( idxNum & 4 ){
988 pCur->iRuleset = (fuzzer_cost)sqlite3_value_int(argv[idx]);
989 idx++;
990 }
684 pCur->nullRule.pNext = pCur->pVtab->pRule; 991 pCur->nullRule.pNext = pCur->pVtab->pRule;
685 pCur->nullRule.rCost = 0; 992 pCur->nullRule.rCost = 0;
686 pCur->nullRule.nFrom = 0; 993 pCur->nullRule.nFrom = 0;
687 pCur->nullRule.nTo = 0; 994 pCur->nullRule.nTo = 0;
688 pCur->nullRule.zFrom = ""; 995 pCur->nullRule.zFrom = "";
689 pStem->pRule = &pCur->nullRule;
690 pStem->n = pStem->nBasis;
691 pCur->iRowid = 1; 996 pCur->iRowid = 1;
997 assert( pCur->pStem==0 );
998
999 /* If the query term is longer than FUZZER_MX_OUTPUT_LENGTH bytes, this
1000 ** query will return zero rows. */
1001 if( (int)strlen(zWord)<FUZZER_MX_OUTPUT_LENGTH ){
1002 pCur->pStem = pStem = fuzzerNewStem(pCur, zWord, (fuzzer_cost)0);
1003 if( pStem==0 ) return SQLITE_NOMEM;
1004 pStem->pRule = &pCur->nullRule;
1005 pStem->n = pStem->nBasis;
1006 }else{
1007 pCur->rLimit = 0;
1008 }
1009
692 return SQLITE_OK; 1010 return SQLITE_OK;
693 } 1011 }
694 1012
695 /* 1013 /*
696 ** Only the word and distance columns have values. All other columns 1014 ** Only the word and distance columns have values. All other columns
697 ** return NULL 1015 ** return NULL
698 */ 1016 */
699 static int fuzzerColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ 1017 static int fuzzerColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
700 fuzzer_cursor *pCur = (fuzzer_cursor*)cur; 1018 fuzzer_cursor *pCur = (fuzzer_cursor*)cur;
701 if( i==0 ){ 1019 if( i==0 ){
(...skipping 26 matching lines...) Expand all
728 ** that the cursor has nothing more to output. 1046 ** that the cursor has nothing more to output.
729 */ 1047 */
730 static int fuzzerEof(sqlite3_vtab_cursor *cur){ 1048 static int fuzzerEof(sqlite3_vtab_cursor *cur){
731 fuzzer_cursor *pCur = (fuzzer_cursor*)cur; 1049 fuzzer_cursor *pCur = (fuzzer_cursor*)cur;
732 return pCur->rLimit<=(fuzzer_cost)0; 1050 return pCur->rLimit<=(fuzzer_cost)0;
733 } 1051 }
734 1052
735 /* 1053 /*
736 ** Search for terms of these forms: 1054 ** Search for terms of these forms:
737 ** 1055 **
738 ** word MATCH $str 1056 ** (A) word MATCH $str
739 ** distance < $value 1057 ** (B1) distance < $value
740 ** distance <= $value 1058 ** (B2) distance <= $value
1059 ** (C) ruleid == $ruleid
741 ** 1060 **
742 ** The distance< and distance<= are both treated as distance<=. 1061 ** The distance< and distance<= are both treated as distance<=.
743 ** The query plan number is as follows: 1062 ** The query plan number is a bit vector:
744 ** 1063 **
745 ** 0: None of the terms above are found 1064 ** bit 1: Term of the form (A) found
746 ** 1: There is a "word MATCH" term with $str in filter.argv[0]. 1065 ** bit 2: Term like (B1) or (B2) found
747 ** 2: There is a "distance<" term with $value in filter.argv[0]. 1066 ** bit 3: Term like (C) found
748 ** 3: Both "word MATCH" and "distance<" with $str in argv[0] and 1067 **
749 ** $value in argv[1]. 1068 ** If bit-1 is set, $str is always in filter.argv[0]. If bit-2 is set
1069 ** then $value is in filter.argv[0] if bit-1 is clear and is in
1070 ** filter.argv[1] if bit-1 is set. If bit-3 is set, then $ruleid is
1071 ** in filter.argv[0] if bit-1 and bit-2 are both zero, is in
1072 ** filter.argv[1] if exactly one of bit-1 and bit-2 are set, and is in
1073 ** filter.argv[2] if both bit-1 and bit-2 are set.
750 */ 1074 */
751 static int fuzzerBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ 1075 static int fuzzerBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
752 int iPlan = 0; 1076 int iPlan = 0;
753 int iDistTerm = -1; 1077 int iDistTerm = -1;
1078 int iRulesetTerm = -1;
754 int i; 1079 int i;
1080 int seenMatch = 0;
755 const struct sqlite3_index_constraint *pConstraint; 1081 const struct sqlite3_index_constraint *pConstraint;
1082 double rCost = 1e12;
1083
756 pConstraint = pIdxInfo->aConstraint; 1084 pConstraint = pIdxInfo->aConstraint;
757 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ 1085 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
1086 if( pConstraint->iColumn==0
1087 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
1088 seenMatch = 1;
1089 }
758 if( pConstraint->usable==0 ) continue; 1090 if( pConstraint->usable==0 ) continue;
759 if( (iPlan & 1)==0 1091 if( (iPlan & 1)==0
760 && pConstraint->iColumn==0 1092 && pConstraint->iColumn==0
761 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH 1093 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH
762 ){ 1094 ){
763 iPlan |= 1; 1095 iPlan |= 1;
764 pIdxInfo->aConstraintUsage[i].argvIndex = 1; 1096 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
765 pIdxInfo->aConstraintUsage[i].omit = 1; 1097 pIdxInfo->aConstraintUsage[i].omit = 1;
1098 rCost /= 1e6;
766 } 1099 }
767 if( (iPlan & 2)==0 1100 if( (iPlan & 2)==0
768 && pConstraint->iColumn==1 1101 && pConstraint->iColumn==1
769 && (pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT 1102 && (pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT
770 || pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE) 1103 || pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE)
771 ){ 1104 ){
772 iPlan |= 2; 1105 iPlan |= 2;
773 iDistTerm = i; 1106 iDistTerm = i;
1107 rCost /= 10.0;
1108 }
1109 if( (iPlan & 4)==0
1110 && pConstraint->iColumn==2
1111 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
1112 ){
1113 iPlan |= 4;
1114 pIdxInfo->aConstraintUsage[i].omit = 1;
1115 iRulesetTerm = i;
1116 rCost /= 10.0;
774 } 1117 }
775 } 1118 }
776 if( iPlan==2 ){ 1119 if( iPlan & 2 ){
777 pIdxInfo->aConstraintUsage[iDistTerm].argvIndex = 1; 1120 pIdxInfo->aConstraintUsage[iDistTerm].argvIndex = 1+((iPlan&1)!=0);
778 }else if( iPlan==3 ){ 1121 }
779 pIdxInfo->aConstraintUsage[iDistTerm].argvIndex = 2; 1122 if( iPlan & 4 ){
1123 int idx = 1;
1124 if( iPlan & 1 ) idx++;
1125 if( iPlan & 2 ) idx++;
1126 pIdxInfo->aConstraintUsage[iRulesetTerm].argvIndex = idx;
780 } 1127 }
781 pIdxInfo->idxNum = iPlan; 1128 pIdxInfo->idxNum = iPlan;
782 if( pIdxInfo->nOrderBy==1 1129 if( pIdxInfo->nOrderBy==1
783 && pIdxInfo->aOrderBy[0].iColumn==1 1130 && pIdxInfo->aOrderBy[0].iColumn==1
784 && pIdxInfo->aOrderBy[0].desc==0 1131 && pIdxInfo->aOrderBy[0].desc==0
785 ){ 1132 ){
786 pIdxInfo->orderByConsumed = 1; 1133 pIdxInfo->orderByConsumed = 1;
787 } 1134 }
788 pIdxInfo->estimatedCost = (double)10000; 1135 if( seenMatch && (iPlan&1)==0 ) rCost = 1e99;
1136 pIdxInfo->estimatedCost = rCost;
789 1137
790 return SQLITE_OK; 1138 return SQLITE_OK;
791 } 1139 }
792 1140
793 /* 1141 /*
794 ** Disallow all attempts to DELETE or UPDATE. Only INSERTs are allowed. 1142 ** A virtual table module that implements the "fuzzer".
795 **
796 ** On an insert, the cFrom, cTo, and cost columns are used to construct
797 ** a new rule. All other columns are ignored. The rule is ignored
798 ** if cFrom and cTo are identical. A NULL value for cFrom or cTo is
799 ** interpreted as an empty string. The cost must be positive.
800 */
801 static int fuzzerUpdate(
802 sqlite3_vtab *pVTab,
803 int argc,
804 sqlite3_value **argv,
805 sqlite_int64 *pRowid
806 ){
807 fuzzer_vtab *p = (fuzzer_vtab*)pVTab;
808 fuzzer_rule *pRule;
809 const char *zFrom;
810 int nFrom;
811 const char *zTo;
812 int nTo;
813 fuzzer_cost rCost;
814 if( argc!=7 ){
815 sqlite3_free(pVTab->zErrMsg);
816 pVTab->zErrMsg = sqlite3_mprintf("cannot delete from a %s virtual table",
817 p->zClassName);
818 return SQLITE_CONSTRAINT;
819 }
820 if( sqlite3_value_type(argv[0])!=SQLITE_NULL ){
821 sqlite3_free(pVTab->zErrMsg);
822 pVTab->zErrMsg = sqlite3_mprintf("cannot update a %s virtual table",
823 p->zClassName);
824 return SQLITE_CONSTRAINT;
825 }
826 zFrom = (char*)sqlite3_value_text(argv[4]);
827 if( zFrom==0 ) zFrom = "";
828 zTo = (char*)sqlite3_value_text(argv[5]);
829 if( zTo==0 ) zTo = "";
830 if( strcmp(zFrom,zTo)==0 ){
831 /* Silently ignore null transformations */
832 return SQLITE_OK;
833 }
834 rCost = sqlite3_value_int(argv[6]);
835 if( rCost<=0 ){
836 sqlite3_free(pVTab->zErrMsg);
837 pVTab->zErrMsg = sqlite3_mprintf("cost must be positive");
838 return SQLITE_CONSTRAINT;
839 }
840 nFrom = strlen(zFrom);
841 nTo = strlen(zTo);
842 pRule = sqlite3_malloc( sizeof(*pRule) + nFrom + nTo );
843 if( pRule==0 ){
844 return SQLITE_NOMEM;
845 }
846 pRule->zFrom = &pRule->zTo[nTo+1];
847 pRule->nFrom = nFrom;
848 memcpy(pRule->zFrom, zFrom, nFrom+1);
849 memcpy(pRule->zTo, zTo, nTo+1);
850 pRule->nTo = nTo;
851 pRule->rCost = rCost;
852 pRule->pNext = p->pNewRule;
853 p->pNewRule = pRule;
854 return SQLITE_OK;
855 }
856
857 /*
858 ** A virtual table module that provides read-only access to a
859 ** Tcl global variable namespace.
860 */ 1143 */
861 static sqlite3_module fuzzerModule = { 1144 static sqlite3_module fuzzerModule = {
862 0, /* iVersion */ 1145 0, /* iVersion */
863 fuzzerConnect, 1146 fuzzerConnect,
864 fuzzerConnect, 1147 fuzzerConnect,
865 fuzzerBestIndex, 1148 fuzzerBestIndex,
866 fuzzerDisconnect, 1149 fuzzerDisconnect,
867 fuzzerDisconnect, 1150 fuzzerDisconnect,
868 fuzzerOpen, /* xOpen - open a cursor */ 1151 fuzzerOpen, /* xOpen - open a cursor */
869 fuzzerClose, /* xClose - close a cursor */ 1152 fuzzerClose, /* xClose - close a cursor */
870 fuzzerFilter, /* xFilter - configure scan constraints */ 1153 fuzzerFilter, /* xFilter - configure scan constraints */
871 fuzzerNext, /* xNext - advance a cursor */ 1154 fuzzerNext, /* xNext - advance a cursor */
872 fuzzerEof, /* xEof - check for end of scan */ 1155 fuzzerEof, /* xEof - check for end of scan */
873 fuzzerColumn, /* xColumn - read data */ 1156 fuzzerColumn, /* xColumn - read data */
874 fuzzerRowid, /* xRowid - read data */ 1157 fuzzerRowid, /* xRowid - read data */
875 fuzzerUpdate, /* xUpdate - INSERT */ 1158 0, /* xUpdate */
876 0, /* xBegin */ 1159 0, /* xBegin */
877 0, /* xSync */ 1160 0, /* xSync */
878 0, /* xCommit */ 1161 0, /* xCommit */
879 0, /* xRollback */ 1162 0, /* xRollback */
880 0, /* xFindMethod */ 1163 0, /* xFindMethod */
881 0, /* xRename */ 1164 0, /* xRename */
882 }; 1165 };
883 1166
884 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 1167 #endif /* SQLITE_OMIT_VIRTUALTABLE */
885 1168
886 1169
887 /* 1170 #ifdef _WIN32
888 ** Register the fuzzer virtual table 1171 __declspec(dllexport)
889 */ 1172 #endif
890 int fuzzer_register(sqlite3 *db){ 1173 int sqlite3_fuzzer_init(
1174 sqlite3 *db,
1175 char **pzErrMsg,
1176 const sqlite3_api_routines *pApi
1177 ){
891 int rc = SQLITE_OK; 1178 int rc = SQLITE_OK;
1179 SQLITE_EXTENSION_INIT2(pApi);
892 #ifndef SQLITE_OMIT_VIRTUALTABLE 1180 #ifndef SQLITE_OMIT_VIRTUALTABLE
893 rc = sqlite3_create_module(db, "fuzzer", &fuzzerModule, 0); 1181 rc = sqlite3_create_module(db, "fuzzer", &fuzzerModule, 0);
894 #endif 1182 #endif
895 return rc; 1183 return rc;
896 } 1184 }
897
898 #ifdef SQLITE_TEST
899 #include <tcl.h>
900 /*
901 ** Decode a pointer to an sqlite3 object.
902 */
903 extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
904
905 /*
906 ** Register the echo virtual table module.
907 */
908 static int register_fuzzer_module(
909 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
910 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
911 int objc, /* Number of arguments */
912 Tcl_Obj *CONST objv[] /* Command arguments */
913 ){
914 sqlite3 *db;
915 if( objc!=2 ){
916 Tcl_WrongNumArgs(interp, 1, objv, "DB");
917 return TCL_ERROR;
918 }
919 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
920 fuzzer_register(db);
921 return TCL_OK;
922 }
923
924
925 /*
926 ** Register commands with the TCL interpreter.
927 */
928 int Sqlitetestfuzzer_Init(Tcl_Interp *interp){
929 static struct {
930 char *zName;
931 Tcl_ObjCmdProc *xProc;
932 void *clientData;
933 } aObjCmd[] = {
934 { "register_fuzzer_module", register_fuzzer_module, 0 },
935 };
936 int i;
937 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
938 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
939 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
940 }
941 return TCL_OK;
942 }
943
944 #endif /* SQLITE_TEST */
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3080704/ext/misc/fileio.c ('k') | third_party/sqlite/sqlite-src-3080704/ext/misc/ieee754.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698