NetBSD Problem Report #57699

From www@netbsd.org  Fri Nov 17 09:41:25 2023
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 07B331A9238
	for <gnats-bugs@gnats.NetBSD.org>; Fri, 17 Nov 2023 09:41:25 +0000 (UTC)
Message-Id: <20231117094123.724A01A9239@mollari.NetBSD.org>
Date: Fri, 17 Nov 2023 09:41:23 +0000 (UTC)
From: code@yoyomail.de
Reply-To: code@yoyomail.de
To: gnats-bugs@NetBSD.org
Subject: Memory leak in libprop?
X-Send-Pr-Version: www-1.0

>Number:         57699
>Category:       lib
>Synopsis:       Memory leak in libprop?
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    thorpej
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Nov 17 09:45:00 +0000 2023
>Closed-Date:    Sun Nov 26 13:14:18 +0000 2023
>Last-Modified:  Sun Nov 26 13:14:18 +0000 2023
>Originator:     M. Boerschig
>Release:        10.0_RC1
>Organization:
>Environment:
NetBSD mybox 10.0_RC1 NetBSD 10.0_RC1 (GENERIC) #0: Sun Nov  5 18:30:08 UTC 2023  mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC amd64

>Description:
I noticed a memory leak while writing a widget for monitoring my laptop battery using envsys.
I boiled it down to libprop, see the reproducer listed below.
First thought was I didn't use libprop properly, but adress sanitizer complains, too.

Also: `envstat -i 1` leaks slowly memory -- the resident memory grows continuously when watched using `top` (might take a couple of hours).


>How-To-Repeat:
The following program will report an adress sanitizer leak:

// compile: gcc -O0 -o libprop_leak libprop_leak.c -lprop -fsanitize=address 
#include <sys/envsys.h>
#include <prop/proplib.h>
#include <paths.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    //while(1) {
    prop_dictionary_t dict;
    int result = 0;
    int sysmonfd = -1;
    if ((sysmonfd = open(_PATH_SYSMON, O_RDONLY)) == -1) {
        return -1;
    }
    result = prop_dictionary_recv_ioctl(sysmonfd, ENVSYS_GETDICTIONARY, &dict);
    if(result == -1) {
        return -2;
    }
    prop_object_release(dict);
    (void)close(sysmonfd);
   // }
}

Will result in the following sanitizer output:

=================================================================
==22658==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 194 byte(s) in 25 object(s) allocated from:
    #0 0x7f7ff6e435ff in malloc (/usr/lib/libasan.so.5+0x435ff)
    #1 0x7f7ff6a092d6 in _prop_string_internalize (/usr/lib/libprop.so.1+0x92d6)
    #2 0x7f7ff6a09cc6 in _prop_object_internalize_by_tag (/usr/lib/libprop.so.1+0x9cc6)
    #3 0x7f7ff6a0a612 in _prop_generic_internalize (/usr/lib/libprop.so.1+0xa612)
    #4 0x7f7ff6a06f92 in prop_dictionary_recv_ioctl (/usr/lib/libprop.so.1+0x6f92)
    #5 0x400dc8 in main (/build/mb/battery/./libprop_leak+0x400dc8)
    #6 0x400c6c in ___start /usr/src/lib/csu/common/crt0-common.c:350:2
    #7 0x7f7ff7c0baf7  (/usr/libexec/ld.elf_so+0xbaf7)

SUMMARY: AddressSanitizer: 194 byte(s) leaked in 25 allocation(s).
>Fix:

>Release-Note:

>Audit-Trail:

Responsible-Changed-From-To: lib-bug-people->thorpej
Responsible-Changed-By: thorpej@NetBSD.org
Responsible-Changed-When: Fri, 17 Nov 2023 17:35:25 +0000
Responsible-Changed-Why:
My fault, easy fix.


State-Changed-From-To: open->analyzed
State-Changed-By: thorpej@NetBSD.org
State-Changed-When: Fri, 17 Nov 2023 17:35:25 +0000
State-Changed-Why:
Fix in progress.


From: Jason Thorpe <thorpej@me.com>
To: code@yoyomail.de
Cc: Jason Thorpe <thorpej@netbsd.org>,
 gnats-bugs@netbsd.org,
 lib-bug-people@netbsd.org,
 netbsd-bugs@netbsd.org,
 gnats-admin@netbsd.org
Subject: Re: lib/57699 (Memory leak in libprop?)
Date: Fri, 17 Nov 2023 10:53:45 -0800

 > On Nov 17, 2023, at 9:35 AM, thorpej@netbsd.org <thorpej@NetBSD.org> wrote:
 > 
 > State-Changed-From-To: open->analyzed
 > State-Changed-By: thorpej@NetBSD.org
 > State-Changed-When: Fri, 17 Nov 2023 17:35:25 +0000
 > State-Changed-Why:
 > Fix in progress.

 Can you give this patch a whirl?

 	https://www.netbsd.org/~thorpej/pr-57699-diff-2.txt

 -- thorpej

From: "M. Boerschig" <code@yoyomail.de>
To: gnats-bugs@netbsd.org, thorpej@netbsd.org, gnats-admin@netbsd.org,
 netbsd-bugs@netbsd.org
