xref: /sqlite-3.40.0/src/loadext.c (revision 48864df9)
1 /*
2 ** 2006 June 7
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains code used to dynamically load extensions into
13 ** the SQLite library.
14 */
15 
16 #ifndef SQLITE_CORE
17   #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
18 #endif
19 #include "sqlite3ext.h"
20 #include "sqliteInt.h"
21 #include <string.h>
22 
23 #ifndef SQLITE_OMIT_LOAD_EXTENSION
24 
25 /*
26 ** Some API routines are omitted when various features are
27 ** excluded from a build of SQLite.  Substitute a NULL pointer
28 ** for any missing APIs.
29 */
30 #ifndef SQLITE_ENABLE_COLUMN_METADATA
31 # define sqlite3_column_database_name   0
32 # define sqlite3_column_database_name16 0
33 # define sqlite3_column_table_name      0
34 # define sqlite3_column_table_name16    0
35 # define sqlite3_column_origin_name     0
36 # define sqlite3_column_origin_name16   0
37 # define sqlite3_table_column_metadata  0
38 #endif
39 
40 #ifdef SQLITE_OMIT_AUTHORIZATION
41 # define sqlite3_set_authorizer         0
42 #endif
43 
44 #ifdef SQLITE_OMIT_UTF16
45 # define sqlite3_bind_text16            0
46 # define sqlite3_collation_needed16     0
47 # define sqlite3_column_decltype16      0
48 # define sqlite3_column_name16          0
49 # define sqlite3_column_text16          0
50 # define sqlite3_complete16             0
51 # define sqlite3_create_collation16     0
52 # define sqlite3_create_function16      0
53 # define sqlite3_errmsg16               0
54 # define sqlite3_open16                 0
55 # define sqlite3_prepare16              0
56 # define sqlite3_prepare16_v2           0
57 # define sqlite3_result_error16         0
58 # define sqlite3_result_text16          0
59 # define sqlite3_result_text16be        0
60 # define sqlite3_result_text16le        0
61 # define sqlite3_value_text16           0
62 # define sqlite3_value_text16be         0
63 # define sqlite3_value_text16le         0
64 # define sqlite3_column_database_name16 0
65 # define sqlite3_column_table_name16    0
66 # define sqlite3_column_origin_name16   0
67 #endif
68 
69 #ifdef SQLITE_OMIT_COMPLETE
70 # define sqlite3_complete 0
71 # define sqlite3_complete16 0
72 #endif
73 
74 #ifdef SQLITE_OMIT_DECLTYPE
75 # define sqlite3_column_decltype16      0
76 # define sqlite3_column_decltype        0
77 #endif
78 
79 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
80 # define sqlite3_progress_handler 0
81 #endif
82 
83 #ifdef SQLITE_OMIT_VIRTUALTABLE
84 # define sqlite3_create_module 0
85 # define sqlite3_create_module_v2 0
86 # define sqlite3_declare_vtab 0
87 # define sqlite3_vtab_config 0
88 # define sqlite3_vtab_on_conflict 0
89 #endif
90 
91 #ifdef SQLITE_OMIT_SHARED_CACHE
92 # define sqlite3_enable_shared_cache 0
93 #endif
94 
95 #ifdef SQLITE_OMIT_TRACE
96 # define sqlite3_profile       0
97 # define sqlite3_trace         0
98 #endif
99 
100 #ifdef SQLITE_OMIT_GET_TABLE
101 # define sqlite3_free_table    0
102 # define sqlite3_get_table     0
103 #endif
104 
105 #ifdef SQLITE_OMIT_INCRBLOB
106 #define sqlite3_bind_zeroblob  0
107 #define sqlite3_blob_bytes     0
108 #define sqlite3_blob_close     0
109 #define sqlite3_blob_open      0
110 #define sqlite3_blob_read      0
111 #define sqlite3_blob_write     0
112 #define sqlite3_blob_reopen    0
113 #endif
114 
115 /*
116 ** The following structure contains pointers to all SQLite API routines.
117 ** A pointer to this structure is passed into extensions when they are
118 ** loaded so that the extension can make calls back into the SQLite
119 ** library.
120 **
121 ** When adding new APIs, add them to the bottom of this structure
122 ** in order to preserve backwards compatibility.
123 **
124 ** Extensions that use newer APIs should first call the
125 ** sqlite3_libversion_number() to make sure that the API they
126 ** intend to use is supported by the library.  Extensions should
127 ** also check to make sure that the pointer to the function is
128 ** not NULL before calling it.
129 */
130 static const sqlite3_api_routines sqlite3Apis = {
131   sqlite3_aggregate_context,
132 #ifndef SQLITE_OMIT_DEPRECATED
133   sqlite3_aggregate_count,
134 #else
135   0,
136 #endif
137   sqlite3_bind_blob,
138   sqlite3_bind_double,
139   sqlite3_bind_int,
140   sqlite3_bind_int64,
141   sqlite3_bind_null,
142   sqlite3_bind_parameter_count,
143   sqlite3_bind_parameter_index,
144   sqlite3_bind_parameter_name,
145   sqlite3_bind_text,
146   sqlite3_bind_text16,
147   sqlite3_bind_value,
148   sqlite3_busy_handler,
149   sqlite3_busy_timeout,
150   sqlite3_changes,
151   sqlite3_close,
152   sqlite3_collation_needed,
153   sqlite3_collation_needed16,
154   sqlite3_column_blob,
155   sqlite3_column_bytes,
156   sqlite3_column_bytes16,
157   sqlite3_column_count,
158   sqlite3_column_database_name,
159   sqlite3_column_database_name16,
160   sqlite3_column_decltype,
161   sqlite3_column_decltype16,
162   sqlite3_column_double,
163   sqlite3_column_int,
164   sqlite3_column_int64,
165   sqlite3_column_name,
166   sqlite3_column_name16,
167   sqlite3_column_origin_name,
168   sqlite3_column_origin_name16,
169   sqlite3_column_table_name,
170   sqlite3_column_table_name16,
171   sqlite3_column_text,
172   sqlite3_column_text16,
173   sqlite3_column_type,
174   sqlite3_column_value,
175   sqlite3_commit_hook,
176   sqlite3_complete,
177   sqlite3_complete16,
178   sqlite3_create_collation,
179   sqlite3_create_collation16,
180   sqlite3_create_function,
181   sqlite3_create_function16,
182   sqlite3_create_module,
183   sqlite3_data_count,
184   sqlite3_db_handle,
185   sqlite3_declare_vtab,
186   sqlite3_enable_shared_cache,
187   sqlite3_errcode,
188   sqlite3_errmsg,
189   sqlite3_errmsg16,
190   sqlite3_exec,
191 #ifndef SQLITE_OMIT_DEPRECATED
192   sqlite3_expired,
193 #else
194   0,
195 #endif
196   sqlite3_finalize,
197   sqlite3_free,
198   sqlite3_free_table,
199   sqlite3_get_autocommit,
200   sqlite3_get_auxdata,
201   sqlite3_get_table,
202   0,     /* Was sqlite3_global_recover(), but that function is deprecated */
203   sqlite3_interrupt,
204   sqlite3_last_insert_rowid,
205   sqlite3_libversion,
206   sqlite3_libversion_number,
207   sqlite3_malloc,
208   sqlite3_mprintf,
209   sqlite3_open,
210   sqlite3_open16,
211   sqlite3_prepare,
212   sqlite3_prepare16,
213   sqlite3_profile,
214   sqlite3_progress_handler,
215   sqlite3_realloc,
216   sqlite3_reset,
217   sqlite3_result_blob,
218   sqlite3_result_double,
219   sqlite3_result_error,
220   sqlite3_result_error16,
221   sqlite3_result_int,
222   sqlite3_result_int64,
223   sqlite3_result_null,
224   sqlite3_result_text,
225   sqlite3_result_text16,
226   sqlite3_result_text16be,
227   sqlite3_result_text16le,
228   sqlite3_result_value,
229   sqlite3_rollback_hook,
230   sqlite3_set_authorizer,
231   sqlite3_set_auxdata,
232   sqlite3_snprintf,
233   sqlite3_step,
234   sqlite3_table_column_metadata,
235 #ifndef SQLITE_OMIT_DEPRECATED
236   sqlite3_thread_cleanup,
237 #else
238   0,
239 #endif
240   sqlite3_total_changes,
241   sqlite3_trace,
242 #ifndef SQLITE_OMIT_DEPRECATED
243   sqlite3_transfer_bindings,
244 #else
245   0,
246 #endif
247   sqlite3_update_hook,
248   sqlite3_user_data,
249   sqlite3_value_blob,
250   sqlite3_value_bytes,
251   sqlite3_value_bytes16,
252   sqlite3_value_double,
253   sqlite3_value_int,
254   sqlite3_value_int64,
255   sqlite3_value_numeric_type,
256   sqlite3_value_text,
257   sqlite3_value_text16,
258   sqlite3_value_text16be,
259   sqlite3_value_text16le,
260   sqlite3_value_type,
261   sqlite3_vmprintf,
262   /*
263   ** The original API set ends here.  All extensions can call any
264   ** of the APIs above provided that the pointer is not NULL.  But
265   ** before calling APIs that follow, extension should check the
266   ** sqlite3_libversion_number() to make sure they are dealing with
267   ** a library that is new enough to support that API.
268   *************************************************************************
269   */
270   sqlite3_overload_function,
271 
272   /*
273   ** Added after 3.3.13
274   */
275   sqlite3_prepare_v2,
276   sqlite3_prepare16_v2,
277   sqlite3_clear_bindings,
278 
279   /*
280   ** Added for 3.4.1
281   */
282   sqlite3_create_module_v2,
283 
284   /*
285   ** Added for 3.5.0
286   */
287   sqlite3_bind_zeroblob,
288   sqlite3_blob_bytes,
289   sqlite3_blob_close,
290   sqlite3_blob_open,
291   sqlite3_blob_read,
292   sqlite3_blob_write,
293   sqlite3_create_collation_v2,
294   sqlite3_file_control,
295   sqlite3_memory_highwater,
296   sqlite3_memory_used,
297 #ifdef SQLITE_MUTEX_OMIT
298   0,
299   0,
300   0,
301   0,
302   0,
303 #else
304   sqlite3_mutex_alloc,
305   sqlite3_mutex_enter,
306   sqlite3_mutex_free,
307   sqlite3_mutex_leave,
308   sqlite3_mutex_try,
309 #endif
310   sqlite3_open_v2,
311   sqlite3_release_memory,
312   sqlite3_result_error_nomem,
313   sqlite3_result_error_toobig,
314   sqlite3_sleep,
315   sqlite3_soft_heap_limit,
316   sqlite3_vfs_find,
317   sqlite3_vfs_register,
318   sqlite3_vfs_unregister,
319 
320   /*
321   ** Added for 3.5.8
322   */
323   sqlite3_threadsafe,
324   sqlite3_result_zeroblob,
325   sqlite3_result_error_code,
326   sqlite3_test_control,
327   sqlite3_randomness,
328   sqlite3_context_db_handle,
329 
330   /*
331   ** Added for 3.6.0
332   */
333   sqlite3_extended_result_codes,
334   sqlite3_limit,
335   sqlite3_next_stmt,
336   sqlite3_sql,
337   sqlite3_status,
338 
339   /*
340   ** Added for 3.7.4
341   */
342   sqlite3_backup_finish,
343   sqlite3_backup_init,
344   sqlite3_backup_pagecount,
345   sqlite3_backup_remaining,
346   sqlite3_backup_step,
347 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
348   sqlite3_compileoption_get,
349   sqlite3_compileoption_used,
350 #else
351   0,
352   0,
353 #endif
354   sqlite3_create_function_v2,
355   sqlite3_db_config,
356   sqlite3_db_mutex,
357   sqlite3_db_status,
358   sqlite3_extended_errcode,
359   sqlite3_log,
360   sqlite3_soft_heap_limit64,
361   sqlite3_sourceid,
362   sqlite3_stmt_status,
363   sqlite3_strnicmp,
364 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
365   sqlite3_unlock_notify,
366 #else
367   0,
368 #endif
369 #ifndef SQLITE_OMIT_WAL
370   sqlite3_wal_autocheckpoint,
371   sqlite3_wal_checkpoint,
372   sqlite3_wal_hook,
373 #else
374   0,
375   0,
376   0,
377 #endif
378   sqlite3_blob_reopen,
379   sqlite3_vtab_config,
380   sqlite3_vtab_on_conflict,
381   sqlite3_close_v2,
382   sqlite3_db_filename,
383   sqlite3_db_readonly,
384   sqlite3_db_release_memory,
385   sqlite3_errstr,
386   sqlite3_stmt_busy,
387   sqlite3_stmt_readonly,
388   sqlite3_stricmp,
389   sqlite3_uri_boolean,
390   sqlite3_uri_int64,
391   sqlite3_uri_parameter,
392   sqlite3_vsnprintf,
393   sqlite3_wal_checkpoint_v2
394 };
395 
396 /*
397 ** Attempt to load an SQLite extension library contained in the file
398 ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
399 ** default entry point name (sqlite3_extension_init) is used.  Use
400 ** of the default name is recommended.
401 **
402 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
403 **
404 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
405 ** error message text.  The calling function should free this memory
406 ** by calling sqlite3DbFree(db, ).
407 */
408 static int sqlite3LoadExtension(
409   sqlite3 *db,          /* Load the extension into this database connection */
410   const char *zFile,    /* Name of the shared library containing extension */
411   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
412   char **pzErrMsg       /* Put error message here if not 0 */
413 ){
414   sqlite3_vfs *pVfs = db->pVfs;
415   void *handle;
416   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
417   char *zErrmsg = 0;
418   void **aHandle;
419   int nMsg = 300 + sqlite3Strlen30(zFile);
420 
421   if( pzErrMsg ) *pzErrMsg = 0;
422 
423   /* Ticket #1863.  To avoid a creating security problems for older
424   ** applications that relink against newer versions of SQLite, the
425   ** ability to run load_extension is turned off by default.  One
426   ** must call sqlite3_enable_load_extension() to turn on extension
427   ** loading.  Otherwise you get the following error.
428   */
429   if( (db->flags & SQLITE_LoadExtension)==0 ){
430     if( pzErrMsg ){
431       *pzErrMsg = sqlite3_mprintf("not authorized");
432     }
433     return SQLITE_ERROR;
434   }
435 
436   if( zProc==0 ){
437     zProc = "sqlite3_extension_init";
438   }
439 
440   handle = sqlite3OsDlOpen(pVfs, zFile);
441   if( handle==0 ){
442     if( pzErrMsg ){
443       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
444       if( zErrmsg ){
445         sqlite3_snprintf(nMsg, zErrmsg,
446             "unable to open shared library [%s]", zFile);
447         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
448       }
449     }
450     return SQLITE_ERROR;
451   }
452   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
453                    sqlite3OsDlSym(pVfs, handle, zProc);
454   if( xInit==0 ){
455     if( pzErrMsg ){
456       nMsg += sqlite3Strlen30(zProc);
457       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
458       if( zErrmsg ){
459         sqlite3_snprintf(nMsg, zErrmsg,
460             "no entry point [%s] in shared library [%s]", zProc,zFile);
461         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
462       }
463       sqlite3OsDlClose(pVfs, handle);
464     }
465     return SQLITE_ERROR;
466   }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
467     if( pzErrMsg ){
468       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
469     }
470     sqlite3_free(zErrmsg);
471     sqlite3OsDlClose(pVfs, handle);
472     return SQLITE_ERROR;
473   }
474 
475   /* Append the new shared library handle to the db->aExtension array. */
476   aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
477   if( aHandle==0 ){
478     return SQLITE_NOMEM;
479   }
480   if( db->nExtension>0 ){
481     memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
482   }
483   sqlite3DbFree(db, db->aExtension);
484   db->aExtension = aHandle;
485 
486   db->aExtension[db->nExtension++] = handle;
487   return SQLITE_OK;
488 }
489 int sqlite3_load_extension(
490   sqlite3 *db,          /* Load the extension into this database connection */
491   const char *zFile,    /* Name of the shared library containing extension */
492   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
493   char **pzErrMsg       /* Put error message here if not 0 */
494 ){
495   int rc;
496   sqlite3_mutex_enter(db->mutex);
497   rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
498   rc = sqlite3ApiExit(db, rc);
499   sqlite3_mutex_leave(db->mutex);
500   return rc;
501 }
502 
503 /*
504 ** Call this routine when the database connection is closing in order
505 ** to clean up loaded extensions
506 */
507 void sqlite3CloseExtensions(sqlite3 *db){
508   int i;
509   assert( sqlite3_mutex_held(db->mutex) );
510   for(i=0; i<db->nExtension; i++){
511     sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
512   }
513   sqlite3DbFree(db, db->aExtension);
514 }
515 
516 /*
517 ** Enable or disable extension loading.  Extension loading is disabled by
518 ** default so as not to open security holes in older applications.
519 */
520 int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
521   sqlite3_mutex_enter(db->mutex);
522   if( onoff ){
523     db->flags |= SQLITE_LoadExtension;
524   }else{
525     db->flags &= ~SQLITE_LoadExtension;
526   }
527   sqlite3_mutex_leave(db->mutex);
528   return SQLITE_OK;
529 }
530 
531 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
532 
533 /*
534 ** The auto-extension code added regardless of whether or not extension
535 ** loading is supported.  We need a dummy sqlite3Apis pointer for that
536 ** code if regular extension loading is not available.  This is that
537 ** dummy pointer.
538 */
539 #ifdef SQLITE_OMIT_LOAD_EXTENSION
540 static const sqlite3_api_routines sqlite3Apis = { 0 };
541 #endif
542 
543 
544 /*
545 ** The following object holds the list of automatically loaded
546 ** extensions.
547 **
548 ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
549 ** mutex must be held while accessing this list.
550 */
551 typedef struct sqlite3AutoExtList sqlite3AutoExtList;
552 static SQLITE_WSD struct sqlite3AutoExtList {
553   int nExt;              /* Number of entries in aExt[] */
554   void (**aExt)(void);   /* Pointers to the extension init functions */
555 } sqlite3Autoext = { 0, 0 };
556 
557 /* The "wsdAutoext" macro will resolve to the autoextension
558 ** state vector.  If writable static data is unsupported on the target,
559 ** we have to locate the state vector at run-time.  In the more common
560 ** case where writable static data is supported, wsdStat can refer directly
561 ** to the "sqlite3Autoext" state vector declared above.
562 */
563 #ifdef SQLITE_OMIT_WSD
564 # define wsdAutoextInit \
565   sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
566 # define wsdAutoext x[0]
567 #else
568 # define wsdAutoextInit
569 # define wsdAutoext sqlite3Autoext
570 #endif
571 
572 
573 /*
574 ** Register a statically linked extension that is automatically
575 ** loaded by every new database connection.
576 */
577 int sqlite3_auto_extension(void (*xInit)(void)){
578   int rc = SQLITE_OK;
579 #ifndef SQLITE_OMIT_AUTOINIT
580   rc = sqlite3_initialize();
581   if( rc ){
582     return rc;
583   }else
584 #endif
585   {
586     int i;
587 #if SQLITE_THREADSAFE
588     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
589 #endif
590     wsdAutoextInit;
591     sqlite3_mutex_enter(mutex);
592     for(i=0; i<wsdAutoext.nExt; i++){
593       if( wsdAutoext.aExt[i]==xInit ) break;
594     }
595     if( i==wsdAutoext.nExt ){
596       int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
597       void (**aNew)(void);
598       aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
599       if( aNew==0 ){
600         rc = SQLITE_NOMEM;
601       }else{
602         wsdAutoext.aExt = aNew;
603         wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
604         wsdAutoext.nExt++;
605       }
606     }
607     sqlite3_mutex_leave(mutex);
608     assert( (rc&0xff)==rc );
609     return rc;
610   }
611 }
612 
613 /*
614 ** Reset the automatic extension loading mechanism.
615 */
616 void sqlite3_reset_auto_extension(void){
617 #ifndef SQLITE_OMIT_AUTOINIT
618   if( sqlite3_initialize()==SQLITE_OK )
619 #endif
620   {
621 #if SQLITE_THREADSAFE
622     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
623 #endif
624     wsdAutoextInit;
625     sqlite3_mutex_enter(mutex);
626     sqlite3_free(wsdAutoext.aExt);
627     wsdAutoext.aExt = 0;
628     wsdAutoext.nExt = 0;
629     sqlite3_mutex_leave(mutex);
630   }
631 }
632 
633 /*
634 ** Load all automatic extensions.
635 **
636 ** If anything goes wrong, set an error in the database connection.
637 */
638 void sqlite3AutoLoadExtensions(sqlite3 *db){
639   int i;
640   int go = 1;
641   int rc;
642   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
643 
644   wsdAutoextInit;
645   if( wsdAutoext.nExt==0 ){
646     /* Common case: early out without every having to acquire a mutex */
647     return;
648   }
649   for(i=0; go; i++){
650     char *zErrmsg;
651 #if SQLITE_THREADSAFE
652     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
653 #endif
654     sqlite3_mutex_enter(mutex);
655     if( i>=wsdAutoext.nExt ){
656       xInit = 0;
657       go = 0;
658     }else{
659       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
660               wsdAutoext.aExt[i];
661     }
662     sqlite3_mutex_leave(mutex);
663     zErrmsg = 0;
664     if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
665       sqlite3Error(db, rc,
666             "automatic extension loading failed: %s", zErrmsg);
667       go = 0;
668     }
669     sqlite3_free(zErrmsg);
670   }
671 }
672