Implement apis to set and get the name of a wasm module (#3254)
Add API wasm_runtime_set_module_name and wasm_runtime_get_module_name, and by default, a module's name is "" if the set module name api isn't called.
This commit is contained in:
@ -4400,3 +4400,154 @@ wasm_propagate_wasi_args(WASMModule *module)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool
|
||||
wasm_check_utf8_str(const uint8 *str, uint32 len)
|
||||
{
|
||||
/* The valid ranges are taken from page 125, below link
|
||||
https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
|
||||
const uint8 *p = str, *p_end = str + len;
|
||||
uint8 chr;
|
||||
|
||||
while (p < p_end) {
|
||||
chr = *p;
|
||||
|
||||
if (chr == 0) {
|
||||
LOG_WARNING(
|
||||
"LIMITATION: a string which contains '\\00' is unsupported");
|
||||
return false;
|
||||
}
|
||||
else if (chr < 0x80) {
|
||||
p++;
|
||||
}
|
||||
else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
|
||||
if (p[1] < 0x80 || p[1] > 0xBF) {
|
||||
return false;
|
||||
}
|
||||
p += 2;
|
||||
}
|
||||
else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
|
||||
if (chr == 0xE0) {
|
||||
if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (chr == 0xED) {
|
||||
if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else { /* chr >= 0xE1 && chr <= 0xEF */
|
||||
if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
p += 3;
|
||||
}
|
||||
else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
|
||||
if (chr == 0xF0) {
|
||||
if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
|
||||
|| p[3] < 0x80 || p[3] > 0xBF) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (chr <= 0xF3) { /* and also chr >= 0xF1 */
|
||||
if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
|
||||
|| p[3] < 0x80 || p[3] > 0xBF) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else { /* chr == 0xF4 */
|
||||
if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
|
||||
|| p[3] < 0x80 || p[3] > 0xBF) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
p += 4;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return (p == p_end);
|
||||
}
|
||||
|
||||
char *
|
||||
wasm_const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
|
||||
bool is_load_from_file_buf, char *error_buf,
|
||||
uint32 error_buf_size)
|
||||
{
|
||||
StringNode *node, *node_next;
|
||||
|
||||
if (!wasm_check_utf8_str(str, len)) {
|
||||
set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (len == 0) {
|
||||
return "";
|
||||
}
|
||||
else if (is_load_from_file_buf) {
|
||||
/* As the file buffer can be referred to after loading, we use
|
||||
the previous byte of leb encoded size to adjust the string:
|
||||
move string 1 byte backward and then append '\0' */
|
||||
char *c_str = (char *)str - 1;
|
||||
bh_memmove_s(c_str, len + 1, c_str + 1, len);
|
||||
c_str[len] = '\0';
|
||||
return c_str;
|
||||
}
|
||||
|
||||
/* Search const str list */
|
||||
node = module->const_str_list;
|
||||
while (node) {
|
||||
node_next = node->next;
|
||||
if (strlen(node->str) == len && !memcmp(node->str, str, len))
|
||||
break;
|
||||
node = node_next;
|
||||
}
|
||||
|
||||
if (node) {
|
||||
return node->str;
|
||||
}
|
||||
|
||||
if (!(node = runtime_malloc(sizeof(StringNode) + len + 1, error_buf,
|
||||
error_buf_size))) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->str = ((char *)node) + sizeof(StringNode);
|
||||
bh_memcpy_s(node->str, len + 1, str, len);
|
||||
node->str[len] = '\0';
|
||||
|
||||
if (!module->const_str_list) {
|
||||
/* set as head */
|
||||
module->const_str_list = node;
|
||||
node->next = NULL;
|
||||
}
|
||||
else {
|
||||
/* insert it */
|
||||
node->next = module->const_str_list;
|
||||
module->const_str_list = node;
|
||||
}
|
||||
|
||||
return node->str;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_set_module_name(WASMModule *module, const char *name, char *error_buf,
|
||||
uint32_t error_buf_size)
|
||||
{
|
||||
if (!name)
|
||||
return false;
|
||||
|
||||
module->name =
|
||||
wasm_const_str_list_insert((const uint8 *)name, strlen(name), module,
|
||||
false, error_buf, error_buf_size);
|
||||
return module->name != NULL;
|
||||
}
|
||||
|
||||
const char *
|
||||
wasm_get_module_name(WASMModule *module)
|
||||
{
|
||||
return module->name;
|
||||
}
|
||||
Reference in New Issue
Block a user