public inbox for jit@gcc.gnu.org
 help / color / mirror / Atom feed
* BUG?: memory leak when compile
@ 2023-12-08  9:18 zhao xin
  2023-12-08 14:37 ` David Malcolm
  0 siblings, 1 reply; 3+ messages in thread
From: zhao xin @ 2023-12-08  9:18 UTC (permalink / raw)
  To: jit

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

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?


thanks.







^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-12-08 16:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-08  9:18 BUG?: memory leak when compile zhao xin
2023-12-08 14:37 ` David Malcolm
2023-12-08 16:13   ` zhao xin

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).