NCDInterpreter.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /**
  2. * @file NCDInterpreter.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 <string.h>
  31. #include <stdlib.h>
  32. #include <limits.h>
  33. #include <stdarg.h>
  34. #include <misc/offset.h>
  35. #include <misc/balloc.h>
  36. #include <misc/expstring.h>
  37. #include <base/BLog.h>
  38. #include <ncd/NCDConfigParser.h>
  39. #include <ncd/NCDSugar.h>
  40. #include <ncd/modules/modules.h>
  41. #include "NCDInterpreter.h"
  42. #include <generated/blog_channel_ncd.h>
  43. #define SSTATE_CHILD 0
  44. #define SSTATE_ADULT 1
  45. #define SSTATE_DYING 2
  46. #define SSTATE_FORGOTTEN 3
  47. #define PSTATE_WORKING 0
  48. #define PSTATE_UP 1
  49. #define PSTATE_WAITING 2
  50. #define PSTATE_TERMINATING 3
  51. struct statement {
  52. NCDModuleInst inst;
  53. NCDValMem args_mem;
  54. int mem_size;
  55. int i;
  56. };
  57. struct process {
  58. NCDInterpreter *interp;
  59. BReactor *reactor;
  60. NCDInterpProcess *iprocess;
  61. NCDModuleProcess *module_process;
  62. BSmallTimer wait_timer;
  63. BSmallPending work_job;
  64. LinkedList1Node list_node; // node in processes
  65. int ap;
  66. int fp;
  67. int num_statements;
  68. unsigned int error:1;
  69. unsigned int have_alloc:1;
  70. #ifndef NDEBUG
  71. int state;
  72. #endif
  73. struct statement statements[];
  74. };
  75. static void start_terminate (NCDInterpreter *interp, int exit_code);
  76. static char * implode_id_strings (NCDInterpreter *interp, const NCD_string_id_t *names, size_t num_names, char del);
  77. static int alloc_base_type_strings (NCDInterpreter *interp, const struct NCDModuleGroup *g);
  78. static int process_new (NCDInterpreter *interp, NCDInterpProcess *iprocess, NCDModuleProcess *module_process);
  79. static void process_free (struct process *p, NCDModuleProcess **out_mp);
  80. static void process_set_state (struct process *p, int state);
  81. static void process_start_terminating (struct process *p);
  82. static int process_have_child (struct process *p);
  83. static void process_assert_pointers (struct process *p);
  84. static void process_logfunc (struct process *p);
  85. static void process_log (struct process *p, int level, const char *fmt, ...);
  86. static void process_work_job_handler_working (struct process *p);
  87. static void process_work_job_handler_up (struct process *p);
  88. static void process_work_job_handler_waiting (struct process *p);
  89. static void process_work_job_handler_terminating (struct process *p);
  90. static int replace_placeholders_callback (void *arg, int plid, NCDValMem *mem, NCDValRef *out);
  91. static void process_advance (struct process *p);
  92. static void process_wait_timer_handler (BSmallTimer *timer);
  93. static int process_find_object (struct process *p, int pos, NCD_string_id_t name, NCDObject *out_object);
  94. static int process_resolve_object_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDObject *out_object);
  95. static int process_resolve_variable_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value);
  96. static void statement_logfunc (struct statement *ps);
  97. static void statement_log (struct statement *ps, int level, const char *fmt, ...);
  98. static struct process * statement_process (struct statement *ps);
  99. static int statement_mem_is_allocated (struct statement *ps);
  100. static int statement_mem_size (struct statement *ps);
  101. static int statement_allocate_memory (struct statement *ps, int alloc_size);
  102. static void statement_instance_func_event (NCDModuleInst *inst, int event);
  103. static int statement_instance_func_getobj (NCDModuleInst *inst, NCD_string_id_t objname, NCDObject *out_object);
  104. static int statement_instance_func_initprocess (void *vinterp, NCDModuleProcess *mp, NCD_string_id_t template_name);
  105. static void statement_instance_logfunc (NCDModuleInst *inst);
  106. static void statement_instance_func_interp_exit (void *vinterp, int exit_code);
  107. static int statement_instance_func_interp_getargs (void *vinterp, NCDValMem *mem, NCDValRef *out_value);
  108. static btime_t statement_instance_func_interp_getretrytime (void *vinterp);
  109. static void process_moduleprocess_func_event (struct process *p, int event);
  110. static int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object);
  111. int NCDInterpreter_Init (NCDInterpreter *o, const char *program, size_t program_len, struct NCDInterpreter_params params)
  112. {
  113. ASSERT(program);
  114. ASSERT(params.handler_finished);
  115. ASSERT(params.num_extra_args >= 0);
  116. ASSERT(params.reactor);
  117. #ifndef BADVPN_NO_PROCESS
  118. ASSERT(params.manager);
  119. #endif
  120. #ifndef BADVPN_NO_UDEV
  121. ASSERT(params.umanager);
  122. #endif
  123. #ifndef BADVPN_NO_RANDOM
  124. ASSERT(params.random2);
  125. #endif
  126. // set params
  127. o->params = params;
  128. // set not terminating
  129. o->terminating = 0;
  130. // init string index
  131. if (!NCDStringIndex_Init(&o->string_index)) {
  132. BLog(BLOG_ERROR, "NCDStringIndex_Init failed");
  133. goto fail0;
  134. }
  135. // init method index
  136. if (!NCDMethodIndex_Init(&o->method_index, &o->string_index)) {
  137. BLog(BLOG_ERROR, "NCDMethodIndex_Init failed");
  138. goto fail1;
  139. }
  140. // init module index
  141. if (!NCDModuleIndex_Init(&o->mindex)) {
  142. BLog(BLOG_ERROR, "NCDModuleIndex_Init failed");
  143. goto fail2;
  144. }
  145. // add module groups to index and allocate string id's for base_type's
  146. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  147. if (!NCDModuleIndex_AddGroup(&o->mindex, *g, &o->method_index)) {
  148. BLog(BLOG_ERROR, "NCDModuleIndex_AddGroup failed");
  149. goto fail3;
  150. }
  151. if (!alloc_base_type_strings(o, *g)) {
  152. goto fail3;
  153. }
  154. }
  155. // parse config file
  156. if (!NCDConfigParser_Parse((char *)program, program_len, &o->program)) {
  157. BLog(BLOG_ERROR, "NCDConfigParser_Parse failed");
  158. goto fail3;
  159. }
  160. // desugar
  161. if (!NCDSugar_Desugar(&o->program)) {
  162. BLog(BLOG_ERROR, "NCDSugar_Desugar failed");
  163. goto fail4;
  164. }
  165. // init placeholder database
  166. if (!NCDPlaceholderDb_Init(&o->placeholder_db, &o->string_index)) {
  167. BLog(BLOG_ERROR, "NCDPlaceholderDb_Init failed");
  168. goto fail4;
  169. }
  170. // init interp program
  171. if (!NCDInterpProg_Init(&o->iprogram, &o->program, &o->string_index, &o->placeholder_db, &o->mindex, &o->method_index)) {
  172. BLog(BLOG_ERROR, "NCDInterpProg_Init failed");
  173. goto fail5;
  174. }
  175. // init pointers to global resources in out struct NCDModuleInst_iparams.
  176. // Don't initialize any callback at this point as these must not be called
  177. // from globalinit functions of modules.
  178. o->module_iparams.reactor = params.reactor;
  179. #ifndef BADVPN_NO_PROCESS
  180. o->module_iparams.manager = params.manager;
  181. #endif
  182. #ifndef BADVPN_NO_UDEV
  183. o->module_iparams.umanager = params.umanager;
  184. #endif
  185. #ifndef BADVPN_NO_RANDOM
  186. o->module_iparams.random2 = params.random2;
  187. #endif
  188. o->module_iparams.string_index = &o->string_index;
  189. // init modules
  190. o->num_inited_modules = 0;
  191. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  192. // map strings
  193. if ((*g)->strings && !NCDStringIndex_GetRequests(&o->string_index, (*g)->strings)) {
  194. BLog(BLOG_ERROR, "NCDStringIndex_GetRequests failed for some module");
  195. goto fail6;
  196. }
  197. // call func_globalinit
  198. if ((*g)->func_globalinit && !(*g)->func_globalinit(&o->module_iparams)) {
  199. BLog(BLOG_ERROR, "globalinit failed for some module");
  200. goto fail6;
  201. }
  202. o->num_inited_modules++;
  203. }
  204. // init the rest of the module parameters structures
  205. o->module_params.func_event = statement_instance_func_event;
  206. o->module_params.func_getobj = statement_instance_func_getobj;
  207. o->module_params.logfunc = (BLog_logfunc)statement_instance_logfunc;
  208. o->module_params.iparams = &o->module_iparams;
  209. o->module_iparams.user = o;
  210. o->module_iparams.func_initprocess = statement_instance_func_initprocess;
  211. o->module_iparams.func_interp_exit = statement_instance_func_interp_exit;
  212. o->module_iparams.func_interp_getargs = statement_instance_func_interp_getargs;
  213. o->module_iparams.func_interp_getretrytime = statement_instance_func_interp_getretrytime;
  214. // init processes list
  215. LinkedList1_Init(&o->processes);
  216. // init processes
  217. for (NCDProcess *p = NCDProgram_FirstProcess(&o->program); p; p = NCDProgram_NextProcess(&o->program, p)) {
  218. if (NCDProcess_IsTemplate(p)) {
  219. continue;
  220. }
  221. // get string id for process name
  222. NCD_string_id_t name_id = NCDStringIndex_Lookup(&o->string_index, NCDProcess_Name(p));
  223. ASSERT(name_id >= 0)
  224. // find iprocess
  225. NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&o->iprogram, name_id);
  226. ASSERT(iprocess)
  227. if (!process_new(o, iprocess, NULL)) {
  228. BLog(BLOG_ERROR, "failed to initialize process, exiting");
  229. goto fail7;
  230. }
  231. }
  232. DebugObject_Init(&o->d_obj);
  233. return 1;
  234. fail7:;
  235. LinkedList1Node *ln;
  236. while (ln = LinkedList1_GetFirst(&o->processes)) {
  237. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  238. NCDModuleProcess *mp;
  239. process_free(p, &mp);
  240. ASSERT(!mp)
  241. }
  242. fail6:
  243. // free modules
  244. while (o->num_inited_modules-- > 0) {
  245. const struct NCDModuleGroup **g = &ncd_modules[o->num_inited_modules];
  246. if ((*g)->func_globalfree) {
  247. (*g)->func_globalfree();
  248. }
  249. }
  250. // free interp program
  251. NCDInterpProg_Free(&o->iprogram);
  252. fail5:
  253. // free placeholder database
  254. NCDPlaceholderDb_Free(&o->placeholder_db);
  255. fail4:
  256. // free program AST
  257. NCDProgram_Free(&o->program);
  258. fail3:
  259. // free module index
  260. NCDModuleIndex_Free(&o->mindex);
  261. fail2:
  262. // free method index
  263. NCDMethodIndex_Free(&o->method_index);
  264. fail1:
  265. // free string index
  266. NCDStringIndex_Free(&o->string_index);
  267. fail0:
  268. return 0;
  269. }
  270. void NCDInterpreter_Free (NCDInterpreter *o)
  271. {
  272. DebugObject_Free(&o->d_obj);
  273. // any process that exists must be completely uninitialized
  274. // free processes
  275. LinkedList1Node *ln;
  276. while (ln = LinkedList1_GetFirst(&o->processes)) {
  277. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  278. NCDModuleProcess *mp;
  279. process_free(p, &mp);
  280. ASSERT(!mp)
  281. }
  282. // free modules
  283. while (o->num_inited_modules-- > 0) {
  284. const struct NCDModuleGroup **g = &ncd_modules[o->num_inited_modules];
  285. if ((*g)->func_globalfree) {
  286. (*g)->func_globalfree();
  287. }
  288. }
  289. // free interp program
  290. NCDInterpProg_Free(&o->iprogram);
  291. // free placeholder database
  292. NCDPlaceholderDb_Free(&o->placeholder_db);
  293. // free program AST
  294. NCDProgram_Free(&o->program);
  295. // free module index
  296. NCDModuleIndex_Free(&o->mindex);
  297. // free method index
  298. NCDMethodIndex_Free(&o->method_index);
  299. // free string index
  300. NCDStringIndex_Free(&o->string_index);
  301. }
  302. void NCDInterpreter_RequestShutdown (NCDInterpreter *o, int exit_code)
  303. {
  304. DebugObject_Access(&o->d_obj);
  305. start_terminate(o, exit_code);
  306. }
  307. void start_terminate (NCDInterpreter *interp, int exit_code)
  308. {
  309. // remember exit code
  310. interp->main_exit_code = exit_code;
  311. // if we're already terminating, there's nothing to do
  312. if (interp->terminating) {
  313. return;
  314. }
  315. // set the terminating flag
  316. interp->terminating = 1;
  317. // if there are no processes, we're done
  318. if (LinkedList1_IsEmpty(&interp->processes)) {
  319. interp->params.handler_finished(interp->params.user, interp->main_exit_code);
  320. return;
  321. }
  322. // start terminating non-template processes
  323. for (LinkedList1Node *ln = LinkedList1_GetFirst(&interp->processes); ln; ln = LinkedList1Node_Next(ln)) {
  324. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  325. if (p->module_process) {
  326. continue;
  327. }
  328. process_start_terminating(p);
  329. }
  330. }
  331. char * implode_id_strings (NCDInterpreter *interp, const NCD_string_id_t *names, size_t num_names, char del)
  332. {
  333. ExpString str;
  334. if (!ExpString_Init(&str)) {
  335. goto fail0;
  336. }
  337. int is_first = 1;
  338. while (num_names > 0) {
  339. if (!is_first && !ExpString_AppendChar(&str, del)) {
  340. goto fail1;
  341. }
  342. const char *name_str = NCDStringIndex_Value(&interp->string_index, *names);
  343. if (!ExpString_Append(&str, name_str)) {
  344. goto fail1;
  345. }
  346. names++;
  347. num_names--;
  348. is_first = 0;
  349. }
  350. return ExpString_Get(&str);
  351. fail1:
  352. ExpString_Free(&str);
  353. fail0:
  354. return NULL;
  355. }
  356. int alloc_base_type_strings (NCDInterpreter *interp, const struct NCDModuleGroup *g)
  357. {
  358. for (struct NCDModule *m = g->modules; m->type; m++) {
  359. const char *type = (m->base_type ? m->base_type : m->type);
  360. ASSERT(type)
  361. m->base_type_id = NCDStringIndex_Get(&interp->string_index, type);
  362. if (m->base_type_id < 0) {
  363. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  364. return 0;
  365. }
  366. }
  367. return 1;
  368. }
  369. int process_new (NCDInterpreter *interp, NCDInterpProcess *iprocess, NCDModuleProcess *module_process)
  370. {
  371. ASSERT(iprocess)
  372. // get number of statements
  373. int num_statements = NCDInterpProcess_NumStatements(iprocess);
  374. // get size of preallocated memory
  375. int mem_size = NCDInterpProcess_PreallocSize(iprocess);
  376. if (mem_size < 0) {
  377. goto fail0;
  378. }
  379. // start with size of process structure
  380. size_t alloc_size = sizeof(struct process);
  381. // add size of statements array
  382. if (num_statements > SIZE_MAX / sizeof(struct statement)) {
  383. goto fail0;
  384. }
  385. if (!BSizeAdd(&alloc_size, num_statements * sizeof(struct statement))) {
  386. goto fail0;
  387. }
  388. // align for preallocated memory
  389. if (!BSizeAlign(&alloc_size, BMAX_ALIGN)) {
  390. goto fail0;
  391. }
  392. size_t mem_off = alloc_size;
  393. // add size of preallocated memory
  394. if (mem_size > SIZE_MAX || !BSizeAdd(&alloc_size, mem_size)) {
  395. goto fail0;
  396. }
  397. // allocate memory
  398. struct process *p = BAlloc(alloc_size);
  399. if (!p) {
  400. goto fail0;
  401. }
  402. // set variables
  403. p->interp = interp;
  404. p->reactor = interp->params.reactor;
  405. p->iprocess = iprocess;
  406. p->module_process = module_process;
  407. p->ap = 0;
  408. p->fp = 0;
  409. p->num_statements = num_statements;
  410. p->error = 0;
  411. p->have_alloc = 0;
  412. // set module process handlers
  413. if (p->module_process) {
  414. NCDModuleProcess_Interp_SetHandlers(p->module_process, p,
  415. (NCDModuleProcess_interp_func_event)process_moduleprocess_func_event,
  416. (NCDModuleProcess_interp_func_getobj)process_moduleprocess_func_getobj);
  417. }
  418. // init statements
  419. char *mem = (char *)p + mem_off;
  420. for (int i = 0; i < num_statements; i++) {
  421. struct statement *ps = &p->statements[i];
  422. ps->i = i;
  423. ps->inst.istate = SSTATE_FORGOTTEN;
  424. ps->mem_size = NCDInterpProcess_StatementPreallocSize(iprocess, i);
  425. ps->inst.mem = mem + NCDInterpProcess_StatementPreallocOffset(iprocess, i);
  426. }
  427. // init timer
  428. BSmallTimer_Init(&p->wait_timer, process_wait_timer_handler);
  429. // set state, init work job
  430. process_set_state(p, PSTATE_WORKING);
  431. BSmallPending_Init(&p->work_job, BReactor_PendingGroup(p->reactor), (BSmallPending_handler)process_work_job_handler_working, p);
  432. // insert to processes list
  433. LinkedList1_Append(&interp->processes, &p->list_node);
  434. // schedule work
  435. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  436. return 1;
  437. fail0:
  438. BLog(BLOG_ERROR, "failed to allocate memory for process %s", NCDInterpProcess_Name(iprocess));
  439. return 0;
  440. }
  441. void process_set_state (struct process *p, int state)
  442. {
  443. #ifndef NDEBUG
  444. p->state = state;
  445. #endif
  446. }
  447. void process_free (struct process *p, NCDModuleProcess **out_mp)
  448. {
  449. ASSERT(p->ap == 0)
  450. ASSERT(p->fp == 0)
  451. ASSERT(out_mp)
  452. // give module process to caller so it can inform the process creator that the process has terminated
  453. *out_mp = p->module_process;
  454. // free statement memory
  455. if (p->have_alloc) {
  456. for (int i = 0; i < p->num_statements; i++) {
  457. struct statement *ps = &p->statements[i];
  458. if (statement_mem_is_allocated(ps)) {
  459. free(ps->inst.mem);
  460. }
  461. }
  462. }
  463. // remove from processes list
  464. LinkedList1_Remove(&p->interp->processes, &p->list_node);
  465. // free work job
  466. BSmallPending_Free(&p->work_job, BReactor_PendingGroup(p->reactor));
  467. // free timer
  468. BReactor_RemoveSmallTimer(p->reactor, &p->wait_timer);
  469. // free strucure
  470. BFree(p);
  471. }
  472. void process_start_terminating (struct process *p)
  473. {
  474. // set state terminating
  475. process_set_state(p, PSTATE_TERMINATING);
  476. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_terminating, p);
  477. // schedule work
  478. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  479. }
  480. int process_have_child (struct process *p)
  481. {
  482. return (p->ap > 0 && p->statements[p->ap - 1].inst.istate == SSTATE_CHILD);
  483. }
  484. void process_assert_pointers (struct process *p)
  485. {
  486. ASSERT(p->ap <= p->num_statements)
  487. ASSERT(p->fp >= p->ap)
  488. ASSERT(p->fp <= p->num_statements)
  489. #ifndef NDEBUG
  490. // check AP
  491. for (int i = 0; i < p->ap; i++) {
  492. if (i == p->ap - 1) {
  493. ASSERT(p->statements[i].inst.istate == SSTATE_ADULT || p->statements[i].inst.istate == SSTATE_CHILD)
  494. } else {
  495. ASSERT(p->statements[i].inst.istate == SSTATE_ADULT)
  496. }
  497. }
  498. // check FP
  499. int fp = p->num_statements;
  500. while (fp > 0 && p->statements[fp - 1].inst.istate == SSTATE_FORGOTTEN) {
  501. fp--;
  502. }
  503. ASSERT(p->fp == fp)
  504. #endif
  505. }
  506. void process_logfunc (struct process *p)
  507. {
  508. BLog_Append("process %s: ", NCDInterpProcess_Name(p->iprocess));
  509. }
  510. void process_log (struct process *p, int level, const char *fmt, ...)
  511. {
  512. va_list vl;
  513. va_start(vl, fmt);
  514. BLog_LogViaFuncVarArg((BLog_logfunc)process_logfunc, p, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  515. va_end(vl);
  516. }
  517. void process_work_job_handler_working (struct process *p)
  518. {
  519. process_assert_pointers(p);
  520. ASSERT(p->state == PSTATE_WORKING)
  521. // cleaning up?
  522. if (p->ap < p->fp) {
  523. // order the last living statement to die, if needed
  524. struct statement *ps = &p->statements[p->fp - 1];
  525. if (ps->inst.istate != SSTATE_DYING) {
  526. statement_log(ps, BLOG_INFO, "killing");
  527. // set statement state DYING
  528. ps->inst.istate = SSTATE_DYING;
  529. // order it to die
  530. NCDModuleInst_Die(&ps->inst);
  531. return;
  532. }
  533. return;
  534. }
  535. // clean?
  536. if (process_have_child(p)) {
  537. ASSERT(p->ap > 0)
  538. ASSERT(p->ap <= p->num_statements)
  539. struct statement *ps = &p->statements[p->ap - 1];
  540. ASSERT(ps->inst.istate == SSTATE_CHILD)
  541. statement_log(ps, BLOG_INFO, "clean");
  542. // report clean
  543. NCDModuleInst_Clean(&ps->inst);
  544. return;
  545. }
  546. // advancing?
  547. if (p->ap < p->num_statements) {
  548. struct statement *ps = &p->statements[p->ap];
  549. ASSERT(ps->inst.istate == SSTATE_FORGOTTEN)
  550. if (p->error) {
  551. statement_log(ps, BLOG_INFO, "waiting after error");
  552. // clear error
  553. p->error = 0;
  554. // set wait timer
  555. BReactor_SetSmallTimer(p->reactor, &p->wait_timer, BTIMER_SET_RELATIVE, p->interp->params.retry_time);
  556. } else {
  557. // advance
  558. process_advance(p);
  559. }
  560. return;
  561. }
  562. // we've finished
  563. process_log(p, BLOG_INFO, "victory");
  564. // set state up
  565. process_set_state(p, PSTATE_UP);
  566. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_up, p);
  567. // set module process up
  568. if (p->module_process) {
  569. NCDModuleProcess_Interp_Up(p->module_process);
  570. return;
  571. }
  572. }
  573. void process_work_job_handler_up (struct process *p)
  574. {
  575. process_assert_pointers(p);
  576. ASSERT(p->state == PSTATE_UP)
  577. ASSERT(p->ap < p->num_statements || process_have_child(p))
  578. // if we have module process, wait for its permission to continue
  579. if (p->module_process) {
  580. // set state waiting
  581. process_set_state(p, PSTATE_WAITING);
  582. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_waiting, p);
  583. // set module process down
  584. NCDModuleProcess_Interp_Down(p->module_process);
  585. return;
  586. }
  587. // set state working
  588. process_set_state(p, PSTATE_WORKING);
  589. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_working, p);
  590. // delegate the rest to the working handler
  591. process_work_job_handler_working(p);
  592. }
  593. void process_work_job_handler_waiting (struct process *p)
  594. {
  595. process_assert_pointers(p);
  596. ASSERT(p->state == PSTATE_WAITING)
  597. // do absolutely nothing. Having this no-op handler avoids a branch
  598. // in statement_instance_func_event().
  599. }
  600. void process_work_job_handler_terminating (struct process *p)
  601. {
  602. process_assert_pointers(p);
  603. ASSERT(p->state == PSTATE_TERMINATING)
  604. again:
  605. if (p->fp == 0) {
  606. NCDInterpreter *interp = p->interp;
  607. // free process
  608. NCDModuleProcess *mp;
  609. process_free(p, &mp);
  610. // if program is terminating amd there are no more processes, exit program
  611. if (interp->terminating && LinkedList1_IsEmpty(&interp->processes)) {
  612. ASSERT(!mp)
  613. interp->params.handler_finished(interp->params.user, interp->main_exit_code);
  614. return;
  615. }
  616. // inform the process creator that the process has terminated
  617. if (mp) {
  618. NCDModuleProcess_Interp_Terminated(mp);
  619. return;
  620. }
  621. return;
  622. }
  623. // order the last living statement to die, if needed
  624. struct statement *ps = &p->statements[p->fp - 1];
  625. ASSERT(ps->inst.istate != SSTATE_FORGOTTEN)
  626. if (ps->inst.istate != SSTATE_DYING) {
  627. statement_log(ps, BLOG_INFO, "killing");
  628. // update AP
  629. if (p->ap > ps->i) {
  630. p->ap = ps->i;
  631. }
  632. // optimize for statements which can be destroyed immediately
  633. if (NCDModuleInst_TryFree(&ps->inst)) {
  634. if (BLog_WouldLog(BLOG_INFO, BLOG_CURRENT_CHANNEL)) {
  635. if (ps->inst.is_error) {
  636. statement_log(ps, BLOG_ERROR, "died with error");
  637. } else {
  638. statement_log(ps, BLOG_INFO, "died");
  639. }
  640. }
  641. // free arguments memory
  642. NCDValMem_Free(&ps->args_mem);
  643. // set statement state FORGOTTEN
  644. ps->inst.istate = SSTATE_FORGOTTEN;
  645. // update FP
  646. while (p->fp > 0 && p->statements[p->fp - 1].inst.istate == SSTATE_FORGOTTEN) {
  647. p->fp--;
  648. }
  649. goto again;
  650. }
  651. // set statement state DYING
  652. ps->inst.istate = SSTATE_DYING;
  653. // order it to die
  654. NCDModuleInst_Die(&ps->inst);
  655. return;
  656. }
  657. }
  658. int replace_placeholders_callback (void *arg, int plid, NCDValMem *mem, NCDValRef *out)
  659. {
  660. struct statement *ps = arg;
  661. ASSERT(plid >= 0)
  662. ASSERT(mem)
  663. ASSERT(out)
  664. struct process *p = statement_process(ps);
  665. const NCD_string_id_t *varnames;
  666. size_t num_names;
  667. NCDPlaceholderDb_GetVariable(&p->interp->placeholder_db, plid, &varnames, &num_names);
  668. return process_resolve_variable_expr(statement_process(ps), ps->i, varnames, num_names, mem, out);
  669. }
  670. void process_advance (struct process *p)
  671. {
  672. process_assert_pointers(p);
  673. ASSERT(p->ap == p->fp)
  674. ASSERT(!process_have_child(p))
  675. ASSERT(p->ap < p->num_statements)
  676. ASSERT(!p->error)
  677. ASSERT(!BSmallPending_IsSet(&p->work_job))
  678. ASSERT(p->state == PSTATE_WORKING)
  679. struct statement *ps = &p->statements[p->ap];
  680. ASSERT(ps->inst.istate == SSTATE_FORGOTTEN)
  681. statement_log(ps, BLOG_INFO, "initializing");
  682. // need to determine the module and object to use it on (if it's a method)
  683. const struct NCDModule *module;
  684. void *method_context = NULL;
  685. // get object names, e.g. "my.cat" in "my.cat->meow();"
  686. // (or NULL if this is not a method statement)
  687. const NCD_string_id_t *objnames;
  688. size_t num_objnames;
  689. NCDInterpProcess_StatementObjNames(p->iprocess, p->ap, &objnames, &num_objnames);
  690. if (!objnames) {
  691. // not a method; module is already known by NCDInterpProcess
  692. module = NCDInterpProcess_StatementGetSimpleModule(p->iprocess, p->ap);
  693. if (!module) {
  694. statement_log(ps, BLOG_ERROR, "unknown simple statement: %s", NCDInterpProcess_StatementCmdName(p->iprocess, p->ap));
  695. goto fail0;
  696. }
  697. } else {
  698. // get object
  699. NCDObject object;
  700. if (!process_resolve_object_expr(p, p->ap, objnames, num_objnames, &object)) {
  701. goto fail0;
  702. }
  703. method_context = object.user;
  704. // get object type
  705. NCD_string_id_t object_type = NCDObject_Type(&object);
  706. if (object_type < 0) {
  707. statement_log(ps, BLOG_ERROR, "cannot call method on object with no type");
  708. goto fail0;
  709. }
  710. // find module based on type of object
  711. module = NCDInterpProcess_StatementGetMethodModule(p->iprocess, p->ap, object_type, &p->interp->method_index);
  712. if (!module) {
  713. const char *type_str = NCDStringIndex_Value(&p->interp->string_index, object_type);
  714. statement_log(ps, BLOG_ERROR, "unknown method statement: %s::%s", type_str, NCDInterpProcess_StatementCmdName(p->iprocess, p->ap));
  715. goto fail0;
  716. }
  717. }
  718. // copy arguments
  719. NCDValRef args;
  720. NCDValReplaceProg prog;
  721. if (!NCDInterpProcess_CopyStatementArgs(p->iprocess, ps->i, &ps->args_mem, &args, &prog)) {
  722. statement_log(ps, BLOG_ERROR, "NCDInterpProcess_CopyStatementArgs failed");
  723. goto fail0;
  724. }
  725. // replace placeholders with values of variables
  726. if (!NCDValReplaceProg_Execute(prog, &ps->args_mem, replace_placeholders_callback, ps)) {
  727. statement_log(ps, BLOG_ERROR, "failed to replace variables in arguments with values");
  728. goto fail1;
  729. }
  730. // allocate memory
  731. if (!statement_allocate_memory(ps, module->alloc_size)) {
  732. statement_log(ps, BLOG_ERROR, "failed to allocate memory");
  733. goto fail1;
  734. }
  735. // set statement state CHILD
  736. ps->inst.istate = SSTATE_CHILD;
  737. // increment AP
  738. p->ap++;
  739. // increment FP
  740. p->fp++;
  741. process_assert_pointers(p);
  742. // initialize module instance
  743. NCDModuleInst_Init(&ps->inst, module, method_context, args, &p->interp->module_params);
  744. return;
  745. fail1:
  746. NCDValMem_Free(&ps->args_mem);
  747. fail0:
  748. // set error
  749. p->error = 1;
  750. // schedule work to start the timer
  751. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  752. }
  753. void process_wait_timer_handler (BSmallTimer *timer)
  754. {
  755. struct process *p = UPPER_OBJECT(timer, struct process, wait_timer);
  756. process_assert_pointers(p);
  757. ASSERT(!BSmallPending_IsSet(&p->work_job))
  758. // check if something happened that means we no longer need to retry
  759. if (p->ap != p->fp || process_have_child(p) || p->ap == p->num_statements) {
  760. return;
  761. }
  762. process_log(p, BLOG_INFO, "retrying");
  763. // advance. Note: the asserts for this are indeed satisfied, though this
  764. // it not trivial to prove.
  765. process_advance(p);
  766. }
  767. int process_find_object (struct process *p, int pos, NCD_string_id_t name, NCDObject *out_object)
  768. {
  769. ASSERT(pos >= 0)
  770. ASSERT(pos <= p->num_statements)
  771. ASSERT(out_object)
  772. int i = NCDInterpProcess_FindStatement(p->iprocess, pos, name);
  773. if (i >= 0) {
  774. struct statement *ps = &p->statements[i];
  775. ASSERT(i < p->num_statements)
  776. if (ps->inst.istate == SSTATE_FORGOTTEN) {
  777. process_log(p, BLOG_ERROR, "statement (%d) is uninitialized", i);
  778. return 0;
  779. }
  780. *out_object = NCDModuleInst_Object(&ps->inst);
  781. return 1;
  782. }
  783. if (p->module_process && NCDModuleProcess_Interp_GetSpecialObj(p->module_process, name, out_object)) {
  784. return 1;
  785. }
  786. return 0;
  787. }
  788. int process_resolve_object_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDObject *out_object)
  789. {
  790. ASSERT(pos >= 0)
  791. ASSERT(pos <= p->num_statements)
  792. ASSERT(names)
  793. ASSERT(num_names > 0)
  794. ASSERT(out_object)
  795. NCDObject object;
  796. if (!process_find_object(p, pos, names[0], &object)) {
  797. goto fail;
  798. }
  799. if (!NCDObject_ResolveObjExprCompact(&object, names + 1, num_names - 1, out_object)) {
  800. goto fail;
  801. }
  802. return 1;
  803. fail:;
  804. char *name = implode_id_strings(p->interp, names, num_names, '.');
  805. process_log(p, BLOG_ERROR, "failed to resolve object (%s) from position %zu", (name ? name : ""), pos);
  806. free(name);
  807. return 0;
  808. }
  809. int process_resolve_variable_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value)
  810. {
  811. ASSERT(pos >= 0)
  812. ASSERT(pos <= p->num_statements)
  813. ASSERT(names)
  814. ASSERT(num_names > 0)
  815. ASSERT(mem)
  816. ASSERT(out_value)
  817. NCDObject object;
  818. if (!process_find_object(p, pos, names[0], &object)) {
  819. goto fail;
  820. }
  821. if (!NCDObject_ResolveVarExprCompact(&object, names + 1, num_names - 1, mem, out_value)) {
  822. goto fail;
  823. }
  824. return 1;
  825. fail:;
  826. char *name = implode_id_strings(p->interp, names, num_names, '.');
  827. process_log(p, BLOG_ERROR, "failed to resolve variable (%s) from position %zu", (name ? name : ""), pos);
  828. free(name);
  829. return 0;
  830. }
  831. void statement_logfunc (struct statement *ps)
  832. {
  833. process_logfunc(statement_process(ps));
  834. BLog_Append("statement %zu: ", ps->i);
  835. }
  836. void statement_log (struct statement *ps, int level, const char *fmt, ...)
  837. {
  838. if (!BLog_WouldLog(BLOG_CURRENT_CHANNEL, level)) {
  839. return;
  840. }
  841. va_list vl;
  842. va_start(vl, fmt);
  843. BLog_LogViaFuncVarArg((BLog_logfunc)statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  844. va_end(vl);
  845. }
  846. struct process * statement_process (struct statement *ps)
  847. {
  848. return UPPER_OBJECT(ps - ps->i, struct process, statements);
  849. }
  850. int statement_mem_is_allocated (struct statement *ps)
  851. {
  852. return (ps->mem_size < 0);
  853. }
  854. int statement_mem_size (struct statement *ps)
  855. {
  856. return (ps->mem_size >= 0 ? ps->mem_size : -ps->mem_size);
  857. }
  858. int statement_allocate_memory (struct statement *ps, int alloc_size)
  859. {
  860. ASSERT(alloc_size >= 0)
  861. if (alloc_size > statement_mem_size(ps)) {
  862. // allocate new memory
  863. char *new_mem = malloc(alloc_size);
  864. if (!new_mem) {
  865. statement_log(ps, BLOG_ERROR, "malloc failed");
  866. return 0;
  867. }
  868. // release old memory unless it was preallocated
  869. if (statement_mem_is_allocated(ps)) {
  870. free(ps->inst.mem);
  871. }
  872. struct process *p = statement_process(ps);
  873. // register memory in statement
  874. ps->inst.mem = new_mem;
  875. ps->mem_size = -alloc_size;
  876. // set the alloc flag in the process to make sure process_free()
  877. // releases the allocated memory
  878. p->have_alloc = 1;
  879. // register alloc size for future preallocations
  880. NCDInterpProcess_StatementBumpAllocSize(p->iprocess, ps->i, alloc_size);
  881. }
  882. return 1;
  883. }
  884. void statement_instance_func_event (NCDModuleInst *inst, int event)
  885. {
  886. struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
  887. ASSERT(ps->inst.istate == SSTATE_CHILD || ps->inst.istate == SSTATE_ADULT || ps->inst.istate == SSTATE_DYING)
  888. struct process *p = statement_process(ps);
  889. process_assert_pointers(p);
  890. // schedule work
  891. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  892. switch (event) {
  893. case NCDMODULE_EVENT_UP: {
  894. ASSERT(ps->inst.istate == SSTATE_CHILD)
  895. statement_log(ps, BLOG_INFO, "up");
  896. // set state ADULT
  897. ps->inst.istate = SSTATE_ADULT;
  898. } break;
  899. case NCDMODULE_EVENT_DOWN: {
  900. ASSERT(ps->inst.istate == SSTATE_ADULT)
  901. statement_log(ps, BLOG_INFO, "down");
  902. // set state CHILD
  903. ps->inst.istate = SSTATE_CHILD;
  904. // clear error
  905. if (ps->i < p->ap) {
  906. p->error = 0;
  907. }
  908. // update AP
  909. if (p->ap > ps->i + 1) {
  910. p->ap = ps->i + 1;
  911. }
  912. } break;
  913. case NCDMODULE_EVENT_DEAD: {
  914. if (BLog_WouldLog(BLOG_INFO, BLOG_CURRENT_CHANNEL)) {
  915. if (ps->inst.is_error) {
  916. statement_log(ps, BLOG_ERROR, "died with error");
  917. } else {
  918. statement_log(ps, BLOG_INFO, "died");
  919. }
  920. }
  921. // free instance
  922. NCDModuleInst_Free(&ps->inst);
  923. // free arguments memory
  924. NCDValMem_Free(&ps->args_mem);
  925. // set state FORGOTTEN
  926. ps->inst.istate = SSTATE_FORGOTTEN;
  927. // set error
  928. if (ps->inst.is_error && ps->i < p->ap) {
  929. p->error = 1;
  930. }
  931. // update AP
  932. if (p->ap > ps->i) {
  933. p->ap = ps->i;
  934. }
  935. // update FP
  936. while (p->fp > 0 && p->statements[p->fp - 1].inst.istate == SSTATE_FORGOTTEN) {
  937. p->fp--;
  938. }
  939. } break;
  940. }
  941. }
  942. int statement_instance_func_getobj (NCDModuleInst *inst, NCD_string_id_t objname, NCDObject *out_object)
  943. {
  944. struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
  945. ASSERT(ps->inst.istate != SSTATE_FORGOTTEN)
  946. return process_find_object(statement_process(ps), ps->i, objname, out_object);
  947. }
  948. int statement_instance_func_initprocess (void *vinterp, NCDModuleProcess* mp, NCD_string_id_t template_name)
  949. {
  950. NCDInterpreter *interp = vinterp;
  951. // find process
  952. NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&interp->iprogram, template_name);
  953. if (!iprocess) {
  954. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  955. BLog(BLOG_ERROR, "no template named %s", str);
  956. return 0;
  957. }
  958. // make sure it's a template
  959. if (!NCDInterpProcess_IsTemplate(iprocess)) {
  960. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  961. BLog(BLOG_ERROR, "need template to create a process, but %s is a process", str);
  962. return 0;
  963. }
  964. // create process
  965. if (!process_new(interp, iprocess, mp)) {
  966. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  967. BLog(BLOG_ERROR, "failed to create process from template %s", str);
  968. return 0;
  969. }
  970. if (BLog_WouldLog(BLOG_INFO, BLOG_CURRENT_CHANNEL)) {
  971. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  972. BLog(BLOG_INFO, "created process from template %s", str);
  973. }
  974. return 1;
  975. }
  976. void statement_instance_logfunc (NCDModuleInst *inst)
  977. {
  978. struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
  979. ASSERT(ps->inst.istate != SSTATE_FORGOTTEN)
  980. statement_logfunc(ps);
  981. BLog_Append("module: ");
  982. }
  983. void statement_instance_func_interp_exit (void *vinterp, int exit_code)
  984. {
  985. NCDInterpreter *interp = vinterp;
  986. start_terminate(interp, exit_code);
  987. }
  988. int statement_instance_func_interp_getargs (void *vinterp, NCDValMem *mem, NCDValRef *out_value)
  989. {
  990. NCDInterpreter *interp = vinterp;
  991. *out_value = NCDVal_NewList(mem, interp->params.num_extra_args);
  992. if (NCDVal_IsInvalid(*out_value)) {
  993. BLog(BLOG_ERROR, "NCDVal_NewList failed");
  994. goto fail;
  995. }
  996. for (int i = 0; i < interp->params.num_extra_args; i++) {
  997. NCDValRef arg = NCDVal_NewString(mem, interp->params.extra_args[i]);
  998. if (NCDVal_IsInvalid(arg)) {
  999. BLog(BLOG_ERROR, "NCDVal_NewString failed");
  1000. goto fail;
  1001. }
  1002. NCDVal_ListAppend(*out_value, arg);
  1003. }
  1004. return 1;
  1005. fail:
  1006. *out_value = NCDVal_NewInvalid();
  1007. return 1;
  1008. }
  1009. btime_t statement_instance_func_interp_getretrytime (void *vinterp)
  1010. {
  1011. NCDInterpreter *interp = vinterp;
  1012. return interp->params.retry_time;
  1013. }
  1014. void process_moduleprocess_func_event (struct process *p, int event)
  1015. {
  1016. ASSERT(p->module_process)
  1017. switch (event) {
  1018. case NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: {
  1019. ASSERT(p->state == PSTATE_WAITING)
  1020. // set state working
  1021. process_set_state(p, PSTATE_WORKING);
  1022. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_working, p);
  1023. // schedule work
  1024. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  1025. } break;
  1026. case NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: {
  1027. ASSERT(p->state != PSTATE_TERMINATING)
  1028. process_log(p, BLOG_INFO, "process termination requested");
  1029. // start terminating
  1030. process_start_terminating(p);
  1031. } break;
  1032. default: ASSERT(0);
  1033. }
  1034. }
  1035. int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object)
  1036. {
  1037. ASSERT(p->module_process)
  1038. return process_find_object(p, p->num_statements, name, out_object);
  1039. }