Intel-TSX: Why rtm is failing? - c

I'm new in using Intel-TSX. So, please correct me on any terminological/conceptional mistake.
I'm trying to write a custom rsa engine using a polarssl library from here (I know it is old, but I found it easy to understand). I have the following code,
result=-1;
unsigned block;
int key_len= 128;
while(result!=1){
if ((block = _xbegin()) == _XBEGIN_STARTED) {
if( rsa_pkcs1_decrypt( &rsa_polar, &myrand, NULL, RSA_PRIVATE, &key_len, from, decrypt_plaintext, sizeof(decrypt_plaintext) ) != 0 )
exit(0);
rsa_free(&rsa_polar);
result=1;
_xend();
}else{
printf("RTM 2: Transaction failed\n");
printf("status is %ld\n", block);
}
printf("Block 2: Result is %d\n", result);
}
The code inside the rtm block doesn't work. However, the same code works outside rtm block. Upon running the code I'm getting the following output,
.
.
RTM 2: Transaction failed
status is 0
Block 2: Result is -1
.
.
Any help/suggestions on how to solve it?

Related

Contents in message-queue is changed

I am using uuntu 18.04.1LTS and studying IPC using C. I'm testing Unix i/o using LPC this time, and there's a problem when more than one client connects to the server at the same time.
(when only one client connected, there is no problem.)
sprintf(s1,"./%sA",t);
sprintf(s2, "./%sB", t);
if (MakeDirectory(s1, 0755) == -1) {
return -1;
}
if (MakeDirectory(s2, 0755) == -1) {
return -1;
}
for (i = 0; i < 5; i++)
{
memset(dirName, 0, SIZE);
sprintf(dirName, "%s/%d",s1,i);
usleep(300000);
if (MakeDirectory(dirName, 0755) == -1) {
return -1;
}
}
This code is client's main function. There is no problem at the top, but after running the repeat statement once (when i = 1), MakeDirectory() returns -1 with an error.
(t refers to the pid of the forked process converted into a string.)
int MakeDirectory(char* path, int mode) {
memset(&pRequest, 0x00, LPC_REQUEST_SIZE);
memset(&pResponse, 0x00, LPC_RESPONSE_SIZE);
pRequest.pid = getpid();
pRequest.service = LPC_MAKE_DIRECTORY;
pRequest.numArg = 2;
pRequest.lpcArgs[0].argSize = strlen(path);
strcpy(pRequest.lpcArgs[0].argData, path);
pRequest.lpcArgs[1].argSize = mode;
msgsnd(rqmsqid, &pRequest, LPC_REQUEST_SIZE, 0);
msgrcv(rpmsqid, &pResponse, LPC_RESPONSE_SIZE, getpid(), 0);
int res = pResponse.responseSize;
return res;
}
This is client's MakeDirectory, and
int MakeDirectory(LpcRequest* pRequest) {
memset(&pResponse, 0x00, LPC_RESPONSE_SIZE);
char *path = pRequest->lpcArgs[0].argData;
int mode = pRequest->lpcArgs[1].argSize;
int res = mkdir(path, mode);
pResponse.errorno = 0;
pResponse.pid = pRequest->pid;
printf("%ld\n", pResponse.pid);
pResponse.responseSize = res;
msgsnd(rpmsqid, &pResponse, LPC_RESPONSE_SIZE, 0);
return res;
}
This is a function of the server that runs after checking the pRequest.service when the MakeDirectory function is enabled on the client.
Again, there's nothing wrong with having one client, and if there's more than one. I checked with printf(), but the server passes 0 and the client receives -1. I don't know why this happens.
There's too much missing from your code to know definitively what's happening. I'm placing my bet on either using unallocated memory, or not recognizing a syscall error.
I'm using LTS 16, and there's no definition on my system for LpcRequest or LPC_REQUEST_SIZE, etc. You don't show how they're defined, so we don't know for example if pRequest.lpcArgs[1] exists.
You're also not checking the return code for msgsnd and msgrcv, a sure recipe for endless hours of entertaining debugging.
I suggest you edit your question to include working code, and a shell script that produces the mysterious result. Then someone will be able, if willing, to debug it and explain where you went wrong.
My other suggestion in this area is pretty standard: W. Richard Stevens's books on TCP/IP, specifically Unix Network Programming. If you're studying this stuff, you'll absolutely be glad to have read it.

