Skip to content

Commit 69a5245

Browse files
add mac test code
1 parent f3d37ad commit 69a5245

File tree

5 files changed

+3789
-21
lines changed

5 files changed

+3789
-21
lines changed

Makefile

+27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# need to set up AFL once parser is written https://medium.com/@ayushpriya10/fuzzing-applications-with-american-fuzzy-lop-afl-54facc65d102
22

3+
ifeq ($(OS),MacOS)
34
.PHONY: test64 builtest64 valgrind64 clean test32 buildtest32 valgrind32 deps show checkleaks
45

56
test: buildtest64 valgrind64 buildtest32 valgrind32 clean checkleaks
@@ -45,3 +46,29 @@ show:
4546
checkleaks:
4647
cat test/valgrind64.txt | grep "no leaks are possible" >/dev/null
4748
cat test/valgrind32.txt | grep "no leaks are possible" >/dev/null
49+
else
50+
.PHONY: test buildtest valgrind clean deps show checkleaks
51+
52+
VALGRINDOPTS = -atExit
53+
54+
test: buildtest valgrind clean checkleaks
55+
56+
CXXFLAGS += --std=c++11 -g
57+
58+
buildtest:
59+
$(CXX) $(CXXFLAGS) pickle_test.cpp -o pickletest
60+
61+
valgrind: buildtest
62+
MallocStackLogging=1 leaks $(VALGRINDOPTS) -- ./pickletest > test/outMac.txt 2>&1
63+
64+
clean:
65+
$(RM) -f pickletest
66+
$(RM) -rf pickletest.dSYM
67+
68+
show:
69+
cat test/outMac.txt
70+
cat test/valgrindMac.txt
71+
72+
checkleaks:
73+
cat test/outMac.txt | grep "0 leaks for 0 total leaked bytes" >/dev/null
74+
endif

pickle.cpp

+15-20
Original file line numberDiff line numberDiff line change
@@ -497,17 +497,17 @@ object* splice_match(pvm* vm, object* cookie, object* inst_type) {
497497
namespace dumper {
498498

499499
static void make_refs_list(pvm* vm, object* obj, object** alist) {
500-
again:
501-
if (obj == NULL || (obj->type != &cons_type && obj->type != &obj_type)) return;
502-
object* entry = assoc(*alist, obj);
503-
if (entry) {
504-
cdr(entry) = vm->integer(2);
505-
return;
500+
for (;;) {
501+
if (obj == NULL || (obj->type != &cons_type && obj->type != &obj_type)) return;
502+
object* entry = assoc(*alist, obj);
503+
if (entry) {
504+
cdr(entry) = vm->integer(2);
505+
return;
506+
}
507+
vm->push(vm->cons(obj, vm->integer(1)), *alist);
508+
if (obj->type != &obj_type) make_refs_list(vm, car(obj), alist); // hashmaps are guaranteed non disjoint, i guess
509+
obj = cdr(obj);
506510
}
507-
vm->push(vm->cons(obj, vm->integer(1)), *alist);
508-
if (obj->type != &obj_type) make_refs_list(vm, car(obj), alist); // hashmaps are guaranteed non disjoint, i guess
509-
obj = cdr(obj);
510-
goto again;
511511
}
512512

513513
// returns zero if the object doesn't need a #N# marker
@@ -535,8 +535,7 @@ static int64_t reffed(pvm* vm, object* obj, object* alist, int64_t* counter) {
535535
static void print_with_refs(pvm*, object*, object*, int64_t*);
536536

537537
static void print_hashmap(pvm* vm, object* node, object* alist, int64_t* counter) {
538-
recur:
539-
if (node) {
538+
while (node) {
540539
if (car(node)) {
541540
object* hinfo = car(node);
542541
print_with_refs(vm, cadr(hinfo), alist, counter);
@@ -547,7 +546,6 @@ static void print_hashmap(pvm* vm, object* node, object* alist, int64_t* counter
547546
if (!cdr(node)) return;
548547
print_hashmap(vm, cadr(node), alist, counter);
549548
node = cddr(node);
550-
goto recur;
551549
}
552550
}
553551

@@ -570,15 +568,12 @@ static void print_with_refs(pvm* vm, object* obj, object* alist, int64_t* counte
570568
putchar('"');
571569
for (char* c = obj->as_chars; *c; c++) {
572570
char e = parser::escape(*c);
573-
if (e != *c) {
574-
putchar('\\');
575-
putchar(e);
576-
}
577-
else putchar(*c);
571+
if (e != *c) putchar('\\');
572+
putchar(e);
578573
}
579574
putchar('"');
580575
}
581-
PRINTTYPE(&symbol_type, as_chars, strpbrk(obj->as_chars, "(){}[] ") ? "#|%s|" : "'%s");
576+
PRINTTYPE(&symbol_type, as_chars, strpbrk(obj->as_chars, "(){}[] ") ? "#|%s|" : ":%s");
582577
PRINTTYPE(&integer_type, as_big_int, "%" PRId64);
583578
PRINTTYPE(&float_type, as_double, "%lg");
584579
PRINTTYPE(&c_function_type, as_ptr, "<function %p>");
@@ -622,7 +617,7 @@ static void print_with_refs(pvm* vm, object* obj, object* alist, int64_t* counte
622617
print_hashmap(vm, cdr(obj), alist, counter);
623618
putchar('}');
624619
}
625-
else printf("<%s:%p>", obj->type->name, obj->as_ptr);
620+
else printf("<%s: %p>", obj->type->name, obj->as_ptr);
626621
}
627622

628623
}

pickle_test.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,9 @@ int main() {
121121

122122
printf("all done -- cleaning up\n");
123123
// implicit destruction of vm;
124+
125+
#ifdef __APPLE__
126+
system("leaks executablename");
127+
#endif
124128
return 0;
125129
}

0 commit comments

Comments
 (0)