protobuf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include "protobuf.h"
  31. #include <php.h>
  32. #include <Zend/zend_interfaces.h>
  33. #include "arena.h"
  34. #include "array.h"
  35. #include "bundled_php.h"
  36. #include "convert.h"
  37. #include "def.h"
  38. #include "map.h"
  39. #include "message.h"
  40. #include "names.h"
  41. // -----------------------------------------------------------------------------
  42. // Module "globals"
  43. // -----------------------------------------------------------------------------
  44. // Despite the name, module "globals" are really thread-locals:
  45. // * PROTOBUF_G(var) accesses the thread-local variable for 'var'. Either:
  46. // * PROTOBUF_G(var) -> protobuf_globals.var (Non-ZTS / non-thread-safe)
  47. // * PROTOBUF_G(var) -> <Zend magic> (ZTS / thread-safe builds)
  48. #define PROTOBUF_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(protobuf, v)
  49. ZEND_BEGIN_MODULE_GLOBALS(protobuf)
  50. // Set by the user to make the descriptor pool persist between requests.
  51. zend_bool keep_descriptor_pool_after_request;
  52. // Currently we make the generated pool a "global", which means that if a user
  53. // does explicitly create threads within their request, the other threads will
  54. // get different results from DescriptorPool::getGeneratedPool(). We require
  55. // that all descriptors are loaded from the main thread.
  56. zval generated_pool;
  57. // A upb_symtab that we are saving for the next request so that we don't have
  58. // to rebuild it from scratch. When keep_descriptor_pool_after_request==true,
  59. // we steal the upb_symtab from the global DescriptorPool object just before
  60. // destroying it.
  61. upb_symtab *saved_symtab;
  62. // Object cache (see interface in protobuf.h).
  63. HashTable object_cache;
  64. // Name cache (see interface in protobuf.h).
  65. HashTable name_msg_cache;
  66. HashTable name_enum_cache;
  67. ZEND_END_MODULE_GLOBALS(protobuf)
  68. ZEND_DECLARE_MODULE_GLOBALS(protobuf)
  69. const zval *get_generated_pool() {
  70. return &PROTOBUF_G(generated_pool);
  71. }
  72. // This is a PHP extension (not a Zend extension). What follows is a summary of
  73. // a PHP extension's lifetime and when various handlers are called.
  74. //
  75. // * PHP_GINIT_FUNCTION(protobuf) / PHP_GSHUTDOWN_FUNCTION(protobuf)
  76. // are the constructor/destructor for the globals. The sequence over the
  77. // course of a process lifetime is:
  78. //
  79. // # Process startup
  80. // GINIT(<Main Thread Globals>)
  81. // MINIT
  82. //
  83. // foreach request:
  84. // RINIT
  85. // # Request is processed here.
  86. // RSHUTDOWN
  87. //
  88. // foreach thread:
  89. // GINIT(<This Thread Globals>)
  90. // # Code for the thread runs here.
  91. // GSHUTDOWN(<This Thread Globals>)
  92. //
  93. // # Process Shutdown
  94. // #
  95. // # These should be running per the docs, but I have not been able to
  96. // # actually get the process-wide shutdown functions to run.
  97. // #
  98. // # MSHUTDOWN
  99. // # GSHUTDOWN(<Main Thread Globals>)
  100. //
  101. // * Threads can be created either explicitly by the user, inside a request,
  102. // or implicitly by the runtime, to process multiple requests concurrently.
  103. // If the latter is being used, then the "foreach thread" block above
  104. // actually looks like this:
  105. //
  106. // foreach thread:
  107. // GINIT(<This Thread Globals>)
  108. // # A non-main thread will only receive requests when using a threaded
  109. // # MPM with Apache
  110. // foreach request:
  111. // RINIT
  112. // # Request is processed here.
  113. // RSHUTDOWN
  114. // GSHUTDOWN(<This Thread Globals>)
  115. //
  116. // That said, it appears that few people use threads with PHP:
  117. // * The pthread package documented at
  118. // https://www.php.net/manual/en/class.thread.php nas not been released
  119. // since 2016, and the current release fails to compile against any PHP
  120. // newer than 7.0.33.
  121. // * The GitHub master branch supports 7.2+, but this has not been released
  122. // to PECL.
  123. // * Its owner has disavowed it as "broken by design" and "in an untenable
  124. // position for the future": https://github.com/krakjoe/pthreads/issues/929
  125. // * The only way to use PHP with requests in different threads is to use the
  126. // Apache 2 mod_php with the "worker" MPM. But this is explicitly
  127. // discouraged by the documentation: https://serverfault.com/a/231660
  128. static PHP_GSHUTDOWN_FUNCTION(protobuf) {
  129. if (protobuf_globals->saved_symtab) {
  130. upb_symtab_free(protobuf_globals->saved_symtab);
  131. }
  132. }
  133. static PHP_GINIT_FUNCTION(protobuf) {
  134. ZVAL_NULL(&protobuf_globals->generated_pool);
  135. protobuf_globals->saved_symtab = NULL;
  136. }
  137. /**
  138. * PHP_RINIT_FUNCTION(protobuf)
  139. *
  140. * This function is run at the beginning of processing each request.
  141. */
  142. static PHP_RINIT_FUNCTION(protobuf) {
  143. // Create the global generated pool.
  144. // Reuse the symtab (if any) left to us by the last request.
  145. upb_symtab *symtab = PROTOBUF_G(saved_symtab);
  146. DescriptorPool_CreateWithSymbolTable(&PROTOBUF_G(generated_pool), symtab);
  147. // Set up autoloader for bundled sources.
  148. zend_eval_string("spl_autoload_register('protobuf_internal_loadbundled');",
  149. NULL, "autoload_register.php");
  150. zend_hash_init(&PROTOBUF_G(object_cache), 64, NULL, NULL, 0);
  151. zend_hash_init(&PROTOBUF_G(name_msg_cache), 64, NULL, NULL, 0);
  152. zend_hash_init(&PROTOBUF_G(name_enum_cache), 64, NULL, NULL, 0);
  153. return SUCCESS;
  154. }
  155. /**
  156. * PHP_RSHUTDOWN_FUNCTION(protobuf)
  157. *
  158. * This function is run at the end of processing each request.
  159. */
  160. static PHP_RSHUTDOWN_FUNCTION(protobuf) {
  161. // Preserve the symtab if requested.
  162. if (PROTOBUF_G(keep_descriptor_pool_after_request)) {
  163. zval *zv = &PROTOBUF_G(generated_pool);
  164. PROTOBUF_G(saved_symtab) = DescriptorPool_Steal(zv);
  165. }
  166. zval_dtor(&PROTOBUF_G(generated_pool));
  167. zend_hash_destroy(&PROTOBUF_G(object_cache));
  168. zend_hash_destroy(&PROTOBUF_G(name_msg_cache));
  169. zend_hash_destroy(&PROTOBUF_G(name_enum_cache));
  170. return SUCCESS;
  171. }
  172. // -----------------------------------------------------------------------------
  173. // Bundled PHP sources
  174. // -----------------------------------------------------------------------------
  175. // We bundle PHP sources for well-known types into the C extension. There is no
  176. // need to implement these in C.
  177. static PHP_FUNCTION(protobuf_internal_loadbundled) {
  178. char *name = NULL;
  179. zend_long size;
  180. BundledPhp_File *file;
  181. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &size) != SUCCESS) {
  182. return;
  183. }
  184. for (file = bundled_files; file->filename; file++) {
  185. if (strcmp(file->filename, name) == 0) {
  186. zend_eval_string((char*)file->contents, NULL, (char*)file->filename);
  187. return;
  188. }
  189. }
  190. }
  191. ZEND_BEGIN_ARG_INFO_EX(arginfo_load_bundled_source, 0, 0, 1)
  192. ZEND_ARG_INFO(0, class_name)
  193. ZEND_END_ARG_INFO()
  194. // -----------------------------------------------------------------------------
  195. // Object Cache.
  196. // -----------------------------------------------------------------------------
  197. void ObjCache_Add(const void *upb_obj, zend_object *php_obj) {
  198. zend_ulong k = (zend_ulong)upb_obj;
  199. zend_hash_index_add_ptr(&PROTOBUF_G(object_cache), k, php_obj);
  200. }
  201. void ObjCache_Delete(const void *upb_obj) {
  202. if (upb_obj) {
  203. zend_ulong k = (zend_ulong)upb_obj;
  204. int ret = zend_hash_index_del(&PROTOBUF_G(object_cache), k);
  205. PBPHP_ASSERT(ret == SUCCESS);
  206. }
  207. }
  208. bool ObjCache_Get(const void *upb_obj, zval *val) {
  209. zend_ulong k = (zend_ulong)upb_obj;
  210. zend_object *obj = zend_hash_index_find_ptr(&PROTOBUF_G(object_cache), k);
  211. if (obj) {
  212. GC_ADDREF(obj);
  213. ZVAL_OBJ(val, obj);
  214. return true;
  215. } else {
  216. ZVAL_NULL(val);
  217. return false;
  218. }
  219. }
  220. // -----------------------------------------------------------------------------
  221. // Name Cache.
  222. // -----------------------------------------------------------------------------
  223. void NameMap_AddMessage(const upb_msgdef *m) {
  224. char *k = GetPhpClassname(upb_msgdef_file(m), upb_msgdef_fullname(m));
  225. zend_hash_str_add_ptr(&PROTOBUF_G(name_msg_cache), k, strlen(k), (void*)m);
  226. free(k);
  227. }
  228. void NameMap_AddEnum(const upb_enumdef *e) {
  229. char *k = GetPhpClassname(upb_enumdef_file(e), upb_enumdef_fullname(e));
  230. zend_hash_str_add_ptr(&PROTOBUF_G(name_enum_cache), k, strlen(k), (void*)e);
  231. free(k);
  232. }
  233. const upb_msgdef *NameMap_GetMessage(zend_class_entry *ce) {
  234. const upb_msgdef *ret =
  235. zend_hash_find_ptr(&PROTOBUF_G(name_msg_cache), ce->name);
  236. if (!ret && ce->create_object) {
  237. zval tmp;
  238. zval zv;
  239. ZVAL_OBJ(&tmp, ce->create_object(ce));
  240. zend_call_method_with_0_params(&tmp, ce, NULL, "__construct", &zv);
  241. zval_ptr_dtor(&tmp);
  242. zval_ptr_dtor(&zv);
  243. ret = zend_hash_find_ptr(&PROTOBUF_G(name_msg_cache), ce->name);
  244. }
  245. return ret;
  246. }
  247. const upb_enumdef *NameMap_GetEnum(zend_class_entry *ce) {
  248. const upb_enumdef *ret =
  249. zend_hash_find_ptr(&PROTOBUF_G(name_enum_cache), ce->name);
  250. return ret;
  251. }
  252. // -----------------------------------------------------------------------------
  253. // Module init.
  254. // -----------------------------------------------------------------------------
  255. zend_function_entry protobuf_functions[] = {
  256. PHP_FE(protobuf_internal_loadbundled, arginfo_load_bundled_source)
  257. ZEND_FE_END
  258. };
  259. static const zend_module_dep protobuf_deps[] = {
  260. ZEND_MOD_OPTIONAL("date")
  261. ZEND_MOD_END
  262. };
  263. PHP_INI_BEGIN()
  264. STD_PHP_INI_ENTRY("protobuf.keep_descriptor_pool_after_request", "0",
  265. PHP_INI_SYSTEM, OnUpdateBool,
  266. keep_descriptor_pool_after_request, zend_protobuf_globals,
  267. protobuf_globals)
  268. PHP_INI_END()
  269. static PHP_MINIT_FUNCTION(protobuf) {
  270. REGISTER_INI_ENTRIES();
  271. Arena_ModuleInit();
  272. Array_ModuleInit();
  273. Convert_ModuleInit();
  274. Def_ModuleInit();
  275. Map_ModuleInit();
  276. Message_ModuleInit();
  277. return SUCCESS;
  278. }
  279. static PHP_MSHUTDOWN_FUNCTION(protobuf) {
  280. return SUCCESS;
  281. }
  282. zend_module_entry protobuf_module_entry = {
  283. STANDARD_MODULE_HEADER_EX,
  284. NULL,
  285. protobuf_deps,
  286. "protobuf", // extension name
  287. protobuf_functions, // function list
  288. PHP_MINIT(protobuf), // process startup
  289. PHP_MSHUTDOWN(protobuf), // process shutdown
  290. PHP_RINIT(protobuf), // request shutdown
  291. PHP_RSHUTDOWN(protobuf), // request shutdown
  292. NULL, // extension info
  293. "3.13.0", // extension version
  294. PHP_MODULE_GLOBALS(protobuf), // globals descriptor
  295. PHP_GINIT(protobuf), // globals ctor
  296. PHP_GSHUTDOWN(protobuf), // globals dtor
  297. NULL, // post deactivate
  298. STANDARD_MODULE_PROPERTIES_EX
  299. };
  300. ZEND_GET_MODULE(protobuf)