Skip to content

Commit

Permalink
add metadata adding functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dragoncoder047 authored Jan 25, 2024
1 parent 10b427f commit b8b96db
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 60 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# need to set up AFL once parser is written https://medium.com/@ayushpriya10/fuzzing-applications-with-american-fuzzy-lop-afl-54facc65d102

.PHONY: test64 builtest64 valgrind64 clean test32 buildtest32 valgrind32 deps show checkleaks

test: buildtest64 valgrind64 buildtest32 valgrind32 clean checkleaks
Expand Down
53 changes: 32 additions & 21 deletions pickle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,14 @@ bool needs_escape(char c) {

static void init_metadata(object* self, va_list args) {
self->cells = new cell[4];
self->cells[0].as_ptr = va_arg(args, object*);
self->cells[1].as_obj = va_arg(args, object*);
self->cells[2].as_obj = va_arg(args, object*);
self->cells[3].as_obj = va_arg(args, object*);
self->cells[0].as_obj = va_arg(args, object*); // line
self->cells[1].as_obj = va_arg(args, object*); // column
self->cells[2].as_obj = va_arg(args, object*); // file
self->cells[3].as_obj = va_arg(args, object*); // prototypes list
}

static void mark_metadata(object* self) {
self->cells[0].as_obj->mark();
self->cells[1].as_obj->mark();
self->cells[2].as_obj->mark();
self->cells[3].as_obj->mark();
static void mark4(object* self) {
for (int i = 0; i < 3; i++) self->cells[i].as_obj->mark();
}

static void init_c_function(object* self, va_list args) {
Expand Down Expand Up @@ -106,19 +103,33 @@ static void init_error(object* self, va_list args) {
self->cells[2].as_obj = va_arg(args, object*);
}

static void mark_error(object* self) {
for (int i = 0; i < 3; i++) self->cells[i].as_obj->mark();
static void init_int(object* self, va_list args) {
self->as_big_int = va_arg(args, int64_t);
}

static int cmp_int(object* a, object* b) {
return a->as_big_int - b->as_big_int;
}

static void init_float(object* self, va_list args) {
self->as_double = va_arg(args, double);
}

static int cmp_float(object* a, object* b) {
return (int)(a->as_double - b->as_double);
}

const object_schema metadata_type("object_metadata", init_metadata, NULL, mark_metadata, tinobsy::schema_functions::finalize_cons);
const object_schema cons_type("cons", tinobsy::schema_functions::init_cons, cmp_c_function, tinobsy::schema_functions::mark_cons, tinobsy::schema_functions::finalize_cons);
const object_schema metadata_type("object_metadata", init_metadata, NULL, mark4, tinobsy::schema_functions::finalize_cons);
const object_schema cons_type("cons", tinobsy::schema_functions::init_cons, NULL, tinobsy::schema_functions::mark_cons, tinobsy::schema_functions::finalize_cons);
const object_schema partial_type("function_partial", init_function_partial, NULL, NULL, tinobsy::schema_functions::finalize_cons);
const object_schema c_function_type("c_function", init_c_function, NULL, NULL, NULL);
const object_schema string_type("string", init_string, cmp_string, mark_string, del_string);
const object_schema symbol_type("symbol", tinobsy::schema_functions::init_str, tinobsy::schema_functions::cmp_str, NULL, tinobsy::schema_functions::finalize_str);
const object_schema error_type("error", init_error, NULL, mark_error, tinobsy::schema_functions::finalize_cons);
const object_schema c_function_type("c_function", init_c_function, cmp_c_function, NULL, NULL);
const object_schema error_type("error", init_error, NULL, mark4, tinobsy::schema_functions::finalize_cons);
const object_schema integer_type("int", init_int, cmp_int, NULL, NULL);
const object_schema float_type("float", init_float, cmp_float, NULL, NULL);

object* pickle::cons_list(size_t len, ...) {
object* pickle::list(size_t len, ...) {
va_list args;
va_start(args, len);
object* head;
Expand Down Expand Up @@ -195,7 +206,7 @@ void pickle::run_next_thunk() {
DBG("Data function");
object* current_cont = this->make_partial(
this->wrap_func(funcs::eval),
this->cons_list(1, func), // args is ignored because they should already be added to env
this->list(1, func), // args is ignored because they should already be added to env
thunk->cells[2].as_obj,
thunk->cells[3].as_obj,
thunk->cells[4].as_obj);
Expand All @@ -219,9 +230,9 @@ void funcs::parse(pickle* runner, object* args, object* env, object* cont, objec
else goto success;
}
TODO;
// result = runner->make_error(runner->wrap_symbol("SyntaxError"), runner->cons_list(1, result), cont)
// result = runner->wrap_error(runner->wrap_symbol("SyntaxError"), runner->list(1, result), cont)
success:
runner->set_retval(runner->cons_list(1, result), env, cont, fail_cont);
runner->set_retval(runner->list(1, result), env, cont, fail_cont);
s->cells[1].as_obj = result; // Save parse for later if constantly reparsing string (i.e. a loop)
return;
failure:
Expand All @@ -248,7 +259,7 @@ void funcs::eval(pickle* runner, object* args, object* env, object* cont, object
env,
runner->make_partial(
runner->wrap_func(funcs::splice_match),
runner->cons_list(2, runner->append(ast, NULL), NULL/*matched_pattern->match_info()*/),
runner->list(2, runner->append(ast, NULL), NULL/*matched_pattern->match_info()*/),
oldenv,
runner->make_partial(
runner->wrap_func(funcs::eval),
Expand All @@ -263,7 +274,7 @@ void funcs::eval(pickle* runner, object* args, object* env, object* cont, object
));
} else {
// No matches so return unchanged
runner->set_retval(runner->cons_list(1, ast), env, cont, fail_cont);
runner->set_retval(runner->list(1, ast), env, cont, fail_cont);
}
}

Expand Down
30 changes: 28 additions & 2 deletions pickle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ extern const object_schema string_type;
extern const object_schema symbol_type;
extern const object_schema stream_type;
extern const object_schema error_type;
extern const object_schema integer_type;
extern const object_schema float_type;

class pickle : public tinobsy::vm {
public:
object* queue_head = NULL;
object* queue_tail = NULL;
object* cons_list(size_t len, ...);
object* globals = NULL;
object* list(size_t len, ...);
object* append(object* l1, object* l2);
void set_retval(object* args, object* env, object* cont, object* fail_cont);
void set_failure(object* err, object* env, object* cont, object* fail_cont);
Expand All @@ -56,9 +59,32 @@ class pickle : public tinobsy::vm {
inline object* cons(object* car, object* cdr) {
return this->allocate(&cons_type, car, cdr);
}
inline object* make_error(object* type, object* details, object* continuation) {
inline object* wrap_error(object* type, object* details, object* continuation) {
return this->allocate(&error_type, type, details, continuation);
}
inline object* wrap_integer(int64_t x) {
return this->allocate(&integer_type, x);
}
inline object* wrap_float(double x) {
return this->allocate(&float_type, x);
}
inline object* wrap_metadata(object* line, object* col, object* file, object* prototypes) {
return this->allocate(&metadata_type, line, col, file, prototypes);
}
inline object* wrap_metadata(int64_t line, int64_t col, const char* file, object* prototypes) {
return this->allocate(&metadata_type, this->wrap_integer(line), this->wrap_integer(col), this->wrap_string(file), prototypes);
}
inline object* with_metadata(object* x, int64_t line, int64_t col, const char* file, object* prototypes) {
if (x->meta) {
x->meta->cells[0].as_obj = this->wrap_integer(line);
x->meta->cells[1].as_obj = this->wrap_integer(col);
x->meta->cells[2].as_obj = this->wrap_string(file);
x->meta->cells[2].as_obj = prototypes;
} else {
x->meta = this->wrap_metadata(line, col, file, prototypes);
}
return x;
}
private:
void mark_globals();
};
Expand Down
3 changes: 2 additions & 1 deletion pickle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ void start_catch_segfault() {
int main() {
start_catch_segfault();
auto vm = new pickle::pickle();
auto foo = vm->wrap_string("thisWillCauseASyntaxError");
auto foo = vm->with_metadata(vm->wrap_string("thisWillCauseASyntaxError"), 1, 1, "foo.pickle", vm->list(3, NULL, NULL, NULL));
vm->run_next_thunk();
vm->gc();
printf("hello world\n");
delete vm;
return 0;
Expand Down
62 changes: 58 additions & 4 deletions test/out32.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,66 @@
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a cons
[tinobsy/tinobsy.cpp:110-init_cons]
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a cons
[tinobsy/tinobsy.cpp:110-init_cons]
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a cons
[tinobsy/tinobsy.cpp:110-init_cons]
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a string
[pickle.cpp:84-init_string] init_string: thisWillCauseASyntaxError
[pickle.cpp:81-init_string] init_string: thisWillCauseASyntaxError
[tinobsy/tinobsy.cpp:38-allocate] Trying to intern a string
[tinobsy/tinobsy.cpp:47-allocate] New string not interned
[pickle.cpp:178-run_next_thunk] run_next_thunk
hello world
[tinobsy/tinobsy.cpp:100-~vm] vm::~vm() {
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a string
[pickle.cpp:81-init_string] init_string: foo.pickle
[tinobsy/tinobsy.cpp:38-allocate] Trying to intern a string
[tinobsy/tinobsy.cpp:47-allocate] New string not interned
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a int
[tinobsy/tinobsy.cpp:38-allocate] Trying to intern a int
[tinobsy/tinobsy.cpp:47-allocate] New int not interned
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a int
[tinobsy/tinobsy.cpp:38-allocate] Trying to intern a int
[tinobsy/tinobsy.cpp:41-allocate] Interned! int
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a int begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a object_metadata
[pickle.cpp:189-run_next_thunk] run_next_thunk
[tinobsy/tinobsy.cpp:79-gc] vm::gc() begin, 7 objects {
[tinobsy/tinobsy.cpp:59-mark] NULL::mark()
[tinobsy/tinobsy.cpp:59-mark] NULL::mark()
[tinobsy/tinobsy.cpp:82-gc] garbage collect sweeping
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a object_metadata begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:124-finalize_cons]
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a int begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a string begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a string begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a cons begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:124-finalize_cons]
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a cons begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:124-finalize_cons]
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a cons begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:124-finalize_cons]
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:95-gc] vm::gc() end, 0 objects, 7 freed }
hello world
[tinobsy/tinobsy.cpp:100-~vm] vm::~vm() {
[tinobsy/tinobsy.cpp:106-~vm] }
62 changes: 58 additions & 4 deletions test/out64.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,66 @@
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a cons
[tinobsy/tinobsy.cpp:110-init_cons]
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a cons
[tinobsy/tinobsy.cpp:110-init_cons]
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a cons
[tinobsy/tinobsy.cpp:110-init_cons]
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a string
[pickle.cpp:84-init_string] init_string: thisWillCauseASyntaxError
[pickle.cpp:81-init_string] init_string: thisWillCauseASyntaxError
[tinobsy/tinobsy.cpp:38-allocate] Trying to intern a string
[tinobsy/tinobsy.cpp:47-allocate] New string not interned
[pickle.cpp:178-run_next_thunk] run_next_thunk
hello world
[tinobsy/tinobsy.cpp:100-~vm] vm::~vm() {
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a string
[pickle.cpp:81-init_string] init_string: foo.pickle
[tinobsy/tinobsy.cpp:38-allocate] Trying to intern a string
[tinobsy/tinobsy.cpp:47-allocate] New string not interned
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a int
[tinobsy/tinobsy.cpp:38-allocate] Trying to intern a int
[tinobsy/tinobsy.cpp:47-allocate] New int not interned
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a int
[tinobsy/tinobsy.cpp:38-allocate] Trying to intern a int
[tinobsy/tinobsy.cpp:41-allocate] Interned! int
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a int begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a object_metadata
[pickle.cpp:189-run_next_thunk] run_next_thunk
[tinobsy/tinobsy.cpp:79-gc] vm::gc() begin, 7 objects {
[tinobsy/tinobsy.cpp:59-mark] NULL::mark()
[tinobsy/tinobsy.cpp:59-mark] NULL::mark()
[tinobsy/tinobsy.cpp:82-gc] garbage collect sweeping
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a object_metadata begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:124-finalize_cons]
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a int begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a string begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a string begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a cons begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:124-finalize_cons]
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a cons begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:124-finalize_cons]
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:22-~object] object::~object() for a cons begin {
[tinobsy/tinobsy.cpp:25-~object] Assertion succeeded: xt != NULL
[tinobsy/tinobsy.cpp:124-finalize_cons]
[tinobsy/tinobsy.cpp:27-~object] }
[tinobsy/tinobsy.cpp:95-gc] vm::gc() end, 0 objects, 7 freed }
hello world
[tinobsy/tinobsy.cpp:100-~vm] vm::~vm() {
[tinobsy/tinobsy.cpp:106-~vm] }
28 changes: 14 additions & 14 deletions test/valgrind32.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
==10620== Memcheck, a memory error detector
==10620== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==10620== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==10620== Command: ./pickletest32
==10620==
==10620==
==10620== HEAP SUMMARY:
==10620== in use at exit: 0 bytes in 0 blocks
==10620== total heap usage: 6 allocs, 6 frees, 23,118 bytes allocated
==10620==
==10620== All heap blocks were freed -- no leaks are possible
==10620==
==10620== For lists of detected and suppressed errors, rerun with: -s
==10620== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==22550== Memcheck, a memory error detector
==22550== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22550== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==22550== Command: ./pickletest32
==22550==
==22550==
==22550== HEAP SUMMARY:
==22550== in use at exit: 0 bytes in 0 blocks
==22550== total heap usage: 19 allocs, 19 frees, 23,349 bytes allocated
==22550==
==22550== All heap blocks were freed -- no leaks are possible
==22550==
==22550== For lists of detected and suppressed errors, rerun with: -s
==22550== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
28 changes: 14 additions & 14 deletions test/valgrind64.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
==10594== Memcheck, a memory error detector
==10594== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==10594== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==10594== Command: ./pickletest64
==10594==
==10594==
==10594== HEAP SUMMARY:
==10594== in use at exit: 0 bytes in 0 blocks
==10594== total heap usage: 6 allocs, 6 frees, 76,922 bytes allocated
==10594==
==10594== All heap blocks were freed -- no leaks are possible
==10594==
==10594== For lists of detected and suppressed errors, rerun with: -s
==10594== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==22530== Memcheck, a memory error detector
==22530== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22530== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==22530== Command: ./pickletest64
==22530==
==22530==
==22530== HEAP SUMMARY:
==22530== in use at exit: 0 bytes in 0 blocks
==22530== total heap usage: 19 allocs, 19 frees, 77,317 bytes allocated
==22530==
==22530== All heap blocks were freed -- no leaks are possible
==22530==
==22530== For lists of detected and suppressed errors, rerun with: -s
==22530== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

0 comments on commit b8b96db

Please sign in to comment.