Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Darwin should also use vfork not fork. #1032

Merged
merged 9 commits into from
Dec 16, 2024
Merged
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
8 changes: 8 additions & 0 deletions examples/0013.graph/.test_prop.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
["shortest_path"] # TODO: runtime failed. why?
ignore = true

["strongly_connected_component"] # TODO: runtime failed. why?
ignore = true

["topological_sorting"] # TODO: runtime failed. why?
ignore = true
47 changes: 27 additions & 20 deletions include/fast_io_hosted/process/process/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace fast_io
namespace posix
{
#if defined(__DARWIN_C_LEVEL) || defined(__MSDOS__)
// extern int libc_faccessat(int dirfd, char const *pathname, int mode, int flags) noexcept __asm__("_faccessat");
extern int libc_fexecve(int fd, char *const *argv, char *const *envp) noexcept __asm__("_fexecve");
extern int libc_kill(pid_t pid, int sig) noexcept __asm__("_kill");
extern pid_t libc_fork() noexcept __asm__("_fork");
Expand All @@ -18,7 +17,6 @@ extern pid_t libc_waitpid(pid_t pid, int *status, int options) noexcept __asm__(
[[noreturn]] extern void libc_exit(int status) noexcept __asm__("__Exit");
[[noreturn]] extern void libc_exit2(int status) noexcept __asm__("__exit");
#else
// extern int libc_faccessat(int dirfd, char const *pathname, int mode, int flags) noexcept __asm__("faccessat");
extern int libc_fexecve(int fd, char *const *argv, char *const *envp) noexcept __asm__("fexecve");
extern int libc_kill(pid_t pid, int sig) noexcept __asm__("kill");
extern pid_t libc_fork() noexcept __asm__("fork");
Expand All @@ -33,6 +31,7 @@ struct posix_wait_status
{
int wait_loc{};
};

#if 0
inline constexpr posix_wait_reason reason(posix_wait_status pws) noexcept
{
Expand Down Expand Up @@ -63,6 +62,7 @@ inline constexpr int native_code(posix_wait_status pws) noexcept
inline constexpr ::std::uintmax_t code(posix_wait_status pws) noexcept
{
return static_cast<::std::uintmax_t>(pws.wait_loc);
}

template<::std::integral char_type>
inline constexpr ::std::size_t print_reserve_size(io_reserve_type_t<char_type,posix_wait_status>) noexcept
Expand Down Expand Up @@ -123,7 +123,7 @@ inline posix_wait_status posix_waitpid(pid_t pid)
#if defined(__linux__) && defined(__NR_wait4)
system_call_throw_error(system_call<__NR_wait4, int>(pid, __builtin_addressof(status.wait_loc), 0, nullptr));
#else
if (::fast_io::posix::libc_waitpid(pid, __builtin_addressof(status.wait_loc), 0) == -1)
if (::fast_io::posix::libc_waitpid(pid, __builtin_addressof(status.wait_loc), 0) == -1) [[unlikely]]
{
throw_posix_error();
}
Expand Down Expand Up @@ -178,12 +178,12 @@ struct io_redirector
{
return_code rc;
rc = redirect(0, pio.in);
if (rc.error)
if (rc.error) [[unlikely]]
{
return rc;
}
rc = redirect(1, pio.out);
if (rc.error)
if (rc.error) [[unlikely]]
{
return rc;
}
Expand All @@ -193,7 +193,7 @@ struct io_redirector

inline return_code redirect(int target_fd, posix_io_redirection const &d) noexcept
{
if (!d)
if (!d) [[unlikely]]
{
return {};
}
Expand All @@ -204,7 +204,7 @@ struct io_redirector
// the read/write ends of pipe are all open
// the user shouldn't close them if they pass entire pipe as argument
rc = sys_dup2_nothrow(d.pipe_fds[is_stdin ? 0 : 1], target_fd);
if (rc.error)
if (rc.error) [[unlikely]]
{
return rc;
}
Expand All @@ -219,13 +219,14 @@ struct io_redirector
{
rc = sys_dup2_nothrow(d.fd, target_fd);
}
if (rc.error)
if (rc.error) [[unlikely]]
{
return rc;
}
return {};
}

#if 0
// only used by parent process
static inline void close_pipe_ends(int target_fd, posix_io_redirection const &d) noexcept
{
Expand All @@ -240,8 +241,8 @@ struct io_redirector
bool const is_stdin{target_fd == 0};
sys_close(d.pipe_fds[is_stdin ? 0 : 1]);
}
#endif

private:
inline int devnull()
{
if (fd_devnull != -1)
Expand Down Expand Up @@ -319,7 +320,7 @@ inline pid_t pipefork_execveat_common_impl(int dirfd, char const *cstr, char con
}
else
{
posix_waitpid_noexcept(pid);
::fast_io::details::posix_waitpid_noexcept(pid);
if (n == err_buffer_end)
{
throw_posix_error(errno_from_subproc);
Expand Down Expand Up @@ -423,7 +424,6 @@ struct fd_remapper
}
}

private:
inline int devnull()
{
if (fd_devnull != -1)
Expand Down Expand Up @@ -453,23 +453,27 @@ inline void execveat_inside_vfork(int dirfd, char const *cstr, char const *const
{
t_errno = 0;
}
#if defined(__linux__)
#ifdef __NR_exit_group
#if defined(__NR_exit_group)
::fast_io::system_call_no_return<__NR_exit_group>(127);
#else
::fast_io::system_call_no_return<__NR_exit>(127);
#endif
#else
::fast_io::posix::libc_exit2(127);
#endif
#else
int fd{::fast_io::details::my_posix_openat_noexcept(dirfd, cstr, O_RDONLY | O_NOFOLLOW, 0644)};
if (fd != -1) [[likely]]
{
::fast_io::posix::libc_fexecve(fd, const_cast<char *const *>(args), const_cast<char *const *>(envp));
}
t_errno = errno;
#if defined(__linux__)
#if defined(__NR_exit_group)
::fast_io::system_call_no_return<__NR_exit_group>(127);
#else
::fast_io::system_call_no_return<__NR_exit>(127);
#endif
#else
::fast_io::posix::libc_exit2(127);
#endif
#endif
__builtin_unreachable();
}
Expand Down Expand Up @@ -604,7 +608,8 @@ class posix_process : public posix_process_observer
inline posix_process(posix_at_entry pate, path_type const &filename, posix_process_args const &args,
posix_process_envs const &envp, posix_process_io const &pio)
: posix_process_observer{
#ifdef __DARWIN_C_LEVEL
// #ifdef __DARWIN_C_LEVEL
#if 0
::fast_io::details::pipefork_execveat_impl(pate.fd, filename, args.get(), envp.get(), pio)
#else
::fast_io::details::vfork_execveat_impl(pate.fd, filename, args.get(), envp.get(), pio)
Expand All @@ -617,7 +622,8 @@ class posix_process : public posix_process_observer
inline posix_process(path_type const &filename, posix_process_args const &args, posix_process_envs const &envp,
posix_process_io const &pio)
: posix_process_observer{
#ifdef __DARWIN_C_LEVEL
// #ifdef __DARWIN_C_LEVEL
#if 0
::fast_io::details::pipefork_execve_impl(filename, args.get(), envp.get(), pio)
#else
::fast_io::details::vfork_execve_impl(filename, args.get(), envp.get(), pio)
Expand All @@ -629,7 +635,8 @@ class posix_process : public posix_process_observer
inline posix_process(::fast_io::posix_fs_dirent ent, posix_process_args const &args, posix_process_envs const &envp,
posix_process_io const &pio)
: posix_process_observer{
#ifdef __DARWIN_C_LEVEL
// #ifdef __DARWIN_C_LEVEL
#if 0
::fast_io::details::pipefork_execveat_common_impl(ent.fd, ent.filename, args.get(), envp.get(), pio)
#else
::fast_io::details::vfork_execveat_common_impl(ent.fd, ent.filename, args.get(), envp.get(), pio)
Expand Down Expand Up @@ -658,7 +665,7 @@ class posix_process : public posix_process_observer
}
inline ~posix_process()
{
details::posix_waitpid_noexcept(this->pid);
::fast_io::details::posix_waitpid_noexcept(this->pid);
this->pid = -1;
}
};
Expand Down
1 change: 0 additions & 1 deletion include/fast_io_legacy_impl/c/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,6 @@ using c_file_factory_unlocked = c_family_file_factory<c_family::native_unlocked>

namespace freestanding
{

template <c_family fm, ::std::integral char_type>
struct is_trivially_relocatable<basic_c_family_file<fm, char_type>>
{
Expand Down
25 changes: 14 additions & 11 deletions tests/0000.tests_prebuild/gentests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,24 +281,24 @@ inline void parse_prop_files(fast_io::native_file_loader &&file, file_property_t
}
}

inline bool gen_cmake_file(fast_io::native_io_observer nio, std::u8string_view prefix, file_entry_t prop);
inline bool gen_cmake_file(fast_io::native_io_observer nio, std::u8string_view prefix, file_entry_t prop, bool test);

inline void cmake_gen_rec_dir_def(fast_io::u8obuf_file &cmake_file, fast_io::native_io_observer nio, std::u8string_view filename, std::u8string_view prefix, file_entry_t const &prop)
inline void cmake_gen_rec_dir_def(fast_io::u8obuf_file &cmake_file, fast_io::native_io_observer nio, std::u8string_view filename, std::u8string_view prefix, file_entry_t const &prop, bool test)
{
if (prop.ignore)
{
return;
}
std::u8string new_prefix{fast_io::u8concat_std(prefix, filename, u8".")};
fast_io::dir_file dir{at(nio), filename};
if (!gen_cmake_file(dir, new_prefix, prop))
if (!gen_cmake_file(dir, new_prefix, prop, test))
{
return;
}
print(cmake_file, u8"include(${CMAKE_CURRENT_LIST_DIR}/", filename, u8"/CMakeLists.txt)\n");
}

inline void cmake_gen_file_def(fast_io::u8obuf_file &cmake_file, std::u8string_view targetname, auto &&filename, file_entry_t const &prop)
inline void cmake_gen_file_def(fast_io::u8obuf_file &cmake_file, std::u8string_view targetname, auto &&filename, file_entry_t const &prop, bool test)
{
if (prop.ignore || prop.will_fail == will_fail_t::compiletime)
{
Expand Down Expand Up @@ -328,13 +328,16 @@ inline void cmake_gen_file_def(fast_io::u8obuf_file &cmake_file, std::u8string_v
print(cmake_file, u8"set_target_properties(", targetname, u8" PROPERTIES CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -fsanitize=address\")\n");
}
}
print(cmake_file, u8"add_test(", targetname, u8" ", targetname, u8")\n");
if (test)
{
print(cmake_file, u8"add_test(", targetname, u8" ", targetname, u8")\n");
}
// // set_tests_properties does not work. why?
// if (prop.will_fail == will_fail_t::runtime)
// print(cmake_file, u8"set_tests_properties(", targetname, u8" PROPERTIES WILL_FAIL TRUE)\n");
}

inline bool gen_cmake_file(fast_io::native_io_observer nio, std::u8string_view prefix, file_entry_t prop)
inline bool gen_cmake_file(fast_io::native_io_observer nio, std::u8string_view prefix, file_entry_t prop, bool test)
{
bool generated{};
fast_io::u8obuf_file cmake_file;
Expand Down Expand Up @@ -375,14 +378,14 @@ inline bool gen_cmake_file(fast_io::native_io_observer nio, std::u8string_view p
{
using enum fast_io::file_type;
case directory:
cmake_gen_rec_dir_def(cmake_file, nio, filename, prefix, curr_prop);
cmake_gen_rec_dir_def(cmake_file, nio, filename, prefix, curr_prop, test);
generated = true;
break;
default:
std::u8string_view ext{u8extension(entry)};
if (ext == u8".cpp" || ext == u8".cc")
{
cmake_gen_file_def(cmake_file, fast_io::u8concat_std(prefix, u8stem(entry)), filename, curr_prop);
cmake_gen_file_def(cmake_file, fast_io::u8concat_std(prefix, u8stem(entry)), filename, curr_prop, test);
generated = true;
}
break;
Expand Down Expand Up @@ -439,7 +442,7 @@ int main(int argc, char *argv[])
fast_io::dir_file tests_dir{R"(./tests)"};
fast_io::dir_file examples_dir{R"(./examples)"};
fast_io::dir_file benchmark_dir{R"(./benchmark)"};
gen_cmake_file(tests_dir, u8"tests.", {});
gen_cmake_file(examples_dir, u8"examples.", {});
gen_cmake_file(benchmark_dir, u8"benchmark.", {});
gen_cmake_file(tests_dir, u8"tests.", {}, true);
gen_cmake_file(examples_dir, u8"examples.", {}, true);
gen_cmake_file(benchmark_dir, u8"benchmark.", {}, false);
}
3 changes: 3 additions & 0 deletions tests/0029.decorators/.test_prop.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
["decopartial.cc"] # TODO: runtime failed. why?
ignore = true

["decopartialiobuf.cc"] # TODO: runtime failed. why?
ignore = true
2 changes: 1 addition & 1 deletion tests/0035.install_path/argv0_install_path.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <fast_io.h>
#include <fast_io_device.h>
#include <fast_io_tools/install_path.h>
#include <fast_io_driver/install_path.h>

int main(int argc, char**argv)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/0035.install_path/install_path.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <fast_io.h>
#include <fast_io_device.h>
#include <fast_io_tools/install_path.h>
#include <fast_io_driver/install_path.h>

int main()
{
Expand Down
Loading