v1.1.43 hangs with GLib/GDK threads

lowhalo@hush.com lowhalo@hush.com
Sun, 7 Sep 2003 14:25:13 -0700


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi --

> The CVS version contains the new thread handling code, would be cool,

> if you could give that a try.

Sorry I didn't get back to you sooner.  I was trying to fight through
some
packaging issues in order to get a release out for my project.

I tried using v1.1.43 with Ultramagnetic and unfortunately, I still have
the same problem.  After GTK/GDK threads are initialized, libgcrypt
hangs randomly inside some pthread functions.  I'm absolutely certain
this
is libgcrypt's fault because it does not misbehave if I disable
HAVE_PTHREAD in config.h.

Its worthy to mention that these hangs sometimes occur when calling
g_mutex_lock() from my own code.  This suggests that libgcrypt is somehow
handling pthreads in a very instable way, though I'm too pressed for
time
to examine it more fully.

Below is a complete program which reproduces this deadlock issue with
the
latest v1.1.43.

    - low halo


- ---- SNIP ----

/* Licensed under the GPL v2.0. */
/* Compile with:
 * gcc -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include \
 *     -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -o main main.c
\
 *     `gtk-config --libs` `glib-config --libs` -pthread -lglib-2.0
 *     -lgmodule-2.0 -lgobject-2.0 -lgthread-2.0 -lgtk-x11-2.0 -lgcrypt
 */
#include <stdio.h>
#include <glib.h>
#include <gcrypt.h>

#define REQUIRED_LIBGCRYPT_VERSION "1.1.43"

gcry_sexp_t *key = NULL;
GMutex *mutex = NULL;

gpointer thread_function(gpointer nothing);
void init_gcrypt(void);
void perform_gcrypt_function(void);
void generate_key(void);

int main(int ac, char **av) {

  int i = 0;
  GThread *thread = NULL;

  init_gcrypt();
  g_thread_init(NULL);
  gdk_threads_init();

  generate_key();

  mutex = g_mutex_new();

  thread = g_thread_create(&thread_function, NULL, FALSE, NULL);

  while(1) {
    g_mutex_lock(mutex);
    if (rand() % 13 == 0)
      sleep(1);

    perform_gcrypt_function();

    printf("tick: %d\n", i++);

    g_mutex_unlock(mutex);
  }

  return 0;
}

void init_gcrypt(void) {

  if (!gcry_control(GCRYCTL_ANY_INITIALIZATION_P)) {
    if (!gcry_check_version(REQUIRED_LIBGCRYPT_VERSION)) {
      printf("ERROR:  this version of ultramagnetic requires libgcrypt
v%s. "
             "You only have v%s.\n", REQUIRED_LIBGCRYPT_VERSION,
	     gcry_check_version(NULL));
      exit(1);
    }
    gcry_control(GCRYCTL_INIT_SECMEM, 1024 * 1024 * 3);
    gcry_control(GCRYCTL_INITIALIZATION_FINISHED);
  }
  printf("libgcrypt v%s initialized.\n", gcry_check_version(NULL));
}

void generate_key(void) {

  gcry_sexp_t params;

  key = g_new0(gcry_sexp_t, 1);

  printf("Generating key...");
  fflush(stdout);

  gcry_sexp_build(&params, NULL, "(genkey(elg(nbits %d)))", 512);
  if (gcry_pk_genkey(key, params) != 0) {
    printf("Error generating key.\n");
    exit(1);
  }
  printf("done.\n");

}


gpointer thread_function(gpointer nothing) {

  int j = 0;
  while(1) {
    g_mutex_lock(mutex);

    if (rand() % 7 == 0)
      sleep(1);

    printf("tock: %d\n", j++);
    g_mutex_unlock(mutex);
  }

}


void perform_gcrypt_function(void) {

  gcry_mpi_t an_mpi;
  gcry_sexp_t an_sexp, encrypted_sexp;
  int err = 0;
  unsigned char buffer[] = "Encrypt this!";
  int buffer_len = strlen(buffer);
  gcry_error_t ret;

  if (gcry_mpi_scan(&an_mpi, GCRYMPI_FMT_USG, buffer, buffer_len, NULL)
!= 0) {
    printf("gcry_mpi_scan failed.\n");
    exit(1);
  }
  
  if (gcry_sexp_build(&an_sexp, &err,
		      "(data(flags)(value %m))", an_mpi) != 0) {
    printf("gcry_sexp_build failed.\n");
    exit(1);
  }


  if ((ret = gcry_pk_encrypt(&encrypted_sexp, an_sexp, *key)) != 0) {
    printf("encryption failed: %s\n", gcry_strerror(ret));
    exit(1);
  }

}

- ---- SNIP ----

- --
low halo <lowhalo at-s1gn hush d0t c0m>
Defender of Truth and Liberty
http://ultramagnetic.sourceforge.net/

http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x9BFD99BF
58CE 3215 226A 69ED 4D20 4044 C925 54F9 9BFD 99BF

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/W6K+ySVU+Zv9mb8RAgNQAJwI7aIq3soMMEnW/I5cPG5kxxHPNwCgpvKM
9AkC0OZ5TuQ6qAI/L/gqIhw=
=2u+S
-----END PGP SIGNATURE-----




Concerned about your privacy? Follow this link to get
FREE encrypted email: https://www.hushmail.com/?l=2

Free, ultra-private instant messaging with Hush Messenger
https://www.hushmail.com/services.php?subloc=messenger&l=434

Promote security and make money with the Hushmail Affiliate Program: 
https://www.hushmail.com/about.php?subloc=affiliate&l=427