diff --git a/ATTRIBUTIONS.md b/ATTRIBUTIONS.md index 04b5fa25b..a2e76403a 100644 --- a/ATTRIBUTIONS.md +++ b/ATTRIBUTIONS.md @@ -2,329 +2,64 @@ WebAssembly Micro Runtime Attributions ====================================== WAMR project reused some components from other open source project: -- **wasmtime**: for the wasi libc implementation - **cJson**: used in the host_tool for remotely managing wasm applications - **contiki-ng**: for the coap protocol implementation - **freebsd libm**: used in core/shared/platform/alios/bh_math.c -- **littlevgl**: for the gui samples and wrapped the wasm graphic layer. +- **littlevgl**: for the gui samples and wrapped the wasm graphic layer +- **llvm**. for the AOT/JIT compilation +- **wasm-c-api**. to implement the C-APIs of wasm. using headers and sameples +- **wasmtime**: for the wasi libc implementation +- **zephyr**. for several platform specific examples -The WAMR fast interpreter is a clean room development. We would acknowledge the inspirations by [WASM3](https://github.com/wasm3/wasm3) open source project for the approach of pre-calculated oprand stack location. +The WAMR fast interpreter is a clean room development. We would acknowledge the inspirations by [WASM3](https://github.com/wasm3/wasm3) open source project for the approach of pre-calculated oprand stack location. + +| third party components | version number | latest release | vendor pages | CVE details | +| --- | --- | --- | --- | --- | +| cjson | 1.7.10 | 1.7.14 | https://github.com/DaveGamble/cJSON | https://www.cvedetails.com/vendor/19164/Cjson-Project.html | +| contiki-ng (er-coap) | unspecified | 3.0 | https://github.com/contiki-os/contiki | https://www.cvedetails.com/vendor/16528/Contiki-os.html | +| freebsd libm | unspecified | 13.0 | https://www.freebsd.org/ | https://www.cvedetails.com/vendor/6/Freebsd.html | +| littlevgl | 6.0.1 | 7.11.0 | https://lvgl.io/ | | +| llvm | 11.0.1 | 12.0.0 | https://llvm.org | https://www.cvedetails.com/vendor/13260/Llvm.html | +| wasm-c-api | ac9b509f4df86e40e56e9b01f3f49afab0100037 | c9d31284651b975f05ac27cee0bab1377560b87e | https://github.com/WebAssembly/wasm-c-api | | +| wasmtime | unspecified | v0.26.0 | https://github.com/bytecodealliance/wasmtime | | +| zephyr | unspecified | v2.5.0 | https://www.zephyrproject.org/ | https://www.cvedetails.com/vendor/19255/Zephyrproject.html | ## Licenses -### wasmtime - -``` - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - ---- LLVM Exceptions to the Apache 2.0 License ---- - -As an exception, if, as a result of your compiling your source code, portions -of this Software are embedded into an Object form of such source code, you -may redistribute such embedded portions in such Object form without complying -with the conditions of Sections 4(a), 4(b) and 4(d) of the License. - -In addition, if you combine or link compiled forms of this Software with -software that is licensed under the GPLv2 ("Combined Software") and if a -court of competent jurisdiction determines that the patent provision (Section -3), the indemnity provision (Section 9) or other Section of the License -conflicts with the conditions of the GPLv2, you may retroactively and -prospectively choose to deem waived or otherwise exclude such Section(s) of -the License, but only in their entirety and only with respect to the Combined -Software. -``` - ### cJson -``` -Copyright (c) 2009-2017 Dave Gamble and cJSON contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -``` +[LICENSE](./test-tools/host-tool/external/cJSON/LICENSE) ### contiki-ng -``` - - -Copyright (c) (Year), (Name of copyright holder) All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - - Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -``` +[LICENSE](./core/shared/coap/er-coap/LICENSE.md) ### freebsd libm -``` -Copyright 1992-2011 The FreeBSD Project. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - The views and conclusions contained in the software and documentation - are those of the authors and should not be interpreted as representing - official policies, either expressed or implied, of the FreeBSD - Project. -``` +[COPYRIGHT](./core/shared/platform/common/math/COPYRIGHT) ### littlevgl -``` -MIT licence -Copyright (c) 2016 Gábor Kiss-Vámosi +[LICENCE](./samples/littlevgl/LICENCE.txt) -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +[LICENSE](./core/deps/lvgl/LICENCE.txt) -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +### llvm -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -``` +[LICENSE](./core/deps/llvm/llvm/LICENCE.txt) +### wasm-c-api +[LICENSE](./samples/wasm-c-api/src/LICENSE) +### wasmtime +[LICENSE](./core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/LICENSE) +[LICENSE](./core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/LICENSE) + +[LICENSE](./core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/include/LICENSE) + +### zephyr + +[LICENSE](./samples/gui/wasm-runtime-wgl/src/platform/zephyr/LICENSE) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index d34708c19..68aad2588 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -219,6 +219,8 @@ else () message (" Reference types disabled") endif () if (DEFINED WAMR_BH_VPRINTF) - add_definitions (-DBH_VPRINTF=${WAMR_BH_VPRINTF}) + add_definitions (-DBH_VPRINTF=${WAMR_BH_VPRINTF}) +endif () +if (WAMR_DISABLE_APP_ENTRY EQUAL 1) + message (" WAMR application entry functions excluded") endif () - diff --git a/core/iwasm/common/iwasm_common.cmake b/core/iwasm/common/iwasm_common.cmake index 99c8c7e9f..7e9944fc0 100644 --- a/core/iwasm/common/iwasm_common.cmake +++ b/core/iwasm/common/iwasm_common.cmake @@ -10,6 +10,10 @@ add_definitions(-DBH_FREE=wasm_runtime_free) file (GLOB c_source_all ${IWASM_COMMON_DIR}/*.c) +if (WAMR_DISABLE_APP_ENTRY EQUAL 1) + list(REMOVE_ITEM c_source_all "${IWASM_COMMON_DIR}/wasm_application.c") +endif () + if (WAMR_BUILD_INVOKE_NATIVE_GENERAL EQUAL 1) # Use invokeNative C version instead of asm code version # if WAMR_BUILD_INVOKE_NATIVE_GENERAL is explicitly set. diff --git a/core/iwasm/common/wasm_application.c b/core/iwasm/common/wasm_application.c new file mode 100644 index 000000000..01ae4f9a7 --- /dev/null +++ b/core/iwasm/common/wasm_application.c @@ -0,0 +1,672 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "bh_platform.h" +#if WASM_ENABLE_INTERP != 0 +#include "../interpreter/wasm_runtime.h" +#endif +#if WASM_ENABLE_AOT != 0 +#include "../aot/aot_runtime.h" +#endif + +static void +set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) +{ + if (error_buf != NULL) + snprintf(error_buf, error_buf_size, "%s", string); +} + +static void * +runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst, + char *error_buf, uint32 error_buf_size) +{ + void *mem; + + if (size >= UINT32_MAX + || !(mem = wasm_runtime_malloc((uint32)size))) { + if (module_inst != NULL) { + wasm_runtime_set_exception(module_inst, + "allocate memory failed"); + } + else if (error_buf != NULL) { + set_error_buf(error_buf, error_buf_size, + "allocate memory failed"); + } + return NULL; + } + + memset(mem, 0, (uint32)size); + return mem; +} + +static union { + int a; + char b; +} __ue = { .a = 1 }; + +#define is_little_endian() (__ue.b == 1) + +/** + * Implementation of wasm_application_execute_main() + */ + +static WASMFunctionInstanceCommon* +resolve_function(const WASMModuleInstanceCommon *module_inst, + const char *name); + +static bool +check_main_func_type(const WASMType *type) +{ + if (!(type->param_count == 0 || type->param_count == 2) + ||type->result_count > 1) { + LOG_ERROR("WASM execute application failed: invalid main function type.\n"); + return false; + } + + if (type->param_count == 2 + && !(type->types[0] == VALUE_TYPE_I32 + && type->types[1] == VALUE_TYPE_I32)) { + LOG_ERROR("WASM execute application failed: invalid main function type.\n"); + return false; + } + + if (type->result_count + && type->types[type->param_count] != VALUE_TYPE_I32) { + LOG_ERROR("WASM execute application failed: invalid main function type.\n"); + return false; + } + + return true; +} + +bool +wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, + int32 argc, char *argv[]) +{ + WASMFunctionInstanceCommon *func; + WASMType *func_type = NULL; + uint32 argc1 = 0, argv1[2] = { 0 }; + uint32 total_argv_size = 0; + uint64 total_size; + uint32 argv_buf_offset = 0; + int32 i; + char *argv_buf, *p, *p_end; + uint32 *argv_offsets, module_type; + bool ret, is_import_func = true; + +#if WASM_ENABLE_LIBC_WASI != 0 + if (wasm_runtime_is_wasi_mode(module_inst)) { + /* In wasi mode, we should call function named "_start" + which initializes the wasi envrionment and then calls + the actual main function. Directly call main function + may cause exception thrown. */ + if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) + return wasm_runtime_create_exec_env_and_call_wasm( + module_inst, func, 0, NULL); + /* if no start function is found, we execute + the main function as normal */ + } +#endif /* end of WASM_ENABLE_LIBC_WASI */ + + if (!(func = resolve_function(module_inst, "main")) + && !(func = resolve_function(module_inst, "__main_argc_argv")) + && !(func = resolve_function(module_inst, "_main"))) { + wasm_runtime_set_exception(module_inst, + "lookup main function failed"); + return false; + } + +#if WASM_ENABLE_INTERP != 0 + if (module_inst->module_type == Wasm_Module_Bytecode) { + is_import_func = ((WASMFunctionInstance*)func)->is_import_func; + } +#endif +#if WASM_ENABLE_AOT != 0 + if (module_inst->module_type == Wasm_Module_AoT) { + is_import_func = ((AOTFunctionInstance*)func)->is_import_func; + } +#endif + + if (is_import_func) { + wasm_runtime_set_exception(module_inst, + "lookup main function failed"); + return false; + } + + module_type = module_inst->module_type; + func_type = wasm_runtime_get_function_type(func, module_type); + + if (!func_type) { + LOG_ERROR("invalid module instance type"); + return false; + } + + if (!check_main_func_type(func_type)) { + wasm_runtime_set_exception(module_inst, + "invalid function type of main function"); + return false; + } + + if (func_type->param_count) { + for (i = 0; i < argc; i++) + total_argv_size += (uint32)(strlen(argv[i]) + 1); + total_argv_size = align_uint(total_argv_size, 4); + + total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc; + + if (total_size >= UINT32_MAX + || !(argv_buf_offset = + wasm_runtime_module_malloc(module_inst, (uint32)total_size, + (void**)&argv_buf))) { + wasm_runtime_set_exception(module_inst, + "allocate memory failed"); + return false; + } + + p = argv_buf; + argv_offsets = (uint32*)(p + total_argv_size); + p_end = p + total_size; + + for (i = 0; i < argc; i++) { + bh_memcpy_s(p, (uint32)(p_end - p), argv[i], (uint32)(strlen(argv[i]) + 1)); + argv_offsets[i] = argv_buf_offset + (uint32)(p - argv_buf); + p += strlen(argv[i]) + 1; + } + + argc1 = 2; + argv1[0] = (uint32)argc; + argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets); + } + + ret = wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, + argc1, argv1); + if (argv_buf_offset) + wasm_runtime_module_free(module_inst, argv_buf_offset); + return ret; +} + +#if WASM_ENABLE_MULTI_MODULE != 0 +static WASMModuleInstance * +get_sub_module_inst(const WASMModuleInstance *parent_module_inst, + const char *sub_module_name) +{ + WASMSubModInstNode *node = + bh_list_first_elem(parent_module_inst->sub_module_inst_list); + + while (node && strcmp(node->module_name, sub_module_name)) { + node = bh_list_elem_next(node); + } + return node ? node->module_inst : NULL; +} + +static bool +parse_function_name(char *orig_function_name, char **p_module_name, + char **p_function_name) +{ + if (orig_function_name[0] != '$') { + *p_module_name = NULL; + *p_function_name = orig_function_name; + return true; + } + + /** + * $module_name$function_name\0 + * ===> + * module_name\0function_name\0 + * ===> + * module_name + * function_name + */ + char *p1 = orig_function_name; + char *p2 = strchr(p1 + 1, '$'); + if (!p2) { + LOG_DEBUG("can not parse the incoming function name"); + return false; + } + + *p_module_name = p1 + 1; + *p2 = '\0'; + *p_function_name = p2 + 1; + return strlen(*p_module_name) && strlen(*p_function_name); +} +#endif + +/** + * Implementation of wasm_application_execute_func() + */ + +static WASMFunctionInstanceCommon* +resolve_function(const WASMModuleInstanceCommon *module_inst, + const char *name) +{ + uint32 i = 0; + WASMFunctionInstanceCommon *ret = NULL; +#if WASM_ENABLE_MULTI_MODULE != 0 + WASMModuleInstance *sub_module_inst = NULL; + char *orig_name = NULL; + char *sub_module_name = NULL; + char *function_name = NULL; + uint32 length = (uint32)(strlen(name) + 1); + + orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0); + if (!orig_name) { + return NULL; + } + + strncpy(orig_name, name, length); + + if (!parse_function_name(orig_name, &sub_module_name, &function_name)) { + goto LEAVE; + } + + LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name); + + if (sub_module_name) { + sub_module_inst = get_sub_module_inst( + (WASMModuleInstance *)module_inst, sub_module_name); + if (!sub_module_inst) { + LOG_DEBUG("can not find a sub module named %s", sub_module_name); + goto LEAVE; + } + } +#else + const char *function_name = name; +#endif + +#if WASM_ENABLE_INTERP != 0 + if (module_inst->module_type == Wasm_Module_Bytecode) { + WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst; + +#if WASM_ENABLE_MULTI_MODULE != 0 + wasm_inst = sub_module_inst ? sub_module_inst : wasm_inst; +#endif /* WASM_ENABLE_MULTI_MODULE */ + + for (i = 0; i < wasm_inst->export_func_count; i++) { + if (!strcmp(wasm_inst->export_functions[i].name, function_name)) { + ret = wasm_inst->export_functions[i].function; + break; + } + } + } +#endif /* WASM_ENABLE_INTERP */ + +#if WASM_ENABLE_AOT != 0 + if (module_inst->module_type == Wasm_Module_AoT) { + AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst; + AOTFunctionInstance *export_funcs = (AOTFunctionInstance *) + aot_inst->export_funcs.ptr; + for (i = 0; i < aot_inst->export_func_count; i++) { + if (!strcmp(export_funcs[i].func_name, function_name)) { + ret = &export_funcs[i]; + break; + } + } + } +#endif + +#if WASM_ENABLE_MULTI_MODULE != 0 +LEAVE: + wasm_runtime_free(orig_name); +#endif + return ret; +} + +union ieee754_float { + float f; + + /* This is the IEEE 754 single-precision format. */ + union { + struct { + unsigned int negative:1; + unsigned int exponent:8; + unsigned int mantissa:23; + } ieee_big_endian; + struct { + unsigned int mantissa:23; + unsigned int exponent:8; + unsigned int negative:1; + } ieee_little_endian; + } ieee; +}; + +union ieee754_double { + double d; + + /* This is the IEEE 754 double-precision format. */ + union { + struct { + unsigned int negative:1; + unsigned int exponent:11; + /* Together these comprise the mantissa. */ + unsigned int mantissa0:20; + unsigned int mantissa1:32; + } ieee_big_endian; + + struct { + /* Together these comprise the mantissa. */ + unsigned int mantissa1:32; + unsigned int mantissa0:20; + unsigned int exponent:11; + unsigned int negative:1; + } ieee_little_endian; + } ieee; +}; + +bool +wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, + const char *name, int32 argc, char *argv[]) +{ + WASMFunctionInstanceCommon *func; + WASMType *type = NULL; + uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0; + int32 i, p, module_type; + uint64 total_size; + const char *exception; + char buf[128]; + + bh_assert(argc >= 0); + LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc); + func = resolve_function(module_inst, name); + + if (!func) { + snprintf(buf, sizeof(buf), "lookup function %s failed", name); + wasm_runtime_set_exception(module_inst, buf); + goto fail; + } + +#if WASM_ENABLE_INTERP != 0 + if (module_inst->module_type == Wasm_Module_Bytecode) { + WASMFunctionInstance *wasm_func = (WASMFunctionInstance*)func; + if (wasm_func->is_import_func +#if WASM_ENABLE_MULTI_MODULE != 0 + && !wasm_func->import_func_inst +#endif + ) { + snprintf(buf, sizeof(buf), "lookup function %s failed", name); + wasm_runtime_set_exception(module_inst, buf); + goto fail; + } + } +#endif + + module_type = module_inst->module_type; + type = wasm_runtime_get_function_type(func, module_type); + + if (!type) { + LOG_ERROR("invalid module instance type"); + return false; + } + + if (type->param_count != (uint32)argc) { + wasm_runtime_set_exception(module_inst, + "invalid input argument count"); + goto fail; + } + + argc1 = type->param_cell_num; + cell_num = (argc1 > type->ret_cell_num) ? argc1 : type->ret_cell_num; + + total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2); + if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, + NULL, 0)))) { + goto fail; + } + + /* Parse arguments */ + for (i = 0, p = 0; i < argc; i++) { + char *endptr = NULL; + bh_assert(argv[i] != NULL); + if (argv[i][0] == '\0') { + snprintf(buf, sizeof(buf), "invalid input argument %d", i); + wasm_runtime_set_exception(module_inst, buf); + goto fail; + } + switch (type->types[i]) { + case VALUE_TYPE_I32: + argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0); + break; + case VALUE_TYPE_I64: + { + union { uint64 val; uint32 parts[2]; } u; + u.val = strtoull(argv[i], &endptr, 0); + argv1[p++] = u.parts[0]; + argv1[p++] = u.parts[1]; + break; + } + case VALUE_TYPE_F32: + { + float32 f32 = strtof(argv[i], &endptr); + if (isnan(f32)) { + if (argv[i][0] == '-') { + union ieee754_float u; + u.f = f32; + if (is_little_endian()) + u.ieee.ieee_little_endian.negative = 1; + else + u.ieee.ieee_big_endian.negative = 1; + memcpy(&f32, &u.f, sizeof(float)); + } + if (endptr[0] == ':') { + uint32 sig; + union ieee754_float u; + sig = (uint32)strtoul(endptr + 1, &endptr, 0); + u.f = f32; + if (is_little_endian()) + u.ieee.ieee_little_endian.mantissa = sig; + else + u.ieee.ieee_big_endian.mantissa = sig; + memcpy(&f32, &u.f, sizeof(float)); + } + } + memcpy(&argv1[p++], &f32, sizeof(float)); + break; + } + case VALUE_TYPE_F64: + { + union { float64 val; uint32 parts[2]; } u; + u.val = strtod(argv[i], &endptr); + if (isnan(u.val)) { + if (argv[i][0] == '-') { + union ieee754_double ud; + ud.d = u.val; + if (is_little_endian()) + ud.ieee.ieee_little_endian.negative = 1; + else + ud.ieee.ieee_big_endian.negative = 1; + memcpy(&u.val, &ud.d, sizeof(double)); + } + if (endptr[0] == ':') { + uint64 sig; + union ieee754_double ud; + sig = strtoull(endptr + 1, &endptr, 0); + ud.d = u.val; + if (is_little_endian()) { + ud.ieee.ieee_little_endian.mantissa0 = sig >> 32; + ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig; + } + else { + ud.ieee.ieee_big_endian.mantissa0 = sig >> 32; + ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig; + } + memcpy(&u.val, &ud.d, sizeof(double)); + } + } + argv1[p++] = u.parts[0]; + argv1[p++] = u.parts[1]; + break; + } +#if WASM_ENABLE_SIMD != 0 + case VALUE_TYPE_V128: + { + /* it likes 0x123\0x234 or 123\234 */ + /* retrive first i64 */ + *(uint64*)(argv1 + p) = strtoull(argv[i], &endptr, 0); + /* skip \ */ + endptr++; + /* retrive second i64 */ + *(uint64*)(argv1 + p + 2) = strtoull(endptr, &endptr, 0); + p += 4; + break; + } +#endif /* WASM_ENABLE_SIMD != 0 */ +#if WASM_ENABLE_REF_TYPES != 0 + case VALUE_TYPE_FUNCREF: + { + if (strncmp(argv[i], "null", 4) == 0 + || strncmp(argv[i], "NULL", 4) == 0) { + argv1[p++] = NULL_REF; + } + else { + argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0); + } + break; + } + case VALUE_TYPE_EXTERNREF: + { + if (strncmp(argv[i], "null", 4) == 0 + || strncmp(argv[i], "NULL", 4) == 0) { + argv1[p++] = NULL_REF; + } + else { + uint64 val = strtoull(argv[i], &endptr, 0); + void *extern_obj = (void *)(uintptr_t)val; + uint32 externref_idx; + + if (!wasm_externref_obj2ref(module_inst, extern_obj, + &externref_idx)) { + wasm_runtime_set_exception( + module_inst, "map extern object to ref failed"); + goto fail; + } + argv1[p++] = externref_idx; + } + break; + } +#endif /* WASM_ENABLE_REF_TYPES */ + default: + bh_assert(0); + break; + } + if (endptr && *endptr != '\0' && *endptr != '_') { + snprintf(buf, sizeof(buf), "invalid input argument %d: %s", + i, argv[i]); + wasm_runtime_set_exception(module_inst, buf); + goto fail; + } + } + bh_assert(p == (int32)argc1); + + wasm_runtime_set_exception(module_inst, NULL); + if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, + argc1, argv1)) { + goto fail; + } + + /* print return value */ + for (j = 0; j < type->result_count; j++) { + switch (type->types[type->param_count + j]) { + case VALUE_TYPE_I32: + { + os_printf("0x%x:i32", argv1[k]); + k++; + break; + } + case VALUE_TYPE_I64: + { + union { uint64 val; uint32 parts[2]; } u; + u.parts[0] = argv1[k]; + u.parts[1] = argv1[k + 1]; + k += 2; +#ifdef PRIx64 + os_printf("0x%"PRIx64":i64", u.val); +#else + char buf[16]; + if (sizeof(long) == 4) + snprintf(buf, sizeof(buf), "%s", "0x%llx:i64"); + else + snprintf(buf, sizeof(buf), "%s", "0x%lx:i64"); + os_printf(buf, u.val); +#endif + break; + } + case VALUE_TYPE_F32: + { + os_printf("%.7g:f32", *(float32*)(argv1 + k)); + k++; + break; + } + case VALUE_TYPE_F64: + { + union { float64 val; uint32 parts[2]; } u; + u.parts[0] = argv1[k]; + u.parts[1] = argv1[k + 1]; + k += 2; + os_printf("%.7g:f64", u.val); + break; + } +#if WASM_ENABLE_REF_TYPES + case VALUE_TYPE_FUNCREF: + { + if (argv1[k] != NULL_REF) + os_printf("%u:ref.func", argv1[k]); + else + os_printf("func:ref.null"); + k++; + break; + } + case VALUE_TYPE_EXTERNREF: + { + if (argv1[k] != NULL_REF) { + void *extern_obj = NULL; + bool ret = wasm_externref_ref2obj(argv1[k], &extern_obj); + bh_assert(ret); + (void)ret; + os_printf("%p:ref.extern", extern_obj); + } + else + os_printf("extern:ref.null"); + k++; + break; + } +#endif +#if WASM_ENABLE_SIMD != 0 + case VALUE_TYPE_V128: + { + uint64 *v = (uint64*)(argv1 + k); +#if defined(PRIx64) + os_printf("<0x%016"PRIx64" 0x%016"PRIx64">:v128", *v, *(v + 1)); +#else + if (4 == sizeof(long)) { + os_printf("<0x%016llx 0x%016llx>:v128", *v, *(v + 1)); + } + else { + os_printf("<0x%016lx 0x%016lx>:v128", *v, *(v + 1)); + } +#endif /* PRIx64 */ + k += 4; + break; + } +#endif /* WASM_ENABLE_SIMD != 0 */ + default: + bh_assert(0); + break; + } + if (j < (uint32)(type->result_count - 1)) + os_printf(","); + } + os_printf("\n"); + + wasm_runtime_free(argv1); + return true; + +fail: + if (argv1) + wasm_runtime_free(argv1); + + exception = wasm_runtime_get_exception(module_inst); + bh_assert(exception); + os_printf("%s\n", exception); + return false; +} \ No newline at end of file diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 27f1f02eb..c4c7e778e 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -2266,312 +2266,6 @@ wasm_exec_env_get_module(WASMExecEnv *exec_env) return NULL; } -/** - * Implementation of wasm_application_execute_main() - */ - -static WASMFunctionInstanceCommon* -resolve_function(const WASMModuleInstanceCommon *module_inst, - const char *name); - -static bool -check_main_func_type(const WASMType *type) -{ - if (!(type->param_count == 0 || type->param_count == 2) - ||type->result_count > 1) { - LOG_ERROR("WASM execute application failed: invalid main function type.\n"); - return false; - } - - if (type->param_count == 2 - && !(type->types[0] == VALUE_TYPE_I32 - && type->types[1] == VALUE_TYPE_I32)) { - LOG_ERROR("WASM execute application failed: invalid main function type.\n"); - return false; - } - - if (type->result_count - && type->types[type->param_count] != VALUE_TYPE_I32) { - LOG_ERROR("WASM execute application failed: invalid main function type.\n"); - return false; - } - - return true; -} - -bool -wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, - int32 argc, char *argv[]) -{ - WASMFunctionInstanceCommon *func; - WASMType *func_type = NULL; - uint32 argc1 = 0, argv1[2] = { 0 }; - uint32 total_argv_size = 0; - uint64 total_size; - uint32 argv_buf_offset = 0; - int32 i; - char *argv_buf, *p, *p_end; - uint32 *argv_offsets, module_type; - bool ret, is_import_func = true; - -#if WASM_ENABLE_LIBC_WASI != 0 - if (wasm_runtime_is_wasi_mode(module_inst)) { - /* In wasi mode, we should call function named "_start" - which initializes the wasi envrionment and then calls - the actual main function. Directly call main function - may cause exception thrown. */ - if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) - return wasm_runtime_create_exec_env_and_call_wasm( - module_inst, func, 0, NULL); - /* if no start function is found, we execute - the main function as normal */ - } -#endif /* end of WASM_ENABLE_LIBC_WASI */ - - if (!(func = resolve_function(module_inst, "main")) - && !(func = resolve_function(module_inst, "__main_argc_argv")) - && !(func = resolve_function(module_inst, "_main"))) { - wasm_runtime_set_exception(module_inst, - "lookup main function failed"); - return false; - } - -#if WASM_ENABLE_INTERP != 0 - if (module_inst->module_type == Wasm_Module_Bytecode) { - is_import_func = ((WASMFunctionInstance*)func)->is_import_func; - } -#endif -#if WASM_ENABLE_AOT != 0 - if (module_inst->module_type == Wasm_Module_AoT) { - is_import_func = ((AOTFunctionInstance*)func)->is_import_func; - } -#endif - - if (is_import_func) { - wasm_runtime_set_exception(module_inst, - "lookup main function failed"); - return false; - } - - module_type = module_inst->module_type; - func_type = wasm_runtime_get_function_type(func, module_type); - - if (!func_type) { - LOG_ERROR("invalid module instance type"); - return false; - } - - if (!check_main_func_type(func_type)) { - wasm_runtime_set_exception(module_inst, - "invalid function type of main function"); - return false; - } - - if (func_type->param_count) { - for (i = 0; i < argc; i++) - total_argv_size += (uint32)(strlen(argv[i]) + 1); - total_argv_size = align_uint(total_argv_size, 4); - - total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc; - - if (total_size >= UINT32_MAX - || !(argv_buf_offset = - wasm_runtime_module_malloc(module_inst, (uint32)total_size, - (void**)&argv_buf))) { - wasm_runtime_set_exception(module_inst, - "allocate memory failed"); - return false; - } - - p = argv_buf; - argv_offsets = (uint32*)(p + total_argv_size); - p_end = p + total_size; - - for (i = 0; i < argc; i++) { - bh_memcpy_s(p, (uint32)(p_end - p), argv[i], (uint32)(strlen(argv[i]) + 1)); - argv_offsets[i] = argv_buf_offset + (uint32)(p - argv_buf); - p += strlen(argv[i]) + 1; - } - - argc1 = 2; - argv1[0] = (uint32)argc; - argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets); - } - - ret = wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, - argc1, argv1); - if (argv_buf_offset) - wasm_runtime_module_free(module_inst, argv_buf_offset); - return ret; -} - -#if WASM_ENABLE_MULTI_MODULE != 0 -static WASMModuleInstance * -get_sub_module_inst(const WASMModuleInstance *parent_module_inst, - const char *sub_module_name) -{ - WASMSubModInstNode *node = - bh_list_first_elem(parent_module_inst->sub_module_inst_list); - - while (node && strcmp(node->module_name, sub_module_name)) { - node = bh_list_elem_next(node); - } - return node ? node->module_inst : NULL; -} - -static bool -parse_function_name(char *orig_function_name, char **p_module_name, - char **p_function_name) -{ - if (orig_function_name[0] != '$') { - *p_module_name = NULL; - *p_function_name = orig_function_name; - return true; - } - - /** - * $module_name$function_name\0 - * ===> - * module_name\0function_name\0 - * ===> - * module_name - * function_name - */ - char *p1 = orig_function_name; - char *p2 = strchr(p1 + 1, '$'); - if (!p2) { - LOG_DEBUG("can not parse the incoming function name"); - return false; - } - - *p_module_name = p1 + 1; - *p2 = '\0'; - *p_function_name = p2 + 1; - return strlen(*p_module_name) && strlen(*p_function_name); -} -#endif - -/** - * Implementation of wasm_application_execute_func() - */ - -static WASMFunctionInstanceCommon* -resolve_function(const WASMModuleInstanceCommon *module_inst, - const char *name) -{ - uint32 i = 0; - WASMFunctionInstanceCommon *ret = NULL; -#if WASM_ENABLE_MULTI_MODULE != 0 - WASMModuleInstance *sub_module_inst = NULL; - char *orig_name = NULL; - char *sub_module_name = NULL; - char *function_name = NULL; - uint32 length = (uint32)(strlen(name) + 1); - - orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0); - if (!orig_name) { - return NULL; - } - - strncpy(orig_name, name, length); - - if (!parse_function_name(orig_name, &sub_module_name, &function_name)) { - goto LEAVE; - } - - LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name); - - if (sub_module_name) { - sub_module_inst = get_sub_module_inst( - (WASMModuleInstance *)module_inst, sub_module_name); - if (!sub_module_inst) { - LOG_DEBUG("can not find a sub module named %s", sub_module_name); - goto LEAVE; - } - } -#else - const char *function_name = name; -#endif - -#if WASM_ENABLE_INTERP != 0 - if (module_inst->module_type == Wasm_Module_Bytecode) { - WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst; - -#if WASM_ENABLE_MULTI_MODULE != 0 - wasm_inst = sub_module_inst ? sub_module_inst : wasm_inst; -#endif /* WASM_ENABLE_MULTI_MODULE */ - - for (i = 0; i < wasm_inst->export_func_count; i++) { - if (!strcmp(wasm_inst->export_functions[i].name, function_name)) { - ret = wasm_inst->export_functions[i].function; - break; - } - } - } -#endif /* WASM_ENABLE_INTERP */ - -#if WASM_ENABLE_AOT != 0 - if (module_inst->module_type == Wasm_Module_AoT) { - AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst; - AOTFunctionInstance *export_funcs = (AOTFunctionInstance *) - aot_inst->export_funcs.ptr; - for (i = 0; i < aot_inst->export_func_count; i++) { - if (!strcmp(export_funcs[i].func_name, function_name)) { - ret = &export_funcs[i]; - break; - } - } - } -#endif - -#if WASM_ENABLE_MULTI_MODULE != 0 -LEAVE: - wasm_runtime_free(orig_name); -#endif - return ret; -} - -union ieee754_float { - float f; - - /* This is the IEEE 754 single-precision format. */ - union { - struct { - unsigned int negative:1; - unsigned int exponent:8; - unsigned int mantissa:23; - } ieee_big_endian; - struct { - unsigned int mantissa:23; - unsigned int exponent:8; - unsigned int negative:1; - } ieee_little_endian; - } ieee; -}; - -union ieee754_double { - double d; - - /* This is the IEEE 754 double-precision format. */ - union { - struct { - unsigned int negative:1; - unsigned int exponent:11; - /* Together these comprise the mantissa. */ - unsigned int mantissa0:20; - unsigned int mantissa1:32; - } ieee_big_endian; - - struct { - /* Together these comprise the mantissa. */ - unsigned int mantissa1:32; - unsigned int mantissa0:20; - unsigned int exponent:11; - unsigned int negative:1; - } ieee_little_endian; - } ieee; -}; - static union { int a; char b; @@ -2579,323 +2273,6 @@ static union { #define is_little_endian() (__ue.b == 1) -bool -wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, - const char *name, int32 argc, char *argv[]) -{ - WASMFunctionInstanceCommon *func; - WASMType *type = NULL; - uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0; - int32 i, p, module_type; - uint64 total_size; - const char *exception; - char buf[128]; - - bh_assert(argc >= 0); - LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc); - func = resolve_function(module_inst, name); - - if (!func) { - snprintf(buf, sizeof(buf), "lookup function %s failed", name); - wasm_runtime_set_exception(module_inst, buf); - goto fail; - } - -#if WASM_ENABLE_INTERP != 0 - if (module_inst->module_type == Wasm_Module_Bytecode) { - WASMFunctionInstance *wasm_func = (WASMFunctionInstance*)func; - if (wasm_func->is_import_func -#if WASM_ENABLE_MULTI_MODULE != 0 - && !wasm_func->import_func_inst -#endif - ) { - snprintf(buf, sizeof(buf), "lookup function %s failed", name); - wasm_runtime_set_exception(module_inst, buf); - goto fail; - } - } -#endif - - module_type = module_inst->module_type; - type = wasm_runtime_get_function_type(func, module_type); - - if (!type) { - LOG_ERROR("invalid module instance type"); - return false; - } - - if (type->param_count != (uint32)argc) { - wasm_runtime_set_exception(module_inst, - "invalid input argument count"); - goto fail; - } - - argc1 = type->param_cell_num; - cell_num = (argc1 > type->ret_cell_num) ? argc1 : type->ret_cell_num; - - total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2); - if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, - NULL, 0)))) { - goto fail; - } - - /* Parse arguments */ - for (i = 0, p = 0; i < argc; i++) { - char *endptr = NULL; - bh_assert(argv[i] != NULL); - if (argv[i][0] == '\0') { - snprintf(buf, sizeof(buf), "invalid input argument %d", i); - wasm_runtime_set_exception(module_inst, buf); - goto fail; - } - switch (type->types[i]) { - case VALUE_TYPE_I32: - argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0); - break; - case VALUE_TYPE_I64: - { - union { uint64 val; uint32 parts[2]; } u; - u.val = strtoull(argv[i], &endptr, 0); - argv1[p++] = u.parts[0]; - argv1[p++] = u.parts[1]; - break; - } - case VALUE_TYPE_F32: - { - float32 f32 = strtof(argv[i], &endptr); - if (isnan(f32)) { - if (argv[i][0] == '-') { - union ieee754_float u; - u.f = f32; - if (is_little_endian()) - u.ieee.ieee_little_endian.negative = 1; - else - u.ieee.ieee_big_endian.negative = 1; - memcpy(&f32, &u.f, sizeof(float)); - } - if (endptr[0] == ':') { - uint32 sig; - union ieee754_float u; - sig = (uint32)strtoul(endptr + 1, &endptr, 0); - u.f = f32; - if (is_little_endian()) - u.ieee.ieee_little_endian.mantissa = sig; - else - u.ieee.ieee_big_endian.mantissa = sig; - memcpy(&f32, &u.f, sizeof(float)); - } - } - memcpy(&argv1[p++], &f32, sizeof(float)); - break; - } - case VALUE_TYPE_F64: - { - union { float64 val; uint32 parts[2]; } u; - u.val = strtod(argv[i], &endptr); - if (isnan(u.val)) { - if (argv[i][0] == '-') { - union ieee754_double ud; - ud.d = u.val; - if (is_little_endian()) - ud.ieee.ieee_little_endian.negative = 1; - else - ud.ieee.ieee_big_endian.negative = 1; - memcpy(&u.val, &ud.d, sizeof(double)); - } - if (endptr[0] == ':') { - uint64 sig; - union ieee754_double ud; - sig = strtoull(endptr + 1, &endptr, 0); - ud.d = u.val; - if (is_little_endian()) { - ud.ieee.ieee_little_endian.mantissa0 = sig >> 32; - ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig; - } - else { - ud.ieee.ieee_big_endian.mantissa0 = sig >> 32; - ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig; - } - memcpy(&u.val, &ud.d, sizeof(double)); - } - } - argv1[p++] = u.parts[0]; - argv1[p++] = u.parts[1]; - break; - } -#if WASM_ENABLE_SIMD != 0 - case VALUE_TYPE_V128: - { - /* it likes 0x123\0x234 or 123\234 */ - /* retrive first i64 */ - *(uint64*)(argv1 + p) = strtoull(argv[i], &endptr, 0); - /* skip \ */ - endptr++; - /* retrive second i64 */ - *(uint64*)(argv1 + p + 2) = strtoull(endptr, &endptr, 0); - p += 4; - break; - } -#endif /* WASM_ENABLE_SIMD != 0 */ -#if WASM_ENABLE_REF_TYPES != 0 - case VALUE_TYPE_FUNCREF: - { - if (strncmp(argv[i], "null", 4) == 0 - || strncmp(argv[i], "NULL", 4) == 0) { - argv1[p++] = NULL_REF; - } - else { - argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0); - } - break; - } - case VALUE_TYPE_EXTERNREF: - { - if (strncmp(argv[i], "null", 4) == 0 - || strncmp(argv[i], "NULL", 4) == 0) { - argv1[p++] = NULL_REF; - } - else { - uint64 val = strtoull(argv[i], &endptr, 0); - void *extern_obj = (void *)(uintptr_t)val; - uint32 externref_idx; - - if (!wasm_externref_obj2ref(module_inst, extern_obj, - &externref_idx)) { - wasm_runtime_set_exception( - module_inst, "map extern object to ref failed"); - goto fail; - } - argv1[p++] = externref_idx; - } - break; - } -#endif /* WASM_ENABLE_REF_TYPES */ - default: - bh_assert(0); - break; - } - if (endptr && *endptr != '\0' && *endptr != '_') { - snprintf(buf, sizeof(buf), "invalid input argument %d: %s", - i, argv[i]); - wasm_runtime_set_exception(module_inst, buf); - goto fail; - } - } - bh_assert(p == (int32)argc1); - - wasm_runtime_set_exception(module_inst, NULL); - if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, - argc1, argv1)) { - goto fail; - } - - /* print return value */ - for (j = 0; j < type->result_count; j++) { - switch (type->types[type->param_count + j]) { - case VALUE_TYPE_I32: - { - os_printf("0x%x:i32", argv1[k]); - k++; - break; - } - case VALUE_TYPE_I64: - { - union { uint64 val; uint32 parts[2]; } u; - u.parts[0] = argv1[k]; - u.parts[1] = argv1[k + 1]; - k += 2; -#ifdef PRIx64 - os_printf("0x%"PRIx64":i64", u.val); -#else - char buf[16]; - if (sizeof(long) == 4) - snprintf(buf, sizeof(buf), "%s", "0x%llx:i64"); - else - snprintf(buf, sizeof(buf), "%s", "0x%lx:i64"); - os_printf(buf, u.val); -#endif - break; - } - case VALUE_TYPE_F32: - { - os_printf("%.7g:f32", *(float32*)(argv1 + k)); - k++; - break; - } - case VALUE_TYPE_F64: - { - union { float64 val; uint32 parts[2]; } u; - u.parts[0] = argv1[k]; - u.parts[1] = argv1[k + 1]; - k += 2; - os_printf("%.7g:f64", u.val); - break; - } -#if WASM_ENABLE_REF_TYPES - case VALUE_TYPE_FUNCREF: - { - if (argv1[k] != NULL_REF) - os_printf("%u:ref.func", argv1[k]); - else - os_printf("func:ref.null"); - k++; - break; - } - case VALUE_TYPE_EXTERNREF: - { - if (argv1[k] != NULL_REF) { - void *extern_obj = NULL; - bool ret = wasm_externref_ref2obj(argv1[k], &extern_obj); - bh_assert(ret); - (void)ret; - os_printf("%p:ref.extern", extern_obj); - } - else - os_printf("extern:ref.null"); - k++; - break; - } -#endif -#if WASM_ENABLE_SIMD != 0 - case VALUE_TYPE_V128: - { - uint64 *v = (uint64*)(argv1 + k); -#if defined(PRIx64) - os_printf("<0x%016"PRIx64" 0x%016"PRIx64">:v128", *v, *(v + 1)); -#else - if (4 == sizeof(long)) { - os_printf("<0x%016llx 0x%016llx>:v128", *v, *(v + 1)); - } - else { - os_printf("<0x%016lx 0x%016lx>:v128", *v, *(v + 1)); - } -#endif /* PRIx64 */ - k += 4; - break; - } -#endif /* WASM_ENABLE_SIMD != 0 */ - default: - bh_assert(0); - break; - } - if (j < (uint32)(type->result_count - 1)) - os_printf(","); - } - os_printf("\n"); - - wasm_runtime_free(argv1); - return true; - -fail: - if (argv1) - wasm_runtime_free(argv1); - - exception = wasm_runtime_get_exception(module_inst); - bh_assert(exception); - os_printf("%s\n", exception); - return false; -} - bool wasm_runtime_register_natives(const char *module_name, NativeSymbol *native_symbols, diff --git a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c index 57ba67cd9..cb21bac77 100644 --- a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c +++ b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c @@ -365,6 +365,8 @@ handle_1_to_9: float64 f64; char buf[16], *s; + /* Make 8-byte aligned */ + ap = (_va_list)(((uintptr_t)ap + 7) & ~(uintptr_t)7); CHECK_VA_ARG(ap, float64); f64 = _va_arg(ap, float64); snprintf(buf, sizeof(buf), "%f", f64); diff --git a/core/shared/mem-alloc/ems/ems_kfc.c b/core/shared/mem-alloc/ems/ems_kfc.c index 95664dc9a..905a4da27 100644 --- a/core/shared/mem-alloc/ems/ems_kfc.c +++ b/core/shared/mem-alloc/ems/ems_kfc.c @@ -46,13 +46,6 @@ gc_init_internal(gc_heap_t *heap, char *base_addr, gc_size_t heap_max_size) bh_assert(root->size <= HMU_FC_NORMAL_MAX_SIZE); -#if WASM_ENABLE_MEMORY_TRACING != 0 - os_printf("Heap created, total size: %u\n", buf_size); - os_printf(" heap struct size: %u\n", sizeof(gc_heap_t)); - os_printf(" actual heap size: %u\n", heap_max_size); - os_printf(" padding bytes: %u\n", - buf_size - sizeof(gc_heap_t) - heap_max_size); -#endif return heap; } @@ -74,6 +67,13 @@ gc_init_with_pool(char *buf, gc_size_t buf_size) base_addr = (char*) (((uintptr_t) base_addr + 7) & (uintptr_t)~7) + GC_HEAD_PADDING; heap_max_size = (uint32)(buf_end - base_addr) & (uint32)~7; +#if WASM_ENABLE_MEMORY_TRACING != 0 + os_printf("Heap created, total size: %u\n", buf_size); + os_printf(" heap struct size: %u\n", sizeof(gc_heap_t)); + os_printf(" actual heap size: %u\n", heap_max_size); + os_printf(" padding bytes: %u\n", + buf_size - sizeof(gc_heap_t) - heap_max_size); +#endif return gc_init_internal(heap, base_addr, heap_max_size); } @@ -110,6 +110,14 @@ gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size, heap_max_size = (uint32)(pool_buf_end - base_addr) & (uint32)~7; +#if WASM_ENABLE_MEMORY_TRACING != 0 + os_printf("Heap created, total size: %u\n", + struct_buf_size + pool_buf_size); + os_printf(" heap struct size: %u\n", sizeof(gc_heap_t)); + os_printf(" actual heap size: %u\n", heap_max_size); + os_printf(" padding bytes: %u\n", + pool_buf_size - heap_max_size); +#endif return gc_init_internal(heap, base_addr, heap_max_size); } diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 0127f4d1b..72eb1f16a 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -138,6 +138,12 @@ Currently we only profile the memory consumption of module, module_instance and #### **Enable reference types feature** - **WAMR_BUILD_REF_TYPES**=1/0, default to disable if not set +#### **Exclude WAMR application entry functions** +- **WAMR_DISABLE_APP_ENTRY**=1/0, default to disable if not set + +> Note: The WAMR application entry (`core/iwasm/common/wasm_application.c`) encapsulate some common process to instantiate, execute the wasm functions and print the results. Some platform related APIs are used in these functions, so you can enable this flag to exclude this file if your platform doesn't support those APIs. +> *Don't enable this flag if you are building `product-mini`* + **Combination of configurations:** We can combine the configurations. For example, if we want to disable interpreter, enable AOT and WASI, we can run command: diff --git a/product-mini/platforms/alios-things/aos.mk b/product-mini/platforms/alios-things/aos.mk index 4cdf95dda..09c636d68 100644 --- a/product-mini/platforms/alios-things/aos.mk +++ b/product-mini/platforms/alios-things/aos.mk @@ -94,6 +94,7 @@ $(NAME)_SOURCES := ${SHARED_ROOT}/platform/alios/alios_platform.c \ ${SHARED_ROOT}/utils/bh_vector.c \ ${SHARED_ROOT}/utils/runtime_timer.c \ ${IWASM_ROOT}/libraries/libc-builtin/libc_builtin_wrapper.c \ + ${IWASM_ROOT}/common/wasm_application.c \ ${IWASM_ROOT}/common/wasm_runtime_common.c \ ${IWASM_ROOT}/common/wasm_native.c \ ${IWASM_ROOT}/common/wasm_exec_env.c \ diff --git a/product-mini/platforms/esp-idf/sources.mk b/product-mini/platforms/esp-idf/sources.mk index c9e5ce0f8..cc5c4dcc1 100644 --- a/product-mini/platforms/esp-idf/sources.mk +++ b/product-mini/platforms/esp-idf/sources.mk @@ -41,6 +41,7 @@ override PROJECT_CSRC := $(PROJECT_CSRC) \ $(WAMR_SRC_ROOT)/core/shared/utils/bh_log.c \ $(WAMR_SRC_ROOT)/core/shared/utils/bh_vector.c \ $(WAMR_SRC_ROOT)/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c \ + $(WAMR_SRC_ROOT)/core/iwasm/common/wasm_application.c \ $(WAMR_SRC_ROOT)/core/iwasm/common/wasm_runtime_common.c \ $(WAMR_SRC_ROOT)/core/iwasm/common/wasm_exec_env.c \ $(WAMR_SRC_ROOT)/core/iwasm/common/wasm_native.c \ diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index 5fe31448a..9978553fe 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -180,6 +180,7 @@ CSRCS += nuttx_platform.c \ bh_read_file.c \ runtime_timer.c \ libc_builtin_wrapper.c \ + wasm_application.c \ wasm_runtime_common.c \ wasm_native.c \ wasm_exec_env.c \