Skip to content

Conversation

@ampandey-AMD
Copy link

@ampandey-AMD ampandey-AMD commented Nov 3, 2025

  • Use reinterpret_cast for pointer arithmetic.
  • Add sanitizer interception logic for api 'hsa_amd_pointer_info'.
  • Allow only valid values of ptr and len in non-ASan mode.
    • ptr == Actual agentBaseAddress && len ==
      original_len_used_in_alloc
  • Allow only valid values of ptr and len in ASan mode. pinfo resembles
    to pointer info extracted in GetBlockBegin function.
    • ptr == pinfo.agentBaseAddress && len == pinfo.sizeInBytes
    • ptr == original_ptr_returned_by_ASAN && len ==
      original_len_used_in_alloc

@z1-cciauto
Copy link
Collaborator

@ampandey-AMD ampandey-AMD force-pushed the fix-swdev-563823-ipc-handles branch from 971d852 to 0c173eb Compare November 3, 2025 16:32
@z1-cciauto
Copy link
Collaborator

@ampandey-AMD ampandey-AMD force-pushed the fix-swdev-563823-ipc-handles branch from 0c173eb to c1765c0 Compare November 3, 2025 16:40
@z1-cciauto
Copy link
Collaborator

@b-sumner
Copy link

b-sumner commented Nov 3, 2025

Does the test application in the ticket print the same "base" "len" and "end" as the uninstrumented version (taking into account address randomization)?

@ampandey-AMD
Copy link
Author

Does the test application in the ticket print the same "base" "len" and "end" as the uninstrumented version (taking into account address randomization)?

Yes. If we include the current fix with/without intercepting hsa_amd_pointer_info.

Non-Instrumented O/P

hsa_amd_pointer_info: test_buff 0x7c947a815000 base 0x7c947a814000 len 16384 end 0x7c947a818000
hsa_amd_ipc_memory_create: handle: [7a814000][7c94][395301][0][4][0][0][5325e42b]

Instrumented O/P

hsa_amd_pointer_info: test_buff 0x7ef7c9509000 base 0x7ef7c9508000 len 16384 end 0x7ef7c950c000
hsa_amd_ipc_memory_create: handle: [c9508000][7ef7][394f4c][0][4][0][0][3a761d78]

@b-sumner
Copy link

b-sumner commented Nov 4, 2025

But the output in the ticket, when -fsanitize=address is not used is:

hsa_amd_pointer_info: test_buff 0x7f3df2400000 base 0x7f3df2400000 len 1024 end 0x7f3df2400400

Here the buffer and the base are the same, and len is 1024. Shouldn't our intercepted version of hsa_amd_pointer_info return the same buffer and base, and a len of 1024 instead of 16384?

@ampandey-AMD
Copy link
Author

ampandey-AMD commented Nov 4, 2025

But the output in the ticket, when -fsanitize=address is not used is:

hsa_amd_pointer_info: test_buff 0x7f3df2400000 base 0x7f3df2400000 len 1024 end 0x7f3df2400400

Here the buffer and the base are the same, and len is 1024. Shouldn't our intercepted version of hsa_amd_pointer_info return the same buffer and base, and a len of 1024 instead of 16384?

Hi @b-sumner , Ok in my previous o/p, the problem was because of LD_LIBRARY_PATH set to asan instrumented libraries and I compiled non-instrumented o/p without flags -fsanitize=address -shared-libsan. So my executable was actually linked to asan instrumented libraries of rocr and hipamd.

If I avoid adding the /opt/rocm/llvm/lib/asan or /opt/rocm/lib/asan from my LD_LIBRARY_PATH, then I am getting this non-instrumented O/P

hsa_amd_pointer_info: test_buff 0x7b6a28800000 base 0x7b6a28800000 len 1024 end 0x7b6a28800400
hsa_amd_ipc_memory_create: handle: [28800000][7b6a][1feb9][0][4][0][80000000][3d9ec7b6]

I think instrumentation here is not kind of problem, we enable the asan interception of those hsa_amd* api calls in the asanified libraries only as our code is bounded by preprocessor macro SANITIZER_AMDGPU. We actually don't try to instrument the hsa_api* calls itself.

@ampandey-AMD
Copy link
Author

Hi @b-sumner,
So I tested locally by removing the asan interception code of hsa_amd_pointer_info.

Here are the results without asan_hsa_amd_pointer_info implemented.

Instrumented (ASan enabled via -fsanitize=address -shared-libsan with LD_LIBRARY_PATH set to path/to/lib/asan)

hsa_amd_pointer_info: test_buff 0x75863cea5000 base 0x75863cea4000 len 16384 end 0x75863cea8000
hsa_amd_ipc_memory_create: handle: [3cea4000][7586][22d9b][0][4][0][0][51fdf52f]

Non-Instrumented (ASan disabled with LD_LIBRARY_PATH not set to /path/to/lib/asan)

hsa_amd_pointer_info: test_buff 0x74bd90c00000 base 0x74bd90c00000 len 1024 end 0x74bd90c00400
hsa_amd_ipc_memory_create: handle: [90c00000][74bd][22cd4][0][4][0][80000000][3a14da89]

@b-sumner
Copy link

b-sumner commented Nov 4, 2025

Thanks! My thinking is that in order to provide acceptable information to our interception of hsa_amd_ipc_memory_create, our interception of hsa_amd_pointer_info should return the same base, len, and end as observed when the application is not built with instrumentation (or running instrumented libraries). But I would like to hear @bing-ma's take on this.

@ampandey-AMD ampandey-AMD force-pushed the fix-swdev-563823-ipc-handles branch from c1765c0 to 0bb451f Compare November 5, 2025 13:11
@z1-cciauto
Copy link
Collaborator

@ampandey-AMD ampandey-AMD force-pushed the fix-swdev-563823-ipc-handles branch from 0bb451f to 9bc5e55 Compare November 5, 2025 13:39
@z1-cciauto
Copy link
Collaborator

@ampandey-AMD ampandey-AMD changed the title [SWDEV-563823][Compiler-rt][ASan] Intercept API 'hsa_amd_pointer_info'. [SWDEV-563823][Compiler-rt][ASan] Simplify API Logic 'asan_hsa_amd_ipc_memory_create'. Nov 5, 2025
@ampandey-AMD ampandey-AMD force-pushed the fix-swdev-563823-ipc-handles branch from 9bc5e55 to 339e178 Compare November 5, 2025 13:44
@z1-cciauto
Copy link
Collaborator

'asan_hsa_amd_ipc_memory_create'.

 - Use reinterpret_cast<uptr> for pointer arithmetic.
 - Add sanitizer interception logic for api 'hsa_amd_pointer_info'.
 - Allow only valid values of ptr and len in non-ASan mode.
    - ptr == Actual agentBaseAddress && len ==
      original_len_used_in_alloc
 - Allow only valid values of ptr and len in ASan mode. pinfo resembles
   to pointer info extracted in GetBlockBegin function.
    - ptr == pinfo.agentBaseAddress && len == pinfo.sizeInBytes
    - ptr == original_ptr_returned_by_ASAN && len ==
      original_len_used_in_alloc
@z1-cciauto
Copy link
Collaborator

Improve logic of asan_hsa_amd_ipc_memory_create based on observations.
@ampandey-AMD ampandey-AMD force-pushed the fix-swdev-563823-ipc-handles branch from 69a471e to ee235e3 Compare November 6, 2025 06:42
@z1-cciauto
Copy link
Collaborator

suggestions.

This commit again adds the interception of 'hsa_amd_pointer_info' call.
@ampandey-AMD ampandey-AMD force-pushed the fix-swdev-563823-ipc-handles branch from ee235e3 to 8681926 Compare November 6, 2025 06:44
@z1-cciauto
Copy link
Collaborator

@z1-cciauto
Copy link
Collaborator

@ampandey-AMD ampandey-AMD force-pushed the fix-swdev-563823-ipc-handles branch from afae2e3 to 619e575 Compare November 7, 2025 04:31
@z1-cciauto
Copy link
Collaborator

uint32_t* num_agents_accessible,
hsa_agent_t** accessible) {
void* ptr_ = get_allocator().GetBlockBegin(ptr);
AsanChunk* m = instance.GetAsanChunkByAddr(reinterpret_cast<uptr>(ptr_));
Copy link

Choose a reason for hiding this comment

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

Do we not need to guard against null ptr_ here like we do in ipc create?

Copy link
Author

@ampandey-AMD ampandey-AMD Nov 8, 2025

Choose a reason for hiding this comment

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

If ptr_ == nullptr then if condition at line number 1559 will fail.

Then if condition at line number 1562 for first condition status == HSA_STATUS_SUCCESS would fail ambiguously as status is not getting set up with default value. I think we need to initialize this with status=HSA_STATUS_ERROR_NOT_INITIALIZED. Is that ok @b-sumner && @bing-ma?

Copy link
Author

Choose a reason for hiding this comment

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

I default initialized status with value HSA_STATUS_ERROR_NOT_INITIALIZED in 00898ca.

  - Changes to 'asan_hsa_amd_pointer_info'
    - Initialize status with default value
      'HSA_STATUS_ERROR_NOT_INITIALIZE'.
@z1-cciauto
Copy link
Collaborator

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.

3 participants