NCDInterpBlock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * @file NCDInterpBlock.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/split_string.h>
  35. #include <misc/hashfun.h>
  36. #include <misc/maxalign.h>
  37. #include <misc/strdup.h>
  38. #include <base/BLog.h>
  39. #include "NCDInterpBlock.h"
  40. #include <generated/blog_channel_ncd.h>
  41. static int compute_prealloc (NCDInterpBlock *o)
  42. {
  43. int size = 0;
  44. for (int i = 0; i < o->num_stmts; i++) {
  45. int mod = size % BMAX_ALIGN;
  46. int align_size = (mod == 0 ? 0 : BMAX_ALIGN - mod);
  47. if (align_size + o->stmts[i].alloc_size > INT_MAX - size) {
  48. return 0;
  49. }
  50. o->stmts[i].prealloc_offset = size + align_size;
  51. size += align_size + o->stmts[i].alloc_size;
  52. }
  53. ASSERT(size >= 0)
  54. o->prealloc_size = size;
  55. return 1;
  56. }
  57. int convert_value_recurser (NCDPlaceholderDb *pdb, NCDValue *value, NCDValMem *mem, NCDValRef *out)
  58. {
  59. ASSERT(pdb)
  60. ASSERT((NCDValue_Type(value), 1))
  61. ASSERT(mem)
  62. ASSERT(out)
  63. switch (NCDValue_Type(value)) {
  64. case NCDVALUE_STRING: {
  65. *out = NCDVal_NewStringBin(mem, (const uint8_t *)NCDValue_StringValue(value), NCDValue_StringLength(value));
  66. if (NCDVal_IsInvalid(*out)) {
  67. goto fail;
  68. }
  69. } break;
  70. case NCDVALUE_LIST: {
  71. *out = NCDVal_NewList(mem, NCDValue_ListCount(value));
  72. if (NCDVal_IsInvalid(*out)) {
  73. goto fail;
  74. }
  75. for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
  76. NCDValRef vval;
  77. if (!convert_value_recurser(pdb, e, mem, &vval)) {
  78. goto fail;
  79. }
  80. NCDVal_ListAppend(*out, vval);
  81. }
  82. } break;
  83. case NCDVALUE_MAP: {
  84. *out = NCDVal_NewMap(mem, NCDValue_MapCount(value));
  85. if (NCDVal_IsInvalid(*out)) {
  86. goto fail;
  87. }
  88. for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
  89. NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
  90. NCDValRef vkey;
  91. NCDValRef vval;
  92. if (!convert_value_recurser(pdb, ekey, mem, &vkey) ||
  93. !convert_value_recurser(pdb, eval, mem, &vval)
  94. ) {
  95. goto fail;
  96. }
  97. int res = NCDVal_MapInsert(*out, vkey, vval);
  98. ASSERT(res) // we assume different variables get different placeholder ids
  99. }
  100. } break;
  101. case NCDVALUE_VAR: {
  102. int plid;
  103. if (!NCDPlaceholderDb_AddVariable(pdb, NCDValue_VarName(value), &plid)) {
  104. goto fail;
  105. }
  106. if (NCDVAL_MINIDX + plid >= -1) {
  107. goto fail;
  108. }
  109. *out = NCDVal_NewPlaceholder(mem, plid);
  110. } break;
  111. default:
  112. goto fail;
  113. }
  114. return 1;
  115. fail:
  116. return 0;
  117. }
  118. int NCDInterpBlock_Init (NCDInterpBlock *o, NCDBlock *block, NCDProcess *process, NCDPlaceholderDb *pdb)
  119. {
  120. ASSERT(block)
  121. ASSERT(process)
  122. ASSERT(pdb)
  123. if (NCDBlock_NumStatements(block) > INT_MAX) {
  124. BLog(BLOG_ERROR, "too many statements");
  125. goto fail0;
  126. }
  127. int num_stmts = NCDBlock_NumStatements(block);
  128. if (!(o->stmts = BAllocArray(num_stmts, sizeof(o->stmts[0])))) {
  129. BLog(BLOG_ERROR, "BAllocArray failed");
  130. goto fail0;
  131. }
  132. if (!BStringTrie_Init(&o->trie)) {
  133. BLog(BLOG_ERROR, "BStringTrie_Init failed");
  134. goto fail1;
  135. }
  136. o->num_stmts = 0;
  137. o->prealloc_size = -1;
  138. o->process = process;
  139. for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
  140. ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
  141. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  142. e->name = NCDStatement_Name(s);
  143. e->cmdname = NCDStatement_RegCmdName(s);
  144. e->objnames = NULL;
  145. e->num_objnames = 0;
  146. e->alloc_size = 0;
  147. NCDValMem mem;
  148. NCDValMem_Init(&mem);
  149. NCDValRef val;
  150. if (!convert_value_recurser(pdb, NCDStatement_RegArgs(s), &mem, &val)) {
  151. BLog(BLOG_ERROR, "convert_value_recurser failed");
  152. NCDValMem_Free(&mem);
  153. goto loop_fail0;
  154. }
  155. e->arg_ref = NCDVal_ToSafe(val);
  156. if (!NCDValReplaceProg_Init(&e->arg_prog, val)) {
  157. BLog(BLOG_ERROR, "NCDValReplaceProg_Init failed");
  158. NCDValMem_Free(&mem);
  159. goto loop_fail0;
  160. }
  161. if (!NCDValMem_FreeExport(&mem, &e->arg_data, &e->arg_len)) {
  162. BLog(BLOG_ERROR, "NCDValMem_FreeExport failed");
  163. NCDValMem_Free(&mem);
  164. goto loop_fail1;
  165. }
  166. if (NCDStatement_RegObjName(s)) {
  167. if (!(e->objnames = b_strdup(NCDStatement_RegObjName(s)))) {
  168. BLog(BLOG_ERROR, "b_strdup failed");
  169. goto loop_fail2;
  170. }
  171. e->num_objnames = split_string_inplace2(e->objnames, '.') + 1;
  172. }
  173. if (e->name) {
  174. int existing_idx = BStringTrie_Lookup(&o->trie, e->name);
  175. ASSERT(existing_idx >= -1)
  176. ASSERT(existing_idx < o->num_stmts)
  177. ASSERT(existing_idx == -1 || !strcmp(o->stmts[existing_idx].name, e->name))
  178. e->next_equal = existing_idx;
  179. if (!BStringTrie_Set(&o->trie, e->name, o->num_stmts)) {
  180. BLog(BLOG_ERROR, "BStringTrie_Set failed");
  181. goto loop_fail3;
  182. }
  183. }
  184. o->num_stmts++;
  185. continue;
  186. loop_fail3:
  187. free(e->objnames);
  188. loop_fail2:
  189. BFree(e->arg_data);
  190. loop_fail1:
  191. NCDValReplaceProg_Free(&e->arg_prog);
  192. loop_fail0:
  193. goto fail2;
  194. }
  195. ASSERT(o->num_stmts == num_stmts)
  196. DebugObject_Init(&o->d_obj);
  197. return 1;
  198. fail2:
  199. while (o->num_stmts-- > 0) {
  200. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  201. free(e->objnames);
  202. BFree(e->arg_data);
  203. NCDValReplaceProg_Free(&e->arg_prog);
  204. }
  205. BStringTrie_Free(&o->trie);
  206. fail1:
  207. BFree(o->stmts);
  208. fail0:
  209. return 0;
  210. }
  211. void NCDInterpBlock_Free (NCDInterpBlock *o)
  212. {
  213. DebugObject_Free(&o->d_obj);
  214. while (o->num_stmts-- > 0) {
  215. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  216. free(e->objnames);
  217. BFree(e->arg_data);
  218. NCDValReplaceProg_Free(&e->arg_prog);
  219. }
  220. BStringTrie_Free(&o->trie);
  221. BFree(o->stmts);
  222. }
  223. int NCDInterpBlock_FindStatement (NCDInterpBlock *o, int from_index, const char *name)
  224. {
  225. DebugObject_Access(&o->d_obj);
  226. ASSERT(from_index >= 0)
  227. ASSERT(from_index <= o->num_stmts)
  228. ASSERT(name)
  229. int stmt_idx = BStringTrie_Lookup(&o->trie, name);
  230. ASSERT(stmt_idx >= -1)
  231. ASSERT(stmt_idx < o->num_stmts)
  232. while (stmt_idx >= 0) {
  233. ASSERT(!strcmp(o->stmts[stmt_idx].name, name))
  234. if (stmt_idx < from_index) {
  235. return stmt_idx;
  236. }
  237. stmt_idx = o->stmts[stmt_idx].next_equal;
  238. ASSERT(stmt_idx >= -1)
  239. ASSERT(stmt_idx < o->num_stmts)
  240. }
  241. return -1;
  242. }
  243. const char * NCDInterpBlock_StatementCmdName (NCDInterpBlock *o, int i)
  244. {
  245. DebugObject_Access(&o->d_obj);
  246. ASSERT(i >= 0)
  247. ASSERT(i < o->num_stmts)
  248. return o->stmts[i].cmdname;
  249. }
  250. void NCDInterpBlock_StatementObjNames (NCDInterpBlock *o, int i, const char **out_objnames, size_t *out_num_objnames)
  251. {
  252. DebugObject_Access(&o->d_obj);
  253. ASSERT(i >= 0)
  254. ASSERT(i < o->num_stmts)
  255. ASSERT(out_objnames)
  256. ASSERT(out_num_objnames)
  257. *out_objnames = o->stmts[i].objnames;
  258. *out_num_objnames = o->stmts[i].num_objnames;
  259. }
  260. int NCDInterpBlock_CopyStatementArgs (NCDInterpBlock *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog)
  261. {
  262. DebugObject_Access(&o->d_obj);
  263. ASSERT(i >= 0)
  264. ASSERT(i < o->num_stmts)
  265. ASSERT(out_valmem)
  266. ASSERT(out_val)
  267. ASSERT(out_prog)
  268. struct NCDInterpBlock__stmt *e = &o->stmts[i];
  269. if (!NCDValMem_InitImport(out_valmem, e->arg_data, e->arg_len)) {
  270. return 0;
  271. }
  272. *out_val = NCDVal_FromSafe(out_valmem, e->arg_ref);
  273. *out_prog = e->arg_prog;
  274. return 1;
  275. }
  276. void NCDInterpBlock_StatementBumpAllocSize (NCDInterpBlock *o, int i, int alloc_size)
  277. {
  278. DebugObject_Access(&o->d_obj);
  279. ASSERT(i >= 0)
  280. ASSERT(i < o->num_stmts)
  281. ASSERT(alloc_size >= 0)
  282. if (alloc_size > o->stmts[i].alloc_size) {
  283. o->stmts[i].alloc_size = alloc_size;
  284. o->prealloc_size = -1;
  285. }
  286. }
  287. int NCDInterpBlock_StatementPreallocSize (NCDInterpBlock *o, int i)
  288. {
  289. DebugObject_Access(&o->d_obj);
  290. ASSERT(i >= 0)
  291. ASSERT(i < o->num_stmts)
  292. return o->stmts[i].alloc_size;
  293. }
  294. int NCDInterpBlock_PreallocSize (NCDInterpBlock *o)
  295. {
  296. DebugObject_Access(&o->d_obj);
  297. ASSERT(o->prealloc_size == -1 || o->prealloc_size >= 0)
  298. if (o->prealloc_size < 0 && !compute_prealloc(o)) {
  299. return -1;
  300. }
  301. return o->prealloc_size;
  302. }
  303. int NCDInterpBlock_StatementPreallocOffset (NCDInterpBlock *o, int i)
  304. {
  305. DebugObject_Access(&o->d_obj);
  306. ASSERT(i >= 0)
  307. ASSERT(i < o->num_stmts)
  308. ASSERT(o->prealloc_size >= 0)
  309. return o->stmts[i].prealloc_offset;
  310. }
  311. NCDProcess * NCDInterpBlock_Process (NCDInterpBlock *o)
  312. {
  313. DebugObject_Access(&o->d_obj);
  314. return o->process;
  315. }