Skip to content
Open
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
10 changes: 9 additions & 1 deletion interpreters/python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ ifeq ($(CONFIG_ARCH_HAVE_FORK),y)
else
@echo "export ac_cv_func_fork=\"no\"" >> $@
endif
ifneq ($(CONFIG_ARCH_HAVE_VFORK),)
Comment thread
casaroli marked this conversation as resolved.
@echo "export ac_cv_func_vfork=\"yes\"" >> $@
else
@echo "export ac_cv_func_vfork=\"no\"" >> $@
endif
ifeq ($(CONFIG_SYSTEM_SYSTEM),y)
@echo "export ac_cv_func_system=\"yes\"" >> $@
else
Expand All @@ -135,7 +140,10 @@ endif

$(SETUP_LOCAL):
$(Q) ( cp $(SETUP_LOCAL).in $(SETUP_LOCAL))
ifneq ($(CONFIG_ARCH_HAVE_FORK),y)
# _posixsubprocess is the fork-then-exec path, so vfork() is enough for it;
# os.fork() itself needs a real fork() and is governed by ac_cv_func_fork
# above.
ifeq ($(CONFIG_ARCH_HAVE_FORK)$(CONFIG_ARCH_HAVE_VFORK),)
@echo "_posixsubprocess" >> $@
endif
ifneq ($(CONFIG_LIBC_DLFCN),y)
Expand Down
7 changes: 7 additions & 0 deletions netutils/libwebsockets/lws_config_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
/* #undef USE_CYASSL */

/* Define to 1 if you have the `fork' function. */

#ifdef CONFIG_ARCH_HAVE_FORK
Comment thread
casaroli marked this conversation as resolved.
#define LWS_HAVE_FORK
#endif

#ifndef CONFIG_DISABLE_ENVIRON
/* Define to 1 if you have the `getenv' function. */
Expand Down Expand Up @@ -111,10 +114,14 @@
/* #undef LWS_HAVE_VFORK_H */

/* Define to 1 if `fork' works. */
#ifdef CONFIG_ARCH_HAVE_FORK
#define LWS_HAVE_WORKING_FORK
#endif

/* Define to 1 if `vfork' works. */
#ifdef CONFIG_ARCH_HAVE_VFORK
#define LWS_HAVE_WORKING_VFORK
#endif

/* Define to 1 if execvpe() exists */
#define LWS_HAVE_EXECVPE
Expand Down
4 changes: 4 additions & 0 deletions testing/fs/fdsantest/fdsantest_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ static void test_case_overflow(void **state)
assert_int_equal(open_count, close_count);
}

#ifdef CONFIG_ARCH_HAVE_VFORK
Comment thread
casaroli marked this conversation as resolved.
static void test_case_vfork(void **state)
{
int fd = open("/dev/null", O_RDONLY);
Expand All @@ -112,6 +113,7 @@ static void test_case_vfork(void **state)

android_fdsan_close_with_tag(fd, 0xbadc0de);
}
#endif

/****************************************************************************
* Public Functions
Expand All @@ -129,7 +131,9 @@ int main(int argc, FAR char *argv[])
cmocka_unit_test(test_case_unowned_tagged_close),
cmocka_unit_test(test_case_owned_tagged_close),
cmocka_unit_test(test_case_overflow),
#ifdef CONFIG_ARCH_HAVE_VFORK
cmocka_unit_test(test_case_vfork),
#endif
};

return cmocka_run_group_tests(tests, NULL, NULL);
Expand Down
2 changes: 1 addition & 1 deletion testing/ltp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ BLACKWORDS += "pthread_spin_trylock"
endif

# Where NuttX does not declare fork(), a test that calls it cannot be built.
# The pattern spares vfork() and task_fork(), which remain available.
# The pattern spares vfork(), and any identifier that ends in _fork().

ifeq ($(CONFIG_ARCH_HAVE_FORK),)
BLACKWORDS += "[^v_]fork("
Expand Down
8 changes: 5 additions & 3 deletions testing/ostest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ if(CONFIG_TESTING_OSTEST)
endif()
endif()

if(CONFIG_ARCH_HAVE_VFORK)
list(APPEND SRCS vfork.c)
endif()

if(CONFIG_ARCH_HAVE_FORK)
if(CONFIG_SCHED_WAITPID)
list(APPEND SRCS vfork.c)
endif()
list(APPEND SRCS fork.c)
endif()

if(CONFIG_ARCH_SETJMP_H)
Expand Down
8 changes: 6 additions & 2 deletions testing/ostest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,14 @@ CSRCS += sigev_thread.c
endif
endif

ifeq ($(CONFIG_ARCH_HAVE_FORK),y)
ifeq ($(CONFIG_SCHED_WAITPID),y)
# Each test is built where the primitive it tests exists.

ifeq ($(CONFIG_ARCH_HAVE_VFORK),y)
CSRCS += vfork.c
endif

ifeq ($(CONFIG_ARCH_HAVE_FORK),y)
CSRCS += fork.c
endif

ifeq ($(CONFIG_ARCH_SETJMP_H),y)
Expand Down
276 changes: 276 additions & 0 deletions testing/ostest/fork.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
/****************************************************************************
* apps/testing/ostest/fork.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

#include "ostest.h"

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define FORK_HEAPSIZE 256
#define FORK_PARENTMARK 0x5a
#define FORK_CHILDMARK 0xa5

/* Distinct values written through a pointer to a stack local, to check that
* the child's stack is at the same virtual address as the parent's.
*/

#define FORK_STACKPARENT 0x1234
#define FORK_STACKCHILD 0x5678

/****************************************************************************
* Private Data
****************************************************************************/

/* .data and .bss, which a fork() child must get its own copy of */

