Failure Injection:Try to write into protected RAM area [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm working on an home project to learn more about embedded systems. So I'm also not professional with C language :)
Lets say I have a struct:
static st_struct myStruct;
The struct is defined as:
typedef struct
{
int a;
long xy;
} st_struct
To keep the code simple here, the struct is defined and only valid within a protected RAM area, in the address space from 0x04001000 - 0x04003000.
This protected area prevents other tasks to write into it, they can only read.
If some task/function is trying to modify or write into this area, the CPU will reset.
The variable myStruct is located within address 0x04001f15.
I would like to provoke this behavior of trying to write into the "not allowed", exactly to the myStruct variable. What would be the best way to implement such kind of failure injection in this example? Can you give an example how to do this with pointer arithmetic?

From what I understand you want the code for the other application so produce the faulty behavior.
In the other process/program do this
typedef struct
{
int a;
long xy;
} st_struct;
st_struct *ms = (st_struct*) 0x04001f15;
ms->a = 0;
ms->xy = 0;
This way the other application will try to access the same struct and will fault.

Related

Is it better to use a pointer instead of returning a value? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 months ago.
Improve this question
I was wondering what is the best way to get a value from a function.
A function can return a value like an int for example.
But you can also change the value of a variable with a pointer passed as a parameter to the function.
See below, two examples of codes that do this in two different ways, but produce the same result.
int example_return()
{
return (1);
}
int main(void)
{
int value;
value = example_return();
}
void example_ptr(int *a)
{
*a = 1;
}
int main(void)
{
int value;
example_ptr(&value);
}
Is there a real difference between the two options, which is the best way?
Side effects should be avoided if not absolutely needed. If you can return the value - return it. Basically, the function should a black-box which does something with parameters and returns the value.
Why:
It is much safer. You definitely will not invoke undefined behaviour.
It helps the compiler to optimize the code.
In your example -
int example_return(void)
{
return (1);
}
It depends on your purpose, If you have a pointer to data, then you have two levels of memory access. First to load an address from the pointer and second to actually load the data. If you simply directly reference a variable, there is only one level of memory access.
there are some advantages of using pointers ( Memory sharing, Runtime-sized data structures, and Resizable data structures).
most of the time pointers reduce the complexity of a program.

Array out of bounds in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to know how I can catch IndexOutofbounds in C.
I do not want my program to exit abnormally, I want to print a message for the user that clarify the error
how I can check for this case
char a[50];
fgets(a,200,stdin);
I need to exit the program and throw an error,and I do not need to change the 200 to use sizeof()
This is one of those things you simply cannot do in C. At least not without a lot of hazzle. You will have to keep track of such things yourself. So when you declare an array, then you will have to store the size and do something like this:
size_t size=1000;
int arr[size];
...
if(i>=size || i<0) {
// Handle error
} else {
// Do something with arr[i]
}
You can make abstractions and make it more Java-like with constructs like this:
struct array {
int *val;
size_t size;
};
int getVal(struct array array, size_t index) {
if(index>=array.size || index<0)
// Handle error
else
return array.val[index];
}
But if you are using constructs like that, chances are high that it might me a good idea to switch to another language instead.
If we look at your particular example:
char a[50];
fgets(a,200,stdin);
I'm sorry to say it, but it is impossible to make fgets throw an error in this situation.
First of all, C doesn’t have a structured exception handling mechanism. There’s no try...catch control structure in C (there is in C++ and C#, but those are different languages).
Secondly, C doesn’t do any bounds checking on array accesses - you, the programmer, are expected to do those checks yourself.

Pointer to pointer in a structure [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How can a structure's member (which is a pointer) be accessed through another pointer? Let's say that *ptr is the pointer i want to use to access *time, which is the pointer that belongs to the structure. Is it correct if I write ptr->time?
Would it be correct if I wrote ptr->time = v[i], if I wanted to assign the values of v[i] (array) to *time?
Would it be correct if I wrote ptr->time = v[i], if I wanted to assign the values of v[i] (array) to *time?
No. If you have...
struct {
int *time;
} *ptr;
int v[10], i = 0;
...then you have to write *(ptr->time) = v[i]
If time is a pointer, being inside a struct change nothing to that. So if you want to access the int pointed by time, you have to deference it too.

C: Two Different Arrays Pointing to the Same Struct [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am working on a problem where I have a "master array" of pointers to a typedef struct programs. For each item/structure that I create, I allocate memory to a programs* temp variable and store it into an array programs* master_array[x].
However, my problem requires me to handle errors in the master_array where if a program goes into a "blocked" state, then I have to manage that(those) process(es) separately.
I want to create a second array called programs* blocked_array[y] and store programs that are blocked in there. In this case, I can have two arrays pointing to the same program. However, when I am finished with handling a program in blocked, how can I deallocate it/dereference the blocked_array[y]'s pointer to that program without impacting the master_array[x]'s pointer?
Do I create an empty program temp, not allocate memory to this temp program and make the blocked_array[y] point to that temp program to effectively, empty out blocked_array?
Would this create some sort of unintended consequence or does doing this stop the blocked array from pointing to it while preserving the master_array[x]'s pointer? Any thoughts would help, thanks.
You can simply add a bool isBlocked; field in your struct programs, and then you can handle this with a single array by
if(master_array[x]->isBlocked)
{
// Do something
}
else
{
// Do something else
}
Alternatively, you can use another array to store "isBlocked" information:
bool isBlocked[sizeof master_array / sizeof master_array[0]] = {0};
...
isBlocked[x] = true;
...
if(isBlocked[x])
{
// Do something
}
else
{
// Do something else
}

how to initialize and use a 2d struct in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So what i am trying to do is have an array of lists, here is my code:
typedef struct stackList{
List * list;
} stack;
int main(){
int x;
stack ** stackTable;
for(x=0;x<100;x++)
stackTable[x]=malloc(sizeof(stack*)*100);
}
i get a segmentation fault on the for loop, i would assume the way i am trying to use the struct is wrong. Would i rather in the defintion of the struct use List ** list;
or is there a way to use it the way i am trying to use it
You get segmentation fault because you're accessing stackTable while it is uninitialized. You can't know to what address of memory it points, and you haven't allocated an array to hold the pointers that you are dereferencing.
You need to make stackTable point to a valid array of pointers, in this case I think is convenient to make it be an array:
Stack* stackTable[100];
Now you have an array of pointers to Stack, you can initialize them.
If instead you have just temporarily an array large 100, and you need to make it grow in future, that's how dynamically allocating it:
Stack** stackTable= malloc(100*sizeof(Stack*));
Before trying too hard to play with pointers and dynamic memory I might suggest writing some basic programs using basic 2d arrays. For instance:
char array2d[10][10];
Once you're confortable inserting elements into this array, extracting elements, etc, you can apply all of the same principles to any type.

Resources