Rusty Russell [Mon, 17 Dec 2018 00:16:32 +0000 (10:46 +1030)]
tal: handle take() pointers more carefully.
take() applies to the literal pointer value, so tal_free() means it may
randomly apply to a future allocation. Similarly, tal_resize() should
preserve it.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Dmitry Petukhov [Sun, 4 Nov 2018 14:58:04 +0000 (19:58 +0500)]
small fix for ccan/take/take.c
Hello.
I've decided to take a look at c-lighting code, because we might
consider using it in the future.
I found a small problem in ccan/take/take.c that only concerns code
that runs with CCAN_TAKE_DEBUG set (DEVELOPER=1 for c-lighting).
It is a small issue, but I decided to notify you as the author of the
code, anyway.
the issue is:
in take_() function, potential failure of realloc for labelarr is not
handled.
I attached a diff with a fix.
I thought that making a pull request for c-lighting would not be right,
as ccan is a separate project, but I did not find a way to report this
at http://git.ozlabs.org/, where ccan repo resides.
Rusty Russell [Fri, 2 Nov 2018 01:27:57 +0000 (11:57 +1030)]
opt: add new parse_early_args_incomplete.
If we have plugins, and those can register args, we have a problem finding
the plugin dir! So, do a best-effort incomplete parse.
Note that this can screw up in theory if we have "--unknown --foo" since we
don't know if unknown takes an argument (in which case, ignore --foo) or
not.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell [Thu, 23 Aug 2018 02:41:13 +0000 (12:11 +0930)]
tal: allow notifiers on NULL.
A destructor on NULL doesn't make sense, but notifiers (eg. new children)
do. We fix up a mistake in run-notifier (comparing ctx with itself) and
loose typing in tal.c's tal_add_notifier_ too.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell [Fri, 27 Jul 2018 04:29:18 +0000 (13:59 +0930)]
ccan/tal: always include a length field.
The current semantics of tal_count() / tal_bytelen() are to return 0
for anything not allocated using tal_arr*. This is because we tried
to save a native-length word in the header, but produces an awkward
API.
(To make it worse, defining CCAN_TAL_DEBUG turns length to always on,
and we enable that for c-lightning developer mode, which hides bugs!).
However, for c-lightning, just over half of allocations want a length:
these use 3 words each, so we're actually worse off overall.
The answer is to always have a length field in the header. This also
simplfies the tal code.
samba-allocs stats before:
Tal time: 1237102-1305755(1.251e+06+/-2.1e+04)ns
Tal_free time: 1346871-1514514(1.37844e+06+/-5.2e+04)ns
After:
Tal time: 1115180-1180633(1.1351e+06+/-2.1e+04)ns
Tal_free time: 1334381-1465933(1.39148e+06+/-4.7e+04)ns
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell [Fri, 27 Jul 2018 04:23:19 +0000 (13:53 +0930)]
tal/str and tal/stack: use _label interfaces.
In particular, tal/str now passes through the label from the caller,
so (in case of CCAN_TAL_DEBUG) you can actually see the file and line
where the caller was, not just inside ccan/str.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell [Fri, 27 Jul 2018 04:22:20 +0000 (13:52 +0930)]
tal: add _label interfaces.
There are a number of other utilities which use the tal_alloc_ and
tal_dup_arr_ internal interfaces directly, because they want to set
the label themselves. We're about to break them all by changing those
internal interfaces, so give them a mid-level interface to use.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell [Wed, 4 Jul 2018 04:07:28 +0000 (13:37 +0930)]
ccan/structeq: make it safe when there's padding.
ccan/cppmagic FTW!
The only issue is that we can't tell if there's padding or they've missed
a member, so we add a padding bytes count, so they'll get an error if it
(for example) the structure adds a new member later.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell [Thu, 10 May 2018 02:25:59 +0000 (11:55 +0930)]
endian: fix compilation with musl libc.
It defines __BYTE_ORDER to __BYTE_ORDER__; gcc complains when we define
it to something else. Let it be already defined, but check that the
value is what we expect.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
gcc6 introduceed a new warning switched with -Wmisleading-identation. This caused to generate a compilation warning for if statement misleadingly indented, which caused HAVE_ASPRINTF to be defined as 0. Adding newline after an if statement fixes the problem.
Rusty Russell [Mon, 26 Mar 2018 10:36:34 +0000 (21:06 +1030)]
intmap: reimplement so that intmap_after works.
A critbit tree is a binary tree which keeps branches for each bit
which differs in the leaves. It's a simple data structure, but not
entirely simple to implement the primitives people expect, as this bus
shows.
The bug: I added an open iterator, and intmap_after_ for a random
value would sometimes return the wrong node.
Cause: we don't know what the prefix is as we iterate, so by only
testing the critbits in the tree, we can end up in the wrong place.
This is only a problem if the value isn't in the (sub)tree, but this
can easily happen even with contiguous trees should deletion occur.
You can see an example in the next patch, which adds a test.
After finding a bug in my intmap_after() routine, I went searching for
other implementations to see how they handled it. Most didn't provide
an open-ended iterator like this, relying on callback iterators which
don't allow deletion. Gah!
The exception was https://github.com/blynn/blt/blob/master/blt.c#L179
which implements blt_ceil() which does this (if you add one to the
key, at least). However, it does it by effectively finding a node,
using that to derive the prefix, then walking down the tree again.
That's pretty suboptimal.
There are basically two choices if you want an efficient after()
operation: to reimplement this approach with some optimizations
(ie. keep branches as we descend, and when we get to the bottom and
know the prefix, we know which branch to go down), or keep the bits
which got to each node.
The latter is more optimal, but less generally useful: for bit
strings, for example, we could keep the bits in common on each node,
rather than storing the entire string at the bottom. But in practice
you'd be doing allocations to re-create the index if the caller wanted
it.
However, in this implementation our keys are 64 bits only, and we
already use a u8 for the bit number: using a 64-bit value there
consumes no more space (thanks to alignment). We can store the
critbit by using the prefix capped by a bit: 0b10000...0000 means
no prefix and highest bit is the critbit, and 0bxxxxx1000...000
means the prefix is xxxxxx and the critbit is the 6th highest bit.
The penalty is that iteration 70% slower. It's still pretty fast
though.
Before:
$ for i in `seq 5`; do ./speed 10000000; done | stats 10000000,random generation (nsec),3-4(3.2+/-0.4) 10000000,critbit insert (nsec),1530-1751(1633.2+/-80) 10000000,critbit successful lookup (nsec),1723-1993(1806.8+/-97) 10000000,critbit failed lookup (nsec),1763-2104(1933.6+/-1.3e+02) 10000000,critbit iteration (nsec),208-266(242.2+/-19) 10000000,critbit memory (bytes),48 10000000,critbit delete (nsec),1747-1861(1803.8+/-42) 10000000,critbit consecutive iteration (nsec),182-228(210+/-18)
Yubin Ruan [Mon, 12 Mar 2018 03:24:14 +0000 (11:24 +0800)]
fix misspelling in the example of container_of
From 47c92fe951545e780ca31c598bbcbe5347059b27 Mon Sep 17 00:00:00 2001
From: Yubin Ruan <ablacktshirt@gmail.com>
Date: Mon, 12 Mar 2018 11:22:35 +0800
Subject: [PATCH] fix misspelling in the example of container_of
Signed-off-by: Yubin Ruan <ablacktshirt@gmail.com> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Rusty Russell [Wed, 25 Oct 2017 05:39:47 +0000 (16:09 +1030)]
io: query whether io_plan in/out have started.
For lightning, we want to hand the socket off to another daemon, but we need
to be on a packet boundary. This lets us check if we've part-read or
part-written.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
An application built using glibc would expect __BYTE_ORDER to tell if
it should be compiled for BIG_ENDIAN or LITTLE_ENDIAN, whereas ccan uses
HAVE_LITTLE_ENDIAN and HAVE_BIG_ENDIAN for the same purpose.
Hence setting __BYTE_ORDER based on what CCAN provides will no longer
break the applications which check endianness the glibc way.
Signed-off-by: Akshay Adiga <akshay.adiga@linux.vnet.ibm.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Damien Grassart [Tue, 29 Aug 2017 10:08:42 +0000 (12:08 +0200)]
darray: Fix bug in the darray_remove() macro
The memmove() call should be using the index argument to determine the
number of bytes to copy. To be consistent with the rest of the code,
we should also not evaluate the index parameter multiple
times. Calling this with rand() % arr.size would otherwise generally
segfault.
Finally, we want to avoid using "index" as an identifier so as to not
shadow index(3) in the C library.
Signed-off-by: Damien Grassart <damien@grassart.com> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Damien Grassart [Tue, 29 Aug 2017 10:08:40 +0000 (12:08 +0200)]
darray: Add darray_insert() to insert a value at a specified index
This module currently supports removing but not inserting at a
specified index, so this adds that along with some tests. Inserting a
value moves all existing data beyond index over one element.
Signed-off-by: Damien Grassart <damien@grassart.com> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
David Gibson [Sun, 23 Jul 2017 05:21:36 +0000 (15:21 +1000)]
objset: Use TCON_WRAP instead of TCON
TCON() uses flexible-array members which aren't allowed in the middle
of structures, except as a gcc extension. TCON_WRAP() avoids this and so
is more portable.
This doesn't change the objset interface, only its internals.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
David Gibson [Sun, 23 Jul 2017 05:11:33 +0000 (15:11 +1000)]
jmap: Use TCON_WRAP instead of TCON
TCON() uses flexible-array members which aren't allowed in the middle
of structures, except as a gcc extension. TCON_WRAP() avoids this and so
is more portable.
This doesn't change the jmap interface, only its internals.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
David Gibson [Sun, 23 Jul 2017 04:54:13 +0000 (14:54 +1000)]
jset: Use TCON_WRAP instead of TCON
TCON() uses flexible-array members which aren't allowed in the middle
of structures, except as a gcc extension. TCON_WRAP() avoids this and so
is more portable.
This doesn't change the jset interface, only its internals.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
David Gibson [Thu, 20 Jul 2017 14:06:01 +0000 (00:06 +1000)]
tlist: Use TCON_WRAP instead of TCON
TCON() uses flexible-array members which aren't allowed in the middle
of structures, except as a gcc extension. TCON_WRAP() avoids this and so
is more portable.
This doesn't change the tlist interface, only its internals.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Rusty Russell [Wed, 31 May 2017 03:05:45 +0000 (12:35 +0930)]
io: fix nasty io_wake corner case.
If we're duplex and one io_always callback makes the other io_always,
we screwed up and hit an assertion later when the conn was in the
always list but didn't actually want to be.
io_wake() uses io_always(), so this is how it happened. Writing a
test case for this was a bit fun, too.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
David Gibson [Sun, 2 Apr 2017 15:15:53 +0000 (01:15 +1000)]
net: Add check for failure of setsockopt()
make_listen_fd() didn't check for failure of setsockopt(). There's no
real reason not to, since we have an obvious way to report an error to the
caller.
Found with Coverity Scan.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
David Gibson [Sun, 2 Apr 2017 11:21:02 +0000 (21:21 +1000)]
crypto/ripemd160: Correct badly sized union member
struct ripemd160_ctx has a union for converting between u8[] and u32[]
data. Unfortunately the u32 array has a miscalculated size, half the size
of the u8 array. That means some accesses which are within the union can
technically overrun the u32 array.
Found by Coverity scan.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
David Gibson [Fri, 31 Mar 2017 13:48:22 +0000 (00:48 +1100)]
Fix missing va_end()s
This corrects several places in ccan where stdarg.h is used but there is a
missing va_end(). You can get away with this on many platforms, but not
all.
Caught by Coverity scan.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
David Gibson [Fri, 31 Mar 2017 12:51:22 +0000 (23:51 +1100)]
lbalance: Switch to tlist2
lbalance uses the tlist module. tlist causes compile warnings on clang if
you're not careful, because it can put 0 length arrays in the middle of
structures. tlist2 doesn't have the problem, and also has a slightly
cleaner interface.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
David Gibson [Fri, 31 Mar 2017 11:39:10 +0000 (22:39 +1100)]
tools/ccanlint: Add missing header file
tools/ccanlint/async.c uses kill(2), but doesn't include the signal.h
header it comes from. One some platforms we get away with this via
indirect includes, but not on all.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Rusty Russell [Tue, 14 Mar 2017 01:45:19 +0000 (12:15 +1030)]
io: add io_flush_sync().
This is needed for emergency handling in lightningd: we want to output
a (fatal) error packet on the socket, but we don't want to do so in the middle
of another packet.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
David Gibson [Fri, 20 Jan 2017 12:49:43 +0000 (23:49 +1100)]
coroutine: Stack allocation
At present, coroutine stacks must be allocated explicitly by the user,
then initialized with coroutine_stack_init(). This adds a new
coroutine_stack_alloc() function which allocates a stack, making life
easier for users. coroutine_stack_release() will automatically determine
if the given stack was set up with _init() or alloc() and act
accordingly.
The stacks are allocate with mmap() rather than a plain malloc(), and a
guard page is added, so an overflow of the stack should result in a
relatively debuggable SEGV instead of random data corruption.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>