getting irrelevant values for malloc macros - M_MMAP_THRESHOLD and M_ARENA_MAX

Tried to print values of M_MMAP_THRESHOLD and M_ARENA_MAX in a sample c program :
if (mallopt(M_ARENA_MAX, 0) == 0) {
printf("mallopt() 2 failed");
exit(EXIT_FAILURE);
}
if (mallopt(M_MMAP_THRESHOLD, 64) == 0) {
printf("mallopt() 2 failed");
exit(EXIT_FAILURE);
}
p = malloc(1000);
if (p == NULL) {
fprintf(stderr, "malloc() failed");
exit(EXIT_FAILURE);
}
printf("Value for M_MMAP_MAX : %d \n",M_MMAP_MAX);
printf("Value for M_MMAP_THRESHOLD : %d \n",M_MMAP_THRESHOLD);
Output:
Value for M_MMAP_MAX : -4
Value for M_MMAP_THRESHOLD : -3
If you can suggest - how to get values for these macros.
The macros are selectors: the value tells mallopt which option to set. The definition of mallopt (only slightly simplified) is:
int mallopt(int which, int value) {
int result = 0;
internal_lock_malloc_state();
switch (which) {
case M_MMAP_MAX:
result = internal_set_maximum_mmap(value);
break;
case M_MMAP_THRESHOLD:
result = internal_set_threshold(value);
break;
// ...
}
internal_unlock_malloc_state();
return result;
}
The internal functions above are probably actually written out, but that makes no difference here. The important thing is that the macro is just a small integer which indicates which option is to be modified.
Unfortunately, from your perspective, there is no way to examine the current value of these options. In fact, there is not even a guarantee that there is a current value. For example, consider an implementation of malloc which never uses mmap, perhaps because the host system doesn't implement memory mapping. Such an implementation could quite reasonably ignore any attempt to set these options, by replacing the internal_set... functions above with result = 1;.
In short, if you want to query what the current value of a malloc option is, it is up to you to remember the last value you set it to. (And there is no way to get the default value other than by reading the documentation.)

malloc() "crashing" without returning any error (program pauses)

I have a piece of code in which malloc() makes the program pause, without neither really crashing, nor returning an error code (NULL).
Piece of code (has to be executed 24 times, stops at the 22th) :
fprintf(stderr, "malloc");
//-- copy sound
pitched_sound = malloc(sizeof(Mix_Chunk));
if (pitched_sound == NULL)
return -1;
*pitched_sound = *orig_sound;
pitched_sound->abuf = malloc(sound->alen / note_factor);
if (pitched_sound->abuf == NULL)
return -1;
fprintf(stderr, "mallocok.");
Any idea?
I'm running on a Rasperry Pi (ARM), could be related?
How could I debug this?
Many thanks!

linux kernel + conditional statements

