| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | brk.c |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file is the brk test. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #include "lxtcommon.h" |
| 16 | #include "unittests.h" |
| 17 | #include <unistd.h> |
| 18 | #include <stdio.h> |
| 19 | |
| 20 | #define LXT_NAME "brk" |
| 21 | |
| 22 | int BrkTest(PLXT_ARGS Args); |
| 23 | |
| 24 | // |
| 25 | // Global constants |
| 26 | // |
| 27 | |
| 28 | static const LXT_VARIATION g_LxtVariations[] = { |
| 29 | {"Brk Test", BrkTest}, |
| 30 | }; |
| 31 | |
| 32 | int BrkTestEntry(int Argc, char* Argv[]) |
| 33 | |
| 34 | /*++ |
| 35 | --*/ |
| 36 | |
| 37 | { |
| 38 | |
| 39 | LXT_ARGS Args; |
| 40 | int Result; |
| 41 | |
| 42 | LxtCheckResult(LxtInitialize(Argc, Argv, &Args, LXT_NAME)); |
| 43 | LxtCheckResult(LxtRunVariations(&Args, g_LxtVariations, LXT_COUNT_OF(g_LxtVariations))); |
| 44 | |
| 45 | ErrorExit: |
| 46 | LxtUninitialize(); |
| 47 | return !LXT_SUCCESS(Result); |
| 48 | } |
| 49 | |
| 50 | int BrkTest(PLXT_ARGS Args) |
| 51 | |
| 52 | /*++ |
| 53 | --*/ |
| 54 | |
| 55 | { |
| 56 | |
| 57 | int Result; |
| 58 | int Status; |
| 59 | char* BreakAddress; |
| 60 | char* NewBreakAddress; |
| 61 | int PageSize; |
| 62 | |
| 63 | // |
| 64 | // Get current brk address. |
| 65 | // |
| 66 | |
| 67 | LxtLogInfo("Getting current break address"); |
| 68 | BreakAddress = (char*)sbrk(0); |
| 69 | LxtLogInfo("Current break address is 0x%p", BreakAddress); |
| 70 | |
| 71 | // |
| 72 | // Increase the brk address. |
| 73 | // |
| 74 | |
| 75 | PageSize = 4096; |
| 76 | BreakAddress += PageSize; |
| 77 | Status = brk(BreakAddress); |
| 78 | if (Status != 0) |
| 79 | { |
| 80 | LxtLogError("Brk call to increase the address failed"); |
| 81 | Result = LXT_RESULT_FAILURE; |
| 82 | goto ErrorExit; |
| 83 | } |
| 84 | |
| 85 | NewBreakAddress = (char*)sbrk(0); |
| 86 | LxtLogInfo("New break address 0x%p", NewBreakAddress); |
| 87 | if ((NewBreakAddress < BreakAddress) || (NewBreakAddress > (BreakAddress + PageSize))) |
| 88 | { |
| 89 | LxtLogError( |
| 90 | "The returned brk address does not match the expected \ |
| 91 | break address"); |
| 92 | |
| 93 | Result = LXT_RESULT_FAILURE; |
| 94 | goto ErrorExit; |
| 95 | } |
| 96 | LxtLogInfo("New Break address set!"); |
| 97 | |
| 98 | // |
| 99 | // Decrease the break address. |
| 100 | // |
| 101 | |
| 102 | BreakAddress = (NewBreakAddress - PageSize); |
| 103 | LxtLogInfo("Decreasing the break address by a page"); |
| 104 | Status = brk(BreakAddress); |
| 105 | if (Status != 0) |
| 106 | { |
| 107 | LxtLogError("Brk call to decrease the address failed"); |
| 108 | Result = LXT_RESULT_FAILURE; |
| 109 | goto ErrorExit; |
| 110 | } |
| 111 | |
| 112 | NewBreakAddress = (char*)sbrk(0); |
| 113 | if (NewBreakAddress != BreakAddress) |
| 114 | { |
| 115 | LxtLogError( |
| 116 | "The returned brk address after decreasing did not match\ |
| 117 | the expected break address"); |
| 118 | |
| 119 | Result = LXT_RESULT_FAILURE; |
| 120 | goto ErrorExit; |
| 121 | } |
| 122 | |
| 123 | Result = LXT_RESULT_SUCCESS; |
| 124 | |
| 125 | ErrorExit: |
| 126 | return Result; |
| 127 | } |