huntlabs / hunt

A refined core library for D programming language. The module has concurrency / collections / event / io / logging / text / serialization and more.
Apache License 2.0
95 stars 15 forks source link

There may be bugs in Radix tree. #56

Closed shove70 closed 5 years ago

shove70 commented 5 years ago

On MacOS, There may be bugs in Radix tree.

import std.stdio;

import core.stdc.stdlib;
import core.stdc.string;

import hunt.collection.Radix;

void main()
{
    byte[] data = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
    ubyte[] key = cast(ubyte[])"A key";

    void *value = malloc(data.length + 4);
    uint len = cast(uint)data.length;
    memcpy(value, &len, 4);
    memcpy(value + 4, data.ptr, data.length);

    rax *r;
    r = rax.New();
    writeln("Insert: ", r.Insert(key, value));

    void *read;
    if (!r.find(key, read))
    {
        writeln("Not found.");
        return;
    }

    memcpy(&len, read, 4);
    data = new byte[len];
    memcpy(data.ptr, read + 4, len);
    writeln(data);

    r.Remove(key);
    rax.Free(r);
}

Using this code to test, run it several times, and you will get different results:

1:
Running ./hunttest_radix
Insert: 1
Not found.
2:
Running ./hunttest_radix
Insert: 1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]
(Program does not exit, there is a dead cycle...)
3:
Running ./hunttest_radix
Insert: 1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Program exited with code -11
4:
Running ./hunttest_radix
Insert: 1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]
(Normal end as expected)

Radix has a problem that prevents hunt-framework from working properly.

shove70 commented 5 years ago

On centos7: line 1522: assert(r.numele == 0); Sometimes you will encounter assertion errors.

On macOS: In addition to the four cases mentioned above, there is a fifth case:

malloc: Incorrect checksum for freed object 0x7f92d74040b0: probably modified after being freed.
Corrupt value: 0x7f92d7403230
hunttest_radix(14390,0x1036375c0) malloc: *** set a breakpoint in malloc_error_break to debug
Program exited with code -6

Preliminary judgment: void* n = malloc (... size) The structure lacks initialization. After adding memset (n, 0, size); or *n.init; after malloc, the error situation is greatly reduced, but it still exists.

In a word, Radix has a lot of problems, which affect the normal use of hunt-framework.

zhangyuchun commented 5 years ago

fixed a bug at https://github.com/huntlabs/hunt/commit/2f1dc6d1cca8044edd903d14e24f146474f94dfe. i run your example and radix's unittest in debian with dmd-2.086.0 (x86_64) well. what's compier?

shove70 commented 5 years ago

If (s. length = 0) is a small problem, the key problem is not here.

DMD: 2.086.0 (x86_64) OS: MacOS 10.14.5

shove70 commented 5 years ago
  1. Run test2() in unittest in the radix.d file in centos7, and you can see the exception triggered by the assert(r.numele == 0); DMD: 2.086.0 (x86_64)

  2. Run my example and radix's unittest in MacOS with dmd-2.086.0 (x86_64). You can see these problems.

  3. Note: Please add two lines to unittest: test1(); test2();

shove70 commented 5 years ago

https://github.com/huntlabs/hunt/pull/60 Fixed.