Cc: 
Subject: Re: lib/57699 (Memory leak in libprop?)
Date: Fri, 17 Nov 2023 20:38:39 +0100

 Am 17.11.23 um 19:55 schrieb Jason Thorpe:
 > The following reply was made to PR lib/57699; it has been noted by GNATS.
 > 
 > From: Jason Thorpe <thorpej@me.com>
 > To: code@yoyomail.de
 > Cc: Jason Thorpe <thorpej@netbsd.org>,
 >  gnats-bugs@netbsd.org,
 >  lib-bug-people@netbsd.org,
 >  netbsd-bugs@netbsd.org,
 >  gnats-admin@netbsd.org
 > Subject: Re: lib/57699 (Memory leak in libprop?)
 > Date: Fri, 17 Nov 2023 10:53:45 -0800
 > 
 >  > On Nov 17, 2023, at 9:35 AM, thorpej@netbsd.org <thorpej@NetBSD.org> wrote:
 >  > 
 >  > State-Changed-From-To: open->analyzed
 >  > State-Changed-By: thorpej@NetBSD.org
 >  > State-Changed-When: Fri, 17 Nov 2023 17:35:25 +0000
 >  > State-Changed-Why:
 >  > Fix in progress.
 >  
 >  Can you give this patch a whirl?
 >  
 >  	https://www.netbsd.org/~thorpej/pr-57699-diff-2.txt
 >  
 >  -- thorpej
 >  

 Thanks for the quick response.
 The provided patch works.
 ASan no longer complains, also `envstat` doesn't eat memory anymore.
 Will this be included in the 10 release?


From: Jason Thorpe <thorpej@me.com>
To: "M. Boerschig" <code@yoyomail.de>
Cc: gnats-bugs@netbsd.org, thorpej@netbsd.org, gnats-admin@netbsd.org,
 netbsd-bugs@netbsd.org
Subject: Re: lib/57699 (Memory leak in libprop?)
Date: Fri, 17 Nov 2023 11:48:38 -0800

 Yup!  Thanks for verifying the fix!

 -- thorpej
 Sent from my iPhone.

 > On Nov 17, 2023, at 11:38=E2=80=AFAM, M. Boerschig <code@yoyomail.de> wrot=
 e:
 >=20
 > =EF=BB=BFAm 17.11.23 um 19:55 schrieb Jason Thorpe:
 >> The following reply was made to PR lib/57699; it has been noted by GNATS.=

 >>=20
 >> From: Jason Thorpe <thorpej@me.com>
 >> To: code@yoyomail.de
 >> Cc: Jason Thorpe <thorpej@netbsd.org>,
 >> gnats-bugs@netbsd.org,
 >> lib-bug-people@netbsd.org,
 >> netbsd-bugs@netbsd.org,
 >> gnats-admin@netbsd.org
 >> Subject: Re: lib/57699 (Memory leak in libprop?)
 >> Date: Fri, 17 Nov 2023 10:53:45 -0800
 >>=20
 >>>> On Nov 17, 2023, at 9:35 AM, thorpej@netbsd.org <thorpej@NetBSD.org> wr=
 ote:
 >>>=20
 >>> State-Changed-From-To: open->analyzed
 >>> State-Changed-By: thorpej@NetBSD.org
 >>> State-Changed-When: Fri, 17 Nov 2023 17:35:25 +0000
 >>> State-Changed-Why:
 >>> Fix in progress.
 >>=20
 >> Can you give this patch a whirl?
 >>=20
 >>    https://www.netbsd.org/~thorpej/pr-57699-diff-2.txt
 >>=20
 >> -- thorpej
 >>=20
 >=20
 > Thanks for the quick response.
 > The provided patch works.
 > ASan no longer complains, also `envstat` doesn't eat memory anymore.
 > Will this be included in the 10 release?
 >=20
 >=20

From: "Jason R Thorpe" <thorpej@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/57699 CVS commit: src/common/lib/libprop
Date: Fri, 17 Nov 2023 21:29:33 +0000

 Module Name:	src
 Committed By:	thorpej
 Date:		Fri Nov 17 21:29:33 UTC 2023

 Modified Files:
 	src/common/lib/libprop: prop_string.c

 Log Message:
 In _prop_string_instantiate(), when we de-dup a non-MUTABLE string, make
 sure we free the provided string buffer if NOCOPY is not set.  Fixes
 a memory leak reported by M. Boerschig.

 While we're at it, also change _prop_string_instantiate() to free the
 provided string buffer in the not-NOCOPY case when string object allocation
 fails (this was previously handled by _prop_string_instantiate()'s
 callers).

 PR lib/57699


 To generate a diff of this commit:
 cvs rdiff -u -r1.17 -r1.18 src/common/lib/libprop/prop_string.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: analyzed->pending-pullups
State-Changed-By: thorpej@NetBSD.org
State-Changed-When: Fri, 17 Nov 2023 21:36:28 +0000
State-Changed-Why:
Pullup to netbsd-10 requested.


From: "Manuel Bouyer" <bouyer@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/57699 CVS commit: [netbsd-10] src/common/lib/libprop
Date: Sun, 26 Nov 2023 12:29:49 +0000

 Module Name:	src
 Committed By:	bouyer
 Date:		Sun Nov 26 12:29:49 UTC 2023

 Modified Files:
 	src/common/lib/libprop [netbsd-10]: prop_string.c

 Log Message:
 Pull up following revision(s) (requested by thorpej in ticket #467):
 	common/lib/libprop/prop_string.c: revision 1.18
 In _prop_string_instantiate(), when we de-dup a non-MUTABLE string, make
 sure we free the provided string buffer if NOCOPY is not set.  Fixes
 a memory leak reported by M. Boerschig.
 While we're at it, also change _prop_string_instantiate() to free the
 provided string buffer in the not-NOCOPY case when string object allocation
 fails (this was previously handled by _prop_string_instantiate()'s
 callers).
 PR lib/57699


 To generate a diff of this commit:
 cvs rdiff -u -r1.17 -r1.17.2.1 src/common/lib/libprop/prop_string.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: pending-pullups->closed
State-Changed-By: thorpej@NetBSD.org
State-Changed-When: Sun, 26 Nov 2023 13:14:18 +0000
State-Changed-Why:
Pullup to netbsd-10 branch completed.  Originator already confirmed fix.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2023 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.