static volatile int g_forkdata = 1;
static volatile int g_forkbss;
static FAR unsigned char *g_forkheap;

/* Pointer to a local in fork_test()'s frame, taken before fork(). In .data
* rather than on the stack: the compiler could rematerialise a local
* pointer from the current stack pointer, which would test nothing.
*/

static FAR volatile int *g_forkstackptr;

/****************************************************************************
* Private Functions
****************************************************************************/

/****************************************************************************
* Name: fork_child
*
* Description:
* Everything here is forbidden to a vfork() child and permitted to a
* fork() child. Returns the child's exit status.
*
****************************************************************************/

static int fork_child(void)
{
FAR char *scratch;

/* The parent must see none of this. */

g_forkdata = 2;
g_forkbss = 2;
memset(g_forkheap, FORK_CHILDMARK, FORK_HEAPSIZE);

/* A vfork() child may not call these; a fork() child may. */

scratch = malloc(64);
if (scratch == NULL)
{
printf("fork_test: ERROR Child could not malloc()\n");
return 1;
}

strlcpy(scratch, "child", 64);
printf("fork_test: Child running independently (%s)\n", scratch);
free(scratch);

/* Give the parent time to make its own writes, so that if the two shared
* memory we would see the parent's values below rather than our own.
*/

usleep(200 * 1000);

if (g_forkdata != 2 || g_forkbss != 2)
{
printf("fork_test: ERROR Child saw the parent's writes: "
"data=%d bss=%d\n", g_forkdata, g_forkbss);
return 1;
}

if (g_forkheap[0] != FORK_CHILDMARK ||
g_forkheap[FORK_HEAPSIZE - 1] != FORK_CHILDMARK)
{
printf("fork_test: ERROR Child saw the parent's heap writes\n");
return 1;
}

return 0;
}

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: fork_test
*
* Description:
* Verify the defining property of POSIX fork(): the child gets its own
* copy of the parent's memory, in both directions. That is the exact
* opposite of the sharing vfork() gives. Also verifies that none of
* vfork()'s restrictions apply.
*
****************************************************************************/

int fork_test(void)
{
pid_t pid;
int status = 0;
int ret = 0;

/* volatile so it is really in memory and the compare is not folded. */

volatile int stackvar = FORK_STACKPARENT;

printf("fork_test: Started\n");

/* Publish its address before forking. See g_forkstackptr. */

g_forkstackptr = &stackvar;

g_forkdata = 1;
g_forkbss = 1;

g_forkheap = malloc(FORK_HEAPSIZE);
if (g_forkheap == NULL)
{
printf("fork_test: ERROR Failed to allocate the heap probe\n");
ASSERT(false);
return -1;
}

memset(g_forkheap, FORK_PARENTMARK, FORK_HEAPSIZE);

pid = fork();
if (pid == 0)
{
/* The child's stack is at the parent's virtual addresses, so this
* pointer names the child's own live local. A relocated stack would
* write into the copy of the parent's instead.
*/

*g_forkstackptr = FORK_STACKCHILD;
if (stackvar != FORK_STACKCHILD)
{
printf("fork_test: ERROR Child stack was relocated: wrote %d "
"through %p, local at %p reads %d\n",
FORK_STACKCHILD, g_forkstackptr, &stackvar, stackvar);
_exit(1);
}

/* Returns from fork_child() and from this branch -- both illegal
* for a vfork() child.
*/

_exit(fork_child());
}
else if (pid < 0)
{
printf("fork_test: ERROR fork() failed: %d\n", errno);
free(g_forkheap);
ASSERT(false);
return -1;
}

/* Parent runs concurrently: write now, check isolation afterwards. */

g_forkdata = 3;
g_forkbss = 3;
memset(g_forkheap, FORK_PARENTMARK, FORK_HEAPSIZE);

#ifdef CONFIG_SCHED_WAITPID
/* Wait for the child to be done before comparing memory. waitpid() blocks
* on a child that is still alive whether or not its exit status will be
* retained, so this synchronises either way; ECHILD simply means the child
* had already finished, which is just as good. It is not a failure:
* ostest_main() sets SA_NOCLDWAIT on SIGCHLD for the whole run, so an
* exited child's status is not kept even where CONFIG_SCHED_CHILD_STATUS
* is enabled.
*/

if (waitpid(pid, &status, 0) != pid && errno != ECHILD)
{
printf("fork_test: ERROR waitpid() failed: %d\n", errno);
free(g_forkheap);
ASSERT(false);
return -1;
}
#else
sleep(1);
#endif

if (g_forkdata != 3 || g_forkbss != 3)
{
printf("fork_test: ERROR Parent saw the child's writes: "
"data=%d bss=%d (expected 3, 3)\n", g_forkdata, g_forkbss);
ret = -1;
}

if (g_forkheap[0] != FORK_PARENTMARK ||
g_forkheap[FORK_HEAPSIZE - 1] != FORK_PARENTMARK)
{
printf("fork_test: ERROR Parent saw the child's heap writes\n");
ret = -1;
}

/* The child wrote to the same stack address; the parent must not see it */

if (stackvar != FORK_STACKPARENT)
{
printf("fork_test: ERROR Parent saw the child's stack write: "
"%d (expected %d)\n", stackvar, FORK_STACKPARENT);
ret = -1;
}

#ifdef CONFIG_SCHED_WAITPID
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
{
printf("fork_test: ERROR Child reported failure, status 0x%04x\n",
status);
ret = -1;
}
#endif

free(g_forkheap);
g_forkheap = NULL;

if (ret < 0)
{
ASSERT(false);
return ret;
}

printf("fork_test: Parent and child had independent memory\n");
return 0;
}
Loading
Loading