Răsfoiți Sursa

implement printf syscall wrapper for linux

wenyongh 6 ani în urmă
părinte
comite
40e7c627aa

+ 27 - 14
core/iwasm/runtime/platform/linux/wasm-native.c

@@ -109,31 +109,44 @@ __syscall3_wrapper(int32 arg0, int32 arg1, int32 arg2, int32 arg3)
             return syscall(54, arg1, arg2, wsz);
         }
 
-        case 145: /* readv */
         case 146: /* writev */
         {
             /* Implement syscall 54 and syscall 146 to support printf()
                for non SIDE_MODULE=1 mode */
-            uint32 iovcnt = arg3, i;
-            struct iovec *vec_begin, *vec;
-
-            if (!validate_app_addr(arg2, sizeof(struct iovec)))
+            struct iovec_app {
+                int32 iov_base_offset;
+                uint32 iov_len;
+            } *vec;
+            int32 vec_offset = arg2, str_offset;
+            uint32 iov_count = arg3, i;
+            int32 count = 0;
+            char *iov_base, *str;
+
+            if (!validate_app_addr(vec_offset, sizeof(struct iovec_app)))
                 return 0;
 
-            vec_begin = vec = (struct iovec*)addr_app_to_native(arg2);
-            for (i = 0; i < iovcnt; i++, vec++) {
+            vec = (struct iovec_app *)addr_app_to_native(vec_offset);
+            for (i = 0; i < iov_count; i++, vec++) {
                 if (vec->iov_len > 0) {
-                    if (!validate_app_addr((int32)vec->iov_base, 1))
+                    if (!validate_app_addr(vec->iov_base_offset, 1))
                         return 0;
-                    vec->iov_base = addr_app_to_native((int32)vec->iov_base);
+                    iov_base = (char*)addr_app_to_native(vec->iov_base_offset);
+
+                    if (!(str_offset = module_malloc(vec->iov_len + 1)))
+                        return 0;
+
+                    str = addr_app_to_native(str_offset);
+
+                    memcpy(str, iov_base, vec->iov_len);
+                    str[vec->iov_len] = '\0';
+                    count += wasm_printf("%s", str);
+
+                    module_free(str_offset);
                 }
             }
-          if (arg0 == 145)
-              return syscall(145, arg1, vec_begin, arg3);
-          else
-              return syscall(146, arg1, vec_begin, arg3);
+            return count;
         }
-
+        case 145: /* readv */
         case 3: /* read*/
         case 5: /* open */
         case 221: /* fcntl */

+ 2 - 2
core/shared-lib/mem-alloc/ems/ems_alloc.c

@@ -152,7 +152,7 @@ void gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
     bh_assert(
             hmu && (gc_uint8*) hmu >= heap->base_addr
                     && (gc_uint8*) hmu < heap->base_addr + heap->current_size);
-    bh_assert(((gc_uint32) hmu_to_obj(hmu) & 7) == 0);
+    bh_assert(((gc_uint32)(uintptr_t)hmu_to_obj(hmu) & 7) == 0);
     bh_assert(
             size > 0
                     && ((gc_uint8*) hmu) + size
@@ -242,7 +242,7 @@ BH_STATIC hmu_t *alloc_hmu(gc_heap_t *heap, gc_size_t size)
 
             p = node->next;
             node->next = p->next;
-            bh_assert(((gc_int32) hmu_to_obj(p) & 7) == 0);
+            bh_assert(((gc_int32)(intptr_t)hmu_to_obj(p) & 7) == 0);
 
             if ((gc_size_t) node_idx
                     != init_node_idx&& ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) { /* with bigger size*/

+ 1 - 1
core/shared-lib/platform/linux/bh_thread.c

@@ -75,7 +75,7 @@ static void *vm_thread_wrapper(void *arg)
 {
     thread_wrapper_arg * targ = arg;
     LOG_VERBOSE("THREAD CREATE 0x%08x\n", &targ);
-    targ->stack = (void *) ((unsigned int) (&arg) & ~0xfff);
+    targ->stack = (void *) ((uintptr_t)(&arg) & ~0xfff);
     _vm_tls_put(1, targ);
     targ->start(targ->arg);
     bh_free(targ);