)]}'
{
  "commit": "82d23ca811ab365217fdc9bda215e23adb5e4a30",
  "tree": "b02efe54c8d3eb47f780605eff20dac625e8eace",
  "parents": [
    "343c2cbd8577101a8057d1459810ff983d07943c"
  ],
  "author": {
    "name": "Simon Marchi",
    "email": "simon.marchi@polymtl.ca",
    "time": "Thu Sep 29 16:14:40 2022 -0400"
  },
  "committer": {
    "name": "Simon Marchi",
    "email": "simon.marchi@efficios.com",
    "time": "Tue Oct 11 13:52:18 2022 -0400"
  },
  "message": "gdb: fix auxv caching\n\nThere\u0027s a flaw in the interaction of the auxv caching and the fact that\ntarget_auxv_search allows reading auxv from an arbitrary target_ops\n(passed in as a parameter).  This has consequences as explained in this\nthread:\n\n  https://inbox.sourceware.org/gdb-patches/20220719144542.1478037-1-luis.machado@arm.com/\n\nIn summary, when loading an AArch64 core file with MTE support by\npassing the executable and core file names directly to GDB, we see the\nMTE info:\n\n    $ ./gdb -nx --data-directory\u003ddata-directory -q aarch64-mte-gcore aarch64-mte-gcore.core\n    ...\n    Program terminated with signal SIGSEGV, Segmentation fault\n    Memory tag violation while accessing address 0x0000ffff8ef5e000\n    Allocation tag 0x1\n    Logical tag 0x0.\n    #0  0x0000aaaade3d0b4c in ?? ()\n    (gdb)\n\nBut if we do it as two separate commands (file and core) we don\u0027t:\n\n    $ ./gdb -nx --data-directory\u003ddata-directory -q -ex \"file aarch64-mte-gcore\" -ex \"core aarch64-mte-gcore.core\"\n    ...\n    Program terminated with signal SIGSEGV, Segmentation fault.\n    #0  0x0000aaaade3d0b4c in ?? ()\n    (gdb)\n\nThe problem with the latter is that auxv data gets improperly cached\nbetween the two commands.  When executing the file command, auxv gets\nfirst queried here, when loading the executable:\n\n    #0  target_auxv_search (ops\u003d0x55555b842400 \u003cexec_ops\u003e, match\u003d0x9, valp\u003d0x7fffffffc5d0) at /home/simark/src/binutils-gdb/gdb/auxv.c:383\n    #1  0x0000555557e576f2 in svr4_exec_displacement (displacementp\u003d0x7fffffffc8c0) at /home/simark/src/binutils-gdb/gdb/solib-svr4.c:2482\n    #2  0x0000555557e594d1 in svr4_relocate_main_executable () at /home/simark/src/binutils-gdb/gdb/solib-svr4.c:2878\n    #3  0x0000555557e5989e in svr4_solib_create_inferior_hook (from_tty\u003d1) at /home/simark/src/binutils-gdb/gdb/solib-svr4.c:2933\n    #4  0x0000555557e6e49f in solib_create_inferior_hook (from_tty\u003d1) at /home/simark/src/binutils-gdb/gdb/solib.c:1253\n    #5  0x0000555557f33e29 in symbol_file_command (args\u003d0x7fffffffe01c \"aarch64-mte-gcore\", from_tty\u003d1) at /home/simark/src/binutils-gdb/gdb/symfile.c:1655\n    #6  0x00005555573319c3 in file_command (arg\u003d0x7fffffffe01c \"aarch64-mte-gcore\", from_tty\u003d1) at /home/simark/src/binutils-gdb/gdb/exec.c:555\n    #7  0x0000555556e47185 in do_simple_func (args\u003d0x7fffffffe01c \"aarch64-mte-gcore\", from_tty\u003d1, c\u003d0x612000047740) at /home/simark/src/binutils-gdb/gdb/cli/cli-decode.c:95\n    #8  0x0000555556e551c9 in cmd_func (cmd\u003d0x612000047740, args\u003d0x7fffffffe01c \"aarch64-mte-gcore\", from_tty\u003d1) at /home/simark/src/binutils-gdb/gdb/cli/cli-decode.c:2543\n    #9  0x00005555580e63fd in execute_command (p\u003d0x7fffffffe02c \"e\", from_tty\u003d1) at /home/simark/src/binutils-gdb/gdb/top.c:692\n    #10 0x0000555557771913 in catch_command_errors (command\u003d0x5555580e55ad \u003cexecute_command(char const*, int)\u003e, arg\u003d0x7fffffffe017 \"file aarch64-mte-gcore\", from_tty\u003d1, do_bp_actions\u003dtrue) at /home/simark/src/binutils-gdb/gdb/main.c:513\n    #11 0x0000555557771fba in execute_cmdargs (cmdarg_vec\u003d0x7fffffffd570, file_type\u003dCMDARG_FILE, cmd_type\u003dCMDARG_COMMAND, ret\u003d0x7fffffffd230) at /home/simark/src/binutils-gdb/gdb/main.c:608\n    #12 0x00005555577755ac in captured_main_1 (context\u003d0x7fffffffda10) at /home/simark/src/binutils-gdb/gdb/main.c:1299\n    #13 0x0000555557775c2d in captured_main (data\u003d0x7fffffffda10) at /home/simark/src/binutils-gdb/gdb/main.c:1320\n    #14 0x0000555557775cc2 in gdb_main (args\u003d0x7fffffffda10) at /home/simark/src/binutils-gdb/gdb/main.c:1345\n    #15 0x00005555568bdcbe in main (argc\u003d10, argv\u003d0x7fffffffdba8) at /home/simark/src/binutils-gdb/gdb/gdb.c:32\n\nHere, target_auxv_search is called on the inferior\u0027s target stack.  The\ntarget stack only contains the exec target, so the query returns empty\nauxv data.  This gets cached for that inferior in `auxv_inferior_data`.\n\nIn its constructor (before it is pushed to the inferior\u0027s target stack),\nthe core_target needs to identify the right target description from the\ncore, and for that asks the gdbarch to read a target description from\nthe core file.  Because some implementations of\ngdbarch_core_read_description (such as AArch64\u0027s) need to read auxv data\nfrom the core in order to determine the right target description, the\ncore_target passes a pointer to itself, allowing implementations to call\ntarget_auxv_search it.  However, because we have previously cached\n(empty) auxv data for that inferior, target_auxv_search searched that\ncached (empty) auxv data, not auxv data read from the core.  Remember\nthat this data was obtained by reading auxv on the inferior\u0027s target\nstack, which only contained an exec target.\n\nThe problem I see is that while target_auxv_search offers the\nflexibility of reading from an arbitrary (passed as an argument) target,\nthe caching doesn\u0027t do the distinction of which target is being queried,\nand where the cached data came from.  So, you could read auxv from a\ntarget A, it gets cached, then you try to read auxv from a target B, and\nit returns the cached data from target A.  That sounds wrong.  In our\ncase, we expect to read different auxv data from the core target than\nwhat we have read from the target stack earlier, so it doesn\u0027t make\nsense to hit the cache in this case.\n\nTo fix this, I propose splitting the code paths that read auxv data from\nan inferior\u0027s target stack and those that read from a passed-in target.\nThe code path that reads from the target stack will keep caching,\nwhereas the one that reads from a passed-in target won\u0027t.  And since,\nsearching in auxv data is independent from where this data came from,\nsplit the \"read\" part from the \"search\" part.\n\nFrom what I understand, auxv caching was introduced mostly to reduce\nlatency on remote connections, when doing many queries.  With the change\nI propose, only the queries done while constructing the core_target\nend up not using cached auxv data.  This is fine, because there are just\na handful of queries max, done at this point, and reading core files is\nlocal.\n\nThe changes to auxv functions are:\n\n - Introduce 2 target_read_auxv functions.  One reads from an explicit\n   target_ops and doesn\u0027t do caching (to be used in\n   gdbarch_core_read_description context).  The other takes no argument,\n   reads from the current inferior\u0027s target stack (it looks just like a\n   standard target function wrapper) and does caching.\n\n   The first target_read_auxv actually replaces get_auxv_inferior_data,\n   since it became a trivial wrapper around it.\n\n - Change the existing target_auxv_search to not read auxv data from the\n   target, but to accept it as a parameter (a gdb::byte_vector).  This\n   function doesn\u0027t care where the data came from, it just searches in\n   it.  It still needs to take a target_ops and gdbarch to know how to\n   parse auxv entries.\n\n - Add a convenience target_auxv_search overload that reads auxv\n   data from the inferior\u0027s target stack and searches in it.  This\n   overload is useful to replace the exist target_auxv_search calls that\n   passed the `current_inferior ()-\u003etop_target ()` target and keep the\n   call sites short.\n\n - Modify parse_auxv to accept a target_ops and gdbarch to use for\n   parsing entries.  Not strictly related to the rest of this change,\n   but it seems like a good change in the context.\n\nChanges in architecture-specific files (tdep and nat):\n\n - In linux-tdep, linux_get_hwcap and linux_get_hwcap2 get split in two,\n   similar to target_auxv_search.  One version receives auxv data,\n   target and arch as parameters.  The other gets everything from the\n   current inferior.  The latter is for convenience, to avoid making\n   call sites too ugly.\n\n - Call sites of linux_get_hwcap and linux_get_hwcap2 are adjusted to\n   use either of the new versions.  The call sites in\n   gdbarch_core_read_description context explicitly read auxv data from\n   the passed-in target and call the linux_get_hwcap{,2} function with\n   parameters.  Other call sites use the versions without parameters.\n\n - Same idea for arm_fbsd_read_description_auxv.\n\n - Call sites of target_auxv_search that passed\n   `current_inferior ()-\u003etop_target ()` are changed to use the\n   target_auxv_search overload that works in the current inferior.\n\nReviewed-By: John Baldwin \u003cjhb@FreeBSD.org\u003e\nReviewed-By: Luis Machado \u003cluis.machado@arm.com\u003e\nChange-Id: Ib775a220cf1e76443fb7da2fdff8fc631128fe66\n",
  "tree_diff": [
    {
      "type": "modify",
      "old_id": "eda79ec6d35ce126309cded13102e12ab4578324",
      "old_mode": 33188,
      "old_path": "gdb/aarch64-linux-nat.c",
      "new_id": "caefcb3648529c0237d260f07549299874d48948",
      "new_mode": 33188,
      "new_path": "gdb/aarch64-linux-nat.c"
    },
    {
      "type": "modify",
      "old_id": "0954e219a259847d890e7da1ff64d906684cf78a",
      "old_mode": 33188,
      "old_path": "gdb/aarch64-linux-tdep.c",
      "new_id": "476db5aa3b8821a81ab8a6dd829344d5a18300e6",
      "new_mode": 33188,
      "new_path": "gdb/aarch64-linux-tdep.c"
    },
    {
      "type": "modify",
      "old_id": "b161b7ed9080895702dd253f246b484f5f7e66d7",
      "old_mode": 33188,
      "old_path": "gdb/arm-fbsd-nat.c",
      "new_id": "bbd722ed9230974989e095fa45302bfd71dabd50",
      "new_mode": 33188,
      "new_path": "gdb/arm-fbsd-nat.c"
    },
    {
      "type": "modify",
      "old_id": "dabbceb743abf7046c7584b5de2121c7c138bf51",
      "old_mode": 33188,
      "old_path": "gdb/arm-fbsd-tdep.c",
      "new_id": "28fc73d694e6ba1e7a888ed075080cdb1edb2d45",
      "new_mode": 33188,
      "new_path": "gdb/arm-fbsd-tdep.c"
    },
    {
      "type": "modify",
      "old_id": "193eb76df3c98b628e0957255b8a11eacae667ad",
      "old_mode": 33188,
      "old_path": "gdb/arm-fbsd-tdep.h",
      "new_id": "85d7b59d1362ab7586587d935164f4548b6c9de5",
      "new_mode": 33188,
      "new_path": "gdb/arm-fbsd-tdep.h"
    },
    {
      "type": "modify",
      "old_id": "0188c78fe7a08155548a1116b3697129e736d661",
      "old_mode": 33188,
      "old_path": "gdb/arm-linux-nat.c",
      "new_id": "a8b582fbef32a44f774308b969db1ac8084a823d",
      "new_mode": 33188,
      "new_path": "gdb/arm-linux-nat.c"
    },
    {
      "type": "modify",
      "old_id": "86ab5794b378dcd8020807ceff105d15bb727b8d",
      "old_mode": 33188,
      "old_path": "gdb/arm-linux-tdep.c",
      "new_id": "65343f6c0758ad6c14e763370e44adee95ca66df",
      "new_mode": 33188,
      "new_path": "gdb/arm-linux-tdep.c"
    },
    {
      "type": "modify",
      "old_id": "76fc821c07c3d7e7acb778ff5617bc728de9b318",
      "old_mode": 33188,
      "old_path": "gdb/auxv.c",
      "new_id": "5853437b0f24b7d848b5c6d300f1159b5fd16c58",
      "new_mode": 33188,
      "new_path": "gdb/auxv.c"
    },
    {
      "type": "modify",
      "old_id": "ab2a5dee5f74f5905a61ee842ead217e0e3b94d9",
      "old_mode": 33188,
      "old_path": "gdb/auxv.h",
      "new_id": "983e3bc9b0d9fd48ce31ad415303a1058e5b6d23",
      "new_mode": 33188,
      "new_path": "gdb/auxv.h"
    },
    {
      "type": "modify",
      "old_id": "cea35fd49405a4e5097d45f2fe9cb98be489bf07",
      "old_mode": 33188,
      "old_path": "gdb/elfread.c",
      "new_id": "21d52f933553ac6c9153b6bab466e20e81b153ee",
      "new_mode": 33188,
      "new_path": "gdb/elfread.c"
    },
    {
      "type": "modify",
      "old_id": "309777c55f284c3805c3863f6faec5f8d87d7f71",
      "old_mode": 33188,
      "old_path": "gdb/fbsd-tdep.c",
      "new_id": "8431caf8f5972d567e54ac633963160ce50a7094",
      "new_mode": 33188,
      "new_path": "gdb/fbsd-tdep.c"
    },
    {
      "type": "modify",
      "old_id": "adf518023bbe4617ed434bfacad4ca97106be571",
      "old_mode": 33188,
      "old_path": "gdb/linux-tdep.c",
      "new_id": "dccb45d73a8d9c4dea99b1ac4f6f15525eaf06e9",
      "new_mode": 33188,
      "new_path": "gdb/linux-tdep.c"
    },
    {
      "type": "modify",
      "old_id": "bb907f2c8f3547b8a4402e0941bea49f77a1ed23",
      "old_mode": 33188,
      "old_path": "gdb/linux-tdep.h",
      "new_id": "95cc29c828c2f77a458be54b9446573bc46218a6",
      "new_mode": 33188,
      "new_path": "gdb/linux-tdep.h"
    },
    {
      "type": "modify",
      "old_id": "dfa81e19a79f3906747b0b10d3f13dfc14c15f08",
      "old_mode": 33188,
      "old_path": "gdb/ppc-linux-nat.c",
      "new_id": "795bb298955f92b3660350980d7559e465b17426",
      "new_mode": 33188,
      "new_path": "gdb/ppc-linux-nat.c"
    },
    {
      "type": "modify",
      "old_id": "8fcfb42a254d236c040a590fef0d541731f52b45",
      "old_mode": 33188,
      "old_path": "gdb/ppc-linux-tdep.c",
      "new_id": "12f418fb5ac6375d9fbb1d4a5fffb9225f2c15b4",
      "new_mode": 33188,
      "new_path": "gdb/ppc-linux-tdep.c"
    },
    {
      "type": "modify",
      "old_id": "4f2233a9bae21cb8354de3456f7fc99567535ec9",
      "old_mode": 33188,
      "old_path": "gdb/rs6000-tdep.c",
      "new_id": "8b6d666bbe76b72f74a715578821c0d3efc24722",
      "new_mode": 33188,
      "new_path": "gdb/rs6000-tdep.c"
    },
    {
      "type": "modify",
      "old_id": "2b21e08223621f4988293fb042a4878582ae0760",
      "old_mode": 33188,
      "old_path": "gdb/s390-linux-nat.c",
      "new_id": "96833e804e9af5b4f867011a519dce925a9a86a1",
      "new_mode": 33188,
      "new_path": "gdb/s390-linux-nat.c"
    },
    {
      "type": "modify",
      "old_id": "1ead5403ff1a42dcec380a57828c34ba778ad49f",
      "old_mode": 33188,
      "old_path": "gdb/s390-linux-tdep.c",
      "new_id": "ef2ed8510a64861ff66db9645ab236103abb58d7",
      "new_mode": 33188,
      "new_path": "gdb/s390-linux-tdep.c"
    },
    {
      "type": "modify",
      "old_id": "ce33f38ec60b07825e4730e41175d04875da4556",
      "old_mode": 33188,
      "old_path": "gdb/solib-svr4.c",
      "new_id": "27267e0bde9c9f26f19782ab29336aae8263575b",
      "new_mode": 33188,
      "new_path": "gdb/solib-svr4.c"
    },
    {
      "type": "modify",
      "old_id": "bf4961a1b7ca298007a33a95fd365ef527e3c16c",
      "old_mode": 33188,
      "old_path": "gdb/sparc64-tdep.c",
      "new_id": "25e8ce377c11630e99fb8574c10eb2a4fd524fd8",
      "new_mode": 33188,
      "new_path": "gdb/sparc64-tdep.c"
    }
  ]
}
