dl.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Brian Schaffner <brian@tool.net> |
  14. | Shane Caraveo <shane@caraveo.com> |
  15. | Zeev Suraski <zeev@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include "dl.h"
  20. #include "php_globals.h"
  21. #include "php_ini.h"
  22. #include "ext/standard/info.h"
  23. #include "SAPI.h"
  24. #if defined(HAVE_LIBDL)
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #ifdef PHP_WIN32
  29. #include "win32/param.h"
  30. #include "win32/winutil.h"
  31. #define GET_DL_ERROR() php_win_err()
  32. #else
  33. #include <sys/param.h>
  34. #define GET_DL_ERROR() DL_ERROR()
  35. #endif
  36. #endif /* defined(HAVE_LIBDL) */
  37. /* {{{ Load a PHP extension at runtime */
  38. PHPAPI PHP_FUNCTION(dl)
  39. {
  40. char *filename;
  41. size_t filename_len;
  42. ZEND_PARSE_PARAMETERS_START(1, 1)
  43. Z_PARAM_STRING(filename, filename_len)
  44. ZEND_PARSE_PARAMETERS_END();
  45. if (!PG(enable_dl)) {
  46. php_error_docref(NULL, E_WARNING, "Dynamically loaded extensions aren't enabled");
  47. RETURN_FALSE;
  48. }
  49. if (filename_len >= MAXPATHLEN) {
  50. php_error_docref(NULL, E_WARNING, "Filename exceeds the maximum allowed length of %d characters", MAXPATHLEN);
  51. RETURN_FALSE;
  52. }
  53. #if ZEND_RC_DEBUG
  54. bool orig_rc_debug = zend_rc_debug;
  55. /* FIXME: Loading extensions during the request breaks some invariants. In
  56. * particular, it will create persistent interned strings, which is not
  57. * allowed at this stage. */
  58. zend_rc_debug = false;
  59. #endif
  60. php_dl(filename, MODULE_TEMPORARY, return_value, 0);
  61. if (Z_TYPE_P(return_value) == IS_TRUE) {
  62. EG(full_tables_cleanup) = 1;
  63. }
  64. #if ZEND_RC_DEBUG
  65. zend_rc_debug = orig_rc_debug;
  66. #endif
  67. }
  68. /* }}} */
  69. #if defined(HAVE_LIBDL)
  70. /* {{{ php_load_shlib */
  71. PHPAPI void *php_load_shlib(const char *path, char **errp)
  72. {
  73. void *handle;
  74. char *err;
  75. handle = DL_LOAD(path);
  76. if (!handle) {
  77. err = GET_DL_ERROR();
  78. #ifdef PHP_WIN32
  79. if (err && (*err)) {
  80. size_t i = strlen(err);
  81. (*errp)=estrdup(err);
  82. php_win32_error_msg_free(err);
  83. while (i > 0 && isspace((*errp)[i-1])) { (*errp)[i-1] = '\0'; i--; }
  84. } else {
  85. (*errp) = estrdup("<No message>");
  86. }
  87. #else
  88. (*errp) = estrdup(err);
  89. GET_DL_ERROR(); /* free the buffer storing the error */
  90. #endif
  91. }
  92. return handle;
  93. }
  94. /* }}} */
  95. /* {{{ php_load_extension */
  96. PHPAPI int php_load_extension(const char *filename, int type, int start_now)
  97. {
  98. void *handle;
  99. char *libpath;
  100. zend_module_entry *module_entry;
  101. zend_module_entry *(*get_module)(void);
  102. int error_type, slash_suffix = 0;
  103. char *extension_dir;
  104. char *err1, *err2;
  105. if (type == MODULE_PERSISTENT) {
  106. extension_dir = INI_STR("extension_dir");
  107. } else {
  108. extension_dir = PG(extension_dir);
  109. }
  110. if (type == MODULE_TEMPORARY) {
  111. error_type = E_WARNING;
  112. } else {
  113. error_type = E_CORE_WARNING;
  114. }
  115. /* Check if passed filename contains directory separators */
  116. if (strchr(filename, '/') != NULL || strchr(filename, DEFAULT_SLASH) != NULL) {
  117. /* Passing modules with full path is not supported for dynamically loaded extensions */
  118. if (type == MODULE_TEMPORARY) {
  119. php_error_docref(NULL, E_WARNING, "Temporary module name should contain only filename");
  120. return FAILURE;
  121. }
  122. libpath = estrdup(filename);
  123. } else if (extension_dir && extension_dir[0]) {
  124. slash_suffix = IS_SLASH(extension_dir[strlen(extension_dir)-1]);
  125. /* Try as filename first */
  126. if (slash_suffix) {
  127. spprintf(&libpath, 0, "%s%s", extension_dir, filename); /* SAFE */
  128. } else {
  129. spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename); /* SAFE */
  130. }
  131. } else {
  132. return FAILURE; /* Not full path given or extension_dir is not set */
  133. }
  134. handle = php_load_shlib(libpath, &err1);
  135. if (!handle) {
  136. /* Now, consider 'filename' as extension name and build file name */
  137. char *orig_libpath = libpath;
  138. if (slash_suffix) {
  139. spprintf(&libpath, 0, "%s" PHP_SHLIB_EXT_PREFIX "%s." PHP_SHLIB_SUFFIX, extension_dir, filename); /* SAFE */
  140. } else {
  141. spprintf(&libpath, 0, "%s%c" PHP_SHLIB_EXT_PREFIX "%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, filename); /* SAFE */
  142. }
  143. handle = php_load_shlib(libpath, &err2);
  144. if (!handle) {
  145. php_error_docref(NULL, error_type, "Unable to load dynamic library '%s' (tried: %s (%s), %s (%s))",
  146. filename, orig_libpath, err1, libpath, err2);
  147. efree(orig_libpath);
  148. efree(err1);
  149. efree(libpath);
  150. efree(err2);
  151. return FAILURE;
  152. }
  153. efree(orig_libpath);
  154. efree(err1);
  155. }
  156. efree(libpath);
  157. #ifdef PHP_WIN32
  158. if (!php_win32_image_compatible(handle, &err1)) {
  159. php_error_docref(NULL, error_type, err1);
  160. efree(err1);
  161. DL_UNLOAD(handle);
  162. return FAILURE;
  163. }
  164. #endif
  165. get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");
  166. /* Some OS prepend _ to symbol names while their dynamic linker
  167. * does not do that automatically. Thus we check manually for
  168. * _get_module. */
  169. if (!get_module) {
  170. get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");
  171. }
  172. if (!get_module) {
  173. if (DL_FETCH_SYMBOL(handle, "zend_extension_entry") || DL_FETCH_SYMBOL(handle, "_zend_extension_entry")) {
  174. DL_UNLOAD(handle);
  175. php_error_docref(NULL, error_type, "Invalid library (appears to be a Zend Extension, try loading using zend_extension=%s from php.ini)", filename);
  176. return FAILURE;
  177. }
  178. DL_UNLOAD(handle);
  179. php_error_docref(NULL, error_type, "Invalid library (maybe not a PHP library) '%s'", filename);
  180. return FAILURE;
  181. }
  182. module_entry = get_module();
  183. if (module_entry->zend_api != ZEND_MODULE_API_NO) {
  184. php_error_docref(NULL, error_type,
  185. "%s: Unable to initialize module\n"
  186. "Module compiled with module API=%d\n"
  187. "PHP compiled with module API=%d\n"
  188. "These options need to match\n",
  189. module_entry->name, module_entry->zend_api, ZEND_MODULE_API_NO);
  190. DL_UNLOAD(handle);
  191. return FAILURE;
  192. }
  193. if(strcmp(module_entry->build_id, ZEND_MODULE_BUILD_ID)) {
  194. php_error_docref(NULL, error_type,
  195. "%s: Unable to initialize module\n"
  196. "Module compiled with build ID=%s\n"
  197. "PHP compiled with build ID=%s\n"
  198. "These options need to match\n",
  199. module_entry->name, module_entry->build_id, ZEND_MODULE_BUILD_ID);
  200. DL_UNLOAD(handle);
  201. return FAILURE;
  202. }
  203. module_entry->type = type;
  204. module_entry->module_number = zend_next_free_module();
  205. module_entry->handle = handle;
  206. if ((module_entry = zend_register_module_ex(module_entry)) == NULL) {
  207. DL_UNLOAD(handle);
  208. return FAILURE;
  209. }
  210. if ((type == MODULE_TEMPORARY || start_now) && zend_startup_module_ex(module_entry) == FAILURE) {
  211. DL_UNLOAD(handle);
  212. return FAILURE;
  213. }
  214. if ((type == MODULE_TEMPORARY || start_now) && module_entry->request_startup_func) {
  215. if (module_entry->request_startup_func(type, module_entry->module_number) == FAILURE) {
  216. php_error_docref(NULL, error_type, "Unable to initialize module '%s'", module_entry->name);
  217. DL_UNLOAD(handle);
  218. return FAILURE;
  219. }
  220. }
  221. return SUCCESS;
  222. }
  223. /* }}} */
  224. #else
  225. static void php_dl_error(const char *filename)
  226. {
  227. php_error_docref(NULL, E_WARNING, "Cannot dynamically load %s - dynamic modules are not supported", filename);
  228. }
  229. PHPAPI void *php_load_shlib(const char *path, char **errp)
  230. {
  231. php_dl_error(path);
  232. (*errp) = estrdup("No DL support");
  233. return NULL;
  234. }
  235. PHPAPI int php_load_extension(const char *filename, int type, int start_now)
  236. {
  237. php_dl_error(filename);
  238. return FAILURE;
  239. }
  240. #endif
  241. /* {{{ php_dl */
  242. PHPAPI void php_dl(const char *file, int type, zval *return_value, int start_now)
  243. {
  244. /* Load extension */
  245. if (php_load_extension(file, type, start_now) == FAILURE) {
  246. RETVAL_FALSE;
  247. } else {
  248. RETVAL_TRUE;
  249. }
  250. }
  251. /* }}} */
  252. PHP_MINFO_FUNCTION(dl)
  253. {
  254. #if defined(HAVE_LIBDL)
  255. #define PHP_DL_SUPPORT_STATUS "enabled"
  256. #else
  257. #define PHP_DL_SUPPORT_STATUS "unavailable"
  258. #endif
  259. php_info_print_table_row(2, "Dynamic Library Support", PHP_DL_SUPPORT_STATUS);
  260. }