public inbox for jit@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: zhao xin <esrrhs@163.com>, jit@gcc.gnu.org
Subject: Re: BUG?: memory leak when compile
Date: Fri, 08 Dec 2023 09:37:48 -0500	[thread overview]
Message-ID: <a904f07a9f87f3cc022946e1d446c48b662b7666.camel@redhat.com> (raw)
In-Reply-To: <50fd9986.4898.18c48b89163.Coremail.esrrhs@163.com>

On Fri, 2023-12-08 at 17:18 +0800, zhao xin wrote:
> Hi, I write a simple code below:
> -------------------------------------
> #include <libgccjit.h>
> 
> #include <stdlib.h>
> #include <stdio.h>
> #include <dlfcn.h>
> 
> __attribute__((used)) void called_function(int i, int j, int k) {
> 
> }
> 
> int
> main(int argc, char **argv) {
>     while (1) {
>         gcc_jit_context *ctxt = gcc_jit_context_acquire();
>         /* Let's try to inject the equivalent of:
>          extern void called_function (int i, int j, int k);
> 
>             void
>             test_caller (int a)
>             {
>                 called_function (a * 3, a * 4, a * 5);
>             }
>             */
>         int i;
>         gcc_jit_type *void_type = gcc_jit_context_get_type(ctxt,
> GCC_JIT_TYPE_VOID);
>         gcc_jit_type *int_type = gcc_jit_context_get_type(ctxt,
> GCC_JIT_TYPE_INT);
> 
>         /* Declare the imported function.  */
>         gcc_jit_param *params[3];
>         params[0] = gcc_jit_context_new_param(ctxt, NULL, int_type,
> "i");
>         params[1] = gcc_jit_context_new_param(ctxt, NULL, int_type,
> "j");
>         params[2] = gcc_jit_context_new_param(ctxt, NULL, int_type,
> "k");
>         gcc_jit_function *called_fn =
>                 gcc_jit_context_new_function(ctxt, NULL,
> GCC_JIT_FUNCTION_IMPORTED, void_type, "called_function", 3,
>                                              params,
>                                              0);
> 
>         /* Build the test_fn.  */
>         gcc_jit_param *param_a = gcc_jit_context_new_param(ctxt,
> NULL, int_type, "a");
>         gcc_jit_function *test_fn =
>                 gcc_jit_context_new_function(ctxt, NULL,
> GCC_JIT_FUNCTION_EXPORTED, void_type, "test_caller", 1,
>                                              &param_a,
>                                              0);
>         /* "a * 3, a * 4, a * 5"  */
>         gcc_jit_rvalue *args[3];
>         for (i = 0; i < 3; i++)
>             args[i] = gcc_jit_context_new_binary_op(ctxt, NULL,
> GCC_JIT_BINARY_OP_MULT, int_type,
>                                                    
> gcc_jit_param_as_rvalue(param_a),
>                                                    
> gcc_jit_context_new_rvalue_from_int(ctxt, int_type, (i + 3)));
>         gcc_jit_block *block = gcc_jit_function_new_block(test_fn,
> NULL);
>         gcc_jit_block_add_eval(block, NULL,
> gcc_jit_context_new_call(ctxt, NULL, called_fn, 3, args));
>         gcc_jit_block_end_with_void_return(block, NULL);
> 
>         /* Compile the function.  */
>         gcc_jit_result *result = gcc_jit_context_compile(ctxt);
> 
>         gcc_jit_context_release(ctxt);
>         gcc_jit_result_release(result);
>     }
>     return 0;
> }
> 
> -------------------------------------
> most of it copy from the gcc/testsuite/jit.dg/test-calling-external-
> function.c
> and then compile it with command:
> 
> -------------------------------------
> gcc    test.c     -o test     -lgccjit -Wl,-E
> -------------------------------------
> then, just run 
> -------------------------------------
> ./test
> -------------------------------------
> and the memory will grow larger and larger.
> 
> 
> and use valgrind also can find many memory leak output, just like
> 
> -------------------------------------
> ==15303== 8,128 bytes in 2 blocks are definitely lost in loss record
> 702 of 731
> ==15303==    at 0x4C29EC3: malloc (vg_replace_malloc.c:309)
> ==15303==    by 0x6953727: xmalloc (in
> /usr/local/lib/libgccjit.so.0.0.1)
> ==15303==    by 0x694E7A4: _obstack_begin_worker (in
> /usr/local/lib/libgccjit.so.0.0.1)
> ==15303==    by 0x55E03BD: driver::putenv_COLLECT_GCC(char const*)
> const (in /usr/local/lib/libgccjit.so.0.0.1)
> ==15303==    by 0x558922A: driver::main(int, char**) (in
> /usr/local/lib/libgccjit.so.0.0.1)
> ==15303==    by 0x55C2EE0:
> gcc::jit::playback::context::invoke_embedded_driver(vec<char*,
> va_heap, vl_ptr> const*) (in /usr/local/lib/libgccjit.so.0.0.1)
> ==15303==    by 0x55C55A2:
> gcc::jit::playback::context::invoke_driver(char const*, char const*,
> char const*, timevar_id_t, bool, bool) (in
> /usr/local/lib/libgccjit.so.0.0.1)
> ==15303==    by 0x55C6422:
> gcc::jit::playback::context::convert_to_dso(char const*) (in
> /usr/local/lib/libgccjit.so.0.0.1)
> ==15303==    by 0x55C646F:
> gcc::jit::playback::compile_to_memory::postprocess(char const*) (in
> /usr/local/lib/libgccjit.so.0.0.1)
> ==15303==    by 0x55C4D2D: gcc::jit::playback::context::compile() (in
> /usr/local/lib/libgccjit.so.0.0.1)
> ==15303==    by 0x55B996D: gcc::jit::recording::context::compile()
> (in /usr/local/lib/libgccjit.so.0.0.1)
> ==15303==    by 0x55A9B70: gcc_jit_context_compile (in
> /usr/local/lib/libgccjit.so.0.0.1)
> 
> -------------------------------------
> 
> 
> then I gdb the code, found driver::putenv_COLLECT_GCC malloc first,
> and then driver::maybe_putenv_COLLECT_LTO_WRAPPER malloc again, so
> the before malloc memory leaked.
> and that is just one of the memory leake.
> 
> 
> so, is my code not correct or there is has bug?

Which version of libgccjit is this?

This looks like a leak in libgccjit; sorry about this.

We've been trying to fix them here:
  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63854

Dave

  reply	other threads:[~2023-12-08 14:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-08  9:18 zhao xin
2023-12-08 14:37 ` David Malcolm [this message]
2023-12-08 16:13   ` zhao xin

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=a904f07a9f87f3cc022946e1d446c48b662b7666.camel@redhat.com \
    --to=dmalcolm@redhat.com \
    --cc=esrrhs@163.com \
    --cc=jit@gcc.gnu.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).