From: Francis Dupont Date: Mon, 20 Jun 2016 11:22:01 +0000 (+0200) Subject: [fdxhook] boost shared_ptr ctor must zero fill X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=46412c2fa49c79ced672f800f8500ab9615a8b52;p=thirdparty%2Fkea.git [fdxhook] boost shared_ptr ctor must zero fill --- diff --git a/src/hooks/external/NOTES b/src/hooks/external/NOTES index cc8bf3b85d..9020d1705b 100644 --- a/src/hooks/external/NOTES +++ b/src/hooks/external/NOTES @@ -1,7 +1,6 @@ Kea hooks in external (i.e., not C++) languages. -Asked for customers: hooks written in python (see below), lua, -ocaml (see below?), ... +Asked for customers: hooks written in python (done), lua, ocaml (done), ... Kea hook summary: - dynamic shared objects loaded from the config on hooks-libraries @@ -40,7 +39,13 @@ Answer: C++ objects map to lua full userdata by default https://www.lua.org/pil/p1.html is obsolete but better) Question: C++ shared_ptr? -Answer: must use explicit finalization to reset shared_ptr's +Answer: must use explicit finalization to reset shared_ptr's, +should be encapsulated into a structure. Note the example codes make +the strong assumption shared pointers are built by the default +constructor as a memory block of zero (the documentation says +only the use count and the managed pointer are initialized to 0). +The test tool inspect.cc can be used to verify boost shared pointers +are implemented as expected. Question: DHCPv4 packet example? Answer: Pkt4Ptr with addOption, getCiaddr, getHtype, getHWAddr, @@ -52,5 +57,6 @@ Answer: OptionPtr with (class) factory, getData, getType, len, toText, ... Question: external language types? Answer: python3: int, float, str, bytes - ocaml: int, float, string (include binary) + ocaml: int, float, string, bytes (same representation than string) lua: (number) integer, (number) float, string (include binary) + diff --git a/src/hooks/external/inspect.cc b/src/hooks/external/inspect.cc new file mode 100644 index 0000000000..dd335bff88 --- /dev/null +++ b/src/hooks/external/inspect.cc @@ -0,0 +1,72 @@ +// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include +#include + +#include + +namespace isc { + +typedef int* IntPtr; +typedef int*** IntPtr3; + +struct Four { + IntPtr3 a, b, c, d; + + Four() : a(0), b(0), c(0), d() { } +}; + +typedef Four* FourPtr; + +typedef boost::shared_ptr FourSharedPtr; + +struct Encap { + FourSharedPtr object; +}; + +} // end of namespace isc + +using namespace std; +using namespace isc; + +int main() { + cout << "sizeof(IntPtr ::= int*) is " << sizeof(IntPtr) * 8 << " octets\n"; + cout << "sizeof(IntPtr3 ::= int***) is " << sizeof(IntPtr3) * 8 << " octets\n"; + if (sizeof(IntPtr) != sizeof(IntPtr3)) { + cout << "unusual size difference between IntPtr and IntPtr3?\n"; + } + if (sizeof(Four) != 4 * sizeof(IntPtr3)) { + cout << "sizeof(Four) is " << sizeof(Four) * 8 + << " octets and don't match expected " << sizeof(IntPtr3) * 32 + << " octet?\n"; + } + Four one; + Four zero; + memset(&zero, 0, sizeof(Four)); + if (memcmp(&zero, &one, sizeof(Four)) != 0) { + cout << "Four instance is not initialized to 0 as expected?\n"; + } + cout << "sizeof(FourPtr ::= Four*) is " << sizeof(FourPtr) * 8 << " octets\n"; + cout << "sizeof(FourSharedPtr ::= shared_ptr is " << sizeof(FourSharedPtr) * 8 << " octets\n"; + cout << "sizeof(Encap) is " << sizeof(Encap) * 8 << " octets\n"; + if (sizeof(FourSharedPtr) != sizeof(Encap)) { + cout << "unusual size difference between Encap and FourSharedPtr\n"; + } + size_t size = sizeof(Encap); + if (sizeof(Four) < sizeof(Encap)) { + cout << "shared pointers are very big?\n"; + size = sizeof(Four); + } + Encap encap; + if (memcmp(&encap, &zero, size) != 0) { + cerr << "shared pointers are not initialized to 0???\n"; + return (-1); + } + cout << "shared pointers are initialized to 0 as expected\n"; + return (0); +} + diff --git a/src/hooks/external/ocaml/NOTES b/src/hooks/external/ocaml/NOTES index ea47cc9d81..cd595caa90 100644 --- a/src/hooks/external/ocaml/NOTES +++ b/src/hooks/external/ocaml/NOTES @@ -38,3 +38,7 @@ despite of the .c extension, and another silents clang warning... There is no entry point in the ocaml runtime to unload it. Not a big problem but it means memory leaks can become hard to track. + +ocaml is statically typed so there is no type check in the runtime +even in the C++ code. So if you bug an external declaration you +likely bug the whole thing!