Skip to content
Draft
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
41 changes: 41 additions & 0 deletions testing/sched/spawnattr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ##############################################################################
# apps/testing/sched/spawnattr/CMakeLists.txt
#
# 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.
#
# ##############################################################################

if(CONFIG_TESTING_SPAWNATTR)

set(SRCS spawnattr_main.c)

nuttx_add_application(
NAME
spawnattr
PRIORITY
${CONFIG_TESTING_SPAWNATTR_PRIORITY}
STACKSIZE
${CONFIG_TESTING_SPAWNATTR_STACKSIZE}
MODULE
${CONFIG_TESTING_SPAWNATTR}
COMPILE_FLAGS
${FLAGS}
SRCS
${SRCS})

endif()
26 changes: 26 additions & 0 deletions testing/sched/spawnattr/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config TESTING_SPAWNATTR
tristate "posix_spawn() attribute error test"
default n
depends on LIBC_EXECFUNCS && SCHED_WAITPID
---help---
Test posix_spawn() with a spawn attribute that the kernel refuses
after it made the new task (POSIX_SPAWN_SETSCHEDPARAM with
priority 0): posix_spawn() must return EINVAL, and the heap of
the caller must stay intact.

if TESTING_SPAWNATTR

config TESTING_SPAWNATTR_PRIORITY
int "Task priority"
default 100

config TESTING_SPAWNATTR_STACKSIZE
int "Stack size"
default DEFAULT_TASK_STACKSIZE

endif
25 changes: 25 additions & 0 deletions testing/sched/spawnattr/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
############################################################################
# apps/testing/sched/spawnattr/Make.defs
#
# 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.
#
############################################################################

ifneq ($(CONFIG_TESTING_SPAWNATTR),)
CONFIGURED_APPS += $(APPDIR)/testing/sched/spawnattr
endif
32 changes: 32 additions & 0 deletions testing/sched/spawnattr/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
############################################################################
# apps/testing/sched/spawnattr/Makefile
#
# 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.
#
############################################################################

include $(APPDIR)/Make.defs

PROGNAME = spawnattr
PRIORITY = $(CONFIG_TESTING_SPAWNATTR_PRIORITY)
STACKSIZE = $(CONFIG_TESTING_SPAWNATTR_STACKSIZE)
MODULE = $(CONFIG_TESTING_SPAWNATTR)

MAINSRC = spawnattr_main.c

include $(APPDIR)/Application.mk
198 changes: 198 additions & 0 deletions testing/sched/spawnattr/spawnattr_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/****************************************************************************
* apps/testing/sched/spawnattr/spawnattr_main.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.
*
****************************************************************************/

/* posix_spawn() with a spawn attribute that the kernel refuses after it
* made the new task: POSIX_SPAWN_SETSCHEDPARAM with priority 0 gives
* EINVAL. posix_spawn() must return the error, and the memory of the
* caller must stay intact: the heap (a pattern and mallinfo()) after many
* such errors, then a normal posix_spawn(). The child is this program
* again.
*/

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

#include <errno.h>
#include <malloc.h>
#include <sched.h>
#include <spawn.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>

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

#define NAME "spawnattr"
#define ROUNDS 100
#define NBLOCKS 16
#define CHILD_EXIT 7

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

/* Allocate blocks of 16 bytes to 64 KiB, fill them, check them, free
* them. Return false if a block is missing or changed.
*/

/* posix_spawn() of the program with priority 0: return its result */

static int spawn_bad(FAR char *path, FAR char *cargv[])
{
struct sched_param param;
posix_spawnattr_t attr;
pid_t pid = 0;
int ret;

posix_spawnattr_init(&attr);
memset(&param, 0, sizeof(param));
posix_spawnattr_setschedparam(&attr, &param);
posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDPARAM);
ret = posix_spawn(&pid, path, NULL, &attr, cargv, NULL);
posix_spawnattr_destroy(&attr);
if (ret == 0)
{
waitpid(pid, NULL, 0);
}

return ret;
}

static bool heap_round(int r)
{
FAR unsigned char *p[NBLOCKS];
bool ok = true;
size_t n;
size_t j;
int i;

for (i = 0; i < NBLOCKS; i++)
{
n = (size_t)16 << (i % 13);
p[i] = malloc(n);
if (p[i] == NULL)
{
ok = false;
continue;
}

memset(p[i], (r + i) & 0xff, n);
}

for (i = 0; i < NBLOCKS; i++)
{
n = (size_t)16 << (i % 13);
for (j = 0; p[i] != NULL && j < n; j++)
{
if (p[i][j] != ((r + i) & 0xff))
{
ok = false;
break;
}
}

free(p[i]);
}

return ok;
}

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

/****************************************************************************
* Name: main
****************************************************************************/

int main(int argc, FAR char *argv[])
{
FAR char *cargv[3];
struct mallinfo before;
struct mallinfo after;
pid_t pid = 0;
int status = -1;
int fails = 0;
int bad = 0;
bool heap = true;
bool ok;
int ret;
int r;

if (argc > 1 && strcmp(argv[1], "child") == 0)
{
return CHILD_EXIT;
}

cargv[0] = argv[0];
cargv[1] = "child";
cargv[2] = NULL;

/* The first posix_spawn() may set up state that stays: measure after it */

bad += spawn_bad(argv[0], cargv) != EINVAL;
heap_round(0);
before = mallinfo();

for (r = 1; r < ROUNDS; r++)
{
bad += spawn_bad(argv[0], cargv) != EINVAL;
heap &= heap_round(r);
}

after = mallinfo();

ok = bad == 0;
printf(NAME ": prio0 %d posix_spawn() with priority 0: %d not EINVAL "
"%s\n", ROUNDS, bad, ok ? "PASS" : "FAIL");
fails += !ok;

printf(NAME ": heap pattern after each error %s %s\n",
heap ? "intact" : "changed", heap ? "PASS" : "FAIL");
fails += !heap;

ok = before.uordblks == after.uordblks &&
before.fordblks == after.fordblks;
printf(NAME ": mallinfo uordblks %d -> %d, fordblks %d -> %d %s\n",
before.uordblks, after.uordblks, before.fordblks, after.fordblks,
ok ? "PASS" : "FAIL");
fails += !ok;

ret = posix_spawn(&pid, argv[0], NULL, NULL, cargv, NULL);
if (ret == 0 && waitpid(pid, &status, 0) != pid)
{
status = -1;
}

ok = ret == 0 && WIFEXITED(status) && WEXITSTATUS(status) == CHILD_EXIT;
printf(NAME ": good posix_spawn() %d, exit %d %s\n", ret,
WIFEXITED(status) ? WEXITSTATUS(status) : -1, ok ? "PASS" : "FAIL");
fails += !ok;

printf(NAME ": done, %d failed\n", fails);
return fails != 0;
}
Loading