Skip to content

[v2] vfork_launcher undefined behaviour  #314

@gitmodimo

Description

@gitmodimo

In my application stating of multiple processes with vfork_launcher causes exception:

terminate called after throwing an instance of 'boost::wrapexcept<boost::system::system_error>'
  what():  assign: Bad file descriptor [system:9 at ...aarch64-buildroot-linux-gnu/sysroot/usr/include/boost/asio/detail/impl/reactive_descriptor_service.ipp:120:5 in function 'boost::system::error_code boost::asio::detail::reactive_descriptor_service::assign(boost::asio::detail::reactive_descriptor_service::implementation_type&, const native_handle_type&, boost::system::error_code&)']

It seems it is caused by undefined behaviour triggered by child process memory modification. After vfork it is forbidden to modify any memory contents except vfrok return value :

pid = ::vfork();
if (pid == -1)
{
    ...
}
else if (pid == 0)
{
    ctx.notify_fork(BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context::fork_child);//undefined behaviour??
    ec = detail::on_exec_setup(*this, executable, argv, inits...);
    if (!ec)
        close_all_fds(ec);//undefined behaviour??
    if (!ec)
        ::execve(executable.c_str(), const_cast<char * const *>(argv), const_cast<char * const *>(env));

    BOOST_PROCESS_V2_ASSIGN_EC(ec, errno, system_category())
    detail::on_exec_error(*this, executable, argv, ec, inits...);
    ::exit(EXIT_FAILURE);
    return basic_process<Executor>{exec};
}
ctx.notify_fork(BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context::fork_parent);

To fix this I removed notify_fork. It seems completely unnecessary since we execve and we do not hold any io_context. Or am I missing something?
Also close_all_fds also modifies memory but it seems there is no problem with that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions