Vector35 / Vector35/binaryninja-api

Check `_WIN32` instead of `WIN32` definition in C++ code

Open
#7,546 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: API Effort: Trivial
Dominant language
C++
Stars
1.3k
Forks
298
Avg merge
5d 5h
Merged PRs (30d)
19

Description

Bug Description:

The C++ API code is checking WIN32 instead of _WIN32, which causes compilation failure and some confusion when the user uses their own CMAKE_CXX_FLAGS (without redefining WIN32) during CMake configuration on Windows systems.

Steps To Reproduce:

Use your own CMAKE_CXX_FLAGS without defining /DWIN32

cmake -B build -S . -DBN_ALLOW_STUBS=ON -DCMAKE_CXX_FLAGS="/permissive-"
cmake --build build --config Release

and you'll eventually see an error from function.cpp

  function.cpp
C:\Users\ekilmer\src\binaryninja-api\function.cpp(2500,2): error C3861: '__sync_fetch_and_add': identifier not found [C:\Users\ekil
mer\src\binaryninja-api\build\binaryninjaapi.vcxproj]
C:\Users\ekilmer\src\binaryninja-api\function.cpp(2511,2): error C3861: '__sync_fetch_and_add': identifier not found [C:\Users\ekil
mer\src\binaryninja-api\build\binaryninjaapi.vcxproj]

which is this snippet

https://github.com/Vector35/binaryninja-api/blob/5d9fa6553036f9d0e5216948585f4e7dcde3fcb7/function.cpp#L2497-L2501

and when compiling for Windows, I would expect line 2500 to be removed, except the #ifdef WIN32 doesn't work because I've overwritten CMAKE_CXX_FLAGS.

Expected Behavior:

Compilation to succeed by using internally defined compiler definitions like _WIN32 or others here https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170

Additional Information:

CMake is to blame here for defining WIN32 by default in the CMAKE_CXX_FLAGS (check your CMakeCache.txt in the build directory with a default cmake -B build -S .).

While this is easy to resolve on the user's end (just add /DWIN32 to your flags), it was a little confusing to track down at first.

Also, in the case of developing third-party plugins, not using the built-in _WIN32 can force some additional code when using the API as a submodule and wanting to enable more warnings on only the plugin's code instead of the entire API. Maybe there's a better way (I know this isn't perfect) to do this for C++ plugins, but I've done the following in a external/CMakeLists.txt file:

# Clear all of our warning flags for external dependencies
set(CMAKE_CXX_FLAGS "")

# Make the binaryninja-api code available
set(HEADLESS ON)
add_subdirectory(binaryninja-api EXCLUDE_FROM_ALL SYSTEM)

... I could also create a CMake interface target for all of my compilation warnings to link to my plugin target(s) to work around this as well, but I figured I'd try opening this issue to attempt changing things here instead, which might also prevent future folks from running into this issue.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Inspect function.cpp around lines 2497-2511 and reproduce the reported CMake configure/build commands with CMAKE_CXX_FLAGS=/permissive-. Verify the platform guard uses the compiler-defined Windows macro rather than the CMake-provided definition, then rerun the Release build and confirm function.cpp compiles without __sync_fetch_and_add errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp
Domain
build-system, operating-systems
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.