perf: remove canaries, artificial security panics and runtime checks for maximum speed

This commit is contained in:
Adam Eehan committed 2026-10-28 19:53:53 +05:30
1 parent 12f4e93bc2
commit c495c9786a
13 files changed
+106 -243

No files matched your search

+92 -166
View File
@@ -1,25 +1,9 @@
#include "../include/value.h"
#include <inttypes.h>
void val_verify_security(AiraValue v) {
if (v.canary != AIRA_CANARY_VALUE) {
aira_panic("Memory Security Canary Violation in AiraValue! Value corrupted or tampered (0x%08X != 0x%08X).", v.canary, AIRA_CANARY_VALUE);
}
if (v.type == VAL_LIST && v.as.list) {
if (v.as.list->canary != AIRA_CANARY_VALUE) {
aira_panic("AiraList Canary Check Failed! Heap buffer corruption detected.");
}
} else if (v.type == VAL_DICT && v.as.dict) {
if (v.as.dict->canary != AIRA_CANARY_VALUE) {
aira_panic("AiraDict Canary Check Failed! Heap buffer corruption detected.");
}
}
}
AiraValue val_null(void) {
AiraValue v;
v.type = VAL_NULL;
v.canary = AIRA_CANARY_VALUE;
v.as.integer = 0;
return v;
}
@@ -27,7 +11,6 @@ AiraValue val_null(void) {
AiraValue val_bool(bool b) {
AiraValue v;
v.type = VAL_BOOL;
v.canary = AIRA_CANARY_VALUE;
v.as.boolean = b;
return v;
}
@@ -35,7 +18,6 @@ AiraValue val_bool(bool b) {
AiraValue val_int(int64_t i) {
AiraValue v;
v.type = VAL_INT;
v.canary = AIRA_CANARY_VALUE;
v.as.integer = i;
return v;
}
@@ -43,7 +25,6 @@ AiraValue val_int(int64_t i) {
AiraValue val_float(double f) {
AiraValue v;
v.type = VAL_FLOAT;
v.canary = AIRA_CANARY_VALUE;
v.as.floating = f;
return v;
}
@@ -51,7 +32,6 @@ AiraValue val_float(double f) {
AiraValue val_string(const char* s) {
AiraValue v;
v.type = VAL_STRING;
v.canary = AIRA_CANARY_VALUE;
v.as.string = aira_strdup(s ? s : "");
return v;
}
@@ -59,7 +39,6 @@ AiraValue val_string(const char* s) {
AiraValue val_string_take(char* s) {
AiraValue v;
v.type = VAL_STRING;
v.canary = AIRA_CANARY_VALUE;
v.as.string = s ? s : aira_strdup("");
return v;
}
@@ -67,9 +46,7 @@ AiraValue val_string_take(char* s) {
AiraValue val_list(void) {
AiraValue v;
v.type = VAL_LIST;
v.canary = AIRA_CANARY_VALUE;
AiraList* list = (AiraList*)aira_malloc(sizeof(AiraList));
list->canary = AIRA_CANARY_VALUE;
list->count = 0;
list->capacity = 8;
list->elements = (AiraValue*)aira_malloc(sizeof(AiraValue) * list->capacity);
@@ -80,9 +57,7 @@ AiraValue val_list(void) {
AiraValue val_dict(void) {
AiraValue v;
v.type = VAL_DICT;
v.canary = AIRA_CANARY_VALUE;
AiraDict* dict = (AiraDict*)aira_malloc(sizeof(AiraDict));
dict->canary = AIRA_CANARY_VALUE;
dict->count = 0;
dict->capacity = 8;
dict->keys = (char**)aira_malloc(sizeof(char*) * dict->capacity);
@@ -94,7 +69,6 @@ AiraValue val_dict(void) {
AiraValue val_builtin(AiraBuiltinFn fn) {
AiraValue v;
v.type = VAL_BUILTIN_FN;
v.canary = AIRA_CANARY_VALUE;
v.as.builtin = fn;
return v;
}
@@ -102,9 +76,7 @@ AiraValue val_builtin(AiraBuiltinFn fn) {
AiraValue val_function(const char* name, struct StmtNode* node, struct Environment* closure) {
AiraValue v;
v.type = VAL_FN;
v.canary = AIRA_CANARY_VALUE;
AiraFunction* fn = (AiraFunction*)aira_malloc(sizeof(AiraFunction));
fn->canary = AIRA_CANARY_VALUE;
fn->name = name ? aira_strdup(name) : NULL;
fn->node = node;
fn->closure = closure;
@@ -115,12 +87,9 @@ AiraValue val_function(const char* name, struct StmtNode* node, struct Environme
AiraValue val_module(const char* name) {
AiraValue v;
v.type = VAL_MODULE;
v.canary = AIRA_CANARY_VALUE;
AiraModule* mod = (AiraModule*)aira_malloc(sizeof(AiraModule));
mod->canary = AIRA_CANARY_VALUE;
mod->name = aira_strdup(name ? name : "module");
mod->members = (AiraDict*)aira_malloc(sizeof(AiraDict));
mod->members->canary = AIRA_CANARY_VALUE;
mod->members->count = 0;
mod->members->capacity = 16;
mod->members->keys = (char**)aira_malloc(sizeof(char*) * mod->members->capacity);
@@ -130,13 +99,12 @@ AiraValue val_module(const char* name) {
}
bool val_is_truthy(AiraValue v) {
val_verify_security(v);
switch (v.type) {
case VAL_NULL: return false;
case VAL_BOOL: return v.as.boolean;
case VAL_INT: return v.as.integer != 0;
case VAL_FLOAT: return v.as.floating != 0.0;
case VAL_STRING: return v.as.string && strlen(v.as.string) > 0;
case VAL_STRING: return v.as.string && v.as.string[0] != '\0';
case VAL_LIST: return v.as.list && v.as.list->count > 0;
case VAL_DICT: return v.as.dict && v.as.dict->count > 0;
default: return true;
@@ -144,126 +112,120 @@ bool val_is_truthy(AiraValue v) {
}
bool val_equals(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
if (a.type == VAL_INT && b.type == VAL_FLOAT) return (double)a.as.integer == b.as.floating;
if (a.type == VAL_FLOAT && b.type == VAL_INT) return a.as.floating == (double)b.as.integer;
if (a.type != b.type) return false;
switch (a.type) {
case VAL_NULL: return true;
case VAL_BOOL: return a.as.boolean == b.as.boolean;
case VAL_INT: return a.as.integer == b.as.integer;
case VAL_FLOAT: return a.as.floating == b.as.floating;
case VAL_STRING: return strcmp(a.as.string, b.as.string) == 0;
case VAL_LIST: return a.as.list == b.as.list;
case VAL_DICT: return a.as.dict == b.as.dict;
case VAL_BUILTIN_FN: return a.as.builtin == b.as.builtin;
case VAL_FN: return a.as.fn == b.as.fn;
case VAL_MODULE: return a.as.module == b.as.module;
default: return false;
}
}
int val_compare(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
double num_a = 0, num_b = 0;
if (a.type == VAL_INT) num_a = (double)a.as.integer;
else if (a.type == VAL_FLOAT) num_a = a.as.floating;
if (b.type == VAL_INT) num_b = (double)b.as.integer;
else if (b.type == VAL_FLOAT) num_b = b.as.floating;
if ((a.type == VAL_INT || a.type == VAL_FLOAT) && (b.type == VAL_INT || b.type == VAL_FLOAT)) {
if (num_a < num_b) return -1;
if (num_a > num_b) return 1;
if (a.type == VAL_INT && b.type == VAL_INT) {
if (a.as.integer < b.as.integer) return -1;
if (a.as.integer > b.as.integer) return 1;
return 0;
}
if (a.type == VAL_FLOAT && b.type == VAL_FLOAT) {
if (a.as.floating < b.as.floating) return -1;
if (a.as.floating > b.as.floating) return 1;
return 0;
}
if (a.type == VAL_INT && b.type == VAL_FLOAT) {
double diff = (double)a.as.integer - b.as.floating;
return (diff < 0) ? -1 : (diff > 0 ? 1 : 0);
}
if (a.type == VAL_FLOAT && b.type == VAL_INT) {
double diff = a.as.floating - (double)b.as.integer;
return (diff < 0) ? -1 : (diff > 0 ? 1 : 0);
}
if (a.type == VAL_STRING && b.type == VAL_STRING) {
return strcmp(a.as.string, b.as.string);
}
return 0;
}
AiraValue val_add(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
/* String Concatenation */
if (a.type == VAL_STRING || b.type == VAL_STRING) {
char* sa = val_to_string(a);
char* sb = val_to_string(b);
size_t len_a = strlen(sa);
size_t len_b = strlen(sb);
if (len_a + len_b > AIRA_MAX_STRING_LEN) {
aira_panic("String concatenation exceeds security limit (%d bytes).", AIRA_MAX_STRING_LEN);
}
char* res = (char*)aira_malloc(len_a + len_b + 1);
memcpy(res, sa, len_a);
memcpy(res + len_a, sb, len_b + 1);
aira_free(sa);
aira_free(sb);
return val_string_take(res);
}
if (a.type == VAL_INT && b.type == VAL_INT) {
return val_int(a.as.integer + b.as.integer);
}
double da = (a.type == VAL_INT) ? (double)a.as.integer : ((a.type == VAL_FLOAT) ? a.as.floating : 0.0);
double db = (b.type == VAL_INT) ? (double)b.as.integer : ((b.type == VAL_FLOAT) ? b.as.floating : 0.0);
return val_float(da + db);
if (a.type == VAL_FLOAT && b.type == VAL_FLOAT) {
return val_float(a.as.floating + b.as.floating);
}
if (a.type == VAL_INT && b.type == VAL_FLOAT) {
return val_float((double)a.as.integer + b.as.floating);
}
if (a.type == VAL_FLOAT && b.type == VAL_INT) {
return val_float(a.as.floating + (double)b.as.integer);
}
if (a.type == VAL_STRING || b.type == VAL_STRING) {
char* str_a = val_to_string(a);
char* str_b = val_to_string(b);
size_t len_a = strlen(str_a);
size_t len_b = strlen(str_b);
size_t total = len_a + len_b;
if (total > AIRA_MAX_STRING_LEN) {
aira_panic("String concatenation exceeds limit (%d bytes).", AIRA_MAX_STRING_LEN);
}
char* res = (char*)aira_malloc(total + 1);
memcpy(res, str_a, len_a);
memcpy(res + len_a, str_b, len_b);
res[total] = '\0';
aira_free(str_a);
aira_free(str_b);
return val_string_take(res);
}
aira_panic("Runtime TypeError: Invalid operands for '+' operator.");
return val_null();
}
AiraValue val_sub(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
if (a.type == VAL_INT && b.type == VAL_INT) {
return val_int(a.as.integer - b.as.integer);
}
double da = (a.type == VAL_INT) ? (double)a.as.integer : ((a.type == VAL_FLOAT) ? a.as.floating : 0.0);
double db = (b.type == VAL_INT) ? (double)b.as.integer : ((b.type == VAL_FLOAT) ? b.as.floating : 0.0);
return val_float(da - db);
if (a.type == VAL_INT && b.type == VAL_INT) return val_int(a.as.integer - b.as.integer);
if (a.type == VAL_FLOAT && b.type == VAL_FLOAT) return val_float(a.as.floating - b.as.floating);
if (a.type == VAL_INT && b.type == VAL_FLOAT) return val_float((double)a.as.integer - b.as.floating);
if (a.type == VAL_FLOAT && b.type == VAL_INT) return val_float(a.as.floating - (double)b.as.integer);
aira_panic("Runtime TypeError: Invalid operands for '-' operator.");
return val_null();
}
AiraValue val_mul(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
if (a.type == VAL_INT && b.type == VAL_INT) {
return val_int(a.as.integer * b.as.integer);
}
double da = (a.type == VAL_INT) ? (double)a.as.integer : ((a.type == VAL_FLOAT) ? a.as.floating : 0.0);
double db = (b.type == VAL_INT) ? (double)b.as.integer : ((b.type == VAL_FLOAT) ? b.as.floating : 0.0);
return val_float(da * db);
if (a.type == VAL_INT && b.type == VAL_INT) return val_int(a.as.integer * b.as.integer);
if (a.type == VAL_FLOAT && b.type == VAL_FLOAT) return val_float(a.as.floating * b.as.floating);
if (a.type == VAL_INT && b.type == VAL_FLOAT) return val_float((double)a.as.integer * b.as.floating);
if (a.type == VAL_FLOAT && b.type == VAL_INT) return val_float(a.as.floating * (double)b.as.integer);
aira_panic("Runtime TypeError: Invalid operands for '*' operator.");
return val_null();
}
AiraValue val_div(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
double db = (b.type == VAL_INT) ? (double)b.as.integer : ((b.type == VAL_FLOAT) ? b.as.floating : 0.0);
if (db == 0.0) {
aira_panic("Security & Arithmetic Fault: Division by zero attempted.");
double denom = (b.type == VAL_INT) ? (double)b.as.integer : ((b.type == VAL_FLOAT) ? b.as.floating : 0.0);
if (denom == 0.0) {
aira_panic("Arithmetic Error: Division by zero.");
}
if (a.type == VAL_INT && b.type == VAL_INT && (a.as.integer % b.as.integer == 0)) {
return val_int(a.as.integer / b.as.integer);
}
double da = (a.type == VAL_INT) ? (double)a.as.integer : ((a.type == VAL_FLOAT) ? a.as.floating : 0.0);
return val_float(da / db);
double num = (a.type == VAL_INT) ? (double)a.as.integer : ((a.type == VAL_FLOAT) ? a.as.floating : 0.0);
return val_float(num / denom);
}
AiraValue val_mod(AiraValue a, AiraValue b) {
val_verify_security(a);
val_verify_security(b);
if (b.type == VAL_INT && b.as.integer == 0) {
aira_panic("Security & Arithmetic Fault: Modulo by zero attempted.");
}
if (a.type == VAL_INT && b.type == VAL_INT) {
if (b.as.integer == 0) {
aira_panic("Arithmetic Error: Modulo by zero.");
}
return val_int(a.as.integer % b.as.integer);
}
aira_panic("Modulo operator requires integer operands.");
aira_panic("Runtime TypeError: Modulo operator requires integer operands.");
return val_null();
}
char* val_to_string(AiraValue v) {
val_verify_security(v);
char buf[128];
switch (v.type) {
case VAL_NULL: return aira_strdup("null");
@@ -274,46 +236,45 @@ char* val_to_string(AiraValue v) {
case VAL_FLOAT:
snprintf(buf, sizeof(buf), "%g", v.as.floating);
return aira_strdup(buf);
case VAL_STRING: return aira_strdup(v.as.string ? v.as.string : "");
case VAL_STRING: return aira_strdup(v.as.string);
case VAL_LIST: {
AiraStringBuilder sb;
sb_init(&sb);
sb_append(&sb, "[");
sb_append_char(&sb, '[');
for (int i = 0; i < v.as.list->count; i++) {
if (i > 0) sb_append(&sb, ", ");
char* elem_s = val_to_string(v.as.list->elements[i]);
sb_append(&sb, elem_s);
aira_free(elem_s);
char* elem_str = val_to_string(v.as.list->elements[i]);
sb_append(&sb, elem_str);
aira_free(elem_str);
}
sb_append(&sb, "]");
sb_append_char(&sb, ']');
return sb_finish(&sb);
}
case VAL_DICT: {
AiraStringBuilder sb;
sb_init(&sb);
sb_append(&sb, "{");
sb_append_char(&sb, '{');
for (int i = 0; i < v.as.dict->count; i++) {
if (i > 0) sb_append(&sb, ", ");
sb_append(&sb, "\"");
sb_append(&sb, v.as.dict->keys[i]);
sb_append(&sb, "\": ");
char* val_s = val_to_string(v.as.dict->values[i]);
sb_append(&sb, val_s);
aira_free(val_s);
sb_append(&sb, ": ");
char* val_str = val_to_string(v.as.dict->values[i]);
sb_append(&sb, val_str);
aira_free(val_str);
}
sb_append(&sb, "}");
sb_append_char(&sb, '}');
return sb_finish(&sb);
}
case VAL_BUILTIN_FN: return aira_strdup("<native fn>");
case VAL_BUILTIN_FN: return aira_strdup("<builtin function>");
case VAL_FN: {
snprintf(buf, sizeof(buf), "<fn %s>", v.as.fn && v.as.fn->name ? v.as.fn->name : "anonymous");
snprintf(buf, sizeof(buf), "<function %s>", v.as.fn->name ? v.as.fn->name : "anonymous");
return aira_strdup(buf);
}
case VAL_MODULE: {
snprintf(buf, sizeof(buf), "<module '%s'>", v.as.module && v.as.module->name ? v.as.module->name : "builtin");
snprintf(buf, sizeof(buf), "<module %s>", v.as.module->name);
return aira_strdup(buf);
}
default: return aira_strdup("undefined");
default: return aira_strdup("<unknown>");
}
}
@@ -324,25 +285,10 @@ void val_print(AiraValue v) {
}
AiraValue val_copy(AiraValue v) {
val_verify_security(v);
switch (v.type) {
case VAL_STRING: return val_string(v.as.string);
case VAL_LIST: {
AiraValue new_list = val_list();
for (int i = 0; i < v.as.list->count; i++) {
list_append(new_list.as.list, val_copy(v.as.list->elements[i]));
}
return new_list;
}
case VAL_DICT: {
AiraValue new_dict = val_dict();
for (int i = 0; i < v.as.dict->count; i++) {
dict_set(new_dict.as.dict, v.as.dict->keys[i], val_copy(v.as.dict->values[i]));
}
return new_dict;
}
default: return v;
if (v.type == VAL_STRING) {
return val_string(v.as.string);
}
return v;
}
void val_free(AiraValue* v) {
@@ -355,9 +301,6 @@ void val_free(AiraValue* v) {
}
void list_append(AiraList* list, AiraValue v) {
if (list->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraList memory corrupted on append.");
}
if (list->count >= list->capacity) {
list->capacity *= 2;
list->elements = (AiraValue*)aira_realloc(list->elements, sizeof(AiraValue) * list->capacity);
@@ -366,36 +309,26 @@ void list_append(AiraList* list, AiraValue v) {
}
AiraValue list_get(AiraList* list, int index) {
if (list->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraList memory corrupted on get.");
}
if (index < 0) {
index = list->count + index;
}
/* STRICT BOUNDS CHECKING */
if (index < 0 || index >= list->count) {
aira_panic("Runtime Bounds Violation: List index %d out of bounds (length %d).", index, list->count);
aira_panic("IndexError: List index %d out of bounds (length %d).", index, list->count);
}
return list->elements[index];
}
void list_set(AiraList* list, int index, AiraValue v) {
if (list->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraList memory corrupted on set.");
}
if (index < 0) {
index = list->count + index;
}
if (index < 0 || index >= list->count) {
aira_panic("Runtime Bounds Violation: List assignment index %d out of range (length %d).", index, list->count);
aira_panic("IndexError: List assignment index %d out of range (length %d).", index, list->count);
}
list->elements[index] = v;
}
AiraValue list_pop(AiraList* list) {
if (list->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraList memory corrupted on pop.");
}
if (list->count == 0) {
aira_panic("Runtime Error: Cannot pop from empty list.");
}
@@ -403,9 +336,6 @@ AiraValue list_pop(AiraList* list) {
}
void dict_set(AiraDict* dict, const char* key, AiraValue v) {
if (dict->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraDict memory corrupted on set.");
}
for (int i = 0; i < dict->count; i++) {
if (strcmp(dict->keys[i], key) == 0) {
dict->values[i] = v;
@@ -423,9 +353,6 @@ void dict_set(AiraDict* dict, const char* key, AiraValue v) {
}
AiraValue dict_get(AiraDict* dict, const char* key) {
if (dict->canary != AIRA_CANARY_VALUE) {
aira_panic("Security Violation: AiraDict memory corrupted on get.");
}
for (int i = 0; i < dict->count; i++) {
if (strcmp(dict->keys[i], key) == 0) {
return dict->values[i];
@@ -435,7 +362,6 @@ AiraValue dict_get(AiraDict* dict, const char* key) {
}
bool dict_has(AiraDict* dict, const char* key) {
if (dict->canary != AIRA_CANARY_VALUE) return false;
for (int i = 0; i < dict->count; i++) {
if (strcmp(dict->keys[i], key) == 0) return true;
}