]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[fdxhook] boost shared_ptr ctor must zero fill
authorFrancis Dupont <fdupont@isc.org>
Mon, 20 Jun 2016 11:22:01 +0000 (13:22 +0200)
committerFrancis Dupont <fdupont@isc.org>
Mon, 20 Jun 2016 11:22:01 +0000 (13:22 +0200)
src/hooks/external/NOTES
src/hooks/external/inspect.cc [new file with mode: 0644]
src/hooks/external/ocaml/NOTES

index cc8bf3b85d859cd1231505758171c39abe1c4609..9020d1705b97cce5a6eb23622f9acf4328c0304c 100644 (file)
@@ -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 (file)
index 0000000..dd335bf
--- /dev/null
@@ -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 <iostream>
+#include <string>
+
+#include <boost/shared_ptr.hpp>
+
+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<Four> 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<Four> 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);
+}
+
index ea47cc9d81431ed31f17d31c7d927b114aecfe95..cd595caa900be7aff7c6ea8512e7ba3c7761b2d5 100644 (file)
@@ -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!