{"action":"created","issue":{"url":"https://api.github.com/repos/BinaryAnalysisPlatform/bap/issues/231","labels_url":"https://api.github.com/repos/BinaryAnalysisPlatform/bap/issues/231/labels{/name}","comments_url":"https://api.github.com/repos/BinaryAnalysisPlatform/bap/issues/231/comments","events_url":"https://api.github.com/repos/BinaryAnalysisPlatform/bap/issues/231/events","html_url":"https://github.com/BinaryAnalysisPlatform/bap/issues/231","id":85551045,"number":231,"title":"Memmap.lookup returns something unexpected","user":{"login":"rvantonder","id":888624,"avatar_url":"https://avatars.githubusercontent.com/u/888624?v=3","gravatar_id":"","url":"https://api.github.com/users/rvantonder","html_url":"https://github.com/rvantonder","followers_url":"https://api.github.com/users/rvantonder/followers","following_url":"https://api.github.com/users/rvantonder/following{/other_user}","gists_url":"https://api.github.com/users/rvantonder/gists{/gist_id}","starred_url":"https://api.github.com/users/rvantonder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rvantonder/subscriptions","organizations_url":"https://api.github.com/users/rvantonder/orgs","repos_url":"https://api.github.com/users/rvantonder/repos","events_url":"https://api.github.com/users/rvantonder/events{/privacy}","received_events_url":"https://api.github.com/users/rvantonder/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/BinaryAnalysisPlatform/bap/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-05T14:49:56Z","updated_at":"2015-06-05T15:30:01Z","closed_at":null,"body":"When I perform `Memmap.lookup (Project.memory project) addr` on an `addr`, whose value is `0xFE24:32`, I get two pieces of memory back (see below). Both of these contain `0xFE24:32`, but really I only want to tag memory at `0xFE24`, not this whole thing. I also don't understand why I'm getting two memories.\r\n\r\nThe way I'm dealing with this now is to use one of the memories and then perform `Memory.view ~from:addr mem ~words:4` to 'trim' it, and then tag only the part I'm interested in. So my question reduces down to: what exactly does `Memmap.lookup` do? Why does it return two large ranges of memory in this case?\r\n\r\nBinary is coreutils_O1_cat.\r\n\r\n```\r\nAddr: 0xFE24:32\r\nLength Mem 1. 33488 -> size\r\nLength Mem 2. 3092 -> size\r\n```\r\n```\r\nMem 1:\r\n00008000  7F 45 4C 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|\r\n00008010  02 00 28 00 01 00 00 00 D0 91 00 00 34 00 00 00 |..(.........4...|\r\n00008020  30 84 02 00 02 00 00 05 34 00 20 00 08 00 28 00 |0.......4. ...(.|\r\n00008030  26 00 23 00 01 00 00 70 C4 82 00 00 C4 02 01 00 |&.#....p........|\r\n00008040  C4 02 01 00 08 00 00 00 08 00 00 00 04 00 00 00 |................|\r\n00008050  04 00 00 00 06 00 00 00 34 00 00 00 34 80 00 00 |........4...4...|\r\n00008060  34 80 00 00 00 01 00 00 00 01 00 00 05 00 00 00 |4...............|\r\n00008070  04 00 00 00 03 00 00 00 34 01 00 00 34 81 00 00 |........4...4...|\r\n00008080  34 81 00 00 13 00 00 00 13 00 00 00 04 00 00 00 |4...............|\r\n00008090  01 00 00 00 01 00 00 00 00 00 00 00 00 80 00 00 |................|\r\n000080A0  00 80 00 00 D0 82 00 00 D0 82 00 00 05 00 00 00 |................|\r\n000080B0  00 80 00 00 01 00 00 00 D0 82 00 00 D0 82 01 00 |................|\r\n000080C0  D0 82 01 00 80 02 00 00 F4 07 00 00 06 00 00 00 |................|\r\n000080D0  00 80 00 00 02 00 00 00 DC 82 00 00 DC 82 01 00 |................|\r\n000080E0  DC 82 01 00 E8 00 00 00 E8 00 00 00 06 00 00 00 |................|\r\n000080F0  04 00 00 00 04 00 00 00 48 01 00 00 48 81 00 00 |........H...H...|\r\n00008100  48 81 00 00 44 00 00 00 44 00 00 00 04 00 00 00 |H...D...D.......|\r\n00008110  04 00 00 00 51 E5 74 64 00 00 00 00 00 00 00 00 |....Q.td........|\r\n00008120  00 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 |................|\r\n00008130  04 00 00 00 2F 6C 69 62 2F 6C 64 2D 6C 69 6E 75 |..../lib/ld-linu|\r\n00008140  78 2E 73 6F 2E 33 00 00 04 00 00 00 10 00 00 00 |x.so.3..........|\r\n00008150  01 00 00 00 47 4E 55 00 00 00 00 00 02 00 00 00 |....GNU.........|\r\n00008160  06 00 00 00 10 00 00 00 04 00 00 00 14 00 00 00 |................|\r\n00008170  03 00 00 00 47 4E 55 00 07 3D 69 C9 88 48 AA 57 |....GNU..=i..H.W|\r\n00008180  46 D3 43 2E A3 4A E1 53 8A 0F BE 7D 43 00 00 00 |F.C..J.S...}C...|\r\n00008190  02 00 00 00 10 00 00 00 09 00 00 00 B2 C9 E2 80 |................|\r\n000081A0  84 60 19 9A 00 28 82 CC 02 10 00 00 8A 00 43 5D |.`...(........C]|\r\n...\r\n00010290  00 00 00 00 00 00 00 00 00 00 00 00 6D 62 75 69 |............mbui|\r\n000102A0  74 65 72 5F 6D 75 6C 74 69 5F 6E 65 78 74 00 00 |ter_multi_next..|\r\n000102B0  6D 62 75 69 74 65 72 5F 6D 75 6C 74 69 5F 6E 65 |mbuiter_multi_ne|\r\n000102C0  78 74 00 00 0C 8F FF 7F 01 00 00 00 00 00 00 00 |xt..............|\r\n```\r\n\r\n```\r\nMem 2:\r\n0000F6B0  01 00 02 00 F8 FC 00 00 00 00 00 00 00 00 00 00 |................|\r\n0000F6C0  62 00 00 00 08 FD 00 00 00 00 00 00 00 00 00 00 |b...............|\r\n0000F6D0  6E 00 00 00 10 FD 00 00 00 00 00 00 00 00 00 00 |n...............|\r\n0000F6E0  73 00 00 00 20 FD 00 00 00 00 00 00 00 00 00 00 |s... ...........|\r\n0000F6F0  76 00 00 00 34 FD 00 00 00 00 00 00 00 00 00 00 |v...4...........|\r\n0000F700  45 00 00 00 40 FD 00 00 00 00 00 00 00 00 00 00 |E...@...........|\r\n0000F710  54 00 00 00 4C FD 00 00 00 00 00 00 00 00 00 00 |T...L...........|\r\n0000F720  41 00 00 00 58 FD 00 00 00 00 00 00 00 00 00 00 |A...X...........|\r\n0000F730  FE FF FF FF 60 FD 00 00 00 00 00 00 00 00 00 00 |....`...........|\r\n0000F740  FD FF FF FF 00 00 00 00 00 00 00 00 00 00 00 00 |................|\r\n0000F750  00 00 00 00 54 72 79 20 27 25 73 20 2D 2D 68 65 |....Try '%s --he|\r\n0000F760  6C 70 27 20 66 6F 72 20 6D 6F 72 65 20 69 6E 66 |lp' for more inf|\r\n0000F770  6F 72 6D 61 74 69 6F 6E 2E 0A 00 00 55 73 61 67 |ormation....Usag|\r\n0000F780  65 3A 20 25 73 20 5B 4F 50 54 49 4F 4E 5D 2E 2E |e: %s [OPTION]..|\r\n0000F790  2E 20 5B 46 49 4C 45 5D 2E 2E 2E 0A 00 00 00 00 |. [FILE]........|\r\n0000F7A0  43 6F 6E 63 61 74 65 6E 61 74 65 20 46 49 4C 45 |Concatenate FILE|\r\n...\r\n00010270  25 35 30 73 20 25 35 30 73 00 00 00 00 1A 00 00 |%50s %50s.......|\r\n00010280  EF FF FF FF FE FF FF FF FE FF FF 7F 00 00 00 00 |................|\r\n00010290  00 00 00 00 00 00 00 00 00 00 00 00 6D 62 75 69 |............mbui|\r\n000102A0  74 65 72 5F 6D 75 6C 74 69 5F 6E 65 78 74 00 00 |ter_multi_next..|\r\n000102B0  6D 62 75 69 74 65 72 5F 6D 75 6C 74 69 5F 6E 65 |mbuiter_multi_ne|\r\n000102C0  78 74 00 00                                     |xt..            |\r\n```"},"comment":{"url":"https://api.github.com/repos/BinaryAnalysisPlatform/bap/issues/comments/109329276","html_url":"https://github.com/BinaryAnalysisPlatform/bap/issues/231#issuecomment-109329276","issue_url":"https://api.github.com/repos/BinaryAnalysisPlatform/bap/issues/231","id":109329276,"user":{"login":"ivg","id":2336698,"avatar_url":"https://avatars.githubusercontent.com/u/2336698?v=3","gravatar_id":"","url":"https://api.github.com/users/ivg","html_url":"https://github.com/ivg","followers_url":"https://api.github.com/users/ivg/followers","following_url":"https://api.github.com/users/ivg/following{/other_user}","gists_url":"https://api.github.com/users/ivg/gists{/gist_id}","starred_url":"https://api.github.com/users/ivg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ivg/subscriptions","organizations_url":"https://api.github.com/users/ivg/orgs","repos_url":"https://api.github.com/users/ivg/repos","events_url":"https://api.github.com/users/ivg/events{/privacy}","received_events_url":"https://api.github.com/users/ivg/received_events","type":"User","site_admin":false},"created_at":"2015-06-05T15:30:01Z","updated_at":"2015-06-05T15:30:01Z","body":"So, first of all, I would like to clarify terminology. According to a bap parlance, memory (or `mem` for short) is just a piece of data. You may think of it as an array, that is indexed by virtual addresses. But unlike real arrays, memory is just an opaque view. So other way of thinking is to treat `mem` as an interval (and actually this is how it is implemented). \r\n\r\nMemory map (or `memmap`) is an associative data structure (also known as interval trees or interval map or segment tree) that associates this intervals with  some data. Intervals can overlap, dominate each other or even be equal. The following figure will give some idea, on how it looks like.\r\n\r\n```\r\n+----------------------------------------------------------------+\r\n|+--------------------------------+    +-------------------+     |\r\n|| +------------------+           |    |                   |  S  |\r\n|| |                  |           |    |                   |  E  |\r\n|| |          +-------+---------+ |    |                   |  G  |\r\n|| |          |                 | |    |                   |  M  |\r\n|| | SYMBOL 1 |    SYMBOL 2     | |    |     SECTION 2     |  E  |\r\n|| |          |                 | |    |                   |  N  |\r\n|| |          +-------+---------+ |    |                   |  T  |\r\n|| |                  | SECTION 1 |    |                   |     |\r\n|| +------------------+           |    |                   |  1  |\r\n|+--------------------------------+    +-------------------+     |\r\n+----------------------------------------------------------------+\r\n0   1    2    3    4    5    6   7   8   9   A   B   C   D   E   F\r\n```\r\n\r\nIt is simplified example, of course. Here we have the big chunk of memory marked as `segment 1` (i.e., with this memory region we associate a semantic information that it is a segment). We also have some subset of the segment marked as belonging to two different sections, namely `section 1` and `section 2`. Inside `section 1` we also have some portions of data split between to symbols, and some data is shared between to symbols. \r\n\r\nWith a given example, address `4` belongs to 4 memory regions: it is a part of a `segment 1` and `section 1` and belongs to two symbols: `symbol 1` and `symbol 2`. On the other hand, address `8` \r\n Belongs only to the `segment 1` , `B` belongs to 2 regions: the segment and and `section 2`.\r\n\r\nIn your particular case, you have to \"memories\", because this address is mapped to the section `.rodata` and second segment.  \r\n\r\nYou can always see the layout of the memory, by printing it:\r\n\r\n```ocaml\r\nlet print_memory p =\r\n  Project.memory p |> Memmap.to_sequence |> Seq.iter ~f:(fun (mem,x) ->\r\n      printf \"%s(%a)@.%a@.\" (Value.tagname x) Value.pp x Memory.pp mem)\r\n```"}}