Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
wamr-export-test
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mohammad Mahhouk
wamr-export-test
Commits
14d764b6
Commit
14d764b6
authored
3 years ago
by
Mohammad Mahhouk
Browse files
Options
Downloads
Patches
Plain Diff
pcc webassembly example draft
parent
b997117b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CMakeLists.txt
+9
-2
9 additions, 2 deletions
CMakeLists.txt
export_key_value/export_key_value.cpp
+195
-0
195 additions, 0 deletions
export_key_value/export_key_value.cpp
wasm_apps/Makefile
+17
-0
17 additions, 0 deletions
wasm_apps/Makefile
wasm_apps/key_value.c
+43
-0
43 additions, 0 deletions
wasm_apps/key_value.c
with
264 additions
and
2 deletions
CMakeLists.txt
+
9
−
2
View file @
14d764b6
...
...
@@ -75,6 +75,7 @@ file(GLOB WASM_FILES
"
${
CMAKE_BINARY_DIR
}
/add.wasm"
"
${
CMAKE_BINARY_DIR
}
/multadd.wasm"
"
${
CMAKE_BINARY_DIR
}
/test.wasm"
"
${
CMAKE_BINARY_DIR
}
/key_value.wasm"
)
set_property
(
DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
...
...
@@ -83,7 +84,6 @@ set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
set
(
EXPORT_SOURCE_FILES export_natives/export.c
)
add_executable
(
export_test
${
EXPORT_SOURCE_FILES
}
)
target_include_directories
(
export_test PUBLIC
${
WAMR_EMBEDDING_API_INCLUDE_PATH
}
${
CMAKE_CURRENT_LIST_DIR
}
)
target_link_libraries
(
export_test PRIVATE vmlib pthread m
)
...
...
@@ -92,4 +92,11 @@ target_link_libraries(export_test PRIVATE vmlib pthread m)
set
(
MULTMOD_SOURCE_FILES multi_modules/multimod.c
)
add_executable
(
multimod_test
${
MULTMOD_SOURCE_FILES
}
)
target_include_directories
(
multimod_test PUBLIC
${
WAMR_EMBEDDING_API_INCLUDE_PATH
}
${
CMAKE_CURRENT_LIST_DIR
}
)
target_link_libraries
(
multimod_test PRIVATE vmlib pthread m
)
\ No newline at end of file
target_link_libraries
(
multimod_test PRIVATE vmlib pthread m
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-fPIE"
)
set
(
EXPORT_KEY_VALUE_SOURCE_FILES export_key_value/export_key_value.cpp
)
add_executable
(
export_key_value
${
EXPORT_KEY_VALUE_SOURCE_FILES
}
)
target_include_directories
(
export_key_value PUBLIC
${
WAMR_EMBEDDING_API_INCLUDE_PATH
}
${
CMAKE_CURRENT_LIST_DIR
}
)
target_link_libraries
(
export_key_value PRIVATE vmlib pthread m
)
This diff is collapsed.
Click to expand it.
export_key_value/export_key_value.cpp
0 → 100644
+
195
−
0
View file @
14d764b6
#include
<cstdlib>
#include
<stdlib.h>
#include
<stdio.h>
#include
<stdint.h>
#include
<string.h>
#include
"wasm_export.h"
#include
<vector>
std
::
vector
<
char
*>
int_string_map
=
{};
int
get_key_value
(
wasm_exec_env_t
exec_env
,
int
index
)
{
if
(
index
>=
int_string_map
.
size
())
{
printf
(
"Error(get_key_value): index out of bound
\n
"
);
exit
(
EXIT_FAILURE
);
}
wasm_module_inst_t
module_inst
=
get_module_inst
(
exec_env
);
wasm_function_inst_t
func
=
wasm_runtime_lookup_function
(
module_inst
,
"module_malloc"
,
"(i)i"
);
uint32_t
args
[
2
]
=
{
0
};
args
[
0
]
=
strlen
(
int_string_map
[
index
]);
if
(
wasm_runtime_call_wasm
(
exec_env
,
func
,
1
,
args
)
)
{
char
*
buffer
=
(
char
*
)
wasm_runtime_addr_app_to_native
(
module_inst
,
args
[
0
]);
strcpy
(
buffer
,
int_string_map
[
index
]);
return
args
[
0
];
}
else
{
/* exception is thrown if call fails */
printf
(
"%s
\n
"
,
wasm_runtime_get_exception
(
module_inst
));
exit
(
EXIT_FAILURE
);
}
}
void
set_key_value
(
wasm_exec_env_t
exec_env
,
int
index
,
char
*
value
)
{
if
(
index
>
int_string_map
.
size
())
{
printf
(
"error index out of bound!
\n
"
);
exit
(
EXIT_FAILURE
);
}
int_string_map
.
insert
(
int_string_map
.
begin
()
+
index
,
value
);
}
char
*
read_wasm_binary_to_buffer
(
const
char
*
path
,
uint32_t
*
size
)
{
FILE
*
fd
=
fopen
(
path
,
"rb"
);
if
(
!
fd
)
{
fprintf
(
stderr
,
"failed to open the %s file
\n
"
,
path
);
perror
(
"fopen failed!
\n
"
);
return
NULL
;
}
if
(
fseek
(
fd
,
0
,
SEEK_END
)
!=
0
)
{
perror
(
"fseek failed
\n
"
);
return
NULL
;
}
long
fsize
=
ftell
(
fd
);
if
(
fseek
(
fd
,
0
,
SEEK_SET
)
!=
0
)
// rewind(fd); can be used here too
{
perror
(
"fseek failed
\n
"
);
return
NULL
;
}
char
*
buffer
=
(
char
*
)
malloc
(
fsize
+
1
);
if
(
!
buffer
)
{
perror
(
"malloc for file buffer failed!
\n
"
);
return
NULL
;
}
size_t
read_bytes
=
fread
(
buffer
,
1
,
fsize
,
fd
);
if
(
read_bytes
!=
(
size_t
)
fsize
)
{
free
(
buffer
);
fprintf
(
stderr
,
"file read didnt read the whole file size and returned only %zu instead of %zu
\n
"
,
read_bytes
,
fsize
);
exit
(
EXIT_FAILURE
);
}
fclose
(
fd
);
*
size
=
fsize
;
//+ 1;
//buffer[fsize] = 0;
return
buffer
;
}
int
main
(
int
argc
,
char
*
argv
[])
{
static
NativeSymbol
native_symbols
[]
=
{
{
"set_key_value"
,
// the name of WASM function name
(
void
*
)
set_key_value
,
// the native function pointer
"(i$)"
,
// the function prototype signature
NULL
,
// no attachments
},
{
"get_key_value"
,
// the name of WASM function name
(
void
*
)
get_key_value
,
// the native function pointer
"(i)i"
,
// the function prototype signature
NULL
,
// no attachments
}
};
char
*
buffer
,
error_buf
[
128
];
wasm_module_t
module
;
wasm_module_inst_t
module_inst
;
wasm_function_inst_t
func
;
wasm_exec_env_t
exec_env
;
uint32_t
size
,
stack_size
=
8092
,
heap_size
=
8092
;
/* initialize the wasm runtime by default configurations */
if
(
!
wasm_runtime_init
())
{
fprintf
(
stderr
,
"wasm runtime initialisation failed
\n
"
);
exit
(
EXIT_FAILURE
);
}
/* read WASM file into a memory buffer */
buffer
=
read_wasm_binary_to_buffer
(
argv
[
1
],
&
size
);
if
(
buffer
==
NULL
)
{
fprintf
(
stderr
,
"the returned buffer for reading the wasm module is null
\n
"
);
exit
(
EXIT_FAILURE
);
}
/* add line below if we want to export native functions to WASM app */
int
n_native_symbols
=
sizeof
(
native_symbols
)
/
sizeof
(
NativeSymbol
);
if
(
!
wasm_runtime_register_natives
(
"env"
,
native_symbols
,
n_native_symbols
))
{
free
(
buffer
);
exit
(
EXIT_FAILURE
);
}
/* parse the WASM file from buffer and create a WASM module */
module
=
wasm_runtime_load
((
uint8_t
*
)
buffer
,
size
,
error_buf
,
sizeof
(
error_buf
));
if
(
module
==
NULL
)
{
fprintf
(
stderr
,
"failed to load the module %s from the buffer with the error %s
\n
"
,
argv
[
1
],
error_buf
);
exit
(
EXIT_FAILURE
);
}
/* create an instance of the WASM module (WASM linear memory is ready) */
module_inst
=
wasm_runtime_instantiate
(
module
,
stack_size
,
heap_size
,
error_buf
,
sizeof
(
error_buf
));
if
(
module_inst
==
NULL
)
{
fprintf
(
stderr
,
"module instantiation failed!
\n
"
);
exit
(
EXIT_FAILURE
);
}
/* lookup a WASM function by its name
The function signature can NULL here */
func
=
wasm_runtime_lookup_function
(
module_inst
,
"main"
,
"()i"
);
if
(
func
==
NULL
)
{
printf
(
"multadd function not found
\n
"
);
return
-
1
;
}
/* creat an execution environment to execute the WASM functions */
exec_env
=
wasm_runtime_create_exec_env
(
module_inst
,
stack_size
);
if
(
exec_env
==
NULL
)
{
printf
(
"execution environmnet creation failed
\n
"
);
return
-
1
;
}
uint32_t
args
[
1
]
=
{
0
};
if
(
wasm_runtime_call_wasm
(
exec_env
,
func
,
0
,
args
)
)
{
/* the return value is stored in argv[0] */
//printf("the main function returned %ld \n", args[0]);
}
else
{
/* exception is thrown if call fails */
printf
(
"%s
\n
"
,
wasm_runtime_get_exception
(
module_inst
));
}
wasm_runtime_destroy
();
return
0
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
wasm_apps/Makefile
0 → 100644
+
17
−
0
View file @
14d764b6
C
:=
../wasi-sdk/bin/clang
#CFLAGS := --sysroot=../wasi-sdk-12.0/share/wasi-sysroot -Wl,--no-entry,--allow-undefined,--export-all -O3
CFLAGS
:=
--sysroot
=
../wasi-sdk/share/wasi-sysroot
-Wl
,--no-entry
-O3
.PHONY
:
test add multadd key_value all
add
:
add.wasm
multadd
:
multadd.wasm
test
:
test.wasm
key_value
:
key_value.wasm
all
:
test add multadd key_value
clean
:
rm
-f
*
.wasm
%.wasm
:
%.c
$(
C
)
$(
CFLAGS
)
-o
$@
$<
This diff is collapsed.
Click to expand it.
wasm_apps/key_value.c
0 → 100644
+
43
−
0
View file @
14d764b6
#include
<stdio.h>
#include
<stdlib.h>
#define import(name) __attribute__((import_name(name)))
#define export(name) __attribute__((export_name(name))) __attribute__((visibility("default")))
import
(
"get_key_value"
)
extern
char
*
get_key_value
(
int
index
);
import
(
"set_key_value"
)
extern
void
set_key_value
(
int
index
,
char
*
value
);
export
(
"module_malloc"
)
char
*
module_malloc
(
int
size
){
if
(
size
<=
0
)
return
NULL
;
char
*
result
=
(
char
*
)
malloc
(
size
);
return
result
==
NULL
?
NULL
:
result
;
}
export
(
"main"
)
int
main
(){
set_key_value
(
0
,
"moeeeee"
);
set_key_value
(
1
,
"kai"
);
set_key_value
(
2
,
"ok"
);
char
*
result
=
get_key_value
(
0
);
printf
(
"getting index 0 is %s
\n
"
,
result
);
free
(
result
);
result
=
get_key_value
(
1
);
printf
(
"getting index 1 is %s
\n
"
,
result
);
free
(
result
);
result
=
get_key_value
(
2
);
printf
(
"getting index 2 is %s
\n
"
,
result
);
free
(
result
);
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment