Skip to content

Move random_int() to util.cpp - #5555

Open
computezrmle wants to merge 5 commits into
BOINC:masterfrom
computezrmle:move_random_int_to_util
Open

Move random_int() to util.cpp#5555
computezrmle wants to merge 5 commits into
BOINC:masterfrom
computezrmle:move_random_int_to_util

Conversation

@computezrmle

Copy link
Copy Markdown
Contributor

random_int() can easily be used as seed for srand() from various functions if it is in util.cpp.
This is not possible if it is in crypt_prog.cpp since crypt_prog.cpp is not included in the standard build process.
Instead, tools/makefile_sign_executable must be used to build it.

See:
https://boinc.berkeley.edu/trac/wiki/KeySetup#Filesigningutilities

@AenBleidd

Copy link
Copy Markdown
Member

@computezrmle, please fix the failed build.
Thank you

@computezrmle

Copy link
Copy Markdown
Contributor Author

As for Release-x64-msbuild
2024-03-26T21:24:45.8536712Z D:\a\boinc\boinc\win_build\vcpkg_3rdparty_dependencies.vcxproj(182,5): error MSB3073: The command "vcpkg.exe install --x-manifest-root=D:\a\boinc\boinc\win_build\..\3rdParty\vcpkg_ports\configs\msbuild/x64 --x-install-root=D:\a\boinc\boinc\win_build\..\3rdParty\Windows\vcpkg/installed/ --overlay-ports=../../vcpkg_ports/ports --overlay-triplets=../../vcpkg_ports/triplets/ci --triplet x64-windows-static --clean-after-build" exited with code -1.
Is it caused by a missing entry in win_build\vcpkg_3rdparty_dependencies.vcxproj?
If so, I would need a hint since I'm not familiar with the syntax.

As for Release-ARM64-msbuild
2024-03-26T21:25:11.3790575Z D:\a\boinc\boinc\lib\util.cpp(552,18): error C2664: 'HINSTANCE LoadLibrary(LPCWSTR)': cannot convert argument 1 from 'const char [13]' to 'LPCWSTR' [D:\a\boinc\boinc\win_build\boincmgr.vcxproj]
Could this be a multi-byte/unicode issue?
If so, could it be solved using LoadLibrary(L"ADVAPI32.DLL")?

Comment thread lib/util.cpp Outdated
Comment thread lib/util.cpp Outdated
Comment thread lib/util.cpp Outdated
@AenBleidd

Copy link
Copy Markdown
Member

Is it caused by a missing entry in win_build\vcpkg_3rdparty_dependencies.vcxproj?

This was an unrelated issue, now fixed by build rerun.
Others are still here, so please fix them

@AenBleidd AenBleidd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are still two failing builds.
Please check them and fix.
Thank you in advance.

Comment thread lib/util.cpp
if (!hLib) {
fprintf(stderr, "Error: can't load ADVAPI32.DLL\n");
}
BOOLEAN (APIENTRY *pfn)(void*, ULONG) =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the library was not loaded correctly two lines above - this will fail with an exception

Comment thread lib/util.cpp Outdated
}
fclose(f);
#endif
return n;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What will be returned in case of an error?
We should indicate somehow that the function returns a proper result.

Comment thread lib/util.cpp Outdated

#endif

unsigned int random_int() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suggest to change signature function to something like this:
bool random_int(unsigned int& value)
And return the random value using the output parameter, and return true on success and false in case of an error

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, then you need to adjust crypt_prog.cpp with the new function signature

@davidpanderson

Copy link
Copy Markdown
Contributor

Is there a need for this outside of crypt_prog?
You can call srand() with, e.g. the time of day in microseconds.

@computezrmle

Copy link
Copy Markdown
Contributor Author

Can the recent build issues be solved without too much effort?

1st idea to get a random number was to use the standard library functions from C++11.
Looks like certain C++11 functions (including those) don't work for BOINC.

Next idea was to use rand() which needs srand(seed).
Since seed is expected to be an unsigned int random_int() from crypt_prog seemed to be a good idea as

  • it is available in the source tree
  • it delivers an unsigned int
  • it uses a "natural" random source like /dev/random.

As random_int() has build issues for the client when used from crypt_prog the next idea was to move it to lib/util which would make it globally available.
The build issues for the server it has now came unexpected.
If they can be solved, why not leave random_int() in lib/util?

Other sources for seed appear to be less "natural".
A seed base on daytime may deliver a good seed if it is not too coarse.

Can you confirm that dtime() from lib/util returns a double like 86345.123456?
I'm sure it does on Linux.
What about Windows or GCL_SIMULATOR?

If yes, this may be fine enough to cover concurrently starting processes:
seed = (unsigned int)(100000 * dtime())

or, to avoid seed becomes greater than 32-bit max unsigned int:
seed = (unsigned int)(1000 * dtime())
This would give a granularity of 0.1 ms.

Comment thread lib/util.cpp Outdated
}
FreeLibrary(hLib);
#else
FILE* f = fopen("/dev/random", "r");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
FILE* f = fopen("/dev/random", "r");
FILE* f = boinc::fopen("/dev/random", "r");

Comment thread lib/util.cpp Outdated
if (!f) {
return 2;
}
if (1 != fread(&n, sizeof(n), 1, f)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (1 != fread(&n, sizeof(n), 1, f)) {
if (1 != boinc::fread(&n, sizeof(n), 1, f)) {

Comment thread lib/util.cpp Outdated
if (1 != fread(&n, sizeof(n), 1, f)) {
return 3;
}
fclose(f);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
fclose(f);
boinc::fclose(f);

Comment thread lib/crypt_prog.cpp Outdated
die("random_int");
}
srand(srand_seed);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

run locally this command to remove trailing spaces:
python3 ci_tools/trailing_whitespaces_check.py . --fix

@AenBleidd

Copy link
Copy Markdown
Member

Is there a need for this outside of crypt_prog? You can call srand() with, e.g. the time of day in microseconds.

I think, having this as a generally available function won't hurt and could be helpful.

In addition:
Added 'return 0' at the end of 'random_int()' to mitigate a runtime error.
@codecov

codecov Bot commented Mar 28, 2024

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 10.79%. Comparing base (b2af307) to head (d60cab9).
⚠️ Report is 2810 commits behind head on master.

Files with missing lines Patch % Lines
lib/crypt_prog.cpp 0.00% 10 Missing ⚠️
lib/util.cpp 0.00% 8 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5555      +/-   ##
============================================
- Coverage     10.80%   10.79%   -0.01%     
  Complexity     1068     1068              
============================================
  Files           279      279              
  Lines         36294    36303       +9     
  Branches       8409     8412       +3     
============================================
  Hits           3920     3920              
- Misses        31980    31989       +9     
  Partials        394      394              
Files with missing lines Coverage Δ
lib/util.h 40.00% <ø> (ø)
lib/util.cpp 32.50% <0.00%> (-2.33%) ⬇️
lib/crypt_prog.cpp 0.00% <0.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@AenBleidd AenBleidd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me.
@davidpanderson, if you are ok with this - feel free to merge it

@davidpanderson

Copy link
Copy Markdown
Contributor

Sorry, but I don't see a use case for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Reviewer approved

Development

Successfully merging this pull request may close these issues.

3 participants