NCDInterpProcess.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /**
  2. * @file NCDInterpProcess.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdint.h>
  30. #include <limits.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <misc/balloc.h>
  34. #include <misc/maxalign.h>
  35. #include <misc/strdup.h>
  36. #include <base/BLog.h>
  37. #include <ncd/make_name_indices.h>
  38. #include "NCDInterpProcess.h"
  39. #include <generated/blog_channel_ncd.h>
  40. static int compute_prealloc (NCDInterpProcess *o)
  41. {
  42. int size = 0;
  43. for (int i = 0; i < o->num_stmts; i++) {
  44. int mod = size % BMAX_ALIGN;
  45. int align_size = (mod == 0 ? 0 : BMAX_ALIGN - mod);
  46. if (align_size + o->stmts[i].alloc_size > INT_MAX - size) {
  47. return 0;
  48. }
  49. o->stmts[i].prealloc_offset = size + align_size;
  50. size += align_size + o->stmts[i].alloc_size;
  51. }
  52. ASSERT(size >= 0)
  53. o->prealloc_size = size;
  54. return 1;
  55. }
  56. static int convert_value_recurser (NCDPlaceholderDb *pdb, NCDStringIndex *string_index, NCDValue *value, NCDValMem *mem, NCDValRef *out)
  57. {
  58. ASSERT(pdb)
  59. ASSERT(string_index)
  60. ASSERT((NCDValue_Type(value), 1))
  61. ASSERT(mem)
  62. ASSERT(out)
  63. switch (NCDValue_Type(value)) {
  64. case NCDVALUE_STRING: {
  65. const char *str = NCDValue_StringValue(value);
  66. size_t len = NCDValue_StringLength(value);
  67. NCD_string_id_t string_id = NCDStringIndex_GetBin(string_index, str, len);
  68. if (string_id < 0) {
  69. BLog(BLOG_ERROR, "NCDStringIndex_GetBin failed");
  70. goto fail;
  71. }
  72. *out = NCDVal_NewIdString(mem, string_id, string_index);
  73. if (NCDVal_IsInvalid(*out)) {
  74. goto fail;
  75. }
  76. } break;
  77. case NCDVALUE_LIST: {
  78. *out = NCDVal_NewList(mem, NCDValue_ListCount(value));
  79. if (NCDVal_IsInvalid(*out)) {
  80. goto fail;
  81. }
  82. for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
  83. NCDValRef vval;
  84. if (!convert_value_recurser(pdb, string_index, e, mem, &vval)) {
  85. goto fail;
  86. }
  87. if (!NCDVal_ListAppend(*out, vval)) {
  88. BLog(BLOG_ERROR, "depth limit exceeded");
  89. goto fail;
  90. }
  91. }
  92. } break;
  93. case NCDVALUE_MAP: {
  94. *out = NCDVal_NewMap(mem, NCDValue_MapCount(value));
  95. if (NCDVal_IsInvalid(*out)) {
  96. goto fail;
  97. }
  98. for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
  99. NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
  100. NCDValRef vkey;
  101. NCDValRef vval;
  102. if (!convert_value_recurser(pdb, string_index, ekey, mem, &vkey) ||
  103. !convert_value_recurser(pdb, string_index, eval, mem, &vval)
  104. ) {
  105. goto fail;
  106. }
  107. int inserted;
  108. if (!NCDVal_MapInsert(*out, vkey, vval, &inserted)) {
  109. BLog(BLOG_ERROR, "depth limit exceeded");
  110. goto fail;
  111. }
  112. if (!inserted) {
  113. BLog(BLOG_ERROR, "duplicate key in map");
  114. goto fail;
  115. }
  116. }
  117. } break;
  118. case NCDVALUE_VAR: {
  119. int plid;
  120. if (!NCDPlaceholderDb_AddVariable(pdb, NCDValue_VarName(value), &plid)) {
  121. goto fail;
  122. }
  123. if (NCDVAL_MINIDX + plid >= -1) {
  124. goto fail;
  125. }
  126. *out = NCDVal_NewPlaceholder(mem, plid);
  127. } break;
  128. default:
  129. BLog(BLOG_ERROR, "expression type not supported");
  130. goto fail;
  131. }
  132. return 1;
  133. fail:
  134. return 0;
  135. }
  136. int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index)
  137. {
  138. ASSERT(process)
  139. ASSERT(string_index)
  140. ASSERT(pdb)
  141. ASSERT(module_index)
  142. NCDBlock *block = NCDProcess_Block(process);
  143. if (NCDBlock_NumStatements(block) > INT_MAX) {
  144. BLog(BLOG_ERROR, "too many statements");
  145. goto fail0;
  146. }
  147. int num_stmts = NCDBlock_NumStatements(block);
  148. if (!(o->stmts = BAllocArray(num_stmts, sizeof(o->stmts[0])))) {
  149. BLog(BLOG_ERROR, "BAllocArray failed");
  150. goto fail0;
  151. }
  152. o->num_hash_buckets = num_stmts;
  153. if (!(o->hash_buckets = BAllocArray(o->num_hash_buckets, sizeof(o->hash_buckets[0])))) {
  154. BLog(BLOG_ERROR, "BAllocArray failed");
  155. goto fail1;
  156. }
  157. for (size_t i = 0; i < o->num_hash_buckets; i++) {
  158. o->hash_buckets[i] = -1;
  159. }
  160. if (!(o->name = b_strdup(NCDProcess_Name(process)))) {
  161. BLog(BLOG_ERROR, "b_strdup failed");
  162. goto fail2;
  163. }
  164. o->num_stmts = 0;
  165. o->prealloc_size = -1;
  166. o->is_template = NCDProcess_IsTemplate(process);
  167. o->cache = NULL;
  168. for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
  169. ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
  170. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  171. e->name = -1;
  172. e->objnames = NULL;
  173. e->num_objnames = 0;
  174. e->alloc_size = 0;
  175. if (NCDStatement_Name(s)) {
  176. e->name = NCDStringIndex_Get(string_index, NCDStatement_Name(s));
  177. if (e->name < 0) {
  178. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  179. goto loop_fail0;
  180. }
  181. }
  182. e->cmdname = NCDStringIndex_Get(string_index, NCDStatement_RegCmdName(s));
  183. if (e->cmdname < 0) {
  184. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  185. goto loop_fail0;
  186. }
  187. NCDValMem_Init(&e->arg_mem);
  188. NCDValRef val;
  189. if (!convert_value_recurser(pdb, string_index, NCDStatement_RegArgs(s), &e->arg_mem, &val)) {
  190. BLog(BLOG_ERROR, "convert_value_recurser failed");
  191. goto loop_fail1;
  192. }
  193. e->arg_ref = NCDVal_ToSafe(val);
  194. if (!NCDValReplaceProg_Init(&e->arg_prog, val)) {
  195. BLog(BLOG_ERROR, "NCDValReplaceProg_Init failed");
  196. goto loop_fail1;
  197. }
  198. if (NCDStatement_RegObjName(s)) {
  199. if (!ncd_make_name_indices(string_index, NCDStatement_RegObjName(s), &e->objnames, &e->num_objnames)) {
  200. BLog(BLOG_ERROR, "ncd_make_name_indices failed");
  201. goto loop_fail2;
  202. }
  203. e->binding.method_name_id = NCDModuleIndex_GetMethodNameId(module_index, NCDStatement_RegCmdName(s));
  204. if (e->binding.method_name_id == -1) {
  205. BLog(BLOG_ERROR, "NCDModuleIndex_GetMethodNameId failed");
  206. goto loop_fail3;
  207. }
  208. } else {
  209. e->binding.simple_module = NCDModuleIndex_FindModule(module_index, NCDStatement_RegCmdName(s));
  210. }
  211. if (e->name >= 0) {
  212. size_t bucket_idx = e->name % o->num_hash_buckets;
  213. e->hash_next = o->hash_buckets[bucket_idx];
  214. o->hash_buckets[bucket_idx] = o->num_stmts;
  215. }
  216. o->num_stmts++;
  217. continue;
  218. loop_fail3:
  219. BFree(e->objnames);
  220. loop_fail2:
  221. NCDValReplaceProg_Free(&e->arg_prog);
  222. loop_fail1:
  223. NCDValMem_Free(&e->arg_mem);
  224. loop_fail0:
  225. goto fail3;
  226. }
  227. ASSERT(o->num_stmts == num_stmts)
  228. DebugObject_Init(&o->d_obj);
  229. return 1;
  230. fail3:
  231. while (o->num_stmts-- > 0) {
  232. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  233. BFree(e->objnames);
  234. NCDValReplaceProg_Free(&e->arg_prog);
  235. NCDValMem_Free(&e->arg_mem);
  236. }
  237. free(o->name);
  238. fail2:
  239. BFree(o->hash_buckets);
  240. fail1:
  241. BFree(o->stmts);
  242. fail0:
  243. return 0;
  244. }
  245. void NCDInterpProcess_Free (NCDInterpProcess *o)
  246. {
  247. DebugObject_Free(&o->d_obj);
  248. while (o->num_stmts-- > 0) {
  249. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  250. BFree(e->objnames);
  251. NCDValReplaceProg_Free(&e->arg_prog);
  252. NCDValMem_Free(&e->arg_mem);
  253. }
  254. free(o->name);
  255. BFree(o->hash_buckets);
  256. BFree(o->stmts);
  257. }
  258. int NCDInterpProcess_FindStatement (NCDInterpProcess *o, int from_index, NCD_string_id_t name)
  259. {
  260. DebugObject_Access(&o->d_obj);
  261. ASSERT(from_index >= 0)
  262. ASSERT(from_index <= o->num_stmts)
  263. size_t bucket_idx = name % o->num_hash_buckets;
  264. int stmt_idx = o->hash_buckets[bucket_idx];
  265. ASSERT(stmt_idx >= -1)
  266. ASSERT(stmt_idx < o->num_stmts)
  267. while (stmt_idx >= 0) {
  268. if (stmt_idx < from_index && o->stmts[stmt_idx].name == name) {
  269. return stmt_idx;
  270. }
  271. stmt_idx = o->stmts[stmt_idx].hash_next;
  272. ASSERT(stmt_idx >= -1)
  273. ASSERT(stmt_idx < o->num_stmts)
  274. }
  275. return -1;
  276. }
  277. const char * NCDInterpProcess_StatementCmdName (NCDInterpProcess *o, int i, NCDStringIndex *string_index)
  278. {
  279. DebugObject_Access(&o->d_obj);
  280. ASSERT(i >= 0)
  281. ASSERT(i < o->num_stmts)
  282. ASSERT(string_index)
  283. return NCDStringIndex_Value(string_index, o->stmts[i].cmdname);
  284. }
  285. void NCDInterpProcess_StatementObjNames (NCDInterpProcess *o, int i, const NCD_string_id_t **out_objnames, size_t *out_num_objnames)
  286. {
  287. DebugObject_Access(&o->d_obj);
  288. ASSERT(i >= 0)
  289. ASSERT(i < o->num_stmts)
  290. ASSERT(out_objnames)
  291. ASSERT(out_num_objnames)
  292. *out_objnames = o->stmts[i].objnames;
  293. *out_num_objnames = o->stmts[i].num_objnames;
  294. }
  295. const struct NCDInterpModule * NCDInterpProcess_StatementGetSimpleModule (NCDInterpProcess *o, int i, NCDStringIndex *string_index, NCDModuleIndex *module_index)
  296. {
  297. DebugObject_Access(&o->d_obj);
  298. ASSERT(i >= 0)
  299. ASSERT(i < o->num_stmts)
  300. ASSERT(!o->stmts[i].objnames)
  301. struct NCDInterpProcess__stmt *e = &o->stmts[i];
  302. if (!e->binding.simple_module) {
  303. const char *cmdname = NCDStringIndex_Value(string_index, e->cmdname);
  304. e->binding.simple_module = NCDModuleIndex_FindModule(module_index, cmdname);
  305. }
  306. return e->binding.simple_module;
  307. }
  308. const struct NCDInterpModule * NCDInterpProcess_StatementGetMethodModule (NCDInterpProcess *o, int i, NCD_string_id_t obj_type, NCDModuleIndex *module_index)
  309. {
  310. DebugObject_Access(&o->d_obj);
  311. ASSERT(i >= 0)
  312. ASSERT(i < o->num_stmts)
  313. ASSERT(o->stmts[i].objnames)
  314. ASSERT(obj_type >= 0)
  315. ASSERT(module_index)
  316. return NCDModuleIndex_GetMethodModule(module_index, obj_type, o->stmts[i].binding.method_name_id);
  317. }
  318. int NCDInterpProcess_CopyStatementArgs (NCDInterpProcess *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog)
  319. {
  320. DebugObject_Access(&o->d_obj);
  321. ASSERT(i >= 0)
  322. ASSERT(i < o->num_stmts)
  323. ASSERT(out_valmem)
  324. ASSERT(out_val)
  325. ASSERT(out_prog)
  326. struct NCDInterpProcess__stmt *e = &o->stmts[i];
  327. if (!NCDValMem_InitCopy(out_valmem, &e->arg_mem)) {
  328. return 0;
  329. }
  330. *out_val = NCDVal_FromSafe(out_valmem, e->arg_ref);
  331. *out_prog = e->arg_prog;
  332. return 1;
  333. }
  334. void NCDInterpProcess_StatementBumpAllocSize (NCDInterpProcess *o, int i, int alloc_size)
  335. {
  336. DebugObject_Access(&o->d_obj);
  337. ASSERT(i >= 0)
  338. ASSERT(i < o->num_stmts)
  339. ASSERT(alloc_size >= 0)
  340. if (alloc_size > o->stmts[i].alloc_size) {
  341. o->stmts[i].alloc_size = alloc_size;
  342. o->prealloc_size = -1;
  343. }
  344. }
  345. int NCDInterpProcess_PreallocSize (NCDInterpProcess *o)
  346. {
  347. DebugObject_Access(&o->d_obj);
  348. ASSERT(o->prealloc_size == -1 || o->prealloc_size >= 0)
  349. if (o->prealloc_size < 0 && !compute_prealloc(o)) {
  350. return -1;
  351. }
  352. return o->prealloc_size;
  353. }
  354. int NCDInterpProcess_StatementPreallocSize (NCDInterpProcess *o, int i)
  355. {
  356. DebugObject_Access(&o->d_obj);
  357. ASSERT(i >= 0)
  358. ASSERT(i < o->num_stmts)
  359. ASSERT(o->prealloc_size >= 0)
  360. return o->stmts[i].alloc_size;
  361. }
  362. int NCDInterpProcess_StatementPreallocOffset (NCDInterpProcess *o, int i)
  363. {
  364. DebugObject_Access(&o->d_obj);
  365. ASSERT(i >= 0)
  366. ASSERT(i < o->num_stmts)
  367. ASSERT(o->prealloc_size >= 0)
  368. return o->stmts[i].prealloc_offset;
  369. }
  370. const char * NCDInterpProcess_Name (NCDInterpProcess *o)
  371. {
  372. DebugObject_Access(&o->d_obj);
  373. return o->name;
  374. }
  375. int NCDInterpProcess_IsTemplate (NCDInterpProcess *o)
  376. {
  377. DebugObject_Access(&o->d_obj);
  378. return o->is_template;
  379. }
  380. int NCDInterpProcess_NumStatements (NCDInterpProcess *o)
  381. {
  382. DebugObject_Access(&o->d_obj);
  383. return o->num_stmts;
  384. }
  385. int NCDInterpProcess_CachePush (NCDInterpProcess *o, void *elem)
  386. {
  387. DebugObject_Access(&o->d_obj);
  388. ASSERT(elem)
  389. if (o->cache) {
  390. return 0;
  391. }
  392. o->cache = elem;
  393. return 1;
  394. }
  395. void * NCDInterpProcess_CachePull (NCDInterpProcess *o)
  396. {
  397. DebugObject_Access(&o->d_obj);
  398. void *elem = o->cache;
  399. o->cache = NULL;
  400. return elem;
  401. }