2009-03-12 02:53:20 +01:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* sched/sched/sched_verifytcb.c
|
2009-03-12 02:53:20 +01:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* 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
|
2009-03-12 02:53:20 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-03-12 02:53:20 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* 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.
|
2009-03-12 02:53:20 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-14 22:15:18 +01:00
|
|
|
#include <stdbool.h>
|
2009-03-12 02:53:20 +01:00
|
|
|
#include <sched.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2014-08-09 01:29:44 +02:00
|
|
|
#include "sched/sched.h"
|
2009-03-12 02:53:20 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-05-09 20:40:14 +02:00
|
|
|
* Name: nxsched_verify_tcb
|
2009-03-12 02:53:20 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2009-12-14 22:15:18 +01:00
|
|
|
* Return true if the tcb refers to an active task; false if it is a stale
|
2009-03-12 02:53:20 +01:00
|
|
|
* TCB handle.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-09 20:40:14 +02:00
|
|
|
bool nxsched_verify_tcb(FAR struct tcb_s *tcb)
|
2009-03-12 02:53:20 +01:00
|
|
|
{
|
2017-09-11 17:35:39 +02:00
|
|
|
/* Return true if the PID hashes to this TCB. This will catch the case
|
|
|
|
* where the task associated with the TCB has terminated (note that
|
|
|
|
* sched_releasedtcb() will nullify the TCB field in that case). The
|
2017-09-11 22:27:02 +02:00
|
|
|
* following logic will also detect the case where the task associated
|
2017-09-11 17:35:39 +02:00
|
|
|
* with the TCB has terminated and another task has been started with a
|
|
|
|
* different TCB but with a PID hashing to the same entry.
|
|
|
|
*
|
|
|
|
* NOTE: In the event that the TCB has terminated, the 'tcb' parameter
|
|
|
|
* will point at either a stale or a re-allocated memory allocation. The
|
|
|
|
* PID fetched by the use of the bad pointer(tcb->pid) should not cause
|
2017-09-11 22:27:02 +02:00
|
|
|
* any memory faults because we do at least know that the pointer refers
|
|
|
|
* to valid memory in the kernel address space and that the hash macro,
|
|
|
|
* PIDHASH(), will return a valid, in-range index into the g_pidhash[]
|
|
|
|
* table.
|
2017-09-11 17:35:39 +02:00
|
|
|
*
|
|
|
|
* REVISIT: This logic will not, however, catch the case where the task
|
|
|
|
* originally associated with the TCB has terminated, but a new task was
|
|
|
|
* started reusing the same memory allocation for its TDB that was freed
|
|
|
|
* by the terminated task. In this case, a false positive value will be
|
|
|
|
* returned: The TCB is valid but does not refer to the same task as
|
|
|
|
* before. This case is not detectable with the limited amount of
|
|
|
|
* information available.
|
|
|
|
*/
|
2009-03-12 02:53:20 +01:00
|
|
|
|
2021-06-15 10:45:40 +02:00
|
|
|
irqstate_t flags;
|
2022-07-07 18:01:22 +02:00
|
|
|
bool valid;
|
2021-06-15 10:45:40 +02:00
|
|
|
|
|
|
|
flags = enter_critical_section();
|
2024-06-06 10:49:50 +02:00
|
|
|
valid = tcb == g_pidhash[PIDHASH(tcb->pid)];
|
2021-06-15 10:45:40 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
|
2022-07-07 18:01:22 +02:00
|
|
|
return valid;
|
2009-03-12 02:53:20 +01:00
|
|
|
}
|