Skip to content
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

Small int prep work #1532

Merged
merged 5 commits into from
Nov 3, 2020
Merged

Small int prep work #1532

merged 5 commits into from
Nov 3, 2020

Conversation

fbs
Copy link
Member

@fbs fbs commented Sep 23, 2020

This is based on #1509, so that will have to land first.

This is some more prep work for #1091. Main things:

  • break some if nesting in the codegen into separate functions
  • treat integer dereference like a kptr(uint64 *) as discussed in Define pointer arithmetic #1507. E.g. $x = *123 => $x = * (uint64*)123;
  • fix a printing bug
  • Make the SizedType.size attribute private

@lgtm-com
Copy link

lgtm-com bot commented Sep 23, 2020

This pull request introduces 1 alert when merging 579a41d into 2ed1ed5 - view on LGTM.com

new alerts:

  • 1 for Missing return statement

@fbs fbs mentioned this pull request Sep 23, 2020
CHANGELOG.md Outdated
@@ -64,6 +64,8 @@ and this project adheres to
- [#1530](https://github.com/iovisor/bpftrace/pull/1530)
- Fix pointer arithmetic for positional parameters
- [#1514](https://github.com/iovisor/bpftrace/pull/1514)
- Printing of small integers with `printf`
- [#1531](https://github.com/iovisor/bpftrace/pull/1531)
Copy link
Member Author

Choose a reason for hiding this comment

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

off by 1

@fbs fbs force-pushed the 1091_integer_size_part1 branch 3 times, most recently from c7de39d to 883c72d Compare September 25, 2020 14:46
@danobi danobi added the do-not-merge Changes are not ready to be merged into master yet label Sep 26, 2020
@sumanthkorikkar
Copy link
Contributor

I am happy to test these changes, once it is rebased on master (after addrspace integration), which could solve few things for us. Thank you

@fbs fbs force-pushed the 1091_integer_size_part1 branch from 883c72d to 438e649 Compare October 4, 2020 13:40
@fbs fbs removed the do-not-merge Changes are not ready to be merged into master yet label Oct 4, 2020
@fbs
Copy link
Member Author

fbs commented Oct 4, 2020

ready for review now :)

@sumanthkorikkar
Copy link
Contributor

ok. I will check and test this. Thnx

@sumanthkorikkar
Copy link
Contributor

I do see the following additional failures.

[ FAILED ] array.array element access - assign to map
[ FAILED ] array.array element access - assign to var
[ FAILED ] intcast.Casting ints
[ FAILED ] variable.32-bit tracepoint arg

@sumanthkorikkar
Copy link
Contributor

sumanthkorikkar commented Oct 6, 2020

There is an endianness issue here while printing. I will provide the detailed debug output for analysis. Similarly the case for commit #1509 . Also these failures are related to printf.
[ FAILED ] intptrcast.Casting ints
[ FAILED ] intptrcast.Sum casted value
[ FAILED ] call.printf_argument_alignment

@fbs
Copy link
Member Author

fbs commented Oct 6, 2020

Thanks, didn't expect that to happen in this PR. I'll dig into it

@sumanthkorikkar
Copy link
Contributor

sumanthkorikkar commented Oct 11, 2020

This seems to be tricky:

  1. In semantic_analyser:
    // Promote to 64-bit if it's not an aggregate type
    if (!ty.IsAggregate() && !ty.IsTimestampTy())
    ty.size = 8;

  2. Then in codegen:
    // As these types are 64 bit, we adjust dst size appropriately in codegen
    expr_ = b_.CreateIntCast(b_.CreateLoad(dst),
    b_.getInt64Ty(),
    type.IsSigned());

  3. Now we consider the arg.type.GetIntBitWidth() in get_arg_values(). where size is not 64 bit (For 32 bit / 16 bit val). Hence it fails for Big endian. Previously we considered arg.type.size for this ( which is 64 bit).

  • This is specific to CreateProbeRead cases

@fbs
Copy link
Member Author

fbs commented Oct 11, 2020

I think #1533 will fix that. But might be better to close this and merge that in one go to avoid regressions.

Or i could add a setter for the size attribute here and have it update size_bits when needed. Eventually I hope to get rid of thee option to override size completely.

@sumanthkorikkar
Copy link
Contributor

ok. Fine with both. If #1533 is complex and takes long, then we could probably have a setter for the size attribute. Thanks.

@fbs fbs force-pushed the 1091_integer_size_part1 branch from 438e649 to f22545b Compare October 13, 2020 22:24
@fbs
Copy link
Member Author

fbs commented Oct 13, 2020

Updated to iniclude a setter/getter.

@sumanthkorikkar
Copy link
Contributor

ok. will check. Thanks.

@fbs fbs force-pushed the 1091_integer_size_part1 branch 2 times, most recently from fec3690 to 4ef17bf Compare October 14, 2020 10:17
@fbs
Copy link
Member Author

fbs commented Oct 14, 2020

@sumanthkorikkar think I got it fixed :)

// When dereferencing a 32-bit integer, only read in 32-bits, etc.
int size = type.IsPtrTy() ? type.GetPointeeTy()->GetSize()
: type.GetSize();
auto as = type.IsPtrTy() ? type.GetAS() : AddrSpace::kernel;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to set AddrSpace::kernel here?

Copy link
Contributor

Choose a reason for hiding this comment

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

If this is a valid case: *(int64)somintaddr. Then it would resolve to probe kernel

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes this should remain backwards compatibility with how integer dereference works currently. As the old probe_read helpers are deprecated we shouldn't rely on them anymore, so we eneed to set the AS here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think, this will resolve to probe kernel then, which is not correct. uprobe:./testprogs/intptrcast:fn { $a=*(reg("r15")+160);

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that differs from current behaviour where its also broken. But would be good to discuss in a separate issue

Copy link
Member Author

Choose a reason for hiding this comment

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

updated

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh, I didn't know bpf_probe_read was dropped in some architectures... If addrspace is none, then bpftrace emits a warning. So, how about keeping addrspace::none in this case and use bpf_probe_read_kernel() if bpf_probe_read() is not available. A user can use uptr() to change the behavior if needed. And maybe we can add a better auto-detect mechanism in another PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

As this PR has some massive refactoring changes and a printf fix I don't want to block this on this discussion/change right now so I've restored the original AS behaviour. For non overlapping arch we should probably get autodetection working properly but that is too much for this PR. We can continue the discussion in #1507 :)

Copy link
Contributor

Choose a reason for hiding this comment

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

ok

@fbs
Copy link
Member Author

fbs commented Oct 17, 2020

Did this fix all the test issues @sumanthkorikkar ?

@sumanthkorikkar
Copy link
Contributor

sumanthkorikkar commented Oct 17, 2020

Most of the issues reported works. Thanks. There are few other issues. Checking if they are related to this and will let you know soon.

@sumanthkorikkar
Copy link
Contributor

sumanthkorikkar commented Oct 18, 2020

  • [ FAILED ] usdt."usdt sized arguments" . There is an endianness issue here. But this is not related to this PR. I will send a fix for this today.

For these two failures, Didnt get an idea yet.

  • [ FAILED ] intptrcast.Casting ints
  • [ FAILED ] intptrcast.Sum casted value

For these, doing (int64*) instead of (int16*) works.

  • bpftrace -v -e 'uprobe:./testprogs/intptrcast:fn { $a=*(uint64*)(reg("r15")+160); printf("%d\n", $a); exit();}'-> Works
  • bpftrace -v -e 'uprobe:./testprogs/intptrcast:fn { $a=*(uint16*)(reg("r15")+160); printf("%d\n", $a); exit();}' -> Does not work

@sumanthkorikkar
Copy link
Contributor

#1566 Added for usdt_sized_args

@fbs
Copy link
Member Author

fbs commented Oct 18, 2020

For these two failures, Didnt get an idea yet.

* [  FAILED  ] intptrcast.Casting ints

* [  FAILED  ] intptrcast.Sum casted value

For these, doing (int64*) instead of (int16*) works.

* bpftrace -v -e 'uprobe:./testprogs/intptrcast:fn { $a=*(uint64*)(reg("r15")+160); printf("%d\n", $a); exit();}'-> Works

* bpftrace -v -e 'uprobe:./testprogs/intptrcast:fn { $a=*(uint16*)(reg("r15")+160); printf("%d\n", $a); exit();}' -> Does not work

Interesting thanks! Is this a regression compared to master? Can you attach/mail me the -dd output for both? Once we have the VM setup I can dig into this :)

Copy link
Member

@danobi danobi left a comment

Choose a reason for hiding this comment

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

LGTM (aside from addrspace), just some questions


size_t len = std::min(binop.left->type.size, binop.right->type.size);
expr_ = b_.CreateStrncmp(ctx_,
left_string,
left_as,
right_string,
right_as,
len,
len + 1,
Copy link
Member

Choose a reason for hiding this comment

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

Intentional change?

@@ -718,28 +718,59 @@ std::vector<std::unique_ptr<IPrintable>> BPFtrace::get_arg_values(const std::vec
switch (arg.type.type)
{
case Type::integer:
switch (arg.type.size)
if (arg.type.IsSigned())
Copy link
Member

Choose a reason for hiding this comment

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

The reason this fix works is b/c we're forcing a sign extension here, right? If so, can you add that information to the commit message?

Copy link
Member Author

Choose a reason for hiding this comment

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

@danobi I've removed the confusing cast hack thing and just have Printables handle the actual printing, should be easier to make sense of now.

@mmisono mmisono mentioned this pull request Oct 26, 2020
3 tasks
@fbs fbs force-pushed the 1091_integer_size_part1 branch 3 times, most recently from 36639bd to c972d32 Compare October 27, 2020 21:33
@fbs
Copy link
Member Author

fbs commented Oct 27, 2020

For these two failures, Didnt get an idea yet.

* [  FAILED  ] intptrcast.Casting ints

* [  FAILED  ] intptrcast.Sum casted value

These don't seem related to this PR, happens on my master build too.

@sumanthkorikkar
Copy link
Contributor

sumanthkorikkar commented Oct 28, 2020

For these two failures, Didnt get an idea yet.

* [  FAILED  ] intptrcast.Casting ints

* [  FAILED  ] intptrcast.Sum casted value

These don't seem related to this PR, happens on my master build too.

Ok. I will check these 2 failures in another PR. Thanks.

Printf conversion works on s390x.

@fbs fbs force-pushed the 1091_integer_size_part1 branch from c972d32 to 40218ba Compare October 29, 2020 19:08
@fbs
Copy link
Member Author

fbs commented Oct 30, 2020

Looks like the debian embedded build broke somehow, doesn't look related

@fbs fbs force-pushed the 1091_integer_size_part1 branch 2 times, most recently from e318a46 to 34a5eb7 Compare November 3, 2020 10:46
fbs added 5 commits November 3, 2020 12:47
Directly modifying the size field is tricky as other fields (like
size_bits) will have to be updated too. By making the field private and
hiding it behind a getter and setter that logic will only have to exist
in one place.

Ideally we disallow modifying size completely but that will require some
more changes.
When resizing an integer we have to insert a cast to ensure the sign
extension happens (for signed ints), just modifying the `size` isn't
enough.
The integer conversion causes signed integers to be printed wrong as
they're not correctly sign extended:

```
bpftrace -e 'BEGIN { printf("%d", (int16)-123); exit() ;}'
Attaching 1 probe...
65413
```
@fbs fbs force-pushed the 1091_integer_size_part1 branch from ce58c09 to 2e6a1e4 Compare November 3, 2020 11:48
@fbs fbs merged commit 91b0705 into bpftrace:master Nov 3, 2020
@fbs fbs deleted the 1091_integer_size_part1 branch August 1, 2021 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants