mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Roman Yeryomin <roman@ubnt.com>
To: musl <musl@lists.openwall.com>
Subject: [RFC] malloc + threads = memleak?
Date: Fri, 9 Feb 2018 16:33:38 +0200	[thread overview]
Message-ID: <CADA+G1tvfRqOo2-Djy4VuCW=PXieGd4O8kYwYHhVErJHukMD-Q@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 377 bytes --]

Hello!

I have a very easy reproducible memory leak here when using
malloc/free in two threads.
Attaching a simple test.
thread() here is calling pthread_create():
https://github.com/yeryomin/liba/blob/master/liba.c#L181

Do I misunderstand something or is it a bug?
This can be avoided by wrapping malloc into locks but not sure if
that's the fix you want...


Regards,
Roman

[-- Attachment #2: musl-test.c --]
[-- Type: text/x-csrc, Size: 1426 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

#include <liba.h>

#define PIDPATH		"/tmp"
#define NAME		argv[0]
#define TIMER		30

#define SIZE		10000

int timer = TIMER;
int counter = 0;

void thread42( int *thr )
{
	int i;
	void* allocs[SIZE];

	while (1) {
		for ( i = 0; i < SIZE; i++ )
			allocs[i] = malloc(1500);
		
		syslog( LOG_INFO, "%i: %ld\n", *thr, time(NULL) );

		for ( i = 0; i < SIZE; i++ )
			free(allocs[i]);
	}
}

void reset_timer( int sig )
{
	/* temporarily ingnore interrupts */
	signal( sig, SIG_IGN );
	counter++;
	syslog( LOG_INFO, "%i HUP received, resetting timer (was %i)...",
				counter, timer );
	timer = TIMER;
	/* reenable interrupt */
	a_signal( sig, reset_timer );
}

int main( int argc, char **argv )
{
	int pid = daemonize( PIDPATH, NAME );
	int thr1 = 1;
	int thr2 = 2;

	/* register sighup handler for daemon */
	a_signal( SIGHUP, reset_timer );

	/* start two malloc threads */
	thread( "thread42-1", &thread42, &thr1 );
	thread( "thread42-2", &thread42, &thr2 );

	/* daemon stuff */
	syslog( LOG_INFO, "Working hard..." );
	while ( timer > 0 ) {
		sleep(1);
		timer--;
		if ( timer < 5 )
			syslog( LOG_INFO, "Dying in %i...", timer + 1 );
	}

	/* cleanup, if ever get here */
	syslog( LOG_INFO, "Exiting %s (%i)...", a_daemon_name, pid );
	unlink( a_daemon_pidfile );
	exit(EXIT_SUCCESS);
}

             reply	other threads:[~2018-02-09 14:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-09 14:33 Roman Yeryomin [this message]
2018-02-09 15:00 ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADA+G1tvfRqOo2-Djy4VuCW=PXieGd4O8kYwYHhVErJHukMD-Q@mail.gmail.com' \
    --to=roman@ubnt.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).