Bug 14550 - Loading a custom JIT reader plugin crashes GDB during backtrace
Summary: Loading a custom JIT reader plugin crashes GDB during backtrace
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: backtrace (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 7.6
Assignee: Sanjoy Das
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-09-06 09:29 UTC by Sanjoy Das
Modified: 2013-02-01 20:41 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sanjoy Das 2012-09-06 09:29:34 UTC
Sample code (from Michael Haupt <michael.haupt@oracle.com> with minor modifications):

To crash GDB, jit-reader-load myreader.c and break inside the `func` function in jithost.c

myreader.c: 

#include "gdb/jit-reader.h"

GDB_DECLARE_GPL_COMPATIBLE_READER;

static enum gdb_status read_debug_info(struct gdb_reader_funcs* self,
                                       struct gdb_symbol_callbacks* cbs,
                                       void* memory, long memory_sz) {
    GDB_CORE_ADDR* buf = (GDB_CORE_ADDR*) memory;
    GDB_CORE_ADDR start = buf[0];
    GDB_CORE_ADDR end = buf[1];

    struct gdb_object* object = cbs->object_open(cbs);
    struct gdb_symtab* symtab = cbs->symtab_open(cbs, object, "");
    struct gdb_block* block = cbs->block_open(cbs, symtab, NULL, start, end,
                                              "fluffy");

    cbs->symtab_close(cbs, symtab);
    cbs->object_close(cbs, object);
    return GDB_SUCCESS;
}

enum gdb_status unwind_frame(struct gdb_reader_funcs* self,
                             struct gdb_unwind_callbacks* cbs) {
    return GDB_SUCCESS;
}

struct gdb_frame_id get_frame_id(struct gdb_reader_funcs* self,
                                 struct gdb_unwind_callbacks* cbs) {
    struct gdb_frame_id frame = { 0x602000, 0 };
    return frame;
}

void destroy_reader(struct gdb_reader_funcs* self) { }

extern struct gdb_reader_funcs* gdb_init_reader(void) {
    static struct gdb_reader_funcs funcs = {
        GDB_READER_INTERFACE_VERSION,
	NULL,
	read_debug_info,
	unwind_frame,
	get_frame_id,
	destroy_reader
    };
    return &funcs;
}


jithost.c:


#include <stdio.h>
#include <sys/mman.h>

#include "gdb/jit-reader.h"
#include "defs.h"
#include "jit.h"

struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
struct jit_code_entry entry = { 0, 0, 0, 0 };

typedef void (*func)();

void __attribute__((noinline)) __jit_debug_register_code() {
    __asm("");
}

#define SIZE 4096

int main(int argc, char** argv) {
    char* code = (char*) valloc(SIZE);
    GDB_CORE_ADDR* buffer = (GDB_CORE_ADDR*) valloc(SIZE);
    fprintf(stderr, "buffer at %p, code at %p\n", buffer, code);

    if (0 != mprotect(code, SIZE, PROT_WRITE | PROT_READ | PROT_EXEC)) {
        perror(NULL);
	exit(3);
    }

    code[0] = 0xc3; // RET
    buffer[0] = (GDB_CORE_ADDR) code; // store code address in buffer
    buffer[1] = (GDB_CORE_ADDR) (code + 1); // and the end of the generated "function"

    entry.symfile_addr = (GDB_CORE_ADDR) buffer;
    entry.symfile_size = SIZE;

    __jit_debug_descriptor.first_entry =
         __jit_debug_descriptor.relevant_entry = (GDB_CORE_ADDR) &entry;
    __jit_debug_descriptor.action_flag = JIT_REGISTER;
    __jit_debug_descriptor.version = 1;
    __jit_debug_register_code();

    (((func)(code)))();

    return 0;
}
Comment 1 Sourceware Commits 2013-01-17 14:11:35 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	sanjoyd@sourceware.org	2013-01-17 14:11:27

Modified files:
	gdb            : ChangeLog jit.c 

Log message:
	PR gdb/14550
	
	* jit.c (finalize_symtab): Ensure that only the global block has a
	NULL superblock.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/ChangeLog.diff?cvsroot=src&r1=1.15024&r2=1.15025
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/jit.c.diff?cvsroot=src&r1=1.42&r2=1.43
Comment 2 Tom Tromey 2013-02-01 20:41:43 UTC
IIUC this is fixed now.