I basically am running into a very odd situation in a system call that I am writing. I want to check some values if they are the same return -2 which indicates a certain type of error has occurred. I am using printk() to print the values of the variables right before my "else if" and it says that they are equal to one another but yet the conditional is not being executed (i.e. we don't enter the else if) I am fairly new to working in the kernel but this seems very off to me and am wondering if there is some nuance of working in the kernel I am not aware of so if anyone could venture a guess as to why if I know the values of my variables the conditional would not execute I would really appreciate your help
//---------------------------------------//
/* sys_receiveMsg421()
Description:
- Copies the first message in the mailbox into <msg>
*/
asmlinkage long sys_receiveMsg421(unsigned long mbxID, char *msg, unsigned long N)
{
int result = 0;
int mboxIndex = checkBoxId(mbxID);
int msgIndex = 0;
//acquire the lock
down_interruptible(&sem);
//check to make sure the mailbox with <mbxID> exists
if(!mboxIndex)
{
//free our lock
up(&sem);
return -1;
}
else
mboxIndex--;
printk("<1>mboxIndex = %d\nNumber of messages = %dCurrent Msg = %d\n",mboxIndex, groupBox.boxes[mboxIndex].numMessages, groupBox.boxes[mboxIndex].currentMsg );
//check to make sure we have a message to recieve
-----------CODE NOT EXECUTING HERE------------------------------------------------
if(groupBox.boxes[mboxIndex].numMessages == groupBox.boxes[mboxIndex].currentMsg)
{
//free our lock
up(&sem);
return -2;
}
//retrieve the message
else
{
//check to make sure the msg is a valid pointer before continuing
if(!access_ok(VERIFY_READ, msg, N * sizeof(char)))
{
printk("<1>Access has been denied for %lu\n", mbxID);
//free our lock
up(&sem);
return -1;
}
else
{
//calculate the index of the message to be retrieved
msgIndex = groupBox.boxes[mboxIndex].currentMsg;
//copy from kernel to user variable
result = copy_to_user(msg, groupBox.boxes[mboxIndex].messages[msgIndex], N);
//increment message position
groupBox.boxes[mboxIndex].currentMsg++;
//free our lock
up(&sem);
//return number of bytes copied
return (N - result);
}
}
}
UPDATE: Solved my problem by just changing the return value to something else and it works fine very weird though
Please remember to use punctuation; I don't like running out of breath while reading questions.
Are you sure the if block isn't being entered? A printk there (and another in the corresponding else block) would take you one step further, no?
As for the question: No, there isn't anything specific to kernel code that would make this not work.
And you seem to have synchronization covered, too. Though: I see that you're acquiring mboxIndex outside the critical section. Could that cause a problem? It's hard to tell from this snippet, which doesn't even have groupBox declared.
Perhaps numMessages and/or currentMsg are defined as long?
If so, your printk, which uses %d, would print just some of the bits, so you may think they're equal while they are not.

Ridiculously simple MPI_Send/Recv problem I don't understand

I have two functions with different algorithms. In the first function I implemented non-blocking communications (MPI_Irecv, MPI_Isend) and the program runs without any errors. Even when I change the non-blocking to blocking communication, everything is fine. No deadlock.
But if I implement the second function with basic blocking communication like this (reduced the algorithm to the problem):
if( my_rank == 0)
{
a = 3 ;
MPI_Send(&a,1,MPI_DOUBLE,1,0,MPI_COMM_WORLD) ;
}
else if( my_rank == 1 )
{
MPI_Recv(&a,1,MPI_DOUBLE,0,0,MPI_COMM_WORLD, &status ) ;
}
So, process 1 should receive the value a from process 0. But I'm getting this error:
Fatal error in MPI_Recv: Message
truncated, error stack:
MPI_Recv(187).......................:
MPI_Recv(buf=0xbfbef2a8, count=1,
MPI_DOUBLE, src=0, tag=0,
MPI_COMM_WORLD, status=0xbfbef294)
failed
MPIDI_CH3U_Request_unpack_uebuf(600):
Message truncated; 32 bytes received
but buffer size is 8 rank 2 in job 39
Blabla caused collective
abort of all ranks exit status of
rank 2: killed by signal 9
If I run the program with only one of the two functions, then they work as they are supposed to. But both together results in the error message above. I do understand the error message, but I don't know what I can do to prevent it. Can someone explain to me where I have to look for the error? Since I'm not getting a deadlock in the first function, I'm assuming that there can't be a unreceived send from the first function which leads to the error in the second.
So, here is the the first function:
MPI_Type_vector(m,1,m,MPI_DOUBLE, &column_mpi_t ) ;
MPI_Type_commit(&column_mpi_t) ;
T = (double**)malloc(m*sizeof(double*)) ;
T_data = (double*)malloc(m*m*sizeof(double)) ;
for(i=0;i<m;i++)
{
T[i] = &(T_data[i*m]) ;
}
if(my_rank==0)
{
s = &(T[0][0]) ;
for(i=1;i<p;i++)
{
MPI_Send(s,1,column_mpi_t,i,0,MPI_COMM_WORLD) ;
}
}
for(k=0;k<m-1;k++)
{
if(k%p != my_rank)
{
rbuffer = &(T[0][k]) ;
MPI_Recv(rbuffer,1,column_mpi_t,k%p,0,MPI_COMM_WORLD,&status) ;
}
for(j=k+1;j<n;j++)
{
if(j%p==my_rank)
{
if(j==k+1 && j!=n-1)
{
sbuffer = &(T[0][k+1]) ;
for(i=0;i<p;i++)
{
if(i!= (k+1)%p )
MPI_Send(sbuffer,1,column_mpi_t,i,0,MPI_COMM_WORLD) ;
}
}
}
}
}
I came to the conclusion that the derived datatype is the origin of my problems. Somebody sees why?
Ok, im wrong. If i change the MPI datatype in MPI_Irecv/send to MPI_DOUBLE,that would fit to the datatypes of recv/send of the second function ..so no truncation error. So, no solution....

Resources