{"id":2858,"url":"https://patchwork.libcamera.org/api/1.1/patches/2858/?format=json","web_url":"https://patchwork.libcamera.org/patch/2858/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200218141839.5574-1-kieran.bingham@ideasonboard.com>","date":"2020-02-18T14:18:39","name":"[libcamera-devel,v2] libcamera: media_device: prevent sign extension on casts","commit_ref":"90240a79506a3c400f3af4cb0b08746ae87c79e2","pull_url":null,"state":"accepted","archived":false,"hash":"bc5628b7a1496bd7dbefdb1627c17101e8e191cd","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/1.1/people/4/?format=json","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2858/mbox/","series":[{"id":687,"url":"https://patchwork.libcamera.org/api/1.1/series/687/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=687","date":"2020-02-18T14:18:39","name":"[libcamera-devel,v2] libcamera: media_device: prevent sign extension on casts","version":2,"mbox":"https://patchwork.libcamera.org/series/687/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2858/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2858/checks/","tags":{},"headers":{"Return-Path":"<kieran.bingham@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E34FD60837\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 18 Feb 2020 15:18:42 +0100 (CET)","from localhost.localdomain\n\t(cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 4F55F2F9;\n\tTue, 18 Feb 2020 15:18:42 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1582035522;\n\tbh=bvGf19VUVpTEvuBqmphl5eQG+Xaks9QZTL6fF2OvU6k=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=ZJwjENIWA/ydUZUxfNMp+dPiCvRZGLrjYM1sQCWZ7oq4fylsRyXGdMR4ijwQbXAHD\n\tawP3xJgmjhbRvjt318JrDDMhJBy/MLUefykVPwYdFEgnRzIrZOgS2g+3RWDF2GvSbk\n\tzvu6b3+ygLkJGB29VKvpgBx981sRVBxwy+4/V6bQ=","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"libcamera devel <libcamera-devel@lists.libcamera.org>","Date":"Tue, 18 Feb 2020 14:18:39 +0000","Message-Id":"<20200218141839.5574-1-kieran.bingham@ideasonboard.com>","X-Mailer":"git-send-email 2.20.1","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2] libcamera: media_device: prevent sign\n\textension on casts","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Tue, 18 Feb 2020 14:18:43 -0000"},"content":"The conversion of pointers to integers is implementation defined and\ndiffers between g++ and clang++ when utilising a uint64_t type.\n\n    #include <iostream>\n\n    int main(int argc, char **argv)\n    {\n        void *ptr = reinterpret_cast<void *>(0xf1234567);\n        uint64_t u64 = reinterpret_cast<uint64_t>(ptr);\n\tuint64_t uint64 = reinterpret_cast<uintptr_t>(ptr);\n\n        std::cout << \"ptr \" << ptr\n\t\t  << \" u64 0x\" << std::hex << u64\n\t\t  << \" uint64 0x\" << std::hex << uint64\n\t\t  << std::endl;\n\n\treturn 0;\n    }\n\nWhen compiled with g++ produces the following unexpected output:\n\n  ptr 0xf1234567 u64 0xfffffffff1234567 uint64 0xf1234567\n\nThe standards states:\n\n\"A pointer can be explicitly converted to any integral type large enough\nto hold all values of its type. The mapping function is\nimplementation-defined. [Note: It is intended to be unsurprising to\nthose who know the addressing structure of the underlying machine. — end\nnote]\"\n\nAnd as such the g++ implementation appears to be little more surprising\nthan expected in this situation.\n\nThe MediaDevice passes pointers to the kernel via the struct\nmedia_v2_topology in which pointers are cast using a uint64 type (__u64),\nwhich is affected by the sign extension described above when BIT(32) is\nset and causes an invalid address to be given to the kernel.\n\nEnsure that we cast using uintptr_t which is not affected by the sign\nextension issue.\n\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\nv2:\n - Expand commit message to explain the underlying issue.\n - include stdint.h\n - use uintptr_t instead of std::uintptr_t\n\n src/libcamera/media_device.cpp | 9 +++++----\n 1 file changed, 5 insertions(+), 4 deletions(-)","diff":"diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp\nindex fad475b9ac76..0d6b5efd9e7a 100644\n--- a/src/libcamera/media_device.cpp\n+++ b/src/libcamera/media_device.cpp\n@@ -9,6 +9,7 @@\n \n #include <errno.h>\n #include <fcntl.h>\n+#include <stdint.h>\n #include <string>\n #include <string.h>\n #include <sys/ioctl.h>\n@@ -236,10 +237,10 @@ int MediaDevice::populate()\n \t */\n \twhile (true) {\n \t\ttopology.topology_version = 0;\n-\t\ttopology.ptr_entities = reinterpret_cast<__u64>(ents);\n-\t\ttopology.ptr_interfaces = reinterpret_cast<__u64>(interfaces);\n-\t\ttopology.ptr_links = reinterpret_cast<__u64>(links);\n-\t\ttopology.ptr_pads = reinterpret_cast<__u64>(pads);\n+\t\ttopology.ptr_entities = reinterpret_cast<uintptr_t>(ents);\n+\t\ttopology.ptr_interfaces = reinterpret_cast<uintptr_t>(interfaces);\n+\t\ttopology.ptr_links = reinterpret_cast<uintptr_t>(links);\n+\t\ttopology.ptr_pads = reinterpret_cast<uintptr_t>(pads);\n \n \t\tret = ioctl(fd_, MEDIA_IOC_G_TOPOLOGY, &topology);\n \t\tif (ret < 0) {\n","prefixes":["libcamera-devel","v2"]}