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

[ICF] Compare alloca position for testing equivalence #173

Merged
merged 1 commit into from
Nov 3, 2023

Conversation

Hyxogen
Copy link
Contributor

@Hyxogen Hyxogen commented Nov 3, 2023

This fixes an incorrect detection of identical code involving alloca ordering.

Previously for allocas, we only checked if the allocated type, operands and alignment were the same. However this doesn't take into account that two identical alloca instructions at different relative points in the function can not be taken for the same.

Fixed by adding a map that keeps track of allocas that take place at relatively the same position, and using that map to check alloca equivalence.

An example of miscompiled code (compiled with -O0):

#include <cassert>

void Fill(int& a, int& b) {
	a = 1;
	b = 2;
}

int Foo() {
	int a = 0;
	int b = 0;
	Fill(a, b);
	return a;
}

int Bar() {
	int a = 0;
	int b = 0;
	Fill(a, b);
	return b;
}

int main() {
	assert(Foo() == 1);
	assert(Bar() == 2);
}

This would generate the following IR before ICF for the functions Foo and Bar:

; Function Attrs: mustprogress nofree noinline norecurse nosync nounwind willreturn memory(none)
define internal fastcc noundef i32 @_Z3Foov() unnamed_addr #1 section "asmjs" {
entry:
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  store i32 0, i32* %a, align 4
  call fastcc void @_Z4FillRiS_(i32* noundef nonnull align 4 dereferenceable(4) %a, i32* noundef nonnull align 4 dereferenceable(4) %b)
  %0 = load i32, i32* %a, align 4
  ret i32 %0
}

; Function Attrs: mustprogress nofree noinline norecurse nosync nounwind willreturn memory(none)
define internal fastcc noundef i32 @_Z3Barv() unnamed_addr #1 section "asmjs" {
entry:
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  store i32 0, i32* %b, align 4
  call fastcc void @_Z4FillRiS_(i32* noundef nonnull align 4 dereferenceable(4) %a, i32* noundef nonnull align 4 dereferenceable(4) %b)
  %0 = load i32, i32* %b, align 4
  ret i32 %0
}

The instructions:

  %0 = load i32, i32* %a, align 4
  %0 = load i32, i32* %b, align 4

Would be taken for the same, resulting in a miscompile.

@Hyxogen
Copy link
Contributor Author

Hyxogen commented Nov 3, 2023

Currently still running my asan tests, will mark for review once all those pass

This fixes an incorrect detection of identical code involving alloca
ordering.

Previously for allocas, we only checked if the allocated type, operands
and alignment were the same. However this doesn't take into account that
two identical alloca instructions at different relative points in the
function can not be taken for the same.

Fixed by adding a map that keeps track of allocas that take place at
relatively the same position, and using that map to check alloca
equivalence.
@Hyxogen Hyxogen force-pushed the fix-icf-alloca-compare branch from 98d66a7 to 88388b5 Compare November 3, 2023 11:53
@yuri91 yuri91 marked this pull request as ready for review November 3, 2023 14:25
@Hyxogen
Copy link
Contributor Author

Hyxogen commented Nov 3, 2023

All my asan tests pass, feel free to merge @yuri91

@yuri91 yuri91 merged commit f101b4e into master Nov 3, 2023
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.

2 participants