1
votes

I'm trying to write a multithreaded RPC server that returns a struct. Here's my XDR file. I'm running rpcgen -MN foo.x to generate multithreaded compatible code.

// foo.x
struct foo_out {
  string name<128>;
};

program FOO_PROG {
  version FOO_VERS {
  foo_out foo(void) = 2;
  } = 2 ;
} = 0x31230000;

Here's my server, you can see it takes a pointer to my struct, allocates some memory for the name string and then copies over the string. I also added a debug output

// foo_server.c
#include "foo.h"
#include <stdio.h>
#include <stdlib.h>
#include <rpc/pmap_clnt.h>
#include <string.h>
#include <memory.h>
#include <sys/socket.h>
#include <netinet/in.h>

bool_t foo_2_svc(foo_out *out, struct svc_req *req) {
  out->name = malloc(sizeof("foo"));
  strcpy(out->name, "foo");
  printf("Value: '%s'\n", out->name);
  return TRUE;
}

int
foo_prog_2_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result) {
  xdr_free(xdr_result, result);
  return(1);
}

Here's the client. It creates a variable, passes in a pointer to that variable and then outputs the value:

// foo_client.c
#include "memory.h" /* for memset */
#include "foo.h"
#include "stdio.h"
#include "stdlib.h"
#include "rpc/pmap_clnt.h"
#include "string.h"
#include "memory.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include <unistd.h>

int
main (int argc,char **argv)
{
  CLIENT *cl;

  cl = clnt_create("127.0.0.1", FOO_PROG, FOO_VERS, "tcp");
  if (cl == NULL) {
    clnt_perror(cl, "call failed");
    exit (1);
  }

  foo_out out;
  if (foo_2(&out, cl) != RPC_SUCCESS) {
    printf("failed \n");
    // exit(1);
  }
  printf("foo out: %s\n",   out.name);

  sleep(1);
  exit(0);
}

When I run this, the server is fine I see Value: 'foo' every time it is called via the client. However the client crashes with a segv. Using address sanitizer I'm getting this:

=================================================================
==8269==ERROR: AddressSanitizer: SEGV on unknown address 0x0000004b0503 (pc 0x7fc9e2b5160f bp 0x000000000003 sp 0x7ffe264190c0 T0)
    #0 0x7fc9e2b5160e in xdr_string (/lib/x86_64-linux-gnu/libc.so.6+0x13b60e)
    #1 0x460751 in __interceptor_xdr_string.part.268 (/vagrant/foo/foo_client+0x460751)
    #2 0x4b04a7 in xdr_foo_out /vagrant/foo/foo_xdr.c:13
    #3 0x7fc9e2b4b3ea  (/lib/x86_64-linux-gnu/libc.so.6+0x1353ea)
    #4 0x4b03b8 in foo_2 /vagrant/foo/foo_clnt.c:15
    #5 0x4b042e in main /vagrant/foo/foo_client.c:28
    #6 0x7fc9e2a3682f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #7 0x405298 in _start (/vagrant/foo/foo_client+0x405298)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV ??:0 xdr_string
==8269==ABORTING

When I use GDB to inspect the contents of the struct before and after thefoo_2 call, it hasn't changed. It looks like the server didn't modify the value at all.

Question: How do you modify a struct with a string using multithreaded RPC calls? If I change my example to use an integer instead of a string, it works fine. So it seems like i'm missing something basic. Any ideas on what i'm doing wrong or steps to take to debug this?

1
Someone on Reddit found the issue reddit.com/r/C_Programming/comments/6p8smu/… - Schneems

1 Answers

0
votes

In your client try allocating memory for the foo_out struct:

foo_out *out = malloc(sizeof(foo_out));
if (foo_2(out, cl) != RPC_SUCCESS) {
  printf("failed \n");
  // exit(1);
}
printf("foo out: %s\n",   out->name);