Skip to content

Question: Without the cast operation, malloc cannot be correctly identified. #419

@sybs5968

Description

@sybs5968
void Cast1() {
    void *mem = malloc(100);
    char *arr = (char *)mem;
    arr[0] = '%'; arr[1] = '#'; arr[2] = '\0';
    printf("%s\n" , arr);
    free(mem);
}

/*
 func.func @Cast1() attributes {llvm.linkage = #llvm.linkage<external>} {
    %c0_i8 = arith.constant 0 : i8
    %c35_i8 = arith.constant 35 : i8
    %c37_i8 = arith.constant 37 : i8
    %c100_i64 = arith.constant 100 : i64
    %0 = call @malloc(%c100_i64) : (i64) -> memref<?xi8>
    affine.store %c37_i8, %0[0] : memref<?xi8>
    affine.store %c35_i8, %0[1] : memref<?xi8>
    affine.store %c0_i8, %0[2] : memref<?xi8>
    %1 = llvm.mlir.addressof @str0 : !llvm.ptr
    %2 = llvm.getelementptr %1[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<4 x i8>
    %3 = "polygeist.memref2pointer"(%0) : (memref<?xi8>) -> !llvm.ptr
    %4 = llvm.call @printf(%2, %3) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, !llvm.ptr) -> i32
    memref.dealloc %0 : memref<?xi8>
    return
  }
  func.func private @malloc(i64) -> memref<?xi8> attributes {llvm.linkage = #llvm.linkage<external>}
*/

I noticed that your malloc conversion is done in VisitCastExpr, so you can only recognize malloc as a custom function if you have a special case like above.

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