Skip to content

Commit 3ad2b39

Browse files
committed
add test PAGESIZE macro in shared libaries
This includes a new `add_waslibc_shared_test` CMake function which builds the test executable using shared libraries linked via `wasm-tools component link`. There's probably a better way to express this in CMake, but this is the only one I was able to make work.
1 parent 80687da commit 3ad2b39

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

test/CMakeLists.txt

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,33 @@ target_compile_options(libc_test_support PRIVATE
6868
-Wno-sign-compare
6969
)
7070

71+
function(add_wasilibc_shared_flags target)
72+
add_dependencies(${target} sysroot builtins)
73+
# Using an `INTERFACE` looks to get what we want here which is to rebuild this
74+
# if the library changes but not actually add any arguments to the link-line.
75+
target_link_libraries(${target} INTERFACE c)
76+
endfunction()
77+
78+
if (NOT TARGET_TRIPLE MATCHES "-threads")
79+
add_library(libc_test_support_shared SHARED
80+
"${LIBC_TEST}/src/common/path.c"
81+
"${LIBC_TEST}/src/common/print.c"
82+
"${LIBC_TEST}/src/common/rand.c"
83+
"${LIBC_TEST}/src/common/utf8.c"
84+
)
85+
target_include_directories(libc_test_support_shared PUBLIC "${LIBC_TEST}/src/common")
86+
add_wasilibc_shared_flags(libc_test_support_shared)
87+
# Disable some warnings in this third-party code
88+
target_compile_options(libc_test_support_shared PRIVATE
89+
-fvisibility=default
90+
-fPIC
91+
-Wno-unused-variable
92+
-Wno-unused-parameter
93+
-Wno-unused-function
94+
-Wno-sign-compare
95+
)
96+
endif()
97+
7198
# Adds a new test executable to build
7299
#
73100
# * `executable_name` must be a unique name and valid cmake target name.
@@ -83,7 +110,7 @@ function(add_test_executable executable_name src)
83110
set(multiValueArgs LDFLAGS CFLAGS)
84111
cmake_parse_arguments(PARSE_ARGV 1 arg "${options}" "${oneValueArgs}" "${multiValueArgs}")
85112

86-
# Build the test exeutable itself and apply all custom options as applicable.
113+
# Build the test executable itself and apply all custom options as applicable.
87114
add_executable(${executable_name} ${src})
88115
clang_format_target(${executable_name})
89116
add_wasilibc_flags(${executable_name})
@@ -352,6 +379,40 @@ if (TARGET_TRIPLE MATCHES "-threads")
352379
target_compile_options(pthread_mutex_busywait.wasm PRIVATE -Wno-unused-variable)
353380
endif()
354381

382+
# ========= shared library tests ================================
383+
384+
function(add_wasilibc_shared_test test_file)
385+
cmake_path(REPLACE_EXTENSION test_file so OUTPUT_VARIABLE test_library)
386+
cmake_path(REPLACE_EXTENSION test_file wasm OUTPUT_VARIABLE test_name)
387+
set(test_file "${CMAKE_CURRENT_SOURCE_DIR}/src/${test_file}")
388+
389+
add_library(${test_library} SHARED ${test_file})
390+
target_compile_options(${test_library} PRIVATE -fPIC)
391+
target_link_libraries(${test_library} PRIVATE libc_test_support_shared)
392+
# Here we embed the component type custom section into `${test_library}` using
393+
# `wasm-tools component embed`, then generate the `${test_name}` file by
394+
# linking `${test_library}`, `liblibc_test_support_shared.so`, and `libc.so`
395+
# together using `wasm-tools component link`.
396+
#
397+
# TODO: There must be a better way to express this in CMAKE.
398+
add_custom_command(
399+
TARGET ${test_library}
400+
POST_BUILD
401+
COMMAND ${wasm_tools} component embed --world wasi:cli/command "${CMAKE_CURRENT_SOURCE_DIR}/../wasi/p2/wit" $<TARGET_FILE:${test_library}> -o $<TARGET_FILE:${test_library}>
402+
COMMAND ${wasm_tools} component link $<TARGET_FILE:${test_library}> "${SYSROOT_LIB}/libc.so" $<TARGET_FILE:libc_test_support_shared> -o ${test_name}
403+
DEPENDS engine
404+
)
405+
406+
add_test(
407+
NAME "${test_name}"
408+
COMMAND ${ENGINE} ${test_name}
409+
)
410+
endfunction()
411+
412+
if (WASI STREQUAL "p2")
413+
add_wasilibc_shared_test(malloc-shared.c)
414+
endif()
415+
355416
# ========= sockets-related tests ===============================
356417

357418
if (NOT (WASI STREQUAL "p1"))

test/src/malloc-shared.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "test.h"
2+
#include <stdbool.h>
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
#include <__macro_PAGESIZE.h>
6+
7+
__attribute__((__export_name__("wasi:cli/run@0.2.0#run")))
8+
bool exports_wasi_cli_run_run(void) {
9+
void* p = malloc(7777777);
10+
if (p == NULL) {
11+
t_error("malloc failed\n");
12+
} else {
13+
free(p);
14+
}
15+
16+
return t_status != 0;
17+
}

0 commit comments

Comments
 (0)