NCDInterpBlock.c 11 KB

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