How to give data ranges to function parameters which are pointers
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nideesh Madhu
el 6 de Mayo de 2021
Comentada: Anirban
el 7 de Mayo de 2021
While running code prover I got so many Orange warnings and was trying to reduce them by giving data ranges
to function input parameters. But I was not able to input the range for pointer variables.(There is some structure pointer
passed to our functions).
Is there any way of giving data range to pointer variables?
For example in the below code snippet how to give data ranges for a->x,a->y and a->z
typedef volatile struct
{
uint32_t x;
uint32_t y;
uint32_t z;
} GTM_XYZ_Pram_T;
void DD_GTM_XYZ_Init ( GTM_XYZ_Pram_T *a, uint32_t b, uint32_t c, uint32_t d )
{
}
0 comentarios
Respuesta aceptada
Anirban
el 6 de Mayo de 2021
I assume you are getting an orange warning on the dereferences a->x, a->y, and so on. The orange warning is probably an Illegally dereferenced pointer warning that states that a might be NULL.
You can specify a constraint that the pointer a is not NULL (and also other constraints). See more details in External Constraints for Polyspace Analysis. This constraint is not available in C++ since a base type pointer can point to derived types and managing constraints on pointers is difficult. It is possible that your Source code language is set to C-CPP, so you are running into the C++ limitation. If you are using a C-only project, you can set the language to C. Then in the Constraint Specification interface, you should be able to constrain pointers.
4 comentarios
Anirban
el 7 de Mayo de 2021
Editada: Anirban
el 7 de Mayo de 2021
Sorry I misunderstood your question. When you said 'orange warnings', I assumed it was orange Illegally dereferenced pointer warnings because the pointer to the structure was considered as maybe-null. I didn't realize you want to constrain the structure fields through the pointer.
Yes, as of now, this constraint is not possible (but this might change in the future). But I wanted to approach the problem from a different angle. I wanted to understand why you want to constrain an uint32_t variable to [0...255] , which is typically the range for an uint8_t variable.
Maybe, this is an issue where the analysis configuration needs to change. Polyspace makes assumptions about data ranges based on the data types. Let's say your uint32_t is a typedef for unsigned int (and Polyspace would know that if the typedef is in the code), and you are working on a target where int takes 8 bits (a hypothetical scenario). Maybe, when you are doing the Polyspace analysis, you are using a target where int is 32 bits and you are getting orange overflows because of that. If that is the case, you can create your own custom target. You can choose mcpu for the option Target processor type. See more details in Generic Target Options. However, note that even mcpu allows for not much wiggle room in sizes. You can go between 16 and 32 bits for int, for instance (this works in most cases). If you are really working on a target where int is 8 bits, then you can specify that using the option -custom-target.
It also looks like this is a case where you can benefit from contacting Support. Support can systematically investigate your orange warnings and suggest ways to reduce them. See Contact Technical Support About Issues with Running Polyspace.
Anirban
el 7 de Mayo de 2021
For the sake of completeness, I also wanted to answer your question on constraining structure fields through a pointer. Although you cannot constrain it directly through the Constraint Specification wizard yet, you can impose a constraint as follows.
Let us say you want to constrain the values of a->x, a->y and a->z here (note that I removed volatile from your example, otherwise constraint specifications won't apply, see Assumptions About Volatile Variables):
typedef struct
{
uint32_t x;
uint32_t y;
uint32_t z;
} GTM_XYZ_Pram_T;
void DD_GTM_XYZ_Init ( GTM_XYZ_Pram_T *a, uint32_t b, uint32_t c, uint32_t d )
{
}
You can write a function data_constraints that constrains those parameters and then passes the constrained parameters onto your real function. Like this:
void data_constraints( GTM_XYZ_Pram_T *a, uint32_t b, uint32_t c, uint32_t d) {
assert(a->x >=0 && a->x <=255);
assert(a->y >=0 && a->y <=255);
assert(a->z >=0 && a->z <=255);
DD_GTM_XYZ_Init(a, b, c, d);
}
You can then provide the file containing the function data_constraints for the Polyspace analysis. If you contact Technical Support, they can help you with specifying data constraints this way.
Más respuestas (0)
Ver también
Categorías
Más información sobre Multitasking en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!