NCDInterpProcess.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. struct NCDInterpProcess__stmt {
  41. NCD_string_id_t name;
  42. NCD_string_id_t cmdname;
  43. NCD_string_id_t *objnames;
  44. size_t num_objnames;
  45. union {
  46. const struct NCDInterpModule *simple_module;
  47. int method_name_id;
  48. } binding;
  49. NCDValMem arg_mem;
  50. NCDValSafeRef arg_ref;
  51. NCDValReplaceProg arg_prog;
  52. int alloc_size;
  53. int prealloc_offset;
  54. int hash_next;
  55. };
  56. static int compute_prealloc (NCDInterpProcess *o)
  57. {
  58. int size = 0;
  59. for (int i = 0; i < o->num_stmts; i++) {
  60. int mod = size % BMAX_ALIGN;
  61. int align_size = (mod == 0 ? 0 : BMAX_ALIGN - mod);
  62. if (align_size + o->stmts[i].alloc_size > INT_MAX - size) {
  63. return 0;
  64. }
  65. o->stmts[i].prealloc_offset = size + align_size;
  66. size += align_size + o->stmts[i].alloc_size;
  67. }
  68. ASSERT(size >= 0)
  69. o->prealloc_size = size;
  70. return 1;
  71. }
  72. static int convert_value_recurser (NCDPlaceholderDb *pdb, NCDStringIndex *string_index, NCDValue *value, NCDValMem *mem, NCDValRef *out)
  73. {
  74. ASSERT(pdb)
  75. ASSERT(string_index)
  76. ASSERT((NCDValue_Type(value), 1))
  77. ASSERT(mem)
  78. ASSERT(out)
  79. switch (NCDValue_Type(value)) {
  80. case NCDVALUE_STRING: {
  81. const char *str = NCDValue_StringValue(value);
  82. size_t len = NCDValue_StringLength(value);
  83. NCD_string_id_t string_id = NCDStringIndex_GetBin(string_index, str, len);
  84. if (string_id < 0) {
  85. BLog(BLOG_ERROR, "NCDStringIndex_GetBin failed");
  86. goto fail;
  87. }
  88. *out = NCDVal_NewIdString(mem, string_id, string_index);
  89. if (NCDVal_IsInvalid(*out)) {
  90. goto fail;
  91. }
  92. } break;
  93. case NCDVALUE_LIST: {
  94. *out = NCDVal_NewList(mem, NCDValue_ListCount(value));
  95. if (NCDVal_IsInvalid(*out)) {
  96. goto fail;
  97. }
  98. for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
  99. NCDValRef vval;
  100. if (!convert_value_recurser(pdb, string_index, e, mem, &vval)) {
  101. goto fail;
  102. }
  103. if (!NCDVal_ListAppend(*out, vval)) {
  104. BLog(BLOG_ERROR, "depth limit exceeded");
  105. goto fail;
  106. }
  107. }
  108. } break;
  109. case NCDVALUE_MAP: {
  110. *out = NCDVal_NewMap(mem, NCDValue_MapCount(value));
  111. if (NCDVal_IsInvalid(*out)) {
  112. goto fail;
  113. }
  114. for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
  115. NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
  116. NCDValRef vkey;
  117. NCDValRef vval;
  118. if (!convert_value_recurser(pdb, string_index, ekey, mem, &vkey) ||
  119. !convert_value_recurser(pdb, string_index, eval, mem, &vval)
  120. ) {
  121. goto fail;
  122. }
  123. int inserted;
  124. if (!NCDVal_MapInsert(*out, vkey, vval, &inserted)) {
  125. BLog(BLOG_ERROR, "depth limit exceeded");
  126. goto fail;
  127. }
  128. if (!inserted) {
  129. BLog(BLOG_ERROR, "duplicate key in map");
  130. goto fail;
  131. }
  132. }
  133. } break;
  134. case NCDVALUE_VAR: {
  135. int plid;
  136. if (!NCDPlaceholderDb_AddVariable(pdb, NCDValue_VarName(value), &plid)) {
  137. goto fail;
  138. }
  139. if (NCDVAL_MINIDX + plid >= -1) {
  140. goto fail;
  141. }
  142. *out = NCDVal_NewPlaceholder(mem, plid);
  143. } break;
  144. default:
  145. BLog(BLOG_ERROR, "expression type not supported");
  146. goto fail;
  147. }
  148. return 1;
  149. fail:
  150. return 0;
  151. }
  152. int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index)
  153. {
  154. ASSERT(process)
  155. ASSERT(string_index)
  156. ASSERT(pdb)
  157. ASSERT(module_index)
  158. NCDBlock *block = NCDProcess_Block(process);
  159. if (NCDBlock_NumStatements(block) > INT_MAX) {
  160. BLog(BLOG_ERROR, "too many statements");
  161. goto fail0;
  162. }
  163. int num_stmts = NCDBlock_NumStatements(block);
  164. if (!(o->stmts = BAllocArray(num_stmts, sizeof(o->stmts[0])))) {
  165. BLog(BLOG_ERROR, "BAllocArray failed");
  166. goto fail0;
  167. }
  168. o->num_hash_buckets = num_stmts;
  169. if (!(o->hash_buckets = BAllocArray(o->num_hash_buckets, sizeof(o->hash_buckets[0])))) {
  170. BLog(BLOG_ERROR, "BAllocArray failed");
  171. goto fail1;
  172. }
  173. for (size_t i = 0; i < o->num_hash_buckets; i++) {
  174. o->hash_buckets[i] = -1;
  175. }
  176. if (!(o->name = b_strdup(NCDProcess_Name(process)))) {
  177. BLog(BLOG_ERROR, "b_strdup failed");
  178. goto fail2;
  179. }
  180. o->num_stmts = 0;
  181. o->prealloc_size = -1;
  182. o->is_template = NCDProcess_IsTemplate(process);
  183. o->cache = NULL;
  184. for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
  185. ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
  186. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  187. e->name = -1;
  188. e->objnames = NULL;
  189. e->num_objnames = 0;
  190. e->alloc_size = 0;
  191. if (NCDStatement_Name(s)) {
  192. e->name = NCDStringIndex_Get(string_index, NCDStatement_Name(s));
  193. if (e->name < 0) {
  194. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  195. goto loop_fail0;
  196. }
  197. }
  198. e->cmdname = NCDStringIndex_Get(string_index, NCDStatement_RegCmdName(s));
  199. if (e->cmdname < 0) {
  200. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  201. goto loop_fail0;
  202. }
  203. NCDValMem_Init(&e->arg_mem);
  204. NCDValRef val;
  205. if (!convert_value_recurser(pdb, string_index, NCDStatement_RegArgs(s), &e->arg_mem, &val)) {
  206. BLog(BLOG_ERROR, "convert_value_recurser failed");
  207. goto loop_fail1;
  208. }
  209. e->arg_ref = NCDVal_ToSafe(val);
  210. if (!NCDValReplaceProg_Init(&e->arg_prog, val)) {
  211. BLog(BLOG_ERROR, "NCDValReplaceProg_Init failed");
  212. goto loop_fail1;
  213. }
  214. if (NCDStatement_RegObjName(s)) {
  215. if (!ncd_make_name_indices(string_index, NCDStatement_RegObjName(s), &e->objnames, &e->num_objnames)) {
  216. BLog(BLOG_ERROR, "ncd_make_name_indices failed");
  217. goto loop_fail2;
  218. }
  219. e->binding.method_name_id = NCDModuleIndex_GetMethodNameId(module_index, NCDStatement_RegCmdName(s));
  220. if (e->binding.method_name_id == -1) {
  221. BLog(BLOG_ERROR, "NCDModuleIndex_GetMethodNameId failed");
  222. goto loop_fail3;
  223. }
  224. } else {
  225. e->binding.simple_module = NCDModuleIndex_FindModule(module_index, NCDStatement_RegCmdName(s));
  226. }
  227. if (e->name >= 0) {
  228. size_t bucket_idx = e->name % o->num_hash_buckets;
  229. e->hash_next = o->hash_buckets[bucket_idx];
  230. o->hash_buckets[bucket_idx] = o->num_stmts;
  231. }
  232. o->num_stmts++;
  233. continue;
  234. loop_fail3:
  235. BFree(e->objnames);
  236. loop_fail2:
  237. NCDValReplaceProg_Free(&e->arg_prog);
  238. loop_fail1:
  239. NCDValMem_Free(&e->arg_mem);
  240. loop_fail0:
  241. goto fail3;
  242. }
  243. ASSERT(o->num_stmts == num_stmts)
  244. DebugObject_Init(&o->d_obj);
  245. return 1;
  246. fail3:
  247. while (o->num_stmts-- > 0) {
  248. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  249. BFree(e->objnames);
  250. NCDValReplaceProg_Free(&e->arg_prog);
  251. NCDValMem_Free(&e->arg_mem);
  252. }
  253. free(o->name);
  254. fail2:
  255. BFree(o->hash_buckets);
  256. fail1:
  257. BFree(o->stmts);
  258. fail0:
  259. return 0;
  260. }
  261. void NCDInterpProcess_Free (NCDInterpProcess *o)
  262. {
  263. DebugObject_Free(&o->d_obj);
  264. while (o->num_stmts-- > 0) {
  265. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  266. BFree(e->objnames);
  267. NCDValReplaceProg_Free(&e->arg_prog);
  268. NCDValMem_Free(&e->arg_mem);
  269. }
  270. free(o->name);
  271. BFree(o->hash_buckets);
  272. BFree(o->stmts);
  273. }
  274. int NCDInterpProcess_FindStatement (NCDInterpProcess *o, int from_index, NCD_string_id_t name)
  275. {
  276. DebugObject_Access(&o->d_obj);
  277. ASSERT(from_index >= 0)
  278. ASSERT(from_index <= o->num_stmts)
  279. size_t bucket_idx = name % o->num_hash_buckets;
  280. int stmt_idx = o->hash_buckets[bucket_idx];
  281. ASSERT(stmt_idx >= -1)
  282. ASSERT(stmt_idx < o->num_stmts)
  283. while (stmt_idx >= 0) {
  284. if (stmt_idx < from_index && o->stmts[stmt_idx].name == name) {
  285. return stmt_idx;
  286. }
  287. stmt_idx = o->stmts[stmt_idx].hash_next;
  288. ASSERT(stmt_idx >= -1)
  289. ASSERT(stmt_idx < o->num_stmts)
  290. }
  291. return -1;
  292. }
  293. const char * NCDInterpProcess_StatementCmdName (NCDInterpProcess *o, int i, NCDStringIndex *string_index)
  294. {
  295. DebugObject_Access(&o->d_obj);
  296. ASSERT(i >= 0)
  297. ASSERT(i < o->num_stmts)
  298. ASSERT(string_index)
  299. return NCDStringIndex_Value(string_index, o->stmts[i].cmdname);
  300. }
  301. void NCDInterpProcess_StatementObjNames (NCDInterpProcess *o, int i, const NCD_string_id_t **out_objnames, size_t *out_num_objnames)
  302. {
  303. DebugObject_Access(&o->d_obj);
  304. ASSERT(i >= 0)
  305. ASSERT(i < o->num_stmts)
  306. ASSERT(out_objnames)
  307. ASSERT(out_num_objnames)
  308. *out_objnames = o->stmts[i].objnames;
  309. *out_num_objnames = o->stmts[i].num_objnames;
  310. }
  311. const struct NCDInterpModule * NCDInterpProcess_StatementGetSimpleModule (NCDInterpProcess *o, int i, NCDStringIndex *string_index, NCDModuleIndex *module_index)
  312. {
  313. DebugObject_Access(&o->d_obj);
  314. ASSERT(i >= 0)
  315. ASSERT(i < o->num_stmts)
  316. ASSERT(!o->stmts[i].objnames)
  317. struct NCDInterpProcess__stmt *e = &o->stmts[i];
  318. if (!e->binding.simple_module) {
  319. const char *cmdname = NCDStringIndex_Value(string_index, e->cmdname);
  320. e->binding.simple_module = NCDModuleIndex_FindModule(module_index, cmdname);
  321. }
  322. return e->binding.simple_module;
  323. }
  324. const struct NCDInterpModule * NCDInterpProcess_StatementGetMethodModule (NCDInterpProcess *o, int i, NCD_string_id_t obj_type, NCDModuleIndex *module_index)
  325. {
  326. DebugObject_Access(&o->d_obj);
  327. ASSERT(i >= 0)
  328. ASSERT(i < o->num_stmts)
  329. ASSERT(o->stmts[i].objnames)
  330. ASSERT(obj_type >= 0)
  331. ASSERT(module_index)
  332. return NCDModuleIndex_GetMethodModule(module_index, obj_type, o->stmts[i].binding.method_name_id);
  333. }
  334. int NCDInterpProcess_CopyStatementArgs (NCDInterpProcess *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog)
  335. {
  336. DebugObject_Access(&o->d_obj);
  337. ASSERT(i >= 0)
  338. ASSERT(i < o->num_stmts)
  339. ASSERT(out_valmem)
  340. ASSERT(out_val)
  341. ASSERT(out_prog)
  342. struct NCDInterpProcess__stmt *e = &o->stmts[i];
  343. if (!NCDValMem_InitCopy(out_valmem, &e->arg_mem)) {
  344. return 0;
  345. }
  346. *out_val = NCDVal_FromSafe(out_valmem, e->arg_ref);
  347. *out_prog = e->arg_prog;
  348. return 1;
  349. }
  350. void NCDInterpProcess_StatementBumpAllocSize (NCDInterpProcess *o, int i, int alloc_size)
  351. {
  352. DebugObject_Access(&o->d_obj);
  353. ASSERT(i >= 0)
  354. ASSERT(i < o->num_stmts)
  355. ASSERT(alloc_size >= 0)
  356. if (alloc_size > o->stmts[i].alloc_size) {
  357. o->stmts[i].alloc_size = alloc_size;
  358. o->prealloc_size = -1;
  359. }
  360. }
  361. int NCDInterpProcess_PreallocSize (NCDInterpProcess *o)
  362. {
  363. DebugObject_Access(&o->d_obj);
  364. ASSERT(o->prealloc_size == -1 || o->prealloc_size >= 0)
  365. if (o->prealloc_size < 0 && !compute_prealloc(o)) {
  366. return -1;
  367. }
  368. return o->prealloc_size;
  369. }
  370. int NCDInterpProcess_StatementPreallocSize (NCDInterpProcess *o, int i)
  371. {
  372. DebugObject_Access(&o->d_obj);
  373. ASSERT(i >= 0)
  374. ASSERT(i < o->num_stmts)
  375. ASSERT(o->prealloc_size >= 0)
  376. return o->stmts[i].alloc_size;
  377. }
  378. int NCDInterpProcess_StatementPreallocOffset (NCDInterpProcess *o, int i)
  379. {
  380. DebugObject_Access(&o->d_obj);
  381. ASSERT(i >= 0)
  382. ASSERT(i < o->num_stmts)
  383. ASSERT(o->prealloc_size >= 0)
  384. return o->stmts[i].prealloc_offset;
  385. }
  386. const char * NCDInterpProcess_Name (NCDInterpProcess *o)
  387. {
  388. DebugObject_Access(&o->d_obj);
  389. return o->name;
  390. }
  391. int NCDInterpProcess_IsTemplate (NCDInterpProcess *o)
  392. {
  393. DebugObject_Access(&o->d_obj);
  394. return o->is_template;
  395. }
  396. int NCDInterpProcess_NumStatements (NCDInterpProcess *o)
  397. {
  398. DebugObject_Access(&o->d_obj);
  399. return o->num_stmts;
  400. }
  401. int NCDInterpProcess_CachePush (NCDInterpProcess *o, void *elem)
  402. {
  403. DebugObject_Access(&o->d_obj);
  404. ASSERT(elem)
  405. if (o->cache) {
  406. return 0;
  407. }
  408. o->cache = elem;
  409. return 1;
  410. }
  411. void * NCDInterpProcess_CachePull (NCDInterpProcess *o)
  412. {
  413. DebugObject_Access(&o->d_obj);
  414. void *elem = o->cache;
  415. o->cache = NULL;
  416. return elem;
  417. }