/home/runner/work/panackelty/panackelty/src/vm/process_async.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "platform.h" |
2 | | #include "process_async.h" |
3 | | #include "host_capabilities.h" |
4 | | #include "host_types.h" |
5 | | #include "numeric.h" |
6 | | #include <stdlib.h> |
7 | | #include <string.h> |
8 | | #ifndef __wasi__ |
9 | | #include <pthread.h> |
10 | | #include <stdatomic.h> |
11 | | #endif |
12 | | |
13 | 1.21k | #define PROCESS_WORKERS 4 |
14 | 1.01k | #define PROCESS_COPY_LIMIT (32u * 1024u * 1024u) |
15 | 1.62k | #define PROCESS_IO_LIMIT (16u * 1024u * 1024u) |
16 | | |
17 | | struct VMProcess { |
18 | | VM worker_vm; |
19 | | Value *arguments[7], *result; |
20 | | char *ticket; |
21 | | bool isolated, done, abandoned, admitted; |
22 | | #ifndef __wasi__ |
23 | | atomic_bool cancelled; |
24 | | pthread_mutex_t mutex; |
25 | | pthread_cond_t completed; |
26 | | #endif |
27 | | }; |
28 | | |
29 | | static Value *process_error(bool isolated, const char *code, int native) |
30 | 5 | { |
31 | 5 | const char *operation = isolated ? "process_run_isolated" : "process_run"; Branch (31:29): [True: 5, False: 0]
|
32 | 5 | char *names[] = {"operation", "code", "native_code"}; |
33 | 5 | Value *fields[] = {value_data(V_STR, (const uint8_t *)operation, strlen(operation)), |
34 | 5 | value_data(V_STR, (const uint8_t *)code, strlen(code)), value_size((size_t)native)}; |
35 | 5 | Value *problem = fields[0] && fields[1] && fields[2] Branch (35:22): [True: 5, False: 0]
Branch (35:35): [True: 5, False: 0]
Branch (35:48): [True: 5, False: 0]
|
36 | 5 | ? named_value(V_RECORD, "HostError", names, fields, 3) : NULL; |
37 | 20 | for (size_t i = 0; i < 3; i++) release(fields[i]); Branch (37:24): [True: 15, False: 5]
|
38 | 5 | return host_variant("Error", problem); |
39 | 5 | } |
40 | | |
41 | | #ifndef __wasi__ |
42 | | static atomic_uint active_processes; |
43 | | static pthread_mutex_t drain_mutex = PTHREAD_MUTEX_INITIALIZER; |
44 | | static pthread_cond_t drained = PTHREAD_COND_INITIALIZER; |
45 | | |
46 | | static void free_process(VMProcess *process) |
47 | 406 | { |
48 | 3.24k | for (size_t i = 0; i < 7; i++) release(process->arguments[i]); Branch (48:24): [True: 1.42k, False: 203]
Branch (48:24): [True: 1.42k, False: 203]
|
49 | 408 | for (size_t i = 0; i < process->worker_vm.env_count; i++) free(process->worker_vm.environment[i]); Branch (49:24): [True: 1, False: 203]
Branch (49:24): [True: 1, False: 203]
|
50 | 406 | free(process->worker_vm.environment); |
51 | 406 | free(process->ticket); |
52 | 406 | release(process->result); |
53 | 406 | pthread_cond_destroy(&process->completed); |
54 | 406 | pthread_mutex_destroy(&process->mutex); |
55 | 406 | bool admitted = process->admitted; |
56 | 406 | free(process); |
57 | 406 | if (admitted) { Branch (57:9): [True: 202, False: 1]
Branch (57:9): [True: 202, False: 1]
|
58 | 404 | pthread_mutex_lock(&drain_mutex); |
59 | 404 | atomic_fetch_sub(&active_processes, 1); |
60 | 404 | pthread_cond_broadcast(&drained); |
61 | 404 | pthread_mutex_unlock(&drain_mutex); |
62 | 404 | } |
63 | 406 | } process_async.c:free_process Line | Count | Source | 47 | 203 | { | 48 | 1.62k | for (size_t i = 0; i < 7; i++) release(process->arguments[i]); Branch (48:24): [True: 1.42k, False: 203]
| 49 | 204 | for (size_t i = 0; i < process->worker_vm.env_count; i++) free(process->worker_vm.environment[i]); Branch (49:24): [True: 1, False: 203]
| 50 | 203 | free(process->worker_vm.environment); | 51 | 203 | free(process->ticket); | 52 | 203 | release(process->result); | 53 | 203 | pthread_cond_destroy(&process->completed); | 54 | 203 | pthread_mutex_destroy(&process->mutex); | 55 | 203 | bool admitted = process->admitted; | 56 | 203 | free(process); | 57 | 203 | if (admitted) { Branch (57:9): [True: 202, False: 1]
| 58 | 202 | pthread_mutex_lock(&drain_mutex); | 59 | 202 | atomic_fetch_sub(&active_processes, 1); | 60 | 202 | pthread_cond_broadcast(&drained); | 61 | 202 | pthread_mutex_unlock(&drain_mutex); | 62 | 202 | } | 63 | 203 | } |
process_async.c:free_process Line | Count | Source | 47 | 203 | { | 48 | 1.62k | for (size_t i = 0; i < 7; i++) release(process->arguments[i]); Branch (48:24): [True: 1.42k, False: 203]
| 49 | 204 | for (size_t i = 0; i < process->worker_vm.env_count; i++) free(process->worker_vm.environment[i]); Branch (49:24): [True: 1, False: 203]
| 50 | 203 | free(process->worker_vm.environment); | 51 | 203 | free(process->ticket); | 52 | 203 | release(process->result); | 53 | 203 | pthread_cond_destroy(&process->completed); | 54 | 203 | pthread_mutex_destroy(&process->mutex); | 55 | 203 | bool admitted = process->admitted; | 56 | 203 | free(process); | 57 | 203 | if (admitted) { Branch (57:9): [True: 202, False: 1]
| 58 | 202 | pthread_mutex_lock(&drain_mutex); | 59 | 202 | atomic_fetch_sub(&active_processes, 1); | 60 | 202 | pthread_cond_broadcast(&drained); | 61 | 202 | pthread_mutex_unlock(&drain_mutex); | 62 | 202 | } | 63 | 203 | } |
|
64 | | |
65 | | static Value *copy_operand(Value *value) |
66 | 2.33k | { |
67 | 2.33k | if (value->kind == V_ARRAY) { Branch (67:9): [True: 348, False: 818]
Branch (67:9): [True: 348, False: 818]
|
68 | 696 | Value *copy = value_new(V_ARRAY); |
69 | 696 | if (!copy) return NULL; Branch (69:13): [True: 8, False: 340]
Branch (69:13): [True: 8, False: 340]
|
70 | 680 | size_t count = value->as.sequence.count; |
71 | 680 | copy->as.sequence.items = calloc(count ? count : 1, sizeof(Value *)); Branch (71:42): [True: 189, False: 151]
Branch (71:42): [True: 189, False: 151]
|
72 | 680 | if (!copy->as.sequence.items) { release(copy); return NULL; } Branch (72:13): [True: 8, False: 332]
Branch (72:13): [True: 8, False: 332]
|
73 | 1.35k | for (size_t i = 0; i < count; i++) { Branch (73:28): [True: 362, False: 316]
Branch (73:28): [True: 362, False: 316]
|
74 | 724 | Value *item = value->as.sequence.items[i]; |
75 | 724 | copy->as.sequence.items[i] = value_data(V_STR, item->as.bytes.data, item->as.bytes.length); |
76 | 724 | if (!copy->as.sequence.items[i]) { release(copy); return NULL; } Branch (76:17): [True: 16, False: 346]
Branch (76:17): [True: 16, False: 346]
|
77 | 692 | copy->as.sequence.count++; |
78 | 692 | } |
79 | 632 | return copy; |
80 | 664 | } |
81 | 1.63k | if (value->kind == V_NAT || value->kind == V_DURATION) Branch (81:9): [True: 138, False: 680]
Branch (81:33): [True: 146, False: 534]
Branch (81:9): [True: 138, False: 680]
Branch (81:33): [True: 146, False: 534]
|
82 | 568 | return value_big(value->kind, &value->as.integer); |
83 | 1.06k | return value_data(value->kind, value->as.bytes.data, value->as.bytes.length); |
84 | 1.63k | } process_async.c:copy_operand Line | Count | Source | 66 | 1.16k | { | 67 | 1.16k | if (value->kind == V_ARRAY) { Branch (67:9): [True: 348, False: 818]
| 68 | 348 | Value *copy = value_new(V_ARRAY); | 69 | 348 | if (!copy) return NULL; Branch (69:13): [True: 8, False: 340]
| 70 | 340 | size_t count = value->as.sequence.count; | 71 | 340 | copy->as.sequence.items = calloc(count ? count : 1, sizeof(Value *)); Branch (71:42): [True: 189, False: 151]
| 72 | 340 | if (!copy->as.sequence.items) { release(copy); return NULL; } Branch (72:13): [True: 8, False: 332]
| 73 | 678 | for (size_t i = 0; i < count; i++) { Branch (73:28): [True: 362, False: 316]
| 74 | 362 | Value *item = value->as.sequence.items[i]; | 75 | 362 | copy->as.sequence.items[i] = value_data(V_STR, item->as.bytes.data, item->as.bytes.length); | 76 | 362 | if (!copy->as.sequence.items[i]) { release(copy); return NULL; } Branch (76:17): [True: 16, False: 346]
| 77 | 346 | copy->as.sequence.count++; | 78 | 346 | } | 79 | 316 | return copy; | 80 | 332 | } | 81 | 818 | if (value->kind == V_NAT || value->kind == V_DURATION) Branch (81:9): [True: 138, False: 680]
Branch (81:33): [True: 146, False: 534]
| 82 | 284 | return value_big(value->kind, &value->as.integer); | 83 | 534 | return value_data(value->kind, value->as.bytes.data, value->as.bytes.length); | 84 | 818 | } |
process_async.c:copy_operand Line | Count | Source | 66 | 1.16k | { | 67 | 1.16k | if (value->kind == V_ARRAY) { Branch (67:9): [True: 348, False: 818]
| 68 | 348 | Value *copy = value_new(V_ARRAY); | 69 | 348 | if (!copy) return NULL; Branch (69:13): [True: 8, False: 340]
| 70 | 340 | size_t count = value->as.sequence.count; | 71 | 340 | copy->as.sequence.items = calloc(count ? count : 1, sizeof(Value *)); Branch (71:42): [True: 189, False: 151]
| 72 | 340 | if (!copy->as.sequence.items) { release(copy); return NULL; } Branch (72:13): [True: 8, False: 332]
| 73 | 678 | for (size_t i = 0; i < count; i++) { Branch (73:28): [True: 362, False: 316]
| 74 | 362 | Value *item = value->as.sequence.items[i]; | 75 | 362 | copy->as.sequence.items[i] = value_data(V_STR, item->as.bytes.data, item->as.bytes.length); | 76 | 362 | if (!copy->as.sequence.items[i]) { release(copy); return NULL; } Branch (76:17): [True: 16, False: 346]
| 77 | 346 | copy->as.sequence.count++; | 78 | 346 | } | 79 | 316 | return copy; | 80 | 332 | } | 81 | 818 | if (value->kind == V_NAT || value->kind == V_DURATION) Branch (81:9): [True: 138, False: 680]
Branch (81:33): [True: 146, False: 534]
| 82 | 284 | return value_big(value->kind, &value->as.integer); | 83 | 534 | return value_data(value->kind, value->as.bytes.data, value->as.bytes.length); | 84 | 818 | } |
|
85 | | |
86 | | static void *process_worker(void *context) |
87 | 126 | { |
88 | 126 | VMProcess *process = context; |
89 | 126 | Value *result = host_process_run_cancelable(&process->worker_vm, process->arguments, |
90 | 126 | process->isolated, &process->cancelled, process->ticket); |
91 | 126 | pthread_mutex_lock(&process->mutex); |
92 | 126 | process->result = result; |
93 | 126 | process->done = true; |
94 | 126 | bool abandoned = process->abandoned; |
95 | 126 | pthread_cond_signal(&process->completed); |
96 | 126 | pthread_mutex_unlock(&process->mutex); |
97 | 126 | if (abandoned) free_process(process); Branch (97:9): [True: 6, False: 120]
|
98 | 126 | return NULL; |
99 | 126 | } |
100 | | |
101 | | static bool add_bytes(size_t *total, size_t amount) |
102 | 1.01k | { |
103 | 1.01k | if (amount > PROCESS_COPY_LIMIT - *total) return false; Branch (103:9): [True: 0, False: 1.01k]
|
104 | 1.01k | *total += amount; |
105 | 1.01k | return true; |
106 | 1.01k | } |
107 | | #endif |
108 | | |
109 | | VMProcess *process_async_start(VM *vm, Value **arguments, bool isolated) |
110 | 414 | { |
111 | 414 | const ValueKind kinds[] = {V_PATH, V_ARRAY, V_BYTES, V_PATH, V_ARRAY, V_DURATION, V_NAT}; |
112 | 3.31k | for (size_t i = 0; i < 7; i++) { Branch (112:24): [True: 1.44k, False: 207]
Branch (112:24): [True: 1.44k, False: 207]
|
113 | 2.89k | if (arguments[i]->kind != kinds[i]) { vm->error = "VM trap: invalid process operands"; return NULL; } Branch (113:13): [True: 0, False: 1.44k]
Branch (113:13): [True: 0, False: 1.44k]
|
114 | 2.89k | } |
115 | 414 | VMProcess *process = calloc(1, sizeof(*process)); |
116 | 414 | if (!process) return NULL; Branch (116:9): [True: 4, False: 203]
Branch (116:9): [True: 4, False: 203]
|
117 | 406 | process->isolated = isolated; |
118 | 406 | if (vm->coverage && !vm->coverage_run) vm_coverage_gap(vm->coverage); Branch (118:9): [True: 0, False: 203]
Branch (118:25): [True: 0, False: 0]
Branch (118:9): [True: 0, False: 203]
Branch (118:25): [True: 0, False: 0]
|
119 | 406 | #ifndef __wasi__ |
120 | 406 | atomic_init(&process->cancelled, false); |
121 | 406 | if (pthread_mutex_init(&process->mutex, NULL)) { free(process); return NULL; } Branch (121:9): [True: 0, False: 203]
Branch (121:9): [True: 0, False: 203]
|
122 | 406 | if (pthread_cond_init(&process->completed, NULL)) { Branch (122:9): [True: 0, False: 203]
Branch (122:9): [True: 0, False: 203]
|
123 | 0 | pthread_mutex_destroy(&process->mutex); free(process); return NULL; |
124 | 0 | } |
125 | 406 | const char *problem = NULL; |
126 | 406 | size_t timeout, limit, copied = 0; |
127 | 406 | if (arguments[5]->as.integer.sign < 0) problem = "negative_duration"; Branch (127:9): [True: 0, False: 203]
Branch (127:9): [True: 0, False: 203]
|
128 | 406 | else if (!pn_big_fits_size(&arguments[5]->as.integer, &timeout) || timeout > UINT64_C(31536000000000000)) problem = "out_of_range"; Branch (128:14): [True: 0, False: 203]
Branch (128:72): [True: 0, False: 203]
Branch (128:14): [True: 0, False: 203]
Branch (128:72): [True: 0, False: 203]
|
129 | 406 | else if (!timeout) problem = "timeout"; Branch (129:14): [True: 0, False: 203]
Branch (129:14): [True: 0, False: 203]
|
130 | 406 | if (!pn_big_fits_size(&arguments[6]->as.integer, &limit) || limit > PROCESS_IO_LIMIT || Branch (130:9): [True: 0, False: 203]
Branch (130:65): [True: 0, False: 203]
Branch (130:9): [True: 0, False: 203]
Branch (130:65): [True: 0, False: 203]
|
131 | 406 | arguments[2]->as.bytes.length > PROCESS_IO_LIMIT || (!isolated && vm->env_count > 4096)) problem = "out_of_range"; Branch (131:9): [True: 0, False: 203]
Branch (131:62): [True: 1, False: 202]
Branch (131:75): [True: 0, False: 1]
Branch (131:9): [True: 0, False: 203]
Branch (131:62): [True: 1, False: 202]
Branch (131:75): [True: 0, False: 1]
|
132 | 3.24k | for (size_t i = 0; i < 7 && !problem; i++) { Branch (132:24): [True: 1.42k, False: 203]
Branch (132:33): [True: 1.42k, False: 0]
Branch (132:24): [True: 1.42k, False: 203]
Branch (132:33): [True: 1.42k, False: 0]
|
133 | 2.84k | Value *value = arguments[i]; |
134 | 2.84k | if (value->kind == V_ARRAY) { Branch (134:13): [True: 406, False: 1.01k]
Branch (134:13): [True: 406, False: 1.01k]
|
135 | 812 | if (value->as.sequence.count > 4096) { problem = "out_of_range"; break; } Branch (135:17): [True: 0, False: 406]
Branch (135:17): [True: 0, False: 406]
|
136 | 1.62k | for (size_t j = 0; j < value->as.sequence.count; j++) { Branch (136:32): [True: 404, False: 406]
Branch (136:32): [True: 404, False: 406]
|
137 | 808 | Value *item = value->as.sequence.items[j]; |
138 | 808 | if (item->kind != V_STR) { vm->error = "VM trap: process entries require Str"; free_process(process); return NULL; } Branch (138:21): [True: 0, False: 404]
Branch (138:21): [True: 0, False: 404]
|
139 | 808 | if (!add_bytes(&copied, item->as.bytes.length)) { problem = "out_of_range"; break; } Branch (139:21): [True: 0, False: 404]
Branch (139:21): [True: 0, False: 404]
|
140 | 808 | } |
141 | 2.03k | } else if (value->kind == V_PATH || value->kind == V_BYTES) { Branch (141:20): [True: 406, False: 609]
Branch (141:45): [True: 203, False: 406]
Branch (141:20): [True: 406, False: 609]
Branch (141:45): [True: 203, False: 406]
|
142 | 1.21k | if (!add_bytes(&copied, value->as.bytes.length)) problem = "out_of_range"; Branch (142:17): [True: 0, False: 609]
Branch (142:17): [True: 0, False: 609]
|
143 | 1.21k | } |
144 | 2.84k | } |
145 | 408 | for (size_t i = 0; !isolated && i < vm->env_count && !problem; i++) Branch (145:24): [True: 2, False: 202]
Branch (145:37): [True: 1, False: 1]
Branch (145:58): [True: 1, False: 0]
Branch (145:24): [True: 2, False: 202]
Branch (145:37): [True: 1, False: 1]
Branch (145:58): [True: 1, False: 0]
|
146 | 2 | if (!add_bytes(&copied, strlen(vm->environment[i]))) problem = "out_of_range"; Branch (146:13): [True: 0, False: 1]
Branch (146:13): [True: 0, False: 1]
|
147 | 406 | if (problem) { process->result = process_error(isolated, problem, 0); process->done = true; return process; } Branch (147:9): [True: 0, False: 203]
Branch (147:9): [True: 0, False: 203]
|
148 | 406 | unsigned count = atomic_load(&active_processes); |
149 | 406 | while (count < PROCESS_WORKERS && !atomic_compare_exchange_weak(&active_processes, &count, count + 1)) {} Branch (149:12): [True: 202, False: 1]
Branch (149:39): [True: 0, False: 202]
Branch (149:12): [True: 202, False: 1]
Branch (149:39): [True: 0, False: 202]
|
150 | 406 | if (count == PROCESS_WORKERS) { Branch (150:9): [True: 1, False: 202]
Branch (150:9): [True: 1, False: 202]
|
151 | 2 | process->result = process_error(isolated, "resource_limit", 0); process->done = true; return process; |
152 | 2 | } |
153 | 404 | process->admitted = true; |
154 | 2.59k | for (size_t i = 0; i < 7; i++) { Branch (154:24): [True: 1.16k, False: 130]
Branch (154:24): [True: 1.16k, False: 130]
|
155 | 2.33k | process->arguments[i] = copy_operand(arguments[i]); |
156 | 2.33k | if (!process->arguments[i]) { free_process(process); return NULL; } Branch (156:13): [True: 72, False: 1.09k]
Branch (156:13): [True: 72, False: 1.09k]
|
157 | 2.33k | } |
158 | 260 | if (!isolated) { Branch (158:9): [True: 1, False: 129]
Branch (158:9): [True: 1, False: 129]
|
159 | 2 | process->worker_vm.environment = calloc(vm->env_count ? vm->env_count : 1, sizeof(char *)); Branch (159:49): [True: 1, False: 0]
Branch (159:49): [True: 1, False: 0]
|
160 | 2 | if (!process->worker_vm.environment) { free_process(process); return NULL; } Branch (160:13): [True: 0, False: 1]
Branch (160:13): [True: 0, False: 1]
|
161 | 4 | for (size_t i = 0; i < vm->env_count; i++) { Branch (161:28): [True: 1, False: 1]
Branch (161:28): [True: 1, False: 1]
|
162 | 2 | process->worker_vm.environment[i] = strdup(vm->environment[i]); |
163 | 2 | if (!process->worker_vm.environment[i]) { free_process(process); return NULL; } Branch (163:17): [True: 0, False: 1]
Branch (163:17): [True: 0, False: 1]
|
164 | 2 | process->worker_vm.env_count++; |
165 | 2 | } |
166 | 2 | if (vm->coverage_run) { Branch (166:13): [True: 0, False: 1]
Branch (166:13): [True: 0, False: 1]
|
167 | 0 | process->ticket = vm_coverage_process_ticket(vm->coverage_run); |
168 | 0 | if (!process->ticket) vm_coverage_gap(vm->coverage); Branch (168:17): [True: 0, False: 0]
Branch (168:17): [True: 0, False: 0]
|
169 | 0 | } |
170 | 258 | } else if (vm->coverage_run) vm_coverage_gap(vm->coverage); Branch (170:16): [True: 0, False: 129]
Branch (170:16): [True: 0, False: 129]
|
171 | 260 | pthread_attr_t attributes; |
172 | 260 | if (pthread_attr_init(&attributes)) { free_process(process); return NULL; } Branch (172:9): [True: 0, False: 130]
Branch (172:9): [True: 0, False: 130]
|
173 | 260 | int error = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); |
174 | 260 | pthread_t worker; |
175 | 260 | if (!error) error = pthread_create(&worker, &attributes, process_worker, process); Branch (175:9): [True: 130, False: 0]
Branch (175:9): [True: 130, False: 0]
|
176 | 260 | pthread_attr_destroy(&attributes); |
177 | 260 | if (error) { process->result = process_error(isolated, "resource_limit", error); process->done = true; } Branch (177:9): [True: 4, False: 126]
Branch (177:9): [True: 4, False: 126]
|
178 | | #else |
179 | | process->result = process_error(isolated, "unsupported_host", 0); process->done = true; |
180 | | #endif |
181 | 260 | return process; |
182 | 260 | } Line | Count | Source | 110 | 207 | { | 111 | 207 | const ValueKind kinds[] = {V_PATH, V_ARRAY, V_BYTES, V_PATH, V_ARRAY, V_DURATION, V_NAT}; | 112 | 1.65k | for (size_t i = 0; i < 7; i++) { Branch (112:24): [True: 1.44k, False: 207]
| 113 | 1.44k | if (arguments[i]->kind != kinds[i]) { vm->error = "VM trap: invalid process operands"; return NULL; } Branch (113:13): [True: 0, False: 1.44k]
| 114 | 1.44k | } | 115 | 207 | VMProcess *process = calloc(1, sizeof(*process)); | 116 | 207 | if (!process) return NULL; Branch (116:9): [True: 4, False: 203]
| 117 | 203 | process->isolated = isolated; | 118 | 203 | if (vm->coverage && !vm->coverage_run) vm_coverage_gap(vm->coverage); Branch (118:9): [True: 0, False: 203]
Branch (118:25): [True: 0, False: 0]
| 119 | 203 | #ifndef __wasi__ | 120 | 203 | atomic_init(&process->cancelled, false); | 121 | 203 | if (pthread_mutex_init(&process->mutex, NULL)) { free(process); return NULL; } Branch (121:9): [True: 0, False: 203]
| 122 | 203 | if (pthread_cond_init(&process->completed, NULL)) { Branch (122:9): [True: 0, False: 203]
| 123 | 0 | pthread_mutex_destroy(&process->mutex); free(process); return NULL; | 124 | 0 | } | 125 | 203 | const char *problem = NULL; | 126 | 203 | size_t timeout, limit, copied = 0; | 127 | 203 | if (arguments[5]->as.integer.sign < 0) problem = "negative_duration"; Branch (127:9): [True: 0, False: 203]
| 128 | 203 | else if (!pn_big_fits_size(&arguments[5]->as.integer, &timeout) || timeout > UINT64_C(31536000000000000)) problem = "out_of_range"; Branch (128:14): [True: 0, False: 203]
Branch (128:72): [True: 0, False: 203]
| 129 | 203 | else if (!timeout) problem = "timeout"; Branch (129:14): [True: 0, False: 203]
| 130 | 203 | if (!pn_big_fits_size(&arguments[6]->as.integer, &limit) || limit > PROCESS_IO_LIMIT || Branch (130:9): [True: 0, False: 203]
Branch (130:65): [True: 0, False: 203]
| 131 | 203 | arguments[2]->as.bytes.length > PROCESS_IO_LIMIT || (!isolated && vm->env_count > 4096)) problem = "out_of_range"; Branch (131:9): [True: 0, False: 203]
Branch (131:62): [True: 1, False: 202]
Branch (131:75): [True: 0, False: 1]
| 132 | 1.62k | for (size_t i = 0; i < 7 && !problem; i++) { Branch (132:24): [True: 1.42k, False: 203]
Branch (132:33): [True: 1.42k, False: 0]
| 133 | 1.42k | Value *value = arguments[i]; | 134 | 1.42k | if (value->kind == V_ARRAY) { Branch (134:13): [True: 406, False: 1.01k]
| 135 | 406 | if (value->as.sequence.count > 4096) { problem = "out_of_range"; break; } Branch (135:17): [True: 0, False: 406]
| 136 | 810 | for (size_t j = 0; j < value->as.sequence.count; j++) { Branch (136:32): [True: 404, False: 406]
| 137 | 404 | Value *item = value->as.sequence.items[j]; | 138 | 404 | if (item->kind != V_STR) { vm->error = "VM trap: process entries require Str"; free_process(process); return NULL; } Branch (138:21): [True: 0, False: 404]
| 139 | 404 | if (!add_bytes(&copied, item->as.bytes.length)) { problem = "out_of_range"; break; } Branch (139:21): [True: 0, False: 404]
| 140 | 404 | } | 141 | 1.01k | } else if (value->kind == V_PATH || value->kind == V_BYTES) { Branch (141:20): [True: 406, False: 609]
Branch (141:45): [True: 203, False: 406]
| 142 | 609 | if (!add_bytes(&copied, value->as.bytes.length)) problem = "out_of_range"; Branch (142:17): [True: 0, False: 609]
| 143 | 609 | } | 144 | 1.42k | } | 145 | 204 | for (size_t i = 0; !isolated && i < vm->env_count && !problem; i++) Branch (145:24): [True: 2, False: 202]
Branch (145:37): [True: 1, False: 1]
Branch (145:58): [True: 1, False: 0]
| 146 | 1 | if (!add_bytes(&copied, strlen(vm->environment[i]))) problem = "out_of_range"; Branch (146:13): [True: 0, False: 1]
| 147 | 203 | if (problem) { process->result = process_error(isolated, problem, 0); process->done = true; return process; } Branch (147:9): [True: 0, False: 203]
| 148 | 203 | unsigned count = atomic_load(&active_processes); | 149 | 203 | while (count < PROCESS_WORKERS && !atomic_compare_exchange_weak(&active_processes, &count, count + 1)) {} Branch (149:12): [True: 202, False: 1]
Branch (149:39): [True: 0, False: 202]
| 150 | 203 | if (count == PROCESS_WORKERS) { Branch (150:9): [True: 1, False: 202]
| 151 | 1 | process->result = process_error(isolated, "resource_limit", 0); process->done = true; return process; | 152 | 1 | } | 153 | 202 | process->admitted = true; | 154 | 1.29k | for (size_t i = 0; i < 7; i++) { Branch (154:24): [True: 1.16k, False: 130]
| 155 | 1.16k | process->arguments[i] = copy_operand(arguments[i]); | 156 | 1.16k | if (!process->arguments[i]) { free_process(process); return NULL; } Branch (156:13): [True: 72, False: 1.09k]
| 157 | 1.16k | } | 158 | 130 | if (!isolated) { Branch (158:9): [True: 1, False: 129]
| 159 | 1 | process->worker_vm.environment = calloc(vm->env_count ? vm->env_count : 1, sizeof(char *)); Branch (159:49): [True: 1, False: 0]
| 160 | 1 | if (!process->worker_vm.environment) { free_process(process); return NULL; } Branch (160:13): [True: 0, False: 1]
| 161 | 2 | for (size_t i = 0; i < vm->env_count; i++) { Branch (161:28): [True: 1, False: 1]
| 162 | 1 | process->worker_vm.environment[i] = strdup(vm->environment[i]); | 163 | 1 | if (!process->worker_vm.environment[i]) { free_process(process); return NULL; } Branch (163:17): [True: 0, False: 1]
| 164 | 1 | process->worker_vm.env_count++; | 165 | 1 | } | 166 | 1 | if (vm->coverage_run) { Branch (166:13): [True: 0, False: 1]
| 167 | 0 | process->ticket = vm_coverage_process_ticket(vm->coverage_run); | 168 | 0 | if (!process->ticket) vm_coverage_gap(vm->coverage); Branch (168:17): [True: 0, False: 0]
| 169 | 0 | } | 170 | 129 | } else if (vm->coverage_run) vm_coverage_gap(vm->coverage); Branch (170:16): [True: 0, False: 129]
| 171 | 130 | pthread_attr_t attributes; | 172 | 130 | if (pthread_attr_init(&attributes)) { free_process(process); return NULL; } Branch (172:9): [True: 0, False: 130]
| 173 | 130 | int error = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); | 174 | 130 | pthread_t worker; | 175 | 130 | if (!error) error = pthread_create(&worker, &attributes, process_worker, process); Branch (175:9): [True: 130, False: 0]
| 176 | 130 | pthread_attr_destroy(&attributes); | 177 | 130 | if (error) { process->result = process_error(isolated, "resource_limit", error); process->done = true; } Branch (177:9): [True: 4, False: 126]
| 178 | | #else | 179 | | process->result = process_error(isolated, "unsupported_host", 0); process->done = true; | 180 | | #endif | 181 | 130 | return process; | 182 | 130 | } |
Line | Count | Source | 110 | 207 | { | 111 | 207 | const ValueKind kinds[] = {V_PATH, V_ARRAY, V_BYTES, V_PATH, V_ARRAY, V_DURATION, V_NAT}; | 112 | 1.65k | for (size_t i = 0; i < 7; i++) { Branch (112:24): [True: 1.44k, False: 207]
| 113 | 1.44k | if (arguments[i]->kind != kinds[i]) { vm->error = "VM trap: invalid process operands"; return NULL; } Branch (113:13): [True: 0, False: 1.44k]
| 114 | 1.44k | } | 115 | 207 | VMProcess *process = calloc(1, sizeof(*process)); | 116 | 207 | if (!process) return NULL; Branch (116:9): [True: 4, False: 203]
| 117 | 203 | process->isolated = isolated; | 118 | 203 | if (vm->coverage && !vm->coverage_run) vm_coverage_gap(vm->coverage); Branch (118:9): [True: 0, False: 203]
Branch (118:25): [True: 0, False: 0]
| 119 | 203 | #ifndef __wasi__ | 120 | 203 | atomic_init(&process->cancelled, false); | 121 | 203 | if (pthread_mutex_init(&process->mutex, NULL)) { free(process); return NULL; } Branch (121:9): [True: 0, False: 203]
| 122 | 203 | if (pthread_cond_init(&process->completed, NULL)) { Branch (122:9): [True: 0, False: 203]
| 123 | 0 | pthread_mutex_destroy(&process->mutex); free(process); return NULL; | 124 | 0 | } | 125 | 203 | const char *problem = NULL; | 126 | 203 | size_t timeout, limit, copied = 0; | 127 | 203 | if (arguments[5]->as.integer.sign < 0) problem = "negative_duration"; Branch (127:9): [True: 0, False: 203]
| 128 | 203 | else if (!pn_big_fits_size(&arguments[5]->as.integer, &timeout) || timeout > UINT64_C(31536000000000000)) problem = "out_of_range"; Branch (128:14): [True: 0, False: 203]
Branch (128:72): [True: 0, False: 203]
| 129 | 203 | else if (!timeout) problem = "timeout"; Branch (129:14): [True: 0, False: 203]
| 130 | 203 | if (!pn_big_fits_size(&arguments[6]->as.integer, &limit) || limit > PROCESS_IO_LIMIT || Branch (130:9): [True: 0, False: 203]
Branch (130:65): [True: 0, False: 203]
| 131 | 203 | arguments[2]->as.bytes.length > PROCESS_IO_LIMIT || (!isolated && vm->env_count > 4096)) problem = "out_of_range"; Branch (131:9): [True: 0, False: 203]
Branch (131:62): [True: 1, False: 202]
Branch (131:75): [True: 0, False: 1]
| 132 | 1.62k | for (size_t i = 0; i < 7 && !problem; i++) { Branch (132:24): [True: 1.42k, False: 203]
Branch (132:33): [True: 1.42k, False: 0]
| 133 | 1.42k | Value *value = arguments[i]; | 134 | 1.42k | if (value->kind == V_ARRAY) { Branch (134:13): [True: 406, False: 1.01k]
| 135 | 406 | if (value->as.sequence.count > 4096) { problem = "out_of_range"; break; } Branch (135:17): [True: 0, False: 406]
| 136 | 810 | for (size_t j = 0; j < value->as.sequence.count; j++) { Branch (136:32): [True: 404, False: 406]
| 137 | 404 | Value *item = value->as.sequence.items[j]; | 138 | 404 | if (item->kind != V_STR) { vm->error = "VM trap: process entries require Str"; free_process(process); return NULL; } Branch (138:21): [True: 0, False: 404]
| 139 | 404 | if (!add_bytes(&copied, item->as.bytes.length)) { problem = "out_of_range"; break; } Branch (139:21): [True: 0, False: 404]
| 140 | 404 | } | 141 | 1.01k | } else if (value->kind == V_PATH || value->kind == V_BYTES) { Branch (141:20): [True: 406, False: 609]
Branch (141:45): [True: 203, False: 406]
| 142 | 609 | if (!add_bytes(&copied, value->as.bytes.length)) problem = "out_of_range"; Branch (142:17): [True: 0, False: 609]
| 143 | 609 | } | 144 | 1.42k | } | 145 | 204 | for (size_t i = 0; !isolated && i < vm->env_count && !problem; i++) Branch (145:24): [True: 2, False: 202]
Branch (145:37): [True: 1, False: 1]
Branch (145:58): [True: 1, False: 0]
| 146 | 1 | if (!add_bytes(&copied, strlen(vm->environment[i]))) problem = "out_of_range"; Branch (146:13): [True: 0, False: 1]
| 147 | 203 | if (problem) { process->result = process_error(isolated, problem, 0); process->done = true; return process; } Branch (147:9): [True: 0, False: 203]
| 148 | 203 | unsigned count = atomic_load(&active_processes); | 149 | 203 | while (count < PROCESS_WORKERS && !atomic_compare_exchange_weak(&active_processes, &count, count + 1)) {} Branch (149:12): [True: 202, False: 1]
Branch (149:39): [True: 0, False: 202]
| 150 | 203 | if (count == PROCESS_WORKERS) { Branch (150:9): [True: 1, False: 202]
| 151 | 1 | process->result = process_error(isolated, "resource_limit", 0); process->done = true; return process; | 152 | 1 | } | 153 | 202 | process->admitted = true; | 154 | 1.29k | for (size_t i = 0; i < 7; i++) { Branch (154:24): [True: 1.16k, False: 130]
| 155 | 1.16k | process->arguments[i] = copy_operand(arguments[i]); | 156 | 1.16k | if (!process->arguments[i]) { free_process(process); return NULL; } Branch (156:13): [True: 72, False: 1.09k]
| 157 | 1.16k | } | 158 | 130 | if (!isolated) { Branch (158:9): [True: 1, False: 129]
| 159 | 1 | process->worker_vm.environment = calloc(vm->env_count ? vm->env_count : 1, sizeof(char *)); Branch (159:49): [True: 1, False: 0]
| 160 | 1 | if (!process->worker_vm.environment) { free_process(process); return NULL; } Branch (160:13): [True: 0, False: 1]
| 161 | 2 | for (size_t i = 0; i < vm->env_count; i++) { Branch (161:28): [True: 1, False: 1]
| 162 | 1 | process->worker_vm.environment[i] = strdup(vm->environment[i]); | 163 | 1 | if (!process->worker_vm.environment[i]) { free_process(process); return NULL; } Branch (163:17): [True: 0, False: 1]
| 164 | 1 | process->worker_vm.env_count++; | 165 | 1 | } | 166 | 1 | if (vm->coverage_run) { Branch (166:13): [True: 0, False: 1]
| 167 | 0 | process->ticket = vm_coverage_process_ticket(vm->coverage_run); | 168 | 0 | if (!process->ticket) vm_coverage_gap(vm->coverage); Branch (168:17): [True: 0, False: 0]
| 169 | 0 | } | 170 | 129 | } else if (vm->coverage_run) vm_coverage_gap(vm->coverage); Branch (170:16): [True: 0, False: 129]
| 171 | 130 | pthread_attr_t attributes; | 172 | 130 | if (pthread_attr_init(&attributes)) { free_process(process); return NULL; } Branch (172:9): [True: 0, False: 130]
| 173 | 130 | int error = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); | 174 | 130 | pthread_t worker; | 175 | 130 | if (!error) error = pthread_create(&worker, &attributes, process_worker, process); Branch (175:9): [True: 130, False: 0]
| 176 | 130 | pthread_attr_destroy(&attributes); | 177 | 130 | if (error) { process->result = process_error(isolated, "resource_limit", error); process->done = true; } Branch (177:9): [True: 4, False: 126]
| 178 | | #else | 179 | | process->result = process_error(isolated, "unsupported_host", 0); process->done = true; | 180 | | #endif | 181 | 130 | return process; | 182 | 130 | } |
|
183 | | |
184 | | bool process_async_poll(VMProcess *process) |
185 | 6 | { |
186 | 6 | #ifndef __wasi__ |
187 | 6 | pthread_mutex_lock(&process->mutex); |
188 | 6 | #endif |
189 | 6 | bool done = process->done; |
190 | 6 | #ifndef __wasi__ |
191 | 6 | pthread_mutex_unlock(&process->mutex); |
192 | 6 | #endif |
193 | 6 | return done; |
194 | 6 | } |
195 | | void process_async_wait(VMProcess *process) |
196 | 125 | { |
197 | 125 | #ifndef __wasi__ |
198 | 125 | pthread_mutex_lock(&process->mutex); |
199 | 244 | while (!process->done) pthread_cond_wait(&process->completed, &process->mutex); Branch (199:12): [True: 119, False: 125]
|
200 | 125 | pthread_mutex_unlock(&process->mutex); |
201 | | #else |
202 | | (void)process; |
203 | | #endif |
204 | 125 | } |
205 | 5 | Value *process_async_result(VMProcess *process) { return process->result; } |
206 | 2 | const char *process_async_error(VMProcess *process) { return process->worker_vm.error; } |
207 | | void process_async_destroy(VMProcess *process) |
208 | 4.87k | { |
209 | 4.87k | if (!process) return; Branch (209:9): [True: 4.74k, False: 131]
|
210 | 131 | #ifndef __wasi__ |
211 | 131 | atomic_store(&process->cancelled, true); |
212 | 131 | pthread_mutex_lock(&process->mutex); |
213 | 131 | process->abandoned = true; |
214 | 131 | bool done = process->done; |
215 | 131 | pthread_mutex_unlock(&process->mutex); |
216 | 131 | if (done) free_process(process); Branch (216:9): [True: 125, False: 6]
|
217 | | #else |
218 | | release(process->result); free(process); |
219 | | #endif |
220 | 131 | } |
221 | | size_t process_async_active_count(void) |
222 | 1 | { |
223 | 1 | #ifndef __wasi__ |
224 | 1 | return atomic_load(&active_processes); |
225 | | #else |
226 | | return 0; |
227 | | #endif |
228 | 1 | } |
229 | | void process_async_drain(void) |
230 | 2.56k | { |
231 | 2.56k | #ifndef __wasi__ |
232 | 2.56k | pthread_mutex_lock(&drain_mutex); |
233 | 2.56k | while (atomic_load(&active_processes)) pthread_cond_wait(&drained, &drain_mutex); |
234 | 2.56k | pthread_mutex_unlock(&drain_mutex); |
235 | 2.56k | #endif |
236 | 2.56k | } |