Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion libc-bottom-half/headers/public/__macro_PAGESIZE.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* consideration. POSIX has deprecated `getpagesize` in favor of
* `sysconf(_SC_PAGESIZE)` which does not have this problem.
*/
#if __clang_major__ >= 22
// FIXME(#778): `__wasm_first_page_end` is broken for PIC as of this writing and
// will likely require `wasm-ld` changes, at which point we can re-enable it.
#if __clang_major__ >= 22 && !defined __pic__
extern char __wasm_first_page_end;
#define PAGESIZE ((unsigned long)&__wasm_first_page_end)
#else
Expand Down
80 changes: 69 additions & 11 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,47 @@ function(add_wasilibc_flags target)
endif()
endfunction()

add_library(libc_test_support STATIC
function(add_wasilibc_shared_flags target)
add_dependencies(${target} sysroot builtins)
# Using an `INTERFACE` looks to get what we want here which is to rebuild this
# if the library changes but not actually add any arguments to the link-line.
target_link_libraries(${target} INTERFACE c)
endfunction()

function(add_library_pair name)
add_library(${name} STATIC ${ARGN})

if (NOT TARGET_TRIPLE MATCHES "-threads")
add_library(${name}_shared SHARED ${ARGN})
set_pic(${name}_shared)
endif()
endfunction()

add_library_pair(libc_test_support
"${LIBC_TEST}/src/common/path.c"
"${LIBC_TEST}/src/common/print.c"
"${LIBC_TEST}/src/common/rand.c"
"${LIBC_TEST}/src/common/utf8.c"
)
target_include_directories(libc_test_support PUBLIC "${LIBC_TEST}/src/common")
add_wasilibc_flags(libc_test_support)
# Disable some warnings in this third-party code
target_compile_options(libc_test_support PRIVATE
-Wno-unused-variable
-Wno-unused-parameter
-Wno-unused-function
-Wno-sign-compare
)

function(configure_libc_test_support suffix)
# Disable some warnings in this third-party code
target_include_directories(libc_test_support${suffix} PUBLIC "${LIBC_TEST}/src/common")
cmake_language(CALL add_wasilibc${suffix}_flags libc_test_support${suffix})
target_compile_options(libc_test_support${suffix} PRIVATE
-Wno-unused-variable
-Wno-unused-parameter
-Wno-unused-function
-Wno-sign-compare
)
endfunction()

configure_libc_test_support("")

if (NOT TARGET_TRIPLE MATCHES "-threads")
configure_libc_test_support("_shared")
target_compile_options(libc_test_support_shared PRIVATE -fvisibility=default)
endif()
Comment on lines +55 to +95
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid duplicating the above here I might recommend poking around add_internal_library_pair and doing something similar here. Basically building two libraries, one with -fPIC one without, and then linking to the "right one". A small loop could handle defining options for both of those targets.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed an update; let me know if you think more needs to be done with this.


# Adds a new test executable to build
#
Expand All @@ -83,7 +109,7 @@ function(add_test_executable executable_name src)
set(multiValueArgs LDFLAGS CFLAGS)
cmake_parse_arguments(PARSE_ARGV 1 arg "${options}" "${oneValueArgs}" "${multiValueArgs}")

# Build the test exeutable itself and apply all custom options as applicable.
# Build the test executable itself and apply all custom options as applicable.
add_executable(${executable_name} ${src})
clang_format_target(${executable_name})
add_wasilibc_flags(${executable_name})
Expand Down Expand Up @@ -352,6 +378,38 @@ if (TARGET_TRIPLE MATCHES "-threads")
target_compile_options(pthread_mutex_busywait.wasm PRIVATE -Wno-unused-variable)
endif()

# ========= shared library tests ================================

function(add_wasilibc_shared_test test_file)
cmake_path(REPLACE_EXTENSION test_file so OUTPUT_VARIABLE test_library)
cmake_path(REPLACE_EXTENSION test_file wasm OUTPUT_VARIABLE test_name)
set(test_file "${CMAKE_CURRENT_SOURCE_DIR}/src/${test_file}")

add_library(${test_library} SHARED ${test_file})
set_pic(${test_library})
target_link_libraries(${test_library} PRIVATE libc_test_support_shared)
# Here we embed the component type custom section into `${test_library}` using
# `wasm-tools component embed`, then generate the `${test_name}` file by
# linking `${test_library}`, `liblibc_test_support_shared.so`, and `libc.so`
# together using `wasm-tools component link`.
add_custom_command(
TARGET ${test_library}
POST_BUILD
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}>
COMMAND ${wasm_tools} component link $<TARGET_FILE:${test_library}> "${SYSROOT_LIB}/libc.so" $<TARGET_FILE:libc_test_support_shared> -o ${test_name}
DEPENDS wasm_tools engine
)
Comment on lines +388 to +401
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compilation-wise it might be easier to compile an executable with clang but pass -Wl,--skip-wit-component and then afterwards manually invoke the wasm-tools component link command. That should skip the embed part here and also look more like an executable test file

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per your later comment it sounds like it's okay to leave this as-is and just remove the TODO part, correct?


add_test(
NAME "${test_name}"
COMMAND ${ENGINE} ${test_name}
)
endfunction()

if (WASI STREQUAL "p2")
add_wasilibc_shared_test(malloc-shared.c)
endif()

# ========= sockets-related tests ===============================

if (NOT (WASI STREQUAL "p1"))
Expand Down
17 changes: 17 additions & 0 deletions test/src/malloc-shared.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "test.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <__macro_PAGESIZE.h>

__attribute__((__export_name__("wasi:cli/run@0.2.0#run")))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this mean to bypass crt startup? if so, it seems like too much assumptions on malloc implementation.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't intended to do that, no. Functions such as __wasm_call_ctors, __wasm_apply_data_relocs, etc. will still be run; that's taken care of by wasm-tools compose link.

This is just a way to build an executable component from shared libraries, since ctest wants to run executables rather than shared libraries. There might be a better way to do that, but this seems to work fine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. thank you for explanation.

bool exports_wasi_cli_run_run(void) {
void* p = malloc(7777777);
if (p == NULL) {
t_error("malloc failed\n");
} else {
free(p);
}

return t_status != 0;
}