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

libfuzzer forces use of popcnt instruction in its code #52893

Open
theultramage opened this issue Dec 27, 2021 · 1 comment
Open

libfuzzer forces use of popcnt instruction in its code #52893

theultramage opened this issue Dec 27, 2021 · 1 comment

Comments

@theultramage
Copy link

theultramage commented Dec 27, 2021

FuzzerPlatform.h defines ATTRIBUTE_TARGET_POPCNT as a shorthand for __attribute__((target("popcnt"))). This applies an override to the global build options, letting the compiler use the (SSE4?) 'popcnt' instruction for faster execution of certain bit code. It is currently only being used in FuzzerTracePC.cpp on 13 functions.

The logic apparently being applied to ATTRIBUTE_TARGET_POPCNT is "if it's x86_64, it has popcnt". This is not true for various older processors. Forcing use of this instruction causes the resulting binary libclang_rt.fuzzer-x86_64.a to crash with SIGILL when executing any of the flagged functions on processors that don't support popcnt.

This was reported on the freebsd bugtracker in august 2021, after mysql80-server-8.0.26 was released and integrated into the ports tree. The relevant mysql change happened in february 2021 but wasn't put into a release until later. I have tried checking the now deleted svn history for libFuzzer and so far found llvm-svn: 279601 which shows that these overrides were already there in 2016. I could not find the changeset that initially added them since that site cannot access the repo.

It is not obvious why this code even exists. The popcnt flag should already be getting automatically set/unset based on the build's target architecture, so at best it's a no-op. Is it possible that the original author misunderstood the usage of this attribute, and thought it acted as a hint to the optimizer, and that it would be smart about only applying the optimization for eligible targets? (Does it actually also act as a hint?)

@DimitryAndric
Copy link
Collaborator

  • 9eb170f (r300999 by @kubamracek) introduced the check whether attribute((target("popcnt"))) was even supported (for older compilers)
  • bceadcf (r279601 by @kcc) added the attribute only for x86_64 (even though this isn't strictly true and __POPCNT__ should also have been checked)
  • 5a5d554 (r279002 by @kcc) added the attribute unconditionally
  • d46a59f (r278839 by @kcc) originally added a -mpopcnt compile flag

In all cases, I think the intent was that the popcnt instruction should be more efficient, but I don't know why it wasn't simply left to __builtin_popcnt to decide how to emit it...

@mkurdej mkurdej removed the new issue label Dec 28, 2021
freebsd-git pushed a commit to freebsd/freebsd-src that referenced this issue Dec 30, 2021
Since popcnt is only supported by CPUTYPE=nehalem and later, ensure that
this instruction is only emitted when appropriate. Otherwise, programs
using the library can abort with SIGILL.

See also: llvm/llvm-project#52893

PR:		258156
Reported by:	Eric Rucker <bhtooefr@bhtooefr.org>
MFC after:	3 days
freebsd-git pushed a commit to freebsd/freebsd-src that referenced this issue Jan 2, 2022
Since popcnt is only supported by CPUTYPE=nehalem and later, ensure that
this instruction is only emitted when appropriate. Otherwise, programs
using the library can abort with SIGILL.

See also: llvm/llvm-project#52893

PR:		258156
Reported by:	Eric Rucker <bhtooefr@bhtooefr.org>
MFC after:	3 days

(cherry picked from commit 1331805)
freebsd-git pushed a commit to freebsd/freebsd-src that referenced this issue Jan 2, 2022
Since popcnt is only supported by CPUTYPE=nehalem and later, ensure that
this instruction is only emitted when appropriate. Otherwise, programs
using the library can abort with SIGILL.

See also: llvm/llvm-project#52893

PR:		258156
Reported by:	Eric Rucker <bhtooefr@bhtooefr.org>
MFC after:	3 days

(cherry picked from commit 1331805)
bsdjhb pushed a commit to bsdjhb/cheribsd that referenced this issue Jan 25, 2022
Since popcnt is only supported by CPUTYPE=nehalem and later, ensure that
this instruction is only emitted when appropriate. Otherwise, programs
using the library can abort with SIGILL.

See also: llvm/llvm-project#52893

PR:		258156
Reported by:	Eric Rucker <bhtooefr@bhtooefr.org>
MFC after:	3 days
netgate-git-updates pushed a commit to pfsense/FreeBSD-src that referenced this issue Mar 15, 2022
Since popcnt is only supported by CPUTYPE=nehalem and later, ensure that
this instruction is only emitted when appropriate. Otherwise, programs
using the library can abort with SIGILL.

See also: llvm/llvm-project#52893

PR:		258156
Reported by:	Eric Rucker <bhtooefr@bhtooefr.org>
MFC after:	3 days

(cherry picked from commit 1331805)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants