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

string assignment invalid BPF_LD_IMM insn #226

Closed
brendangregg opened this issue Sep 17, 2015 · 4 comments
Closed

string assignment invalid BPF_LD_IMM insn #226

brendangregg opened this issue Sep 17, 2015 · 4 comments
Labels

Comments

@brendangregg
Copy link
Member

When dealing with strings, I often want to switch to a default if a given char * is null.

Test program:

from __future__ import print_function
from bcc import BPF

# load BPF program
b = BPF(text = """
void kprobe__sys_sync(void *ctx) {
        char *out = NULL, *hello = "world";
        if (out == NULL) {
                out = hello;
        }
        bpf_trace_printk("sync() %s\\n", out);
};
""")

# header
print("%-18s %s" % ("TIME(s)", "CALL"))

# format output
while 1:
        (task, pid, cpu, flags, ts, msg) = b.trace_fields()
        print("%-18.9f %s" % (ts, msg))
# ./teststr 
bpf: Invalid argument
0: (b7) r1 = 0
1: (73) *(u8 *)(r10 -6) = r1
2: (b7) r1 = 2675
3: (6b) *(u16 *)(r10 -8) = r1
4: (18) r1 = 0x636e7973
6: (7b) *(u64 *)(r10 -16) = r1
7: (bf) r1 = r10
8: (07) r1 += -16
9: (b7) r2 = 11
10: (00) r0 = 0x0
invalid BPF_LD_IMM insn

Traceback (most recent call last):
  File "./teststr", line 26, in <module>
    """)
  File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 353, in __init__
    self._trace_autoload()
  File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 573, in _trace_autoload
    fn = self.load_func(func_name, BPF.KPROBE)
  File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 388, in load_func
    raise Exception("Failed to load BPF program %s" % func_name)
Exception: Failed to load BPF program kprobe__sys_sync
@drzaeus77
Copy link
Collaborator

Global strings won't work. Try changing it to hello[] = "world"

@brendangregg
Copy link
Member Author

Yes, that worked, thanks. We'll need to include an example of that somewhere at least.

@drzaeus77 drzaeus77 added bug and removed bug labels Sep 18, 2015
@brendangregg
Copy link
Member Author

FWIW, the current error this produces is:

# python ./test.py 
bpf: Failed to load program: Invalid argument
unknown opcode 00

Traceback (most recent call last):
  File "./test.py", line 13, in <module>
    """)
  File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 339, in __init__
    self._trace_autoload()
  File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 1067, in _trace_autoload
    fn = self.load_func(func_name, BPF.KPROBE)
  File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 379, in load_func
    (func_name, errstr))
Exception: Failed to load BPF program kprobe__do_nanosleep: Invalid argument

For this issue I'd either have a better error message (somehow), or document "unknown opcode 00" in the BPF Errors section of the reference guide explaining what it's about.

@yonghong-song
Copy link
Collaborator

Agreed. We could add a little more explanation in libbpf.c bpf_print_hints() if we found unknown opcode 00. The reason is typically due to ignored relocation which has initial 0's in the original place.

yonghong-song added a commit that referenced this issue Dec 31, 2018
fix issue #226

The unknown opcode typically happens if the bpf
program has an external reference which does not
get resolved. Note bcc does not even preform
relocations for maps as map_id is directly
used in bpf problem through bpf_pseudo_fd()
intrinsic.

Instead of the error:
  bpf: Failed to load program: Invalid argument
  unknown opcode 00

A little explanation is added like the below:
  HINT: The 'unknown opcode' can happen if you referencea global
  or static variable, or data in read only section.
  For example,'char *p = "hello"' will result in p referencing a
  read only section,and 'char p[] = "hello"' will have "hello"
  stored on the stack.

Signed-off-by: Yonghong Song <[email protected]>
yonghong-song added a commit that referenced this issue Jan 1, 2019
fix issue #226

The unknown opcode typically happens if the bpf
program has an external reference which does not
get resolved. Note bcc does not even preform
relocations for maps as map_id is directly
used in bpf problem through bpf_pseudo_fd()
intrinsic.

Instead of the error:
  bpf: Failed to load program: Invalid argument
  unknown opcode 00

A little explanation is added like the below:
  HINT: The 'unknown opcode' can happen if you referencea global
  or static variable, or data in read only section.
  For example,'char *p = "hello"' will result in p referencing a
  read only section,and 'char p[] = "hello"' will have "hello"
  stored on the stack.

Signed-off-by: Yonghong Song <[email protected]>
palexster pushed a commit to palexster/bcc that referenced this issue Jul 7, 2019
fix issue iovisor#226

The unknown opcode typically happens if the bpf
program has an external reference which does not
get resolved. Note bcc does not even preform
relocations for maps as map_id is directly
used in bpf problem through bpf_pseudo_fd()
intrinsic.

Instead of the error:
  bpf: Failed to load program: Invalid argument
  unknown opcode 00

A little explanation is added like the below:
  HINT: The 'unknown opcode' can happen if you referencea global
  or static variable, or data in read only section.
  For example,'char *p = "hello"' will result in p referencing a
  read only section,and 'char p[] = "hello"' will have "hello"
  stored on the stack.

Signed-off-by: Yonghong Song <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants