-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix endianness handling in {zfs,zvol}_replay_clone_range() #18866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ryao
wants to merge
2
commits into
openzfs:master
Choose a base branch
from
ryao:replay_clone_range-endianness-fixes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /** | ||
| * @name ZIL replay flexible array left un-byteswapped | ||
| * @description Detects byteswap_uint*_array(p, sizeof(*p)) / sizeof(T) on a | ||
| * log-record type that ends in a multi-byte flexible array member | ||
| * (e.g. blkptr_t lr_bps[]). sizeof() only covers the fixed header, | ||
| * so the trailing FAM stays foreign-endian on opposite-endian ZIL | ||
| * replay. Opaque uint8_t[] FAMs (names, raw write data) are ignored. | ||
| * See TX_CLONE_RANGE replay (zfs_replay_clone_range / | ||
| * zvol_replay_clone_range). | ||
| * @kind problem | ||
| * @severity error | ||
| * @tags correctness | ||
| * endianness | ||
| * @id cpp/zilFamByteswap | ||
| */ | ||
|
|
||
| import cpp | ||
|
|
||
| /** | ||
| * A struct that ends with a C99 flexible array member or a GCC zero-length | ||
| * array used the same way. | ||
| */ | ||
| class FlexibleArrayStruct extends Struct { | ||
| Field fam; | ||
|
|
||
| FlexibleArrayStruct() { | ||
| fam = this.getAField() and | ||
| ( | ||
| ( | ||
| fam.getType() instanceof ArrayType and | ||
| not fam.getType().(ArrayType).hasArraySize() | ||
| ) | ||
| or | ||
| ( | ||
| fam.getType() instanceof ArrayType and | ||
| fam.getType().(ArrayType).hasArraySize() and | ||
| fam.getType().(ArrayType).getArraySize() = 0 | ||
| ) | ||
| ) and | ||
| not exists(Field f2 | | ||
| f2 = this.getAField() and f2.getByteOffset() > fam.getByteOffset() | ||
| ) | ||
| } | ||
|
|
||
| Field getFam() { result = fam } | ||
|
|
||
| Type getFamElementType() { | ||
| result = fam.getType().(ArrayType).getBaseType().getUnspecifiedType() | ||
| } | ||
|
|
||
| /** Prefer typedef name (lr_clone_range_t) over the tag name. */ | ||
| string getDisplayName() { | ||
| exists(TypedefType t | | ||
| t.getBaseType().getUnspecifiedType() = this and result = t.getName() | ||
| ) | ||
| or | ||
| ( | ||
| not exists(TypedefType t | t.getBaseType().getUnspecifiedType() = this) and | ||
| this.getName() != "" and | ||
| result = this.getName() | ||
| ) | ||
| or | ||
| ( | ||
| not exists(TypedefType t | t.getBaseType().getUnspecifiedType() = this) and | ||
| this.getName() = "" and | ||
| result = "<anonymous struct>" | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * FAM holds multi-byte structured data that a header-only sizeof() byteswap | ||
| * would leave wrong-endian. | ||
| */ | ||
| predicate hasStructuredFam() { | ||
| exists(Type et | et = this.getFamElementType() | et.getSize() > 1) | ||
| } | ||
| } | ||
|
|
||
| class ByteswapCall extends FunctionCall { | ||
| ByteswapCall() { | ||
| this.getTarget().getName().regexpMatch("byteswap_uint(64|32|16)_array") | ||
| } | ||
|
|
||
| Expr getBufferArg() { result = this.getArgument(0) } | ||
|
|
||
| Expr getSizeArg() { result = this.getArgument(1) } | ||
| } | ||
|
|
||
| Type strip(Type t) { result = t.getUnspecifiedType() } | ||
|
|
||
| predicate typeIsFlexibleStruct(Type t, FlexibleArrayStruct fas) { | ||
| strip(t) = fas | ||
| or | ||
| strip(t).(PointerType).getBaseType().getUnspecifiedType() = fas | ||
| or | ||
| exists(TypedefType ta | | ||
| strip(t) = ta and strip(ta.getBaseType()) = fas | ||
| ) | ||
| or | ||
| exists(TypedefType ta | | ||
| strip(t).(PointerType).getBaseType().getUnspecifiedType() = ta and | ||
| strip(ta.getBaseType()) = fas | ||
| ) | ||
| } | ||
|
|
||
| predicate sizeIsOnlyFixedHeader(Expr sizeArg, FlexibleArrayStruct fas) { | ||
| exists(SizeofTypeOperator s | | ||
| s = sizeArg and typeIsFlexibleStruct(s.getTypeOperand(), fas) | ||
| ) | ||
| or | ||
| exists(SizeofExprOperator s | | ||
| s = sizeArg and typeIsFlexibleStruct(s.getExprOperand().getType(), fas) | ||
| ) | ||
| or | ||
| sizeIsOnlyFixedHeader(sizeArg.(Conversion).getExpr(), fas) | ||
| or | ||
| sizeIsOnlyFixedHeader(sizeArg.(ParenthesisExpr).getExpr(), fas) | ||
| } | ||
|
|
||
| predicate bufferIsFlexibleStruct(Expr buf, FlexibleArrayStruct fas) { | ||
| typeIsFlexibleStruct(buf.getType(), fas) | ||
| or | ||
| typeIsFlexibleStruct(buf.(Cast).getType(), fas) | ||
| or | ||
| bufferIsFlexibleStruct(buf.(Conversion).getExpr(), fas) | ||
| or | ||
| bufferIsFlexibleStruct(buf.(ParenthesisExpr).getExpr(), fas) | ||
| } | ||
|
|
||
| /** | ||
| * The same function also byteswaps the FAM field (correct split header/FAM | ||
| * pattern). Suppress those. | ||
| */ | ||
| predicate famIsByteswappedSeparately(ByteswapCall headerSwap, FlexibleArrayStruct fas) { | ||
| exists(ByteswapCall famSwap, FieldAccess fa | | ||
| famSwap != headerSwap and | ||
| famSwap.getEnclosingFunction() = headerSwap.getEnclosingFunction() and | ||
| fa.getTarget() = fas.getFam() and | ||
| fa.getEnclosingElement*() = famSwap.getBufferArg() | ||
| ) | ||
| } | ||
|
|
||
| from ByteswapCall c, FlexibleArrayStruct fas, string tname | ||
| where | ||
| fas.hasStructuredFam() and | ||
| tname = min(string n | n = fas.getDisplayName() | n) and | ||
| sizeIsOnlyFixedHeader(c.getSizeArg(), fas) and | ||
| bufferIsFlexibleStruct(c.getBufferArg(), fas) and | ||
| not famIsByteswappedSeparately(c, fas) | ||
| select c, | ||
| "byteswap of sizeof(" + tname + | ||
| ") does not convert flexible array member '" + fas.getFam().getName() + | ||
| "' (element type " + fas.getFamElementType().getName() + ") in " + | ||
| c.getEnclosingFunction().getName() + "." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll need a refresher on what should byteswap what, but it seems here you are accessing
lrc_reclenboth before and after byteswap, which seems wrong.