Skip to content

ubsan/memsan fix. - #6937

Merged
akuzm merged 1 commit into
ClickHouse:masterfrom
amosbird:ubmemfix
Sep 17, 2019
Merged

akuzm merged 1 commit into
ClickHouse:masterfrom
amosbird:ubmemfix

Conversation

@amosbird

Copy link
Copy Markdown
Collaborator

I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/?lang=en

  • Bug Fix

When getting data from ColumnArray https://github.com/yandex/ClickHouse/blob/master/dbms/src/Columns/ColumnArray.cpp#L144
we can get an invalid pointer when current array is empty. (due to left padding of podarray, it should be some global readonly address)

Theoretically we'd like to get the same pointer value in https://github.com/yandex/ClickHouse/blob/master/dbms/src/Columns/ColumnArray.cpp#L151 . However, since we have a dedicated ZeroValueStorage in hash table. The pointer of zero lengthed arrays becomes nil after placing into the agg states hash table, which leads to UBSan and MemSan failure.

@amosbird

amosbird commented Sep 13, 2019 •

Copy link
Copy Markdown
Collaborator Author

This bug can be reproduced using the following query

SELECT groupUniqArray(v)
FROM
(
    SELECT
        1 AS id,
        [2] AS v
    UNION ALL
    SELECT
        1 AS id,
        [] AS v
)
GROUP BY id

clickhouse build command is

cmake -DSANITIZE=memory -DCMAKE_C_COMPILER="clang-8" -DCMAKE_CXX_COMPILER="clang++-8" -DCMAKE_CXX_FLAGS="-stdlib=libc++" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_CXX_FLAGS="-O1 -fno-omit-frame-pointer -fno-optimize-sibling-calls" -DCMAKE_C_FLAGS="-O1 -fno-omit-frame-pointer -fno-optimize-sibling-calls" -D ENABLE_HDFS=0 -D ENABLE_CAPNP=0 -D ENABLE_RDKAFKA=0 -D ENABLE_ICU=0 -D ENABLE_POCO_MONGODB=0 -D ENABLE_POCO_NETSSL=0 -D ENABLE_POCO_ODBC=0 -D ENABLE_ODBC=0 -D ENABLE_MYSQL=0 -D ENABLE_EMBEDDED_COMPILER=0 -D USE_INTERNAL_CAPNP_LIBRARY=0 -D USE_SIMDJSON=0 -DENABLE_READLINE=0 -DENABLE_ORC=0 -DENABLE_PARQUET=0 -DUSE_CAPNP=0 -DUSE_INTERNAL_ORC_LIBRARY=0 -DUSE_ORC=0 -DENABLE_SSL=0 ..

@amosbird

Copy link
Copy Markdown
Collaborator Author

Actuall we can also make all StringRef zero value storages pointing to a global empty string.

@akuzm

akuzm commented Sep 13, 2019 •

Copy link
Copy Markdown
Contributor

Great, thanks for investigating!

Your test case reproduces the error for me, we probably should add it to the tests. It has a shorter form using the new Values table function: select groupUniqArray(v) from values('id int, v Array(int)', (1, [2]), (1, [])) group by id.

So, is the core cause that we compare uninitialized end to uninitialized pos here?
https://github.com/yandex/ClickHouse/blob/master/dbms/src/Columns/ColumnArray.cpp#L166

I think it would be good to achieve that the pos is always well-defined, by pointing it to a global empty string as you suggest. Or maybe even making it nullptr in such cases? That is, require that if StringRef.size == 0, then StringRef.data == nullptr.

@amosbird

Copy link
Copy Markdown
Collaborator Author

I guess it's because ZeroValueStorage is not initialized. https://github.com/yandex/ClickHouse/blob/master/dbms/src/Common/HashTable/HashTable.h#L223
. I'm not sure how memsan reasons about this failure, but I remember the ubsan error is due to the invalid pos = 0xffffffffffffffff and pos + field_size overflows.

@akuzm

akuzm commented Sep 13, 2019

Copy link
Copy Markdown
Contributor

Looking at emplaceIfZero, I don't see where the constructor is called for zero value Cell at all. Is this correct?

Before, there was an explicit initialization of zero StringRef in GroupUniqArray:
https://github.com/yandex/ClickHouse/pull/6729/files#diff-9281c6b3757a84abfb5643adf2bb48d2L251

but I removed it in the key holder PR -- don't remember why.

Anyway, it looks like it was a hack, and we should just properly call the constructor for zero Cell, the same way we do for all other Cells in emplaceNonZeroImpl.

@amosbird

Copy link
Copy Markdown
Collaborator Author

yeah, initializing it properly also fixes the bug.

@akuzm

akuzm commented Sep 13, 2019

Copy link
Copy Markdown
Contributor

I'd prefer to call the Cell constructor in emplaceIfZero, because it would be only initialized on demand, and also would properly initialize complex cells like ClearableHashTableCell, which is not initialized to all zeros but has a version field.

@amosbird

Copy link
Copy Markdown
Collaborator Author

@akuzm you just caught me in the middle :) . That commit was supposed to run a CI test but I soon realize it's too slow so I did locally...

@amosbird

Copy link
Copy Markdown
Collaborator Author

I cannot reproduce the thread san build failure locally using

docker run -it --rm --net=host -v /ci/tsandebs:/package_folder  yandex/clickhouse-stateless-test

Is it some flaky test?

@akuzm

akuzm commented Sep 16, 2019

Copy link
Copy Markdown
Contributor

Is it some flaky test?

The logs are truncated, looks like some CI failure.

Comment thread dbms/src/Common/HashTable/HashTable.h Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should call the Cell constructor here, like we do for non-zero Cells. We already have Cells with non-trivial constructors, like ClearableHashTableCell.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amosbird

Copy link
Copy Markdown
Collaborator Author

Ok, how can I trigger a retest without force push new stuff?

@akuzm

akuzm commented Sep 17, 2019

Copy link
Copy Markdown
Contributor

The thread sanitizer CI failure looks unrelated. The performance test didn't finish, but it shouldn't be influenced because we didn't change much. Will merge as is.

@akuzm
akuzm merged commit 75dc7d3 into ClickHouse:master Sep 17, 2019
@KochetovNicolai KochetovNicolai added the pr-bugfix Pull request with bugfix, not backported by default label Sep 19, 2019
KochetovNicolai pushed a commit that referenced this pull request Sep 19, 2019
In hash tables, properly initialize zero key cell.

(cherry picked from commit 75dc7d3)
KochetovNicolai pushed a commit that referenced this pull request Sep 19, 2019
In hash tables, properly initialize zero key cell.

(cherry picked from commit 75dc7d3)
KochetovNicolai pushed a commit that referenced this pull request Sep 20, 2019
In hash tables, properly initialize zero key cell.

(cherry picked from commit 75dc7d3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants