File size: 9,857 Bytes
6afa130 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | /*
* sov_obligations.c -- Obligation Generator Implementation
* FORGE Phase 2 (stub for compilation)
*/
#include "sov_obligations.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
ObligationSet *sov_obset_new(void)
{
ObligationSet *obset = (ObligationSet *)malloc(sizeof(ObligationSet));
if (obset) {
memset(obset, 0, sizeof(ObligationSet));
obset->capacity = 10;
obset->items = (Obligation *)malloc(10 * sizeof(Obligation));
}
return obset;
}
void sov_obset_free(ObligationSet *obset)
{
if (obset) {
if (obset->items) {
for (size_t i = 0; i < obset->count; i++) {
free(obset->items[i].description);
}
free(obset->items);
}
free(obset);
}
}
int32_t sov_obset_add_inv(ObligationSet *obset,
const int64_t *A __attribute__((unused)),
size_t n __attribute__((unused)),
uint32_t pc_start,
uint32_t pc_end)
{
if (!obset) return -1;
/* Expand capacity if needed */
if (obset->count >= obset->capacity) {
size_t new_cap = obset->capacity * 2;
Obligation *new_items = (Obligation *)realloc(obset->items, new_cap * sizeof(Obligation));
if (!new_items) return -1;
obset->items = new_items;
obset->capacity = new_cap;
}
/* Initialize new obligation */
Obligation *obl = &obset->items[obset->count];
memset(obl, 0, sizeof(Obligation));
obl->id = obset->next_id;
obl->kind = OBL_KIND_INV;
obl->start_pc = pc_start;
obl->end_pc = pc_end;
obl->description = (char *)malloc(64);
if (obl->description) {
snprintf(obl->description, 64, "Invariant at PC %u", pc_start);
obl->desc_len = strlen(obl->description);
}
obset->count++;
return (int32_t)obset->next_id++;
}
int32_t sov_obset_add_solve(ObligationSet *obset,
const int64_t *A,
const int64_t *b,
size_t m,
size_t n,
uint32_t pc_start,
uint32_t pc_end)
{
if (!obset || !A || !b) return -1;
if (obset->count >= obset->capacity) {
size_t new_cap = obset->capacity * 2;
Obligation *new_items = (Obligation *)realloc(obset->items, new_cap * sizeof(Obligation));
if (!new_items) return -1;
obset->items = new_items;
obset->capacity = new_cap;
}
Obligation *obl = &obset->items[obset->count];
memset(obl, 0, sizeof(Obligation));
obl->id = obset->next_id;
obl->kind = OBL_KIND_SOLVE;
obl->start_pc = pc_start;
obl->end_pc = pc_end;
obl->description = (char *)malloc(128);
if (obl->description) {
snprintf(obl->description, 128, "SOLVE_OK: verify A*x=b for %zu x %zu system at PC %u",
m, n, pc_start);
obl->desc_len = strlen(obl->description);
}
obl->params = (void **)malloc(2 * sizeof(void *));
if (obl->params) {
obl->params[0] = (void *)(intptr_t)m;
obl->params[1] = (void *)(intptr_t)n;
obl->num_params = 2;
}
obset->count++;
return (int32_t)obset->next_id++;
}
int32_t sov_obset_add_lstsq(ObligationSet *obset,
const int64_t *A,
const int64_t *b,
size_t m,
size_t n,
uint32_t pc_start,
uint32_t pc_end)
{
if (!obset || !A || !b) return -1;
if (obset->count >= obset->capacity) {
size_t new_cap = obset->capacity * 2;
Obligation *new_items = (Obligation *)realloc(obset->items, new_cap * sizeof(Obligation));
if (!new_items) return -1;
obset->items = new_items;
obset->capacity = new_cap;
}
Obligation *obl = &obset->items[obset->count];
memset(obl, 0, sizeof(Obligation));
obl->id = obset->next_id;
obl->kind = OBL_KIND_LSTSQ;
obl->start_pc = pc_start;
obl->end_pc = pc_end;
obl->description = (char *)malloc(128);
if (obl->description) {
snprintf(obl->description, 128, "LSTSQ_OK: verify A^T(Ax-b)=0 for %zu x %zu system at PC %u",
m, n, pc_start);
obl->desc_len = strlen(obl->description);
}
obl->params = (void **)malloc(2 * sizeof(void *));
if (obl->params) {
obl->params[0] = (void *)(intptr_t)m;
obl->params[1] = (void *)(intptr_t)n;
obl->num_params = 2;
}
obset->count++;
return (int32_t)obset->next_id++;
}
int32_t sov_obset_add_type(ObligationSet *obset,
const char *description,
uint32_t pc_start,
uint32_t pc_end)
{
if (!obset || !description) return -1;
/* Expand capacity if needed */
if (obset->count >= obset->capacity) {
size_t new_cap = obset->capacity * 2;
Obligation *new_items = (Obligation *)realloc(obset->items, new_cap * sizeof(Obligation));
if (!new_items) return -1;
obset->items = new_items;
obset->capacity = new_cap;
}
/* Initialize new obligation */
Obligation *obl = &obset->items[obset->count];
memset(obl, 0, sizeof(Obligation));
obl->id = obset->next_id;
obl->kind = OBL_KIND_TYPE;
obl->start_pc = pc_start;
obl->end_pc = pc_end;
obl->description = (char *)malloc(256);
if (obl->description) {
snprintf(obl->description, 256, "%s", description);
obl->desc_len = strlen(obl->description);
}
obset->count++;
return (int32_t)obset->next_id++;
}
int32_t sov_obset_add_prop(ObligationSet *obset,
const char *property,
uint32_t pc_start,
uint32_t pc_end)
{
if (!obset || !property) return -1;
if (obset->count >= obset->capacity) {
size_t new_cap = obset->capacity * 2;
Obligation *new_items = (Obligation *)realloc(obset->items, new_cap * sizeof(Obligation));
if (!new_items) return -1;
obset->items = new_items;
obset->capacity = new_cap;
}
Obligation *obl = &obset->items[obset->count];
memset(obl, 0, sizeof(Obligation));
obl->id = obset->next_id;
obl->kind = OBL_KIND_PROP;
obl->start_pc = pc_start;
obl->end_pc = pc_end;
obl->description = (char *)malloc(256);
if (obl->description) {
snprintf(obl->description, 256, "Property: %s", property);
obl->desc_len = strlen(obl->description);
}
obset->count++;
return (int32_t)obset->next_id++;
}
Obligation *sov_obset_get(ObligationSet *obset, uint32_t id)
{
if (!obset) return NULL;
for (size_t i = 0; i < obset->count; i++) {
if (obset->items[i].id == id)
return &obset->items[i];
}
return NULL;
}
Obligation *sov_obset_at(ObligationSet *obset, size_t index)
{
if (!obset || index >= obset->count) return NULL;
return &obset->items[index];
}
int sov_obset_set_witness(ObligationSet *obset,
uint32_t id,
const void *witness,
size_t witness_len)
{
if (!obset) return -1;
Obligation *obl = sov_obset_get(obset, id);
if (!obl) return -1;
obl->params = (void **)realloc(obl->params, (obl->num_params + 2) * sizeof(void *));
if (!obl->params) return -1;
obl->params[obl->num_params] = (void *)witness;
obl->params[obl->num_params + 1] = (void *)(intptr_t)witness_len;
obl->num_params += 2;
return 0;
}
const void *sov_obset_get_witness(ObligationSet *obset,
uint32_t id,
size_t *out_len)
{
if (!obset || !out_len) return NULL;
Obligation *obl = sov_obset_get(obset, id);
if (!obl || obl->num_params < 2) {
*out_len = 0;
return NULL;
}
*out_len = (size_t)(intptr_t)obl->params[obl->num_params - 1];
return obl->params[obl->num_params - 2];
}
int sov_obset_to_json(ObligationSet *obset,
uint8_t **out_json,
size_t *out_len)
{
if (!obset || !out_json) return -1;
size_t buf_size = 4096 + obset->count * 256;
uint8_t *buf = (uint8_t *)malloc(buf_size);
if (!buf) return -1;
size_t pos = 0;
pos += snprintf((char *)buf + pos, buf_size - pos, "{"version":1,"obligations":[");
for (size_t i = 0; i < obset->count; i++) {
Obligation *obl = &obset->items[i];
if (i > 0) pos += snprintf((char *)buf + pos, buf_size - pos, ",");
pos += snprintf((char *)buf + pos, buf_size - pos,
"{"id":%u,"kind":%d,"start_pc":%u,"end_pc":%u,"description":"",
obl->id, (int)obl->kind, obl->start_pc, obl->end_pc);
for (size_t j = 0; j < obl->desc_len && pos < buf_size - 1; j++) {
uint8_t c = (uint8_t)obl->description[j];
if (c == '"' || c == '\') {
buf[pos++] = '\';
}
buf[pos++] = c;
}
pos += snprintf((char *)buf + pos, buf_size - pos, ""}");
}
pos += snprintf((char *)buf + pos, buf_size - pos, "]}");
*out_json = buf;
*out_len = pos;
return 0;
}
ObligationSet *sov_obset_from_json(const uint8_t *json_bytes,
size_t len)
{
return NULL;
}
|