protobuf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "convert.h"
  36. #include "def.h"
  37. #include "map.h"
  38. #include "message.h"
  39. #include "names.h"
  40. // -----------------------------------------------------------------------------
  41. // Module "globals"
  42. // -----------------------------------------------------------------------------
  43. // Despite the name, module "globals" are really thread-locals:
  44. // * PROTOBUF_G(var) accesses the thread-local variable for 'var'. Either:
  45. // * PROTOBUF_G(var) -> protobuf_globals.var (Non-ZTS / non-thread-safe)
  46. // * PROTOBUF_G(var) -> <Zend magic> (ZTS / thread-safe builds)
  47. #define PROTOBUF_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(protobuf, v)
  48. ZEND_BEGIN_MODULE_GLOBALS(protobuf)
  49. // Set by the user to make the descriptor pool persist between requests.
  50. zend_bool keep_descriptor_pool_after_request;
  51. // Currently we make the generated pool a "global", which means that if a user
  52. // does explicitly create threads within their request, the other threads will
  53. // get different results from DescriptorPool::getGeneratedPool(). We require
  54. // that all descriptors are loaded from the main thread.
  55. zval generated_pool;
  56. // A upb_symtab that we are saving for the next request so that we don't have
  57. // to rebuild it from scratch. When keep_descriptor_pool_after_request==true,
  58. // we steal the upb_symtab from the global DescriptorPool object just before
  59. // destroying it.
  60. upb_symtab *saved_symtab;
  61. // Object cache (see interface in protobuf.h).
  62. HashTable object_cache;
  63. // Name cache (see interface in protobuf.h).
  64. HashTable name_msg_cache;
  65. HashTable name_enum_cache;
  66. ZEND_END_MODULE_GLOBALS(protobuf)
  67. ZEND_DECLARE_MODULE_GLOBALS(protobuf)
  68. const zval *get_generated_pool() {
  69. return &PROTOBUF_G(generated_pool);
  70. }
  71. // This is a PHP extension (not a Zend extension). What follows is a summary of
  72. // a PHP extension's lifetime and when various handlers are called.
  73. //
  74. // * PHP_GINIT_FUNCTION(protobuf) / PHP_GSHUTDOWN_FUNCTION(protobuf)
  75. // are the constructor/destructor for the globals. The sequence over the
  76. // course of a process lifetime is:
  77. //
  78. // # Process startup
  79. // GINIT(<Main Thread Globals>)
  80. // MINIT
  81. //
  82. // foreach request:
  83. // RINIT
  84. // # Request is processed here.
  85. // RSHUTDOWN
  86. //
  87. // foreach thread:
  88. // GINIT(<This Thread Globals>)
  89. // # Code for the thread runs here.
  90. // GSHUTDOWN(<This Thread Globals>)
  91. //
  92. // # Process Shutdown
  93. // #
  94. // # These should be running per the docs, but I have not been able to
  95. // # actually get the process-wide shutdown functions to run.
  96. // #
  97. // # MSHUTDOWN
  98. // # GSHUTDOWN(<Main Thread Globals>)
  99. //
  100. // * Threads can be created either explicitly by the user, inside a request,
  101. // or implicitly by the runtime, to process multiple requests concurrently.
  102. // If the latter is being used, then the "foreach thread" block above
  103. // actually looks like this:
  104. //
  105. // foreach thread:
  106. // GINIT(<This Thread Globals>)
  107. // # A non-main thread will only receive requests when using a threaded
  108. // # MPM with Apache
  109. // foreach request:
  110. // RINIT
  111. // # Request is processed here.
  112. // RSHUTDOWN
  113. // GSHUTDOWN(<This Thread Globals>)
  114. //
  115. // That said, it appears that few people use threads with PHP:
  116. // * The pthread package documented at
  117. // https://www.php.net/manual/en/class.thread.php nas not been released
  118. // since 2016, and the current release fails to compile against any PHP
  119. // newer than 7.0.33.
  120. // * The GitHub master branch supports 7.2+, but this has not been released
  121. // to PECL.
  122. // * Its owner has disavowed it as "broken by design" and "in an untenable
  123. // position for the future": https://github.com/krakjoe/pthreads/issues/929
  124. // * The only way to use PHP with requests in different threads is to use the
  125. // Apache 2 mod_php with the "worker" MPM. But this is explicitly
  126. // discouraged by the documentation: https://serverfault.com/a/231660
  127. static PHP_GSHUTDOWN_FUNCTION(protobuf) {
  128. if (protobuf_globals->saved_symtab) {
  129. upb_symtab_free(protobuf_globals->saved_symtab);
  130. }
  131. }
  132. static PHP_GINIT_FUNCTION(protobuf) {
  133. ZVAL_NULL(&protobuf_globals->generated_pool);
  134. protobuf_globals->saved_symtab = NULL;
  135. }
  136. /**
  137. * PHP_RINIT_FUNCTION(protobuf)
  138. *
  139. * This function is run at the beginning of processing each request.
  140. */
  141. static PHP_RINIT_FUNCTION(protobuf) {
  142. // Create the global generated pool.
  143. // Reuse the symtab (if any) left to us by the last request.
  144. upb_symtab *symtab = PROTOBUF_G(saved_symtab);
  145. DescriptorPool_CreateWithSymbolTable(&PROTOBUF_G(generated_pool), symtab);
  146. zend_hash_init(&PROTOBUF_G(object_cache), 64, NULL, NULL, 0);
  147. zend_hash_init(&PROTOBUF_G(name_msg_cache), 64, NULL, NULL, 0);
  148. zend_hash_init(&PROTOBUF_G(name_enum_cache), 64, NULL, NULL, 0);
  149. return SUCCESS;
  150. }
  151. /**
  152. * PHP_RSHUTDOWN_FUNCTION(protobuf)
  153. *
  154. * This function is run at the end of processing each request.
  155. */
  156. static PHP_RSHUTDOWN_FUNCTION(protobuf) {
  157. // Preserve the symtab if requested.
  158. if (PROTOBUF_G(keep_descriptor_pool_after_request)) {
  159. zval *zv = &PROTOBUF_G(generated_pool);
  160. PROTOBUF_G(saved_symtab) = DescriptorPool_Steal(zv);
  161. }
  162. zval_dtor(&PROTOBUF_G(generated_pool));
  163. zend_hash_destroy(&PROTOBUF_G(object_cache));
  164. zend_hash_destroy(&PROTOBUF_G(name_msg_cache));
  165. zend_hash_destroy(&PROTOBUF_G(name_enum_cache));
  166. return SUCCESS;
  167. }
  168. // -----------------------------------------------------------------------------
  169. // Object Cache.
  170. // -----------------------------------------------------------------------------
  171. void ObjCache_Add(const void *upb_obj, zend_object *php_obj) {
  172. zend_ulong k = (zend_ulong)upb_obj;
  173. zend_hash_index_add_ptr(&PROTOBUF_G(object_cache), k, php_obj);
  174. }
  175. void ObjCache_Delete(const void *upb_obj) {
  176. if (upb_obj) {
  177. zend_ulong k = (zend_ulong)upb_obj;
  178. int ret = zend_hash_index_del(&PROTOBUF_G(object_cache), k);
  179. PBPHP_ASSERT(ret == SUCCESS);
  180. }
  181. }
  182. bool ObjCache_Get(const void *upb_obj, zval *val) {
  183. zend_ulong k = (zend_ulong)upb_obj;
  184. zend_object *obj = zend_hash_index_find_ptr(&PROTOBUF_G(object_cache), k);
  185. if (obj) {
  186. GC_ADDREF(obj);
  187. ZVAL_OBJ(val, obj);
  188. return true;
  189. } else {
  190. ZVAL_NULL(val);
  191. return false;
  192. }
  193. }
  194. // -----------------------------------------------------------------------------
  195. // Name Cache.
  196. // -----------------------------------------------------------------------------
  197. void NameMap_AddMessage(const upb_msgdef *m) {
  198. char *k = GetPhpClassname(upb_msgdef_file(m), upb_msgdef_fullname(m));
  199. zend_hash_str_add_ptr(&PROTOBUF_G(name_msg_cache), k, strlen(k), (void*)m);
  200. free(k);
  201. }
  202. void NameMap_AddEnum(const upb_enumdef *e) {
  203. char *k = GetPhpClassname(upb_enumdef_file(e), upb_enumdef_fullname(e));
  204. zend_hash_str_add_ptr(&PROTOBUF_G(name_enum_cache), k, strlen(k), (void*)e);
  205. free(k);
  206. }
  207. const upb_msgdef *NameMap_GetMessage(zend_class_entry *ce) {
  208. const upb_msgdef *ret =
  209. zend_hash_find_ptr(&PROTOBUF_G(name_msg_cache), ce->name);
  210. if (!ret && ce->create_object) {
  211. #if PHP_VERSION_ID < 80000
  212. zval tmp;
  213. zval zv;
  214. ZVAL_OBJ(&tmp, ce->create_object(ce));
  215. zend_call_method_with_0_params(&tmp, ce, NULL, "__construct", &zv);
  216. zval_ptr_dtor(&tmp);
  217. #else
  218. zval zv;
  219. zend_object *tmp = ce->create_object(ce);
  220. zend_call_method_with_0_params(tmp, ce, NULL, "__construct", &zv);
  221. OBJ_RELEASE(tmp);
  222. #endif
  223. zval_ptr_dtor(&zv);
  224. ret = zend_hash_find_ptr(&PROTOBUF_G(name_msg_cache), ce->name);
  225. }
  226. return ret;
  227. }
  228. const upb_enumdef *NameMap_GetEnum(zend_class_entry *ce) {
  229. const upb_enumdef *ret =
  230. zend_hash_find_ptr(&PROTOBUF_G(name_enum_cache), ce->name);
  231. return ret;
  232. }
  233. // -----------------------------------------------------------------------------
  234. // Module init.
  235. // -----------------------------------------------------------------------------
  236. zend_function_entry protobuf_functions[] = {
  237. ZEND_FE_END
  238. };
  239. static const zend_module_dep protobuf_deps[] = {
  240. ZEND_MOD_OPTIONAL("date")
  241. ZEND_MOD_END
  242. };
  243. PHP_INI_BEGIN()
  244. STD_PHP_INI_ENTRY("protobuf.keep_descriptor_pool_after_request", "0",
  245. PHP_INI_SYSTEM, OnUpdateBool,
  246. keep_descriptor_pool_after_request, zend_protobuf_globals,
  247. protobuf_globals)
  248. PHP_INI_END()
  249. static PHP_MINIT_FUNCTION(protobuf) {
  250. REGISTER_INI_ENTRIES();
  251. Arena_ModuleInit();
  252. Array_ModuleInit();
  253. Convert_ModuleInit();
  254. Def_ModuleInit();
  255. Map_ModuleInit();
  256. Message_ModuleInit();
  257. return SUCCESS;
  258. }
  259. static PHP_MSHUTDOWN_FUNCTION(protobuf) {
  260. return SUCCESS;
  261. }
  262. zend_module_entry protobuf_module_entry = {
  263. STANDARD_MODULE_HEADER_EX,
  264. NULL,
  265. protobuf_deps,
  266. "protobuf", // extension name
  267. protobuf_functions, // function list
  268. PHP_MINIT(protobuf), // process startup
  269. PHP_MSHUTDOWN(protobuf), // process shutdown
  270. PHP_RINIT(protobuf), // request shutdown
  271. PHP_RSHUTDOWN(protobuf), // request shutdown
  272. NULL, // extension info
  273. "3.13.0", // extension version
  274. PHP_MODULE_GLOBALS(protobuf), // globals descriptor
  275. PHP_GINIT(protobuf), // globals ctor
  276. PHP_GSHUTDOWN(protobuf), // globals dtor
  277. NULL, // post deactivate
  278. STANDARD_MODULE_PROPERTIES_EX
  279. };
  280. ZEND_GET_MODULE(protobuf)