8000 Considering ptr casts -- add tests · Issue #1 · focs-lab/llvm-project · GitHub
[go: up one dir, main page]

Skip to content
Considering ptr casts -- add tests #1
Closed
@apaznikov

Description

@apaznikov

Check if it works -- add tests

Example illustrating how IntToPtr can be used to create a pointer to an escaping object, translated into English:

C Code:

int *leak_example(int x) {
  int *p = (int *)(x + 10); // Create a pointer to an address calculated from x
  return p;                // Return the pointer
}

LLVM IR:

define dso_local i32* @leak_example(i32 %x) #0 {
entry:
  %add = add nsw i32 %x, 10  // Calculate the address
  %0 = inttoptr i32 %add to i32* // Convert the integer to a pointer
  ret i32* %0                 // Return the pointer
}

In this example:

  • The function leak_example takes an integer argument x.
  • Inside the function, an address is calculated by adding x to 10.
  • The inttoptr instruction converts the resulting integer to a pointer of type i32*.
  • The function returns the created pointer.

Problem:

  • We don't know what the created pointer refers to.
  • It might refer to a valid object in memory, but it could also point to unallocated memory or memory that has already been freed.
  • Consequently, dereferencing this pointer could lead to undefined behavior.

Leakage:

  • In this example, the pointer p escapes from the function leak_example.
  • If the caller uses this pointer without checking its validity, it might result in an error.

Escape Analysis:

  • Escape analysis must consider IntToPtr instructions since they can create pointers to escaping objects.
  • In this example, escape analysis should mark the pointer returned from leak_example as escaping.

Conclusion:

  • IntToPtr is a powerful instruction that allows you to work with memory at a low level.
  • However, its use requires caution as it can lead to undefined behavior and memory leaks.
  • Escape analysis must consider IntToPtr for accurate leak analysis.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0