This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly][libObject] Avoid re-use of Section object during parsing
ClosedPublic

Authored by sbc100 on Aug 31 2021, 4:13 AM.

Details

Summary

The re-use of this struct across iterations of the loop was causing
fields (specifically Name) to be incorrectly shared between multiple
sections.

Diff Detail

Event Timeline

sbc100 created this revision.Aug 31 2021, 4:13 AM
sbc100 requested review of this revision.Aug 31 2021, 4:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 31 2021, 4:13 AM

The test has changed quite a bit more than I'd expect. What's the reasoning behind that?

The test has changed quite a bit more than I'd expect. What's the reasoning behind that?

This change means that the this test acts a regression test for the bug. In order for the bug to show itself we have to put a custom section before a builtin section. This is because only custom sections have names and the bug is that the Name field from a custom section was getting applied to all builtin sections that follow it.

If you prefer I can leave this test as is but write a specific regression test in llvm/test/Object instead?

I guess another option on the test would be to just keep the same sections that are in this test now, but still add foo at the front. The existing tests can stay the same and we could add one that covers this case (or we could switch one of the existing tests to use foo instead of producers if that would cover both cases, so we don't need to increase the total number of tests).

llvm/test/tools/llvm-objcopy/wasm/basic-only-section.test
17–19

The intent here was to test that the known sections are gone (because the custom section was after the known sections). But now it doesn't test that, because the custom section is after the known sections.

sbc100 added inline comments.Aug 31 2021, 12:27 PM
llvm/test/tools/llvm-objcopy/wasm/basic-only-section.test
17–19

Oh in that case I can just CHECK-NEXT: ... to ensure this is the one and only section. That seems strictly more clear.

sbc100 marked an inline comment as done.Aug 31 2021, 12:28 PM
sbc100 updated this revision to Diff 369764.Aug 31 2021, 12:37 PM

Better test

jhenderson accepted this revision.Sep 1 2021, 12:44 AM

If you prefer I can leave this test as is but write a specific regression test in llvm/test/Object instead?

I personally would generally prefer a gtest unit test for library-level changes, as I find the actual testing can be more specific, and less prone to rotting so that the test no longer covers the thing it's supposed to cover. However, I'm not a wasm developer, so I'm not going to ask for it, as there are probably norms there that are better followed.

Anyway, LGTM, but please wait for @dschuff.

This revision is now accepted and ready to land.Sep 1 2021, 12:44 AM
dschuff accepted this revision.Sep 1 2021, 8:53 AM

AFAIK we don't have gtest tests for wasm's lib/Object code so I don't want to block this little fix on creating a whole new set of tests. This change to the existing tests looks ok to me.
But I am interested in gtest unit tests more, because I like that style of testing generally. e.g. gor the file-based tests we have YAML as a convenient way to create test input, but it seems like for gtest tests you'd have to duplicate or write some custom test-harness code to generate inputs. Do you know if there are existing unit tests for lib/Object code, or know of any best practices for testing that library?

AFAIK we don't have gtest tests for wasm's lib/Object code so I don't want to block this little fix on creating a whole new set of tests. This change to the existing tests looks ok to me.
But I am interested in gtest unit tests more, because I like that style of testing generally. e.g. gor the file-based tests we have YAML as a convenient way to create test input, but it seems like for gtest tests you'd have to duplicate or write some custom test-harness code to generate inputs. Do you know if there are existing unit tests for lib/Object code, or know of any best practices for testing that library?

llvm/unittests/Object contains unittests for the Object library. Mostly, they're testing the easier to test API, but there are some, such as ELFObjectFileTest.cpp and MinidumpTest.cpp, which do similar things to what you might need to do to get wasm libObject tests sorted.

sbc100 added a comment.Sep 2 2021, 1:58 AM

AFAIK we don't have gtest tests for wasm's lib/Object code so I don't want to block this little fix on creating a whole new set of tests. This change to the existing tests looks ok to me.
But I am interested in gtest unit tests more, because I like that style of testing generally. e.g. gor the file-based tests we have YAML as a convenient way to create test input, but it seems like for gtest tests you'd have to duplicate or write some custom test-harness code to generate inputs. Do you know if there are existing unit tests for lib/Object code, or know of any best practices for testing that library?

llvm/unittests/Object contains unittests for the Object library. Mostly, they're testing the easier to test API, but there are some, such as ELFObjectFileTest.cpp and MinidumpTest.cpp, which do similar things to what you might need to do to get wasm libObject tests sorted.

Interesting, it looks like they also use yaml2obj to generate test input in the unit tests:
https://github.com/llvm/llvm-project/blob/dfb7518df18ad420984f965ef5ccfb4e126d839b/llvm/unittests/Object/ELFObjectFileTest.cpp#L318-L331

For now, I think I'm OK with leaving the testing here in the objcopy tests since that bug in question only effects objcopy (no other tools seem to care about the name of non-custom sections).