nasty bugfix when returning bools
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 8 Dec 2020 17:05:00 +0000 (11:05 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 8 Dec 2020 17:05:00 +0000 (11:05 -0600)
It used to mess up the stack and in unforgiving environments,
i.e. wasmtime or wasmer, it caused misaligned reads resulting
in undefined behaviour.

core/file.onyx
core/sys/js.onyx
onyx
src/onyxwasm.c

index 7ec1d47bc30f8809f487c1eb7285479c20061e20..c107ddd8562eb9ed1241972c9819e2493d4f2a3c 100644 (file)
@@ -107,7 +107,7 @@ get_contents :: proc {
     proc (path: string) -> string {
         tmp_file: File;
 
-        if !open(^tmp_file, path, OpenMode.Read) do return "";
+        if !open(^tmp_file, path, OpenMode.Read) do return string.{ null, 0 };
         defer close(tmp_file);
 
         return get_contents(tmp_file);
index 83b2dbd095e10b7c3ce75298e801f567a369bcbf..e09f55cb4c4799377762f3975e6b71625a63c422 100644 (file)
@@ -13,12 +13,12 @@ proc () #export "_start" {
     context.allocator = allocator.heap_allocator;
     context.temp_allocator = allocator.heap_allocator;
 
-    stdio_init();
-
     args : [] cstring;
     args.data  = null;
     args.count = 0;
 
+    stdio_init();
+
     main.main(args);
 
     print_buffer_flush();
diff --git a/onyx b/onyx
index d33443f99987d6565ff2753e9458a53018815f80..a01c7ce4813781d6747076c5d368b654e2731305 100755 (executable)
Binary files a/onyx and b/onyx differ
index baea982386bf804057060cd0ada385ef339a916e..a711607e86642052397897f9616620c641dbc6af 100644 (file)
@@ -1413,14 +1413,15 @@ EMIT_FUNC(call, AstCall* call) {
     CallingConvention cc = type_function_get_cc(call->callee->type);
     assert(cc != CC_Undefined);
 
+    b32 needs_stack = (cc == CC_Return_Stack) || (stack_grow_amm > 0);
+
     Type* return_type = call->callee->type->Function.return_type;
     u32 return_size = type_size_of(return_type);
     u32 return_align = type_alignment_of(return_type);
     bh_align(return_size, return_align);
 
-    stack_grow_amm += return_size;
-
-    b32 needs_stack = (cc == CC_Return_Stack) || (stack_grow_amm > 0);
+    if (cc == CC_Return_Stack)
+        stack_grow_amm += return_size;
 
     if (needs_stack) {
         WID(WI_GLOBAL_GET, stack_top_idx);